Skip to content

Commit

Permalink
always return id on 409
Browse files Browse the repository at this point in the history
  • Loading branch information
jsbroks committed Nov 13, 2024
1 parent 6e242d8 commit 5ebc8c8
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export const POST = request()

if (releaseChannel)
return NextResponse.json(
{ error: "Release channel already exists" },
{ error: "Release channel already exists", id: releaseChannel.id },
{ status: 409 },
);

Expand Down
78 changes: 44 additions & 34 deletions apps/webservice/src/app/api/v1/environments/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ import { NextResponse } from "next/server";
import { isPresent } from "ts-is-present";
import { z } from "zod";

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

import { authn, authz } from "../auth";
Expand Down Expand Up @@ -45,8 +46,8 @@ export const POST = request()
),
)
.handle<{ user: User; can: PermissionChecker; body: z.infer<typeof body> }>(
(ctx) =>
ctx.db
async (ctx) => {
const environment = await ctx.db
.insert(schema.environment)
.values({
...ctx.body,
Expand All @@ -55,36 +56,45 @@ export const POST = request()
: undefined,
})
.returning()
.then(takeFirst)
.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) => {
if (
error.code === "23505" &&
error.constraint === "environment_system_id_name_key"
) {
return NextResponse.json(
{
error:
"An environment with this name already exists in this system.",
},
{ status: 409 },
);
}
return NextResponse.json(
{ error: "Failed to create environment" },
{ status: 500 },
.then(takeFirstOrNull);

if (environment)
return NextResponse.json(
{ error: "Environment already exists", id: environment.id },
{ status: 409 },
);

try {
const environment = await ctx.db
.insert(schema.environment)
.values({
...ctx.body,
expiresAt: isPresent(ctx.body.expiresAt)
? new Date(ctx.body.expiresAt)
: undefined,
})
.returning()
.then(takeFirst);

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) {
logger.error("Failed to create environment", { error });
return NextResponse.json(
{ error: "Failed to create environment" },
{ status: 500 },
);
}
},
);
2 changes: 1 addition & 1 deletion apps/webservice/src/app/api/v1/release-channels/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export const POST = request()

if (releaseChannel)
return NextResponse.json(
{ error: "Release channel already exists" },
{ error: "Release channel already exists", id: releaseChannel.id },
{ status: 409 },
);

Expand Down
2 changes: 1 addition & 1 deletion apps/webservice/src/app/api/v1/releases/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export const POST = request()

if (existingRelease)
return NextResponse.json(
{ error: "Release already exists" },
{ error: "Release already exists", releaseId: existingRelease.id },
{ status: 409 },
);

Expand Down

0 comments on commit 5ebc8c8

Please sign in to comment.