import { db, userNotificationPrefs, eq } from "@epicure/db"; export type NotificationCategory = | "follow" | "comment" | "reply" | "reaction" | "rating" | "mention" | "leftoverExpiring" | "shoppingList"; export type NotificationPrefs = { follow: boolean; comment: boolean; reply: boolean; reaction: boolean; rating: boolean; mention: boolean; leftoverExpiring: boolean; shoppingList: boolean; followEmail: boolean; commentEmail: boolean; replyEmail: boolean; reactionEmail: boolean; ratingEmail: boolean; mentionEmail: boolean; leftoverExpiringEmail: boolean; shoppingListEmail: boolean; weeklyDigestEmail: boolean; }; const DEFAULT_PREFS: NotificationPrefs = { follow: true, comment: true, reply: true, reaction: true, rating: true, mention: true, leftoverExpiring: true, shoppingList: true, followEmail: true, commentEmail: true, replyEmail: true, reactionEmail: true, ratingEmail: true, mentionEmail: true, leftoverExpiringEmail: true, shoppingListEmail: true, weeklyDigestEmail: true, }; export async function getNotificationPrefs(userId: string): Promise { const row = await db.query.userNotificationPrefs.findFirst({ where: eq(userNotificationPrefs.userId, userId) }); if (!row) return { ...DEFAULT_PREFS }; return { follow: row.follow, comment: row.comment, reply: row.reply, reaction: row.reaction, rating: row.rating, mention: row.mention, leftoverExpiring: row.leftoverExpiring, shoppingList: row.shoppingList, followEmail: row.followEmail, commentEmail: row.commentEmail, replyEmail: row.replyEmail, reactionEmail: row.reactionEmail, ratingEmail: row.ratingEmail, mentionEmail: row.mentionEmail, leftoverExpiringEmail: row.leftoverExpiringEmail, shoppingListEmail: row.shoppingListEmail, weeklyDigestEmail: row.weeklyDigestEmail, }; } /** No row yet means every category defaults to on — same default the DB columns encode. */ export async function isNotificationCategoryEnabled(userId: string, category: NotificationCategory): Promise { const prefs = await getNotificationPrefs(userId); return prefs[category]; } /** Same as isNotificationCategoryEnabled, but for the email variant of the category. */ export async function isEmailCategoryEnabled(userId: string, category: NotificationCategory): Promise { const prefs = await getNotificationPrefs(userId); return prefs[`${category}Email`]; } export async function isWeeklyDigestEnabled(userId: string): Promise { const prefs = await getNotificationPrefs(userId); return prefs.weeklyDigestEmail; }