Skip to content

Commit

Permalink
add approval section
Browse files Browse the repository at this point in the history
  • Loading branch information
zacharyblasczyk committed Oct 27, 2024
1 parent b5c9751 commit 3cf34e6
Showing 1 changed file with 46 additions and 2 deletions.
48 changes: 46 additions & 2 deletions apps/webservice/src/app/api/v1/jobs/[jobId]/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { db } from "@ctrlplane/db/client";
import {
deployment,
environment,
environmentPolicyApproval,
job,
jobVariable,
release,
Expand All @@ -15,6 +16,7 @@ import {
target,
targetMetadata,
updateJob,
user,
} from "@ctrlplane/db/schema";
import { onJobCompletion } from "@ctrlplane/job-dispatch";
import { variablesAES256 } from "@ctrlplane/secrets";
Expand All @@ -26,8 +28,8 @@ export const GET = async (
req: NextRequest,
{ params }: { params: { jobId: string } },
) => {
const user = await getUser(req);
if (!user)
const caller = await getUser(req);
if (!caller)
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });

const je = await db
Expand All @@ -40,6 +42,7 @@ export const GET = async (
.leftJoin(target, eq(target.id, releaseJobTrigger.targetId))
.leftJoin(release, eq(release.id, releaseJobTrigger.releaseId))
.leftJoin(deployment, eq(deployment.id, release.deploymentId))
.leftJoin(user, eq(releaseJobTrigger.causedById, user.id))
.where(and(eq(job.id, params.jobId), isNull(environment.deletedAt)))
.then(takeFirst)
.then((row) => ({
Expand All @@ -49,8 +52,48 @@ export const GET = async (
target: row.target,
deployment: row.deployment,
release: row.release,
causedBy: row.user
? {
id: row.user.id,
name: row.user.name,
email: row.user.email,
}
: null,
}));

const policyId = je.environment?.policyId;
const approval =
je.release?.id && policyId
? await db
.select()
.from(environmentPolicyApproval)
.leftJoin(user, eq(environmentPolicyApproval.userId, user.id))
.where(
and(
eq(environmentPolicyApproval.releaseId, je.release.id),
eq(environmentPolicyApproval.policyId, policyId),
),
)
.then(takeFirstOrNull)
.then((row) =>
row
? {
id: row.environment_policy_approval.id,
status: row.environment_policy_approval.status,
approver:
row.user &&
row.environment_policy_approval.status !== "pending"
? {
id: row.user.id,
name: row.user.name,
email: row.user.email,
}
: null,
}
: null,
)
: null;

const jobVariableRows = await db
.select()
.from(jobVariable)
Expand Down Expand Up @@ -80,6 +123,7 @@ export const GET = async (
...je,
variables,
target: targetWithMetadata,
approval,
});
};

Expand Down

0 comments on commit 3cf34e6

Please sign in to comment.