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 /api/cron/links/update-shortlinks cron

Kiran Ked1569def2bd
diff --git a/apps/web/app/api/cron/domains/update/route.ts b/apps/web/app/api/cron/domains/update/route.ts
index 70c8542b36..c36319220e 100644
--- a/apps/web/app/api/cron/domains/update/route.ts
+++ b/apps/web/app/api/cron/domains/update/route.ts
@@ -1,9 +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 { z } from "zod";
export const dynamic = "force-dynamic";
@@ -36,7 +38,7 @@ export async function POST(req: Request) {
where: {
domain: oldDomain,
},
- take: 500,
+ take: 100,
});
if (links.length === 0) {
@@ -78,6 +80,14 @@ 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,
+ },
+ }),
]);
}
diff --git a/apps/web/app/api/cron/links/update-shortlinks/route.ts b/apps/web/app/api/cron/links/update-shortlinks/route.ts
new file mode 100644
index 0000000000..7d2d6d8bd9
--- /dev/null
+++ b/apps/web/app/api/cron/links/update-shortlinks/route.ts
@@ -0,0 +1,59 @@
+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);
+ }
+}