fix: clarify why SMS delivery is skipped when a channel gate is off
Surface env vs dashboard disable reasons in SMS logs so password reset and other sends are easier to diagnose when only one layer is enabled.
This commit is contained in:
@@ -4,7 +4,7 @@ const config = require('../../config/config');
|
|||||||
const logger = require('../../utils/logger');
|
const logger = require('../../utils/logger');
|
||||||
const { Setting, SETTINGS_KEY } = require('./settingModel');
|
const { Setting, SETTINGS_KEY } = require('./settingModel');
|
||||||
const {
|
const {
|
||||||
isChannelEnabled,
|
isDbChannelEnabled,
|
||||||
isEnvChannelEnabled,
|
isEnvChannelEnabled,
|
||||||
toPublicMessaging
|
toPublicMessaging
|
||||||
} = require('../../utils/messagingChannels');
|
} = require('../../utils/messagingChannels');
|
||||||
@@ -35,20 +35,46 @@ const invalidateMessagingCache = () => {
|
|||||||
cacheAt = 0;
|
cacheAt = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
const isMessagingChannelEnabled = async (channel) => {
|
const getMessagingChannelState = async (channel) => {
|
||||||
if (!isEnvChannelEnabled(channel, config)) return false;
|
const envEnabled = isEnvChannelEnabled(channel, config);
|
||||||
|
if (!envEnabled) {
|
||||||
|
return {
|
||||||
|
enabled: false,
|
||||||
|
envEnabled: false,
|
||||||
|
dbEnabled: null,
|
||||||
|
blockReason: 'env_disabled'
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const db = await getDbMessagingFlags();
|
const db = await getDbMessagingFlags();
|
||||||
return isChannelEnabled(channel, { env: config, db });
|
const dbEnabled = isDbChannelEnabled(channel, db);
|
||||||
|
return {
|
||||||
|
enabled: dbEnabled,
|
||||||
|
envEnabled: true,
|
||||||
|
dbEnabled,
|
||||||
|
blockReason: dbEnabled ? null : 'dashboard_disabled'
|
||||||
|
};
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
logger.warn(`[Messaging] Failed to read settings for ${channel}, using env only: ${err.message}`);
|
logger.warn(`[Messaging] Failed to read settings for ${channel}, using env only: ${err.message}`);
|
||||||
return true;
|
return {
|
||||||
|
enabled: true,
|
||||||
|
envEnabled: true,
|
||||||
|
dbEnabled: null,
|
||||||
|
blockReason: null
|
||||||
|
};
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const isMessagingChannelEnabled = async (channel) => {
|
||||||
|
const state = await getMessagingChannelState(channel);
|
||||||
|
return state.enabled;
|
||||||
|
};
|
||||||
|
|
||||||
const getPublicMessaging = (doc) => toPublicMessaging(readDbFlags(doc), config);
|
const getPublicMessaging = (doc) => toPublicMessaging(readDbFlags(doc), config);
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
|
getMessagingChannelState,
|
||||||
isMessagingChannelEnabled,
|
isMessagingChannelEnabled,
|
||||||
invalidateMessagingCache,
|
invalidateMessagingCache,
|
||||||
getPublicMessaging,
|
getPublicMessaging,
|
||||||
|
|||||||
@@ -4,7 +4,7 @@
|
|||||||
const axios = require('axios');
|
const axios = require('axios');
|
||||||
const config = require('../../config/config');
|
const config = require('../../config/config');
|
||||||
const logger = require('../logger');
|
const logger = require('../logger');
|
||||||
const { isMessagingChannelEnabled } = require('../../components/settings/messagingFlags');
|
const { getMessagingChannelState } = require('../../components/settings/messagingFlags');
|
||||||
|
|
||||||
const toBoolean = (value) => {
|
const toBoolean = (value) => {
|
||||||
if (typeof value === 'boolean') return value;
|
if (typeof value === 'boolean') return value;
|
||||||
@@ -22,16 +22,23 @@ const redactSmsParams = (params = []) => (
|
|||||||
);
|
);
|
||||||
|
|
||||||
const sendSingleSms = async (mobile, templateId, params = []) => {
|
const sendSingleSms = async (mobile, templateId, params = []) => {
|
||||||
console.log('[SMS] About to send notification:', {
|
const channelState = await getMessagingChannelState('sms');
|
||||||
|
|
||||||
|
logger.info('[SMS] About to send notification:', {
|
||||||
mobile,
|
mobile,
|
||||||
templateId,
|
templateId,
|
||||||
params: redactSmsParams(params),
|
params: redactSmsParams(params),
|
||||||
SMS_ENABLED: config.SMS_ENABLED,
|
SMS_ENABLED: channelState.envEnabled,
|
||||||
|
dashboardEnabled: channelState.dbEnabled,
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!(await isMessagingChannelEnabled('sms'))) {
|
if (!channelState.enabled) {
|
||||||
logger.info(`[SMS] Skipped (channel disabled) → ${mobile} template=${templateId}`);
|
const reason = channelState.blockReason || 'channel_disabled';
|
||||||
return { skipped: true, reason: 'channel_disabled' };
|
const detail = reason === 'dashboard_disabled'
|
||||||
|
? 'dashboard smsEnabled=false'
|
||||||
|
: 'SMS_ENABLED env flag is false';
|
||||||
|
logger.info(`[SMS] Skipped (${detail}) → ${mobile} template=${templateId}`);
|
||||||
|
return { skipped: true, reason };
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!templateId) {
|
if (!templateId) {
|
||||||
|
|||||||
Reference in New Issue
Block a user