@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 133 lines 4.43 KB
Raw
1 import * as licensing from "@distilled.cloud/gcp/licensing_v1";
2 import * as Config from "effect/Config";
3 import * as Effect from "effect/Effect";
4 import * as Option from "effect/Option";
5 import * as Stream from "effect/Stream";
6
7 export const DEFAULT_PRODUCT_ID = "Google-Apps";
8 export const DEFAULT_CUSTOMER_ID = "my_customer";
9
10 export const sameText = (left: string | undefined, right: string | undefined) =>
11 (left ?? "") === (right ?? "");
12
13 export const sameUser = (left: string | undefined, right: string | undefined) =>
14 (left ?? "").trim().toLowerCase() === (right ?? "").trim().toLowerCase();
15
16 export const replaceOnIdentity = (input: {
17 previousProductId?: string;
18 nextProductId?: string;
19 previousUserId?: string;
20 nextUserId?: string;
21 }) => {
22 if (
23 input.previousProductId !== undefined &&
24 input.nextProductId !== undefined &&
25 !sameText(input.previousProductId, input.nextProductId)
26 ) {
27 return { action: "replace" as const, deleteFirst: true };
28 }
29 if (
30 input.previousUserId !== undefined &&
31 input.nextUserId !== undefined &&
32 !sameUser(input.previousUserId, input.nextUserId)
33 ) {
34 return { action: "replace" as const, deleteFirst: true };
35 }
36 return undefined;
37 };
38
39 export const normalizeCustomerId = (value: string | undefined) => {
40 const trimmed = value?.trim();
41 if (trimmed === undefined || trimmed.length === 0) {
42 return undefined;
43 }
44 return trimmed.startsWith("customers/") ? trimmed.slice("customers/".length) : trimmed;
45 };
46
47 /** Optional setting from the ConfigProvider (env by default). */
48 const optionalSetting = (key: string) =>
49 Effect.gen(function* () {
50 const value = Option.getOrUndefined(yield* Config.option(Config.String(key)))?.trim();
51 return value !== undefined && value.length > 0 ? value : undefined;
52 });
53
54 /** Customer for `list` and the `customerId` default: `GOOGLE_LICENSE_CUSTOMER_ID`. */
55 export const listCustomerId = () =>
56 Effect.gen(function* () {
57 return normalizeCustomerId(
58 (yield* optionalSetting("GOOGLE_LICENSE_CUSTOMER_ID")) ??
59 (yield* optionalSetting("GOOGLE_WORKSPACE_CUSTOMER_ID")),
60 );
61 });
62
63 /** Product swept by `list`: `GOOGLE_LICENSE_PRODUCT_ID`. */
64 export const listProductId = () => optionalSetting("GOOGLE_LICENSE_PRODUCT_ID");
65
66 /** SKU swept by `list`: `GOOGLE_LICENSE_SKU_ID`. */
67 export const listSkuId = () => optionalSetting("GOOGLE_LICENSE_SKU_ID");
68
69 /** User whose assignments `list` returns: `GOOGLE_LICENSE_USER_ID`. */
70 export const listUserId = () => optionalSetting("GOOGLE_LICENSE_USER_ID");
71
72 const emptyList = <A>() => Effect.succeed([] as A[]);
73
74 export const getAssignment = (productId: string, skuId: string, userId: string) =>
75 productId.length === 0 || skuId.length === 0 || userId.length === 0
76 ? Effect.undefined
77 : licensing
78 .getLicenseAssignments({
79 productId,
80 skuId,
81 userId,
82 })
83 .pipe(Effect.catchTag("NotFound", () => Effect.undefined));
84
85 export const listAssignments = (productId: string, customerId: string, skuId?: string) => {
86 if (productId.length === 0 || customerId.length === 0) {
87 return emptyList<licensing.LicenseAssignment>();
88 }
89 const pages =
90 skuId !== undefined && skuId.length > 0
91 ? licensing.listForProductAndSkuLicenseAssignments.pages({
92 productId,
93 skuId,
94 customerId,
95 maxResults: 100,
96 })
97 : licensing.listForProductLicenseAssignments.pages({
98 productId,
99 customerId,
100 maxResults: 100,
101 });
102 return pages.pipe(
103 Stream.flatMap((page) => Stream.fromIterable(page.items ?? [])),
104 Stream.runCollect,
105 Effect.map((chunk) => Array.from(chunk)),
106 Effect.catchTag("NotFound", () => emptyList<licensing.LicenseAssignment>()),
107 );
108 };
109
110 export const findAssignment = (
111 productId: string,
112 userId: string,
113 customerId: string | undefined,
114 skuId?: string,
115 ) =>
116 Effect.gen(function* () {
117 if (customerId === undefined || customerId.length === 0) {
118 return undefined;
119 }
120 const items = yield* listAssignments(productId, customerId, skuId);
121 return items.find((item) => sameUser(item.userId, userId));
122 });
123
124 export const deleteAssignment = (productId: string, skuId: string, userId: string) =>
125 productId.length === 0 || skuId.length === 0 || userId.length === 0
126 ? Effect.void
127 : licensing
128 .deleteLicenseAssignments({
129 productId,
130 skuId,
131 userId,
132 })
133 .pipe(Effect.catchTag("NotFound", () => Effect.void));