Move sms.ir template IDs out of env into settings so SuperAdmin can manage them, and record every outbound SMS status in notifications.
71 lines
1.8 KiB
JavaScript
71 lines
1.8 KiB
JavaScript
// /utils/senders/sms.base.js
|
|
'use strict';
|
|
|
|
const axios = require('axios');
|
|
const config = require('../../config/config');
|
|
const logger = require('../logger');
|
|
|
|
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 (!toBoolean(config.SMS_ENABLED)) {
|
|
logger.info(`[SMS] Skipped (SMS_ENABLED=false) → ${mobile} template=${templateId}`);
|
|
return { skipped: true };
|
|
}
|
|
|
|
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 };
|