The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
import { prefixWorkspaceId } from "@/lib/api/workspaces/workspace-id";
import { isBlacklistedEmail } from "@/lib/edge-config";
import { plain } from "@/lib/plain/client";
import { syncUserPlanToPlain } from "@/lib/plain/sync-user-plan";
import { upsertPlainCustomer } from "@/lib/plain/upsert-plain-customer";
import { prisma } from "@/lib/prisma";
import { capitalize, formatDate } from "@dub/utils";
import { uiComponent } from "@team-plain/typescript-sdk";
import { waitUntil } from "@vercel/functions";
import { NextRequest, NextResponse } from "next/server";
import {
plainCallbackSchema,
plainCopySection,
plainDivider,
plainEmptyContainer,
plainSpacer,
plainUsageSection,
} from "../utils";
export async function POST(req: NextRequest) {
// authenticate webhook X-Plain-Webhook-Secret
const token = req.headers.get("X-Plain-Webhook-Secret");
if (token !== process.env.PLAIN_WEBHOOK_SECRET) {
return new Response("Unauthorized", { status: 401 });
}
let { customer } = plainCallbackSchema.parse(await req.json());
const user = await prisma.user.findUnique({
where: customer.externalId
? { id: customer.externalId }
: { email: customer.email },
});
if (!user || !user.email) {
const isBannedUser = await isBlacklistedEmail(customer.email);
if (isBannedUser) {
await plain.addCustomerToCustomerGroups({
customerId: customer.id,
customerGroupIdentifiers: [
{
customerGroupKey: "banned_users",
},
],
});
}
return NextResponse.json({
cards: [
{
key: "workspace",
components: [plainEmptyContainer("No user found.")],
},
],
});
}
if (!customer.externalId) {
customer.externalId = user.id;
await upsertPlainCustomer({
id: user.id,
name: user.name,
email: user.email,
});
}
waitUntil(syncUserPlanToPlain(user));
const topWorkspace = await prisma.project.findFirst({
where: {
users: {
some: {
userId: customer.externalId,
},
},
},
include: {
_count: {
select: {
domains: true,
users: true,
},
},
},
orderBy: {
usageLimit: "desc",
},
take: 1,
});
if (!topWorkspace) {
return NextResponse.json({
cards: [
{
key: "workspace",
components: [
plainEmptyContainer("User does not belong to any workspace."),
],
},
],
});
}
const {
id,
name,
slug,
plan,
planTier,
planPeriod,
stripeId,
trialEndsAt,
usage,
usageLimit,
totalClicks,
linksUsage,
linksLimit,
totalLinks,
domainsLimit,
usersLimit,
_count: { domains, users },
} = topWorkspace;
return NextResponse.json({
cards: [
{
key: "workspace",
components: [
...plainCopySection({
label: "Workspace ID",
value: prefixWorkspaceId(id),
}),
plainSpacer,
...plainCopySection({
label: "Workspace Name",
value: name,
}),
plainSpacer,
...plainCopySection({
label: "Workspace Slug",
value: slug,
}),
plainSpacer,
{
componentRow: {
rowMainContent: [
{
componentText: {
text: "Plan",
},
},
],
rowAsideContent: [
{
componentBadge: {
badgeLabel: `${capitalize(plan)} ${planTier > 1 ? `[TIER ${planTier}] ` : ""}${planPeriod ? `[${capitalize(planPeriod)}]` : ""}`,
badgeColor:
plan === "enterprise"
? "RED"
: plan === "advanced"
? "YELLOW"
: plan.startsWith("business")
? "GREEN"
: plan === "pro"
? "BLUE"
: "GREY",
},
},
],
},
},
...(trialEndsAt
? [
uiComponent.spacer({
size: "M",
}),
uiComponent.row({
mainContent: [
uiComponent.text({
text: "⏲️ Trial ends",
size: "M",
color: "NORMAL",
}),
],
asideContent: [
uiComponent.text({
text: formatDate(trialEndsAt),
size: "S",
color: "MUTED",
}),
],
}),
]
: []),
...(stripeId
? [
uiComponent.spacer({
size: "M",
}),
uiComponent.row({
mainContent: [
uiComponent.text({
text: "Stripe Customer",
size: "M",
color: "NORMAL",
}),
uiComponent.text({
text: stripeId,
size: "S",
color: "MUTED",
}),
],
asideContent: [
uiComponent.linkButton({
url: `https://dashboard.stripe.com/customers/${stripeId}`,
label: "View in Stripe",
}),
],
}),
]
: []),
uiComponent.spacer({
size: "M",
}),
uiComponent.row({
mainContent: [
uiComponent.text({
text: "Signed up",
size: "M",
color: "NORMAL",
}),
],
asideContent: [
uiComponent.text({
text: formatDate(user.createdAt),
size: "S",
color: "MUTED",
}),
],
}),
plainDivider,
plainUsageSection({
usage,
usageLimit,
label: "Clicks",
sublabel: "This billing period",
color: "GREEN",
}),
uiComponent.spacer({
size: "M",
}),
plainUsageSection({
usage: linksUsage,
usageLimit: linksLimit,
label: "Links",
sublabel: "This billing period",
color: "YELLOW",
}),
uiComponent.spacer({
size: "M",
}),
plainUsageSection({
usage: totalClicks,
label: "Total Clicks",
color: "GREEN",
}),
uiComponent.spacer({
size: "M",
}),
plainUsageSection({
usage: totalLinks,
label: "Total Links",
color: "YELLOW",
}),
uiComponent.spacer({
size: "M",
}),
plainUsageSection({
usage: domains,
usageLimit: domainsLimit,
label: "Total Domains",
color: "BLUE",
}),
uiComponent.spacer({
size: "M",
}),
plainUsageSection({
usage: users,
usageLimit: usersLimit,
label: "Total Users",
color: "GREY",
}),
],
},
],
});
}