Skip to content

armgit / dub

Public Imported from GitHub
0

The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.

Scrub deleted group IDs from other groups' move-rule conditions.

Kiran K14946f45faf6
diff --git a/apps/web/app/(ee)/api/groups/[groupIdOrSlug]/route.ts b/apps/web/app/(ee)/api/groups/[groupIdOrSlug]/route.ts
index 568ac48104..5b8a8fc5d5 100644
--- a/apps/web/app/(ee)/api/groups/[groupIdOrSlug]/route.ts
+++ b/apps/web/app/(ee)/api/groups/[groupIdOrSlug]/route.ts
@@ -2,6 +2,7 @@ import { recordAuditLog } from "@/lib/api/audit-logs/record-audit-log";
import { DubApiError } from "@/lib/api/errors";
import { getGroupOrThrow } from "@/lib/api/groups/get-group-or-throw";
import { movePartnersToGroup } from "@/lib/api/groups/move-partners-to-group";
+import { removeGroupIdFromMoveRules } from "@/lib/api/groups/remove-group-id-from-move-rules";
import { upsertGroupMoveRules } from "@/lib/api/groups/upsert-group-move-rules";
import { getDefaultProgramIdOrThrow } from "@/lib/api/programs/get-default-program-id-or-throw";
import { parseRequestBody } from "@/lib/api/utils";
@@ -386,20 +387,27 @@ export const DELETE = withWorkspace(
if (deletedGroup) {
waitUntil(
- recordAuditLog({
- workspaceId: workspace.id,
- programId,
- action: "group.deleted",
- description: `Group ${group.name} (${group.id}) deleted`,
- actor: session.user,
- targets: [
- {
- type: "group",
- id: group.id,
- metadata: group,
- },
- ],
- }),
+ Promise.allSettled([
+ recordAuditLog({
+ workspaceId: workspace.id,
+ programId,
+ action: "group.deleted",
+ description: `Group ${group.name} (${group.id}) deleted`,
+ actor: session.user,
+ targets: [
+ {
+ type: "group",
+ id: group.id,
+ metadata: group,
+ },
+ ],
+ }),
+
+ removeGroupIdFromMoveRules({
+ programId,
+ groupId: group.id,
+ }),
+ ]),
);
}
diff --git a/apps/web/lib/api/groups/remove-group-id-from-move-rules.ts b/apps/web/lib/api/groups/remove-group-id-from-move-rules.ts
new file mode 100644
index 0000000000..27fe6be361
--- /dev/null
+++ b/apps/web/lib/api/groups/remove-group-id-from-move-rules.ts
@@ -0,0 +1,97 @@
+import type { WorkflowCondition } from "@/lib/api/workflows/types";
+import { prisma } from "@/lib/prisma";
+import { workflowConditionsSchema } from "@/lib/zod/schemas/workflows";
+
+// Scrubs a deleted group ID from other groups' move-rule workflows in the program.
+export async function removeGroupIdFromMoveRules({
+ programId,
+ groupId,
+}: {
+ programId: string;
+ groupId: string;
+}): Promise<void> {
+ const groups = await prisma.partnerGroup.findMany({
+ where: {
+ programId,
+ id: { not: groupId },
+ workflowId: { not: null },
+ },
+ select: {
+ workflowId: true,
+ workflow: {
+ select: {
+ id: true,
+ triggerConditions: true,
+ },
+ },
+ },
+ });
+
+ for (const group of groups) {
+ if (!group.workflowId || !group.workflow) {
+ continue;
+ }
+
+ const parsed = workflowConditionsSchema.safeParse(
+ group.workflow.triggerConditions,
+ );
+
+ if (!parsed.success) {
+ continue;
+ }
+
+ const nextConditions = scrubGroupIdFromConditions({
+ conditions: parsed.data,
+ groupId,
+ });
+
+ if (JSON.stringify(nextConditions) === JSON.stringify(parsed.data)) {
+ continue;
+ }
+
+ await prisma.workflow.update({
+ where: {
+ id: group.workflowId,
+ },
+ data: {
+ triggerConditions: nextConditions,
+ },
+ });
+ }
+}
+
+function scrubGroupIdFromConditions({
+ conditions,
+ groupId,
+}: {
+ conditions: WorkflowCondition[];
+ groupId: string;
+}): WorkflowCondition[] {
+ return conditions.flatMap((condition) => {
+ if (condition.attribute !== "partnerGroup") {
+ return [condition];
+ }
+
+ const { value } = condition;
+
+ if (typeof value === "string") {
+ return value === groupId ? [] : [condition];
+ }
+
+ if (Array.isArray(value)) {
+ const nextValue = value.filter((id) => id !== groupId);
+
+ if (nextValue.length === 0) {
+ return [];
+ }
+
+ if (nextValue.length === value.length) {
+ return [condition];
+ }
+
+ return [{ ...condition, value: nextValue }];
+ }
+
+ return [condition];
+ });
+}