Files
Epicure/apps/web/lib/notification-prefs.ts
Arnaud c5e1643d39 feat: per-category email prefs, admin ops webhooks, per-user feature toggles (v0.61.0)
- Notification email preferences: every push category (follow, comment,
  reply, reaction, rating, mention, leftoverExpiring, shoppingList) now has
  an independent email toggle, plus a Weekly Digest toggle. Previously email
  sent unconditionally whenever the recipient had one; now gated the same
  way push already was. The weekly-digest cron route excludes opted-out
  users.

- Admin-only site-wide webhooks (Admin → Webhooks): new signups, support
  tickets, and reports filed can now fire an HMAC-signed HTTP webhook
  (Slack/Discord/ops alerting), independent of the existing per-user
  webhooks (which stay scoped to a user's own recipe/meal-plan/shopping-list
  events). Signing/delivery logic factored into lib/webhook-delivery.ts and
  shared by both dispatchers instead of duplicated.

- Settings → Features: users can hide Nutrition, Pantry, Meal Plan,
  Shopping Lists, Collections, or Messages from their own nav. Purely
  cosmetic — hidden pages stay reachable by direct link, nothing is
  access-restricted.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-07-20 23:07:28 +02:00

52 lines
2.5 KiB
TypeScript

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<NotificationPrefs> {
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<boolean> {
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<boolean> {
const prefs = await getNotificationPrefs(userId);
return prefs[`${category}Email`];
}
export async function isWeeklyDigestEnabled(userId: string): Promise<boolean> {
const prefs = await getNotificationPrefs(userId);
return prefs.weeklyDigestEmail;
}