@devries-cameron20 / t3code Mirror · read-only
Fork 0

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

main
ts 81 lines 2.42 KB
Raw
1 import * as Domain from "@alchemy.run/domain";
2 import * as Schema from "effect/Schema";
3
4 export class Deposited extends Domain.Event("Deposited", { amount: Schema.Number }) {}
5 export class Withdrawn extends Domain.Event("Withdrawn", { amount: Schema.Number }) {}
6 export class Deposit extends Domain.Command("Deposit", {
7 target: Schema.String,
8 input: { amount: Schema.Number },
9 events: [Deposited],
10 }) {}
11 export class Withdraw extends Domain.Command("Withdraw", {
12 target: Schema.String,
13 input: { amount: Schema.Number },
14 events: [Withdrawn],
15 }) {}
16 export class Account extends Domain.Aggregate("Account", {
17 id: Schema.String,
18 state: Schema.Struct({ balance: Schema.Number }),
19 initial: { balance: 0 },
20 commands: [Deposit, Withdraw],
21 decide: {
22 Deposit: (_, cmd) => [new Deposited({ amount: cmd.amount })],
23 Withdraw: (_, cmd) => [new Withdrawn({ amount: cmd.amount })],
24 },
25 evolve: {
26 Deposited: (s, e) => ({ balance: s.balance + e.amount }),
27 Withdrawn: (s, e) => ({ balance: s.balance - e.amount }),
28 },
29 }) {}
30
31 const Net = Schema.Struct({ net: Schema.Number });
32
33 /** One row per account, counting deposits only. */
34 export class Summary extends Domain.View("Summary", {
35 from: [Account],
36 key: Account,
37 state: Net,
38 initial: { net: 0 },
39 evolve: { Deposited: (s, e) => ({ net: s.net + e.amount }) },
40 }) {}
41
42 /** Fixed to count withdrawals, an event its rows never received: version 1. */
43 export class SummaryV1 extends Domain.View("Summary", {
44 from: [Account],
45 key: Account,
46 state: Net,
47 initial: { net: 0 },
48 evolve: {
49 Deposited: (s, e) => ({ net: s.net + e.amount }),
50 Withdrawn: (s, e) => ({ net: s.net - e.amount }),
51 },
52 version: 1,
53 }) {}
54
55 /** Each account's latest net, built on the summary. */
56 export class Latest extends Domain.View("Latest", {
57 from: [Summary],
58 key: Account,
59 state: Net,
60 initial: { net: 0 },
61 evolve: { Deposited: (_, __, { state }) => ({ net: state?.net ?? 0 }) },
62 }) {}
63 export class LatestOfV1 extends Domain.View("Latest", {
64 from: [SummaryV1],
65 key: Account,
66 state: Net,
67 initial: { net: 0 },
68 evolve: {
69 Deposited: (_, __, { state }) => ({ net: state?.net ?? 0 }),
70 Withdrawn: (_, __, { state }) => ({ net: state?.net ?? 0 }),
71 },
72 }) {}
73
74 export class Bank extends Domain.Model("Bank", {
75 aggregates: [Account],
76 views: [Summary, Latest],
77 }) {}
78 export class BankV1 extends Domain.Model("Bank", {
79 aggregates: [Account],
80 views: [SummaryV1, LatestOfV1],
81 }) {}