Files

55 lines
2.0 KiB
Swift

// Models/SyncQueueEntry.swift
// ---------------------------
// LEGACY — This model is no longer used. The original design enqueued every
// offline write here and processed in FIFO order; the current architecture uses
// LocalInspection.syncStatus / LocalIssue.syncStatus / PendingPhoto.uploadStatus
// directly (simpler, fewer moving parts, no double-bookkeeping).
//
// The model is kept registered in the SwiftData container solely to maintain
// schema compatibility with existing installs — removing it from modelContainer
// would trigger a migration failure on devices that already have the table.
// A future dedicated migration (phase N) can drop the table explicitly using
// op.execute("DROP TABLE IF EXISTS SyncQueueEntry") once it's safe to do so.
//
// DO NOT add new code that reads or writes this model.
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
}
}