fix(settings): persist every SMS template variable on save
Mongoose Mixed updates were dropping extra invoiceCreated variables after a successful save.
This commit is contained in:
@@ -0,0 +1,61 @@
|
||||
'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']);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user