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.
57 lines
1.4 KiB
JavaScript
57 lines
1.4 KiB
JavaScript
'use strict';
|
|
|
|
const config = require('../../config/config');
|
|
const logger = require('../../utils/logger');
|
|
const { Setting, SETTINGS_KEY } = require('./settingModel');
|
|
const {
|
|
isChannelEnabled,
|
|
isEnvChannelEnabled,
|
|
toPublicMessaging
|
|
} = require('../../utils/messagingChannels');
|
|
|
|
let cache = null;
|
|
let cacheAt = 0;
|
|
const CACHE_TTL_MS = 10000;
|
|
|
|
const readDbFlags = (doc) => ({
|
|
smsEnabled: doc?.smsEnabled,
|
|
emailEnabled: doc?.emailEnabled,
|
|
botEnabled: doc?.botEnabled
|
|
});
|
|
|
|
const getDbMessagingFlags = async () => {
|
|
const now = Date.now();
|
|
if (cache && (now - cacheAt) < CACHE_TTL_MS) return cache;
|
|
const doc = await Setting.findOne({ key: SETTINGS_KEY })
|
|
.select('smsEnabled emailEnabled botEnabled')
|
|
.lean();
|
|
cache = readDbFlags(doc);
|
|
cacheAt = now;
|
|
return cache;
|
|
};
|
|
|
|
const invalidateMessagingCache = () => {
|
|
cache = null;
|
|
cacheAt = 0;
|
|
};
|
|
|
|
const isMessagingChannelEnabled = async (channel) => {
|
|
if (!isEnvChannelEnabled(channel, config)) return false;
|
|
try {
|
|
const db = await getDbMessagingFlags();
|
|
return isChannelEnabled(channel, { env: config, db });
|
|
} catch (err) {
|
|
logger.warn(`[Messaging] Failed to read settings for ${channel}, using env only: ${err.message}`);
|
|
return true;
|
|
}
|
|
};
|
|
|
|
const getPublicMessaging = (doc) => toPublicMessaging(readDbFlags(doc), config);
|
|
|
|
module.exports = {
|
|
isMessagingChannelEnabled,
|
|
invalidateMessagingCache,
|
|
getPublicMessaging,
|
|
getDbMessagingFlags
|
|
};
|