Files
gameno-api/utils/senders/sms.base.js
T
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

72 lines
2.0 KiB
JavaScript

// /utils/senders/sms.base.js
'use strict';
const axios = require('axios');
const config = require('../../config/config');
const logger = require('../logger');
const { isMessagingChannelEnabled } = require('../../components/settings/messagingFlags');
const toBoolean = (value) => {
if (typeof value === 'boolean') return value;
if (value == null) return false;
const normalized = String(value).trim().toLowerCase();
return ['true', '1', 'yes', 'on'].includes(normalized);
};
const redactSmsParams = (params = []) => (
params.map((param) => (
String(param?.name || '').toLowerCase() === 'password'
? { ...param, value: '[redacted]' }
: param
))
);
const sendSingleSms = async (mobile, templateId, params = []) => {
console.log('[SMS] About to send notification:', {
mobile,
templateId,
params: redactSmsParams(params),
SMS_ENABLED: config.SMS_ENABLED,
});
if (!(await isMessagingChannelEnabled('sms'))) {
logger.info(`[SMS] Skipped (channel disabled) → ${mobile} template=${templateId}`);
return { skipped: true, reason: 'channel_disabled' };
}
if (!templateId) {
logger.warn(`[SMS] Missing templateId for ${mobile}`);
return { skipped: true, reason: 'missing_template' };
}
if (!mobile) {
logger.warn('[SMS] Missing mobile number');
return { skipped: true, reason: 'missing_mobile' };
}
const request = {
method: 'POST',
url: 'https://api.sms.ir/v1/send/verify',
headers: {
'X-API-KEY': config.SMS_PANEL_TOKEN,
Accept: 'application/json',
},
data: {
mobile,
templateId: Number(templateId) || templateId,
parameters: params,
},
};
try {
const result = await axios(request);
logger.info(`[SMS] Sent to ${mobile} template=${templateId}`);
return result.data;
} catch (err) {
logger.error(`[SMS] Failed to ${mobile}: ${err.response?.data?.message || err.message}`);
throw err;
}
};
module.exports = { sendSingleSms, toBoolean };