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 DateTime from "effect/DateTime"; |
| 3 | import * as Effect from "effect/Effect"; |
| 4 | import * as Optic from "effect/Optic"; |
| 5 | import * as Schema from "effect/Schema"; |
| 6 | import type { Actor } from "../Actor.ts"; |
| 7 | import { CurrentActor } from "../CurrentActor.context.ts"; |
| 8 | import { CustomerId } from "../Customer/Customer.id.ts"; |
| 9 | import { Cents } from "../Money.ts"; |
| 10 | import { Session } from "../Session.middleware.ts"; |
| 11 | import { AccountId } from "./Account.id.ts"; |
| 12 | import { ChangeOwner, NotPermitted, OwnerChanged } from "./ChangeOwner.command.ts"; |
| 13 | import { AccountClosed, BalanceNotZero, CloseAccount } from "./CloseAccount.command.ts"; |
| 14 | import { CreditTransfer, TransferCredited } from "./CreditTransfer.command.ts"; |
| 15 | import { DebitTransfer, TransferDebited } from "./DebitTransfer.command.ts"; |
| 16 | import { DepositMoney, MoneyDeposited } from "./DepositMoney.command.ts"; |
| 17 | import { AccountFrozen, FreezeAccount } from "./FreezeAccount.command.ts"; |
| 18 | import { Frozen } from "./Frozen.rejection.ts"; |
| 19 | import { InsufficientFunds } from "./InsufficientFunds.rejection.ts"; |
| 20 | import { mustOwn } from "./mustOwn.ts"; |
| 21 | import { NotOpen } from "./NotOpen.rejection.ts"; |
| 22 | import { NotOwner } from "./NotOwner.rejection.ts"; |
| 23 | import { AccountOpened, AlreadyOpen, OpenAccount } from "./OpenAccount.command.ts"; |
| 24 | import { |
| 25 | RecordSettlement, |
| 26 | SettlementOnClosedAccount, |
| 27 | SettlementReceived, |
| 28 | } from "./RecordSettlement.command.ts"; |
| 29 | import { RefundStranded, RefundTransfer, TransferRefunded } from "./RefundTransfer.command.ts"; |
| 30 | import { |
| 31 | DAILY_LIMIT, |
| 32 | DailyLimitExceeded, |
| 33 | MoneyWithdrawn, |
| 34 | WithdrawMoney, |
| 35 | } from "./WithdrawMoney.command.ts"; |
| 36 | |
| 37 | const Open = Schema.TaggedStruct("Open", { |
| 38 | customerId: CustomerId, |
| 39 | owner: Schema.String, |
| 40 | balance: Cents, |
| 41 | frozen: Schema.NullOr(Schema.String), |
| 42 | today: Schema.Struct({ day: Schema.String, withdrawn: Cents }), |
| 43 | }); |
| 44 | type Open = typeof Open.Type; |
| 45 | |
| 46 | const AccountState = Schema.Union([ |
| 47 | Schema.TaggedStruct("Unopened", {}), |
| 48 | Open, |
| 49 | Schema.TaggedStruct("Closed", {}), |
| 50 | ]); |
| 51 | type AccountState = typeof AccountState.Type; |
| 52 | |
| 53 | // Optics that focus on the Open account. decide refuses every command on an |
| 54 | // account that is not open, so these events only follow an Open state; on any |
| 55 | // other state an optic leaves it unchanged, which keeps evolve total. |
| 56 | const open = Optic.id<AccountState>().tag("Open"); |
| 57 | const balance = open.key("balance"); |
| 58 | const frozen = open.key("frozen"); |
| 59 | |
| 60 | // Authorization: customers may act on their own accounts; agents and the system on any. |
| 61 | const openFor = (s: AccountState, actor: Actor): Open | NotOpen | NotOwner => |
| 62 | s._tag !== "Open" |
| 63 | ? new NotOpen() |
| 64 | : actor._tag === "Customer" && actor.customerId !== s.customerId |
| 65 | ? new NotOwner() |
| 66 | : s; |
| 67 | |
| 68 | // The open account, if the current actor (from the command's middleware) may act on it. |
| 69 | const authorized = Effect.fn(function* (s: AccountState) { |
| 70 | const o = openFor(s, yield* CurrentActor); |
| 71 | if (o._tag !== "Open") return yield* o; |
| 72 | return o; |
| 73 | }); |
| 74 | |
| 75 | const dayOf = (t: DateTime.Utc) => DateTime.formatIsoDate(t); |
| 76 | |
| 77 | /** |
| 78 | * A bank account. Each command lives in its own file with the events it |
| 79 | * records and the rejections only it returns; this file holds the state and |
| 80 | * how commands and events change it. Commands sent by customers and agents |
| 81 | * read the current actor from their middleware to authorize it. |
| 82 | */ |
| 83 | export class Account extends Domain.Aggregate("Account", { |
| 84 | id: AccountId, |
| 85 | state: AccountState, |
| 86 | initial: { _tag: "Unopened" }, |
| 87 | commands: [ |
| 88 | OpenAccount, |
| 89 | ChangeOwner, |
| 90 | DepositMoney, |
| 91 | WithdrawMoney, |
| 92 | FreezeAccount, |
| 93 | CloseAccount, |
| 94 | DebitTransfer, |
| 95 | CreditTransfer, |
| 96 | RefundTransfer, |
| 97 | RecordSettlement, |
| 98 | ], |
| 99 | decide: { |
| 100 | OpenAccount: (s, cmd) => |
| 101 | s._tag !== "Unopened" |
| 102 | ? new AlreadyOpen() |
| 103 | : [new AccountOpened({ customerId: cmd.customerId, owner: cmd.owner })], |
| 104 | |
| 105 | ChangeOwner: Effect.fn(function* (s, cmd) { |
| 106 | if (s._tag !== "Open") return yield* new NotOpen(); |
| 107 | const actor = yield* CurrentActor; |
| 108 | if (actor._tag !== "Agent") return yield* new NotPermitted(); |
| 109 | return [new OwnerChanged({ previous: s.customerId, customerId: cmd.customerId, by: actor })]; |
| 110 | }), |
| 111 | |
| 112 | DepositMoney: Effect.fn(function* (s, cmd) { |
| 113 | const o = yield* authorized(s); |
| 114 | return [new MoneyDeposited({ amount: cmd.amount, balanceAfter: o.balance + cmd.amount })]; |
| 115 | }), |
| 116 | |
| 117 | WithdrawMoney: Effect.fn(function* (s, cmd, { now }) { |
| 118 | const o = yield* authorized(s); |
| 119 | if (o.frozen) return yield* new Frozen({ reason: o.frozen }); |
| 120 | if (cmd.amount > o.balance) |
| 121 | return yield* new InsufficientFunds({ balance: o.balance, requested: cmd.amount }); |
| 122 | const used = o.today.day === dayOf(now) ? o.today.withdrawn : 0; |
| 123 | if (used + cmd.amount > DAILY_LIMIT) |
| 124 | return yield* new DailyLimitExceeded({ remaining: DAILY_LIMIT - used }); |
| 125 | return [new MoneyWithdrawn({ amount: cmd.amount, balanceAfter: o.balance - cmd.amount })]; |
| 126 | }), |
| 127 | |
| 128 | FreezeAccount: Effect.fn(function* (s, cmd) { |
| 129 | const o = yield* authorized(s); |
| 130 | const actor = yield* CurrentActor; |
| 131 | return o.frozen ? [] : [new AccountFrozen({ reason: cmd.reason, by: actor })]; |
| 132 | }), |
| 133 | |
| 134 | CloseAccount: Effect.fn(function* (s) { |
| 135 | const o = yield* authorized(s); |
| 136 | if (o.balance !== 0) return yield* new BalanceNotZero({ balance: o.balance }); |
| 137 | return [new AccountClosed()]; |
| 138 | }), |
| 139 | |
| 140 | DebitTransfer: (s, cmd) => { |
| 141 | const o = openFor(s, cmd.requestedBy); |
| 142 | if (o._tag !== "Open") return o; |
| 143 | if (o.frozen) return new Frozen({ reason: o.frozen }); |
| 144 | if (cmd.amount > o.balance) |
| 145 | return new InsufficientFunds({ balance: o.balance, requested: cmd.amount }); |
| 146 | return [ |
| 147 | new TransferDebited({ |
| 148 | transferId: cmd.transferId, |
| 149 | amount: cmd.amount, |
| 150 | balanceAfter: o.balance - cmd.amount, |
| 151 | }), |
| 152 | ]; |
| 153 | }, |
| 154 | |
| 155 | CreditTransfer: (s, cmd) => |
| 156 | s._tag !== "Open" |
| 157 | ? new NotOpen() |
| 158 | : [ |
| 159 | new TransferCredited({ |
| 160 | transferId: cmd.transferId, |
| 161 | amount: cmd.amount, |
| 162 | balanceAfter: s.balance + cmd.amount, |
| 163 | }), |
| 164 | ], |
| 165 | |
| 166 | RefundTransfer: (s, cmd) => |
| 167 | s._tag === "Open" |
| 168 | ? [ |
| 169 | new TransferRefunded({ |
| 170 | transferId: cmd.transferId, |
| 171 | amount: cmd.amount, |
| 172 | balanceAfter: s.balance + cmd.amount, |
| 173 | }), |
| 174 | ] |
| 175 | : [new RefundStranded({ transferId: cmd.transferId, amount: cmd.amount })], |
| 176 | |
| 177 | RecordSettlement: (s, cmd) => |
| 178 | s._tag === "Open" |
| 179 | ? [ |
| 180 | new SettlementReceived({ |
| 181 | settlementId: cmd.settlementId, |
| 182 | amount: cmd.amount, |
| 183 | balanceAfter: s.balance + cmd.amount, |
| 184 | }), |
| 185 | ] |
| 186 | : [new SettlementOnClosedAccount({ settlementId: cmd.settlementId, amount: cmd.amount })], |
| 187 | }, |
| 188 | |
| 189 | evolve: { |
| 190 | AccountOpened: (_, e) => ({ |
| 191 | _tag: "Open" as const, |
| 192 | customerId: e.customerId, |
| 193 | owner: e.owner, |
| 194 | balance: 0, |
| 195 | frozen: null, |
| 196 | today: { day: "", withdrawn: 0 }, |
| 197 | }), |
| 198 | OwnerChanged: (s, e) => open.modify((o) => ({ ...o, customerId: e.customerId }))(s), |
| 199 | MoneyDeposited: (s, e) => Optic.replace(s, balance, e.balanceAfter), |
| 200 | TransferDebited: (s, e) => Optic.replace(s, balance, e.balanceAfter), |
| 201 | TransferCredited: (s, e) => Optic.replace(s, balance, e.balanceAfter), |
| 202 | TransferRefunded: (s, e) => Optic.replace(s, balance, e.balanceAfter), |
| 203 | SettlementReceived: (s, e) => Optic.replace(s, balance, e.balanceAfter), |
| 204 | MoneyWithdrawn: (s, e, { at }) => |
| 205 | open.modify((o) => { |
| 206 | const day = dayOf(at); |
| 207 | const withdrawn = (o.today.day === day ? o.today.withdrawn : 0) + e.amount; |
| 208 | return { ...o, balance: e.balanceAfter, today: { day, withdrawn } }; |
| 209 | })(s), |
| 210 | AccountFrozen: (s, e) => Optic.replace(s, frozen, e.reason), |
| 211 | AccountClosed: () => ({ _tag: "Closed" as const }), |
| 212 | RefundStranded: (s) => s, |
| 213 | SettlementOnClosedAccount: (s) => s, |
| 214 | }, |
| 215 | |
| 216 | reply: { |
| 217 | DepositMoney: (s) => ({ balance: s._tag === "Open" ? s.balance : 0 }), |
| 218 | WithdrawMoney: (s) => ({ balance: s._tag === "Open" ? s.balance : 0 }), |
| 219 | }, |
| 220 | // Clients may read and watch the account itself (its state and committed |
| 221 | // events) through an Api, judged like a view against its current state. |
| 222 | read: { |
| 223 | middleware: [Session], |
| 224 | rejects: [NotOwner], |
| 225 | validate: Effect.fn(function* (_request, s) { |
| 226 | yield* mustOwn(s._tag === "Open" ? s.customerId : null); |
| 227 | }), |
| 228 | }, |
| 229 | }) {} |