The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
import { getConfigResponse } from "@/lib/api/domains/get-config-response";
import { getDomainResponse } from "@/lib/api/domains/get-domain-response";
import { isValidDomainFormat } from "@/lib/api/domains/is-valid-domain";
import { verifyDomain } from "@/lib/api/domains/verify-domain";
import { withCron } from "@/lib/cron/with-cron";
import { prisma } from "@/lib/prisma";
import { NextResponse } from "next/server";
import { handleDomainUpdates } from "./utils";
/**
* Cron to check if domains are verified.
* If a domain is invalid for more than 14 days, we send a reminder email to the workspace owner.
* If a domain is invalid for more than 28 days, we send a second and final reminder email to the workspace owner.
* If a domain is invalid for more than 30 days, we delete it from the database.
**/
// Runs every hour (0 * * * *)
export const dynamic = "force-dynamic";
export const GET = withCron(async () => {
const domains = await prisma.domain.findMany({
where: {
slug: {
// exclude domains that belong to us
notIn: [
"dub.sh",
"chatg.pt",
"amzn.id",
"spti.fi",
"stey.me",
"steven.yt",
"steven.blue",
"owd.li",
"elegance.ai",
],
},
},
select: {
slug: true,
verified: true,
primary: true,
createdAt: true,
},
orderBy: {
lastChecked: "asc",
},
take: 30,
});
const results = await Promise.allSettled(
domains.map(async (domain) => {
const { slug, verified, primary, createdAt } = domain;
const newVerified = await getVerificationStatus(slug);
const prismaResponse = await prisma.domain.update({
where: {
slug,
},
data: {
verified: newVerified,
lastChecked: new Date(),
},
});
const changed = newVerified !== verified;
const updates = await handleDomainUpdates({
domain: slug,
createdAt,
verified: newVerified,
primary,
changed,
});
return {
domain,
previousStatus: verified,
currentStatus: newVerified,
changed,
updates,
prismaResponse,
};
}),
);
return NextResponse.json(results);
});
async function getVerificationStatus(slug: string) {
// The Vercel helpers throw on these slugs, which would leave `lastChecked`
// unchanged and keep the row at the front of this queue on every run.
if (!isValidDomainFormat(slug)) {
return false;
}
const [domainJson, configJson] = await Promise.all([
getDomainResponse(slug),
getConfigResponse(slug),
]);
if (domainJson?.error?.code === "not_found") {
return false;
}
if (!domainJson.verified) {
const verificationJson = await verifyDomain(slug);
return Boolean(verificationJson?.verified);
}
return !configJson.misconfigured;
}