diff --git a/projects/workforce-allocations-to-delius/src/dev/kotlin/uk/gov/justice/digital/hmpps/data/AllocationsDataLoader.kt b/projects/workforce-allocations-to-delius/src/dev/kotlin/uk/gov/justice/digital/hmpps/data/AllocationsDataLoader.kt index c32bf735ea..eb8eb085c8 100644 --- a/projects/workforce-allocations-to-delius/src/dev/kotlin/uk/gov/justice/digital/hmpps/data/AllocationsDataLoader.kt +++ b/projects/workforce-allocations-to-delius/src/dev/kotlin/uk/gov/justice/digital/hmpps/data/AllocationsDataLoader.kt @@ -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() diff --git a/projects/workforce-allocations-to-delius/src/dev/kotlin/uk/gov/justice/digital/hmpps/data/generator/StaffGenerator.kt b/projects/workforce-allocations-to-delius/src/dev/kotlin/uk/gov/justice/digital/hmpps/data/generator/StaffGenerator.kt index 3da9525543..99fab87886 100644 --- a/projects/workforce-allocations-to-delius/src/dev/kotlin/uk/gov/justice/digital/hmpps/data/generator/StaffGenerator.kt +++ b/projects/workforce-allocations-to-delius/src/dev/kotlin/uk/gov/justice/digital/hmpps/data/generator/StaffGenerator.kt @@ -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", diff --git a/projects/workforce-allocations-to-delius/src/dev/resources/simulations/__files/new-event-allocation-body.json b/projects/workforce-allocations-to-delius/src/dev/resources/simulations/__files/new-event-allocation-body.json index f90451f721..18f1c17bf8 100644 --- a/projects/workforce-allocations-to-delius/src/dev/resources/simulations/__files/new-event-allocation-body.json +++ b/projects/workforce-allocations-to-delius/src/dev/resources/simulations/__files/new-event-allocation-body.json @@ -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 } \ No newline at end of file diff --git a/projects/workforce-allocations-to-delius/src/integrationTest/kotlin/uk/gov/justice/digital/hmpps/AllocateEventIntegrationTest.kt b/projects/workforce-allocations-to-delius/src/integrationTest/kotlin/uk/gov/justice/digital/hmpps/AllocateEventIntegrationTest.kt index e4964f6658..0ea55fbe09 100644 --- a/projects/workforce-allocations-to-delius/src/integrationTest/kotlin/uk/gov/justice/digital/hmpps/AllocateEventIntegrationTest.kt +++ b/projects/workforce-allocations-to-delius/src/integrationTest/kotlin/uk/gov/justice/digital/hmpps/AllocateEventIntegrationTest.kt @@ -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 @@ -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 @@ -57,7 +58,8 @@ class AllocateEventIntegrationTest { "new-event-allocation-body", existingManager, event, - 1 + 1, + StaffGenerator.SPO_STAFF.id ) verify(telemetryService).trackEvent( @@ -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( @@ -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)) @@ -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) @@ -134,6 +138,8 @@ class AllocateEventIntegrationTest { assertNotNull(cadeContact) + assertThat(cadeContact!!.staffId, equalTo(staffId)) + assertThat( cadeContact!!.isSensitive, equalTo((allocationDetail as AllocationDetail.EventAllocation).sensitive) diff --git a/projects/workforce-allocations-to-delius/src/integrationTest/kotlin/uk/gov/justice/digital/hmpps/ChoosePractitionerIntegrationTest.kt b/projects/workforce-allocations-to-delius/src/integrationTest/kotlin/uk/gov/justice/digital/hmpps/ChoosePractitionerIntegrationTest.kt index d3d9517989..94a591c988 100644 --- a/projects/workforce-allocations-to-delius/src/integrationTest/kotlin/uk/gov/justice/digital/hmpps/ChoosePractitionerIntegrationTest.kt +++ b/projects/workforce-allocations-to-delius/src/integrationTest/kotlin/uk/gov/justice/digital/hmpps/ChoosePractitionerIntegrationTest.kt @@ -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("example@example.com")) diff --git a/projects/workforce-allocations-to-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/workforceallocations/AllocationDetail.kt b/projects/workforce-allocations-to-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/workforceallocations/AllocationDetail.kt index bc8020dc59..8b20a02635 100644 --- a/projects/workforce-allocations-to-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/workforceallocations/AllocationDetail.kt +++ b/projects/workforce-allocations-to-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/workforceallocations/AllocationDetail.kt @@ -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 diff --git a/projects/workforce-allocations-to-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/service/AllocateEventService.kt b/projects/workforce-allocations-to-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/service/AllocateEventService.kt index 67c43e610e..383170a1d8 100644 --- a/projects/workforce-allocations-to-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/service/AllocateEventService.kt +++ b/projects/workforce-allocations-to-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/service/AllocateEventService.kt @@ -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 @@ -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(auditedInteractionService, orderManagerRepository) { @Transactional @@ -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) } @@ -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), @@ -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 diff --git a/projects/workforce-allocations-to-delius/src/test/kotlin/uk/gov/justice/digital/hmpps/service/AllocateEventServiceTest.kt b/projects/workforce-allocations-to-delius/src/test/kotlin/uk/gov/justice/digital/hmpps/service/AllocateEventServiceTest.kt index 6da3198a24..c7413b0747 100644 --- a/projects/workforce-allocations-to-delius/src/test/kotlin/uk/gov/justice/digital/hmpps/service/AllocateEventServiceTest.kt +++ b/projects/workforce-allocations-to-delius/src/test/kotlin/uk/gov/justice/digital/hmpps/service/AllocateEventServiceTest.kt @@ -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 @@ -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 @@ -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 @@ -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 { + allocateEventService.createEventAllocation( + PersonGenerator.DEFAULT.crn, + allocationDetail.copy(spoStaffCode = spoStaffCode) + ) + } + } }