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