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
| 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 | findOwnedPage, |
| 12 | getPage, |
| 13 | hasOwnershipMarker, |
| 14 | jsonEqual, |
| 15 | listOwnedPages, |
| 16 | ownedByAlchemy, |
| 17 | ownershipTextFromNames, |
| 18 | publicNames, |
| 19 | sameStringList, |
| 20 | defaultNames, |
| 21 | toDisplayName, |
| 22 | } from "./internal.ts"; |
| 23 | |
| 24 | export type StorelayoutpageProps = { |
| 25 | /** |
| 26 | * Play EMM enterprise id. Immutable — changing it replaces the page. |
| 27 | */ |
| 28 | enterpriseId: string; |
| 29 | /** |
| 30 | * Server-assigned store page id. Immutable — changing it replaces the |
| 31 | * page. |
| 32 | */ |
| 33 | pageId?: string; |
| 34 | /** |
| 35 | * Localized page names, shown to device users as given. |
| 36 | */ |
| 37 | name?: androidenterprise.LocalizedText[]; |
| 38 | /** |
| 39 | * Page ids a user can reach from this page. The list cannot include |
| 40 | * this page. |
| 41 | */ |
| 42 | link?: string[]; |
| 43 | }; |
| 44 | |
| 45 | export type Storelayoutpage = Resource< |
| 46 | "GCP.AndroidEnterprise.Storelayoutpage", |
| 47 | StorelayoutpageProps, |
| 48 | { |
| 49 | /** Server-assigned store page id. */ |
| 50 | pageId: string; |
| 51 | /** Play EMM enterprise id. */ |
| 52 | enterpriseId: string; |
| 53 | /** Project id used when the page was reconciled. */ |
| 54 | project: string; |
| 55 | /** Localized names with the Alchemy ownership prefix stripped. */ |
| 56 | name: androidenterprise.LocalizedText[] | undefined; |
| 57 | /** Linked page ids. */ |
| 58 | link: string[] | undefined; |
| 59 | }, |
| 60 | never, |
| 61 | Providers |
| 62 | >; |
| 63 | |
| 64 | /** |
| 65 | * A managed Google Play store page (`storelayoutpages`). |
| 66 | * |
| 67 | * Store pages have no labels field and their names are shown to device |
| 68 | * users, so Alchemy tracks a page by its recorded id only (`list` finds |
| 69 | * pages from older releases that carried a name marker). `enterpriseId` and `pageId` |
| 70 | * are identity — changing either replaces the page. Names and links |
| 71 | * update in place. |
| 72 | * |
| 73 | * ### Creating a Store Page |
| 74 | * **Example:** Generated name |
| 75 | * ```typescript |
| 76 | * const page = yield* GCP.AndroidEnterprise.Storelayoutpage("Home", { |
| 77 | * enterpriseId: "123456789", |
| 78 | * }); |
| 79 | * ``` |
| 80 | * |
| 81 | * **Example:** Explicit localized name |
| 82 | * ```typescript |
| 83 | * const page = yield* GCP.AndroidEnterprise.Storelayoutpage("Home", { |
| 84 | * enterpriseId: "123456789", |
| 85 | * name: [{ locale: "en-US", text: "Home" }], |
| 86 | * }); |
| 87 | * ``` |
| 88 | * |
| 89 | * ### Updating a Store Page |
| 90 | * Re-declare the same logical id with changed props; the engine keeps the |
| 91 | * physical resource and updates it in place. |
| 92 | * |
| 93 | * **Example:** Rename |
| 94 | * ```typescript |
| 95 | * const page = yield* GCP.AndroidEnterprise.Storelayoutpage("Home", { |
| 96 | * enterpriseId: "123456789", |
| 97 | * name: [{ locale: "en-US", text: "Featured" }], |
| 98 | * }); |
| 99 | * ``` |
| 100 | * |
| 101 | * @resource |
| 102 | * @category AndroidEnterprise |
| 103 | */ |
| 104 | export const Storelayoutpage = Resource<Storelayoutpage>("GCP.AndroidEnterprise.Storelayoutpage"); |
| 105 | |
| 106 | export class StorelayoutpageNotResolved extends Data.TaggedError( |
| 107 | "GCP.AndroidEnterprise.StorelayoutpageNotResolved", |
| 108 | )<{ |
| 109 | enterpriseId: string; |
| 110 | pageId: string; |
| 111 | }> {} |
| 112 | |
| 113 | const toAttrs = (page: androidenterprise.StorePage, enterpriseId: string, project: string) => ({ |
| 114 | pageId: page.id ?? "", |
| 115 | enterpriseId, |
| 116 | project, |
| 117 | name: publicNames(page.name), |
| 118 | link: page.link, |
| 119 | }); |
| 120 | |
| 121 | const desiredBody = (input: { |
| 122 | pageId?: string; |
| 123 | name: androidenterprise.LocalizedText[]; |
| 124 | news: StorelayoutpageProps; |
| 125 | current?: androidenterprise.StorePage; |
| 126 | }): androidenterprise.StorePage => ({ |
| 127 | id: input.pageId, |
| 128 | name: input.name, |
| 129 | link: input.news.link ?? input.current?.link, |
| 130 | }); |
| 131 | |
| 132 | const needsSync = (current: androidenterprise.StorePage, desired: androidenterprise.StorePage) => |
| 133 | !jsonEqual(current.name, desired.name) || |
| 134 | (desired.link !== undefined && !sameStringList(current.link, desired.link)); |
| 135 | |
| 136 | export const StorelayoutpageProvider = () => |
| 137 | Provider.succeed(Storelayoutpage, { |
| 138 | stables: ["pageId", "enterpriseId", "project"], |
| 139 | |
| 140 | diff: Effect.fn(function* ({ news, olds, output }) { |
| 141 | if (!isResolved(news)) return undefined; |
| 142 | const previousEnterprise = olds?.enterpriseId ?? output?.enterpriseId; |
| 143 | if (previousEnterprise !== undefined && news.enterpriseId !== previousEnterprise) { |
| 144 | return { action: "replace" as const, deleteFirst: false }; |
| 145 | } |
| 146 | const previousId = olds?.pageId ?? output?.pageId; |
| 147 | if (previousId !== undefined && news.pageId !== undefined && news.pageId !== previousId) { |
| 148 | return { action: "replace" as const, deleteFirst: false }; |
| 149 | } |
| 150 | return undefined; |
| 151 | }), |
| 152 | |
| 153 | read: Effect.fn(function* ({ id, olds, output }) { |
| 154 | const env = yield* GcpEnvironment.current; |
| 155 | const enterpriseId = olds?.enterpriseId ?? output?.enterpriseId ?? ""; |
| 156 | const pageId = olds?.pageId ?? output?.pageId ?? ""; |
| 157 | let existing = yield* getPage(enterpriseId, pageId); |
| 158 | if (existing === undefined && enterpriseId.length > 0) { |
| 159 | existing = yield* findOwnedPage(id, enterpriseId); |
| 160 | } |
| 161 | if (existing === undefined) return undefined; |
| 162 | const attrs = toAttrs(existing, enterpriseId, env.project); |
| 163 | // Only a recorded id (or a legacy ownership marker) proves ownership; |
| 164 | // names and titles are user-visible and carry no marker. |
| 165 | return output !== undefined || |
| 166 | (yield* ownedByAlchemy(id, ownershipTextFromNames(existing.name))) |
| 167 | ? attrs |
| 168 | : Unowned(attrs); |
| 169 | }), |
| 170 | |
| 171 | list: () => |
| 172 | Effect.gen(function* () { |
| 173 | const env = yield* GcpEnvironment.current; |
| 174 | const pages = yield* listOwnedPages(); |
| 175 | return pages |
| 176 | .filter(({ page }) => hasOwnershipMarker(ownershipTextFromNames(page.name))) |
| 177 | .map(({ page, enterpriseId }) => toAttrs(page, enterpriseId, env.project)); |
| 178 | }), |
| 179 | |
| 180 | reconcile: Effect.fn(function* ({ id, news, output }) { |
| 181 | const env = yield* GcpEnvironment.current; |
| 182 | const enterpriseId = news.enterpriseId; |
| 183 | const displayName = yield* toDisplayName(id, news.name?.[0]?.text, output?.name?.[0]?.text); |
| 184 | const name = defaultNames(news.name, displayName); |
| 185 | |
| 186 | let current = yield* getPage(enterpriseId, news.pageId ?? output?.pageId ?? ""); |
| 187 | if (current === undefined) { |
| 188 | current = yield* findOwnedPage(id, enterpriseId); |
| 189 | } |
| 190 | |
| 191 | if (current === undefined) { |
| 192 | const created = yield* androidenterprise |
| 193 | .insertStorelayoutpages({ |
| 194 | enterpriseId, |
| 195 | body: desiredBody({ name, news }), |
| 196 | }) |
| 197 | .pipe(Effect.catchTag("Conflict", () => findOwnedPage(id, enterpriseId))); |
| 198 | current = created ?? undefined; |
| 199 | } |
| 200 | |
| 201 | if (current === undefined) { |
| 202 | return yield* new StorelayoutpageNotResolved({ |
| 203 | enterpriseId, |
| 204 | pageId: news.pageId ?? output?.pageId ?? displayName, |
| 205 | }); |
| 206 | } |
| 207 | |
| 208 | const pageId = current.id ?? news.pageId ?? output?.pageId ?? ""; |
| 209 | const desired = desiredBody({ |
| 210 | pageId, |
| 211 | name, |
| 212 | news, |
| 213 | current, |
| 214 | }); |
| 215 | if (needsSync(current, desired)) { |
| 216 | current = yield* androidenterprise.updateStorelayoutpages({ |
| 217 | enterpriseId, |
| 218 | pageId, |
| 219 | body: desired, |
| 220 | }); |
| 221 | } |
| 222 | |
| 223 | return toAttrs(current, enterpriseId, env.project); |
| 224 | }), |
| 225 | |
| 226 | delete: Effect.fn(function* ({ output }) { |
| 227 | if (!output.enterpriseId || !output.pageId) return; |
| 228 | yield* androidenterprise |
| 229 | .deleteStorelayoutpages({ |
| 230 | enterpriseId: output.enterpriseId, |
| 231 | pageId: output.pageId, |
| 232 | }) |
| 233 | .pipe( |
| 234 | Effect.catchTags({ |
| 235 | NotFound: () => Effect.void, |
| 236 | BadRequest: () => Effect.void, |
| 237 | Conflict: () => Effect.void, |
| 238 | }), |
| 239 | ); |
| 240 | }), |
| 241 | }); |