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.

Delete discount codes before removing links

Kiran K86267c313bb1
diff --git a/apps/web/lib/api/links/delete-link.ts b/apps/web/lib/api/links/delete-link.ts
index 6f9a58810f..80a7ad8633 100644
--- a/apps/web/lib/api/links/delete-link.ts
+++ b/apps/web/lib/api/links/delete-link.ts
@@ -10,7 +10,7 @@ import { includeTags } from "./include-tags";
import { transformLink } from "./utils";
export async function deleteLink(linkId: string) {
- const link = await prisma.link.delete({
+ const link = await prisma.link.findUniqueOrThrow({
where: {
id: linkId,
},
@@ -29,6 +29,18 @@ export async function deleteLink(linkId: string) {
},
});
+ // DiscountCode.linkId is a required relation (onDelete: Restrict),
+ // so the discount code must be deleted before the link
+ if (link.discountCode) {
+ await deleteDiscountCodes([link.discountCode]);
+ }
+
+ await prisma.link.delete({
+ where: {
+ id: linkId,
+ },
+ });
+
waitUntil(
Promise.allSettled([
// if there's a valid image and it has the same link ID, delete it
@@ -53,8 +65,6 @@ export async function deleteLink(linkId: string) {
},
},
}),
-
- link.discountCode && deleteDiscountCodes([link.discountCode]),
]),
);