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

APS-1730 capture previous values on booking amend domain event #2727

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -679,7 +679,7 @@ class PremisesController(
newDepartureDate = body.newDepartureDate,
)

val dateChange = extractResultEntityOrThrow(result)
val dateChange = extractEntityFromCasResult(result)

return ResponseEntity.ok(dateChangeTransformer.transformJpaToApi(dateChange))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,10 @@ enum class DomainEventType(
Cas1EventType.bookingChanged.value,
"An Approved Premises Booking has been changed",
TimelineEventType.approvedPremisesBookingChanged,
schemaVersions = listOf(
DEFAULT_DOMAIN_EVENT_SCHEMA_VERSION,
DomainEventSchemaVersion(2, "Captures previous set values, if changed. Also added characteristics."),
),
),
APPROVED_PREMISES_BOOKING_KEYWORKER_ASSIGNED(
DomainEventCas.CAS1,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import uk.gov.justice.digital.hmpps.approvedpremisesapi.jpa.entity.UserQualifica
import uk.gov.justice.digital.hmpps.approvedpremisesapi.jpa.entity.serviceScopeMatches
import uk.gov.justice.digital.hmpps.approvedpremisesapi.model.PersonInfoResult
import uk.gov.justice.digital.hmpps.approvedpremisesapi.model.validated
import uk.gov.justice.digital.hmpps.approvedpremisesapi.model.validatedCasResult
import uk.gov.justice.digital.hmpps.approvedpremisesapi.problem.InternalServerErrorProblem
import uk.gov.justice.digital.hmpps.approvedpremisesapi.results.AuthorisableActionResult
import uk.gov.justice.digital.hmpps.approvedpremisesapi.results.CasResult
Expand Down Expand Up @@ -416,13 +417,14 @@ class BookingService(
private fun shouldCreateDomainEventForBooking(booking: BookingEntity, user: UserEntity?) =
booking.service == ServiceName.approvedPremises.value && user != null && (booking.application != null || booking.offlineApplication?.eventNumber != null)

@SuppressWarnings("CyclomaticComplexMethod")
@Transactional
fun createDateChange(
booking: BookingEntity,
user: UserEntity,
newArrivalDate: LocalDate?,
newDepartureDate: LocalDate?,
) = validated {
) = validatedCasResult {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🙌

val effectiveNewArrivalDate = newArrivalDate ?: booking.arrivalDate
val effectiveNewDepartureDate = newDepartureDate ?: booking.departureDate

Expand All @@ -438,11 +440,11 @@ class BookingService(
?: throw InternalServerErrorProblem("No bed ID present on Booking: ${booking.id}")

getBookingWithConflictingDates(effectiveNewArrivalDate, expectedLastUnavailableDate, booking.id, bedId)?.let {
return@validated it.id hasConflictError "A Booking already exists for dates from ${it.arrivalDate} to ${it.lastUnavailableDate} which overlaps with the desired dates"
return@validatedCasResult it.id hasConflictError "A Booking already exists for dates from ${it.arrivalDate} to ${it.lastUnavailableDate} which overlaps with the desired dates"
}

getLostBedWithConflictingDates(effectiveNewArrivalDate, expectedLastUnavailableDate, null, bedId)?.let {
return@validated it.id hasConflictError "A Lost Bed already exists for dates from ${it.startDate} to ${it.endDate} which overlaps with the desired dates"
return@validatedCasResult it.id hasConflictError "A Lost Bed already exists for dates from ${it.startDate} to ${it.endDate} which overlaps with the desired dates"
}
}

Expand All @@ -456,11 +458,14 @@ class BookingService(
}
}

val previousArrivalDate = booking.arrivalDate
val previousDepartureDate = booking.departureDate

val dateChangeEntity = dateChangeRepository.save(
DateChangeEntity(
id = UUID.randomUUID(),
previousDepartureDate = booking.departureDate,
previousArrivalDate = booking.arrivalDate,
previousArrivalDate = previousArrivalDate,
previousDepartureDate = previousDepartureDate,
newArrivalDate = effectiveNewArrivalDate,
newDepartureDate = effectiveNewDepartureDate,
changedAt = OffsetDateTime.now(),
Expand All @@ -471,8 +476,8 @@ class BookingService(

updateBooking(
booking.apply {
arrivalDate = dateChangeEntity.newArrivalDate
departureDate = dateChangeEntity.newDepartureDate
arrivalDate = effectiveNewArrivalDate
departureDate = effectiveNewDepartureDate
dateChanges.add(dateChangeEntity)
},
)
Expand All @@ -482,6 +487,16 @@ class BookingService(
booking = booking,
changedBy = user,
bookingChangedAt = OffsetDateTime.now(),
previousArrivalDateIfChanged = if (previousArrivalDate != effectiveNewArrivalDate) {
previousArrivalDate
} else {
null
},
previousDepartureDateIfChanged = if (previousDepartureDate != effectiveNewDepartureDate) {
previousDepartureDate
} else {
null
},
)
}

Expand Down
Loading
Loading