Expose schedule fields on class and invoice templates so SMS can include Jalali start date, weekdays, and hour range.
73 lines
2.7 KiB
JavaScript
73 lines
2.7 KiB
JavaScript
'use strict';
|
|
|
|
const { describe, it } = require('node:test');
|
|
const assert = require('node:assert/strict');
|
|
const {
|
|
SMS_TEMPLATE_DEFS,
|
|
normalizeStoredEntry,
|
|
resolveVariablesList,
|
|
parseIncomingVariables,
|
|
mergeTemplateEntry
|
|
} = require('./smsTemplates');
|
|
|
|
const invoiceDef = SMS_TEMPLATE_DEFS.find((def) => def.key === 'invoiceCreated');
|
|
|
|
const threeInvoiceVars = [
|
|
{ slot: 'amount', name: 'PAYMENT_PRICE' },
|
|
{ slot: 'fullName', name: 'FULLNAME' },
|
|
{ slot: 'course', name: 'COURSE' }
|
|
];
|
|
|
|
describe('SMS template variables', () => {
|
|
it('keeps all invoiceCreated variables when stored as an array', () => {
|
|
const stored = normalizeStoredEntry({
|
|
templateId: '111',
|
|
variables: threeInvoiceVars
|
|
}, invoiceDef);
|
|
const resolved = resolveVariablesList(invoiceDef, stored.variables);
|
|
|
|
assert.equal(resolved.length, 3);
|
|
assert.deepEqual(resolved.map((item) => item.slot), ['amount', 'fullName', 'course']);
|
|
assert.deepEqual(resolved.map((item) => item.name), ['PAYMENT_PRICE', 'FULLNAME', 'COURSE']);
|
|
});
|
|
|
|
it('keeps all invoiceCreated variables when an array was stored as a numeric object', () => {
|
|
const stored = normalizeStoredEntry({
|
|
templateId: '111',
|
|
variables: {
|
|
0: { slot: 'amount', name: 'PAYMENT_PRICE' },
|
|
1: { slot: 'fullName', name: 'FULLNAME' },
|
|
2: { slot: 'course', name: 'COURSE' }
|
|
}
|
|
}, invoiceDef);
|
|
const resolved = resolveVariablesList(invoiceDef, stored.variables);
|
|
|
|
assert.equal(resolved.length, 3);
|
|
assert.deepEqual(resolved.map((item) => item.slot), ['amount', 'fullName', 'course']);
|
|
});
|
|
|
|
it('replaces a legacy single-slot map with every incoming invoiceCreated variable', () => {
|
|
const incoming = parseIncomingVariables(threeInvoiceVars, invoiceDef);
|
|
const merged = mergeTemplateEntry(
|
|
invoiceDef,
|
|
{ templateId: '111', variables: { amount: 'PAYMENT_PRICE' } },
|
|
{ templateId: '111', variables: incoming }
|
|
);
|
|
|
|
assert.equal(merged.variables.length, 3);
|
|
assert.deepEqual(merged.variables.map((item) => item.slot), ['amount', 'fullName', 'course']);
|
|
assert.deepEqual(merged.variables.map((item) => item.name), ['PAYMENT_PRICE', 'FULLNAME', 'COURSE']);
|
|
});
|
|
|
|
it('exposes class start date, class days, and course time slots', () => {
|
|
const expected = ['classStartDate', 'classDays', 'courseTime'];
|
|
for (const key of ['classRegistered', 'classReminder', 'invoiceCreated']) {
|
|
const def = SMS_TEMPLATE_DEFS.find((item) => item.key === key);
|
|
const slots = (def?.slots || []).map((slot) => slot.key);
|
|
for (const slot of expected) {
|
|
assert.ok(slots.includes(slot), `${key} is missing slot ${slot}`);
|
|
}
|
|
}
|
|
});
|
|
});
|