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.

add coupon code creation during bulk create links

Kiran K9d93ef3d5755
diff --git a/apps/web/lib/api/links/bulk-create-links.ts b/apps/web/lib/api/links/bulk-create-links.ts
index 48b898db23..c4ffffb95e 100644
--- a/apps/web/lib/api/links/bulk-create-links.ts
+++ b/apps/web/lib/api/links/bulk-create-links.ts
@@ -4,6 +4,7 @@ import { getParamsFromURL, linkConstructorSimple, truncate } from "@dub/utils";
import { Prisma } from "@prisma/client";
import { waitUntil } from "@vercel/functions";
import { createId } from "../create-id";
+import { enqueueCouponCodeCreateJobs } from "../discounts/enqueue-coupon-code-create-jobs";
import { combineTagIds } from "../tags/combine-tag-ids";
import { encodeKeyIfCaseSensitive } from "./case-sensitivity";
import { includeTags } from "./include-tags";
@@ -213,16 +214,44 @@ export async function bulkCreateLinks({
}
waitUntil(
- Promise.all([
- propagateBulkLinkChanges({
- links: createdLinksData,
- skipRedisCache,
- }),
- updateLinksUsage({
- workspaceId: links[0].projectId!, // this will always be present
- increment: links.length,
- }),
- ]),
+ (async () => {
+ // Find the links with coupon code tracking enabled
+ const partnerLinks = await prisma.link.findMany({
+ where: {
+ id: {
+ in: createdLinksData.map((link) => link.id),
+ },
+ },
+ select: {
+ id: true,
+ key: true,
+ programEnrollment: {
+ select: {
+ discount: {
+ select: {
+ couponCodeTrackingEnabledAt: true,
+ },
+ },
+ },
+ },
+ },
+ });
+
+ Promise.allSettled([
+ propagateBulkLinkChanges({
+ links: createdLinksData,
+ skipRedisCache,
+ }),
+
+ updateLinksUsage({
+ workspaceId: links[0].projectId!, // this will always be present
+ increment: links.length,
+ }),
+
+ // Enqueue coupon code create jobs for partner links
+ enqueueCouponCodeCreateJobs(partnerLinks),
+ ]);
+ })(),
);
// Simplified sorting using the map
diff --git a/apps/web/lib/api/links/delete-link.ts b/apps/web/lib/api/links/delete-link.ts
index 6cae2185c6..0d8f862365 100644
--- a/apps/web/lib/api/links/delete-link.ts
+++ b/apps/web/lib/api/links/delete-link.ts
@@ -24,8 +24,6 @@ export async function deleteLink(linkId: string) {
},
});
- const { project: workspace } = link;
-
waitUntil(
Promise.allSettled([
// if there's a valid image and it has the same link ID, delete it
@@ -52,7 +50,8 @@ export async function deleteLink(linkId: string) {
},
}),
- link.projectId && enqueueCouponCodeDeleteJobs(link),
+ // Delete the coupon code for the link if it exists
+ link.couponCode && enqueueCouponCodeDeleteJobs(link),
]),
);