|
| 1 | +package uk.gov.justice.digital.hmpps.hmppsintegrationapi.controllers.v1.person |
| 2 | + |
| 3 | +import org.springframework.beans.factory.annotation.Autowired |
| 4 | +import org.springframework.web.bind.annotation.GetMapping |
| 5 | +import org.springframework.web.bind.annotation.PathVariable |
| 6 | +import org.springframework.web.bind.annotation.RequestMapping |
| 7 | +import org.springframework.web.bind.annotation.RestController |
| 8 | +import uk.gov.justice.digital.hmpps.hmppsintegrationapi.exception.EntityNotFoundException |
| 9 | +import uk.gov.justice.digital.hmpps.hmppsintegrationapi.extensions.decodeUrlCharacters |
| 10 | +import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps.PersonResponsibleOfficer |
| 11 | +import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps.UpstreamApiError |
| 12 | +import uk.gov.justice.digital.hmpps.hmppsintegrationapi.services.GetCommunityOffenderManagerForPersonService |
| 13 | +import uk.gov.justice.digital.hmpps.hmppsintegrationapi.services.GetPrisonOffenderManagerForPersonService |
| 14 | +import uk.gov.justice.digital.hmpps.hmppsintegrationapi.services.internal.AuditService |
| 15 | + |
| 16 | +@RestController |
| 17 | +@RequestMapping("/v1/persons") |
| 18 | +class PersonResponsibleOfficerController( |
| 19 | + @Autowired val auditService: AuditService, |
| 20 | + @Autowired val getPrisonOffenderManagerForPersonService: GetPrisonOffenderManagerForPersonService, |
| 21 | + @Autowired val getCommunityOffenderManagerForPersonService: GetCommunityOffenderManagerForPersonService, |
| 22 | +) { |
| 23 | + @GetMapping("{encodedHmppsId}/person-responsible-officer") |
| 24 | + fun getPersonResponsibleOfficer( |
| 25 | + @PathVariable encodedHmppsId: String, |
| 26 | + ): Map<String, PersonResponsibleOfficer> { |
| 27 | + val hmppsId = encodedHmppsId.decodeUrlCharacters() |
| 28 | + val prisonOffenderManager = getPrisonOffenderManagerForPersonService.execute(hmppsId) |
| 29 | + val communityOffenderManager = getCommunityOffenderManagerForPersonService.execute(hmppsId) |
| 30 | + |
| 31 | + if (prisonOffenderManager.hasError(UpstreamApiError.Type.ENTITY_NOT_FOUND)) { |
| 32 | + throw EntityNotFoundException("Could not find prison offender manager related to id: $hmppsId") |
| 33 | + } |
| 34 | + |
| 35 | + if (communityOffenderManager.hasError(UpstreamApiError.Type.ENTITY_NOT_FOUND)) { |
| 36 | + throw EntityNotFoundException("Could not find community offender manager related to id: $hmppsId") |
| 37 | + } |
| 38 | + |
| 39 | + val mergedData = |
| 40 | + PersonResponsibleOfficer( |
| 41 | + prisonOffenderManager = prisonOffenderManager.data, |
| 42 | + communityOffenderManager = communityOffenderManager.data, |
| 43 | + ) |
| 44 | + |
| 45 | + auditService.createEvent("GET_PERSON_RESPONSIBLE_OFFICER", mapOf("hmppsId" to hmppsId)) |
| 46 | + return mapOf("data" to mergedData) |
| 47 | + } |
| 48 | +} |
0 commit comments