The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
Add a cron job to cleanup the tests links and domains
Kiran K66c20b76471f
diff --git a/apps/web/app/api/cron/cleanup/route.ts b/apps/web/app/api/cron/cleanup/route.tsnew file mode 100644index 0000000000..281edf7ebd--- /dev/null+++ b/apps/web/app/api/cron/cleanup/route.ts@@ -0,0 +1,73 @@+import { deleteDomainAndLinks } from "@/lib/api/domains";+import { handleAndReturnErrorResponse } from "@/lib/api/errors";+import { deleteLink } from "@/lib/api/links";+import { verifyVercelSignature } from "@/lib/cron/verify-vercel";+import { prisma } from "@/lib/prisma";+import { log } from "@dub/utils";+import { NextResponse } from "next/server";++export const dynamic = "force-dynamic";++const E2E_USER_ID = "cm022rkcw0000ikt14mscg9sg";+const E2E_WORKSPACE_ID = "cm022sis60003ikt1syy7kfhl";++// Cron to remove links and domains created during the E2E test.+// Runs once every day at noon UTC (0 12 * * *)+export async function GET(req: Request) {+ try {+ await verifyVercelSignature(req);++ const oneHourAgo = new Date(Date.now() - 1000 * 60 * 60);++ const [links, domains] = await Promise.all([+ prisma.link.findMany({+ where: {+ userId: E2E_USER_ID,+ projectId: E2E_WORKSPACE_ID,+ createdAt: {+ lt: oneHourAgo,+ },+ },+ select: {+ id: true,+ },+ }),++ prisma.domain.findMany({+ where: {+ projectId: E2E_WORKSPACE_ID,+ slug: {+ endsWith: ".dub-internal-test.com",+ },+ createdAt: {+ lt: oneHourAgo,+ },+ },+ select: {+ slug: true,+ },+ }),+ ]);++ if (links.length > 0) {+ await Promise.all(links.map((link) => deleteLink(link.id)));+ }++ if (domains.length > 0) {+ await Promise.all(+ domains.map((domain) => deleteDomainAndLinks(domain.slug)),+ );+ }++ console.log(`Removed ${links.length} links and ${domains.length} domains`);++ return NextResponse.json({ status: "OK" });+ } catch (error) {+ await log({+ message: `Links and domain cleanup failed - ${error.message}`,+ type: "errors",+ });++ return handleAndReturnErrorResponse(error);+ }+}diff --git a/apps/web/vercel.json b/apps/web/vercel.jsonindex 88b6ad0f8c..2426281bf0 100644--- a/apps/web/vercel.json+++ b/apps/web/vercel.json@@ -7,6 +7,10 @@{"path": "/api/cron/usage","schedule": "0 12 * * *"+ },+ {+ "path": "/api/cron/cleanup",+ "schedule": "0 12 * * *"}],"functions": {