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