Papermark is the open-source DocSend alternative and secure data rooms with built-in analytics and custom domains.
feat: add notification resolve-recipients, queue-digest, and dispatch utilities
Cursor Agent17d2d015b338
diff --git a/lib/notifications/dispatch.ts b/lib/notifications/dispatch.tsnew file mode 100644index 00000000..245763e9--- /dev/null+++ b/lib/notifications/dispatch.ts@@ -0,0 +1,57 @@+import type { TeamNotificationType } from "@/lib/zod/schemas/notifications";++import { queueDigestNotifications } from "./queue-digest";+import {+ type NotificationRecipient,+ resolveRecipients,+} from "./resolve-recipients";++export type DispatchResult = {+ immediate: NotificationRecipient[];+ queued: NotificationRecipient[];+};++export async function dispatchNotification({+ teamId,+ notificationType,+ linkOwnerId,+ documentOwnerId,+ digestPayload,+}: {+ teamId: string;+ notificationType: TeamNotificationType;+ linkOwnerId?: string | null;+ documentOwnerId?: string | null;+ digestPayload: Record<string, unknown>;+}): Promise<DispatchResult> {+ const recipients = await resolveRecipients({+ teamId,+ notificationType,+ linkOwnerId,+ documentOwnerId,+ });++ const immediate: NotificationRecipient[] = [];+ const queued: NotificationRecipient[] = [];++ for (const recipient of recipients) {+ if (recipient.frequency === "IMMEDIATE") {+ immediate.push(recipient);+ } else {+ queued.push(recipient);+ }+ }++ if (queued.length > 0) {+ await queueDigestNotifications(+ queued.map((r) => ({+ userId: r.userId,+ teamId,+ type: notificationType,+ payload: digestPayload,+ })),+ );+ }++ return { immediate, queued };+}diff --git a/lib/notifications/queue-digest.ts b/lib/notifications/queue-digest.tsnew file mode 100644index 00000000..6ce0c1e0--- /dev/null+++ b/lib/notifications/queue-digest.ts@@ -0,0 +1,43 @@+import prisma from "@/lib/prisma";+import type { TeamNotificationType } from "@/lib/zod/schemas/notifications";++export async function queueDigestNotification({+ userId,+ teamId,+ type,+ payload,+}: {+ userId: string;+ teamId: string;+ type: TeamNotificationType;+ payload: Record<string, unknown>;+}) {+ await prisma.notificationDigest.create({+ data: {+ userId,+ teamId,+ type,+ payload,+ },+ });+}++export async function queueDigestNotifications(+ items: {+ userId: string;+ teamId: string;+ type: TeamNotificationType;+ payload: Record<string, unknown>;+ }[],+) {+ if (items.length === 0) return;++ await prisma.notificationDigest.createMany({+ data: items.map((item) => ({+ userId: item.userId,+ teamId: item.teamId,+ type: item.type,+ payload: item.payload,+ })),+ });+}diff --git a/lib/notifications/resolve-recipients.ts b/lib/notifications/resolve-recipients.tsnew file mode 100644index 00000000..202c678c--- /dev/null+++ b/lib/notifications/resolve-recipients.ts@@ -0,0 +1,85 @@+import { Role } from "@prisma/client";++import prisma from "@/lib/prisma";+import {+ DEFAULT_ADMIN_PREFERENCES,+ DEFAULT_MEMBER_PREFERENCES,+ type TeamNotificationFrequency,+ type TeamNotificationType,+} from "@/lib/zod/schemas/notifications";++export type NotificationRecipient = {+ userId: string;+ email: string;+ frequency: TeamNotificationFrequency;+ role: Role;+};++export async function resolveRecipients({+ teamId,+ notificationType,+ linkOwnerId,+ documentOwnerId,+}: {+ teamId: string;+ notificationType: TeamNotificationType;+ linkOwnerId?: string | null;+ documentOwnerId?: string | null;+}): Promise<NotificationRecipient[]> {+ const teamMembers = await prisma.userTeam.findMany({+ where: {+ teamId,+ status: "ACTIVE",+ },+ select: {+ userId: true,+ role: true,+ user: {+ select: {+ email: true,+ },+ },+ },+ });++ const ownerIds = new Set(+ [linkOwnerId, documentOwnerId].filter(Boolean) as string[],+ );++ const eligibleMembers = teamMembers.filter((member) => {+ if (member.role === "ADMIN" || member.role === "MANAGER") return true;+ return ownerIds.has(member.userId);+ });++ if (eligibleMembers.length === 0) return [];++ const userIds = eligibleMembers.map((m) => m.userId);+ const preferences = await prisma.notificationPreference.findMany({+ where: {+ userId: { in: userIds },+ teamId,+ type: notificationType,+ },+ });++ const prefMap = new Map(preferences.map((p) => [p.userId, p.frequency]));++ return eligibleMembers+ .map((member) => {+ const storedFrequency = prefMap.get(member.userId);+ const defaultPrefs =+ member.role === "MEMBER"+ ? DEFAULT_MEMBER_PREFERENCES+ : DEFAULT_ADMIN_PREFERENCES;+ const frequency = (storedFrequency ??+ defaultPrefs[notificationType]) as TeamNotificationFrequency;++ return {+ userId: member.userId,+ email: member.user.email!,+ frequency,+ role: member.role,+ };+ })+ .filter((r) => r.email && r.frequency !== "NEVER");+}