File size: 4,300 Bytes
b784929 0b1d3dc b784929 0b1d3dc 8c5d98c 0b1d3dc b784929 0b1d3dc b784929 0b1d3dc b784929 0b1d3dc b784929 0b1d3dc b784929 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 | 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: 'Send an SMS and 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,
};
|