feat(sms): add FULLNAME, PAYMENT_PRICE, and COURSE template variables for invoice SMS
This commit is contained in:
@@ -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
|
||||
}))
|
||||
});
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user