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 { Account } from "../Account/Account.aggregate.ts"; |
| 5 | import { mustOwn } from "../Account/mustOwn.ts"; |
| 6 | import { NotOwner } from "../Account/NotOwner.rejection.ts"; |
| 7 | import { CustomerId } from "../Customer/Customer.id.ts"; |
| 8 | import { Cents } from "../Money.ts"; |
| 9 | import { Session } from "../Session.middleware.ts"; |
| 10 | import { type TransferRequested } from "./RequestTransfer.command.ts"; |
| 11 | import { Transfer } from "./Transfer.aggregate.ts"; |
| 12 | import { TransferId } from "./Transfer.id.ts"; |
| 13 | |
| 14 | /** Money moved in or out of an account by a transfer. */ |
| 15 | export class TransferMoved extends Domain.Event("TransferMoved", { |
| 16 | transferId: TransferId, |
| 17 | amount: Cents, |
| 18 | direction: Schema.Literals(["in", "out"]), |
| 19 | }) {} |
| 20 | |
| 21 | const moved = (direction: "in" | "out") => (e: TransferRequested) => |
| 22 | new TransferMoved({ transferId: e.transferId, amount: e.amount, direction }); |
| 23 | |
| 24 | /** |
| 25 | * Transfers touching an account. One request is routed to both accounts, so |
| 26 | * the recipient sees incoming transfers here. It keeps the account's owner so |
| 27 | * it can authorize reads. |
| 28 | */ |
| 29 | export class AccountTransfers extends Domain.View("AccountTransfers", { |
| 30 | from: [Transfer, Account], |
| 31 | key: Account, |
| 32 | middleware: [Session], |
| 33 | rejects: [NotOwner], |
| 34 | state: Schema.Struct({ customerId: Schema.NullOr(CustomerId) }), |
| 35 | initial: { customerId: null }, |
| 36 | evolve: { |
| 37 | AccountOpened: (_, e) => ({ customerId: e.customerId }), |
| 38 | OwnerChanged: (_, e) => ({ customerId: e.customerId }), |
| 39 | }, |
| 40 | validate: Effect.fn(function* (_request, transfers) { |
| 41 | yield* mustOwn(transfers?.customerId ?? null); |
| 42 | }), |
| 43 | keyOf: { |
| 44 | TransferRequested: { |
| 45 | out: ({ event }) => Account.ref(event.from), |
| 46 | in: ({ event }) => Account.ref(event.to), |
| 47 | }, |
| 48 | }, |
| 49 | events: [TransferMoved], |
| 50 | emit: { |
| 51 | TransferRequested: { out: moved("out"), in: moved("in") }, |
| 52 | }, |
| 53 | }) {} |