Files
gameno-api/utils/messagingChannels.js
kavehhn 72be01fee3 feat: add messaging toggles, password reset SMS, and payment discounts
Allow SuperAdmin to disable SMS, email, and bot from dashboard settings on top of env flags. Add admin password reset with credentials SMS, plus payment discounts, notes, and payable amount handling.
2026-08-16 06:58:08 +03:30

86 lines
2.2 KiB
JavaScript

'use strict';
const CHANNELS = ['sms', 'email', 'bot'];
const ENV_KEYS = {
sms: 'SMS_ENABLED',
email: 'EMAIL_ENABLED',
bot: 'BOT_ENABLED'
};
const DB_KEYS = {
sms: 'smsEnabled',
email: 'emailEnabled',
bot: 'botEnabled'
};
const toBoolean = (value, fallback = false) => {
if (typeof value === 'boolean') return value;
if (value === undefined || value === null || value === '') return fallback;
if (typeof value === 'number') return value !== 0;
const normalized = String(value).trim().toLowerCase();
if (['true', '1', 'yes', 'on'].includes(normalized)) return true;
if (['false', '0', 'no', 'off'].includes(normalized)) return false;
return fallback;
};
const isEnvChannelEnabled = (channel, envConfig = {}) => {
const key = ENV_KEYS[channel];
if (!key) return false;
return toBoolean(envConfig[key], false);
};
const isDbChannelEnabled = (channel, stored = {}) => {
const key = DB_KEYS[channel];
if (!key) return false;
const value = stored == null ? undefined : stored[key];
if (value === undefined || value === null || value === '') return true;
return toBoolean(value, true);
};
const isChannelEnabled = (channel, { env = {}, db = {} } = {}) => {
return isEnvChannelEnabled(channel, env) && isDbChannelEnabled(channel, db);
};
const toPublicMessaging = (stored = {}, envConfig = {}) => {
const messaging = {};
const env = {};
CHANNELS.forEach((channel) => {
messaging[DB_KEYS[channel]] = isDbChannelEnabled(channel, stored);
env[DB_KEYS[channel]] = isEnvChannelEnabled(channel, envConfig);
});
messaging.env = env;
return messaging;
};
const parseIncomingMessaging = (body = {}) => {
const source = body.messaging && typeof body.messaging === 'object'
? body.messaging
: body;
const result = {};
let hasAny = false;
CHANNELS.forEach((channel) => {
const key = DB_KEYS[channel];
if (!Object.prototype.hasOwnProperty.call(source, key) || source[key] === undefined) {
return;
}
result[key] = toBoolean(source[key], true);
hasAny = true;
});
return hasAny ? result : null;
};
module.exports = {
CHANNELS,
ENV_KEYS,
DB_KEYS,
toBoolean,
isEnvChannelEnabled,
isDbChannelEnabled,
isChannelEnabled,
toPublicMessaging,
parseIncomingMessaging
};