Skip to content

Commit

Permalink
Fix premises seed and throw exception on failure
Browse files Browse the repository at this point in the history
This commit fixes teh approved premises seed CSV file, adding a missing column name.

It also changes the auto seed behaviour to throw an exception if seeding fails, instead of logging an error and carrying on. This will help us spot seeding errors instead of relying on e2e test failure to determine seeding failed.
  • Loading branch information
davidatkinsuk committed Sep 26, 2024
1 parent dfdb4fa commit 72bc54c
Show file tree
Hide file tree
Showing 2 changed files with 162 additions and 157 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ class SeedService(
csv.file.path
}

seedData(seedFileType, seedFileType.value) { filePath }
seedDataThrowExceptions(seedFileType, seedFileType.value) { filePath }
}
}
}
Expand Down Expand Up @@ -146,167 +146,172 @@ class SeedService(
fun seedData(seedFileType: SeedFileType, filename: String) = seedData(seedFileType, filename) { "${seedConfig.filePrefix}/${this.fileName}" }

private fun seedData(seedFileType: SeedFileType, filename: String, resolveCsvPath: SeedJob<*>.() -> String) {
seedLogger.info("Starting seed request: $seedFileType - $filename")

try {
val job: SeedJob<*> = when (seedFileType) {
SeedFileType.approvedPremises -> ApprovedPremisesSeedJob(
filename,
getBean(PremisesRepository::class),
getBean(ProbationRegionRepository::class),
getBean(LocalAuthorityAreaRepository::class),
getBean(CharacteristicRepository::class),
)
SeedFileType.approvedPremisesRooms -> ApprovedPremisesRoomsSeedJob(
filename,
getBean(PremisesRepository::class),
getBean(RoomRepository::class),
getBean(BedRepository::class),
getBean(CharacteristicRepository::class),
)
SeedFileType.user -> UsersSeedJob(
filename,
ServiceName.values().toList(),
getBean(UserService::class),
)
SeedFileType.approvedPremisesApStaffUsers -> ApStaffUsersSeedJob(
filename,
getBean(UserService::class),
seedLogger,
)
SeedFileType.nomisUsers -> NomisUsersSeedJob(
filename,
getBean(NomisUserRepository::class),
)
SeedFileType.externalUsers -> ExternalUsersSeedJob(
filename,
getBean(ExternalUserRepository::class),
)
SeedFileType.cas2Applications -> Cas2ApplicationsSeedJob(
filename,
getBean(Cas2ApplicationRepository::class),
getBean(NomisUserRepository::class),
getBean(ExternalUserRepository::class),
getBean(Cas2StatusUpdateRepository::class),
getBean(Cas2AssessmentRepository::class),
getBean(JsonSchemaService::class),
getBean(Cas2PersistedApplicationStatusFinder::class),
)
SeedFileType.approvedPremisesUsers -> UsersSeedJob(
filename,
listOf(ServiceName.approvedPremises),
getBean(UserService::class),
)
SeedFileType.temporaryAccommodationUsers -> UsersSeedJob(
filename,
listOf(ServiceName.temporaryAccommodation),
getBean(UserService::class),
)
SeedFileType.characteristics -> CharacteristicsSeedJob(
filename,
getBean(CharacteristicRepository::class),
)
SeedFileType.updateNomsNumber -> Cas1UpdateNomsNumberSeedJob(
filename,
getBean(ApplicationRepository::class),
getBean(ApplicationTimelineNoteService::class),
getBean(BookingRepository::class),
)
SeedFileType.temporaryAccommodationPremises -> TemporaryAccommodationPremisesSeedJob(
filename,
getBean(PremisesRepository::class),
getBean(ProbationRegionRepository::class),
getBean(LocalAuthorityAreaRepository::class),
getBean(ProbationDeliveryUnitRepository::class),
getBean(CharacteristicService::class),
)
SeedFileType.temporaryAccommodationBedspace -> TemporaryAccommodationBedspaceSeedJob(
filename,
getBean(PremisesRepository::class),
getBean(CharacteristicService::class),
getBean(RoomService::class),
)

SeedFileType.approvedPremisesCancelBookings -> ApprovedPremisesBookingCancelSeedJob(
filename,
getBean(BookingService::class),
getBean(BookingRepository::class),
)

SeedFileType.approvedPremisesApAreaEmailAddresses -> Cas1ApAreaEmailAddressSeedJob(
filename,
getBean(ApAreaRepository::class),
)

SeedFileType.approvedPremisesBookingAdhocProperty -> Cas1BookingAdhocPropertySeedJob(
filename,
getBean(BookingRepository::class),
)

SeedFileType.approvedPremisesAssessmentMoreInfoBugFix -> Cas1FurtherInfoBugFixSeedJob(
filename,
getBean(AssessmentRepository::class),
)

SeedFileType.approvedPremisesRedactAssessmentDetails -> Cas1RemoveAssessmentDetailsSeedJob(
filename,
getBean(AssessmentRepository::class),
getBean(ObjectMapper::class),
getBean(ApplicationService::class),
)

SeedFileType.approvedPremisesWithdrawPlacementRequest -> Cas1WithdrawPlacementRequestSeedJob(
filename,
getBean(PlacementRequestService::class),
getBean(ApplicationService::class),
)

SeedFileType.approvedPremisesReplayDomainEvents -> Cas1DomainEventReplaySeedJob(
filename,
getBean(DomainEventService::class),
)

SeedFileType.approvedPremisesDuplicateApplication -> Cas1DuplicateApplicationSeedJob(
filename,
getBean(ApplicationService::class),
getBean(OffenderService::class),
)

SeedFileType.approvedPremisesUpdateEventNumber -> Cas1UpdateEventNumberSeedJob(
filename,
getBean(ApplicationService::class),
getBean(ApplicationRepository::class),
getBean(DomainEventRepository::class),
getBean(ObjectMapper::class),
)

SeedFileType.approvedPremisesLinkBookingToPlacementRequest -> Cas1LinkedBookingToPlacementRequestSeedJob(
filename,
getBean(PlacementRequestRepository::class),
getBean(BookingRepository::class),
getBean(ApplicationTimelineNoteService::class),
)

SeedFileType.approvedPremisesOutOfServiceBeds -> Cas1OutOfServiceBedSeedJob(
filename,
getBean(Cas1OutOfServiceBedService::class),
getBean(PremisesService::class),
)

SeedFileType.updateUsersFromApi -> UpdateUsersFromApiSeedJob(
filename,
getBean(UserService::class),
)
}

transactionTemplate.executeWithoutResult { processJob(job, resolveCsvPath) }

seedLogger.info("Seed request complete")
seedDataThrowExceptions(seedFileType, filename, resolveCsvPath)
} catch (exception: Exception) {
seedLogger.error("Unable to complete Seed Job", exception)
}
}

@SuppressWarnings("CyclomaticComplexMethod")
private fun seedDataThrowExceptions(seedFileType: SeedFileType, filename: String, resolveCsvPath: SeedJob<*>.() -> String) {
seedLogger.info("Starting seed request: $seedFileType - $filename")

val job: SeedJob<*> = when (seedFileType) {
SeedFileType.approvedPremises -> ApprovedPremisesSeedJob(
filename,
getBean(PremisesRepository::class),
getBean(ProbationRegionRepository::class),
getBean(LocalAuthorityAreaRepository::class),
getBean(CharacteristicRepository::class),
)
SeedFileType.approvedPremisesRooms -> ApprovedPremisesRoomsSeedJob(
filename,
getBean(PremisesRepository::class),
getBean(RoomRepository::class),
getBean(BedRepository::class),
getBean(CharacteristicRepository::class),
)
SeedFileType.user -> UsersSeedJob(
filename,
ServiceName.values().toList(),
getBean(UserService::class),
)
SeedFileType.approvedPremisesApStaffUsers -> ApStaffUsersSeedJob(
filename,
getBean(UserService::class),
seedLogger,
)
SeedFileType.nomisUsers -> NomisUsersSeedJob(
filename,
getBean(NomisUserRepository::class),
)
SeedFileType.externalUsers -> ExternalUsersSeedJob(
filename,
getBean(ExternalUserRepository::class),
)
SeedFileType.cas2Applications -> Cas2ApplicationsSeedJob(
filename,
getBean(Cas2ApplicationRepository::class),
getBean(NomisUserRepository::class),
getBean(ExternalUserRepository::class),
getBean(Cas2StatusUpdateRepository::class),
getBean(Cas2AssessmentRepository::class),
getBean(JsonSchemaService::class),
getBean(Cas2PersistedApplicationStatusFinder::class),
)
SeedFileType.approvedPremisesUsers -> UsersSeedJob(
filename,
listOf(ServiceName.approvedPremises),
getBean(UserService::class),
)
SeedFileType.temporaryAccommodationUsers -> UsersSeedJob(
filename,
listOf(ServiceName.temporaryAccommodation),
getBean(UserService::class),
)
SeedFileType.characteristics -> CharacteristicsSeedJob(
filename,
getBean(CharacteristicRepository::class),
)
SeedFileType.updateNomsNumber -> Cas1UpdateNomsNumberSeedJob(
filename,
getBean(ApplicationRepository::class),
getBean(ApplicationTimelineNoteService::class),
getBean(BookingRepository::class),
)
SeedFileType.temporaryAccommodationPremises -> TemporaryAccommodationPremisesSeedJob(
filename,
getBean(PremisesRepository::class),
getBean(ProbationRegionRepository::class),
getBean(LocalAuthorityAreaRepository::class),
getBean(ProbationDeliveryUnitRepository::class),
getBean(CharacteristicService::class),
)
SeedFileType.temporaryAccommodationBedspace -> TemporaryAccommodationBedspaceSeedJob(
filename,
getBean(PremisesRepository::class),
getBean(CharacteristicService::class),
getBean(RoomService::class),
)

SeedFileType.approvedPremisesCancelBookings -> ApprovedPremisesBookingCancelSeedJob(
filename,
getBean(BookingService::class),
getBean(BookingRepository::class),
)

SeedFileType.approvedPremisesApAreaEmailAddresses -> Cas1ApAreaEmailAddressSeedJob(
filename,
getBean(ApAreaRepository::class),
)

SeedFileType.approvedPremisesBookingAdhocProperty -> Cas1BookingAdhocPropertySeedJob(
filename,
getBean(BookingRepository::class),
)

SeedFileType.approvedPremisesAssessmentMoreInfoBugFix -> Cas1FurtherInfoBugFixSeedJob(
filename,
getBean(AssessmentRepository::class),
)

SeedFileType.approvedPremisesRedactAssessmentDetails -> Cas1RemoveAssessmentDetailsSeedJob(
filename,
getBean(AssessmentRepository::class),
getBean(ObjectMapper::class),
getBean(ApplicationService::class),
)

SeedFileType.approvedPremisesWithdrawPlacementRequest -> Cas1WithdrawPlacementRequestSeedJob(
filename,
getBean(PlacementRequestService::class),
getBean(ApplicationService::class),
)

SeedFileType.approvedPremisesReplayDomainEvents -> Cas1DomainEventReplaySeedJob(
filename,
getBean(DomainEventService::class),
)

SeedFileType.approvedPremisesDuplicateApplication -> Cas1DuplicateApplicationSeedJob(
filename,
getBean(ApplicationService::class),
getBean(OffenderService::class),
)

SeedFileType.approvedPremisesUpdateEventNumber -> Cas1UpdateEventNumberSeedJob(
filename,
getBean(ApplicationService::class),
getBean(ApplicationRepository::class),
getBean(DomainEventRepository::class),
getBean(ObjectMapper::class),
)

SeedFileType.approvedPremisesLinkBookingToPlacementRequest -> Cas1LinkedBookingToPlacementRequestSeedJob(
filename,
getBean(PlacementRequestRepository::class),
getBean(BookingRepository::class),
getBean(ApplicationTimelineNoteService::class),
)

SeedFileType.approvedPremisesOutOfServiceBeds -> Cas1OutOfServiceBedSeedJob(
filename,
getBean(Cas1OutOfServiceBedService::class),
getBean(PremisesService::class),
)

SeedFileType.updateUsersFromApi -> UpdateUsersFromApiSeedJob(
filename,
getBean(UserService::class),
)
}

transactionTemplate.executeWithoutResult { processJob(job, resolveCsvPath) }

seedLogger.info("Seed request complete")
}

private fun <T : Any> getBean(clazz: KClass<T>) = applicationContext.getBean(clazz.java)

private fun <T> processJob(job: SeedJob<T>, resolveCsvPath: SeedJob<T>.() -> String) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name,addressLine1,addressLine2,town,postcode,latitude,longitude,notes,emailAddress,probationRegion,localAuthorityArea,characteristics,isIAP,isPIPE,isESAP,isSemiSpecialistMentalHealth,isRecoveryFocussed,isSuitableForVulnerable,acceptsSexOffenders,acceptsChildSexOffenders,acceptsNonSexualChildOffenders,acceptsHateCrimeOffenders,isCatered,hasWideStepFreeAccess,hasWideAccessToCommunalAreas,hasStepFreeAccessToCommunalAreas,hasWheelChairAccessibleBathrooms,hasLift,hasTactileFlooring,hasBrailleSignage,hasHearingLoop,status,apCode,qCode
name,addressLine1,addressLine2,town,postcode,latitude,longitude,notes,emailAddress,probationRegion,localAuthorityArea,characteristics,isIAP,isPIPE,isESAP,isSemiSpecialistMentalHealth,isRecoveryFocussed,isSuitableForVulnerable,acceptsSexOffenders,acceptsChildSexOffenders,acceptsNonSexualChildOffenders,acceptsHateCrimeOffenders,isCatered,hasWideStepFreeAccess,hasWideAccessToCommunalAreas,hasStepFreeAccessToCommunalAreas,hasWheelChairAccessibleBathrooms,hasLift,hasTactileFlooring,hasBrailleSignage,hasHearingLoop,status,apCode,qCode,gender
Test AP 1,454 Nader Port,,Quitzonview,OX26 2EQ,51.904735,-1.1635,No,[email protected],Greater Manchester,Malvern Hills,,YES,NO,NO,NO,NO,YES,YES,YES,YES,YES,YES,NO,NO,YES,NO,NO,NO,NO,YES,active,TEST1,Q095,MAN
Test AP 2,7525 Alba Trail,,Theronborough,WA6 9BQ,53.266459,-2.768905,,[email protected],North East,Hambleton,,NO,NO,NO,NO,NO,YES,YES,YES,YES,YES,YES,YES,NO,YES,YES,NO,NO,NO,YES,active,TEST2,Q100,MAN
Test AP 3,8527 Bell Rapids,,South Myah,ML8 5EP,55.751711,-3.851103,restrictions on offences against sex workers due to location of the AP,[email protected],Wales,Tamworth,,NO,YES,NO,NO,NO,YES,YES,YES,YES,YES,YES,YES,YES,YES,YES,NO,NO,NO,NO,active,TEST3,Q057,MAN
Expand Down

0 comments on commit 72bc54c

Please sign in to comment.