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

main
ts 191 lines 7.12 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 } from "./internal.ts";
8
9 export interface ContactInformationProps {
10 /** Full name of the primary contact. */
11 fullName: string;
12 /** First line of the primary contact's mailing address. */
13 addressLine1: string;
14 /** Second line of the mailing address, if any. */
15 addressLine2?: string;
16 /** Third line of the mailing address, if any. */
17 addressLine3?: string;
18 /** City of the primary contact address. */
19 city: string;
20 /** State or region of the primary contact address, if applicable. */
21 stateOrRegion?: string;
22 /** District or county of the primary contact address, if applicable. */
23 districtOrCounty?: string;
24 /** Postal code of the primary contact address. */
25 postalCode: string;
26 /** ISO-3166 two-letter country code of the primary contact address. */
27 countryCode: string;
28 /** Phone number of the primary contact. */
29 phoneNumber: string;
30 /** Company name associated with the primary contact, if any. */
31 companyName?: string;
32 /** Website URL associated with the primary contact, if any. */
33 websiteUrl?: string;
34 /**
35 * Account ID to operate on. Only usable from an Organizations management or
36 * delegated-admin account with trusted access enabled; omit to target the
37 * calling account.
38 */
39 accountId?: string;
40 }
41
42 export interface ContactInformation extends Resource<
43 "AWS.Account.ContactInformation",
44 ContactInformationProps,
45 {
46 fullName: string;
47 addressLine1: string;
48 addressLine2?: string;
49 addressLine3?: string;
50 city: string;
51 stateOrRegion?: string;
52 districtOrCounty?: string;
53 postalCode: string;
54 countryCode: string;
55 phoneNumber: string;
56 companyName?: string;
57 websiteUrl?: string;
58 },
59 never,
60 Providers
61 > {}
62
63 /**
64 * The primary contact information of an AWS account — the account-global
65 * mailing address and phone number AWS uses to reach the account owner. Every
66 * account has exactly one primary contact; this resource upserts it via
67 * `account:PutContactInformation`. AWS does not allow deleting the primary
68 * contact, so destroying the resource stops managing it and leaves the last
69 * value in place.
70 *
71 * ### Setting the Primary Contact
72 * **Example:** Primary Contact for the Calling Account
73 * ```typescript
74 * const contact = yield* ContactInformation("PrimaryContact", {
75 * fullName: "Jane Doe",
76 * addressLine1: "123 Any Street",
77 * city: "Seattle",
78 * stateOrRegion: "WA",
79 * postalCode: "98101",
80 * countryCode: "US",
81 * phoneNumber: "+12065551234",
82 * companyName: "Acme Corp",
83 * websiteUrl: "https://acme.example.com",
84 * });
85 * ```
86 *
87 * **Example:** Primary Contact for an Organizations Member Account
88 * ```typescript
89 * const contact = yield* ContactInformation("MemberContact", {
90 * fullName: "Acme Ops",
91 * addressLine1: "123 Any Street",
92 * city: "Seattle",
93 * postalCode: "98101",
94 * countryCode: "US",
95 * phoneNumber: "+12065551234",
96 * accountId: "123456789012",
97 * });
98 * ```
99 *
100 * @resource
101 */
102 export const ContactInformation = Resource<ContactInformation>("AWS.Account.ContactInformation");
103
104 export const ContactInformationProvider = () =>
105 Provider.effect(
106 ContactInformation,
107 Effect.gen(function* () {
108 const toAttributes = (contact: account.ContactInformation) => ({
109 fullName: unwrapSensitive(contact.FullName) ?? "",
110 addressLine1: unwrapSensitive(contact.AddressLine1) ?? "",
111 addressLine2: unwrapSensitive(contact.AddressLine2),
112 addressLine3: unwrapSensitive(contact.AddressLine3),
113 city: unwrapSensitive(contact.City) ?? "",
114 stateOrRegion: unwrapSensitive(contact.StateOrRegion),
115 districtOrCounty: unwrapSensitive(contact.DistrictOrCounty),
116 postalCode: unwrapSensitive(contact.PostalCode) ?? "",
117 countryCode: unwrapSensitive(contact.CountryCode) ?? "",
118 phoneNumber: unwrapSensitive(contact.PhoneNumber) ?? "",
119 companyName: unwrapSensitive(contact.CompanyName),
120 websiteUrl: unwrapSensitive(contact.WebsiteUrl),
121 });
122
123 const observe = (accountId: string | undefined) =>
124 account.getContactInformation({ AccountId: accountId }).pipe(
125 Effect.map((r) =>
126 r.ContactInformation ? toAttributes(r.ContactInformation) : undefined,
127 ),
128 Effect.catchTag("ResourceNotFoundException", () => Effect.undefined),
129 );
130
131 return {
132 // Account-global singleton setting: nuke must not wipe the real
133 // primary contact of the account.
134 nuke: { skip: true },
135 stables: [],
136 // Targeting a different account manages a different singleton, so an
137 // accountId change replaces rather than mutating the old target.
138 diff: Effect.fn(function* ({ olds, news }) {
139 if (!isResolved(news)) return;
140 if ((olds?.accountId ?? news.accountId) !== news.accountId) {
141 return { action: "replace" } as const;
142 }
143 }),
144 read: Effect.fn(function* ({ olds }) {
145 return yield* observe(olds?.accountId);
146 }),
147 reconcile: Effect.fn(function* ({ news, session }) {
148 // The primary contact is a singleton driven entirely by
149 // `putContactInformation`, which is a full upsert — the request
150 // payload *is* the desired state.
151 yield* account.putContactInformation({
152 ContactInformation: {
153 FullName: news.fullName,
154 AddressLine1: news.addressLine1,
155 AddressLine2: news.addressLine2,
156 AddressLine3: news.addressLine3,
157 City: news.city,
158 StateOrRegion: news.stateOrRegion,
159 DistrictOrCounty: news.districtOrCounty,
160 PostalCode: news.postalCode,
161 CountryCode: news.countryCode,
162 PhoneNumber: news.phoneNumber,
163 CompanyName: news.companyName,
164 WebsiteUrl: news.websiteUrl,
165 },
166 AccountId: news.accountId,
167 });
168 yield* session.note(news.fullName);
169 return {
170 fullName: news.fullName,
171 addressLine1: news.addressLine1,
172 addressLine2: news.addressLine2,
173 addressLine3: news.addressLine3,
174 city: news.city,
175 stateOrRegion: news.stateOrRegion,
176 districtOrCounty: news.districtOrCounty,
177 postalCode: news.postalCode,
178 countryCode: news.countryCode,
179 phoneNumber: news.phoneNumber,
180 companyName: news.companyName,
181 websiteUrl: news.websiteUrl,
182 };
183 }),
184 // Account-global singleton: return the one contact if it exists.
185 list: () => observe(undefined).pipe(Effect.map((contact) => (contact ? [contact] : []))),
186 // AWS does not allow deleting the primary contact of an account —
187 // destroy just stops managing it and leaves the last value in place.
188 delete: Effect.fn(function* () {}),
189 };
190 }),
191 );