@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 184 lines 6.5 KB
Raw
1 import * as account from "@distilled.cloud/aws/account";
2 import { isResolved } from "alchemy/Diff";
3 import * as Provider from "alchemy/Provider";
4 import { Resource } from "alchemy/Resource";
5 import * as Effect from "effect/Effect";
6 import type { Providers } from "../Providers.ts";
7 import { unwrapSensitive as unwrap } from "./internal.ts";
8
9 /**
10 * The category of alternate contact. Each account has at most one contact of
11 * each type.
12 */
13 export type AlternateContactType = "BILLING" | "OPERATIONS" | "SECURITY";
14
15 const ALL_CONTACT_TYPES: AlternateContactType[] = ["BILLING", "OPERATIONS", "SECURITY"];
16
17 export interface AlternateContactProps {
18 /**
19 * Which alternate contact slot to set: `BILLING`, `OPERATIONS`, or
20 * `SECURITY`. Each account holds at most one contact per type; changing the
21 * type replaces the resource.
22 */
23 alternateContactType: AlternateContactType;
24 /** Name of the alternate contact. */
25 name: string;
26 /** Title of the alternate contact. */
27 title: string;
28 /** Email address of the alternate contact. */
29 emailAddress: string;
30 /** Phone number of the alternate contact. */
31 phoneNumber: string;
32 /**
33 * Account ID to operate on. Only usable from an Organizations management or
34 * delegated-admin account with trusted access enabled; omit to target the
35 * calling account.
36 */
37 accountId?: string;
38 }
39
40 export interface AlternateContact extends Resource<
41 "AWS.Account.AlternateContact",
42 AlternateContactProps,
43 {
44 alternateContactType: AlternateContactType;
45 name: string;
46 title: string;
47 emailAddress: string;
48 phoneNumber: string;
49 },
50 never,
51 Providers
52 > {}
53
54 /**
55 * An alternate contact for an AWS account. AWS accounts support one alternate
56 * contact for each of the `BILLING`, `OPERATIONS`, and `SECURITY` categories.
57 * These are account-global singletons: setting one overwrites any existing
58 * contact of the same type, and deleting removes it entirely.
59 *
60 * ### Setting an Alternate Contact
61 * **Example:** Operations Contact
62 * ```typescript
63 * const contact = yield* AlternateContact("OpsContact", {
64 * alternateContactType: "OPERATIONS",
65 * name: "Ops Team",
66 * title: "On-Call Engineer",
67 * emailAddress: "ops@example.com",
68 * phoneNumber: "+15555550123",
69 * });
70 * ```
71 *
72 * **Example:** Billing Contact for an Organizations Member Account
73 * ```typescript
74 * const contact = yield* AlternateContact("BillingContact", {
75 * alternateContactType: "BILLING",
76 * name: "Finance",
77 * title: "AP Clerk",
78 * emailAddress: "ap@example.com",
79 * phoneNumber: "+15555550124",
80 * accountId: "123456789012",
81 * });
82 * ```
83 *
84 * @resource
85 */
86 export const AlternateContact = Resource<AlternateContact>("AWS.Account.AlternateContact");
87
88 export const AlternateContactProvider = () =>
89 Provider.effect(
90 AlternateContact,
91 Effect.gen(function* () {
92 const observe = (alternateContactType: AlternateContactType, accountId: string | undefined) =>
93 account
94 .getAlternateContact({
95 AlternateContactType: alternateContactType,
96 AccountId: accountId,
97 })
98 .pipe(
99 Effect.map((r) => r.AlternateContact),
100 Effect.catchTag("ResourceNotFoundException", () => Effect.undefined),
101 );
102
103 return {
104 // Account-global singleton setting: nuke must not wipe real account
105 // contacts it never created.
106 nuke: { skip: true },
107 stables: [],
108 // Changing the contact type targets a different account-global slot, so
109 // it must replace rather than mutate the old slot in place.
110 diff: Effect.fn(function* ({ olds, news }) {
111 if (!isResolved(news)) return;
112 if (
113 (olds?.alternateContactType ?? news.alternateContactType) !== news.alternateContactType
114 ) {
115 return { action: "replace" } as const;
116 }
117 }),
118 read: Effect.fn(function* ({ olds, output }) {
119 const type = output?.alternateContactType ?? olds?.alternateContactType;
120 if (!type) return undefined;
121 const accountId = olds?.accountId;
122 const contact = yield* observe(type, accountId);
123 if (!contact) return undefined;
124 return {
125 alternateContactType: type,
126 name: unwrap(contact.Name) ?? output?.name ?? "",
127 title: unwrap(contact.Title) ?? output?.title ?? "",
128 emailAddress: unwrap(contact.EmailAddress) ?? output?.emailAddress ?? "",
129 phoneNumber: unwrap(contact.PhoneNumber) ?? output?.phoneNumber ?? "",
130 };
131 }),
132 reconcile: Effect.fn(function* ({ news, session }) {
133 yield* account.putAlternateContact({
134 AlternateContactType: news.alternateContactType,
135 Name: news.name,
136 Title: news.title,
137 EmailAddress: news.emailAddress,
138 PhoneNumber: news.phoneNumber,
139 AccountId: news.accountId,
140 });
141 yield* session.note(`${news.alternateContactType}:${news.emailAddress}`);
142 return {
143 alternateContactType: news.alternateContactType,
144 name: news.name,
145 title: news.title,
146 emailAddress: news.emailAddress,
147 phoneNumber: news.phoneNumber,
148 };
149 }),
150 // Account-global singleton: enumerate each of the three contact slots
151 // and return whichever ones exist.
152 list: () =>
153 Effect.gen(function* () {
154 const contacts = yield* Effect.forEach(
155 ALL_CONTACT_TYPES,
156 (type) =>
157 observe(type, undefined).pipe(
158 Effect.map((contact) =>
159 contact
160 ? {
161 alternateContactType: type,
162 name: unwrap(contact.Name) ?? "",
163 title: unwrap(contact.Title) ?? "",
164 emailAddress: unwrap(contact.EmailAddress) ?? "",
165 phoneNumber: unwrap(contact.PhoneNumber) ?? "",
166 }
167 : undefined,
168 ),
169 ),
170 { concurrency: 3 },
171 );
172 return contacts.filter((c) => c !== undefined);
173 }),
174 delete: Effect.fn(function* ({ output, olds }) {
175 yield* account
176 .deleteAlternateContact({
177 AlternateContactType: output.alternateContactType,
178 AccountId: olds.accountId,
179 })
180 .pipe(Effect.catchTag("ResourceNotFoundException", () => Effect.void));
181 }),
182 };
183 }),
184 );