| 1 | import * as marketplace from "@distilled.cloud/gcp/authorizedbuyersmarketplace_v1"; |
| 2 | import * as Effect from "effect/Effect"; |
| 3 | import { MinimumLogLevel } from "effect/References"; |
| 4 | import * as Schedule from "effect/Schedule"; |
| 5 | |
| 6 | export const logLevel = Effect.provideService( |
| 7 | MinimumLogLevel, |
| 8 | process.env.DEBUG ? "Debug" : "Info", |
| 9 | ); |
| 10 | |
| 11 | export const probeParent = "buyers/1/clients/1"; |
| 12 | export const probeName = `${probeParent}/users/0`; |
| 13 | |
| 14 | const expandParent = (value: string) => { |
| 15 | const trimmed = value.replace(/\/+$/, "").trim(); |
| 16 | if (trimmed.length === 0) return trimmed; |
| 17 | if (trimmed.includes("/clients/")) { |
| 18 | return trimmed.startsWith("buyers/") ? trimmed : `buyers/${trimmed}`; |
| 19 | } |
| 20 | return trimmed; |
| 21 | }; |
| 22 | |
| 23 | const envParent = process.env.GCP_AUTHORIZEDBUYERSMARKETPLACE_PARENT?.trim(); |
| 24 | const envBuyer = process.env.GCP_AUTHORIZEDBUYERSMARKETPLACE_BUYER_ID?.trim(); |
| 25 | const envClient = process.env.GCP_AUTHORIZEDBUYERSMARKETPLACE_CLIENT_ID?.trim(); |
| 26 | |
| 27 | // Full lifecycle needs an Authorized Buyers account with a client; set |
| 28 | // GCP_AUTHORIZEDBUYERSMARKETPLACE_PARENT (or _BUYER_ID + _CLIENT_ID). |
| 29 | export const lifecycleParent = envParent |
| 30 | ? expandParent(envParent) |
| 31 | : envBuyer && envClient |
| 32 | ? expandParent(`${envBuyer}/clients/${envClient}`) |
| 33 | : undefined; |
| 34 | |
| 35 | export const waitUntilGone = (name: string) => |
| 36 | marketplace.getBuyersClientsUsers({ name }).pipe( |
| 37 | Effect.as("found" as const), |
| 38 | Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)), |
| 39 | Effect.repeat({ |
| 40 | schedule: Schedule.spaced("1 second"), |
| 41 | until: (status) => status === "gone", |
| 42 | times: 10, |
| 43 | }), |
| 44 | ); |