4d5269aced
New Settings → Notifications section with a toggle per category (follow, comment, reply, reaction, rating, mention, leftover-expiring, shared shopping list) — previously it was all-or-nothing (browser permission only). userNotificationPrefs (one row per user, defaults all-on so existing users see no behavior change until they opt out of something). Gated the push send in the three places that dispatch one: lib/notifications.ts (the 6 in-app notification types), the leftover-expiry cron, and shopping-list-notify — in-app notification-center entries and email are unaffected, this only gates the push itself, matching the literal ask. Verified locally: GET/PUT round-trips correctly, disabling a category and confirming the settings page renders all 8 toggles.
28 lines
1.2 KiB
TypeScript
28 lines
1.2 KiB
TypeScript
import { db, userNotificationPrefs, eq } from "@epicure/db";
|
|
|
|
export type NotificationCategory =
|
|
| "follow" | "comment" | "reply" | "reaction" | "rating" | "mention"
|
|
| "leftoverExpiring" | "shoppingList";
|
|
|
|
export type NotificationPrefs = Record<NotificationCategory, boolean>;
|
|
|
|
const DEFAULT_PREFS: NotificationPrefs = {
|
|
follow: true, comment: true, reply: true, reaction: true, rating: true, mention: true,
|
|
leftoverExpiring: true, shoppingList: 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,
|
|
};
|
|
}
|
|
|
|
/** 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];
|
|
}
|