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 routes

Kiran K6e6f30acd756
diff --git a/apps/web/app/api/cron/links/sync-tinybird/route.ts b/apps/web/app/api/cron/links/sync-tinybird/route.ts
new file mode 100644
index 0000000000..2372db0b44
--- /dev/null
+++ b/apps/web/app/api/cron/links/sync-tinybird/route.ts
@@ -0,0 +1,56 @@
+import { handleAndReturnErrorResponse } from "@/lib/api/errors";
+import { verifyQstashSignature } from "@/lib/cron/verify-qstash";
+import { prisma } from "@dub/prisma";
+import { z } from "zod";
+
+export const dynamic = "force-dynamic";
+
+const schema = z.object({
+ folderId: z.string(),
+});
+
+// POST /api/cron/links/sync-tinybird
+// Sync the link changes to the Tinybird datasource
+export async function POST(req: Request) {
+ try {
+ const rawBody = await req.text();
+ await verifyQstashSignature({ req, rawBody });
+
+ const { folderId } = schema.parse(JSON.parse(rawBody));
+
+ const folder = await prisma.folder.findUnique({
+ where: {
+ id: folderId,
+ },
+ select: {
+ _count: {
+ select: {
+ links: true,
+ },
+ },
+ },
+ });
+
+ if (!folder) {
+ return new Response("Folder not found. Skipping...");
+ }
+
+ const countOfLinks = folder._count.links;
+
+ if (countOfLinks === 0) {
+ return new Response("No links found in the folder. Skipping...");
+ }
+
+ // TODO:
+ // Find 100 of 5 batches of links
+ // - call recordLink for each link
+ // - Update the folderId to null for each link
+ // invoke scheduleLinkSync with another batch
+
+
+
+ return new Response("Link synced to Tinybird.");
+ } catch (error) {
+ return handleAndReturnErrorResponse(error);
+ }
+}
diff --git a/apps/web/app/api/folders/[folderId]/route.ts b/apps/web/app/api/folders/[folderId]/route.ts
index b1bc2753b9..1fbb4a5bf6 100644
--- a/apps/web/app/api/folders/[folderId]/route.ts
+++ b/apps/web/app/api/folders/[folderId]/route.ts
@@ -1,9 +1,9 @@
import { DubApiError } from "@/lib/api/errors";
+import { scheduleLinkSync } from "@/lib/api/links/utils/sync-links";
import { parseRequestBody } from "@/lib/api/utils";
import { withWorkspace } from "@/lib/auth";
import { verifyFolderAccess } from "@/lib/folder/permissions";
import { getPlanCapabilities } from "@/lib/plan-capabilities";
-import { recordLink } from "@/lib/tinybird";
import { FolderSchema, updateFolderSchema } from "@/lib/zod/schemas/folders";
import { prisma } from "@dub/prisma";
import { waitUntil } from "@vercel/functions";
@@ -113,48 +113,49 @@ export const DELETE = withWorkspace(
requiredPermission: "folders.write",
});
- const [deletedFolder] = await prisma.$transaction([
- prisma.folder.delete({
+ const linksCount = await prisma.link.count({
+ where: {
+ folderId,
+ },
+ });
+
+ await prisma.$transaction(async (tx) => {
+ if (linksCount === 0) {
+ await tx.folder.delete({
+ where: {
+ id: folderId,
+ },
+ });
+ } else {
+ await tx.folder.update({
+ where: {
+ id: folderId,
+ },
+ data: {
+ projectId: "",
+ },
+ });
+ }
+
+ await tx.project.update({
where: {
- id: folderId,
- projectId: workspace.id,
+ id: workspace.id,
},
- include: {
- links: {
- include: {
- tags: {
- include: {
- tag: true,
- },
- },
- },
+ data: {
+ foldersUsage: {
+ decrement: 1,
},
},
- }),
- prisma.project.update({
- where: { id: workspace.id },
- data: { foldersUsage: { decrement: 1 } },
- }),
- ]);
-
- waitUntil(
- (async () => {
- const links = deletedFolder.links;
-
- // TODO:
- // Handle this via background job if number of links is huge
- if (links.length > 0) {
- recordLink(
- links.map((link) => {
- return {
- ...link,
- folderId: null,
- };
- }),
- );
- }
- })(),
- );
+ });
+ });
+
+ if (linksCount > 0) {
+ waitUntil(
+ scheduleLinkSync({
+ folderId,
+ }),
+ );
+ }
return NextResponse.json({ id: folderId });
},
diff --git a/apps/web/lib/api/links/utils/sync-links.ts b/apps/web/lib/api/links/utils/sync-links.ts
new file mode 100644
index 0000000000..21ea956418
--- /dev/null
+++ b/apps/web/lib/api/links/utils/sync-links.ts
@@ -0,0 +1,18 @@
+import { qstash } from "@/lib/cron";
+import { APP_DOMAIN_WITH_NGROK } from "@dub/utils";
+
+export async function scheduleLinkSync({
+ folderId,
+ delay,
+}: {
+ folderId: string;
+ delay?: number;
+}) {
+ return await qstash.publishJSON({
+ url: `${APP_DOMAIN_WITH_NGROK}/api/cron/links/sync-tinybird`,
+ ...(delay && { delay }),
+ body: {
+ folderId,
+ },
+ });
+}