Files
kavehhn 192c595c68 feat: add password change, class unenroll, and optional notify flags
Let admins skip SMS on user, class, and invoice actions, and let users change their own password with the current one.
2026-08-15 23:48:18 +03:30

50 lines
1.2 KiB
JavaScript

// /utils/notifyFlags.js
'use strict';
const NOTIFY_FIELD_KEYS = [
'notify',
'notifySms',
'notifyEmail',
'notifyBot',
'sendSms',
'sendEmail',
'sendBot'
];
const toFlag = (value, fallback = true) => {
if (value === undefined || value === null || value === '') return fallback;
if (typeof value === 'boolean') return value;
if (typeof value === 'number') return value !== 0;
if (typeof value === 'string') {
const normalized = value.trim().toLowerCase();
if (['false', '0', 'no', 'off'].includes(normalized)) return false;
if (['true', '1', 'yes', 'on'].includes(normalized)) return true;
}
return fallback;
};
const pickNotifyFlags = (source = {}) => {
const nested = source && typeof source.notify === 'object' && source.notify !== null
? source.notify
: {};
return {
sms: toFlag(nested.sms ?? source.notifySms ?? source.sendSms, true),
email: toFlag(nested.email ?? source.notifyEmail ?? source.sendEmail, true),
bot: toFlag(nested.bot ?? source.notifyBot ?? source.sendBot, true)
};
};
const omitNotifyFields = (source = {}) => {
const next = { ...source };
NOTIFY_FIELD_KEYS.forEach((key) => {
delete next[key];
});
return next;
};
module.exports = {
pickNotifyFlags,
omitNotifyFields
};