Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

APS-1605 - Add getStaffDetailByCode service function #2586

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import org.springframework.http.HttpStatus
import org.springframework.stereotype.Service
import uk.gov.justice.digital.hmpps.approvedpremisesapi.client.ApDeliusContextApiClient
import uk.gov.justice.digital.hmpps.approvedpremisesapi.client.ClientResult
import uk.gov.justice.digital.hmpps.approvedpremisesapi.model.deliuscontext.StaffDetail
import uk.gov.justice.digital.hmpps.approvedpremisesapi.model.deliuscontext.StaffMember
import uk.gov.justice.digital.hmpps.approvedpremisesapi.results.CasResult

Expand All @@ -13,6 +14,16 @@ class StaffMemberService(private val apDeliusContextApiClient: ApDeliusContextAp

private val log = LoggerFactory.getLogger(this::class.java)

fun getStaffDetailByCode(code: String): CasResult<StaffDetail> = when (val response = apDeliusContextApiClient.getStaffDetailByStaffCode(code)) {
is ClientResult.Success -> CasResult.Success(response.body)
is ClientResult.Failure.StatusCode -> when (response.status) {
HttpStatus.NOT_FOUND -> CasResult.NotFound("Staff", code)
HttpStatus.UNAUTHORIZED -> CasResult.Unauthorised()
else -> response.throwException()
}
is ClientResult.Failure -> response.throwException()
}

fun getStaffMemberByCodeForPremise(code: String, qCode: String): CasResult<StaffMember> {
val premisesStaffMembers =
when (val premisesStaffMembersResult = getStaffMembersForQCode(qCode)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ import uk.gov.justice.digital.hmpps.approvedpremisesapi.api.events.model.PersonR
import uk.gov.justice.digital.hmpps.approvedpremisesapi.api.events.model.Premises
import uk.gov.justice.digital.hmpps.approvedpremisesapi.api.events.model.StaffMember
import uk.gov.justice.digital.hmpps.approvedpremisesapi.api.model.TimelineEvent
import uk.gov.justice.digital.hmpps.approvedpremisesapi.client.ApDeliusContextApiClient
import uk.gov.justice.digital.hmpps.approvedpremisesapi.client.ClientResult
import uk.gov.justice.digital.hmpps.approvedpremisesapi.jpa.entity.ApprovedPremisesEntity
import uk.gov.justice.digital.hmpps.approvedpremisesapi.jpa.entity.Cas1SpaceBookingEntity
import uk.gov.justice.digital.hmpps.approvedpremisesapi.jpa.entity.DepartureReasonEntity
Expand All @@ -28,8 +26,10 @@ import uk.gov.justice.digital.hmpps.approvedpremisesapi.jpa.entity.UserEntity
import uk.gov.justice.digital.hmpps.approvedpremisesapi.model.DomainEvent
import uk.gov.justice.digital.hmpps.approvedpremisesapi.model.PersonSummaryInfoResult
import uk.gov.justice.digital.hmpps.approvedpremisesapi.model.deliuscontext.CaseSummary
import uk.gov.justice.digital.hmpps.approvedpremisesapi.results.CasResult
import uk.gov.justice.digital.hmpps.approvedpremisesapi.service.DomainEventService
import uk.gov.justice.digital.hmpps.approvedpremisesapi.service.OffenderService
import uk.gov.justice.digital.hmpps.approvedpremisesapi.service.StaffMemberService
import uk.gov.justice.digital.hmpps.approvedpremisesapi.transformer.ApplicationTimelineTransformer
import uk.gov.justice.digital.hmpps.approvedpremisesapi.util.UrlTemplate
import uk.gov.justice.digital.hmpps.approvedpremisesapi.util.toLocalDateTime
Expand All @@ -47,7 +47,7 @@ class Cas1SpaceBookingManagementDomainEventService(
val offenderService: OffenderService,
private val cas1SpaceBookingManagementConfig: Cas1SpaceBookingManagementDomainEventServiceConfig,
private val applicationTimelineTransformer: ApplicationTimelineTransformer,
private val apDeliusContextApiClient: ApDeliusContextApiClient,
private val staffMemberService: StaffMemberService,
) {

fun arrivalRecorded(
Expand Down Expand Up @@ -128,14 +128,14 @@ class Cas1SpaceBookingManagementDomainEventService(
timestamp = eventOccurredAt,
eventType = EventType.personNotArrived,
eventDetails = PersonNotArrived(
applicationId = application!!.id,
applicationId = application.id,
applicationUrl = cas1SpaceBookingManagementConfig.applicationUrlTemplate.resolve("id", application.id.toString()),
bookingId = updatedCas1SpaceBooking.id,
personReference = PersonReference(
crn = updatedCas1SpaceBooking.crn,
noms = offenderDetails?.nomsId ?: "Unknown NOMS Id",
),
deliusEventNumber = application!!.eventNumber,
deliusEventNumber = application.eventNumber,
premises = premises,
expectedArrivalOn = updatedCas1SpaceBooking.canonicalArrivalDate,
recordedBy = staffUser!!,
Expand Down Expand Up @@ -258,11 +258,13 @@ class Cas1SpaceBookingManagementDomainEventService(
)
}

@SuppressWarnings("TooGenericExceptionThrown")
private fun getStaffMemberDetails(staffCode: String?): StaffMember? {
return staffCode?.let {
when (val staffDetailResponse = apDeliusContextApiClient.getStaffDetailByStaffCode(staffCode)) {
is ClientResult.Success -> staffDetailResponse.body.toStaffMember()
is ClientResult.Failure -> staffDetailResponse.throwException()
when (val result = staffMemberService.getStaffDetailByCode(staffCode)) {
// the blunt error handling is temporary, it will be resolved as part of domain event structure updates
is CasResult.Error -> throw RuntimeException("Could not load keyworker $staffCode")
is CasResult.Success -> result.value.toStaffMember()
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import org.springframework.http.HttpStatus
import uk.gov.justice.digital.hmpps.approvedpremisesapi.client.ApDeliusContextApiClient
import uk.gov.justice.digital.hmpps.approvedpremisesapi.client.ClientResult
import uk.gov.justice.digital.hmpps.approvedpremisesapi.factory.ContextStaffMemberFactory
import uk.gov.justice.digital.hmpps.approvedpremisesapi.factory.StaffDetailFactory
import uk.gov.justice.digital.hmpps.approvedpremisesapi.model.deliuscontext.StaffMembersPage
import uk.gov.justice.digital.hmpps.approvedpremisesapi.results.CasResult
import uk.gov.justice.digital.hmpps.approvedpremisesapi.service.StaffMemberService
Expand All @@ -18,22 +19,72 @@ class StaffMemberServiceTest {
private val mockApDeliusContextApiClient = mockk<ApDeliusContextApiClient>()
private val staffMemberService = StaffMemberService(mockApDeliusContextApiClient)

private val qCode = "Qcode"
@Nested
inner class GetStaffDetailByCode {
@Test
fun `it returns a staff member`() {
val staffDetail = StaffDetailFactory.staffDetail()

every { mockApDeliusContextApiClient.getStaffDetailByStaffCode("staffCode") } returns ClientResult.Success(
status = HttpStatus.OK,
body = staffDetail,
)

val result = staffMemberService.getStaffDetailByCode("staffCode")

assertThat(result is CasResult.Success).isTrue
result as CasResult.Success

assertThat(result.value).isEqualTo(staffDetail)
}

@Test
fun `it returns Unauthorised when the Upstream API returns Unauthorised`() {
every { mockApDeliusContextApiClient.getStaffDetailByStaffCode("staffCode") } returns ClientResult.Failure.StatusCode(
HttpMethod.GET,
"/staff-members/code",
HttpStatus.UNAUTHORIZED,
body = null,
)

val result = staffMemberService.getStaffDetailByCode("staffCode")

assertThat(result is CasResult.Unauthorised).isTrue
}

@Test
fun `it returns NotFound when the Upstream API returns NotFound`() {
every { mockApDeliusContextApiClient.getStaffDetailByStaffCode("staffCode") } returns ClientResult.Failure.StatusCode(
HttpMethod.GET,
"/staff-members/code",
HttpStatus.NOT_FOUND,
body = null,
)

val result = staffMemberService.getStaffDetailByCode("staffCode")

assertThat(result is CasResult.NotFound).isTrue
result as CasResult.NotFound

assertThat(result.id).isEqualTo("staffCode")
assertThat(result.entityType).isEqualTo("Staff")
}
}

@Nested
inner class GetStaffMemberByCode {
inner class GetStaffMemberByCodeForPremise {
@Test
fun `it returns a staff member`() {
val staffMembers = ContextStaffMemberFactory().produceMany().take(5).toList()

every { mockApDeliusContextApiClient.getStaffMembers(qCode) } returns ClientResult.Success(
every { mockApDeliusContextApiClient.getStaffMembers("qCode") } returns ClientResult.Success(
status = HttpStatus.OK,
body = StaffMembersPage(
content = staffMembers,
),
)

val result = staffMemberService.getStaffMemberByCodeForPremise(staffMembers[2].code, qCode)
val result = staffMemberService.getStaffMemberByCodeForPremise(staffMembers[2].code, "qCode")

assertThat(result is CasResult.Success).isTrue
result as CasResult.Success
Expand All @@ -43,49 +94,49 @@ class StaffMemberServiceTest {
}

@Test
fun `it returns Unauthorised when Delius returns Unauthorised`() {
every { mockApDeliusContextApiClient.getStaffMembers(qCode) } returns ClientResult.Failure.StatusCode(
fun `it returns Unauthorised when the Upstream API returns Unauthorised`() {
every { mockApDeliusContextApiClient.getStaffMembers("qCode") } returns ClientResult.Failure.StatusCode(
HttpMethod.GET,
"/staff-members/code",
HttpStatus.UNAUTHORIZED,
body = null,
)

val result = staffMemberService.getStaffMemberByCodeForPremise("code", qCode)
val result = staffMemberService.getStaffMemberByCodeForPremise("code", "qCode")

assertThat(result is CasResult.Unauthorised).isTrue
}

@Test
fun `it returns NotFound when Delius returns NotFound`() {
every { mockApDeliusContextApiClient.getStaffMembers(qCode) } returns ClientResult.Failure.StatusCode(
fun `it returns NotFound when the Upstream API returns NotFound`() {
every { mockApDeliusContextApiClient.getStaffMembers("qCode") } returns ClientResult.Failure.StatusCode(
HttpMethod.GET,
"/staff-members/code",
HttpStatus.NOT_FOUND,
body = null,
)

val result = staffMemberService.getStaffMemberByCodeForPremise("code", qCode)
val result = staffMemberService.getStaffMemberByCodeForPremise("code", "qCode")

assertThat(result is CasResult.NotFound).isTrue
result as CasResult.NotFound

assertThat(result.id).isEqualTo(qCode)
assertThat(result.id).isEqualTo("qCode")
assertThat(result.entityType).isEqualTo("Team")
}

@Test
fun `it returns a NotFound when a staff member for the QCode cannot me found`() {
val staffMembers = ContextStaffMemberFactory().produceMany().take(5).toList()

every { mockApDeliusContextApiClient.getStaffMembers(qCode) } returns ClientResult.Success(
every { mockApDeliusContextApiClient.getStaffMembers("qCode") } returns ClientResult.Success(
status = HttpStatus.OK,
body = StaffMembersPage(
content = staffMembers,
),
)

val result = staffMemberService.getStaffMemberByCodeForPremise("code", qCode)
val result = staffMemberService.getStaffMemberByCodeForPremise("code", "qCode")

assertThat(result is CasResult.NotFound).isTrue
result as CasResult.NotFound
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,10 @@ import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Nested
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.extension.ExtendWith
import org.springframework.http.HttpStatus
import uk.gov.justice.digital.hmpps.approvedpremisesapi.api.events.model.BookingKeyWorkerAssignedEnvelope
import uk.gov.justice.digital.hmpps.approvedpremisesapi.api.events.model.EventType
import uk.gov.justice.digital.hmpps.approvedpremisesapi.api.events.model.PersonArrivedEnvelope
import uk.gov.justice.digital.hmpps.approvedpremisesapi.api.events.model.PersonDepartedEnvelope
import uk.gov.justice.digital.hmpps.approvedpremisesapi.client.ApDeliusContextApiClient
import uk.gov.justice.digital.hmpps.approvedpremisesapi.client.ClientResult
import uk.gov.justice.digital.hmpps.approvedpremisesapi.factory.ApprovedPremisesApplicationEntityFactory
import uk.gov.justice.digital.hmpps.approvedpremisesapi.factory.ApprovedPremisesEntityFactory
import uk.gov.justice.digital.hmpps.approvedpremisesapi.factory.Cas1SpaceBookingEntityFactory
Expand All @@ -32,8 +29,10 @@ import uk.gov.justice.digital.hmpps.approvedpremisesapi.jpa.entity.DepartureReas
import uk.gov.justice.digital.hmpps.approvedpremisesapi.jpa.entity.MoveOnCategoryEntity
import uk.gov.justice.digital.hmpps.approvedpremisesapi.model.DomainEvent
import uk.gov.justice.digital.hmpps.approvedpremisesapi.model.PersonSummaryInfoResult
import uk.gov.justice.digital.hmpps.approvedpremisesapi.results.CasResult
import uk.gov.justice.digital.hmpps.approvedpremisesapi.service.DomainEventService
import uk.gov.justice.digital.hmpps.approvedpremisesapi.service.OffenderService
import uk.gov.justice.digital.hmpps.approvedpremisesapi.service.StaffMemberService
import uk.gov.justice.digital.hmpps.approvedpremisesapi.service.cas1.Cas1SpaceBookingManagementDomainEventService
import uk.gov.justice.digital.hmpps.approvedpremisesapi.service.cas1.Cas1SpaceBookingManagementDomainEventServiceConfig
import uk.gov.justice.digital.hmpps.approvedpremisesapi.transformer.ApplicationTimelineTransformer
Expand All @@ -51,7 +50,7 @@ import java.util.UUID
class Cas1SpaceBookingManagementDomainEventServiceTest {

@MockK
lateinit var apDeliusContextApiClient: ApDeliusContextApiClient
lateinit var staffMemberService: StaffMemberService

@MockK
lateinit var domainEventService: DomainEventService
Expand Down Expand Up @@ -119,8 +118,7 @@ class Cas1SpaceBookingManagementDomainEventServiceTest {
every { offenderService.getPersonSummaryInfoResults(any(), any()) } returns
listOf(PersonSummaryInfoResult.Success.Full("THEBOOKINGCRN", caseSummary))

every { apDeliusContextApiClient.getStaffDetailByStaffCode(any()) } returns ClientResult.Success(
HttpStatus.OK,
every { staffMemberService.getStaffDetailByCode(any()) } returns CasResult.Success(
keyWorker,
)
}
Expand Down Expand Up @@ -285,8 +283,7 @@ class Cas1SpaceBookingManagementDomainEventServiceTest {
every { offenderService.getPersonSummaryInfoResults(any(), any()) } returns
listOf(PersonSummaryInfoResult.Success.Full("THEBOOKINGCRN", caseSummary))

every { apDeliusContextApiClient.getStaffDetailByStaffCode(any()) } returns ClientResult.Success(
HttpStatus.OK,
every { staffMemberService.getStaffDetailByCode(any()) } returns CasResult.Success(
keyWorker,
)
}
Expand Down Expand Up @@ -426,8 +423,7 @@ class Cas1SpaceBookingManagementDomainEventServiceTest {
every { offenderService.getPersonSummaryInfoResults(any(), any()) } returns
listOf(PersonSummaryInfoResult.Success.Full("THEBOOKINGCRN", caseSummary))

every { apDeliusContextApiClient.getStaffDetailByStaffCode(any()) } returns ClientResult.Success(
HttpStatus.OK,
every { staffMemberService.getStaffDetailByCode(any()) } returns CasResult.Success(
keyWorker,
)
}
Expand Down
Loading