Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 28 additions & 1 deletion apps/webservice/src/app/api/v1/environments/route.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import type { PermissionChecker } from "@ctrlplane/auth/utils";
import type { Tx } from "@ctrlplane/db";
import type { User } from "@ctrlplane/db/schema";
import { NextResponse } from "next/server";
import { isPresent } from "ts-is-present";
import { z } from "zod";

import { takeFirst } from "@ctrlplane/db";
import * as schema from "@ctrlplane/db/schema";
import { createJobsForNewEnvironment } from "@ctrlplane/job-dispatch";
import { Permission } from "@ctrlplane/validators/auth";

import { authn, authz } from "../auth";
Expand All @@ -19,6 +21,19 @@ const body = schema.createEnvironment.extend({
.optional(),
});

const createReleaseChannels = (
db: Tx,
environmentId: string,
releaseChannels: { channelId: string; deploymentId: string }[],
) =>
db.insert(schema.environmentReleaseChannel).values(
releaseChannels.map(({ channelId, deploymentId }) => ({
environmentId,
channelId,
deploymentId,
})),
);

export const POST = request()
.use(authn)
.use(parseBody(body))
Expand All @@ -41,7 +56,19 @@ export const POST = request()
})
.returning()
.then(takeFirst)
.then((environment) => NextResponse.json({ environment }))
.then(async (environment) => {
if (
isPresent(ctx.body.releaseChannels) &&
ctx.body.releaseChannels.length > 0
)
await createReleaseChannels(
ctx.db,
environment.id,
ctx.body.releaseChannels,
);
await createJobsForNewEnvironment(ctx.db, environment);
return NextResponse.json({ environment });
})
Comment on lines +59 to +71
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Improve error handling specificity and transaction rollback.

The error handling could be more specific to help diagnose issues:

  1. Different error messages for release channel creation vs job creation failures
  2. Ensure transaction rollback on failure

Consider this enhanced implementation:

         .then(async (environment) => {
           if (
             isPresent(ctx.body.releaseChannels) &&
             ctx.body.releaseChannels.length > 0
-          )
+          ) {
             await createReleaseChannels(
               ctx.db,
               environment.id,
               ctx.body.releaseChannels,
             );
+          }
           await createJobsForNewEnvironment(ctx.db, environment);
           return NextResponse.json({ environment });
         })
-        .catch(() =>
+        .catch((error) =>
           NextResponse.json(
-            { error: "Failed to create environment" },
+            { 
+              error: "Failed to create environment",
+              details: error.message 
+            },
             { status: 500 },
           ),
         ),
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
.then(async (environment) => {
if (
isPresent(ctx.body.releaseChannels) &&
ctx.body.releaseChannels.length > 0
)
await createReleaseChannels(
ctx.db,
environment.id,
ctx.body.releaseChannels,
);
await createJobsForNewEnvironment(ctx.db, environment);
return NextResponse.json({ environment });
})
.then(async (environment) => {
if (
isPresent(ctx.body.releaseChannels) &&
ctx.body.releaseChannels.length > 0
) {
await createReleaseChannels(
ctx.db,
environment.id,
ctx.body.releaseChannels,
);
}
await createJobsForNewEnvironment(ctx.db, environment);
return NextResponse.json({ environment });
})
.catch((error) =>
NextResponse.json(
{
error: "Failed to create environment",
details: error.message
},
{ status: 500 },
),
),

.catch(() =>
NextResponse.json(
{ error: "Failed to create environment" },
Expand Down
1 change: 1 addition & 0 deletions packages/db/drizzle/0030_easy_alex_wilder.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TYPE "release_job_trigger_type" ADD VALUE 'new_environment';
Loading