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:
@@ -21,15 +21,82 @@ function tehranJalaliLong(d: Date) {
|
||||
return `${toFaDigits(j.jd)} ${jalaliMonthName(j.jm)} ${toFaDigits(j.jy)}`;
|
||||
}
|
||||
|
||||
/** قالب بس/بد مثل UI تهحساب — برای توکنهای مانده در پیامک */
|
||||
export function formatSmsBedBes(v: unknown, maximumFractionDigits = 3): string {
|
||||
if (v == null) return '';
|
||||
const s = String(v).trim();
|
||||
if (!s || s === 'null' || s === 'undefined' || s === 'NaN') return '';
|
||||
const n = Number(s.replace(/,/g, ''));
|
||||
if (!Number.isFinite(n)) return toFaDigits(s);
|
||||
const abs = toFaDigits(
|
||||
new Intl.NumberFormat('en-US', { maximumFractionDigits }).format(Math.abs(n)),
|
||||
);
|
||||
if (n === 0) return abs;
|
||||
return `${abs} ${n < 0 ? 'بد' : 'بس'}`;
|
||||
}
|
||||
|
||||
const METAL_FIELD_LABELS: Record<string, string> = {
|
||||
MandeyeVazni: 'طلا',
|
||||
MandeyeVazniNoghre: 'نقره',
|
||||
MandeyeKolBeTALA: 'کل به طلا',
|
||||
MandeMenhayeSeke: 'منهای سکه',
|
||||
};
|
||||
|
||||
function isMetalMandehKey(key: string) {
|
||||
if (METAL_FIELD_LABELS[key]) return true;
|
||||
if (key.startsWith('Seke_')) return true;
|
||||
return /Vazni|طلا|نقره|فلز|Gold|Silver/i.test(key);
|
||||
}
|
||||
|
||||
function metalFieldLabel(key: string) {
|
||||
if (METAL_FIELD_LABELS[key]) return METAL_FIELD_LABELS[key]!;
|
||||
if (key.startsWith('Seke_')) return `سکه ${key.slice(5)}`;
|
||||
return key;
|
||||
}
|
||||
|
||||
/** از فیلدهای مانده تهحساب، متن مانده ریالی و فلزی میسازد */
|
||||
export function smsBalanceTexts(input: {
|
||||
mandehFields?: Array<{ key: string; value: string | null }> | null;
|
||||
rialBalance?: number | null;
|
||||
goldBalance?: number | null;
|
||||
}): { rialBalance: string; metalBalance: string } {
|
||||
const fields = input.mandehFields ?? [];
|
||||
const mali = fields.find((f) => f.key === 'MandeyeMali' || f.key === 'MandeyeKolBePool');
|
||||
const rial =
|
||||
formatSmsBedBes(mali?.value ?? input.rialBalance) ||
|
||||
formatSmsBedBes(input.rialBalance);
|
||||
const metalParts = fields
|
||||
.filter((f) => isMetalMandehKey(f.key) && f.value != null && String(f.value).trim() !== '')
|
||||
.map((f) => `${metalFieldLabel(f.key)} ${formatSmsBedBes(f.value)}`.trim())
|
||||
.filter(Boolean);
|
||||
const metal =
|
||||
metalParts.join('، ') ||
|
||||
(input.goldBalance != null && formatSmsBedBes(input.goldBalance)
|
||||
? `طلا ${formatSmsBedBes(input.goldBalance)}`
|
||||
: '');
|
||||
return { rialBalance: rial, metalBalance: metal };
|
||||
}
|
||||
|
||||
export function renderSmsText(
|
||||
text: string,
|
||||
vars: { name?: string; phone?: string; title?: string; company?: string },
|
||||
vars: {
|
||||
name?: string;
|
||||
phone?: string;
|
||||
title?: string;
|
||||
company?: string;
|
||||
from?: string;
|
||||
rialBalance?: string;
|
||||
metalBalance?: string;
|
||||
},
|
||||
) {
|
||||
return applyMessageTemplate(text, {
|
||||
name: vars.name,
|
||||
phone: vars.phone,
|
||||
title: vars.title,
|
||||
company: vars.company,
|
||||
from: vars.from,
|
||||
rialBalance: vars.rialBalance,
|
||||
metalBalance: vars.metalBalance,
|
||||
date: tehranJalaliLong(new Date()),
|
||||
}).trim();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user