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')")
|
||||
|
||||
@@ -0,0 +1,133 @@
|
||||
import Foundation
|
||||
|
||||
/// How a recipient receives the plan.
|
||||
public enum DispatchChannel: String, Codable, CaseIterable, Identifiable, Sendable {
|
||||
case email
|
||||
case print
|
||||
case none
|
||||
|
||||
public var id: String { rawValue }
|
||||
|
||||
public var title: String {
|
||||
switch self {
|
||||
case .email: "E-Mail"
|
||||
case .print: "Ausdruck"
|
||||
case .none: "Kein Versand"
|
||||
}
|
||||
}
|
||||
|
||||
public var symbolName: String {
|
||||
switch self {
|
||||
case .email: "envelope.fill"
|
||||
case .print: "printer.fill"
|
||||
case .none: "minus.circle"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Which list is attached to a dispatch.
|
||||
public enum PlanListVariant: String, Codable, CaseIterable, Identifiable, Sendable {
|
||||
case compact
|
||||
case full
|
||||
|
||||
public var id: String { rawValue }
|
||||
|
||||
public var title: String {
|
||||
switch self {
|
||||
case .compact: "Kompaktansicht"
|
||||
case .full: "Ausführlicher Plan"
|
||||
}
|
||||
}
|
||||
|
||||
public var shortTitle: String {
|
||||
switch self {
|
||||
case .compact: "Kompaktliste"
|
||||
case .full: "Plan"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Stored dispatch preference for one person.
|
||||
public struct DispatchPreference: Equatable, Sendable {
|
||||
public var channel: DispatchChannel
|
||||
public var variant: PlanListVariant
|
||||
public var cover: Bool
|
||||
|
||||
public init(channel: DispatchChannel = .email, variant: PlanListVariant = .compact, cover: Bool = true) {
|
||||
self.channel = channel
|
||||
self.variant = variant
|
||||
self.cover = cover
|
||||
}
|
||||
|
||||
/// A selected e-mail channel without a stored address falls back to print,
|
||||
/// mirroring the web version.
|
||||
public func effectiveChannel(for person: Person) -> DispatchChannel {
|
||||
if channel == .email && person.email.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty {
|
||||
return .print
|
||||
}
|
||||
return channel
|
||||
}
|
||||
}
|
||||
|
||||
/// Outcome recorded in the dispatch log. The macOS app opens e-mails in the
|
||||
/// default mail client instead of sending them over SMTP.
|
||||
public enum DispatchStatus: String, Codable, CaseIterable, Sendable {
|
||||
case mail
|
||||
case pdf
|
||||
case failed
|
||||
|
||||
public var title: String {
|
||||
switch self {
|
||||
case .mail: "Im Mailprogramm geöffnet"
|
||||
case .pdf: "Druck-PDF erzeugt"
|
||||
case .failed: "Fehlgeschlagen"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// One log line of a dispatch batch.
|
||||
public struct DispatchLogEntry: Identifiable, Equatable, Sendable {
|
||||
public var id: String
|
||||
public var batchID: String
|
||||
public var rangeStart: Date
|
||||
public var rangeEnd: Date
|
||||
public var personID: String
|
||||
public var personName: String
|
||||
public var channel: DispatchChannel
|
||||
public var variant: PlanListVariant
|
||||
public var cover: Bool
|
||||
public var email: String
|
||||
public var status: DispatchStatus
|
||||
public var detail: String
|
||||
public var createdAt: Date
|
||||
|
||||
public init(
|
||||
id: String = UUID().uuidString,
|
||||
batchID: String,
|
||||
rangeStart: Date,
|
||||
rangeEnd: Date,
|
||||
personID: String,
|
||||
personName: String,
|
||||
channel: DispatchChannel,
|
||||
variant: PlanListVariant = .compact,
|
||||
cover: Bool = false,
|
||||
email: String = "",
|
||||
status: DispatchStatus,
|
||||
detail: String = "",
|
||||
createdAt: Date = Date()
|
||||
) {
|
||||
self.id = id
|
||||
self.batchID = batchID
|
||||
self.rangeStart = rangeStart
|
||||
self.rangeEnd = rangeEnd
|
||||
self.personID = personID
|
||||
self.personName = personName
|
||||
self.channel = channel
|
||||
self.variant = variant
|
||||
self.cover = cover
|
||||
self.email = email
|
||||
self.status = status
|
||||
self.detail = detail
|
||||
self.createdAt = createdAt
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user