@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 33 lines 1.34 KB
Raw
1 import * as Domain from "@alchemy.run/domain";
2 import * as Effect from "effect/Effect";
3 import { ClaimUsername } from "./ClaimUsername.command.ts";
4 import { ConfirmRegistration } from "./ConfirmRegistration.command.ts";
5 import { Customer } from "./Customer.aggregate.ts";
6 import { RegistrationRequested } from "./RegisterCustomer.command.ts";
7 import { RejectRegistration } from "./RejectRegistration.command.ts";
8 import { UsernameClaim } from "./UsernameClaim.aggregate.ts";
9
10 /**
11 * Reserve first, then commit: claim the username, and only then register the
12 * customer. Nothing downstream (the main account) happens for a registration
13 * whose username is taken.
14 */
15 export class UsernameReservation extends Domain.Policy("UsernameReservation", {
16 from: Customer,
17 on: [RegistrationRequested],
18 }) {}
19
20 export const UsernameReservationLive = UsernameReservation.toLayer(
21 Effect.gen(function* () {
22 const claims = yield* UsernameClaim;
23 const customers = yield* Customer;
24 return Effect.fn(function* ({ source, event }) {
25 yield* claims.send(event.username, new ClaimUsername({ customerId: source.id })).pipe(
26 Effect.andThen(customers.send(source, new ConfirmRegistration())),
27 Effect.catchTag("UsernameTaken", () =>
28 customers.send(source, new RejectRegistration({ reason: "username taken" })),
29 ),
30 );
31 });
32 }),
33 );