Skip to content

Commit

Permalink
Merge pull request #2784 from ministryofjustice/feature/aps-1787-crea…
Browse files Browse the repository at this point in the history
…te-app-tidy-up

Use CasResult for CAS1/3 application creation
  • Loading branch information
davidatkinsuk authored Jan 10, 2025
2 parents 048e045 + 1fd9abd commit 54a83ff
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 160 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -203,18 +203,7 @@ class ApplicationsController(
createWithRisks,
)

val application = when (applicationResult) {
is ValidatableActionResult.GeneralValidationError ->
throw BadRequestProblem(errorDetail = applicationResult.message)

is ValidatableActionResult.FieldValidationError ->
throw BadRequestProblem(invalidParams = applicationResult.validationMessages)

is ValidatableActionResult.ConflictError ->
throw ConflictProblem(id = applicationResult.conflictingEntityId, conflictReason = applicationResult.message)

is ValidatableActionResult.Success -> applicationResult.entity
}
val application = extractEntityFromCasResult(applicationResult)

return ResponseEntity
.created(URI.create("/applications/${application.id}"))
Expand All @@ -227,7 +216,7 @@ class ApplicationsController(
user: UserEntity,
body: NewApplication,
createWithRisks: Boolean?,
): ValidatableActionResult<ApplicationEntity> = when (serviceName) {
): CasResult<out ApplicationEntity> = when (serviceName) {
ServiceName.approvedPremises ->
applicationService.createApprovedPremisesApplication(
personInfo.offenderDetailSummary,
Expand All @@ -239,28 +228,18 @@ class ApplicationsController(
)

ServiceName.temporaryAccommodation -> {
when (
val actionResult =
applicationService.createTemporaryAccommodationApplication(
body.crn,
user,
body.convictionId,
body.deliusEventNumber,
body.offenceId,
createWithRisks,
personInfo,
)
) {
is AuthorisableActionResult.NotFound -> throw NotFoundProblem(actionResult.id!!, actionResult.entityType!!)
is AuthorisableActionResult.Unauthorised -> throw ForbiddenProblem()
is AuthorisableActionResult.Success -> actionResult.entity
}
applicationService.createTemporaryAccommodationApplication(
body.crn,
user,
body.convictionId,
body.deliusEventNumber,
body.offenceId,
createWithRisks,
personInfo,
)
}

ServiceName.cas2 -> throw RuntimeException(
"CAS2 now has its own " +
"Cas2ApplicationsController",
)
ServiceName.cas2 -> error("CAS2 now has its own Cas2ApplicationsController")
}

@Transactional
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ import uk.gov.justice.digital.hmpps.approvedpremisesapi.service.OffenderService
import uk.gov.justice.digital.hmpps.approvedpremisesapi.service.UserService
import uk.gov.justice.digital.hmpps.approvedpremisesapi.service.UserService.GetUserResponse
import uk.gov.justice.digital.hmpps.approvedpremisesapi.util.extractEntityFromCasResult
import uk.gov.justice.digital.hmpps.approvedpremisesapi.util.extractEntityFromValidatableActionResult
import java.io.IOException
import java.time.LocalDate
import java.time.OffsetDateTime
Expand Down Expand Up @@ -238,7 +237,7 @@ class Cas1AutoScript(
val personInfo = getPersonInfo(crn)
val createdByUser = (userService.getExistingUserOrCreate(deliusUserName) as GetUserResponse.Success).user

val newApplicationEntity = extractEntityFromValidatableActionResult(
val newApplicationEntity = extractEntityFromCasResult(
applicationService.createApprovedPremisesApplication(
offenderDetails = personInfo.offenderDetailSummary,
user = createdByUser,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import uk.gov.justice.digital.hmpps.approvedpremisesapi.seed.SeedJob
import uk.gov.justice.digital.hmpps.approvedpremisesapi.service.ApplicationService
import uk.gov.justice.digital.hmpps.approvedpremisesapi.service.ApplicationTimelineNoteService
import uk.gov.justice.digital.hmpps.approvedpremisesapi.service.OffenderService
import uk.gov.justice.digital.hmpps.approvedpremisesapi.util.extractEntityFromValidatableActionResult
import uk.gov.justice.digital.hmpps.approvedpremisesapi.util.extractEntityFromCasResult
import java.util.UUID

/**
Expand Down Expand Up @@ -78,7 +78,7 @@ class Cas1DuplicateApplicationSeedJob(

val createdByUser = sourceApplication.createdByUser

val newApplicationEntity = extractEntityFromValidatableActionResult(
val newApplicationEntity = extractEntityFromCasResult(
applicationService.createApprovedPremisesApplication(
offenderDetails = personInfo.offenderDetailSummary,
user = createdByUser,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ import uk.gov.justice.digital.hmpps.approvedpremisesapi.model.ValidationErrors
import uk.gov.justice.digital.hmpps.approvedpremisesapi.model.asApprovedPremisesType
import uk.gov.justice.digital.hmpps.approvedpremisesapi.model.community.OffenderDetailSummary
import uk.gov.justice.digital.hmpps.approvedpremisesapi.model.prisonsapi.InmateStatus
import uk.gov.justice.digital.hmpps.approvedpremisesapi.model.validated
import uk.gov.justice.digital.hmpps.approvedpremisesapi.model.validatedCasResult
import uk.gov.justice.digital.hmpps.approvedpremisesapi.problem.InternalServerErrorProblem
import uk.gov.justice.digital.hmpps.approvedpremisesapi.results.AuthorisableActionResult
import uk.gov.justice.digital.hmpps.approvedpremisesapi.results.CasResult
Expand Down Expand Up @@ -227,7 +227,7 @@ class ApplicationService(
deliusEventNumber: String?,
offenceId: String?,
createWithRisks: Boolean? = true,
) = validated<ApplicationEntity> {
) = validatedCasResult<ApprovedPremisesApplicationEntity> {
val crn = offenderDetails.otherIds.crn

val managingTeamCodes = when (val managingTeamsResult = apDeliusContextApiClient.getTeamsManagingCase(crn)) {
Expand Down Expand Up @@ -352,74 +352,72 @@ class ApplicationService(
offenceId: String?,
createWithRisks: Boolean? = true,
personInfo: PersonInfoResult.Success.Full,
): AuthorisableActionResult<ValidatableActionResult<ApplicationEntity>> {
): CasResult<TemporaryAccommodationApplicationEntity> {
if (!user.hasRole(UserRole.CAS3_REFERRER)) {
return AuthorisableActionResult.Unauthorised()
return CasResult.Unauthorised()
}

return AuthorisableActionResult.Success(
validated {
val offenderDetails =
when (
val offenderDetailsResult = offenderService.getOffenderByCrn(crn, user.deliusUsername)
) {
is AuthorisableActionResult.NotFound -> return@validated "$.crn" hasSingleValidationError "doesNotExist"
is AuthorisableActionResult.Unauthorised ->
return@validated "$.crn" hasSingleValidationError "userPermission"

is AuthorisableActionResult.Success -> offenderDetailsResult.entity
}

if (convictionId == null) {
"$.convictionId" hasValidationError "empty"
}
return validatedCasResult {
val offenderDetails =
when (
val offenderDetailsResult = offenderService.getOffenderByCrn(crn, user.deliusUsername)
) {
is AuthorisableActionResult.NotFound -> return@validatedCasResult "$.crn" hasSingleValidationError "doesNotExist"
is AuthorisableActionResult.Unauthorised ->
return@validatedCasResult "$.crn" hasSingleValidationError "userPermission"

if (deliusEventNumber == null) {
"$.deliusEventNumber" hasValidationError "empty"
is AuthorisableActionResult.Success -> offenderDetailsResult.entity
}

if (offenceId == null) {
"$.offenceId" hasValidationError "empty"
}
if (convictionId == null) {
"$.convictionId" hasValidationError "empty"
}

if (validationErrors.any()) {
return@validated fieldValidationError
}
if (deliusEventNumber == null) {
"$.deliusEventNumber" hasValidationError "empty"
}

if (offenceId == null) {
"$.offenceId" hasValidationError "empty"
}

if (validationErrors.any()) {
return@validatedCasResult fieldValidationError
}

var riskRatings: PersonRisks? = null
var riskRatings: PersonRisks? = null

if (createWithRisks == true) {
val riskRatingsResult = offenderService.getRiskByCrn(crn, user.deliusUsername)
if (createWithRisks == true) {
val riskRatingsResult = offenderService.getRiskByCrn(crn, user.deliusUsername)

riskRatings = when (riskRatingsResult) {
is AuthorisableActionResult.NotFound ->
return@validated "$.crn" hasSingleValidationError "doesNotExist"
riskRatings = when (riskRatingsResult) {
is AuthorisableActionResult.NotFound ->
return@validatedCasResult "$.crn" hasSingleValidationError "doesNotExist"

is AuthorisableActionResult.Unauthorised ->
return@validated "$.crn" hasSingleValidationError "userPermission"
is AuthorisableActionResult.Unauthorised ->
return@validatedCasResult "$.crn" hasSingleValidationError "userPermission"

is AuthorisableActionResult.Success -> riskRatingsResult.entity
}
is AuthorisableActionResult.Success -> riskRatingsResult.entity
}
}

val prisonName = getPrisonName(personInfo)

val createdApplication = applicationRepository.save(
createTemporaryAccommodationApplicationEntity(
crn,
user,
convictionId,
deliusEventNumber,
offenceId,
riskRatings,
offenderDetails,
prisonName,
),
)

success(createdApplication.apply { schemaUpToDate = true })
},
)
val prisonName = getPrisonName(personInfo)

val createdApplication = applicationRepository.save(
createTemporaryAccommodationApplicationEntity(
crn,
user,
convictionId,
deliusEventNumber,
offenceId,
riskRatings,
offenderDetails,
prisonName,
),
)

success(createdApplication.apply { schemaUpToDate = true })
}
}

private fun createTemporaryAccommodationApplicationEntity(
Expand Down Expand Up @@ -495,7 +493,7 @@ class ApplicationService(
applicationId: UUID,
updateFields: Cas1ApplicationUpdateFields,
userForRequest: UserEntity,
): CasResult<ApplicationEntity> {
): CasResult<ApprovedPremisesApplicationEntity> {
val application = applicationRepository.findByIdOrNull(applicationId)?.let(jsonSchemaService::checkSchemaOutdated)
?: return CasResult.NotFound("Application", applicationId.toString())

Expand Down
Loading

0 comments on commit 54a83ff

Please sign in to comment.