05/02 Phase B
This commit is contained in:
@@ -0,0 +1,45 @@
|
||||
// Models/SyncQueueEntry.swift
|
||||
// ---------------------------
|
||||
// SwiftData model for the outbox sync queue.
|
||||
// Every offline write (inspection, issue, photo) enqueues an entry here.
|
||||
// SyncManager processes entries in FIFO order when connectivity is restored.
|
||||
|
||||
import Foundation
|
||||
import SwiftData
|
||||
|
||||
@Model
|
||||
final class SyncQueueEntry {
|
||||
|
||||
@Attribute(.unique) var entryId: String
|
||||
var createdAt: Date
|
||||
/// "inspection" | "issue" | "photo"
|
||||
var entityType: String
|
||||
/// References the entity's localId
|
||||
var localId: String
|
||||
/// "pending" | "in_flight" | "synced" | "failed"
|
||||
var syncStatus: String
|
||||
var retryCount: Int
|
||||
var lastAttemptAt: Date?
|
||||
var lastErrorMessage: String?
|
||||
/// JSON-serialized payload to POST to the server
|
||||
var payloadJSON: String
|
||||
|
||||
init(entityType: String, localId: String, payloadJSON: String) {
|
||||
self.entryId = UUID().uuidString
|
||||
self.createdAt = Date()
|
||||
self.entityType = entityType
|
||||
self.localId = localId
|
||||
self.syncStatus = "pending"
|
||||
self.retryCount = 0
|
||||
self.lastAttemptAt = nil
|
||||
self.lastErrorMessage = nil
|
||||
self.payloadJSON = payloadJSON
|
||||
}
|
||||
|
||||
var payload: [String: Any] {
|
||||
guard let data = payloadJSON.data(using: .utf8),
|
||||
let dict = try? JSONSerialization.jsonObject(with: data) as? [String: Any]
|
||||
else { return [:] }
|
||||
return dict
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user