feat(sms): add FULLNAME, PAYMENT_PRICE, and COURSE template variables for invoice SMS

This commit is contained in:
2026-08-15 18:55:17 +03:30
parent 1ef54ad414
commit 93f3aeb719
3 changed files with 52 additions and 7 deletions
+31 -4
View File
@@ -61,18 +61,45 @@ const sendClassReminderSms = async (receiver, className, time, place = '', userI
});
};
const sendInvoiceCreatedSms = async (receiver, amount, userId = null) => {
const resolvedUserId = userId || await resolveUserIdByPhone(receiver);
const sendInvoiceCreatedSms = async (receiver, payloadOrAmount, userId = null, extraCourse = '') => {
const isObject = typeof payloadOrAmount === 'object' && payloadOrAmount !== null;
const resolvedUserId = (isObject && payloadOrAmount.userId)
? payloadOrAmount.userId
: (userId || await resolveUserIdByPhone(receiver));
let fullName = '';
let amount = 0;
let course = '';
if (isObject) {
fullName = payloadOrAmount.fullName || '';
amount = payloadOrAmount.amount ?? 0;
course = payloadOrAmount.course || '';
} else {
amount = payloadOrAmount;
course = typeof extraCourse === 'string' ? extraCourse : '';
}
if (!fullName && resolvedUserId) {
const user = await User.findById(resolvedUserId).select('name').lean();
fullName = user?.name || '';
}
const { templateId, variables } = await getSmsTemplate('invoiceCreated');
const amountLabel = String(amount);
const fullNameLabel = String(fullName || '').trim();
const courseLabel = String(course || '').trim();
return recordAndSend({
userId: resolvedUserId,
channel: 'sms',
subject: 'ایجاد صورتحساب',
body: `صورتحساب جدید به مبلغ ${amountLabel} تومان صادر شد.`,
body: `صورتحساب جدید به مبلغ ${amountLabel} تومان بابت دوره «${courseLabel || '-'}» برای ${fullNameLabel || 'کارآموز'} صادر شد.`,
relatedEvent: 'payment.created',
sendFn: () => sendSingleSms(receiver, templateId, buildSmsParameters(variables, {
amount: amountLabel
fullName: fullNameLabel,
amount: amountLabel,
course: courseLabel
}))
});
};