Substitute org name and balance tokens in outbound SMS so templates no longer leave {از طرف} literal.
EOF Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -56,7 +56,19 @@ function testTemplate() {
|
||||
}) === 'سلام علی 0912',
|
||||
'name phone vars',
|
||||
);
|
||||
assert(clipSms('الف'.repeat(200)).length === 200, 'sms is not clipped');
|
||||
assert(
|
||||
applyMessageTemplate('تبریک {از طرف}', { from: 'هنراندیشه' }) ===
|
||||
'تبریک هنراندیشه',
|
||||
'from org var',
|
||||
);
|
||||
assert(
|
||||
applyMessageTemplate('مانده {مانده ریالی} / {مانده فلزی}', {
|
||||
rialBalance: '۱۰۰۰ بس',
|
||||
metalBalance: 'طلا ۲',
|
||||
}) === 'مانده ۱۰۰۰ بس / طلا ۲',
|
||||
'balance vars',
|
||||
);
|
||||
assert(clipSms('ا'.repeat(200)).length === 200, 'sms is not clipped');
|
||||
}
|
||||
|
||||
testDelay();
|
||||
|
||||
@@ -77,6 +77,17 @@ export function buildSendAts(
|
||||
return times;
|
||||
}
|
||||
|
||||
/** یکسانسازی کلید توکنهای قالب (فاصله/ZWNJ/ی عربی). */
|
||||
export function normalizeTemplateKey(key: string): string {
|
||||
return String(key ?? '')
|
||||
.normalize('NFKC')
|
||||
.replace(/[\u200c\u200d\u200e\u200f\ufeff]/g, '')
|
||||
.replace(/ي/g, 'ی')
|
||||
.replace(/ك/g, 'ک')
|
||||
.replace(/\s+/g, ' ')
|
||||
.trim();
|
||||
}
|
||||
|
||||
export function applyMessageTemplate(
|
||||
text: string,
|
||||
vars: {
|
||||
@@ -85,22 +96,44 @@ export function applyMessageTemplate(
|
||||
phone?: string;
|
||||
date?: string;
|
||||
company?: string;
|
||||
/** نام سازمان فرستنده */
|
||||
from?: string;
|
||||
/** مانده مالی قالببندیشده */
|
||||
rialBalance?: string;
|
||||
/** ماندههای وزنی/فلزی قالببندیشده */
|
||||
metalBalance?: string;
|
||||
},
|
||||
): string {
|
||||
const map: Record<string, string> = {
|
||||
title: vars.title ?? '',
|
||||
عنوان: vars.title ?? '',
|
||||
نام: vars.name ?? '',
|
||||
name: vars.name ?? '',
|
||||
شماره: vars.phone ?? '',
|
||||
phone: vars.phone ?? '',
|
||||
تاریخ: vars.date ?? '',
|
||||
date: vars.date ?? '',
|
||||
شرکت: vars.company ?? '',
|
||||
company: vars.company ?? '',
|
||||
};
|
||||
const from = vars.from ?? '';
|
||||
const rial = vars.rialBalance ?? '';
|
||||
const metal = vars.metalBalance ?? '';
|
||||
const entries: Array<[string, string]> = [
|
||||
['title', vars.title ?? ''],
|
||||
['عنوان', vars.title ?? ''],
|
||||
['نام', vars.name ?? ''],
|
||||
['name', vars.name ?? ''],
|
||||
['شماره', vars.phone ?? ''],
|
||||
['phone', vars.phone ?? ''],
|
||||
['تاریخ', vars.date ?? ''],
|
||||
['date', vars.date ?? ''],
|
||||
['شرکت', vars.company ?? ''],
|
||||
['company', vars.company ?? ''],
|
||||
['از طرف', from],
|
||||
['ازطرف', from],
|
||||
['from', from],
|
||||
['مانده ریالی', rial],
|
||||
['ماندهریالی', rial],
|
||||
['rialBalance', rial],
|
||||
['مانده فلزی', metal],
|
||||
['ماندهفلزی', metal],
|
||||
['metalBalance', metal],
|
||||
];
|
||||
const map: Record<string, string> = {};
|
||||
for (const [key, value] of entries) {
|
||||
map[normalizeTemplateKey(key)] = value;
|
||||
}
|
||||
return text.replace(/\{([^}]+)\}/g, (full, key: string) => {
|
||||
const next = map[key.trim()];
|
||||
const next = map[normalizeTemplateKey(key)];
|
||||
return next === undefined ? full : next;
|
||||
});
|
||||
}
|
||||
|
||||
@@ -356,6 +356,10 @@ export class MessageAutomationsService {
|
||||
})
|
||||
: null;
|
||||
const lead = await this.lookupLead(job);
|
||||
const org = await this.prisma.organization.findUnique({
|
||||
where: { id: job.organizationId },
|
||||
select: { name: true },
|
||||
});
|
||||
const personName = person
|
||||
? [person.firstName, person.lastName].filter(Boolean).join(' ').trim()
|
||||
: '';
|
||||
@@ -366,6 +370,7 @@ export class MessageAutomationsService {
|
||||
phone,
|
||||
date: formatJalaliDayFa(new Date()),
|
||||
company: person?.company?.name ?? '',
|
||||
from: org?.name?.trim() || '',
|
||||
};
|
||||
const title = applyMessageTemplate(job.rule.title, vars).slice(0, 200);
|
||||
const body = applyMessageTemplate(job.rule.body, vars);
|
||||
|
||||
Reference in New Issue
Block a user