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.

handle isGroupDeleted

Steven Tey9bf027bacbf2
diff --git a/apps/web/app/(ee)/api/cron/groups/remap-default-links/route.ts b/apps/web/app/(ee)/api/cron/groups/remap-default-links/route.ts
index 4524756207..ea255fe225 100644
--- a/apps/web/app/(ee)/api/cron/groups/remap-default-links/route.ts
+++ b/apps/web/app/(ee)/api/cron/groups/remap-default-links/route.ts
@@ -240,6 +240,7 @@ export async function POST(req: Request) {
programId,
partnerIds,
groupId,
+ isGroupDeleted,
},
});
diff --git a/apps/web/app/(ee)/api/cron/groups/remap-discount-codes/route.ts b/apps/web/app/(ee)/api/cron/groups/remap-discount-codes/route.ts
index a1206563b3..b9c8225e35 100644
--- a/apps/web/app/(ee)/api/cron/groups/remap-discount-codes/route.ts
+++ b/apps/web/app/(ee)/api/cron/groups/remap-discount-codes/route.ts
@@ -13,11 +13,12 @@ const inputSchema = z.object({
programId: z.string(),
groupId: z.string(),
partnerIds: z.array(z.string()),
+ isGroupDeleted: z.boolean().optional(),
});
// POST /api/cron/groups/remap-discount-codes
export const POST = withCron(async ({ rawBody }) => {
- const { programId, partnerIds, groupId } = inputSchema.parse(
+ const { programId, partnerIds, groupId, isGroupDeleted } = inputSchema.parse(
JSON.parse(rawBody),
);
@@ -41,6 +42,8 @@ export const POST = withCron(async ({ rawBody }) => {
},
});
+ const oldDiscount = programEnrollments[0]?.discountCodes[0]?.discount;
+
if (programEnrollments.length === 0) {
return logAndRespond("No program enrollments found.");
}
@@ -160,5 +163,24 @@ export const POST = withCron(async ({ rawBody }) => {
}
}
+ // if the group is deleted, need to check if there are any remaining discount codes, if not, delete the discount
+ if (isGroupDeleted && oldDiscount) {
+ const remainingDiscountCodes = await prisma.discountCode.count({
+ where: {
+ discountId: oldDiscount.id,
+ },
+ });
+ if (remainingDiscountCodes === 0) {
+ const deletedDiscount = await prisma.discount.delete({
+ where: {
+ id: oldDiscount.id,
+ },
+ });
+ console.log(
+ `Deleted discount ${oldDiscount.id} because it has no remaining discount codes.`,
+ );
+ }
+ }
+
return logAndRespond("Finished remapping discount codes for the group.");
});
diff --git a/apps/web/app/(ee)/api/groups/[groupIdOrSlug]/route.ts b/apps/web/app/(ee)/api/groups/[groupIdOrSlug]/route.ts
index 8c238cb25a..bdb171b5dd 100644
--- a/apps/web/app/(ee)/api/groups/[groupIdOrSlug]/route.ts
+++ b/apps/web/app/(ee)/api/groups/[groupIdOrSlug]/route.ts
@@ -342,17 +342,11 @@ export const DELETE = withWorkspace(
});
}
- // we can't delete this discount because it is needed for `remap-discount-codes` that runs in movePartnersToGroup
- // if (group.discountId) {
- // // 3. Delete the group's discount
- // await tx.discount.delete({
- // where: {
- // id: group.discountId,
- // },
- // });
- // }
-
- // 4. Delete the group move workflow
+ // Note: we can't delete this group's discount yet because it is needed
+ // for `remap-discount-codes` that runs in movePartnersToGroup
+ // but we will delete the Discount in `remap-discount-codes` once there are no remaining discount codes.
+
+ // 2. Delete the group move workflow
if (group.workflowId) {
await tx.workflow.delete({
where: {
@@ -361,7 +355,7 @@ export const DELETE = withWorkspace(
});
}
- // 5. Delete the group
+ // 3. Delete the group
await tx.partnerGroup.delete({
where: {
id: group.id,
diff --git a/apps/web/lib/api/groups/move-partners-to-group.ts b/apps/web/lib/api/groups/move-partners-to-group.ts
index ff0dd18ef6..5c7905a107 100644
--- a/apps/web/lib/api/groups/move-partners-to-group.ts
+++ b/apps/web/lib/api/groups/move-partners-to-group.ts
@@ -29,6 +29,7 @@ interface MovePartnersToGroupParams {
| "saleRewardId"
| "discountId"
>;
+ isGroupDeleted?: boolean;
}
export async function movePartnersToGroup({
@@ -37,6 +38,7 @@ export async function movePartnersToGroup({
partnerIds,
userId,
group,
+ isGroupDeleted = false,
}: MovePartnersToGroupParams): Promise<number> {
if (partnerIds.length === 0) {
return 0;
@@ -167,6 +169,7 @@ export async function movePartnersToGroup({
groupId: group.id,
partnerIds,
userId: workspaceUserId,
+ isGroupDeleted,
},
}),