The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as Domain from "@alchemy.run/domain"; |
| 2 | import * as Effect from "effect/Effect"; |
| 3 | import { Account } from "./Account.aggregate.ts"; |
| 4 | import { AccountCard, CardChanged } from "./AccountCard.stream.ts"; |
| 5 | import { AccountSummary } from "./AccountSummary.view.ts"; |
| 6 | import { TransferCredited } from "./CreditTransfer.command.ts"; |
| 7 | import { TransferDebited } from "./DebitTransfer.command.ts"; |
| 8 | import { MoneyDeposited } from "./DepositMoney.command.ts"; |
| 9 | import { AccountFrozen } from "./FreezeAccount.command.ts"; |
| 10 | import { SettlementReceived } from "./RecordSettlement.command.ts"; |
| 11 | import { TransferRefunded } from "./RefundTransfer.command.ts"; |
| 12 | import { MoneyWithdrawn } from "./WithdrawMoney.command.ts"; |
| 13 | |
| 14 | /** |
| 15 | * Appends a new card whenever an account's summary changes: each input |
| 16 | * arrives with the summary's state after it. |
| 17 | */ |
| 18 | export class AccountCards extends Domain.Projection("AccountCards", { |
| 19 | from: [AccountSummary], |
| 20 | key: Account, |
| 21 | on: [ |
| 22 | MoneyDeposited, |
| 23 | MoneyWithdrawn, |
| 24 | TransferDebited, |
| 25 | TransferCredited, |
| 26 | TransferRefunded, |
| 27 | SettlementReceived, |
| 28 | AccountFrozen, |
| 29 | ], |
| 30 | writes: [AccountCard], |
| 31 | }) {} |
| 32 | |
| 33 | export const AccountCardsLive = AccountCards.toLayer( |
| 34 | Effect.gen(function* () { |
| 35 | const cards = yield* AccountCard; |
| 36 | return Effect.fn(function* (batch) { |
| 37 | for (const input of batch) { |
| 38 | const summary = input.state; |
| 39 | if (!summary?.customerId || !summary.owner) continue; |
| 40 | yield* cards.append( |
| 41 | input, |
| 42 | new CardChanged({ |
| 43 | customerId: summary.customerId, |
| 44 | owner: summary.owner, |
| 45 | balance: summary.balance, |
| 46 | frozen: summary.frozen, |
| 47 | }), |
| 48 | ); |
| 49 | } |
| 50 | }); |
| 51 | }), |
| 52 | ); |