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

main
ts 31 lines 1.09 KB
Raw
1 import * as Domain from "@alchemy.run/domain";
2 import * as Schema from "effect/Schema";
3 import { ClaimUsername, UsernameClaimed, UsernameTaken } from "./ClaimUsername.command.ts";
4 import { CustomerId } from "./Customer.id.ts";
5 import { Username } from "./Username.ts";
6
7 /**
8 * One username, and who holds it. Uniqueness spans every customer, so the
9 * invariant is its own aggregate, keyed by the username: two customers
10 * claiming the same name race on this one instance, and one of them wins.
11 */
12 export class UsernameClaim extends Domain.Aggregate("UsernameClaim", {
13 id: Username,
14 state: Schema.Union([
15 Schema.TaggedStruct("Free", {}),
16 Schema.TaggedStruct("Claimed", { customerId: CustomerId }),
17 ]),
18 initial: { _tag: "Free" },
19 commands: [ClaimUsername],
20 decide: {
21 ClaimUsername: (s, cmd) =>
22 s._tag === "Free"
23 ? [new UsernameClaimed({ customerId: cmd.customerId })]
24 : s.customerId === cmd.customerId
25 ? []
26 : new UsernameTaken(),
27 },
28 evolve: {
29 UsernameClaimed: (_, e) => ({ _tag: "Claimed" as const, customerId: e.customerId }),
30 },
31 }) {}