Skip to content

Commit

Permalink
Make record-arrival endpoint idempotent
Browse files Browse the repository at this point in the history
  • Loading branch information
gregkhawkins committed Nov 8, 2024
1 parent 2865445 commit 1f7a5d7
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,11 @@ class Cas1SpaceBookingService(
existingCas1SpaceBooking!!

if (existingCas1SpaceBooking.actualArrivalDateTime != null) {
return existingCas1SpaceBooking.id hasConflictError "An arrival is already recorded for this Space Booking"
return if (cas1NewArrival.arrivalDateTime == existingCas1SpaceBooking.actualArrivalDateTime) {
success(existingCas1SpaceBooking)
} else {
existingCas1SpaceBooking.id hasConflictError "An arrival is already recorded for this Space Booking"
}
}

existingCas1SpaceBooking.actualArrivalDateTime = cas1NewArrival.arrivalDateTime
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -518,7 +518,7 @@ class Cas1SpaceBookingServiceTest {
}

@Test
fun `Returns conflict error if the space booking record already has an arrival date recorded`() {
fun `Returns conflict error if the space booking record already has an arrival date recorded that does not match the received arrival date or time`() {
val existingSpaceBookingWithArrivalDate = existingSpaceBooking.copy(actualArrivalDateTime = originalArrivalDate)

every { cas1PremisesService.findPremiseById(any()) } returns premises
Expand All @@ -536,6 +536,61 @@ class Cas1SpaceBookingServiceTest {
assertThat(result.message).isEqualTo("An arrival is already recorded for this Space Booking")
}

@Test
fun `Returns conflict error if the space booking record already has an arrival date recorded that matches the received arrival date but does not match the arrival time`() {
val existingSpaceBookingWithArrivalDate = existingSpaceBooking.copy(
actualArrivalDateTime = originalArrivalDate,
canonicalArrivalDate = originalArrivalDate.toLocalDate(),
)

every { cas1PremisesService.findPremiseById(any()) } returns premises
every { spaceBookingRepository.findByIdOrNull(any()) } returns existingSpaceBookingWithArrivalDate

val result = service.recordArrivalForBooking(
premisesId = UUID.randomUUID(),
bookingId = UUID.randomUUID(),
cas1NewArrival = Cas1NewArrival(originalArrivalDate.plusMillis(1)),
)

assertThat(result).isInstanceOf(CasResult.ConflictError::class.java)
result as CasResult.ConflictError

assertThat(result.message).isEqualTo("An arrival is already recorded for this Space Booking")
}

@Test
fun `Returns success if the space booking record already has an arrival date recorded that matches the received arrival date and time`() {
val existingSpaceBookingWithArrivalDate = existingSpaceBooking.copy(
actualArrivalDateTime = originalArrivalDate,
canonicalArrivalDate = originalArrivalDate.toLocalDate(),
)

every { cas1PremisesService.findPremiseById(any()) } returns premises
every { spaceBookingRepository.findByIdOrNull(any()) } returns existingSpaceBookingWithArrivalDate

val result = service.recordArrivalForBooking(
premisesId = UUID.randomUUID(),
bookingId = UUID.randomUUID(),
cas1NewArrival = Cas1NewArrival(originalArrivalDate),
)

assertThat(result).isInstanceOf(CasResult.Success::class.java)

val extractedResult = (result as CasResult.Success).value
assertThat(existingSpaceBookingWithArrivalDate.premises).isEqualTo(extractedResult.premises)
assertThat(existingSpaceBookingWithArrivalDate.placementRequest).isEqualTo(extractedResult.placementRequest)
assertThat(existingSpaceBookingWithArrivalDate.application).isEqualTo(extractedResult.application)
assertThat(existingSpaceBookingWithArrivalDate.createdAt).isEqualTo(extractedResult.createdAt)
assertThat(existingSpaceBookingWithArrivalDate.createdBy).isEqualTo(extractedResult.createdBy)
assertThat(existingSpaceBookingWithArrivalDate.actualDepartureDateTime).isEqualTo(extractedResult.actualDepartureDateTime)
assertThat(existingSpaceBookingWithArrivalDate.crn).isEqualTo(extractedResult.crn)
assertThat(existingSpaceBookingWithArrivalDate.keyWorkerStaffCode).isEqualTo(extractedResult.keyWorkerStaffCode)
assertThat(existingSpaceBookingWithArrivalDate.keyWorkerAssignedAt).isEqualTo(extractedResult.keyWorkerAssignedAt)
assertThat(existingSpaceBookingWithArrivalDate.expectedArrivalDate).isEqualTo(extractedResult.expectedArrivalDate)
assertThat(originalArrivalDate).isEqualTo(extractedResult.actualArrivalDateTime)
assertThat(originalArrivalDate.toLocalDate()).isEqualTo(extractedResult.canonicalArrivalDate)
}

@Test
fun `Updates existing space booking with arrival information and raises domain event`() {
val updatedSpaceBookingCaptor = slot<Cas1SpaceBookingEntity>()
Expand Down

0 comments on commit 1f7a5d7

Please sign in to comment.