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

Cas2bail/cba56 add application origin #2693

Open
wants to merge 7 commits into
base: cas2bail/dev
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ data class Cas2ApplicationEntity(
var hdcEligibilityDate: LocalDate? = null,
var conditionalReleaseDate: LocalDate? = null,
var telephoneNumber: String? = null,
var applicationOrigin: String? = null,
) {
override fun toString() = "Cas2ApplicationEntity: $id"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,6 @@ data class Cas2ApplicationSummaryEntity(
var latestStatusUpdateStatusId: String? = null,
@Column(name = "referring_prison_code")
val prisonCode: String,
@Column(name = "application_origin")
var applicationOrigin: String? = null,
)
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import uk.gov.justice.digital.hmpps.approvedpremisesapi.api.events.cas2.model.Ca
import uk.gov.justice.digital.hmpps.approvedpremisesapi.api.events.cas2.model.Cas2StaffMember
import uk.gov.justice.digital.hmpps.approvedpremisesapi.api.events.cas2.model.EventType
import uk.gov.justice.digital.hmpps.approvedpremisesapi.api.events.cas2.model.PersonReference
import uk.gov.justice.digital.hmpps.approvedpremisesapi.api.model.ApplicationOrigin
import uk.gov.justice.digital.hmpps.approvedpremisesapi.api.model.SubmitCas2Application
import uk.gov.justice.digital.hmpps.approvedpremisesapi.config.NotifyConfig
import uk.gov.justice.digital.hmpps.approvedpremisesapi.jpa.entity.ApplicationSummaryRepository
Expand Down Expand Up @@ -117,7 +118,7 @@ class ApplicationService(
}
}

fun createApplication(crn: String, user: NomisUserEntity, jwt: String) =
fun createApplication(crn: String, user: NomisUserEntity, jwt: String, applicationOrigin: ApplicationOrigin? = ApplicationOrigin.homeDetentionCurfew) =
validated<Cas2ApplicationEntity> {
val offenderDetailsResult = offenderService.getOffenderByCrn(crn)

Expand Down Expand Up @@ -148,6 +149,7 @@ class ApplicationService(
schemaUpToDate = true,
nomsNumber = offenderDetails.otherIds.nomsNumber,
telephoneNumber = null,
applicationOrigin = applicationOrigin?.toString(),
),
)

