|
| 1 | +import { NextResponse } from "next/server"; |
| 2 | +import httpStatus from "http-status"; |
| 3 | +import { z } from "zod"; |
| 4 | + |
| 5 | +import { buildConflictUpdateColumns, eq, takeFirst } from "@ctrlplane/db"; |
| 6 | +import * as SCHEMA from "@ctrlplane/db/schema"; |
| 7 | +import { |
| 8 | + cancelOldReleaseJobTriggersOnJobDispatch, |
| 9 | + createJobApprovals, |
| 10 | + createReleaseJobTriggers, |
| 11 | + dispatchReleaseJobTriggers, |
| 12 | + isPassingAllPolicies, |
| 13 | + isPassingChannelSelectorPolicy, |
| 14 | +} from "@ctrlplane/job-dispatch"; |
| 15 | +import { logger } from "@ctrlplane/logger"; |
| 16 | +import { Permission } from "@ctrlplane/validators/auth"; |
| 17 | + |
| 18 | +import { authn, authz } from "../../auth"; |
| 19 | +import { parseBody } from "../../body-parser"; |
| 20 | +import { request } from "../../middleware"; |
| 21 | + |
| 22 | +const patchSchema = SCHEMA.updateDeploymentVersion.and( |
| 23 | + z.object({ metadata: z.record(z.string()).optional() }), |
| 24 | +); |
| 25 | + |
| 26 | +export const PATCH = request() |
| 27 | + .use(authn) |
| 28 | + .use(parseBody(patchSchema)) |
| 29 | + .use( |
| 30 | + authz(({ can, extra: { params } }) => |
| 31 | + can |
| 32 | + .perform(Permission.DeploymentVersionUpdate) |
| 33 | + .on({ type: "deploymentVersion", id: params.deploymentVersionId }), |
| 34 | + ), |
| 35 | + ) |
| 36 | + .handle< |
| 37 | + { body: z.infer<typeof patchSchema>; user: SCHEMA.User }, |
| 38 | + { params: { deploymentVersionId: string } } |
| 39 | + >(async (ctx, { params }) => { |
| 40 | + const { deploymentVersionId } = params; |
| 41 | + const { body, user, req } = ctx; |
| 42 | + |
| 43 | + try { |
| 44 | + const deploymentVersion = await ctx.db |
| 45 | + .update(SCHEMA.deploymentVersion) |
| 46 | + .set(body) |
| 47 | + .where(eq(SCHEMA.deploymentVersion.id, deploymentVersionId)) |
| 48 | + .returning() |
| 49 | + .then(takeFirst); |
| 50 | + |
| 51 | + if (Object.keys(body.metadata ?? {}).length > 0) |
| 52 | + await ctx.db |
| 53 | + .insert(SCHEMA.deploymentVersionMetadata) |
| 54 | + .values( |
| 55 | + Object.entries(body.metadata ?? {}).map(([key, value]) => ({ |
| 56 | + versionId: deploymentVersionId, |
| 57 | + key, |
| 58 | + value, |
| 59 | + })), |
| 60 | + ) |
| 61 | + .onConflictDoUpdate({ |
| 62 | + target: [ |
| 63 | + SCHEMA.deploymentVersionMetadata.key, |
| 64 | + SCHEMA.deploymentVersionMetadata.versionId, |
| 65 | + ], |
| 66 | + set: buildConflictUpdateColumns(SCHEMA.deploymentVersionMetadata, [ |
| 67 | + "value", |
| 68 | + ]), |
| 69 | + }); |
| 70 | + |
| 71 | + await createReleaseJobTriggers(ctx.db, "version_updated") |
| 72 | + .causedById(user.id) |
| 73 | + .filter(isPassingChannelSelectorPolicy) |
| 74 | + .versions([deploymentVersionId]) |
| 75 | + .then(createJobApprovals) |
| 76 | + .insert() |
| 77 | + .then((releaseJobTriggers) => { |
| 78 | + dispatchReleaseJobTriggers(ctx.db) |
| 79 | + .releaseTriggers(releaseJobTriggers) |
| 80 | + .filter(isPassingAllPolicies) |
| 81 | + .then(cancelOldReleaseJobTriggersOnJobDispatch) |
| 82 | + .dispatch(); |
| 83 | + }) |
| 84 | + .then(() => |
| 85 | + logger.info( |
| 86 | + `Version for ${deploymentVersionId} job triggers created and dispatched.`, |
| 87 | + req, |
| 88 | + ), |
| 89 | + ); |
| 90 | + |
| 91 | + return NextResponse.json(deploymentVersion); |
| 92 | + } catch (error) { |
| 93 | + logger.error(error); |
| 94 | + return NextResponse.json( |
| 95 | + { error: "Failed to update version" }, |
| 96 | + { status: httpStatus.INTERNAL_SERVER_ERROR }, |
| 97 | + ); |
| 98 | + } |
| 99 | + }); |
0 commit comments