Skip to content

Commit

Permalink
PI-2120: Use spoStaffCode for CADE staffid when present in Event Allo… (
Browse files Browse the repository at this point in the history
#3835)

PI-2120: Use spoStaffCode for CADE staffid when present in Event Allocation
  • Loading branch information
pmcphee77 authored May 24, 2024
1 parent bb68878 commit 2cbde2c
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,8 @@ class AllocationsDataLoader(
staffUserRepository.save(StaffGenerator.STAFF_WITH_USER.user!!)
staffRepository.save(StaffGenerator.INACTIVE_STAFF)
staffUserRepository.save(StaffGenerator.INACTIVE_STAFF.user!!)
staffRepository.save(StaffGenerator.SPO_STAFF)
staffUserRepository.save(StaffGenerator.SPO_STAFF.user!!)

personAllocationDataLoader.loadData()
caseViewDataLoader.loadData()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,13 @@ object StaffGenerator {
endDate = ZonedDateTime.now().minusDays(7)
)

val SPO_STAFF = generateStaffWithUser(
"${TeamGenerator.ALLOCATION_TEAM.code}X",
"Joe",
"Bloggs",
listOf(TeamGenerator.ALLOCATION_TEAM)
)

fun generateStaffWithUser(
code: String,
forename: String = "Test",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"createdBy": "Smith, Bob",
"createdDate": "2022-07-28T10:30:54.023+01:00",
"eventNumber": 2,
"spoStaffCode": "N02ABSX",
"allocationJustificationNotes": "CADE notes",
"sensitiveNotes": true
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import org.springframework.boot.test.mock.mockito.MockBean
import org.springframework.data.repository.findByIdOrNull
import uk.gov.justice.digital.hmpps.data.generator.EventGenerator
import uk.gov.justice.digital.hmpps.data.generator.OrderManagerGenerator
import uk.gov.justice.digital.hmpps.data.generator.StaffGenerator
import uk.gov.justice.digital.hmpps.integrations.delius.contact.ContactRepository
import uk.gov.justice.digital.hmpps.integrations.delius.contact.ContactTypeCode
import uk.gov.justice.digital.hmpps.integrations.delius.event.Event
Expand Down Expand Up @@ -48,7 +49,7 @@ class AllocateEventIntegrationTest {
private lateinit var contactRepository: ContactRepository

@Test
fun `allocate new order manager`() {
fun `allocate new order manager spoStaffCode present`() {
val event = EventGenerator.NEW
val existingManager = OrderManagerGenerator.NEW

Expand All @@ -57,7 +58,8 @@ class AllocateEventIntegrationTest {
"new-event-allocation-body",
existingManager,
event,
1
1,
StaffGenerator.SPO_STAFF.id
)

verify(telemetryService).trackEvent(
Expand All @@ -73,7 +75,7 @@ class AllocateEventIntegrationTest {
}

@Test
fun `allocate historic order manager`() {
fun `allocate historic order manager with no spoStaffCode present`() {
val event = EventGenerator.HISTORIC

val firstOm = orderManagerRepository.save(
Expand All @@ -93,7 +95,8 @@ class AllocateEventIntegrationTest {
"historic-event-allocation-body",
firstOm,
event,
2
2,
StaffGenerator.STAFF_WITH_USER.id
)

val insertedPm = orderManagerRepository.findActiveManagerAtDate(event.id, ZonedDateTime.now().minusDays(2))
Expand All @@ -116,7 +119,8 @@ class AllocateEventIntegrationTest {
jsonFile: String,
existingOm: OrderManager,
event: Event,
originalOmCount: Int
originalOmCount: Int,
staffId: Long
) {
val allocationEvent = prepMessage(messageName, wireMockServer.port())
channelManager.getChannel(queueName).publishAndWait(allocationEvent)
Expand All @@ -134,6 +138,8 @@ class AllocateEventIntegrationTest {

assertNotNull(cadeContact)

assertThat(cadeContact!!.staffId, equalTo(staffId))

assertThat(
cadeContact!!.isSensitive,
equalTo((allocationDetail as AllocationDetail.EventAllocation).sensitive)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class ChoosePractitionerIntegrationTest {
.andExpect(jsonPath("$.communityPersonManager.teamCode").value("N02UAT"))
.andExpect(jsonPath("$.teams.keys()").value(setOf(team1, team2)))
.andExpect(jsonPath("$.teams.$team1.size()").value(1))
.andExpect(jsonPath("$.teams.$team2.size()").value(1))
.andExpect(jsonPath("$.teams.$team2.size()").value(2))
.andExpect(jsonPath("$.teams.$team2[0].name.forename").value("Joe"))
.andExpect(jsonPath("$.teams.$team2[0].name.surname").value("Bloggs"))
.andExpect(jsonPath("$.teams.$team2[0].email").value("[email protected]"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ sealed interface AllocationDetail {
override val code: String = "INT",
@JsonAlias("allocationJustificationNotes")
val notes: String?,
val spoStaffCode: String?,
@JsonAlias("sensitiveNotes")
val sensitive: Boolean = false
) : AllocationDetail
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import uk.gov.justice.digital.hmpps.integrations.delius.audit.BusinessInteractio
import uk.gov.justice.digital.hmpps.integrations.delius.contact.*
import uk.gov.justice.digital.hmpps.integrations.delius.event.*
import uk.gov.justice.digital.hmpps.integrations.delius.event.TransferReasonCode.CASE_ORDER
import uk.gov.justice.digital.hmpps.integrations.delius.provider.Staff
import uk.gov.justice.digital.hmpps.integrations.delius.provider.StaffRepository
import uk.gov.justice.digital.hmpps.integrations.workforceallocations.AllocationDetail.EventAllocation

@Service
Expand All @@ -24,7 +26,8 @@ class AllocateEventService(
private val contactTypeRepository: ContactTypeRepository,
private val contactRepository: ContactRepository,
private val transferReasonRepository: TransferReasonRepository,
private val optimisationTables: OptimisationTables
private val optimisationTables: OptimisationTables,
private val staffRepository: StaffRepository
) : ManagerService<OrderManager>(auditedInteractionService, orderManagerRepository) {

@Transactional
Expand Down Expand Up @@ -63,6 +66,10 @@ class AllocateEventService(
val transferReason = transferReasonRepository.findByCode(CASE_ORDER.value)
?: throw NotFoundException("Transfer Reason", "code", CASE_ORDER.value)

val spoStaff = if (allocationDetail.spoStaffCode != null)
staffRepository.findByCode(allocationDetail.spoStaffCode)
?: throw NotFoundException("SPO Staff", "code", allocationDetail.spoStaffCode) else null

val newOrderManager = OrderManager(eventId = event.id, transferReasonId = transferReason.id).apply {
populate(allocationDetail.createdDate, ts, activeOrderManager)
}
Expand All @@ -81,14 +88,19 @@ class AllocateEventService(
)
)

createCadeContact(allocationDetail, event, newOrderManager)
createCadeContact(allocationDetail, event, newOrderManager, spoStaff)

if (event.hasAccreditedProgrammeRequirement()) {
eventRepository.updateIaps(event.id)
}
}

fun createCadeContact(allocationDetail: EventAllocation, event: Event, orderManager: OrderManager) {
fun createCadeContact(
allocationDetail: EventAllocation,
event: Event,
orderManager: OrderManager,
spoStaff: Staff?
) {
contactRepository.save(
Contact(
type = contactTypeRepository.findByCodeOrThrow(ContactTypeCode.CASE_ALLOCATION_DECISION_EVIDENCE.value),
Expand All @@ -97,7 +109,7 @@ class AllocateEventService(
date = orderManager.startDate.toLocalDate(),
startTime = orderManager.startDate,
teamId = orderManager.team.id,
staffId = orderManager.staff.id,
staffId = spoStaff?.id ?: orderManager.staff.id,
providerId = orderManager.provider.id,
notes = allocationDetail.notes,
isSensitive = allocationDetail.sensitive
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import uk.gov.justice.digital.hmpps.audit.service.OptimisationTables
import uk.gov.justice.digital.hmpps.data.generator.EventGenerator
import uk.gov.justice.digital.hmpps.data.generator.OrderManagerGenerator
import uk.gov.justice.digital.hmpps.data.generator.PersonGenerator
import uk.gov.justice.digital.hmpps.data.generator.TransferReasonGenerator
import uk.gov.justice.digital.hmpps.exception.IgnorableMessageException
import uk.gov.justice.digital.hmpps.exception.NotActiveException
import uk.gov.justice.digital.hmpps.exception.NotFoundException
Expand All @@ -28,6 +29,7 @@ import uk.gov.justice.digital.hmpps.integrations.delius.event.EventRepository
import uk.gov.justice.digital.hmpps.integrations.delius.event.OrderManagerRepository
import uk.gov.justice.digital.hmpps.integrations.delius.event.TransferReasonCode
import uk.gov.justice.digital.hmpps.integrations.delius.event.TransferReasonRepository
import uk.gov.justice.digital.hmpps.integrations.delius.provider.StaffRepository
import uk.gov.justice.digital.hmpps.integrations.workforceallocations.AllocationDetail.EventAllocation
import uk.gov.justice.digital.hmpps.resourceloader.ResourceLoader

Expand Down Expand Up @@ -58,6 +60,9 @@ internal class AllocateEventServiceTest {
@Mock
private lateinit var optimisationTables: OptimisationTables

@Mock
private lateinit var staffRepository: StaffRepository

@InjectMocks
private lateinit var allocateEventService: AllocateEventService

Expand Down Expand Up @@ -185,4 +190,29 @@ internal class AllocateEventServiceTest {
)
}
}

@Test
fun `when spo staff code not found exception thrown`() {
val event = EventGenerator.generate()
val spoStaffCode = "NOTFOUND"
whenever(
eventRepository.findByPersonCrnAndNumberAndSoftDeletedFalse(
PersonGenerator.DEFAULT.crn,
allocationDetail.eventNumber.toString()
)
).thenReturn(event)
whenever(orderManagerRepository.findActiveManagerAtDate(event.id, allocationDetail.createdDate))
.thenReturn(OrderManagerGenerator.DEFAULT)
whenever(eventRepository.countPendingTransfers(event.id)).thenReturn(0)
whenever(transferReasonRepository.findByCode(TransferReasonCode.CASE_ORDER.value)).thenReturn(
TransferReasonGenerator.CASE_ORDER
)
whenever(staffRepository.findByCode(spoStaffCode)).thenReturn(null)
assertThrows<NotFoundException> {
allocateEventService.createEventAllocation(
PersonGenerator.DEFAULT.crn,
allocationDetail.copy(spoStaffCode = spoStaffCode)
)
}
}
}

0 comments on commit 2cbde2c

Please sign in to comment.