05/05 Update the API to follow-up and resinspection

This commit is contained in:
Nguyen Ngo
2026-05-05 18:19:31 -04:00
parent 9d6b5e5bcb
commit b9e504b72c
8 changed files with 342 additions and 18 deletions
@@ -347,6 +347,13 @@ struct ExecuteInspectionView: View {
inspection.status = "completed"
inspection.completedAt = Date()
inspection.syncStatus = "pending"
// Clear follow-up flag on parent immediately
// Do this at submit time rather than relying solely on SyncManager,
// so the badge disappears the moment the inspector taps Submit
// regardless of connectivity or sync timing.
clearParentFollowUpFlag()
try? context.save()
isSubmitting = false
@@ -366,6 +373,49 @@ struct ExecuteInspectionView: View {
dismiss()
}
/// Find the parent LocalInspection and clear its followUpRequired flag.
/// Tries parentLocalId first (set for new re-inspections), then falls back
/// to parentServerId (set after parent has synced), then as a last resort
/// matches by template+facility for re-inspections created before these
/// fields were added (parentLocalId=nil, parentServerId=nil).
private func clearParentFollowUpFlag() {
var parent: LocalInspection?
// Primary: match by the parent's localId UUID (always available if set)
if let lid = inspection.parentLocalId {
parent = try? context.fetch(
FetchDescriptor<LocalInspection>(predicate: #Predicate { $0.localId == lid })
).first
}
// Fallback 1: match by server ID (available once parent has synced)
if parent == nil, let sid = inspection.parentServerId {
parent = try? context.fetch(FetchDescriptor<LocalInspection>())
.first(where: { $0.serverId == sid })
}
// Fallback 2: for stale re-inspections created before parentLocalId existed,
// find any LocalInspection with the same template+facility that has
// followUpRequired=true and is not this inspection itself.
if parent == nil {
let tid = inspection.templateServerId
let fid = inspection.facilityServerId
let selfId = inspection.localId
parent = try? context.fetch(FetchDescriptor<LocalInspection>())
.first(where: {
$0.templateServerId == tid &&
$0.facilityServerId == fid &&
$0.followUpRequired == true &&
$0.localId != selfId
})
}
if let parent {
parent.followUpRequired = false
parent.followUpNote = nil
}
}
// Photo Handling
private func handlePhotoSelected(localPath: String, field: [String: Any]) {