@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 315 lines 10.8 KB
Raw
1 import * as cloudchannel from "@distilled.cloud/gcp/cloudchannel_v1";
2 import { Unowned } from "alchemy/AdoptPolicy";
3 import { isResolved } from "alchemy/Diff";
4 import * as Provider from "alchemy/Provider";
5 import { Resource } from "alchemy/Resource";
6 import * as Data from "effect/Data";
7 import * as Effect from "effect/Effect";
8 import type { Providers } from "../Providers.ts";
9 import {
10 desiredCustomer,
11 findCustomerByDomain,
12 getCustomer,
13 jsonEqual,
14 replaceOnIdentity,
15 sameText,
16 toAccountName,
17 toCustomerAttrs,
18 toCustomerName,
19 toDomain,
20 toOrgDisplayName,
21 updateMaskOf,
22 } from "./internal.ts";
23
24 export type CustomerProps = {
25 /**
26 * Reseller Cloud Channel account. Full name `accounts/{account}` or
27 * the account id (typically `C` followed by digits). Immutable —
28 * changing it replaces the customer.
29 */
30 parent: string;
31 /**
32 * Customer id (last segment of `accounts/{account}/customers/{customer}`).
33 * Server-assigned on create. Immutable — changing it replaces the
34 * customer.
35 */
36 customerId?: string;
37 /**
38 * Organization display name. If omitted, a unique name is generated.
39 */
40 orgDisplayName?: string;
41 /**
42 * Primary domain. Must match `primaryContactInfo.email`'s domain. If
43 * omitted, a unique `{name}.example.com` domain is generated.
44 */
45 domain?: string;
46 /**
47 * Organization postal address. A US address is used when omitted.
48 */
49 orgPostalAddress?: cloudchannel.GoogleTypePostalAddress;
50 /**
51 * Primary contact. Email defaults to `admin@{domain}`.
52 */
53 primaryContactInfo?: cloudchannel.GoogleCloudChannelV1ContactInfo;
54 /**
55 * Secondary contact email used as a recovery address.
56 */
57 alternateEmail?: string;
58 /**
59 * BCP-47 language code such as `en-US`.
60 * @default "en-US"
61 */
62 languageCode?: string;
63 /**
64 * External CRM id.
65 */
66 correlationId?: string;
67 /**
68 * Attestation that provided information is correct. Required for GCP
69 * entitlements.
70 */
71 customerAttestationState?:
72 | cloudchannel.GoogleCloudChannelV1CustomerCustomerAttestationStateEnum
73 | (string & {});
74 /**
75 * Cloud Identity id of the customer's channel partner, when this
76 * customer is sold through a partner.
77 */
78 channelPartnerId?: string;
79 };
80
81 export type Customer = Resource<
82 "GCP.CloudChannel.Customer",
83 CustomerProps,
84 {
85 /** Resource name `accounts/{account}/customers/{customer}`. */
86 name: string;
87 /** Customer id (last path segment). */
88 customerId: string;
89 /** Account or channel-partner parent used on create. */
90 parent: string;
91 /** Reseller account `accounts/{account}`. */
92 account: string;
93 /** Organization display name. */
94 orgDisplayName: string | undefined;
95 /** Primary domain. */
96 domain: string | undefined;
97 /** Organization postal address. */
98 orgPostalAddress: cloudchannel.GoogleTypePostalAddress | undefined;
99 /** Primary contact. */
100 primaryContactInfo: cloudchannel.GoogleCloudChannelV1ContactInfo | undefined;
101 /** Alternate email. */
102 alternateEmail: string | undefined;
103 /** BCP-47 language code. */
104 languageCode: string | undefined;
105 /** External CRM id. */
106 correlationId: string | undefined;
107 /** Customer attestation state. */
108 customerAttestationState: string | undefined;
109 /** Channel partner Cloud Identity id, if any. */
110 channelPartnerId: string | undefined;
111 /** Cloud Identity id, once provisioned. */
112 cloudIdentityId: string | undefined;
113 /** RFC3339 creation timestamp. */
114 createTime: string | undefined;
115 /** RFC3339 last-update timestamp. */
116 updateTime: string | undefined;
117 },
118 never,
119 Providers
120 >;
121
122 /**
123 * A Cloud Channel customer of a reseller or distributor.
124 *
125 * Customers have no labels field, so ownership rests on the deterministic
126 * domain: `read` reports a customer it finds without prior state as
127 * unowned (adopt it with `--adopt`). The reseller account (`parent`)
128 * and customer id are identity. Display name, address, contact, domain,
129 * language, and attestation update in place.
130 *
131 * Creating customers requires Cloud Channel reseller access.
132 *
133 * ### Creating a Customer
134 * **Example:** Generated domain
135 * ```typescript
136 * const customer = yield* GCP.CloudChannel.Customer("Acme", {
137 * parent: "accounts/C012345",
138 * orgDisplayName: "Acme Corp",
139 * });
140 * ```
141 *
142 * **Example:** Explicit domain and contact
143 * ```typescript
144 * const customer = yield* GCP.CloudChannel.Customer("Acme", {
145 * parent: "accounts/C012345",
146 * orgDisplayName: "Acme Corp",
147 * domain: "acme.example.com",
148 * primaryContactInfo: {
149 * firstName: "Ada",
150 * lastName: "Lovelace",
151 * email: "admin@acme.example.com",
152 * },
153 * orgPostalAddress: {
154 * regionCode: "US",
155 * postalCode: "94105",
156 * locality: "San Francisco",
157 * administrativeArea: "CA",
158 * addressLines: ["100 Market Street"],
159 * },
160 * });
161 * ```
162 *
163 * ### Updating a Customer
164 * **Example:** Rename
165 * ```typescript
166 * // Same logical id as before; only the display name changes.
167 * const customer = yield* GCP.CloudChannel.Customer("Acme", {
168 * parent: "accounts/C012345",
169 * orgDisplayName: "Acme Corporation",
170 * domain: "acme.example.com",
171 * });
172 * ```
173 *
174 * @resource
175 * @category CloudChannel
176 */
177 export const Customer = Resource<Customer>("GCP.CloudChannel.Customer");
178
179 export class CustomerNotResolved extends Data.TaggedError("GCP.CloudChannel.CustomerNotResolved")<{
180 resourceName: string;
181 }> {}
182
183 export const CustomerProvider = () =>
184 Provider.succeed(Customer, {
185 stables: ["name", "customerId", "account", "createTime"],
186
187 diff: Effect.fn(function* ({ news, olds, output }) {
188 if (!isResolved(news)) return undefined;
189 const previousParent = olds?.parent ?? output?.parent ?? output?.account;
190 const nextParent = toAccountName(news.parent);
191 return replaceOnIdentity({
192 previousId: olds?.customerId ?? output?.customerId,
193 nextId: news.customerId,
194 previousParent: previousParent !== undefined ? toAccountName(previousParent) : undefined,
195 nextParent,
196 });
197 }),
198
199 read: Effect.fn(function* ({ id, olds, output }) {
200 const parent = toAccountName(olds?.parent ?? output?.parent ?? output?.account ?? "");
201 const name = toCustomerName(parent, olds?.customerId ?? output?.customerId ?? output?.name);
202 let existing = yield* getCustomer(output?.name ?? name);
203 if (existing === undefined) {
204 const domain = yield* toDomain(id, olds?.domain, output?.domain);
205 existing = yield* findCustomerByDomain(parent, domain);
206 }
207 if (existing === undefined) return undefined;
208 const attrs = toCustomerAttrs(existing, parent);
209 // No labels field: without prior state the customer may not be ours.
210 return output !== undefined ? attrs : Unowned(attrs);
211 }),
212
213 reconcile: Effect.fn(function* ({ id, news, output }) {
214 const parent = toAccountName(news.parent);
215 const orgDisplayName = yield* toOrgDisplayName(
216 id,
217 news.orgDisplayName,
218 output?.orgDisplayName,
219 );
220 const domain = yield* toDomain(id, news.domain, output?.domain);
221 const body = desiredCustomer({
222 orgDisplayName,
223 domain,
224 orgPostalAddress: news.orgPostalAddress ?? output?.orgPostalAddress,
225 primaryContactInfo: news.primaryContactInfo ?? output?.primaryContactInfo,
226 alternateEmail: news.alternateEmail,
227 languageCode: news.languageCode ?? output?.languageCode,
228 correlationId: news.correlationId,
229 customerAttestationState: news.customerAttestationState,
230 channelPartnerId: news.channelPartnerId,
231 });
232 const name = toCustomerName(parent, news.customerId ?? output?.customerId ?? output?.name);
233
234 let current = yield* getCustomer(output?.name ?? name);
235 if (current === undefined) {
236 current = yield* findCustomerByDomain(parent, domain);
237 }
238
239 if (current === undefined) {
240 const created = yield* cloudchannel
241 .createAccountsCustomers({
242 parent,
243 body,
244 })
245 .pipe(Effect.catchTag("Conflict", () => findCustomerByDomain(parent, domain)));
246 current = created ?? undefined;
247 }
248
249 if (current === undefined) {
250 return yield* new CustomerNotResolved({
251 resourceName: name || `${parent}/customers/${domain}`,
252 });
253 }
254
255 const currentName = current.name ?? name;
256 const displayChanged = !sameText(current.orgDisplayName, orgDisplayName);
257 const domainChanged = !sameText(current.domain, domain);
258 const addressChanged = !jsonEqual(current.orgPostalAddress, body.orgPostalAddress);
259 const contactChanged = !jsonEqual(
260 {
261 firstName: current.primaryContactInfo?.firstName,
262 lastName: current.primaryContactInfo?.lastName,
263 email: current.primaryContactInfo?.email,
264 phone: current.primaryContactInfo?.phone,
265 title: current.primaryContactInfo?.title,
266 },
267 {
268 firstName: body.primaryContactInfo?.firstName,
269 lastName: body.primaryContactInfo?.lastName,
270 email: body.primaryContactInfo?.email,
271 phone: body.primaryContactInfo?.phone,
272 title: body.primaryContactInfo?.title,
273 },
274 );
275 const alternateChanged = !sameText(current.alternateEmail, news.alternateEmail);
276 const languageChanged = !sameText(current.languageCode, body.languageCode);
277 const correlationChanged = !sameText(current.correlationId, news.correlationId);
278 const attestationChanged =
279 news.customerAttestationState !== undefined &&
280 !sameText(current.customerAttestationState, news.customerAttestationState);
281 const partnerChanged =
282 news.channelPartnerId !== undefined &&
283 !sameText(current.channelPartnerId, news.channelPartnerId);
284
285 const updateMask = updateMaskOf(
286 displayChanged ? "org_display_name" : undefined,
287 domainChanged ? "domain" : undefined,
288 addressChanged ? "org_postal_address" : undefined,
289 contactChanged ? "primary_contact_info" : undefined,
290 alternateChanged ? "alternate_email" : undefined,
291 languageChanged ? "language_code" : undefined,
292 correlationChanged ? "correlation_id" : undefined,
293 attestationChanged ? "customer_attestation_state" : undefined,
294 partnerChanged ? "channel_partner_id" : undefined,
295 );
296
297 if (updateMask.length > 0 && currentName.length > 0) {
298 current = yield* cloudchannel.patchAccountsCustomers({
299 name: currentName,
300 updateMask,
301 body,
302 });
303 }
304
305 return toCustomerAttrs(current, parent);
306 }),
307
308 delete: Effect.fn(function* ({ output }) {
309 const name = output.name;
310 if (name.length === 0) return;
311 yield* cloudchannel
312 .deleteAccountsCustomers({ name })
313 .pipe(Effect.catchTag("NotFound", () => Effect.void));
314 }),
315 });