@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 74 lines 2.51 KB
Raw
1 import { type AccountId, mainAccountId } from "@bank/domain/Account/Account.id";
2 import { CustomerId } from "@bank/domain/Customer/Customer.id";
3 import { Username } from "@bank/domain/Customer/Username";
4 import type { CustomerApi } from "@bank/domain/CustomerApi.api";
5 import { TransferId } from "@bank/domain/Transfer/Transfer.id";
6 import { TransferStatus } from "@bank/domain/Transfer/TransferStatus.view";
7 import * as Effect from "effect/Effect";
8 import * as Option from "effect/Option";
9 import * as Stream from "effect/Stream";
10
11 /** A CustomerApi client, signed in as one customer. */
12 export type CustomerClient = Effect.Success<typeof CustomerApi.client>;
13
14 /** The demo MFA code `MfaChallenge` accepts. */
15 export const mfa = { headers: { "x-mfa-code": "000000" } };
16
17 /**
18 * Register, then watch the customer's dashboard until `UsernameReservation`
19 * and `MainAccountProvisioning` have opened the main account. The username
20 * defaults to the customer id.
21 */
22 export const register = Effect.fn(function* (
23 client: CustomerClient,
24 customerId: string,
25 name: string,
26 username: string = customerId,
27 ) {
28 const id = CustomerId.make(customerId);
29 yield* client.RegisterCustomer(id, { name, username: Username.make(username) });
30 const accountId = mainAccountId(id);
31 yield* client.CustomerDashboard.watch(id).pipe(
32 Stream.map(({ state }) => state),
33 Stream.filter((dashboard) => dashboard !== null && accountId in dashboard.balances),
34 Stream.runHead,
35 Effect.timeout("20 seconds"),
36 );
37 return accountId;
38 });
39
40 /**
41 * Request a transfer under an id the client chooses (so a retry is
42 * idempotent), then watch its status until `TransferExecution` has completed
43 * or failed it.
44 */
45 export const transfer = Effect.fn(function* (
46 client: CustomerClient,
47 request: {
48 readonly transferId: string;
49 readonly from: AccountId;
50 readonly to: AccountId;
51 readonly amount: number;
52 },
53 ) {
54 const transferId = TransferId.make(request.transferId);
55 yield* client.RequestTransfer(
56 transferId,
57 { from: request.from, to: request.to, amount: request.amount },
58 mfa,
59 );
60 const settled = yield* client.TransferStatus.watch(transferId).pipe(
61 Stream.map(({ state }) => state),
62 Stream.filter(
63 (status) => status !== null && (status.completedAt !== null || status.failedReason !== null),
64 ),
65 Stream.runHead,
66 Effect.timeout("20 seconds"),
67 );
68 const status = Option.getOrThrow(settled)!;
69 return {
70 transferId,
71 status: TransferStatus.computed.status(status),
72 reason: status.failedReason,
73 };
74 });