Skip to content

Commit

Permalink
Merge pull request #2757 from ministryofjustice/feature/APS-1769_cas1…
Browse files Browse the repository at this point in the history
…_delius_seed_job

APS-1769 Update Cas1ImportDeliusBookingDataSeedJob to new format
  • Loading branch information
davidatkinsuk authored Jan 3, 2025
2 parents fbeebfe + 371daf0 commit d4bc81a
Show file tree
Hide file tree
Showing 12 changed files with 126 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ interface Cas1DeliusBookingImportRepository : JpaRepository<Cas1DeliusBookingImp
@Table(name = "cas1_delius_booking_import")
data class Cas1DeliusBookingImportEntity(
@Id
val bookingId: UUID,
val id: UUID,
val bookingId: UUID?,
val crn: String,
val eventNumber: String,
val keyWorkerStaffCode: String?,
Expand All @@ -37,4 +38,5 @@ data class Cas1DeliusBookingImportEntity(
val nonArrivalReasonCode: String?,
val nonArrivalReasonDescription: String?,
val nonArrivalNotes: String?,
val premisesQcode: String,
)
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ data class SeedColumns(
) {
fun getStringOrNull(label: String) = columns[label]?.trim()?.ifBlank { null }

fun getUuidOrNull(label: String) = columns[label]?.let { UUID.fromString(it.trim()) }
fun getUuidOrNull(label: String): UUID? = getStringOrNull(label)?.let { UUID.fromString(it) }

fun getIntOrNull(label: String) = columns[label]?.toIntOrNull()

Expand All @@ -25,7 +25,6 @@ data class SeedColumns(

fun getDateFromUtcDateTimeOrNull(label: String): LocalDate? {
val rawValue = getStringOrNull(label) ?: return null

return LocalDate.parse(rawValue.substring(startIndex = 0, endIndex = 10))
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import uk.gov.justice.digital.hmpps.approvedpremisesapi.seed.cas1.Cas1CruManagem
import uk.gov.justice.digital.hmpps.approvedpremisesapi.seed.cas1.Cas1DomainEventReplaySeedJob
import uk.gov.justice.digital.hmpps.approvedpremisesapi.seed.cas1.Cas1DuplicateApplicationSeedJob
import uk.gov.justice.digital.hmpps.approvedpremisesapi.seed.cas1.Cas1FurtherInfoBugFixSeedJob
import uk.gov.justice.digital.hmpps.approvedpremisesapi.seed.cas1.Cas1ImportDeliusBookingDataSeedJob
import uk.gov.justice.digital.hmpps.approvedpremisesapi.seed.cas1.Cas1ImportDeliusReferralsSeedJob
import uk.gov.justice.digital.hmpps.approvedpremisesapi.seed.cas1.Cas1LinkedBookingToPlacementRequestSeedJob
import uk.gov.justice.digital.hmpps.approvedpremisesapi.seed.cas1.Cas1OutOfServiceBedSeedJob
import uk.gov.justice.digital.hmpps.approvedpremisesapi.seed.cas1.Cas1PlanSpacePlanningDryRunSeedJob
Expand Down Expand Up @@ -87,7 +87,7 @@ class SeedService(
SeedFileType.approvedPremisesCruManagementAreas -> getBean(Cas1CruManagementAreaSeedJob::class)
SeedFileType.approvedPremisesBookingToSpaceBooking -> getBean(Cas1BookingToSpaceBookingSeedJob::class)
SeedFileType.approvedPremisesSpacePlanningDryRun -> getBean(Cas1PlanSpacePlanningDryRunSeedJob::class)
SeedFileType.approvedPremisesImportDeliusBookingManagementData -> getBean(Cas1ImportDeliusBookingDataSeedJob::class)
SeedFileType.approvedPremisesImportDeliusReferrals -> getBean(Cas1ImportDeliusReferralsSeedJob::class)
SeedFileType.approvedPremisesUpdateSpaceBooking -> getBean(Cas1UpdateSpaceBookingSeedJob::class)
SeedFileType.temporaryAccommodationReferralRejection -> getBean(Cas3ReferralRejectionSeedJob::class)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import java.time.format.DateTimeFormatter
import java.util.UUID

@Component
class Cas1ImportDeliusBookingDataSeedJob(
class Cas1ImportDeliusReferralsSeedJob(
private val jdbcTemplate: NamedParameterJdbcTemplate,
private val cas1DeliusBookingImportRepository: Cas1DeliusBookingImportRepository,
) : SeedJob<Cas1DeliusBookingManagementDataRow>(
Expand All @@ -39,6 +39,7 @@ class Cas1ImportDeliusBookingDataSeedJob(
"NON_ARRIVAL_REASON_CODE",
"NON_ARRIVAL_REASON_DESCRIPTION",
"NON_ARRIVAL_NOTES",
"HOSTEL_CODE",
),
runInTransaction = false,
) {
Expand All @@ -57,31 +58,51 @@ class Cas1ImportDeliusBookingDataSeedJob(
val seedColumns = SeedColumns(columns)

return Cas1DeliusBookingManagementDataRow(
bookingId = seedColumns.getUuidOrNull("BOOKING_ID")!!,
bookingId = seedColumns.getUuidOrNull("BOOKING_ID"),
crn = seedColumns.getStringOrNull("CRN")!!,
eventNumber = seedColumns.getStringOrNull("EVENT_NUMBER")!!,
keyWorkerStaffCode = seedColumns.getStringOrNull("KEY_WORKER_STAFF_CODE"),
keyWorkerStaffCode = seedColumns.getStringOrNullMinus1IsNull("KEY_WORKER_STAFF_CODE"),
keyWorkerForename = seedColumns.getStringOrNull("KEY_WORKER_FORENAME"),
keyWorkerMiddleName = seedColumns.getStringOrNull("KEY_WORKER_MIDDLE_NAME"),
keyWorkerSurname = seedColumns.getStringOrNull("KEY_WORKER_SURNAME"),
departureReasonCode = seedColumns.getStringOrNull("DEPARTURE_REASON_CODE"),
moveOnCategoryCode = seedColumns.getStringOrNull("MOVE_ON_CATEGORY_CODE"),
moveOnCategoryCode = seedColumns.getStringOrNullMinus1IsNull("MOVE_ON_CATEGORY_CODE"),
moveOnCategoryDescription = seedColumns.getStringOrNull("MOVE_ON_CATEGORY_DESCRIPTION"),
expectedArrivalDate = seedColumns.getDateFromUtcDateTimeOrNull("EXPECTED_ARRIVAL_DATE")!!,
arrivalDate = seedColumns.getDateFromUtcDateTimeOrNull("ARRIVAL_DATE"),
expectedDepartureDate = seedColumns.getDateFromUtcDateTimeOrNull("EXPECTED_DEPARTURE_DATE"),
departureDate = seedColumns.getDateFromUtcDateTimeOrNull("DEPARTURE_DATE"),
nonArrivalDate = seedColumns.getDateFromUtcDateTimeOrNull("NON_ARRIVAL_DATE"),
expectedArrivalDate = seedColumns.getDateFromUtcDateTimeOrNull1900IsNull("EXPECTED_ARRIVAL_DATE"),
arrivalDate = seedColumns.getDateFromUtcDateTimeOrNull1900IsNull("ARRIVAL_DATE"),
expectedDepartureDate = seedColumns.getDateFromUtcDateTimeOrNull1900IsNull("EXPECTED_DEPARTURE_DATE"),
departureDate = seedColumns.getDateFromUtcDateTimeOrNull1900IsNull("DEPARTURE_DATE"),
nonArrivalDate = seedColumns.getDateFromUtcDateTimeOrNull1900IsNull("NON_ARRIVAL_DATE"),
nonArrivalContactDateTime = seedColumns.getLastDateTimeFromListOrNull("NON_ARRIVAL_CONTACT_DATETIME_LIST", DELIUS_IMPORT_DATE_TIME_FORMATTER),
nonArrivalReasonCode = seedColumns.getStringOrNull("NON_ARRIVAL_REASON_CODE"),
nonArrivalReasonCode = seedColumns.getStringOrNullMinus1IsNull("NON_ARRIVAL_REASON_CODE"),
nonArrivalReasonDescription = seedColumns.getStringOrNull("NON_ARRIVAL_REASON_DESCRIPTION"),
nonArrivalNotes = seedColumns.getStringOrNull("NON_ARRIVAL_NOTES"),
premisesQCode = seedColumns.getStringOrNull("HOSTEL_CODE")!!,
)
}

@SuppressWarnings("MagicNumber")
private fun SeedColumns.getDateFromUtcDateTimeOrNull1900IsNull(label: String): LocalDate? {
val date = getDateFromUtcDateTimeOrNull(label)
if (date?.year == 1900) {
return null
}
return date
}

private fun SeedColumns.getStringOrNullMinus1IsNull(label: String): String? {
val value = getStringOrNull(label)
return if (value == "-1") null else value
}

override fun processRow(row: Cas1DeliusBookingManagementDataRow) {
if (row.expectedArrivalDate == null) {
return
}

cas1DeliusBookingImportRepository.save(
Cas1DeliusBookingImportEntity(
id = UUID.randomUUID(),
row.bookingId,
row.crn,
row.eventNumber,
Expand All @@ -103,6 +124,7 @@ class Cas1ImportDeliusBookingDataSeedJob(
row.nonArrivalReasonCode,
row.nonArrivalReasonDescription,
row.nonArrivalNotes,
row.premisesQCode,
),
)
}
Expand All @@ -118,7 +140,7 @@ class Cas1ImportDeliusBookingDataSeedJob(
}

data class Cas1DeliusBookingManagementDataRow(
val bookingId: UUID,
val bookingId: UUID?,
val crn: String,
val eventNumber: String,
val keyWorkerStaffCode: String?,
Expand All @@ -128,7 +150,7 @@ data class Cas1DeliusBookingManagementDataRow(
val departureReasonCode: String?,
val moveOnCategoryCode: String?,
val moveOnCategoryDescription: String?,
val expectedArrivalDate: LocalDate,
val expectedArrivalDate: LocalDate?,
val arrivalDate: LocalDate?,
val expectedDepartureDate: LocalDate?,
val departureDate: LocalDate?,
Expand All @@ -137,4 +159,5 @@ data class Cas1DeliusBookingManagementDataRow(
val nonArrivalReasonCode: String?,
val nonArrivalReasonDescription: String?,
val nonArrivalNotes: String?,
val premisesQCode: String,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
TRUNCATE cas1_delius_booking_import;

ALTER TABLE cas1_delius_booking_import ADD COLUMN id uuid NOT NULL;
ALTER TABLE cas1_delius_booking_import DROP CONSTRAINT cas1_delius_booking_import_pk;
ALTER TABLE cas1_delius_booking_import ADD CONSTRAINT cas1_delius_booking_import_pk PRIMARY KEY (id);

ALTER TABLE cas1_delius_booking_import ALTER COLUMN booking_id DROP NOT NULL;
CREATE INDEX cas1_delius_booking_import_booking_id_idx ON cas1_delius_booking_import (booking_id);

ALTER TABLE cas1_delius_booking_import ADD COLUMN premises_qcode TEXT NOT NULL;
2 changes: 1 addition & 1 deletion src/main/resources/static/_shared.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3724,7 +3724,7 @@ components:
- approved_premises_out_of_service_beds
- approved_premises_cru_management_areas
- approved_premises_space_planning_dry_run
- approved_premises_import_delius_booking_management_data
- approved_premises_import_delius_referrals
- approved_premises_update_space_booking
- temporary_accommodation_referral_rejection
SeedFromExcelFileType:
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/static/codegen/built-api-spec.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8025,7 +8025,7 @@ components:
- approved_premises_out_of_service_beds
- approved_premises_cru_management_areas
- approved_premises_space_planning_dry_run
- approved_premises_import_delius_booking_management_data
- approved_premises_import_delius_referrals
- approved_premises_update_space_booking
- temporary_accommodation_referral_rejection
SeedFromExcelFileType:
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/static/codegen/built-cas1-api-spec.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4946,7 +4946,7 @@ components:
- approved_premises_out_of_service_beds
- approved_premises_cru_management_areas
- approved_premises_space_planning_dry_run
- approved_premises_import_delius_booking_management_data
- approved_premises_import_delius_referrals
- approved_premises_update_space_booking
- temporary_accommodation_referral_rejection
SeedFromExcelFileType:
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/static/codegen/built-cas2-api-spec.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4315,7 +4315,7 @@ components:
- approved_premises_out_of_service_beds
- approved_premises_cru_management_areas
- approved_premises_space_planning_dry_run
- approved_premises_import_delius_booking_management_data
- approved_premises_import_delius_referrals
- approved_premises_update_space_booking
- temporary_accommodation_referral_rejection
SeedFromExcelFileType:
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/static/codegen/built-cas3-api-spec.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3823,7 +3823,7 @@ components:
- approved_premises_out_of_service_beds
- approved_premises_cru_management_areas
- approved_premises_space_planning_dry_run
- approved_premises_import_delius_booking_management_data
- approved_premises_import_delius_referrals
- approved_premises_update_space_booking
- temporary_accommodation_referral_rejection
SeedFromExcelFileType:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,32 +13,48 @@ import java.time.OffsetDateTime
import java.util.UUID

@TestInstance(TestInstance.Lifecycle.PER_METHOD)
class SeedCas1ImportDeliusBookingDataSeedJobTest : SeedTestBase() {
class SeedCas1ImportDeliusReferralsSeedJobTest : SeedTestBase() {

@Autowired
lateinit var cas1DeliusBookingImportRepository: Cas1DeliusBookingImportRepository

@Test
fun `Row with only mandatory fields can be processed`() {
val bookingId = UUID.randomUUID()
fun `Row with no expected arrival date is ignored`() {
withCsv(
csvName = "valid-csv",
contents = listOf(
Cas1DeliusBookingManagementDataRowRaw(
crn = "CRN1",
eventNumber = "1",
hostelCode = "hostel code",
),
).toCsv(),
)

seedService.seedData(SeedFileType.approvedPremisesImportDeliusReferrals, "valid-csv.csv")

assertThat(cas1DeliusBookingImportRepository.findAll()).isEmpty()
}

@Test
fun `Row with only mandatory fields can be processed`() {
withCsv(
csvName = "valid-csv",
contents = listOf(
Cas1DeliusBookingManagementDataRowRaw(
bookingId = bookingId.toString(),
crn = "CRN1",
eventNumber = "1",
expectedArrivalDate = "2024-06-15 00:00:00",
hostelCode = "hostel code",
),
).toCsv(),
)

seedService.seedData(SeedFileType.approvedPremisesImportDeliusBookingManagementData, "valid-csv.csv")
seedService.seedData(SeedFileType.approvedPremisesImportDeliusReferrals, "valid-csv.csv")

val bookingImport = cas1DeliusBookingImportRepository.findAll()[0]

assertThat(bookingImport.bookingId).isEqualTo(bookingId)
assertThat(bookingImport.bookingId).isNull()
assertThat(bookingImport.crn).isEqualTo("CRN1")
assertThat(bookingImport.eventNumber).isEqualTo("1")
assertThat(bookingImport.keyWorkerStaffCode).isNull()
Expand All @@ -57,6 +73,7 @@ class SeedCas1ImportDeliusBookingDataSeedJobTest : SeedTestBase() {
assertThat(bookingImport.nonArrivalReasonCode).isNull()
assertThat(bookingImport.nonArrivalReasonDescription).isNull()
assertThat(bookingImport.nonArrivalNotes).isNull()
assertThat(bookingImport.premisesQcode).isEqualTo("hostel code")
}

@Test
Expand Down Expand Up @@ -86,11 +103,12 @@ class SeedCas1ImportDeliusBookingDataSeedJobTest : SeedTestBase() {
nonArrivalReasonCode = "non arrival reason code",
nonArrivalReasonDescription = "non arrival reason description",
nonArrivalNotes = "non arrival notes",
hostelCode = "hostel code",
),
).toCsv(),
)

seedService.seedData(SeedFileType.approvedPremisesImportDeliusBookingManagementData, "valid-csv.csv")
seedService.seedData(SeedFileType.approvedPremisesImportDeliusReferrals, "valid-csv.csv")

val bookingImport = cas1DeliusBookingImportRepository.findAll()[0]

Expand All @@ -113,6 +131,44 @@ class SeedCas1ImportDeliusBookingDataSeedJobTest : SeedTestBase() {
assertThat(bookingImport.nonArrivalReasonCode).isEqualTo("non arrival reason code")
assertThat(bookingImport.nonArrivalReasonDescription).isEqualTo("non arrival reason description")
assertThat(bookingImport.nonArrivalNotes).isEqualTo("non arrival notes")
assertThat(bookingImport.premisesQcode).isEqualTo("hostel code")
}

@Test
fun `Fields containing invalid data are set to null`() {
val bookingId = UUID.randomUUID()

withCsv(
csvName = "valid-csv",
contents = listOf(
Cas1DeliusBookingManagementDataRowRaw(
bookingId = bookingId.toString(),
crn = "CRN1",
eventNumber = "1",
expectedArrivalDate = "2024-06-15 00:00:00",
hostelCode = "hostel code",
keyWorkerStaffCode = "-1",
moveOnCategoryCode = "-1",
nonArrivalReasonCode = "-1",
arrivalDate = "1900-01-01 00:00:00",
expectedDepartureDate = "1900-01-01 00:00:00",
departureDate = "1900-01-01 00:00:00",
nonArrivalDate = "1900-01-01 00:00:00",
),
).toCsv(),
)

seedService.seedData(SeedFileType.approvedPremisesImportDeliusReferrals, "valid-csv.csv")

val bookingImport = cas1DeliusBookingImportRepository.findAll()[0]

assertThat(bookingImport.keyWorkerStaffCode).isNull()
assertThat(bookingImport.moveOnCategoryCode).isNull()
assertThat(bookingImport.nonArrivalReasonCode).isNull()
assertThat(bookingImport.arrivalDate).isNull()
assertThat(bookingImport.expectedDepartureDate).isNull()
assertThat(bookingImport.departureDate).isNull()
assertThat(bookingImport.nonArrivalDate).isNull()
}

private fun List<Cas1DeliusBookingManagementDataRowRaw>.toCsv(): String {
Expand All @@ -137,6 +193,7 @@ class SeedCas1ImportDeliusBookingDataSeedJobTest : SeedTestBase() {
"NON_ARRIVAL_REASON_CODE",
"NON_ARRIVAL_REASON_DESCRIPTION",
"NON_ARRIVAL_NOTES",
"HOSTEL_CODE",
)
.newRow()

Expand All @@ -161,6 +218,7 @@ class SeedCas1ImportDeliusBookingDataSeedJobTest : SeedTestBase() {
.withQuotedField(it.nonArrivalReasonCode)
.withQuotedField(it.nonArrivalReasonDescription)
.withQuotedField(it.nonArrivalNotes)
.withQuotedField(it.hostelCode)
.newRow()
}

Expand All @@ -187,5 +245,6 @@ class SeedCas1ImportDeliusBookingDataSeedJobTest : SeedTestBase() {
val nonArrivalReasonCode: String = "",
val nonArrivalReasonDescription: String = "",
val nonArrivalNotes: String = "",
val hostelCode: String = "",
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ class SeedCas1BookingToSpaceBookingTest : SeedTestBase() {

deliusBookingImportRepository.save(
Cas1DeliusBookingImportEntity(
id = UUID.randomUUID(),
bookingId = booking1ManagementInfoFromDelius.id,
crn = "irrelevant",
eventNumber = "irrelevant",
Expand All @@ -161,6 +162,7 @@ class SeedCas1BookingToSpaceBookingTest : SeedTestBase() {
nonArrivalReasonCode = "narc1",
nonArrivalReasonDescription = null,
nonArrivalNotes = "the non arrival notes",
premisesQcode = "hostel code",
),
)

Expand Down Expand Up @@ -189,6 +191,7 @@ class SeedCas1BookingToSpaceBookingTest : SeedTestBase() {

deliusBookingImportRepository.save(
Cas1DeliusBookingImportEntity(
id = UUID.randomUUID(),
bookingId = booking2MinimalManagementInfoFromDelius.id,
crn = "irrelevant",
eventNumber = "irrelevant",
Expand All @@ -208,6 +211,7 @@ class SeedCas1BookingToSpaceBookingTest : SeedTestBase() {
nonArrivalReasonCode = null,
nonArrivalReasonDescription = null,
nonArrivalNotes = null,
premisesQcode = "hostel code",
),
)

Expand Down

0 comments on commit d4bc81a

Please sign in to comment.