| 1 | import Foundation |
| 2 | |
| 3 | enum TaskCommand: Sendable { |
| 4 | case merge, cancel, round |
| 5 | case undo(receiptRevision: Int64) |
| 6 | case move(repo: String, revision: Int64) |
| 7 | case kind(String, revision: Int64) |
| 8 | case priority(String, by: Date?, revision: Int64) |
| 9 | } |
| 10 | |
| 11 | extension EquipoClient { |
| 12 | func execute(task id: String, _ command: TaskCommand) async throws -> ThinTask? { |
| 13 | let method: String, suffix: String |
| 14 | var body: [String: AnyEncodable] = [:] |
| 15 | switch command { |
| 16 | case .merge: method = "POST"; suffix = "merge" |
| 17 | case .cancel: method = "POST"; suffix = "cancel" |
| 18 | case .round: method = "POST"; suffix = "round" |
| 19 | case .undo(let revision): |
| 20 | method = "POST"; suffix = "undo-merge"; body["revision"] = .init(revision) |
| 21 | case .move(let repo, let revision): |
| 22 | method = "POST"; suffix = "move"; body = ["repo": .init(repo), "revision": .init(revision)] |
| 23 | case .kind(let kind, let revision): |
| 24 | method = "POST"; suffix = "kind"; body = ["kind": .init(kind), "revision": .init(revision)] |
| 25 | case .priority(let priority, let by, let revision): |
| 26 | method = "PUT"; suffix = "priority" |
| 27 | // The wire expects RFC3339 or explicit null, not JSONEncoder's |
| 28 | // default Date seconds. Changing urgency preserves the deadline. |
| 29 | body = ["priority": .init(priority), "by": .init(by.map { $0.ISO8601Format() }), "revision": .init(revision)] |
| 30 | } |
| 31 | let write = try PendingWrite.json(body) |
| 32 | let request = APIRequest(method, "/tasks/\(escape(id))/\(suffix)", body: write.body, bearer: token, idempotencyKey: write.key) |
| 33 | if case .undo = command { return try await send(request) } |
| 34 | let _: EmptyResponse = try await send(request) |
| 35 | return nil |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | enum TaskActionFailure { |
| 40 | static func words(_ error: Error) -> String { |
| 41 | if error as? EquipoAPIError == .offline || error is URLError { return EquipoStrings.text("card.offline") } |
| 42 | if let api = error as? EquipoAPIError, api.status == 409 { |
| 43 | if let code = api.code, let words = Feed.webText("api." + code) { return words } |
| 44 | return EquipoStrings.text("card.changed") |
| 45 | } |
| 46 | return EquipoStrings.text("card.failed") |
| 47 | } |
| 48 | } |
| 49 | |