generated from ministryofjustice/hmpps-template-kotlin
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1351 from ministryofjustice/feature/aps-193-add-u…
…rls-to-timeline-reponses APS-193 add urls to timeline reponses
- Loading branch information
Showing
28 changed files
with
534 additions
and
99 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
...n/kotlin/uk/gov/justice/digital/hmpps/approvedpremisesapi/convert/UrlTemplateConverter.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package uk.gov.justice.digital.hmpps.approvedpremisesapi.convert | ||
|
||
import org.springframework.core.convert.TypeDescriptor | ||
import org.springframework.core.convert.converter.GenericConverter | ||
import org.springframework.stereotype.Component | ||
import uk.gov.justice.digital.hmpps.approvedpremisesapi.util.UrlTemplate | ||
|
||
@Component | ||
class UrlTemplateConverter : GenericConverter { | ||
override fun getConvertibleTypes(): MutableSet<GenericConverter.ConvertiblePair> { | ||
return mutableSetOf(GenericConverter.ConvertiblePair(String::class.java, UrlTemplate::class.java)) | ||
} | ||
|
||
override fun convert(source: Any?, sourceType: TypeDescriptor, targetType: TypeDescriptor): Any { | ||
val input = source as String | ||
return UrlTemplate(input) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 11 additions & 6 deletions
17
src/main/kotlin/uk/gov/justice/digital/hmpps/approvedpremisesapi/model/DomainEventSummary.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,15 @@ | ||
package uk.gov.justice.digital.hmpps.approvedpremisesapi.model | ||
|
||
import uk.gov.justice.digital.hmpps.approvedpremisesapi.jpa.entity.DomainEventType | ||
import java.time.OffsetDateTime | ||
import java.sql.Timestamp | ||
import java.util.UUID | ||
|
||
data class DomainEventSummary( | ||
val id: String, | ||
val type: DomainEventType, | ||
val occurredAt: OffsetDateTime, | ||
) | ||
interface DomainEventSummary { | ||
val id: String | ||
val type: DomainEventType | ||
val occurredAt: Timestamp | ||
val applicationId: UUID? | ||
val assessmentId: UUID? | ||
val bookingId: UUID? | ||
val premisesId: UUID? | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
...v/justice/digital/hmpps/approvedpremisesapi/transformer/ApplicationTimelineTransformer.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package uk.gov.justice.digital.hmpps.approvedpremisesapi.transformer | ||
|
||
import org.springframework.beans.factory.annotation.Value | ||
import org.springframework.stereotype.Component | ||
import uk.gov.justice.digital.hmpps.approvedpremisesapi.api.model.TimelineEvent | ||
import uk.gov.justice.digital.hmpps.approvedpremisesapi.api.model.TimelineEventAssociatedUrl | ||
import uk.gov.justice.digital.hmpps.approvedpremisesapi.api.model.TimelineEventType | ||
import uk.gov.justice.digital.hmpps.approvedpremisesapi.api.model.TimelineEventUrlType | ||
import uk.gov.justice.digital.hmpps.approvedpremisesapi.jpa.entity.DomainEventType | ||
import uk.gov.justice.digital.hmpps.approvedpremisesapi.model.DomainEventSummary | ||
import uk.gov.justice.digital.hmpps.approvedpremisesapi.util.UrlTemplate | ||
|
||
@Component | ||
class ApplicationTimelineTransformer( | ||
@Value("\${url-templates.frontend.application}") private val applicationUrlTemplate: UrlTemplate, | ||
@Value("\${url-templates.frontend.assessment}") private val assessmentUrlTemplate: UrlTemplate, | ||
@Value("\${url-templates.frontend.booking}") private val bookingUrlTemplate: UrlTemplate, | ||
) { | ||
|
||
fun transformDomainEventSummaryToTimelineEvent(domainEventSummary: DomainEventSummary): TimelineEvent { | ||
val associatedUrls = listOfNotNull( | ||
applicationUrlOrNull(domainEventSummary), | ||
assessmentUrlOrNull(domainEventSummary), | ||
bookingUrlOrNull(domainEventSummary), | ||
) | ||
|
||
return TimelineEvent( | ||
id = domainEventSummary.id, | ||
type = transformDomainEventTypeToTimelineEventType(domainEventSummary.type), | ||
occurredAt = domainEventSummary.occurredAt.toInstant(), | ||
associatedUrls = associatedUrls, | ||
) | ||
} | ||
|
||
private fun applicationUrlOrNull(domainEventSummary: DomainEventSummary) = domainEventSummary.applicationId?.let { | ||
TimelineEventAssociatedUrl( | ||
TimelineEventUrlType.application, | ||
applicationUrlTemplate.resolve(mapOf("id" to domainEventSummary.applicationId.toString())), | ||
) | ||
} | ||
|
||
private fun assessmentUrlOrNull(domainEventSummary: DomainEventSummary) = domainEventSummary.assessmentId?.let { | ||
TimelineEventAssociatedUrl( | ||
TimelineEventUrlType.assessment, | ||
assessmentUrlTemplate.resolve(mapOf("id" to domainEventSummary.assessmentId.toString())), | ||
) | ||
} | ||
|
||
private fun bookingUrlOrNull(domainEventSummary: DomainEventSummary) = domainEventSummary.bookingId?.let { | ||
domainEventSummary.premisesId?.let { | ||
TimelineEventAssociatedUrl( | ||
TimelineEventUrlType.booking, | ||
bookingUrlTemplate.resolve( | ||
mapOf( | ||
"premisesId" to domainEventSummary.premisesId.toString(), | ||
"bookingId" to domainEventSummary.bookingId.toString(), | ||
), | ||
), | ||
) | ||
} | ||
} | ||
|
||
fun transformDomainEventTypeToTimelineEventType(domainEventType: DomainEventType): TimelineEventType { | ||
return when (domainEventType) { | ||
DomainEventType.APPROVED_PREMISES_APPLICATION_SUBMITTED -> TimelineEventType.approvedPremisesApplicationSubmitted | ||
DomainEventType.APPROVED_PREMISES_APPLICATION_ASSESSED -> TimelineEventType.approvedPremisesApplicationAssessed | ||
DomainEventType.APPROVED_PREMISES_BOOKING_MADE -> TimelineEventType.approvedPremisesBookingMade | ||
DomainEventType.APPROVED_PREMISES_PERSON_ARRIVED -> TimelineEventType.approvedPremisesPersonArrived | ||
DomainEventType.APPROVED_PREMISES_PERSON_NOT_ARRIVED -> TimelineEventType.approvedPremisesPersonNotArrived | ||
DomainEventType.APPROVED_PREMISES_PERSON_DEPARTED -> TimelineEventType.approvedPremisesPersonDeparted | ||
DomainEventType.APPROVED_PREMISES_BOOKING_NOT_MADE -> TimelineEventType.approvedPremisesBookingNotMade | ||
DomainEventType.APPROVED_PREMISES_BOOKING_CANCELLED -> TimelineEventType.approvedPremisesBookingCancelled | ||
DomainEventType.APPROVED_PREMISES_BOOKING_CHANGED -> TimelineEventType.approvedPremisesBookingChanged | ||
DomainEventType.APPROVED_PREMISES_APPLICATION_WITHDRAWN -> TimelineEventType.approvedPremisesApplicationWithdrawn | ||
else -> throw IllegalArgumentException("Only CAS1 is currently supported") | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletions
6
src/main/kotlin/uk/gov/justice/digital/hmpps/approvedpremisesapi/util/UrlTemplate.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package uk.gov.justice.digital.hmpps.approvedpremisesapi.util | ||
|
||
class UrlTemplate(val template: String) { | ||
fun resolve(args: Map<String, String>) = | ||
args.entries.fold(template) { acc, (key, value) -> acc.replace("#$key", value) } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
src/main/resources/db/migration/all/20240117111637__add_assessment_id_to_domain_event.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
ALTER TABLE domain_events ADD COLUMN assessment_id UUID NULL; |
34 changes: 34 additions & 0 deletions
34
src/main/resources/db/migration/all/20240117142156__backfill_domain_event_assessment_id.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
UPDATE domain_events d | ||
SET assessment_id = a.id | ||
FROM assessments a | ||
WHERE d.type = 'APPROVED_PREMISES_APPLICATION_ASSESSED' AND | ||
d.assessment_id IS NULL AND | ||
a.application_id = d.application_id AND | ||
a.reallocated_at IS NULL AND | ||
a.decision IS NOT NULL AND | ||
a.submitted_at = d.occurred_at; | ||
|
||
-- there are a few domain events in prod that don't match the above rule | ||
-- for those we assign them a completed assessment related to the same | ||
-- application when there is one and only one such assessment | ||
|
||
UPDATE domain_events d | ||
SET assessment_id = a.id | ||
FROM assessments a | ||
WHERE | ||
d.type = 'APPROVED_PREMISES_APPLICATION_ASSESSED' AND | ||
d.assessment_id IS NULL AND | ||
a.application_id = d.application_id AND | ||
a.reallocated_at IS NULL AND | ||
a.decision IS NOT NULL AND | ||
d.id IN ( | ||
SELECT d1.id | ||
FROM domain_events d1 | ||
INNER JOIN assessments a1 ON a1.application_id = d1.application_id | ||
WHERE d1.type = 'APPROVED_PREMISES_APPLICATION_ASSESSED' AND | ||
d1.assessment_id IS NULL AND | ||
a1.reallocated_at IS NULL AND | ||
a1.decision IS NOT NULL | ||
GROUP BY d1.id | ||
HAVING count(a1.id) = 1 | ||
); |
Oops, something went wrong.