WifiBiz / src /utils /notificationPreferences.js
Mbonea's picture
Add alert notification preferences
0b1d3dc
const db = require('../config/db');
const DEFAULTS = {
device_renewal: true,
device_suspended: true,
device_paused: true,
billing_reminder: true,
payout_completed: true,
payout_failed: true,
trial_granted: true,
purchase_alerts: true,
system_alerts: true,
};
const OPTION_DEFS = [
{
key: 'device_renewal',
label: 'Device renewal',
description: 'Sent when a device subscription payment succeeds and the device is renewed.',
default_enabled: true,
},
{
key: 'device_suspended',
label: 'Device suspended',
description: 'Sent when a device is suspended because billing expired or admin suspended it.',
default_enabled: true,
},
{
key: 'device_paused',
label: 'Device paused',
description: 'Sent when you manually pause a device from the dashboard.',
default_enabled: true,
},
{
key: 'billing_reminder',
label: 'Billing reminders',
description: 'Sent before a device trial or subscription is about to expire.',
default_enabled: true,
},
{
key: 'payout_completed',
label: 'Payout completed',
description: 'Sent when a payout to your phone succeeds.',
default_enabled: true,
},
{
key: 'payout_failed',
label: 'Payout failed',
description: 'Sent when a payout fails and your balance is restored.',
default_enabled: true,
},
{
key: 'trial_granted',
label: 'Trial granted',
description: 'Sent when an admin grants a trial period to one of your devices.',
default_enabled: true,
},
{
key: 'purchase_alerts',
label: 'Purchase alerts',
description: 'Show an in-app alert when a guest buys a WiFi bundle.',
default_enabled: true,
},
{
key: 'system_alerts',
label: 'System alerts',
description: 'Show in-app device and controller alerts from Omada.',
default_enabled: true,
},
];
function normalizeRow(row) {
const prefs = {};
for (const [key, defaultValue] of Object.entries(DEFAULTS)) {
prefs[key] = row ? Boolean(row[key]) : defaultValue;
}
return prefs;
}
async function getNotificationPreferences(clientId) {
const row = await db.queryOne(
`SELECT client_id, device_renewal, device_suspended, device_paused,
billing_reminder, payout_completed, payout_failed, trial_granted,
purchase_alerts, system_alerts
FROM client_notification_preferences
WHERE client_id = ?`,
[clientId]
);
return normalizeRow(row);
}
async function upsertNotificationPreferences(clientId, input) {
const merged = { ...(await getNotificationPreferences(clientId)), ...input };
await db.query(
`INSERT INTO client_notification_preferences
(client_id, device_renewal, device_suspended, device_paused,
billing_reminder, payout_completed, payout_failed, trial_granted,
purchase_alerts, system_alerts)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
ON DUPLICATE KEY UPDATE
device_renewal = VALUES(device_renewal),
device_suspended = VALUES(device_suspended),
device_paused = VALUES(device_paused),
billing_reminder = VALUES(billing_reminder),
payout_completed = VALUES(payout_completed),
payout_failed = VALUES(payout_failed),
trial_granted = VALUES(trial_granted),
purchase_alerts = VALUES(purchase_alerts),
system_alerts = VALUES(system_alerts)`,
[
clientId,
merged.device_renewal ? 1 : 0,
merged.device_suspended ? 1 : 0,
merged.device_paused ? 1 : 0,
merged.billing_reminder ? 1 : 0,
merged.payout_completed ? 1 : 0,
merged.payout_failed ? 1 : 0,
merged.trial_granted ? 1 : 0,
merged.purchase_alerts ? 1 : 0,
merged.system_alerts ? 1 : 0,
]
);
return getNotificationPreferences(clientId);
}
async function canSendTenantNotification(clientId, key) {
const prefs = await getNotificationPreferences(clientId);
return prefs[key] ?? true;
}
function serializePreferenceOptions(preferences) {
return OPTION_DEFS.map(option => ({
...option,
enabled: preferences[option.key] ?? option.default_enabled,
}));
}
module.exports = {
DEFAULTS,
OPTION_DEFS,
getNotificationPreferences,
upsertNotificationPreferences,
canSendTenantNotification,
serializePreferenceOptions,
};