Skip to content

Commit

Permalink
Merge pull request #2748 from ministryofjustice/feature/APS-1759_add_…
Browse files Browse the repository at this point in the history
…spacebooking

APS-1759: Add SpaceBooking information to Subject Access Request API
  • Loading branch information
RobBoothMOJ authored Jan 2, 2025
2 parents 83cf95e + 12d3f8a commit 2d942bd
Show file tree
Hide file tree
Showing 6 changed files with 363 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -441,21 +441,39 @@ from
select json_agg(offline_applications) as json
from (
select
B.CRN,
B.noms_number ,
CASE
WHEN B.id is NOT NULL THEN B.CRN
ELSE sb.crn
END AS crn,
CASE
WHEN B.id is NOT NULL THEN B.noms_number
ELSE null
END AS noms_number,
oa.id as offline_application_id,
b.id as booking_id,
CASE
WHEN B.id is NOT NULL THEN b.id
ELSE sb.id
END AS booking_id,
oa.created_at
from offline_applications oa
inner join bookings b on b.offline_application_id = oa.id
left join bookings b on b.offline_application_id = oa.id
left join cas1_space_bookings sb on sb.offline_application_id = oa.id
where
(b.crn = :crn
or
b.noms_number = :noms_number)
and
(:start_date::date is null or b.created_at >= :start_date)
and
(:end_date::date is null or b.created_at <= :end_date)
b.noms_number = :noms_number)
OR
(sb.crn = :crn)
and (
(:start_date::date is null or b.created_at >= :start_date)
and
(:end_date::date is null or b.created_at <= :end_date)
)
OR (
(:start_date::date is null or sb.created_at >= :start_date)
and
(:end_date::date is null or sb.created_at <= :end_date)
)
) offline_applications
""".trimIndent(),
MapSqlParameterSource()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,89 @@ open class SubjectAccessRequestRepositoryBase(val jdbcTemplate: NamedParameterJd
return toJsonString(result)
}

fun spaceBookings(
crn: String?,
nomsNumber: String?,
startDate: LocalDateTime?,
endDate: LocalDateTime?,
): String {
val result = jdbcTemplate.queryForMap(
"""
select json_agg(spaceBooking) as json
from (
select
b.crn,
a.noms_number,
b.canonical_arrival_date,
b.canonical_departure_date,
b.expected_arrival_date,
b.expected_departure_date,
b.actual_arrival_date,
b.actual_arrival_time,
b.actual_departure_date,
b.actual_departure_time,
b.non_arrival_confirmed_at,
b.non_arrival_notes,
b.non_arrival_reason_id,
apa.risk_ratings -> 'tier' -> 'value' ->> 'level' as tier,
b.created_at,
b.key_worker_staff_code,
b.key_worker_assigned_at,
b.key_worker_name,
b.approved_premises_application_id,
b.offline_application_id,
p."name" as premises_name,
b.delius_event_number,
b.placement_request_id,
b.created_by_user_id,
b.departure_reason_id,
b.departure_notes,
b.departure_move_on_category_id,
b.cancellation_reason_notes,
b.cancellation_reason_id,
b.cancellation_occurred_at,
b.cancellation_recorded_at,
b.migrated_management_info_from,
b.version,
(
SELECT STRING_AGG (characteristics.property_name, ',')
FROM cas1_space_bookings_criteria sbc
LEFT OUTER JOIN characteristics ON characteristics.id = sbc.characteristic_id
WHERE sbc.space_booking_id = b.id
GROUP by sbc.space_booking_id
) AS characteristics_property_names,
CASE
WHEN apa.id IS NOT NULL THEN apa.name
ELSE offline_app.name
END as person_name
FROM
cas1_space_bookings b
LEFT JOIN premises p ON
b.premises_id = p.id
LEFT OUTER JOIN approved_premises_applications apa ON
b.approved_premises_application_id = apa.id
LEFT OUTER JOIN offline_applications offline_app ON
b.offline_application_id = offline_app.id
LEFT OUTER JOIN
applications a on
a.id = apa.id
where
(b.crn = :crn
or a.noms_number = :noms_number )
and (:start_date::date is null or b.created_at >= :start_date)
and (:end_date::date is null or b.created_at <= :end_date)
) spaceBooking
""".trimIndent(),
MapSqlParameterSource().addSarParameters(
crn,
nomsNumber,
startDate,
endDate,
),
)
return toJsonString(result)
}

fun bookingExtensions(
crn: String?,
nomsNumber: String?,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class SubjectAccessRequestService(
val apAssessmentClarificationNotesJson = cas1SubjectAccessRequestRepository.getApprovedPremisesAssessmentClarificationNotes(crn, nomsNumber, startDate, endDate)

val apBookingsJson = cas1SubjectAccessRequestRepository.bookings(crn, nomsNumber, startDate, endDate)
val apSpaceBookingsJson = cas1SubjectAccessRequestRepository.spaceBookings(crn, nomsNumber, startDate, endDate)
val apBookingExtensionsJson = cas1SubjectAccessRequestRepository.bookingExtensions(crn, nomsNumber, startDate, endDate)
val apCancellationsJson = cas1SubjectAccessRequestRepository.cancellations(crn, nomsNumber, startDate, endDate)
val apBedMovesJson = cas1SubjectAccessRequestRepository.bedMoves(crn, nomsNumber, startDate, endDate)
Expand All @@ -53,6 +54,7 @@ class SubjectAccessRequestService(
"Assessments": $apAssessmentsJson,
"AssessmentClarificationNotes" : $apAssessmentClarificationNotesJson,
"Bookings": $apBookingsJson,
"SpaceBookings": $apSpaceBookingsJson,
"OfflineApplications": $offlineApplicationsJson,
"BookingExtensions": $apBookingExtensionsJson,
"Cancellations": $apCancellationsJson,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ class Cas1SpaceBookingEntityFactory : Factory<Cas1SpaceBookingEntity> {
this.departureReason = { departureReason }
}

fun withMoveOnCategory(moveOnCategory: MoveOnCategoryEntity) = apply {
fun withMoveOnCategory(moveOnCategory: MoveOnCategoryEntity?) = apply {
this.departureMoveOnCategory = { moveOnCategory }
}

Expand Down
Loading

0 comments on commit 2d942bd

Please sign in to comment.