diff --git a/apps/webservice/src/app/api/v1/environments/route.ts b/apps/webservice/src/app/api/v1/environments/route.ts index f3ea67ad9..cae2628e2 100644 --- a/apps/webservice/src/app/api/v1/environments/route.ts +++ b/apps/webservice/src/app/api/v1/environments/route.ts @@ -31,9 +31,15 @@ export const POST = request() ), ) .handle<{ user: User; can: PermissionChecker; body: z.infer }>( - ({ db, body }) => - db.transaction(async (tx) => { - try { + async ({ db, body }) => { + try { + const existingEnv = await db + .select() + .from(schema.environment) + .where(eq(schema.environment.name, body.name)) + .then(takeFirstOrNull); + + const environment = await db.transaction(async (tx) => { const releaseChannels = body.releaseChannels?.length ?? 0; const deploymentVersionChannels = body.deploymentVersionChannels?.length ?? 0; @@ -56,36 +62,33 @@ export const POST = request() ) : []; - const existingEnv = await db - .select() - .from(schema.environment) - .where(eq(schema.environment.name, body.name)) - .then(takeFirstOrNull); - const environment = await upsertEnv(tx, { ...body, versionChannels }); - if (existingEnv != null) - await getQueue(Channel.UpdateEnvironment).add(environment.id, { - ...environment, - oldSelector: existingEnv.resourceSelector, - }); - - if (existingEnv == null) - await getQueue(Channel.NewEnvironment).add( - environment.id, - environment, - ); - await createJobsForNewEnvironment(tx, environment); const { metadata } = body; - return NextResponse.json({ ...environment, metadata }); - } 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 }, + return { ...environment, metadata }; + }); + + if (existingEnv != null) + await getQueue(Channel.UpdateEnvironment).add(environment.id, { + ...environment, + oldSelector: existingEnv.resourceSelector, + }); + + if (existingEnv == null) + await getQueue(Channel.NewEnvironment).add( + environment.id, + environment, ); - } - }), + + return NextResponse.json(environment); + } 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 }, + ); + } + }, ); diff --git a/e2e/tests/api/environments.spec.ts b/e2e/tests/api/environments.spec.ts index 950741b9d..eb7b66b5c 100644 --- a/e2e/tests/api/environments.spec.ts +++ b/e2e/tests/api/environments.spec.ts @@ -40,15 +40,27 @@ test.describe("Environments API", () => { }); test("should match resources to new environment", async ({ api, page }) => { + const systemPrefix = importedEntities.system.slug.split("-")[0]!; const environmentResponse = await api.POST("/v1/environments", { body: { name: faker.string.alphanumeric(10), systemId: importedEntities.system.id, resourceSelector: { - type: "metadata", - operator: "equals", - key: "env", - value: "qa", + type: "comparison", + operator: "and", + conditions: [ + { + type: "metadata", + operator: "equals", + key: "env", + value: "qa", + }, + { + type: "identifier", + operator: "starts-with", + value: systemPrefix, + }, + ], }, }, }); @@ -71,7 +83,8 @@ test.describe("Environments API", () => { expect(resourcesResponse.response.status).toBe(200); expect(resourcesResponse.data?.resources?.length).toBe(1); expect(resourcesResponse.data?.resources?.[0]?.identifier).toBe( - importedEntities.resources[0].identifier, + importedEntities.resources.find((r) => r.metadata?.env === "qa") + ?.identifier, ); }); }); diff --git a/e2e/tests/api/environments.spec.yaml b/e2e/tests/api/environments.spec.yaml index 7c921b8f8..55083eb2e 100644 --- a/e2e/tests/api/environments.spec.yaml +++ b/e2e/tests/api/environments.spec.yaml @@ -21,4 +21,3 @@ resources: enabled: true metadata: env: prod -