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.

simplify cron

Kiran Kd9550ea19561
diff --git a/apps/web/app/api/cron/domains/update/route.ts b/apps/web/app/api/cron/domains/update/route.ts
index c36319220e..2492b7ad30 100644
--- a/apps/web/app/api/cron/domains/update/route.ts
+++ b/apps/web/app/api/cron/domains/update/route.ts
@@ -1,11 +1,11 @@
import { queueDomainUpdate } from "@/lib/api/domains/queue";
import { handleAndReturnErrorResponse } from "@/lib/api/errors";
import { linkCache } from "@/lib/api/links/cache";
-import { qstash } from "@/lib/cron";
import { verifyQstashSignature } from "@/lib/cron/verify-qstash";
import { recordLink } from "@/lib/tinybird";
import { prisma } from "@dub/prisma";
-import { APP_DOMAIN_WITH_NGROK } from "@dub/utils";
+import { Link } from "@dub/prisma/client";
+import { linkConstructorSimple } from "@dub/utils";
import { z } from "zod";
export const dynamic = "force-dynamic";
@@ -81,13 +81,7 @@ export async function POST(req: Request) {
recordLink(updatedLinks),
- // Trigger a new cron job to update the link.shortLink column for those links
- qstash.publishJSON({
- url: `${APP_DOMAIN_WITH_NGROK}/api/cron/links/update-shortlinks`,
- body: {
- linkIds,
- },
- }),
+ updateShortLinks(updatedLinks),
]);
}
@@ -102,3 +96,28 @@ export async function POST(req: Request) {
return handleAndReturnErrorResponse(error);
}
}
+
+// Update the shortLink column for a list of links
+const updateShortLinks = async (
+ links: Pick<Link, "id" | "domain" | "key">[],
+) => {
+ if (!links || links.length === 0) {
+ return new Response("No links found.");
+ }
+
+ await Promise.all(
+ links.map((link) =>
+ prisma.link.update({
+ where: {
+ id: link.id,
+ },
+ data: {
+ shortLink: linkConstructorSimple({
+ domain: link.domain,
+ key: link.key,
+ }),
+ },
+ }),
+ ),
+ );
+};
diff --git a/apps/web/app/api/cron/links/update-shortlinks/route.ts b/apps/web/app/api/cron/links/update-shortlinks/route.ts
deleted file mode 100644
index 7d2d6d8bd9..0000000000
--- a/apps/web/app/api/cron/links/update-shortlinks/route.ts
+++ /dev/null
@@ -1,59 +0,0 @@
-import { handleAndReturnErrorResponse } from "@/lib/api/errors";
-import { verifyQstashSignature } from "@/lib/cron/verify-qstash";
-import { prisma } from "@dub/prisma";
-import { linkConstructorSimple } from "@dub/utils/src";
-import { z } from "zod";
-
-export const dynamic = "force-dynamic";
-
-const schema = z.object({
- linkIds: z.array(z.string()),
-});
-
-// This route is used to update the shortlink column for a list of links.
-// POST /api/cron/links/update-shortlinks
-export async function POST(req: Request) {
- try {
- const rawBody = await req.text();
- await verifyQstashSignature({ req, rawBody });
-
- const { linkIds } = schema.parse(JSON.parse(rawBody));
-
- const links = await prisma.link.findMany({
- where: {
- id: {
- in: linkIds,
- },
- },
- select: {
- id: true,
- domain: true,
- key: true,
- },
- });
-
- if (!links || links.length === 0) {
- return new Response("No links found.");
- }
-
- await Promise.all(
- links.map((link) =>
- prisma.link.update({
- where: {
- id: link.id,
- },
- data: {
- shortLink: linkConstructorSimple({
- domain: link.domain,
- key: link.key,
- }),
- },
- }),
- ),
- );
-
- return new Response(`Updated ${links.length} links.`);
- } catch (error) {
- return handleAndReturnErrorResponse(error);
- }
-}