|
| 1 | +package uk.gov.justice.digital.hmpps.hmppsintegrationapi.controllers.v1 |
| 2 | + |
| 3 | +import io.swagger.v3.oas.annotations.Parameter |
| 4 | +import jakarta.validation.ValidationException |
| 5 | +import org.springframework.beans.factory.annotation.Autowired |
| 6 | +import org.springframework.web.bind.annotation.GetMapping |
| 7 | +import org.springframework.web.bind.annotation.PathVariable |
| 8 | +import org.springframework.web.bind.annotation.RequestAttribute |
| 9 | +import org.springframework.web.bind.annotation.RequestMapping |
| 10 | +import org.springframework.web.bind.annotation.RequestParam |
| 11 | +import org.springframework.web.bind.annotation.RestController |
| 12 | +import uk.gov.justice.digital.hmpps.hmppsintegrationapi.exception.EntityNotFoundException |
| 13 | +import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps.DataResponse |
| 14 | +import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps.Transactions |
| 15 | +import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps.UpstreamApiError |
| 16 | +import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.roleconfig.ConsumerFilters |
| 17 | +import uk.gov.justice.digital.hmpps.hmppsintegrationapi.services.GetTransactionsForPersonService |
| 18 | +import uk.gov.justice.digital.hmpps.hmppsintegrationapi.services.internal.AuditService |
| 19 | +import java.time.LocalDate |
| 20 | + |
| 21 | +@RestController |
| 22 | +@RequestMapping("/v1/prison/{prisonId}/prisoners/{hmppsId}/transactions/{accountCode}") |
| 23 | +class TransactionsController( |
| 24 | + @Autowired val auditService: AuditService, |
| 25 | + @Autowired val getTransactionsForPersonService: GetTransactionsForPersonService, |
| 26 | +) { |
| 27 | + // @Operation( |
| 28 | +// summary = "Returns all accounts for a prisoner that they have at a prison.", |
| 29 | +// description = "<b>Applicable filters</b>: <ul><li>prisons</li></ul>", |
| 30 | +// responses = [ |
| 31 | +// ApiResponse(responseCode = "200", useReturnTypeSchema = true, description = "Successfully found a prisoner's accounts."), |
| 32 | +// ApiResponse( |
| 33 | +// responseCode = "400", |
| 34 | +// description = "The HMPPS ID provided has an invalid format or the prisoner does hot have accounts at the specified prison.", |
| 35 | +// content = [Content(schema = Schema(ref = "#/components/schemas/BadRequest"))], |
| 36 | +// ), |
| 37 | +// ApiResponse(responseCode = "404", content = [Content(schema = Schema(ref = "#/components/schemas/PersonNotFound"))]), |
| 38 | +// ApiResponse(responseCode = "500", content = [Content(schema = Schema(ref = "#/components/schemas/InternalServerError"))]), |
| 39 | +// ], |
| 40 | +// ) |
| 41 | + @GetMapping() |
| 42 | + fun getAccountCodeForPerson( |
| 43 | + @PathVariable hmppsId: String, |
| 44 | + @PathVariable prisonId: String, |
| 45 | + @PathVariable accountCode: String, |
| 46 | + @RequestAttribute filters: ConsumerFilters?, |
| 47 | + @Parameter(description = "Start date for transactions (defaults to today if not supplied)") @RequestParam(required = false, name = "fromDate") fromDate: String?, |
| 48 | + @Parameter(description = "To date for transactions (defaults to today if not supplied)") @RequestParam(required = false, name = "toDate") toDate: String?, |
| 49 | + ): DataResponse<Transactions?> { |
| 50 | + var startDate = LocalDate.now().toString() |
| 51 | + var endDate = LocalDate.now().toString() |
| 52 | + |
| 53 | + if (fromDate == null && toDate != null || toDate == null && fromDate != null) { |
| 54 | + throw ValidationException("Both fromDate and toDate must be supplied if one is populated") |
| 55 | + } |
| 56 | + // respect filters |
| 57 | + // catch parse failures |
| 58 | + if (fromDate != null && toDate != null) { |
| 59 | + startDate = fromDate |
| 60 | + endDate = toDate |
| 61 | + } |
| 62 | + |
| 63 | + if (accountCode !in listOf("spends", "savings", "cash")) { |
| 64 | + throw ValidationException("Account code must either be 'spends', 'savings', or 'cash'") |
| 65 | + } |
| 66 | + |
| 67 | + val response = getTransactionsForPersonService.execute(hmppsId, prisonId, accountCode, startDate, endDate, filters) |
| 68 | + |
| 69 | + if (response.hasError(UpstreamApiError.Type.ENTITY_NOT_FOUND)) { |
| 70 | + throw EntityNotFoundException("Could not find transactions with id: $hmppsId") |
| 71 | + } |
| 72 | + |
| 73 | + if (response.hasError(UpstreamApiError.Type.BAD_REQUEST)) { |
| 74 | + throw ValidationException("Either invalid HMPPS ID: $hmppsId at or incorrect prison: $prisonId") |
| 75 | + } |
| 76 | + |
| 77 | + auditService.createEvent("GET_TRANSACTIONS_FOR_PERSON", mapOf("hmppsId" to hmppsId, "prisonId" to prisonId, "fromDate" to fromDate, "toDate" to toDate)) |
| 78 | + return DataResponse(response.data) |
| 79 | + } |
| 80 | +} |
0 commit comments