From 5ebc8c8c55aeb8cf34d9db0b5ab690585d46a29f Mon Sep 17 00:00:00 2001 From: Justin Brooks Date: Tue, 12 Nov 2024 19:35:59 -0500 Subject: [PATCH] always return id on 409 --- .../[deploymentId]/release-channels/route.ts | 2 +- .../src/app/api/v1/environments/route.ts | 78 +++++++++++-------- .../src/app/api/v1/release-channels/route.ts | 2 +- .../src/app/api/v1/releases/route.ts | 2 +- 4 files changed, 47 insertions(+), 37 deletions(-) diff --git a/apps/webservice/src/app/api/v1/deployments/[deploymentId]/release-channels/route.ts b/apps/webservice/src/app/api/v1/deployments/[deploymentId]/release-channels/route.ts index 4c010a28..449d8f3e 100644 --- a/apps/webservice/src/app/api/v1/deployments/[deploymentId]/release-channels/route.ts +++ b/apps/webservice/src/app/api/v1/deployments/[deploymentId]/release-channels/route.ts @@ -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 }, ); diff --git a/apps/webservice/src/app/api/v1/environments/route.ts b/apps/webservice/src/app/api/v1/environments/route.ts index aadd5554..78deeaa3 100644 --- a/apps/webservice/src/app/api/v1/environments/route.ts +++ b/apps/webservice/src/app/api/v1/environments/route.ts @@ -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"; @@ -45,8 +46,8 @@ export const POST = request() ), ) .handle<{ user: User; can: PermissionChecker; body: z.infer }>( - (ctx) => - ctx.db + async (ctx) => { + const environment = await ctx.db .insert(schema.environment) .values({ ...ctx.body, @@ -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 }, + ); + } + }, ); diff --git a/apps/webservice/src/app/api/v1/release-channels/route.ts b/apps/webservice/src/app/api/v1/release-channels/route.ts index d08b806d..5df28d0a 100644 --- a/apps/webservice/src/app/api/v1/release-channels/route.ts +++ b/apps/webservice/src/app/api/v1/release-channels/route.ts @@ -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 }, ); diff --git a/apps/webservice/src/app/api/v1/releases/route.ts b/apps/webservice/src/app/api/v1/releases/route.ts index 209d2e2b..14631ab2 100644 --- a/apps/webservice/src/app/api/v1/releases/route.ts +++ b/apps/webservice/src/app/api/v1/releases/route.ts @@ -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 }, );