1. Version Versendung
This commit is contained in:
1 parent
d7113afa5f
commit
781aa20253
31 files changed
+4023
-10
No files matched your search
@@ -145,6 +145,31 @@ public final class AppDatabase {
|
||||
key TEXT PRIMARY KEY,
|
||||
value TEXT NOT NULL
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS dispatch_preferences (
|
||||
person_id TEXT PRIMARY KEY,
|
||||
channel TEXT NOT NULL DEFAULT 'email',
|
||||
variant TEXT NOT NULL DEFAULT 'compact',
|
||||
cover INTEGER NOT NULL DEFAULT 1,
|
||||
updated_at TEXT NOT NULL
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS dispatch_log (
|
||||
id TEXT PRIMARY KEY,
|
||||
batch_id TEXT NOT NULL,
|
||||
range_start TEXT NOT NULL,
|
||||
range_end TEXT NOT NULL,
|
||||
person_id TEXT NOT NULL DEFAULT '',
|
||||
person_name TEXT NOT NULL DEFAULT '',
|
||||
channel TEXT NOT NULL,
|
||||
variant TEXT NOT NULL DEFAULT '',
|
||||
cover INTEGER NOT NULL DEFAULT 0,
|
||||
email TEXT NOT NULL DEFAULT '',
|
||||
status TEXT NOT NULL,
|
||||
detail TEXT NOT NULL DEFAULT '',
|
||||
created_at TEXT NOT NULL
|
||||
);
|
||||
CREATE INDEX IF NOT EXISTS idx_dispatch_log_created ON dispatch_log(created_at);
|
||||
"""
|
||||
try database.execute(schema)
|
||||
// Older databases predate the free-text location for special services.
|
||||
@@ -153,7 +178,7 @@ public final class AppDatabase {
|
||||
column: "location",
|
||||
definition: "TEXT NOT NULL DEFAULT ''"
|
||||
)
|
||||
try database.run("INSERT OR REPLACE INTO settings(key, value) VALUES('schemaVersion', '2')")
|
||||
try database.run("INSERT OR REPLACE INTO settings(key, value) VALUES('schemaVersion', '3')")
|
||||
}
|
||||
|
||||
private func addColumnIfMissing(table: String, column: String, definition: String) throws {
|
||||
@@ -658,6 +683,90 @@ public final class AppDatabase {
|
||||
)
|
||||
}
|
||||
|
||||
// MARK: - Dispatch
|
||||
|
||||
public func dispatchPreferences() throws -> [String: DispatchPreference] {
|
||||
try database.query("SELECT * FROM dispatch_preferences").reduce(into: [:]) { result, row in
|
||||
guard let personID = row.string("person_id") else { return }
|
||||
result[personID] = DispatchPreference(
|
||||
channel: DispatchChannel(rawValue: row.string("channel", default: "email")) ?? .email,
|
||||
variant: PlanListVariant(rawValue: row.string("variant", default: "compact")) ?? .compact,
|
||||
cover: row.bool("cover", default: true)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
public func saveDispatchPreference(_ preference: DispatchPreference, personID: String) {
|
||||
try? database.run("""
|
||||
INSERT INTO dispatch_preferences(person_id, channel, variant, cover, updated_at)
|
||||
VALUES(?, ?, ?, ?, ?)
|
||||
ON CONFLICT(person_id) DO UPDATE SET
|
||||
channel=excluded.channel, variant=excluded.variant,
|
||||
cover=excluded.cover, updated_at=excluded.updated_at
|
||||
""", [
|
||||
.text(personID), .text(preference.channel.rawValue),
|
||||
.text(preference.variant.rawValue), .bool(preference.cover),
|
||||
.text(ISO8601DateFormatter().string(from: Date()))
|
||||
])
|
||||
}
|
||||
|
||||
public func dispatchLog(limit: Int = 80) throws -> [DispatchLogEntry] {
|
||||
let bounded = max(1, min(limit, 500))
|
||||
return try database.query("""
|
||||
SELECT * FROM dispatch_log ORDER BY created_at DESC, id DESC LIMIT ?
|
||||
""", [.int(bounded)]).compactMap { row in
|
||||
guard let id = row.string("id"),
|
||||
let batchID = row.string("batch_id"),
|
||||
let start = row.date("range_start"),
|
||||
let end = row.date("range_end"),
|
||||
let channel = DispatchChannel(rawValue: row.string("channel", default: "print")),
|
||||
let status = DispatchStatus(rawValue: row.string("status", default: "pdf"))
|
||||
else { return nil }
|
||||
let created = ISO8601DateFormatter().date(from: row.string("created_at", default: "")) ?? Date()
|
||||
return DispatchLogEntry(
|
||||
id: id,
|
||||
batchID: batchID,
|
||||
rangeStart: start,
|
||||
rangeEnd: end,
|
||||
personID: row.string("person_id", default: ""),
|
||||
personName: row.string("person_name", default: ""),
|
||||
channel: channel,
|
||||
variant: PlanListVariant(rawValue: row.string("variant", default: "compact")) ?? .compact,
|
||||
cover: row.bool("cover"),
|
||||
email: row.string("email", default: ""),
|
||||
status: status,
|
||||
detail: row.string("detail", default: ""),
|
||||
createdAt: created
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
public func appendDispatchLog(_ entries: [DispatchLogEntry]) throws {
|
||||
guard !entries.isEmpty else { return }
|
||||
try database.transaction {
|
||||
let formatter = ISO8601DateFormatter()
|
||||
for entry in entries {
|
||||
try database.run("""
|
||||
INSERT INTO dispatch_log(id, batch_id, range_start, range_end, person_id, person_name,
|
||||
channel, variant, cover, email, status, detail, created_at)
|
||||
VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||
""", [
|
||||
.text(entry.id), .text(entry.batchID),
|
||||
.dateKey(entry.rangeStart), .dateKey(entry.rangeEnd),
|
||||
.text(entry.personID), .text(entry.personName),
|
||||
.text(entry.channel.rawValue), .text(entry.variant.rawValue),
|
||||
.bool(entry.cover), .text(entry.email),
|
||||
.text(entry.status.rawValue), .text(entry.detail),
|
||||
.text(formatter.string(from: entry.createdAt))
|
||||
])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public func deleteDispatchLog() throws {
|
||||
try database.run("DELETE FROM dispatch_log")
|
||||
}
|
||||
|
||||
// MARK: - Settings
|
||||
|
||||
public func setting(_ key: String) -> String? {
|
||||
@@ -768,6 +877,7 @@ public final class AppDatabase {
|
||||
DELETE FROM assignments; DELETE FROM services; DELETE FROM service_exceptions;
|
||||
DELETE FROM service_templates; DELETE FROM absences; DELETE FROM people;
|
||||
DELETE FROM churches; DELETE FROM liturgical_days;
|
||||
DELETE FROM dispatch_preferences; DELETE FROM dispatch_log;
|
||||
""")
|
||||
if markDemoSuppressed {
|
||||
try database.run("INSERT OR REPLACE INTO settings(key, value) VALUES('suppressDemoSeed', '1')")
|
||||
|
||||
Reference in new issue
Block a user