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
59 changes: 17 additions & 42 deletions apps/webservice/src/app/api/v1/environments/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { NextResponse } from "next/server";
import _ from "lodash";
import { z } from "zod";

import { and, createEnv, eq, inArray } from "@ctrlplane/db";
import { inArray, upsertEnv } from "@ctrlplane/db";
import * as schema from "@ctrlplane/db/schema";
import { createJobsForNewEnvironment } from "@ctrlplane/job-dispatch";
import { logger } from "@ctrlplane/logger";
Expand All @@ -16,10 +16,6 @@ import { request } from "../middleware";

const body = schema.createEnvironment.extend({
releaseChannels: z.array(z.string()),
expiresAt: z.coerce
.date()
.min(new Date(), "Expires at must be in the future")
.optional(),
});

export const POST = request()
Expand All @@ -33,35 +29,14 @@ export const POST = request()
),
)
.handle<{ user: User; can: PermissionChecker; body: z.infer<typeof body> }>(
async (ctx) => {
const isInSystem = eq(schema.environment.systemId, ctx.body.systemId);
const isSameName = eq(schema.environment.name, ctx.body.name);
const existingEnvironment = await ctx.db.query.environment.findFirst({
where: and(isInSystem, isSameName),
});

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

try {
return ctx.db.transaction(async (tx) => {
const {
releaseChannels: deploymentVersionChannels,
metadata,
...rest
} = ctx.body;

({ db, body }) =>
db.transaction(async (tx) => {
try {
const channels = await tx
.select()
.from(schema.deploymentVersionChannel)
.where(
inArray(
schema.deploymentVersionChannel.id,
deploymentVersionChannels,
),
inArray(schema.deploymentVersionChannel.id, body.releaseChannels),
)
.then((rows) =>
_.uniqBy(rows, (r) => r.deploymentId).map((r) => ({
Expand All @@ -70,21 +45,21 @@ export const POST = request()
})),
);

const environment = await createEnv(tx, {
...rest,
metadata,
const environment = await upsertEnv(tx, {
...body,
versionChannels: channels,
});

await createJobsForNewEnvironment(tx, environment);
const { metadata } = body;
return NextResponse.json({ ...environment, metadata });
});
} catch (error) {
logger.error("Failed to create environment", { error });
return NextResponse.json(
{ error: "Failed to create environment" },
{ status: 500 },
);
}
},
} catch (e) {
const error = e instanceof Error ? e.message : e;
logger.error("Failed to create environment", { error });
return NextResponse.json(
{ error: "Failed to create environment" },
{ status: 500 },
);
}
}),
);
4 changes: 2 additions & 2 deletions packages/api/src/router/environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ import { z } from "zod";

import {
and,
createEnv,
eq,
inArray,
isNotNull,
isNull,
ne,
not,
takeFirst,
upsertEnv,
} from "@ctrlplane/db";
import {
createEnvironment,
Expand Down Expand Up @@ -213,7 +213,7 @@ export const environmentRouter = createTRPCRouter({
})
.input(createEnvironment)
.mutation(({ ctx, input }) =>
ctx.db.transaction((db) => createEnv(db, input)),
ctx.db.transaction((db) => upsertEnv(db, input)),
),

update: protectedProcedure
Expand Down
8 changes: 4 additions & 4 deletions packages/api/src/router/system.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ import {
and,
asc,
count,
createEnv,
eq,
ilike,
isNotNull,
isNull,
or,
takeFirst,
takeFirstOrNull,
upsertEnv,
} from "@ctrlplane/db";
import {
createSystem,
Expand Down Expand Up @@ -158,9 +158,9 @@ export const systemRouter = createTRPCRouter({
.then(takeFirst);

await Promise.all([
createEnv(db, { systemId: sys.id, name: "Production" }),
createEnv(db, { systemId: sys.id, name: "QA" }),
createEnv(db, { systemId: sys.id, name: "Staging" }),
upsertEnv(db, { systemId: sys.id, name: "Production" }),
upsertEnv(db, { systemId: sys.id, name: "QA" }),
upsertEnv(db, { systemId: sys.id, name: "Staging" }),
]);
return sys;
}),
Expand Down
Loading
Loading