feat: add granular notification settings for email and Shoutrrr reminders
This commit is contained in:
@@ -14,6 +14,11 @@ type SettingsBody = {
|
||||
highStockDays: number;
|
||||
shoutrrrEnabled: boolean;
|
||||
shoutrrrUrl: string;
|
||||
// Granular notification settings
|
||||
emailStockReminders: boolean;
|
||||
emailIntakeReminders: boolean;
|
||||
shoutrrrStockReminders: boolean;
|
||||
shoutrrrIntakeReminders: boolean;
|
||||
};
|
||||
|
||||
type TestEmailBody = {
|
||||
@@ -38,6 +43,11 @@ type NotificationSettings = {
|
||||
highStockDays: number;
|
||||
shoutrrrEnabled: boolean;
|
||||
shoutrrrUrl: string;
|
||||
// Granular notification settings
|
||||
emailStockReminders: boolean;
|
||||
emailIntakeReminders: boolean;
|
||||
shoutrrrStockReminders: boolean;
|
||||
shoutrrrIntakeReminders: boolean;
|
||||
};
|
||||
|
||||
function loadNotificationSettings(): NotificationSettings {
|
||||
@@ -54,12 +64,31 @@ function loadNotificationSettings(): NotificationSettings {
|
||||
highStockDays: saved.highStockDays ?? 180,
|
||||
shoutrrrEnabled: saved.shoutrrrEnabled ?? false,
|
||||
shoutrrrUrl: saved.shoutrrrUrl ?? "",
|
||||
// Granular notification settings (default to true for backwards compatibility)
|
||||
emailStockReminders: saved.emailStockReminders ?? true,
|
||||
emailIntakeReminders: saved.emailIntakeReminders ?? true,
|
||||
shoutrrrStockReminders: saved.shoutrrrStockReminders ?? true,
|
||||
shoutrrrIntakeReminders: saved.shoutrrrIntakeReminders ?? true,
|
||||
};
|
||||
}
|
||||
} catch {
|
||||
// ignore
|
||||
}
|
||||
return { emailEnabled: false, notificationEmail: "", reminderDaysBefore: 7, repeatDailyReminders: false, lowStockDays: 30, normalStockDays: 90, highStockDays: 180, shoutrrrEnabled: false, shoutrrrUrl: "" };
|
||||
return {
|
||||
emailEnabled: false,
|
||||
notificationEmail: "",
|
||||
reminderDaysBefore: 7,
|
||||
repeatDailyReminders: false,
|
||||
lowStockDays: 30,
|
||||
normalStockDays: 90,
|
||||
highStockDays: 180,
|
||||
shoutrrrEnabled: false,
|
||||
shoutrrrUrl: "",
|
||||
emailStockReminders: true,
|
||||
emailIntakeReminders: true,
|
||||
shoutrrrStockReminders: true,
|
||||
shoutrrrIntakeReminders: true,
|
||||
};
|
||||
}
|
||||
|
||||
function saveNotificationSettings(settings: NotificationSettings): void {
|
||||
@@ -86,6 +115,11 @@ export async function settingsRoutes(app: FastifyInstance) {
|
||||
highStockDays: notification.highStockDays,
|
||||
shoutrrrEnabled: notification.shoutrrrEnabled,
|
||||
shoutrrrUrl: notification.shoutrrrUrl,
|
||||
// Granular notification settings
|
||||
emailStockReminders: notification.emailStockReminders,
|
||||
emailIntakeReminders: notification.emailIntakeReminders,
|
||||
shoutrrrStockReminders: notification.shoutrrrStockReminders,
|
||||
shoutrrrIntakeReminders: notification.shoutrrrIntakeReminders,
|
||||
// SMTP settings (admin-configured, from .env)
|
||||
smtpHost: process.env.SMTP_HOST ?? "",
|
||||
smtpPort: parseInt(process.env.SMTP_PORT ?? "587"),
|
||||
@@ -114,6 +148,11 @@ export async function settingsRoutes(app: FastifyInstance) {
|
||||
highStockDays: body.highStockDays ?? 180,
|
||||
shoutrrrEnabled: body.shoutrrrEnabled ?? false,
|
||||
shoutrrrUrl: body.shoutrrrUrl ?? "",
|
||||
// Granular notification settings
|
||||
emailStockReminders: body.emailStockReminders ?? true,
|
||||
emailIntakeReminders: body.emailIntakeReminders ?? true,
|
||||
shoutrrrStockReminders: body.shoutrrrStockReminders ?? true,
|
||||
shoutrrrIntakeReminders: body.shoutrrrIntakeReminders ?? true,
|
||||
});
|
||||
|
||||
return reply.send({ success: true });
|
||||
|
||||
@@ -202,12 +202,12 @@ MedAssist Medication Planner`;
|
||||
async function checkAndSendIntakeReminders(logger: { info: (msg: string) => void; error: (msg: string) => void }): Promise<void> {
|
||||
const settings = loadNotificationSettings();
|
||||
|
||||
// Check if any notifications are enabled
|
||||
const emailEnabled = settings.emailEnabled && settings.notificationEmail;
|
||||
const shoutrrrEnabled = settings.shoutrrrEnabled && settings.shoutrrrUrl;
|
||||
// Check if any intake reminder notifications are enabled (granular check)
|
||||
const emailEnabled = settings.emailEnabled && settings.notificationEmail && settings.emailIntakeReminders;
|
||||
const shoutrrrEnabled = settings.shoutrrrEnabled && settings.shoutrrrUrl && settings.shoutrrrIntakeReminders;
|
||||
|
||||
if (!emailEnabled && !shoutrrrEnabled) {
|
||||
return; // No notifications enabled, skip silently
|
||||
return; // No intake reminder notifications enabled, skip silently
|
||||
}
|
||||
|
||||
// Get all medications with intake reminders enabled
|
||||
@@ -247,7 +247,7 @@ async function checkAndSendIntakeReminders(logger: { info: (msg: string) => void
|
||||
let emailSuccess = false;
|
||||
let shoutrrrSuccess = false;
|
||||
|
||||
// Send email if enabled
|
||||
// Send email if enabled for intake reminders
|
||||
if (emailEnabled) {
|
||||
const result = await sendIntakeReminderEmail(settings.notificationEmail, newReminders);
|
||||
emailSuccess = result.success;
|
||||
@@ -258,7 +258,7 @@ async function checkAndSendIntakeReminders(logger: { info: (msg: string) => void
|
||||
}
|
||||
}
|
||||
|
||||
// Send Shoutrrr notification if enabled
|
||||
// Send Shoutrrr notification if enabled for intake reminders
|
||||
if (shoutrrrEnabled) {
|
||||
const title = `Medication Reminder in ${REMINDER_MINUTES_BEFORE} min`;
|
||||
const message = newReminders
|
||||
|
||||
@@ -17,6 +17,11 @@ type NotificationSettings = {
|
||||
highStockDays: number;
|
||||
shoutrrrEnabled: boolean;
|
||||
shoutrrrUrl: string;
|
||||
// Granular notification settings
|
||||
emailStockReminders: boolean;
|
||||
emailIntakeReminders: boolean;
|
||||
shoutrrrStockReminders: boolean;
|
||||
shoutrrrIntakeReminders: boolean;
|
||||
};
|
||||
|
||||
type ReminderState = {
|
||||
@@ -340,12 +345,12 @@ Automatic reminder from MedAssist`;
|
||||
async function checkAndSendReminder(logger: { info: (msg: string) => void; error: (msg: string) => void }): Promise<void> {
|
||||
const settings = loadNotificationSettings();
|
||||
|
||||
// Check if any notifications are enabled
|
||||
const emailEnabled = settings.emailEnabled && settings.notificationEmail;
|
||||
const shoutrrrEnabled = settings.shoutrrrEnabled && settings.shoutrrrUrl;
|
||||
// Check if any stock reminder notifications are enabled (granular check)
|
||||
const emailEnabled = settings.emailEnabled && settings.notificationEmail && settings.emailStockReminders;
|
||||
const shoutrrrEnabled = settings.shoutrrrEnabled && settings.shoutrrrUrl && settings.shoutrrrStockReminders;
|
||||
|
||||
if (!emailEnabled && !shoutrrrEnabled) {
|
||||
logger.info("[Reminder] No notifications enabled");
|
||||
logger.info("[Reminder] No stock reminder notifications enabled");
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user