Skip to content
Merged
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
40 changes: 25 additions & 15 deletions apps/event-worker/src/releases/variable-change/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import type { SQL } from "@ctrlplane/db";
import type { ReleaseRepository } from "@ctrlplane/rule-engine";
import type { ReleaseVariableChangeEvent } from "@ctrlplane/validators/events";
import { Worker } from "bullmq";

import { eq, isNull, takeFirstOrNull } from "@ctrlplane/db";
import { and, eq, isNull, takeFirstOrNull } from "@ctrlplane/db";
import { db } from "@ctrlplane/db/client";
import * as schema from "@ctrlplane/db/schema";
import {
Expand All @@ -15,6 +16,19 @@ import {
import { redis } from "../../redis.js";
import { createAndEvaluateRelease } from "../create-release.js";

const getResourceReleases = async (where: SQL) =>
db
.select()
.from(schema.resourceRelease)
.innerJoin(
schema.resource,
eq(schema.resourceRelease.resourceId, schema.resource.id),
)
.where(and(where, isNull(schema.resource.deletedAt)))
.then((rows) =>
rows.map((r) => ({ ...r.resource_release, resource: r.resource })),
);

const handleDeploymentVariableChange = async (deploymentVariableId: string) => {
const variable = await db
.select()
Expand All @@ -24,10 +38,9 @@ const handleDeploymentVariableChange = async (deploymentVariableId: string) => {

if (variable == null) throw new Error("Deployment variable not found");

return db.query.resourceRelease.findMany({
where: eq(schema.resourceRelease.deploymentId, variable.deploymentId),
with: { resource: { where: isNull(schema.resource.deletedAt) } },
});
return getResourceReleases(
eq(schema.resourceRelease.deploymentId, variable.deploymentId),
);
};

const handleSystemVariableChange = async (systemVariableSetId: string) => {
Expand All @@ -44,18 +57,15 @@ const handleSystemVariableChange = async (systemVariableSetId: string) => {

if (deployment == null) throw new Error("System variable set not found");

return db.query.resourceRelease.findMany({
where: eq(schema.resourceRelease.deploymentId, deployment.id),
with: { resource: { where: isNull(schema.resource.deletedAt) } },
});
return getResourceReleases(
eq(schema.resourceRelease.deploymentId, deployment.id),
);
};

const handleResourceVariableChange = async (resourceVariableId: string) => {
return db.query.resourceRelease.findMany({
where: eq(schema.resourceRelease.resourceId, resourceVariableId),
with: { resource: { where: isNull(schema.resource.deletedAt) } },
});
};
const handleResourceVariableChange = async (resourceVariableId: string) =>
getResourceReleases(
eq(schema.resourceRelease.resourceId, resourceVariableId),
);

export const createReleaseVariableChangeWorker = () =>
new Worker<ReleaseVariableChangeEvent>(
Expand Down
Loading