Skip to content

Commit 003fd11

Browse files
Update handling of occurrences for unapproved auth
1 parent f60d525 commit 003fd11

File tree

5 files changed

+26
-11
lines changed

5 files changed

+26
-11
lines changed

src/main/kotlin/uk/gov/justice/digital/hmpps/externalmovementsapi/domain/tap/authorisation/TemporaryAbsenceAuthorisation.kt

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -338,12 +338,6 @@ class TemporaryAbsenceAuthorisation(
338338
schedule = json
339339
}
340340

341-
fun clearSchedule() = apply {
342-
if (!repeat) {
343-
schedule = null
344-
}
345-
}
346-
347341
companion object {
348342
val PRISON_CODE = TemporaryAbsenceAuthorisation::prisonCode.name
349343
val PERSON = TemporaryAbsenceAuthorisation::person.name

src/main/kotlin/uk/gov/justice/digital/hmpps/externalmovementsapi/model/location/Location.kt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,21 @@ data class Location(
1313
fun isEmpty(): Boolean = description.isNullOrBlank() && address.isNullOrBlank() && postcode.isNullOrBlank()
1414
override fun toString(): String = listOfNotNull(description?.trim(), address?.trim(), postcode?.trim()).joinToString()
1515

16+
override fun equals(other: Any?): Boolean {
17+
if (this === other) return true
18+
if (javaClass != other?.javaClass) return false
19+
20+
other as Location
21+
22+
if (description != other.description) return false
23+
if (address != other.address) return false
24+
if (postcode != other.postcode) return false
25+
26+
return true
27+
}
28+
29+
override fun hashCode(): Int = description?.hashCode() ?: ((0 + (address?.hashCode() ?: 0)) + (postcode?.hashCode() ?: 0))
30+
1631
companion object {
1732
fun empty(): Location = Location(null, null, null, null)
1833
}

src/main/kotlin/uk/gov/justice/digital/hmpps/externalmovementsapi/service/CreateScheduledAbsence.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ class CreateScheduledAbsence(
9191

9292
val occurrence = request.asOccurrence(authorisation).calculateStatus { occurrenceStatusRepository.getByCode(it) }
9393
val locations = (authorisation.locations + occurrence.location).mapTo(linkedSetOf()) { it }
94-
authorisation.applyLocations(ChangeAuthorisationLocations(locations)).clearSchedule()
94+
authorisation.applyLocations(ChangeAuthorisationLocations(locations))
9595

9696
return ReferenceId(tapOccurrenceRepository.save(occurrence).id)
9797
}

src/main/kotlin/uk/gov/justice/digital/hmpps/externalmovementsapi/sync/internal/SyncTapAuthorisation.kt

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -140,12 +140,18 @@ class SyncTapAuthorisation(
140140
val occurrences = occurrenceRepository.findByAuthorisationId(id)
141141
(
142142
occurrences.mapTo(linkedSetOf()) { it.location }.takeIf { it.isNotEmpty() }
143-
?: request.location?.let { linkedSetOf(it) }
143+
?: request.location?.takeIf { it.isNullOrEmpty() }?.let { linkedSetOf(it) }
144144
)?.also { applyLocations(ChangeAuthorisationLocations(it)) }
145145
val schedule = request.schedule()?.also { applySchedule(objectMapper.valueToTree(it)) }
146146
if (schedule != null && !repeat && status.code != AuthorisationStatus.Code.APPROVED.name) {
147-
occurrences.singleOrNull()?.also {
148-
it.reschedule(RescheduleOccurrence(start.atTime(schedule.startTime), end.atTime(schedule.returnTime)))
147+
occurrences.singleOrNull()?.let { occ ->
148+
if (occ.status.code == OccurrenceStatus.Code.SCHEDULED.name) {
149+
occurrenceRepository.delete(occ)
150+
null
151+
} else {
152+
occ.reschedule(RescheduleOccurrence(start.atTime(schedule.startTime), end.atTime(schedule.returnTime)))
153+
occ.calculateStatus { rdPaths.getReferenceData(OccurrenceStatus::class, it) as OccurrenceStatus }
154+
}
149155
} ?: createOccurrence(objectMapper, rdPaths)
150156
}
151157
}

src/main/kotlin/uk/gov/justice/digital/hmpps/externalmovementsapi/sync/internal/SyncTapOccurrence.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ class SyncTapOccurrence(
7676
}
7777
occurrenceRepository.flush()
7878
val locations = occurrenceRepository.findByAuthorisationId(authorisation.id).mapTo(linkedSetOf()) { it.location }
79-
authorisation.applyLocations(ChangeAuthorisationLocations(locations)).clearSchedule()
79+
authorisation.applyLocations(ChangeAuthorisationLocations(locations))
8080
return SyncResponse(occurrence.id)
8181
}
8282

0 commit comments

Comments
 (0)