The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
import { markDomainAsDeleted } from "@/lib/api/domains/mark-domain-deleted";
import { prisma } from "@/lib/prisma";
import { sendBatchEmail } from "@dub/email";
import DomainDeleted from "@dub/email/templates/domain-deleted";
import InvalidDomain from "@dub/email/templates/invalid-domain";
import { log } from "@dub/utils";
export const handleDomainUpdates = async ({
domain,
createdAt,
verified,
primary,
changed,
}: {
domain: string;
createdAt: Date;
verified: boolean;
primary: boolean;
changed: boolean;
}) => {
if (changed) {
console.log(`Domain *${domain}* changed status to *${verified}*`);
}
if (verified) return;
const invalidDays = Math.floor(
(new Date().getTime() - new Date(createdAt).getTime()) / (1000 * 3600 * 24),
);
// do nothing if domain is invalid for less than 14 days
if (invalidDays < 14) return;
const workspace = await prisma.project.findFirst({
where: {
domains: {
some: {
slug: domain,
},
},
},
select: {
id: true,
name: true,
slug: true,
sentEmails: true,
usage: true,
users: {
select: {
user: {
select: {
email: true,
},
},
},
where: {
user: {
isMachine: false,
},
notificationPreference: {
domainConfigurationUpdates: true,
},
},
},
},
});
if (!workspace) {
await log({
message: `Domain *${domain}* is invalid but not associated with any workspace, skipping.`,
type: "cron",
});
return;
}
const workspaceSlug = workspace.slug;
const sentEmails = workspace.sentEmails.map((email) => email.type);
const emails = workspace.users.map((user) => user.user.email) as string[];
// if domain is invalid for more than 30 days, check if we can delete it
if (invalidDays >= 30) {
// Don't delete the domain (manual inspection required)
// if the links for the domain have clicks recorded
const linksClicks = await prisma.link.aggregate({
_sum: {
clicks: true,
},
where: {
domain,
},
});
if (linksClicks._sum.clicks && linksClicks._sum.clicks > 0) {
console.log(
`Domain "${domain}" has been invalid for > 30 days but has links with clicks, skipping...`,
);
return;
}
// else, delete the domain
return await Promise.allSettled([
markDomainAsDeleted({
domain,
}).then(async () => {
// if the deleted domain was primary, make another domain primary
if (primary) {
const anotherDomain = await prisma.domain.findFirst({
where: {
projectId: workspace.id,
},
});
if (!anotherDomain) return;
return prisma.domain.update({
where: {
slug: anotherDomain.slug,
},
data: {
primary: true,
},
});
}
}),
log({
message: `Domain *${domain}* has been invalid for > 30 days andhas links but no link clicks, deleting.`,
type: "cron",
}),
sendBatchEmail(
emails.map((email) => ({
subject: `Your domain ${domain} has been deleted`,
to: email,
react: DomainDeleted({
email,
domain,
workspaceSlug,
}),
variant: "notifications",
})),
),
]);
}
if (invalidDays >= 28) {
const sentSecondDomainInvalidEmail = sentEmails.includes(
`secondDomainInvalidEmail:${domain}`,
);
if (!sentSecondDomainInvalidEmail) {
return sendDomainInvalidEmail({
workspaceSlug,
domain,
invalidDays,
emails,
type: "second",
});
}
}
if (invalidDays >= 14) {
const sentFirstDomainInvalidEmail = sentEmails.includes(
`firstDomainInvalidEmail:${domain}`,
);
if (!sentFirstDomainInvalidEmail) {
return sendDomainInvalidEmail({
workspaceSlug,
domain,
invalidDays,
emails,
type: "first",
});
}
}
return;
};
const sendDomainInvalidEmail = async ({
workspaceSlug,
domain,
invalidDays,
emails,
type,
}: {
workspaceSlug: string;
domain: string;
invalidDays: number;
emails: string[];
type: "first" | "second";
}) => {
console.log(
`Domain *${domain}* is invalid for ${invalidDays} days, email sent.`,
);
return await Promise.allSettled([
sendBatchEmail(
emails.map((email) => ({
subject: `Your domain ${domain} needs to be configured`,
to: email,
react: InvalidDomain({
email,
domain,
workspaceSlug,
invalidDays,
}),
variant: "notifications",
})),
),
prisma.sentEmail.create({
data: {
project: {
connect: {
slug: workspaceSlug,
},
},
type: `${type}DomainInvalidEmail:${domain}`,
},
}),
]);
};