The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as marketplace from "@distilled.cloud/gcp/authorizedbuyersmarketplace_v1"; |
| 2 | import { createPhysicalName } from "alchemy/PhysicalName"; |
| 3 | import * as Effect from "effect/Effect"; |
| 4 | import * as Stream from "effect/Stream"; |
| 5 | |
| 6 | export const PROBE_PARENT = "buyers/1/clients/1"; |
| 7 | export const PROBE_NAME = `${PROBE_PARENT}/users/0`; |
| 8 | |
| 9 | export type ClientUserState = marketplace.ClientUserStateEnum; |
| 10 | |
| 11 | export const lastSegment = (value: string) => { |
| 12 | const trimmed = value.replace(/\/+$/, ""); |
| 13 | const parts = trimmed.split("/"); |
| 14 | return parts[parts.length - 1] || trimmed; |
| 15 | }; |
| 16 | |
| 17 | export const normalizeResourceName = (value: string) => value.replace(/\/+$/, "").trim(); |
| 18 | |
| 19 | export const expandParent = (value: string) => { |
| 20 | const trimmed = normalizeResourceName(value); |
| 21 | if (trimmed.length === 0) return trimmed; |
| 22 | if (trimmed.includes("/clients/")) { |
| 23 | return trimmed.startsWith("buyers/") ? trimmed : `buyers/${trimmed}`; |
| 24 | } |
| 25 | return trimmed; |
| 26 | }; |
| 27 | |
| 28 | export const parentOfName = (name: string) => { |
| 29 | const parts = name.split("/").filter((part) => part.length > 0); |
| 30 | const usersIndex = parts.lastIndexOf("users"); |
| 31 | if (usersIndex > 0) return parts.slice(0, usersIndex).join("/"); |
| 32 | const clientUsersIndex = parts.lastIndexOf("clientUsers"); |
| 33 | if (clientUsersIndex > 0) return parts.slice(0, clientUsersIndex).join("/"); |
| 34 | return ""; |
| 35 | }; |
| 36 | |
| 37 | export const resourceName = (parent: string, userId: string) => |
| 38 | `${expandParent(parent)}/users/${userId}`; |
| 39 | |
| 40 | export const userIdOf = (name: string) => lastSegment(name); |
| 41 | |
| 42 | export const sameText = (left: string | undefined, right: string | undefined) => |
| 43 | (left ?? "") === (right ?? ""); |
| 44 | |
| 45 | export const normalizeEmail = (email: string | undefined) => (email ?? "").trim().toLowerCase(); |
| 46 | |
| 47 | export const replaceOnIdentity = (input: { |
| 48 | previousParent?: string; |
| 49 | nextParent: string; |
| 50 | previousEmail?: string; |
| 51 | nextEmail?: string; |
| 52 | previousUserId?: string; |
| 53 | nextUserId?: string; |
| 54 | }) => { |
| 55 | if ( |
| 56 | input.previousParent !== undefined && |
| 57 | input.previousParent.length > 0 && |
| 58 | input.previousParent !== input.nextParent |
| 59 | ) { |
| 60 | return { action: "replace" as const, deleteFirst: false }; |
| 61 | } |
| 62 | if ( |
| 63 | input.previousEmail !== undefined && |
| 64 | input.nextEmail !== undefined && |
| 65 | normalizeEmail(input.previousEmail) !== normalizeEmail(input.nextEmail) |
| 66 | ) { |
| 67 | return { action: "replace" as const, deleteFirst: false }; |
| 68 | } |
| 69 | if ( |
| 70 | input.previousUserId !== undefined && |
| 71 | input.nextUserId !== undefined && |
| 72 | input.previousUserId !== input.nextUserId |
| 73 | ) { |
| 74 | return { action: "replace" as const, deleteFirst: true }; |
| 75 | } |
| 76 | return undefined; |
| 77 | }; |
| 78 | |
| 79 | export const toEmail = (id: string, requested: string | undefined, existing: string | undefined) => |
| 80 | Effect.gen(function* () { |
| 81 | if (requested !== undefined && requested.length > 0) return requested; |
| 82 | if (existing !== undefined && existing.length > 0) return existing; |
| 83 | const generated = yield* createPhysicalName({ |
| 84 | id, |
| 85 | maxLength: 32, |
| 86 | lowercase: true, |
| 87 | }); |
| 88 | const local = generated.replace(/[^a-z0-9]/g, "").slice(0, 32) || "alchemy"; |
| 89 | return `${local}@example.com`; |
| 90 | }); |
| 91 | |
| 92 | export type ClientUserAttrs = { |
| 93 | name: string; |
| 94 | userId: string; |
| 95 | parent: string; |
| 96 | project: string; |
| 97 | email: string; |
| 98 | state: string | undefined; |
| 99 | }; |
| 100 | |
| 101 | export const toAttrs = ( |
| 102 | user: marketplace.ClientUser, |
| 103 | parent: string, |
| 104 | project: string, |
| 105 | ): ClientUserAttrs => { |
| 106 | const name = user.name ?? ""; |
| 107 | return { |
| 108 | name, |
| 109 | userId: userIdOf(name), |
| 110 | parent: parentOfName(name) || parent, |
| 111 | project, |
| 112 | email: user.email ?? "", |
| 113 | state: user.state, |
| 114 | }; |
| 115 | }; |
| 116 | |
| 117 | /** Email is unique per client, so it identifies the user. */ |
| 118 | export const findUserByEmail = (users: readonly marketplace.ClientUser[], email: string) => |
| 119 | users.find((user) => normalizeEmail(user.email) === normalizeEmail(email)); |
| 120 | |
| 121 | export const listUsers = (parent: string) => |
| 122 | marketplace.listBuyersClientsUsers.pages({ parent, pageSize: 200 }).pipe( |
| 123 | Stream.flatMap((page) => Stream.fromIterable(page.clientUsers ?? [])), |
| 124 | Stream.runCollect, |
| 125 | Effect.map((chunk) => Array.from(chunk)), |
| 126 | ); |