1import * as ccaip from "@distilled.cloud/gcp/contactcenteraiplatform_v1alpha1";
2import * as Effect from "effect/Effect";
3import {
4 waitForOperation as waitForLongRunning,
5 type LongRunningOperation,
6} from "../Operation.ts";
7
8/** Contact center instance provisioning takes 30–45 minutes. */
9const OPERATION_BUDGET = "60 minutes";
10
11/**
12 * Wait for a long-running operation. ALREADY_EXISTS (code 6) means a
13 * concurrent create won the race; reconcile observes the resource next.
14 */
15export const waitForOperation = (operation: LongRunningOperation) =>
16 waitForLongRunning(
17 operation,
18 (name) => ccaip.getProjectsLocationsOperations({ name }),
19 {
20 budget: OPERATION_BUDGET,
21 },
22 ).pipe(
23 Effect.catchIf(
24 (error) => error._tag === "GCP.OperationFailed" && error.code === 6,
25 () => Effect.succeed(operation),
26 ),
27 );
28
29/**
30 * Wait for a delete operation. A vanished operation or NOT_FOUND (code 5)
31 * means the resource is already gone.
32 */
33export const waitForDeleteOperation = (operation: LongRunningOperation) =>
34 waitForOperation(operation).pipe(
35 Effect.catchIf(
36 (error) =>
37 error._tag === "NotFound" ||
38 (error._tag === "GCP.OperationFailed" && error.code === 5),
39 () => Effect.succeed(operation),
40 ),
41 );