The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 06:00 UTC
| 1 | import * as Domain from "@alchemy.run/domain"; |
| 2 | import * as Effect from "effect/Effect"; |
| 3 | import * as Schema from "effect/Schema"; |
| 4 | import { AccountId } from "../Account/Account.id.ts"; |
| 5 | import { NotPermitted } from "../Account/ChangeOwner.command.ts"; |
| 6 | import { Actor } from "../Actor.ts"; |
| 7 | import { CurrentActor } from "../CurrentActor.context.ts"; |
| 8 | import { Cents } from "../Money.ts"; |
| 9 | import { |
| 10 | ApproveTransfer, |
| 11 | NotAwaitingApproval, |
| 12 | TransferApproved, |
| 13 | } from "./ApproveTransfer.command.ts"; |
| 14 | import { CompleteTransfer, TransferCompleted } from "./CompleteTransfer.command.ts"; |
| 15 | import { ExpireTransfer } from "./ExpireTransfer.command.ts"; |
| 16 | import { FailTransfer, TransferFailed } from "./FailTransfer.command.ts"; |
| 17 | import { |
| 18 | AlreadyRequested, |
| 19 | APPROVAL_THRESHOLD, |
| 20 | RequestTransfer, |
| 21 | SameAccount, |
| 22 | TransferRequested, |
| 23 | } from "./RequestTransfer.command.ts"; |
| 24 | import { TransferId } from "./Transfer.id.ts"; |
| 25 | |
| 26 | const Details = { from: AccountId, to: AccountId, amount: Cents, requestedBy: Actor }; |
| 27 | |
| 28 | /** |
| 29 | * A transfer between two accounts: the state of the TransferExecution |
| 30 | * process. Large transfers wait for approval until their window closes; |
| 31 | * approval and expiry race on this single instance, so one of them wins. |
| 32 | */ |
| 33 | export class Transfer extends Domain.Aggregate("Transfer", { |
| 34 | id: TransferId, |
| 35 | state: Schema.Union([ |
| 36 | Schema.TaggedStruct("New", {}), |
| 37 | Schema.TaggedStruct("AwaitingApproval", Details), |
| 38 | Schema.TaggedStruct("Pending", Details), |
| 39 | Schema.TaggedStruct("Completed", {}), |
| 40 | Schema.TaggedStruct("Failed", { reason: Schema.String }), |
| 41 | ]), |
| 42 | initial: { _tag: "New" }, |
| 43 | commands: [RequestTransfer, ApproveTransfer, ExpireTransfer, CompleteTransfer, FailTransfer], |
| 44 | decide: { |
| 45 | // Records who asked, so TransferExecution debits the source on their behalf. |
| 46 | RequestTransfer: Effect.fn(function* (s, cmd, { id }) { |
| 47 | if (s._tag !== "New") return yield* new AlreadyRequested(); |
| 48 | if (cmd.from === cmd.to) return yield* new SameAccount(); |
| 49 | const requestedBy = yield* CurrentActor; |
| 50 | return [ |
| 51 | new TransferRequested({ |
| 52 | transferId: id, |
| 53 | from: cmd.from, |
| 54 | to: cmd.to, |
| 55 | amount: cmd.amount, |
| 56 | requestedBy, |
| 57 | needsApproval: cmd.amount >= APPROVAL_THRESHOLD, |
| 58 | }), |
| 59 | ]; |
| 60 | }), |
| 61 | ApproveTransfer: Effect.fn(function* (s, _, { id }) { |
| 62 | const approvedBy = yield* CurrentActor; |
| 63 | if (approvedBy._tag !== "Agent") return yield* new NotPermitted(); |
| 64 | if (s._tag !== "AwaitingApproval") return yield* new NotAwaitingApproval(); |
| 65 | return [ |
| 66 | new TransferApproved({ |
| 67 | transferId: id, |
| 68 | from: s.from, |
| 69 | to: s.to, |
| 70 | amount: s.amount, |
| 71 | requestedBy: s.requestedBy, |
| 72 | approvedBy, |
| 73 | }), |
| 74 | ]; |
| 75 | }), |
| 76 | ExpireTransfer: (s, _, { id }) => |
| 77 | s._tag === "AwaitingApproval" |
| 78 | ? [new TransferFailed({ transferId: id, reason: "not approved in time" })] |
| 79 | : [], |
| 80 | CompleteTransfer: (s, _, { id }) => |
| 81 | s._tag === "Pending" ? [new TransferCompleted({ transferId: id })] : [], |
| 82 | FailTransfer: (s, cmd, { id }) => |
| 83 | s._tag === "Pending" ? [new TransferFailed({ transferId: id, reason: cmd.reason })] : [], |
| 84 | }, |
| 85 | evolve: { |
| 86 | TransferRequested: (_, e) => ({ |
| 87 | _tag: e.needsApproval ? ("AwaitingApproval" as const) : ("Pending" as const), |
| 88 | from: e.from, |
| 89 | to: e.to, |
| 90 | amount: e.amount, |
| 91 | requestedBy: e.requestedBy, |
| 92 | }), |
| 93 | TransferApproved: (_, e) => ({ |
| 94 | _tag: "Pending" as const, |
| 95 | from: e.from, |
| 96 | to: e.to, |
| 97 | amount: e.amount, |
| 98 | requestedBy: e.requestedBy, |
| 99 | }), |
| 100 | TransferCompleted: () => ({ _tag: "Completed" as const }), |
| 101 | TransferFailed: (_, e) => ({ _tag: "Failed" as const, reason: e.reason }), |
| 102 | }, |
| 103 | }) {} |