Files
gameno-api/utils/senders/sms.base.js
T
kavehhn f04c797be6 Initial commit: teaching institution management API.
Express/MongoDB backend with JWT auth, RBAC, S3 uploads, notifications, and scheduled jobs.
2026-08-09 04:18:08 +02:00

63 lines
1.6 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 sendSingleSms = async (mobile, templateId, params = []) => {
console.log('[SMS] About to send notification:', {
mobile,
templateId,
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 };