@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, 04:45 UTC

main
ts 46 lines 1.96 KB
Raw
1 import {
2 type GetServiceDeploymentsResponse,
3 getServiceDeployments,
4 } from "@distilled.cloud/prisma/management";
5 import * as Data from "effect/Data";
6 import * as Effect from "effect/Effect";
7 import { PrismaPaginationError } from "./Pagination.ts";
8
9 export class DeploymentIdentityConflict extends Data.TaggedError("DeploymentIdentityConflict")<{
10 readonly message: string;
11 readonly cause?: unknown;
12 }> {}
13
14 /** Prove that a deployment belongs to an App before mutating or deleting it. */
15 export const ensureDeploymentMembership = Effect.fn(function* (
16 appId: string,
17 deployment: { id: string; foundryVersionId: string },
18 knownLatestDeploymentId?: string | null,
19 ) {
20 if (knownLatestDeploymentId === deployment.id) return;
21 // Distilled emits the cursor-paginated list operations as plain ops, so
22 // callers walk `pagination` themselves (see `src/Neon/Project.ts`).
23 const deployments: GetServiceDeploymentsResponse["data"][number][] = [];
24 let cursor: string | undefined;
25 while (true) {
26 const page = yield* getServiceDeployments(
27 cursor === undefined ? { serviceId: appId } : { serviceId: appId, cursor },
28 );
29 deployments.push(...page.data);
30 const nextCursor = page.pagination.nextCursor;
31 if (!page.pagination.hasMore) break;
32 if (nextCursor === null) {
33 return yield* new PrismaPaginationError({
34 message:
35 "Invalid Prisma Management API pagination response from getServiceDeployments: hasMore was true without a non-empty nextCursor",
36 });
37 }
38 cursor = nextCursor;
39 }
40 const matches = deployments.filter((candidate) => candidate.id === deployment.id);
41 if (matches.length !== 1 || matches[0]?.foundryVersionId !== deployment.foundryVersionId) {
42 return yield* new DeploymentIdentityConflict({
43 message: `Prisma deployment '${deployment.id}' with Foundry version '${deployment.foundryVersionId}' is not uniquely owned by App '${appId}'. Refusing to start, promote, or delete a mismatched deployment.`,
44 });
45 }
46 });