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

main
ts 304 lines 9.6 KB
Raw
1 import * as androidenterprise from "@distilled.cloud/gcp/androidenterprise_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 { GcpEnvironment } from "../Environment.ts";
9 import type { Providers } from "../Providers.ts";
10 import {
11 findOwnedCluster,
12 getCluster,
13 hasOwnershipMarker,
14 jsonEqual,
15 listOwnedClusters,
16 ownedByAlchemy,
17 ownershipTextFromNames,
18 publicNames,
19 sameStringList,
20 sameText,
21 defaultNames,
22 toDisplayName,
23 } from "./internal.ts";
24
25 export type StorelayoutclusterProps = {
26 /**
27 * Play EMM enterprise id. Immutable — changing it replaces the cluster.
28 */
29 enterpriseId: string;
30 /**
31 * Parent store page id. Immutable — changing it replaces the cluster.
32 */
33 pageId: string;
34 /**
35 * Server-assigned cluster id. Immutable — changing it replaces the
36 * cluster.
37 */
38 clusterId?: string;
39 /**
40 * Localized cluster names, shown to device users as given.
41 */
42 name?: androidenterprise.LocalizedText[];
43 /**
44 * Product ids displayed in the cluster, in order. Duplicates are not
45 * allowed.
46 */
47 productId?: string[];
48 /**
49 * US-ASCII ordering key among elements on the parent page. Max 256
50 * characters.
51 */
52 orderInPage?: string;
53 };
54
55 export type Storelayoutcluster = Resource<
56 "GCP.AndroidEnterprise.Storelayoutcluster",
57 StorelayoutclusterProps,
58 {
59 /** Server-assigned cluster id. */
60 clusterId: string;
61 /** Parent store page id. */
62 pageId: string;
63 /** Play EMM enterprise id. */
64 enterpriseId: string;
65 /** Project id used when the cluster was reconciled. */
66 project: string;
67 /** Localized names with the Alchemy ownership prefix stripped. */
68 name: androidenterprise.LocalizedText[] | undefined;
69 /** Product ids in display order. */
70 productId: string[] | undefined;
71 /** Ordering key on the parent page. */
72 orderInPage: string | undefined;
73 },
74 never,
75 Providers
76 >;
77
78 /**
79 * A managed Google Play store cluster (`storelayoutclusters`).
80 *
81 * Store clusters have no labels field and their names are shown to device
82 * users, so Alchemy tracks a cluster by its recorded id only (`list` finds
83 * clusters from older releases that carried a name marker). `enterpriseId`, `pageId`,
84 * and `clusterId` are identity — changing any replaces the cluster.
85 * Names, products, and order update in place.
86 *
87 * ### Creating a Store Cluster
88 * **Example:** Generated name
89 * ```typescript
90 * const cluster = yield* GCP.AndroidEnterprise.Storelayoutcluster("Apps", {
91 * enterpriseId: page.enterpriseId,
92 * pageId: page.pageId,
93 * });
94 * ```
95 *
96 * **Example:** Explicit name and products
97 * ```typescript
98 * const cluster = yield* GCP.AndroidEnterprise.Storelayoutcluster("Apps", {
99 * enterpriseId: page.enterpriseId,
100 * pageId: page.pageId,
101 * name: [{ locale: "en-US", text: "Work apps" }],
102 * productId: ["app:com.google.android.gm"],
103 * });
104 * ```
105 *
106 * ### Updating a Store Cluster
107 * Re-declare the same logical id with changed props; the engine keeps the
108 * physical resource and updates it in place.
109 *
110 * **Example:** Rename
111 * ```typescript
112 * const cluster = yield* GCP.AndroidEnterprise.Storelayoutcluster("Apps", {
113 * enterpriseId: page.enterpriseId,
114 * pageId: page.pageId,
115 * name: [{ locale: "en-US", text: "Featured apps" }],
116 * });
117 * ```
118 *
119 * @resource
120 * @category AndroidEnterprise
121 */
122 export const Storelayoutcluster = Resource<Storelayoutcluster>(
123 "GCP.AndroidEnterprise.Storelayoutcluster",
124 );
125
126 export class StorelayoutclusterNotResolved extends Data.TaggedError(
127 "GCP.AndroidEnterprise.StorelayoutclusterNotResolved",
128 )<{
129 enterpriseId: string;
130 pageId: string;
131 clusterId: string;
132 }> {}
133
134 const toAttrs = (
135 cluster: androidenterprise.StoreCluster,
136 enterpriseId: string,
137 pageId: string,
138 project: string,
139 ) => ({
140 clusterId: cluster.id ?? "",
141 pageId,
142 enterpriseId,
143 project,
144 name: publicNames(cluster.name),
145 productId: cluster.productId,
146 orderInPage: cluster.orderInPage,
147 });
148
149 const desiredBody = (input: {
150 clusterId?: string;
151 name: androidenterprise.LocalizedText[];
152 news: StorelayoutclusterProps;
153 current?: androidenterprise.StoreCluster;
154 }): androidenterprise.StoreCluster => ({
155 id: input.clusterId,
156 name: input.name,
157 productId: input.news.productId ?? input.current?.productId,
158 orderInPage: input.news.orderInPage ?? input.current?.orderInPage,
159 });
160
161 const needsSync = (
162 current: androidenterprise.StoreCluster,
163 desired: androidenterprise.StoreCluster,
164 ) =>
165 !jsonEqual(current.name, desired.name) ||
166 (desired.productId !== undefined && !sameStringList(current.productId, desired.productId)) ||
167 (desired.orderInPage !== undefined && !sameText(current.orderInPage, desired.orderInPage));
168
169 export const StorelayoutclusterProvider = () =>
170 Provider.succeed(Storelayoutcluster, {
171 stables: ["clusterId", "pageId", "enterpriseId", "project"],
172
173 diff: Effect.fn(function* ({ news, olds, output }) {
174 if (!isResolved(news)) return undefined;
175 const previousEnterprise = olds?.enterpriseId ?? output?.enterpriseId;
176 if (previousEnterprise !== undefined && news.enterpriseId !== previousEnterprise) {
177 return { action: "replace" as const, deleteFirst: false };
178 }
179 const previousPage = olds?.pageId ?? output?.pageId;
180 if (previousPage !== undefined && news.pageId !== previousPage) {
181 return { action: "replace" as const, deleteFirst: false };
182 }
183 const previousId = olds?.clusterId ?? output?.clusterId;
184 if (
185 previousId !== undefined &&
186 news.clusterId !== undefined &&
187 news.clusterId !== previousId
188 ) {
189 return { action: "replace" as const, deleteFirst: false };
190 }
191 return undefined;
192 }),
193
194 read: Effect.fn(function* ({ id, olds, output }) {
195 const env = yield* GcpEnvironment.current;
196 const enterpriseId = olds?.enterpriseId ?? output?.enterpriseId ?? "";
197 const pageId = olds?.pageId ?? output?.pageId ?? "";
198 const clusterId = olds?.clusterId ?? output?.clusterId ?? "";
199 let existing = yield* getCluster(enterpriseId, pageId, clusterId);
200 let observedPageId = pageId;
201 if (existing === undefined && enterpriseId.length > 0) {
202 const found = yield* findOwnedCluster(id, enterpriseId, pageId || undefined);
203 existing = found?.cluster;
204 observedPageId = found?.pageId ?? pageId;
205 }
206 if (existing === undefined) return undefined;
207 const attrs = toAttrs(existing, enterpriseId, observedPageId, env.project);
208 // Only a recorded id (or a legacy ownership marker) proves ownership;
209 // names and titles are user-visible and carry no marker.
210 return output !== undefined ||
211 (yield* ownedByAlchemy(id, ownershipTextFromNames(existing.name)))
212 ? attrs
213 : Unowned(attrs);
214 }),
215
216 list: () =>
217 Effect.gen(function* () {
218 const env = yield* GcpEnvironment.current;
219 const clusters = yield* listOwnedClusters();
220 return clusters
221 .filter(({ cluster }) => hasOwnershipMarker(ownershipTextFromNames(cluster.name)))
222 .map(({ cluster, enterpriseId, pageId }) =>
223 toAttrs(cluster, enterpriseId, pageId, env.project),
224 );
225 }),
226
227 reconcile: Effect.fn(function* ({ id, news, output }) {
228 const env = yield* GcpEnvironment.current;
229 const enterpriseId = news.enterpriseId;
230 const pageId = news.pageId;
231 const displayName = yield* toDisplayName(id, news.name?.[0]?.text, output?.name?.[0]?.text);
232 const name = defaultNames(news.name, displayName);
233
234 let current = yield* getCluster(
235 enterpriseId,
236 pageId,
237 news.clusterId ?? output?.clusterId ?? "",
238 );
239 if (current === undefined) {
240 const found = yield* findOwnedCluster(id, enterpriseId, pageId);
241 current = found?.cluster;
242 }
243
244 if (current === undefined) {
245 const created = yield* androidenterprise
246 .insertStorelayoutclusters({
247 enterpriseId,
248 pageId,
249 body: desiredBody({ name, news }),
250 })
251 .pipe(
252 Effect.catchTag("Conflict", () =>
253 findOwnedCluster(id, enterpriseId, pageId).pipe(
254 Effect.map((found) => found?.cluster),
255 ),
256 ),
257 );
258 current = created ?? undefined;
259 }
260
261 if (current === undefined) {
262 return yield* new StorelayoutclusterNotResolved({
263 enterpriseId,
264 pageId,
265 clusterId: news.clusterId ?? output?.clusterId ?? displayName,
266 });
267 }
268
269 const clusterId = current.id ?? news.clusterId ?? output?.clusterId ?? "";
270 const desired = desiredBody({
271 clusterId,
272 name,
273 news,
274 current,
275 });
276 if (needsSync(current, desired)) {
277 current = yield* androidenterprise.updateStorelayoutclusters({
278 enterpriseId,
279 pageId,
280 clusterId,
281 body: desired,
282 });
283 }
284
285 return toAttrs(current, enterpriseId, pageId, env.project);
286 }),
287
288 delete: Effect.fn(function* ({ output }) {
289 if (!output.enterpriseId || !output.pageId || !output.clusterId) return;
290 yield* androidenterprise
291 .deleteStorelayoutclusters({
292 enterpriseId: output.enterpriseId,
293 pageId: output.pageId,
294 clusterId: output.clusterId,
295 })
296 .pipe(
297 Effect.catchTags({
298 NotFound: () => Effect.void,
299 BadRequest: () => Effect.void,
300 Conflict: () => Effect.void,
301 }),
302 );
303 }),
304 });