Expand Down Expand Up @@ -290,6 +292,7 @@ class ApplicationService(
hdcEligibilityDate = submitApplication.hdcEligibilityDate
conditionalReleaseDate = submitApplication.conditionalReleaseDate
telephoneNumber = submitApplication.telephoneNumber
applicationOrigin = submitApplication.applicationOrigin?.toString()
}
} catch (error: UpstreamApiException) {
return AuthorisableActionResult.Success(
Expand Down Expand Up @@ -345,6 +348,7 @@ class ApplicationService(
username = application.createdByUser.nomisUsername,
),
),
applicationOrigin = application.applicationOrigin,
),
),
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import com.fasterxml.jackson.databind.ObjectMapper
import org.springframework.stereotype.Component
import uk.gov.justice.digital.hmpps.approvedpremisesapi.api.model.ApplicationStatus
import uk.gov.justice.digital.hmpps.approvedpremisesapi.api.model.Cas2Application
import uk.gov.justice.digital.hmpps.approvedpremisesapi.api.model.Cas2ApplicationSummary
import uk.gov.justice.digital.hmpps.approvedpremisesapi.jpa.entity.Cas2ApplicationEntity
import uk.gov.justice.digital.hmpps.approvedpremisesapi.jpa.entity.Cas2ApplicationSummaryEntity
import uk.gov.justice.digital.hmpps.approvedpremisesapi.model.PersonInfoResult
Expand All @@ -21,7 +22,10 @@ class ApplicationsTransformer(
private val assessmentsTransformer: AssessmentsTransformer,
) {

fun transformJpaToApi(jpa: Cas2ApplicationEntity, personInfo: PersonInfoResult): Cas2Application {
fun transformJpaToApi(
jpa: Cas2ApplicationEntity,
personInfo: PersonInfoResult,
): Cas2Application {
return Cas2Application(
id = jpa.id,
person = personTransformer.transformModelToPersonApi(personInfo),
Expand All @@ -37,28 +41,29 @@ class ApplicationsTransformer(
telephoneNumber = jpa.telephoneNumber,
assessment = if (jpa.assessment != null) assessmentsTransformer.transformJpaToApiRepresentation(jpa.assessment!!) else null,
timelineEvents = timelineEventsTransformer.transformApplicationToTimelineEvents(jpa),
applicationOrigin = getApplicationOrigin(jpa.applicationOrigin),
)
}

fun transformJpaSummaryToSummary(
jpaSummary: Cas2ApplicationSummaryEntity,
personName: String,
): uk.gov.justice.digital.hmpps.approvedpremisesapi.api.model.Cas2ApplicationSummary {
return uk.gov.justice.digital.hmpps.approvedpremisesapi.api.model
.Cas2ApplicationSummary(
id = jpaSummary.id,
createdByUserId = UUID.fromString(jpaSummary.userId),
createdByUserName = jpaSummary.userName,
createdAt = jpaSummary.createdAt.toInstant(),
submittedAt = jpaSummary.submittedAt?.toInstant(),
status = getStatusFromSummary(jpaSummary),
latestStatusUpdate = statusUpdateTransformer.transformJpaSummaryToLatestStatusUpdateApi(jpaSummary),
type = "CAS2",
hdcEligibilityDate = jpaSummary.hdcEligibilityDate,
crn = jpaSummary.crn,
nomsNumber = jpaSummary.nomsNumber,
personName = personName,
)
): Cas2ApplicationSummary {
return Cas2ApplicationSummary(
id = jpaSummary.id,
createdByUserId = UUID.fromString(jpaSummary.userId),
createdByUserName = jpaSummary.userName,
createdAt = jpaSummary.createdAt.toInstant(),
submittedAt = jpaSummary.submittedAt?.toInstant(),
status = getStatusFromSummary(jpaSummary),
latestStatusUpdate = statusUpdateTransformer.transformJpaSummaryToLatestStatusUpdateApi(jpaSummary),
type = "CAS2",
hdcEligibilityDate = jpaSummary.hdcEligibilityDate,
crn = jpaSummary.crn,
nomsNumber = jpaSummary.nomsNumber,
personName = personName,
applicationOrigin = getApplicationOrigin(jpaSummary.applicationOrigin),
)
}

private fun getStatus(entity: Cas2ApplicationEntity): ApplicationStatus {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package uk.gov.justice.digital.hmpps.approvedpremisesapi.transformer.cas2

import com.fasterxml.jackson.databind.ObjectMapper
import org.springframework.stereotype.Component
import uk.gov.justice.digital.hmpps.approvedpremisesapi.api.model.ApplicationOrigin
import uk.gov.justice.digital.hmpps.approvedpremisesapi.api.model.Cas2SubmittedApplication
import uk.gov.justice.digital.hmpps.approvedpremisesapi.api.model.Cas2SubmittedApplicationSummary
import uk.gov.justice.digital.hmpps.approvedpremisesapi.jpa.entity.Cas2ApplicationEntity
Expand Down Expand Up @@ -37,6 +38,7 @@ class SubmissionsTransformer(
telephoneNumber = jpa.telephoneNumber,
timelineEvents = timelineEventsTransformer.transformApplicationToTimelineEvents(jpa),
assessment = assessmentsTransformer.transformJpaToApiRepresentation(jpa.assessment!!),
applicationOrigin = getApplicationOrigin(jpa.applicationOrigin),
)
}

Expand All @@ -52,6 +54,17 @@ class SubmissionsTransformer(
submittedAt = jpaSummary.submittedAt?.toInstant(),
crn = jpaSummary.crn,
nomsNumber = jpaSummary.nomsNumber,
applicationOrigin = getApplicationOrigin(jpaSummary.applicationOrigin),
)
}
}

// shared function between SubmissionsTransformer and ApplicationsTransformer
fun getApplicationOrigin(origin: String?): ApplicationOrigin? {
return when (origin) {
"courtBail" -> ApplicationOrigin.courtBail
"prisonBail" -> ApplicationOrigin.prisonBail
"homeDetentionCurfew" -> ApplicationOrigin.homeDetentionCurfew
else -> null
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
-- add application origin to the cas2 application table
ALTER TABLE cas_2_applications
ADD application_origin TEXT NULL;

-- include application origin in the cas 2 application view
DROP VIEW IF EXISTS cas_2_application_summary CASCADE;
CREATE OR REPLACE VIEW cas_2_application_summary AS
SELECT a.id,
a.crn,
a.noms_number,
CAST(a.created_by_user_id AS TEXT),
nu.name,
a.created_at,
a.submitted_at,
a.hdc_eligibility_date,
asu.label,
CAST(asu.status_id AS TEXT),
a.referring_prison_code,
a.conditional_release_date,
asu.created_at AS status_created_at,
a.abandoned_at,
a.application_origin
FROM cas_2_applications a
LEFT JOIN (SELECT DISTINCT ON (application_id) su.application_id, su.label, su.status_id, su.created_at
FROM cas_2_status_updates su
ORDER BY su.application_id, su.created_at DESC) as asu
ON a.id = asu.application_id
JOIN nomis_users nu ON nu.id = a.created_by_user_id;

-- include application origin in the cas 2 application summary view
DROP VIEW IF EXISTS cas_2_application_live_summary CASCADE;
CREATE OR REPLACE VIEW cas_2_application_live_summary AS
SELECT a.id,
a.crn,
a.noms_number,
a.created_by_user_id,
a.name,
a.created_at,
a.submitted_at,
a.hdc_eligibility_date,
a.label,
a.status_id,
a.referring_prison_code,
a.abandoned_at,
a.application_origin
FROM cas_2_application_summary a
WHERE (a.conditional_release_date IS NULL OR a.conditional_release_date >= current_date)
AND a.abandoned_at IS NULL
AND a.status_id IS NULL
OR (a.status_id = '004e2419-9614-4c1e-a207-a8418009f23d' AND
a.status_created_at > (current_date - INTERVAL '32 DAY')) -- Referral withdrawn
OR (a.status_id = 'f13bbdd6-44f1-4362-b9d3-e6f1298b1bf9' AND
a.status_created_at > (current_date - INTERVAL '32 DAY')) -- Referral cancelled
OR (a.status_id = '89458555-3219-44a2-9584-c4f715d6b565' AND
a.status_created_at > (current_date - INTERVAL '32 DAY')) -- Awaiting arrival
OR (a.status_id NOT IN ('004e2419-9614-4c1e-a207-a8418009f23d',
'f13bbdd6-44f1-4362-b9d3-e6f1298b1bf9',
'89458555-3219-44a2-9584-c4f715d6b565'));
16 changes: 16 additions & 0 deletions src/main/resources/static/_shared.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1737,6 +1737,8 @@ components:
type: array
items:
$ref: '_shared.yml#/components/schemas/Cas2TimelineEvent'
applicationOrigin:
$ref: '_shared.yml#/components/schemas/ApplicationOrigin'
required:
- createdBy
- schemaVersion
Expand Down Expand Up @@ -1774,6 +1776,8 @@ components:
assessment:
type: object
$ref: '_shared.yml#/components/schemas/Cas2Assessment'
applicationOrigin:
$ref: '_shared.yml#/components/schemas/ApplicationOrigin'
required:
- id
- person
Expand Down Expand Up @@ -1938,6 +1942,8 @@ components:
type: string
nomsNumber:
type: string
applicationOrigin:
$ref: '_shared.yml#/components/schemas/ApplicationOrigin'
required:
- type
- id
Expand Down Expand Up @@ -2056,6 +2062,8 @@ components:
submittedAt:
type: string
format: date-time
applicationOrigin:
$ref: '_shared.yml#/components/schemas/ApplicationOrigin'
required:
- createdByUserId
- status
Expand Down Expand Up @@ -2358,6 +2366,8 @@ components:
format: date
telephoneNumber:
type: string
applicationOrigin:
$ref: '_shared.yml#/components/schemas/ApplicationOrigin'
required:
- translatedDocument
- applicationId
Expand Down Expand Up @@ -4932,3 +4942,9 @@ components:
- sharedProperty
- singleOccupancy
- wheelchairAccessible
ApplicationOrigin:
type: string
enum:
- courtBail
- prisonBail
- homeDetentionCurfew
2 changes: 2 additions & 0 deletions src/main/resources/static/cas2-domain-events-api.yml
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,8 @@ components:
$ref: '#/components/schemas/Cas2StaffMember'
required:
- staffMember
applicationOrigin:
type: string
required:
- applicationId
- applicationUrl
Expand Down
16 changes: 16 additions & 0 deletions src/main/resources/static/codegen/built-api-spec.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6020,6 +6020,8 @@ components:
type: array
items:
$ref: '#/components/schemas/Cas2TimelineEvent'
applicationOrigin:
$ref: '#/components/schemas/ApplicationOrigin'
required:
- createdBy
- schemaVersion
Expand Down Expand Up @@ -6057,6 +6059,8 @@ components:
assessment:
type: object
$ref: '#/components/schemas/Cas2Assessment'
applicationOrigin:
$ref: '#/components/schemas/ApplicationOrigin'
required:
- id
- person
Expand Down Expand Up @@ -6221,6 +6225,8 @@ components:
type: string
nomsNumber:
type: string
applicationOrigin:
$ref: '#/components/schemas/ApplicationOrigin'
required:
- type
- id
Expand Down Expand Up @@ -6339,6 +6345,8 @@ components:
submittedAt:
type: string
format: date-time
applicationOrigin:
$ref: '#/components/schemas/ApplicationOrigin'
required:
- createdByUserId
- status
Expand Down Expand Up @@ -6641,6 +6649,8 @@ components:
format: date
telephoneNumber:
type: string
applicationOrigin:
$ref: '#/components/schemas/ApplicationOrigin'
required:
- translatedDocument
- applicationId
Expand Down Expand Up @@ -9215,3 +9225,9 @@ components:
- sharedProperty
- singleOccupancy
- wheelchairAccessible
ApplicationOrigin:
type: string
enum:
- courtBail
- prisonBail
- homeDetentionCurfew
16 changes: 16 additions & 0 deletions src/main/resources/static/codegen/built-cas1-api-spec.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2849,6 +2849,8 @@ components:
type: array
items:
$ref: '#/components/schemas/Cas2TimelineEvent'
applicationOrigin:
$ref: '#/components/schemas/ApplicationOrigin'
required:
- createdBy
- schemaVersion
Expand Down Expand Up @@ -2886,6 +2888,8 @@ components:
assessment:
type: object
$ref: '#/components/schemas/Cas2Assessment'
applicationOrigin:
$ref: '#/components/schemas/ApplicationOrigin'
required:
- id
- person
Expand Down Expand Up @@ -3050,6 +3054,8 @@ components:
type: string
nomsNumber:
type: string
applicationOrigin:
$ref: '#/components/schemas/ApplicationOrigin'
required:
- type
- id
Expand Down Expand Up @@ -3168,6 +3174,8 @@ components:
submittedAt:
type: string
format: date-time
applicationOrigin:
$ref: '#/components/schemas/ApplicationOrigin'
required:
- createdByUserId
- status
Expand Down Expand Up @@ -3470,6 +3478,8 @@ components:
format: date
telephoneNumber:
type: string
applicationOrigin:
$ref: '#/components/schemas/ApplicationOrigin'
required:
- translatedDocument
- applicationId
Expand Down Expand Up @@ -6044,6 +6054,12 @@ components:
- sharedProperty
- singleOccupancy
- wheelchairAccessible
ApplicationOrigin:
type: string
enum:
- courtBail
- prisonBail
- homeDetentionCurfew
Cas1PremisesBasicSummary:
type: object
properties:
Expand Down
Loading
Loading