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.

fix group update

Kiran Ke9e81b57e748
diff --git a/apps/web/app/(ee)/api/campaigns/[campaignId]/route.ts b/apps/web/app/(ee)/api/campaigns/[campaignId]/route.ts
index 6dabb68ea5..891113e217 100644
--- a/apps/web/app/(ee)/api/campaigns/[campaignId]/route.ts
+++ b/apps/web/app/(ee)/api/campaigns/[campaignId]/route.ts
@@ -15,6 +15,8 @@ import {
} from "@/lib/zod/schemas/workflows";
import { prisma } from "@dub/prisma";
import { CampaignStatus, CampaignType, Workflow } from "@dub/prisma/client";
+import { arrayEqual } from "@dub/utils";
+import { PartnerGroup } from "@prisma/client";
import { waitUntil } from "@vercel/functions";
import { NextResponse } from "next/server";
@@ -68,10 +70,25 @@ export const PATCH = withWorkspace(
},
});
- const partnerGroups = await throwIfInvalidGroupIds({
- programId,
- groupIds,
- });
+ // if groupIds is provided and is different from the current groupIds, update the groups
+ let updatedPartnerGroups: PartnerGroup[] | undefined = undefined;
+ let shouldUpdateGroups = false;
+
+ if (groupIds !== undefined) {
+ const currentGroupIds = campaign.groups.map(({ groupId }) => groupId);
+ const newGroupIds = groupIds || []; // treat null as empty array (all groups)
+
+ if (!arrayEqual(currentGroupIds, newGroupIds)) {
+ if (newGroupIds.length > 0) {
+ updatedPartnerGroups = await throwIfInvalidGroupIds({
+ programId,
+ groupIds: newGroupIds,
+ });
+ }
+
+ shouldUpdateGroups = true;
+ }
+ }
// TODO
// When pausing we need to disable the workflow
@@ -140,11 +157,15 @@ export const PATCH = withWorkspace(
...(status && { status }),
...(body && { body }),
...(workflow && { workflowId: workflow.id }),
- ...(partnerGroups.length && {
+ ...(shouldUpdateGroups && {
groups: {
- createMany: {
- data: partnerGroups.map(({ id }) => ({ groupId: id })),
- },
+ deleteMany: {},
+ ...(updatedPartnerGroups &&
+ updatedPartnerGroups.length > 0 && {
+ create: updatedPartnerGroups.map((group) => ({
+ groupId: group.id,
+ })),
+ }),
},
}),
},