05/08 Updated code: fix some issues

This commit is contained in:
Nguyen Ngo
2026-05-08 13:28:18 -04:00
parent b9e504b72c
commit 304422cf6f
2 changed files with 44 additions and 4 deletions
+17
View File
@@ -169,7 +169,24 @@ class SyncManager: ObservableObject {
.filter { $0.syncStatus == "pending" }
.sorted { $0.createdAt < $1.createdAt }
// Pre-fetch all LocalInspections once for the parent-failed guard below.
let allInspections = (try? context.fetch(FetchDescriptor<LocalInspection>())) ?? []
for issue in pending {
// Guard: if the parent inspection permanently failed to sync, submitting
// this issue without an inspection_id would create an orphaned server
// record. Skip the issue and mark it failed so the inspector can see
// the error rather than losing the association silently.
if let parentInspection = allInspections.first(where: {
$0.localId == issue.inspectionLocalId
}), parentInspection.syncStatus == "failed" {
issue.syncStatus = "failed"
issue.syncErrorMessage = "Parent inspection failed to sync — issue cannot be submitted."
try? context.save()
syncError = "Issue \(issue.localId.prefix(8))\u{2026} blocked: parent inspection did not sync."
continue
}
do {
let issueId = try await APIClient.shared.submitIssue(issue)
issue.serverId = issueId