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 * 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 | /** What an account's card shows, as of one change. */ |
| 12 | export class CardChanged extends Domain.Event("CardChanged", { |
| 13 | customerId: CustomerId, |
| 14 | owner: Schema.String, |
| 15 | balance: Cents, |
| 16 | frozen: Schema.Boolean, |
| 17 | }) {} |
| 18 | |
| 19 | /** |
| 20 | * Every version of an account's card, in order. The latest entry is the card |
| 21 | * now (`peek`, `watch`); the log is its history (`events`, `list`). Written by |
| 22 | * the AccountCards projection. |
| 23 | */ |
| 24 | export class AccountCard extends Domain.Stream("AccountCard", { |
| 25 | key: Account, |
| 26 | entries: [CardChanged], |
| 27 | middleware: [Session], |
| 28 | rejects: [NotOwner], |
| 29 | // Judged on the latest card: only its owner (or an agent) may read it. |
| 30 | validate: Effect.fn(function* (_request, latest) { |
| 31 | yield* mustOwn(latest?.event.customerId ?? null); |
| 32 | }), |
| 33 | }) {} |