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 waituntil to delete and transfer routes

Steven Tey31f7d73f1259
diff --git a/apps/web/app/api/links/[linkId]/route.ts b/apps/web/app/api/links/[linkId]/route.ts
index 0709564ca4..a73f548e05 100644
--- a/apps/web/app/api/links/[linkId]/route.ts
+++ b/apps/web/app/api/links/[linkId]/route.ts
@@ -10,7 +10,6 @@ import { withWorkspace } from "@/lib/auth";
import prisma from "@/lib/prisma";
import { NewLinkProps } from "@/lib/types";
import { updateLinkBodySchema } from "@/lib/zod/schemas";
-import { waitUntil } from "@vercel/functions";
import { NextResponse } from "next/server";
// GET /api/links/[linkId] – get a link
@@ -130,7 +129,7 @@ export const PUT = PATCH;
// DELETE /api/links/[linkId] – delete a link
export const DELETE = withWorkspace(async ({ headers, link }) => {
- waitUntil(deleteLink(link!.id));
+ await deleteLink(link!.id);
return NextResponse.json(
{ id: link!.id },
diff --git a/apps/web/app/api/links/[linkId]/transfer/route.ts b/apps/web/app/api/links/[linkId]/transfer/route.ts
index bc0dabdda8..d523044382 100644
--- a/apps/web/app/api/links/[linkId]/transfer/route.ts
+++ b/apps/web/app/api/links/[linkId]/transfer/route.ts
@@ -5,6 +5,7 @@ import prisma from "@/lib/prisma";
import { recordLink } from "@/lib/tinybird";
import { formatRedisLink, redis } from "@/lib/upstash";
import z from "@/lib/zod";
+import { waitUntil } from "@vercel/functions";
import { NextResponse } from "next/server";
const transferLinkBodySchema = z.object({
@@ -72,60 +73,63 @@ export const POST = withWorkspace(
interval: "30d",
});
- const response = await Promise.all([
- prisma.link.update({
- where: {
- id: link.id,
+ const response = await prisma.link.update({
+ where: {
+ id: link.id,
+ },
+ data: {
+ projectId: newWorkspaceId,
+ // remove tags when transferring link
+ tags: {
+ deleteMany: {},
},
- data: {
- projectId: newWorkspaceId,
- // remove tags when transferring link
- tags: {
- deleteMany: {},
+ },
+ });
+
+ waitUntil(
+ Promise.all([
+ redis.hset(link.domain.toLowerCase(), {
+ [link.key.toLowerCase()]: await formatRedisLink({
+ ...link,
+ projectId: newWorkspaceId,
+ }),
+ }),
+ recordLink({
+ link: {
+ ...link,
+ projectId: newWorkspaceId,
},
- },
- }),
- redis.hset(link.domain.toLowerCase(), {
- [link.key.toLowerCase()]: await formatRedisLink({
- ...link,
- projectId: newWorkspaceId,
}),
- }),
- recordLink({
- link: {
- ...link,
- projectId: newWorkspaceId,
- },
- }),
- // decrement old workspace usage
- prisma.project.update({
- where: {
- id: workspace.id,
- },
- data: {
- usage: {
- decrement: linkClicks,
+ // decrement old workspace usage
+ prisma.project.update({
+ where: {
+ id: workspace.id,
},
- linksUsage: {
- decrement: 1,
+ data: {
+ usage: {
+ decrement: linkClicks,
+ },
+ linksUsage: {
+ decrement: 1,
+ },
},
- },
- }),
- // increment new workspace usage
- prisma.project.update({
- where: {
- id: newWorkspaceId,
- },
- data: {
- usage: {
- increment: linkClicks,
+ }),
+ // increment new workspace usage
+ prisma.project.update({
+ where: {
+ id: newWorkspaceId,
},
- linksUsage: {
- increment: 1,
+ data: {
+ usage: {
+ increment: linkClicks,
+ },
+ linksUsage: {
+ increment: 1,
+ },
},
- },
- }),
- ]);
+ }),
+ ]),
+ );
return NextResponse.json(response, {
headers,
diff --git a/apps/web/lib/api/links/delete-link.ts b/apps/web/lib/api/links/delete-link.ts
index f5691bd34a..2bd62c723f 100644
--- a/apps/web/lib/api/links/delete-link.ts
+++ b/apps/web/lib/api/links/delete-link.ts
@@ -2,6 +2,7 @@ import prisma from "@/lib/prisma";
import { storage } from "@/lib/storage";
import { recordLink } from "@/lib/tinybird";
import { redis } from "@/lib/upstash";
+import { waitUntil } from "@vercel/functions";
export async function deleteLink(linkId: string) {
const link = await prisma.link.delete({
@@ -12,23 +13,27 @@ export async function deleteLink(linkId: string) {
tags: true,
},
});
- return await Promise.allSettled([
- // if the image is stored in Cloudflare R2, delete it
- link.proxy &&
- link.image?.startsWith(process.env.STORAGE_BASE_URL as string) &&
- storage.delete(`images/${link.id}`),
- redis.hdel(link.domain.toLowerCase(), link.key.toLowerCase()),
- recordLink({ link, deleted: true }),
- link.projectId &&
- prisma.project.update({
- where: {
- id: link.projectId,
- },
- data: {
- linksUsage: {
- decrement: 1,
+ waitUntil(
+ Promise.allSettled([
+ // if the image is stored in Cloudflare R2, delete it
+ link.proxy &&
+ link.image?.startsWith(process.env.STORAGE_BASE_URL as string) &&
+ storage.delete(`images/${link.id}`),
+ redis.hdel(link.domain.toLowerCase(), link.key.toLowerCase()),
+ recordLink({ link, deleted: true }),
+ link.projectId &&
+ prisma.project.update({
+ where: {
+ id: link.projectId,
},
- },
- }),
- ]);
+ data: {
+ linksUsage: {
+ decrement: 1,
+ },
+ },
+ }),
+ ]),
+ );
+
+ return link;
}
diff --git a/apps/web/lib/api/links/index.ts b/apps/web/lib/api/links/index.ts
index 88947b3180..bd1dff4902 100644
--- a/apps/web/lib/api/links/index.ts
+++ b/apps/web/lib/api/links/index.ts
@@ -5,6 +5,5 @@ export * from "./delete-link";
export * from "./get-links-count";
export * from "./get-links-for-workspace";
export * from "./process-link";
-export * from "./transfer-link";
export * from "./update-link";
export * from "./utils";
diff --git a/apps/web/lib/api/links/transfer-link.ts b/apps/web/lib/api/links/transfer-link.ts
deleted file mode 100644
index 849335cf08..0000000000
--- a/apps/web/lib/api/links/transfer-link.ts
+++ /dev/null
@@ -1,22 +0,0 @@
-import prisma from "@/lib/prisma";
-
-export async function transferLink({
- linkId,
- newWorkspaceId,
-}: {
- linkId: string;
- newWorkspaceId: string;
-}) {
- return await prisma.link.update({
- where: {
- id: linkId,
- },
- data: {
- projectId: newWorkspaceId,
- // remove tags when transferring link
- tags: {
- deleteMany: {},
- },
- },
- });
-}