Skip to content

Papermark is the open-source DocSend alternative and secure data rooms with built-in analytics and custom domains.

feat: add yir

Marc Seitze6ab7f443900
diff --git a/app/api/cron/year-in-review/route.ts b/app/api/cron/year-in-review/route.ts
new file mode 100644
index 00000000..93b10366
--- /dev/null
+++ b/app/api/cron/year-in-review/route.ts
@@ -0,0 +1,33 @@
+import { NextResponse } from "next/server";
+
+import { receiver } from "@/lib/cron";
+import { log } from "@/lib/utils";
+import { processEmailQueue } from "@/lib/year-in-review/send-emails";
+
+// Runs every hour (0 * * * *)
+export const maxDuration = 300; // 5 minutes in seconds
+
+export async function POST(req: Request) {
+ const body = await req.json();
+ if (process.env.VERCEL === "1") {
+ const isValid = await receiver.verify({
+ signature: req.headers.get("Upstash-Signature") || "",
+ body: JSON.stringify(body),
+ });
+ if (!isValid) {
+ return new Response("Unauthorized", { status: 401 });
+ }
+ }
+
+ try {
+ await processEmailQueue();
+ return NextResponse.json({ success: true });
+ } catch (error) {
+ await log({
+ message: `Year in review email cron failed. \n\nError: ${(error as Error).message}`,
+ type: "cron",
+ mention: true,
+ });
+ return NextResponse.json({ error: (error as Error).message });
+ }
+}
diff --git a/app/api/og/yir-alt/route.tsx b/app/api/og/yir-alt/route.tsx
new file mode 100644
index 00000000..32cbade7
--- /dev/null
+++ b/app/api/og/yir-alt/route.tsx
@@ -0,0 +1,82 @@
+import { ImageResponse } from "next/og";
+import { NextRequest } from "next/server";
+
+export const runtime = "edge";
+
+export const alt = "Papermark Year in Review";
+export const size = {
+ width: 1200,
+ height: 630,
+};
+export const contentType = "image/png";
+
+export async function GET(req: NextRequest) {
+ const InterBold = await fetch(
+ new URL("@/public/_static/Inter-Bold.ttf", import.meta.url),
+ ).then((res) => res.arrayBuffer());
+
+ const year = req.nextUrl.searchParams.get("year") || "2024";
+ const minutesSpentOnDocs =
+ req.nextUrl.searchParams.get("minutesSpentOnDocs") || "1000";
+ const uploadedDocuments =
+ req.nextUrl.searchParams.get("uploadedDocuments") || "100";
+ const sharedLinks = req.nextUrl.searchParams.get("sharedLinks") || "10";
+ const receivedViews = req.nextUrl.searchParams.get("receivedViews") || "1000";
+
+ return new ImageResponse(
+ (
+ <div tw="flex flex-row bg-[#f9fafb] w-full h-full p-16">
+ <div tw="flex flex-col justify-center w-1/2">
+ <div tw="flex text-7xl font-bold mb-8">Papermark</div>
+ <div tw="flex text-4xl font-base">{year} IN REVIEW</div>
+ </div>
+
+ <div tw="flex flex-col w-1/2 bg-white rounded-lg shadow-md p-12">
+ <div tw="flex flex-col border-b border-gray-200 pb-6 mb-8">
+ <div tw="flex text-4xl font-bold text-gray-800">
+ Papermark Stats
+ </div>
+ </div>
+ <div tw="flex flex-col space-y-10">
+ <div tw="flex justify-between items-center">
+ <div tw="flex text-2xl text-gray-600">Minutes spent on docs</div>
+ <div tw="flex text-3xl font-bold text-gray-900">
+ {minutesSpentOnDocs}
+ </div>
+ </div>
+ <div tw="flex justify-between items-center">
+ <div tw="flex text-2xl text-gray-600">Documents uploaded</div>
+ <div tw="flex text-3xl font-bold text-gray-900">
+ {uploadedDocuments}
+ </div>
+ </div>
+ <div tw="flex justify-between items-center">
+ <div tw="flex text-2xl text-gray-600">Links shared</div>
+ <div tw="flex text-3xl font-bold text-gray-900">
+ {sharedLinks}
+ </div>
+ </div>
+ <div tw="flex justify-between items-center">
+ <div tw="flex text-2xl text-gray-600">Views received</div>
+ <div tw="flex text-3xl font-bold text-gray-900">
+ {receivedViews}
+ </div>
+ </div>
+ </div>
+ </div>
+ </div>
+ ),
+ {
+ ...size,
+ headers: {
+ "Cache-Control": "public, max-age=3600, immutable",
+ },
+ fonts: [
+ {
+ name: "Inter Bold",
+ data: InterBold,
+ },
+ ],
+ },
+ );
+}
diff --git a/app/api/og/yir/route.tsx b/app/api/og/yir/route.tsx
new file mode 100644
index 00000000..a1912071
--- /dev/null
+++ b/app/api/og/yir/route.tsx
@@ -0,0 +1,117 @@
+import { ImageResponse } from "next/og";
+import { NextRequest } from "next/server";
+
+export const runtime = "edge";
+
+export const alt = "Papermark Year in Review";
+export const size = {
+ width: 1200,
+ height: 630,
+};
+export const contentType = "image/png";
+
+export async function GET(req: NextRequest) {
+ const InterBold = await fetch(
+ new URL("@/public/_static/Inter-Bold.ttf", import.meta.url),
+ ).then((res) => res.arrayBuffer());
+
+ const year = req.nextUrl.searchParams.get("year") || "2024";
+ const minutesSpentOnDocs =
+ req.nextUrl.searchParams.get("minutesSpentOnDocs") || "1000";
+ const uploadedDocuments =
+ req.nextUrl.searchParams.get("uploadedDocuments") || "100";
+ const sharedLinks = req.nextUrl.searchParams.get("sharedLinks") || "10";
+ const receivedViews = req.nextUrl.searchParams.get("receivedViews") || "1000";
+
+ return new ImageResponse(
+ (
+ <div
+ tw="flex flex-col bg-gray-50 w-full h-full"
+ style={{ padding: "48px" }}
+ >
+ {/* Header */}
+ <div tw="flex flex-col items-center mb-16">
+ <div tw="flex text-5xl font-bold mb-2">Papermark</div>
+ <div tw="flex text-4xl mb-2">Year in Review</div>
+ <div tw="flex text-5xl font-bold">{year}</div>
+ </div>
+
+ {/* Stats Grid */}
+ <div tw="flex w-full justify-between">
+ {/* Minutes Spent */}
+ <div
+ tw="flex flex-col items-center justify-center rounded-3xl p-8"
+ style={{
+ width: "260px",
+ height: "260px",
+ background:
+ "linear-gradient(135deg, rgba(251, 122, 0, 0.2), rgba(251, 122, 0, 0.7))",
+ }}
+ >
+ <div tw="flex text-2xl text-[#a63b00] mb-2">Minutes Spent</div>
+ <div tw="flex text-4xl font-bold">{minutesSpentOnDocs}</div>
+ </div>
+
+ {/* Documents */}
+ <div
+ tw="flex flex-col items-center justify-center rounded-3xl p-8"
+ style={{
+ width: "260px",
+ height: "260px",
+ background:
+ "linear-gradient(135deg, rgba(75, 85, 99, 0.2), rgba(75, 85, 99, 0.7))",
+ }}
+ >
+ <div tw="flex text-2xl text-gray-600 mb-2">Documents</div>
+ <div tw="flex text-4xl font-bold">{uploadedDocuments}</div>
+ </div>
+
+ {/* Links Shared */}
+ <div
+ tw="flex flex-col items-center justify-center rounded-3xl p-8"
+ style={{
+ width: "260px",
+ height: "260px",
+ background:
+ "linear-gradient(135deg, rgba(228, 197, 160, 0.2), rgba(228, 197, 160, 0.7))",
+ }}
+ >
+ <div tw="flex text-2xl text-[#9c7b4a] mb-2">Links Shared</div>
+ <div tw="flex text-4xl font-bold">{sharedLinks}</div>
+ </div>
+
+ {/* Total Views */}
+ <div
+ tw="flex flex-col items-center justify-center rounded-3xl p-8"
+ style={{
+ width: "260px",
+ height: "260px",
+ background:
+ "linear-gradient(135deg, rgba(16, 185, 129, 0.2), rgba(16, 185, 129, 0.7))",
+ }}
+ >
+ <div tw="flex text-2xl text-[#065f46] mb-2">Total Views</div>
+ <div tw="flex text-4xl font-bold">{receivedViews}</div>
+ </div>
+ </div>
+
+ {/* Footer */}
+ <div tw="flex w-full justify-center mt-16">
+ <div tw="flex text-xl text-gray-400">papermark.io</div>
+ </div>
+ </div>
+ ),
+ {
+ ...size,
+ headers: {
+ "Cache-Control": "public, max-age=3600, immutable",
+ },
+ fonts: [
+ {
+ name: "Inter Bold",
+ data: InterBold,
+ },
+ ],
+ },
+ );
+}
diff --git a/components/emails/year-in-review-papermark.tsx b/components/emails/year-in-review-papermark.tsx
new file mode 100644
index 00000000..583ea34f
--- /dev/null
+++ b/components/emails/year-in-review-papermark.tsx
@@ -0,0 +1,281 @@
+import React from "react";
+
+import {
+ Body,
+ Column,
+ Container,
+ Head,
+ Heading,
+ Hr,
+ Html,
+ Link,
+ Preview,
+ Row,
+ Section,
+ Tailwind,
+ Text,
+} from "@react-email/components";
+import { UploadIcon } from "lucide-react";
+
+interface PapermarkYearInReviewEmailProps {
+ year: number;
+ minutesSpentOnDocs: number;
+ uploadedDocuments: number;
+ sharedLinks: number;
+ receivedViews: number;
+ topDocumentName: string;
+ topDocumentViews: number;
+ mostActiveMonth: string;
+ mostActiveMonthViews: number;
+ sharerPercentile: number;
+ viewingLocations: string[];
+ unsubscribeUrl: string;
+}
+
+export default function PapermarkYearInReviewEmail({
+ year = 2024,
+ minutesSpentOnDocs = 1234,
+ uploadedDocuments = 25,
+ sharedLinks = 50,
+ receivedViews = 500,
+ topDocumentName = "Q4 Financial Report",
+ topDocumentViews = 150,
+ mostActiveMonth = "September",
+ mostActiveMonthViews = 200,
+ sharerPercentile = 95,
+ viewingLocations = ["United States", "United Kingdom", "Germany", "Japan"],
+ unsubscribeUrl,
+}: PapermarkYearInReviewEmailProps) {
+ return (
+ <Html>
+ <Head />
+ <Preview>Your Year in Review with Papermark</Preview>
+ <Tailwind>
+ <Body className="bg-white font-sans">
+ <Container className="mx-auto w-full max-w-[600px] p-0">
+ <Section className="p-8 text-center">
+ <Text className="mx-0 mb-8 mt-4 p-0 text-center text-2xl font-normal">
+ <span className="font-bold tracking-tighter">Papermark</span>
+ </Text>
+ <Text className="text-sm font-normal uppercase tracking-wider">
+ {year} in review
+ </Text>
+ <Heading className="my-4 text-4xl font-medium leading-tight">
+ Your Year with Papermark
+ </Heading>
+ <Text className="mb-8 text-lg leading-8">
+ What a year it's been! Let's take a look at how you've used
+ Papermark to share your important documents and connect with
+ your audience.
+ </Text>
+ <Link
+ href="https://www.papermark.io"
+ className="inline-flex items-center rounded-full bg-gray-900 px-12 py-4 text-center text-sm font-bold text-white no-underline"
+ >
+ <svg
+ xmlns="http://www.w3.org/2000/svg"
+ width="24"
+ height="24"
+ viewBox="0 0 24 24"
+ fill="none"
+ stroke="currentColor"
+ stroke-width="2"
+ stroke-linecap="round"
+ stroke-linejoin="round"
+ className="lucide lucide-upload mr-2 h-4 w-4"
+ >
+ <path d="M21 15v4a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2v-4" />
+ <polyline points="17 8 12 3 7 8" />
+ <line x1="12" x2="12" y1="3" y2="15" />
+ </svg>
+ Share your stats
+ </Link>
+ </Section>
+
+ <Section className="my-6 rounded-2xl bg-[#fb7a00]/10 bg-[radial-gradient(circle_at_bottom_right,#fb7a00_0%,transparent_60%)] p-8 text-center">
+ <Heading className="m-0 text-3xl font-medium text-[#a63b00]">
+ Viewers spent
+ </Heading>
+ <Text className="my-4 text-7xl font-bold leading-none text-gray-900">
+ {minutesSpentOnDocs}
+ </Text>
+ <Text className="mb-4 text-3xl font-medium text-gray-900">
+ minutes on your documents
+ </Text>
+ <Text className="text-sm leading-5 text-gray-900">
+ That's a lot of engagement! Your documents are resonating with
+ your audience.
+ </Text>
+
+ <Hr className="mt-6" style={{ borderColor: "#fb7a00" }} />
+ <Heading className="pt-5 text-xs font-medium uppercase tracking-wider text-gray-900">
+ Your activity
+ </Heading>
+ <Row className="mt-5">
+ <Column className="w-1/3 text-center">
+ <Text className="text-sm font-medium text-[#a63b00]">
+ You uploaded
+ </Text>
+ <Text className="my-1 text-4xl font-bold text-gray-900">
+ {uploadedDocuments}
+ </Text>
+ <Text className="text-2xl text-gray-900">documents</Text>
+ </Column>
+ <Column className="w-1/3 text-center">
+ <Text className="text-sm font-medium text-[#a63b00]">
+ You shared
+ </Text>
+ <Text className="my-1 text-4xl font-bold text-gray-900">
+ {sharedLinks}
+ </Text>
+ <Text className="text-2xl text-gray-900">links</Text>
+ </Column>
+ <Column className="w-1/3 text-center">
+ <Text className="text-sm font-medium text-[#a63b00]">
+ You received
+ </Text>
+ <Text className="my-1 text-4xl font-bold text-gray-900">
+ {receivedViews}
+ </Text>
+ <Text className="text-2xl text-gray-900">views</Text>
+ </Column>
+ </Row>
+ </Section>
+
+ <Section className="my-6 rounded-2xl bg-[#4b5563]/10 bg-[radial-gradient(circle_at_bottom_right,#4b5563_0%,transparent_60%)] p-8 text-center">
+ <Heading className="m-0 text-3xl font-medium text-gray-800">
+ Your top document
+ </Heading>
+ <Text className="my-4 text-2xl font-bold leading-none text-gray-900">
+ "{topDocumentName}"
+ </Text>
+ <Text className="mb-4 text-5xl font-medium text-gray-900">
+ {topDocumentViews} views
+ </Text>
+ <Text className="text-sm leading-5 text-gray-900">
+ This document really caught your audience's attention!
+ </Text>
+ </Section>
+
+ <Section className="my-6 rounded-2xl bg-[#e4c5a0]/10 bg-[radial-gradient(circle_at_bottom_right,#e4c5a0_0%,transparent_60%)] p-8 text-center">
+ <Heading className="m-0 text-3xl font-medium text-[#9c7b4a]">
+ Your most active month
+ </Heading>
+ <Text className="my-4 text-5xl font-bold leading-none text-gray-900">
+ {mostActiveMonth}
+ </Text>
+ <Text className="mb-4 text-3xl font-medium text-gray-900">
+ with {mostActiveMonthViews} views
+ </Text>
+ <Text className="text-sm leading-5 text-gray-900">
+ {mostActiveMonth} was your busiest month. What did you share
+ that got so much attention?
+ </Text>
+
+ <Hr className="mt-6" style={{ borderColor: "#e4c5a0" }} />
+ <Heading className="pt-5 text-xs font-medium uppercase tracking-wider text-gray-900">
+ You're in the top
+ </Heading>
+ <Text className="my-4 text-7xl font-bold leading-none text-gray-900">
+ {sharerPercentile}%
+ </Text>
+ <Text className="mb-4 text-xl font-medium text-gray-900">
+ of sharers on Papermark
+ </Text>
+ <Text className="text-sm leading-5 text-gray-900">
+ You're one of our most active users. Thank you for sharing with
+ Papermark!
+ </Text>
+ </Section>
+
+ <Section className="my-6 rounded-2xl bg-[#10b981]/10 bg-[radial-gradient(circle_at_bottom_right,#10b981_0%,transparent_60%)] p-8 text-center">
+ <Heading className="m-0 text-3xl font-medium text-[#065f46]">
+ Your documents were viewed from
+ </Heading>
+ <Row className="mt-4">
+ <Column>
+ {viewingLocations.map((location, index) => (
+ <Text
+ key={index}
+ className="rounded-full bg-[#10b981] px-3 py-1 text-sm font-medium text-white"
+ style={{
+ margin: "4px 4px",
+ display: "inline-block",
+ }}
+ >
+ {location}
+ </Text>
+ ))}
+ </Column>
+ </Row>
+ <Text className="mt-4 text-sm leading-5 text-[#065f46]">
+ Your content is making waves across the globe!
+ </Text>
+ </Section>
+
+ <Section className="pb-6 text-center">
+ <Text className="text-xl leading-8 text-gray-900">
+ We're excited to support you in the coming year!
+ </Text>
+ <Link
+ href="https://www.papermark.io"
+ className="mt-4 inline-flex items-center rounded-full bg-gray-900 px-12 py-4 text-center text-sm font-bold text-white no-underline"
+ >
+ <svg
+ xmlns="http://www.w3.org/2000/svg"
+ width="24"
+ height="24"
+ viewBox="0 0 24 24"
+ fill="none"
+ stroke="currentColor"
+ stroke-width="2"
+ stroke-linecap="round"
+ stroke-linejoin="round"
+ className="lucide lucide-upload mr-2 h-4 w-4"
+ >
+ <path d="M21 15v4a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2v-4" />
+ <polyline points="17 8 12 3 7 8" />
+ <line x1="12" x2="12" y1="3" y2="15" />
+ </svg>
+ Share your stats
+ </Link>
+ <Link
+ href="https://www.papermark.io"
+ className="mt-4 block items-center text-center text-sm font-bold text-gray-900 no-underline"
+ >
+ Share on LinkedIn
+ </Link>
+ </Section>
+
+ <Hr />
+ <Section className="mt-8 text-gray-400">
+ <Text className="text-xs">
+ © {new Date().getFullYear()}{" "}
+ <a
+ href="https://www.papermark.io"
+ className="text-gray-400 no-underline visited:text-gray-400 hover:text-gray-400"
+ target="_blank"
+ >
+ papermark.io
+ </a>
+ </Text>
+ <Text className="text-xs">
+ You received this Year in Review email because you have an
+ account with Papermark during 2024. If you have any feedback or
+ questions about this email, simply reply to it. To unsubscribe
+ from future Year in Review emails,{" "}
+ <a
+ href={unsubscribeUrl}
+ className="text-gray-400 underline underline-offset-2 visited:text-gray-400 hover:text-gray-400"
+ >
+ click here
+ </a>
+ .
+ </Text>
+ </Section>
+ </Container>
+ </Body>
+ </Tailwind>
+ </Html>
+ );
+}
diff --git a/lib/year-in-review/calculate-percentile.ts b/lib/year-in-review/calculate-percentile.ts
new file mode 100644
index 00000000..6681f185
--- /dev/null
+++ b/lib/year-in-review/calculate-percentile.ts
@@ -0,0 +1,65 @@
+import prisma from "@/lib/prisma";
+
+// Function to get total views for all teams and calculate percentiles
+export async function calculateViewPercentile(teamTotalViews: number) {
+ const allTeamViews = await prisma.$queryRaw<Array<{ total_views: number }>>`
+ WITH TeamStats AS (
+ SELECT
+ "teamId",
+ CAST(stats->>'totalViews' AS INTEGER) as total_views
+ FROM "YearInReview"
+ WHERE stats->>'totalViews' IS NOT NULL
+ )
+ SELECT total_views
+ FROM TeamStats
+ ORDER BY total_views DESC
+ `;
+
+ // Convert to array of numbers and sort descending
+ const viewCounts = allTeamViews
+ .map((t) => t.total_views)
+ .sort((a, b) => b - a);
+ const totalTeams = viewCounts.length;
+
+ if (totalTeams === 0) return 100; // If no other teams, you're at the top
+
+ // Find position of current team
+ const position = viewCounts.findIndex((views) => views <= teamTotalViews) + 1;
+
+ // Calculate percentile (position / total * 100)
+ const percentile = (position / totalTeams) * 100;
+
+ // Map to predefined brackets
+ if (percentile <= 1) return 1;
+ if (percentile <= 3) return 3;
+ if (percentile <= 5) return 5;
+ if (percentile <= 10) return 10;
+ if (percentile <= 25) return 25;
+ if (percentile <= 50) return 50;
+ return 100;
+}
+
+// Updated getYearInReviewStats to include percentile
+export async function getYearInReviewStats(teamId: string) {
+ // Get the team's stats first
+ const stats = await prisma.yearInReview.findFirst({
+ where: { teamId },
+ select: { stats: true },
+ });
+
+ if (!stats?.stats) {
+ throw new Error("Stats not found for team");
+ }
+
+ const parsedStats = stats.stats as any;
+ const totalViews = parseInt(parsedStats.totalViews);
+
+ // Calculate the percentile
+ const sharerPercentile = await calculateViewPercentile(totalViews);
+
+ // Return stats with percentile
+ return {
+ ...parsedStats,
+ sharerPercentile,
+ };
+}
diff --git a/lib/year-in-review/get-stats.ts b/lib/year-in-review/get-stats.ts
new file mode 100644
index 00000000..24da15c3
--- /dev/null
+++ b/lib/year-in-review/get-stats.ts
@@ -0,0 +1,159 @@
+import { Tinybird } from "@chronark/zod-bird";
+import { Prisma } from "@prisma/client";
+import { z } from "zod";
+
+import prisma from "@/lib/prisma";
+
+import { COUNTRIES } from "../constants";
+
+export async function getYearInReviewStats(teamId: string) {
+ // Run all queries in parallel for a specific team
+ const [documents, teamCounts, mostViewedDoc, mostActiveMonth] =
+ await Promise.all([
+ prisma.document.findMany({
+ where: { teamId },
+ select: { id: true },
+ }),
+
+ // 1. Total documents uploaded in 2024
+ prisma.team.findUnique({
+ where: { id: teamId },
+ select: {
+ _count: {
+ select: {
+ documents: {
+ where: {
+ createdAt: {
+ gte: new Date("2024-01-01"),
+ lt: new Date("2024-12-01"),
+ },
+ },
+ },
+ links: {
+ where: {
+ createdAt: {
+ gte: new Date("2024-01-01"),
+ lt: new Date("2024-12-01"),
+ },
+ },
+ },
+ views: {
+ where: {
+ viewedAt: {
+ gte: new Date("2024-01-01"),
+ lt: new Date("2024-12-01"),
+ },
+ },
+ },
+ },
+ },
+ },
+ }),
+
+ // 2. Most viewed document
+ prisma.$queryRaw<
+ Array<{ documentId: string; documentName: string; viewCount: number }>
+ >(Prisma.sql`
+ WITH RankedDocuments AS (
+ SELECT
+ d."id" as "documentId",
+ d."name" as "documentName",
+ COUNT(v."id") as "viewCount",
+ ROW_NUMBER() OVER (ORDER BY COUNT(v."id") DESC) as rn
+ FROM "Document" d
+ LEFT JOIN "View" v ON v."documentId" = d."id"
+ WHERE
+ d."teamId" = ${teamId}
+ AND v."viewedAt" >= '2024-01-01'
+ AND v."viewedAt" < '2024-12-01'
+ GROUP BY d."id", d."name"
+ )
+ SELECT
+ "documentId",
+ "documentName",
+ "viewCount"
+ FROM RankedDocuments
+ WHERE rn = 1
+ LIMIT 1
+ `),
+
+ // 3. Most active month
+ prisma.$queryRaw<Array<{ month: Date; viewCount: number }>>(Prisma.sql`
+ WITH MonthlyViews AS (
+ SELECT
+ DATE_TRUNC('month', "viewedAt") as month,
+ COUNT(*) as "viewCount",
+ ROW_NUMBER() OVER (ORDER BY COUNT(*) DESC) as rn
+ FROM "View"
+ WHERE
+ "teamId" = ${teamId}
+ AND "viewedAt" >= '2024-01-01'
+ AND "viewedAt" < '2024-12-01'
+ GROUP BY DATE_TRUNC('month', "viewedAt")
+ )
+ SELECT month, "viewCount"
+ FROM MonthlyViews
+ WHERE rn = 1
+ LIMIT 1;
+ `),
+ ]);
+
+ // skip if no documents
+ if (documents.length === 0) {
+ return {
+ totalDocuments: teamCounts?._count.documents,
+ totalLinks: teamCounts?._count.links,
+ totalViews: teamCounts?._count.views,
+ };
+ }
+
+ // get total duration for all documents in the team
+ const tinybirdData = await getTotalDuration({
+ documentIds: documents.map((doc) => doc.id).join(","),
+ });
+
+ // write full name of countries
+ const uniqueCountries = tinybirdData.data[0].unique_countries.map(
+ (country) => {
+ // Map country codes to full names using COUNTRIES constant
+ return COUNTRIES[country] || country;
+ },
+ );
+
+ return {
+ totalDocuments: teamCounts?._count.documents,
+ totalLinks: teamCounts?._count.links,
+ totalViews: teamCounts?._count.views,
+ mostViewedDocument: mostViewedDoc[0]
+ ? {
+ documentId: mostViewedDoc[0].documentId,
+ documentName: mostViewedDoc[0].documentName,
+ viewCount: mostViewedDoc[0].viewCount,
+ }
+ : null,
+ mostActiveMonth: mostActiveMonth?.[0]
+ ? {
+ month: new Date(mostActiveMonth[0].month).toLocaleString("en-US", {
+ month: "long",
+ }),
+ viewCount: mostActiveMonth[0].viewCount,
+ }
+ : null,
+ totalDuration: tinybirdData.data[0].total_duration,
+ uniqueCountries: uniqueCountries,
+ };
+}
+
+const tb = new Tinybird({ token: process.env.TINYBIRD_TOKEN! });
+
+// tinybird pipe to get the total view duration for all documents in a team
+export const getTotalDuration = tb.buildPipe({
+ pipe: "get_total_team_duration__v1",
+ parameters: z.object({
+ documentIds: z.string().describe("Comma separated documentIds"),
+ }),
+ data: z.object({
+ total_duration: z.number(),
+ unique_countries: z.array(z.string()),
+ }),
+});
diff --git a/lib/year-in-review/index.ts b/lib/year-in-review/index.ts
new file mode 100644
index 00000000..bc6f5edb
--- /dev/null
+++ b/lib/year-in-review/index.ts
@@ -0,0 +1,56 @@
+import prisma from "@/lib/prisma";
+
+import { getYearInReviewStats } from "./get-stats";
+
+export async function initializeEmailQueue() {
+ const batchSize = 100; // Process teams in batches during initialization
+ let skip = 0;
+ let totalProcessed = 0;
+
+ while (true) {
+ // Get batch of teams
+ const teams = await prisma.team.findMany({
+ skip,
+ take: batchSize,
+ select: {
+ id: true,
+ name: true,
+ },
+ });
+
+ if (teams.length === 0) break;
+
+ // Process each team in the batch and filter out teams with no views
+ const jobData = (
+ await Promise.all(
+ teams.map(async (team) => {
+ const stats = await getYearInReviewStats(team.id);
+
+ // Skip teams with no views
+ if (stats.totalViews === 0) return null;
+
+ return {
+ teamId: team.id,
+ status: "pending",
+ stats: stats,
+ };
+ }),
+ )
+ ).filter((job): job is NonNullable<typeof job> => job !== null);
+
+ // Bulk create jobs with precomputed stats
+ await prisma.yearInReview.createMany({
+ data: jobData,
+ skipDuplicates: true,
+ });
+
+ totalProcessed += teams.length;
+ skip += batchSize;
+
+ console.log(`Processed ${totalProcessed} teams...`);
+ }
+
+ console.log(
+ `Completed creating ${totalProcessed} email jobs with precomputed stats`,
+ );
+}
diff --git a/lib/year-in-review/send-emails.ts b/lib/year-in-review/send-emails.ts
new file mode 100644
index 00000000..56188a65
--- /dev/null
+++ b/lib/year-in-review/send-emails.ts
@@ -0,0 +1,281 @@
+import { render } from "@react-email/components";
+import { nanoid } from "nanoid";
+
+import YearInReviewEmail from "@/components/emails/year-in-review-papermark";
+
+import prisma from "@/lib/prisma";
+import { resend } from "@/lib/resend";
+import { log } from "@/lib/utils";
+import { generateUnsubscribeUrl } from "@/lib/utils/unsubscribe";
+
+const BATCH_SIZE = 5; // Maximum number of emails Resend supports in one batch
+const MAX_ATTEMPTS = 3;
+const RATE_LIMIT_DELAY = 10000; // 10 seconds
+
+type YearReviewStats = {
+ totalDocuments: number;
+ totalLinks: number;
+ totalViews: number;
+ mostViewedDocument: {
+ documentId: string;
+ documentName: string;
+ viewCount: number;
+ };
+ mostActiveMonth: {
+ month: string;
+ viewCount: number;
+ };
+ totalDuration: number;
+ uniqueCountries: string[];
+ sharerPercentile: number;
+};
+
+type EmailWithMetadata = {
+ email: {
+ from: string;
+ to: string;
+ subject: string;
+ react: React.ReactElement;
+ text: string;
+ headers: {
+ "X-Entity-Ref-ID": string;
+ "List-Unsubscribe": string;
+ };
+ };
+ jobId: string;
+ teamId: string;
+ userId: string;
+};
+
+function msToMinutes(ms: number): number {
+ return Math.ceil(ms / 60000);
+}
+
+export async function processEmailQueue() {
+ if (!resend) return;
+ const jobs = await prisma.yearInReview.findMany({
+ where: {
+ AND: [{ status: "pending" }, { attempts: { lt: MAX_ATTEMPTS } }],
+ },
+ take: BATCH_SIZE,
+ orderBy: { createdAt: "asc" },
+ });
+
+ if (jobs.length === 0) return;
+
+ // Mark all jobs as processing
+ await prisma.yearInReview.updateMany({
+ where: {
+ id: {
+ in: jobs.map((job) => job.id),
+ },
+ },
+ data: {
+ status: "processing",
+ attempts: { increment: 1 },
+ lastAttempted: new Date(),
+ },
+ });
+
+ try {
+ // Fetch team data for all jobs in parallel
+ const teamsData = await Promise.all(
+ jobs.map(async (job) => {
+ const team = await prisma.team.findUnique({
+ where: { id: job.teamId },
+ include: {
+ users: {
+ where: { role: { in: ["ADMIN", "MANAGER"] } },
+ include: {
+ user: {
+ select: {
+ email: true,
+ },
+ },
+ },
+ },
+ },
+ });
+
+ return {
+ job,
+ team,
+ };
+ }),
+ );
+
+ // Prepare batch of emails
+ const emailsWithMetadata: EmailWithMetadata[] = (
+ await Promise.all(
+ teamsData
+ .filter(({ team }) => team !== null) // Filter out teams that weren't found
+ .flatMap(async ({ job, team }) => {
+ const stats = job.stats as YearReviewStats;
+
+ return Promise.all(
+ team!.users
+ .filter((userTeam) => userTeam.user.email)
+ .map(async (userTeam) => {
+ const unsubscribeUrl = generateUnsubscribeUrl({
+ viewerId: userTeam.userId,
+ teamId: team!.id,
+ });
+
+ const react = YearInReviewEmail({
+ year: 2024,
+ minutesSpentOnDocs: msToMinutes(stats.totalDuration),
+ uploadedDocuments: stats.totalDocuments,
+ sharedLinks: stats.totalLinks,
+ receivedViews: stats.totalViews,
+ topDocumentName: stats.mostViewedDocument.documentName,
+ topDocumentViews: stats.mostViewedDocument.viewCount,
+ mostActiveMonth: stats.mostActiveMonth.month,
+ mostActiveMonthViews: stats.mostActiveMonth.viewCount,
+ sharerPercentile: stats.sharerPercentile,
+ viewingLocations: stats.uniqueCountries,
+ unsubscribeUrl,
+ });
+
+ const plainText = await render(react, { plainText: true });
+
+ return {
+ email: {
+ from: "Papermark <[email protected]>",
+ to: userTeam.user.email || "[email protected]",
+ subject: "2024 in Review: Your Year with Papermark",
+ react,
+ text: plainText,
+ headers: {
+ "X-Entity-Ref-ID": nanoid(),
+ "List-Unsubscribe": unsubscribeUrl,
+ },
+ },
+ jobId: job.id,
+ teamId: team!.id,
+ userId: userTeam.userId,
+ };
+ }),
+ );
+ }),
+ )
+ ).flat();
+
+ // Process emails in batches
+ for (let i = 0; i < emailsWithMetadata.length; i += BATCH_SIZE) {
+ const batch = emailsWithMetadata.slice(i, i + BATCH_SIZE);
+
+ try {
+ // Extract just the email objects for the batch request
+ const emailBatch = batch.map((item) => item.email);
+ const { data, error } = await resend.batch.send(emailBatch);
+
+ // Track success/failure counts by job
+ const jobCounts = new Map<
+ string,
+ { success: number; failed: number }
+ >();
+
+ // Match results with metadata using array indices
+ if (data) {
+ data.data.forEach((result, index) => {
+ const metadata = batch[index];
+ const counts = jobCounts.get(metadata.jobId) || {
+ success: 0,
+ failed: 0,
+ };
+ counts.success++;
+ jobCounts.set(metadata.jobId, counts);
+ });
+ } else if (error) {
+ batch.forEach((metadata) => {
+ const counts = jobCounts.get(metadata.jobId) || {
+ success: 0,
+ failed: 0,
+ };
+ counts.failed++;
+ log({
+ message: `Failed to send email to ${metadata.email.to}: ${error.message}`,
+ type: "error",
+ mention: true,
+ });
+ jobCounts.set(metadata.jobId, counts);
+ });
+ }
+
+ // Update job statuses
+ for (const [jobId, counts] of jobCounts.entries()) {
+ const totalExpectedEmails =
+ teamsData
+ .find(({ job }) => job.id === jobId)
+ ?.team?.users.filter((ut) => ut.user.email).length || 0;
+
+ const job = jobs.find((j) => j.id === jobId);
+ if (!job) continue;
+
+ const status =
+ counts.failed === 0 && counts.success === totalExpectedEmails
+ ? "completed"
+ : job.attempts >= MAX_ATTEMPTS - 1
+ ? "failed"
+ : "pending";
+
+ await prisma.yearInReview.update({
+ where: { id: jobId },
+ data: {
+ status,
+ error:
+ counts.failed > 0
+ ? `Failed to send ${counts.failed} out of ${totalExpectedEmails} emails`
+ : null,
+ },
+ });
+ }
+
+ // Respect rate limit between batches
+ if (i + BATCH_SIZE < emailsWithMetadata.length) {
+ await new Promise((resolve) => setTimeout(resolve, RATE_LIMIT_DELAY));
+ }
+ } catch (error) {
+ log({
+ message: `Batch send failed: ${error instanceof Error ? error.message : "Unknown error"}`,
+ type: "error",
+ mention: true,
+ });
+
+ // If the entire batch fails, mark affected jobs for retry
+ const jobIds = new Set(batch.map((item) => item.jobId));
+
+ await prisma.yearInReview.updateMany({
+ where: {
+ id: {
+ in: Array.from(jobIds),
+ },
+ },
+ data: {
+ status: "pending",
+ error: error instanceof Error ? error.message : "Unknown error",
+ },
+ });
+ }
+ }
+ } catch (error) {
+ log({
+ message: `Email queue processing failed: ${error instanceof Error ? error.message : "Unknown error"}`,
+ type: "error",
+ mention: true,
+ });
+
+ // Reset all jobs to pending or failed
+ await prisma.yearInReview.updateMany({
+ where: {
+ id: {
+ in: jobs.map((job) => job.id),
+ },
+ },
+ data: {
+ status: "pending",
+ error: error instanceof Error ? error.message : "Unknown error",
+ },
+ });
+ }
+}
diff --git a/prisma/migrations/20241212000000_add_yir/migration.sql b/prisma/migrations/20241212000000_add_yir/migration.sql
new file mode 100644
index 00000000..fc056c12
--- /dev/null
+++ b/prisma/migrations/20241212000000_add_yir/migration.sql
@@ -0,0 +1,24 @@
+-- AlterTable
+ALTER TABLE "UserTeam" ADD COLUMN "notificationPreferences" JSONB;
+
+-- CreateTable
+CREATE TABLE "YearInReview" (
+ "id" TEXT NOT NULL,
+ "teamId" TEXT NOT NULL,
+ "status" TEXT NOT NULL DEFAULT 'pending',
+ "attempts" INTEGER NOT NULL DEFAULT 0,
+ "lastAttempted" TIMESTAMP(3),
+ "error" TEXT,
+ "stats" JSONB NOT NULL,
+ "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
+ "updatedAt" TIMESTAMP(3) NOT NULL,
+
+ CONSTRAINT "YearInReview_pkey" PRIMARY KEY ("id")
+);
+
+-- CreateIndex
+CREATE INDEX "YearInReview_status_attempts_idx" ON "YearInReview"("status", "attempts");
+
+-- CreateIndex
+CREATE INDEX "YearInReview_teamId_idx" ON "YearInReview"("teamId");
+
diff --git a/prisma/schema.prisma b/prisma/schema.prisma
index 22a2daf1..d3051e8d 100644
--- a/prisma/schema.prisma
+++ b/prisma/schema.prisma
@@ -120,6 +120,8 @@ model UserTeam {
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
team Team @relation(fields: [teamId], references: [id], onDelete: Cascade)
+ notificationPreferences Json? // Format: { yearInReview: { "enabled": false } }
+
@@id([userId, teamId])
@@index([userId])
@@index([teamId])
@@ -708,3 +710,21 @@ model Webhook {
@@index([teamId])
}
+
+
+model YearInReview {
+ id String @id @default(cuid())
+ teamId String
+ status String @default("pending") // pending, processing, completed, failed
+ attempts Int @default(0)
+ lastAttempted DateTime?
+ error String?
+
+ stats Json
+
+ createdAt DateTime @default(now())
+ updatedAt DateTime @updatedAt
+
+ @@index([status, attempts])
+ @@index([teamId])
+}