The best way to control Claude Code, Codex and more from any computer or phone

main
ts 57 lines 1.79 KB
Raw
1 import * as Domain from "@alchemy.run/domain";
2 import * as Effect from "effect/Effect";
3 import * as Schema from "effect/Schema";
4 import { CustomerId } from "../Customer/Customer.id.ts";
5 import { Cents } from "../Money.ts";
6 import { Session } from "../Session.middleware.ts";
7 import { Account } from "./Account.aggregate.ts";
8 import { mustOwn } from "./mustOwn.ts";
9 import { NotOwner } from "./NotOwner.rejection.ts";
10
11 /** One movement of money on an account. */
12 export class StatementLine extends Domain.Event("StatementLine", {
13 kind: Schema.Literals([
14 "deposit",
15 "withdrawal",
16 "transfer-in",
17 "transfer-out",
18 "refund",
19 "settlement",
20 ]),
21 amount: Cents,
22 balance: Cents,
23 }) {}
24
25 const line =
26 (kind: StatementLine["kind"]) =>
27 (e: { readonly amount: number; readonly balanceAfter: number }) =>
28 new StatementLine({ kind, amount: e.amount, balance: e.balanceAfter });
29
30 /**
31 * Every movement of money on an account, oldest first: six events become
32 * one. It keeps the account's owner so it can authorize reads.
33 */
34 export class Statement extends Domain.View("Statement", {
35 from: [Account],
36 key: Account,
37 middleware: [Session],
38 rejects: [NotOwner],
39 state: Schema.Struct({ customerId: Schema.NullOr(CustomerId) }),
40 initial: { customerId: null },
41 evolve: {
42 AccountOpened: (_, e) => ({ customerId: e.customerId }),
43 OwnerChanged: (_, e) => ({ customerId: e.customerId }),
44 },
45 validate: Effect.fn(function* (_request, statement) {
46 yield* mustOwn(statement?.customerId ?? null);
47 }),
48 events: [StatementLine],
49 emit: {
50 MoneyDeposited: line("deposit"),
51 MoneyWithdrawn: line("withdrawal"),
52 TransferCredited: line("transfer-in"),
53 TransferDebited: line("transfer-out"),
54 TransferRefunded: line("refund"),
55 SettlementReceived: line("settlement"),
56 },
57 }) {}