import Foundation enum TaskCommand: Sendable { case merge, cancel, round case undo(receiptRevision: Int64) case move(repo: String, revision: Int64) case kind(String, revision: Int64) case priority(String, by: Date?, revision: Int64) } extension EquipoClient { func execute(task id: String, _ command: TaskCommand) async throws -> ThinTask? { let method: String, suffix: String var body: [String: AnyEncodable] = [:] switch command { case .merge: method = "POST"; suffix = "merge" case .cancel: method = "POST"; suffix = "cancel" case .round: method = "POST"; suffix = "round" case .undo(let revision): method = "POST"; suffix = "undo-merge"; body["revision"] = .init(revision) case .move(let repo, let revision): method = "POST"; suffix = "move"; body = ["repo": .init(repo), "revision": .init(revision)] case .kind(let kind, let revision): method = "POST"; suffix = "kind"; body = ["kind": .init(kind), "revision": .init(revision)] case .priority(let priority, let by, let revision): method = "PUT"; suffix = "priority" // The wire expects RFC3339 or explicit null, not JSONEncoder's // default Date seconds. Changing urgency preserves the deadline. body = ["priority": .init(priority), "by": .init(by.map { $0.ISO8601Format() }), "revision": .init(revision)] } let write = try PendingWrite.json(body) let request = APIRequest(method, "/tasks/\(escape(id))/\(suffix)", body: write.body, bearer: token, idempotencyKey: write.key) if case .undo = command { return try await send(request) } let _: EmptyResponse = try await send(request) return nil } } enum TaskActionFailure { static func words(_ error: Error) -> String { if error as? EquipoAPIError == .offline || error is URLError { return EquipoStrings.text("card.offline") } if let api = error as? EquipoAPIError, api.status == 409 { if let code = api.code, let words = Feed.webText("api." + code) { return words } return EquipoStrings.text("card.changed") } return EquipoStrings.text("card.failed") } }