Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(infra): find the variables of an export template in one pass
Yudhi Armyndharisa25807aed065
diff --git a/src/modules/infra/export-tables.media-variables.spec.ts b/src/modules/infra/export-tables.media-variables.spec.tsindex a1081fcff..6bbea5fa6 100644--- a/src/modules/infra/export-tables.media-variables.spec.ts+++ b/src/modules/infra/export-tables.media-variables.spec.ts@@ -20,6 +20,7 @@ import { IntegrationDeliveryFailure } from '../integration/entities/integration-import { StatusUpdate } from '../status-store/entities/status-update.entity';import { SendIdempotencyKey } from '../message/entities/send-idempotency-key.entity';import { AutomationRule } from '../automation/entities/automation-rule.entity';+import * as templateRender from '../../common/utils/template-render';import { renderTemplate } from '../../common/utils/template-render';const entities = [@@ -131,6 +132,19 @@ describe('bulk export budgets payload variables', () => {expect(result.content.image.base64).toBeUndefined();expect(result.variables.p).toBeUndefined();});+ it('reads a template once however many variables the item carries', () => {+ const variables = Object.fromEntries(Array.from({ length: 2000 }, (_, i) => [`v${i}`, 'x']));+ const render = jest.spyOn(templateRender, 'renderTemplate');+ try {+ const result = stripped([entry('{{v7}}{{v1999}}', variables)], 2);+ expect(render.mock.calls.length).toBeLessThanOrEqual(1);+ expect(result.admitted).toEqual([2]);+ expect(result.items[0].content.image.base64).toBe('{{v7}}{{v1999}}');+ expect(Object.keys(result.items[0].variables)).toHaveLength(2001);+ } finally {+ render.mockRestore();+ }+ });it('preserves unrelated descriptive variables without charging them', () => {const result = stripped([entry('{{p}}', { p: 'QUJD', unused: 'a descriptive value longer than the budget' })], 4);expect(result.items[0].content.image.base64).toBe('{{p}}');diff --git a/src/modules/infra/export-tables.ts b/src/modules/infra/export-tables.tsindex 361e88580..3d2a9521f 100644--- a/src/modules/infra/export-tables.ts+++ b/src/modules/infra/export-tables.ts@@ -6,7 +6,7 @@ import type {MessageBatchRow,WebhookDeliveryFailureRow,} from './migration-tables.types';-import { renderTemplate } from '../../common/utils/template-render';+import { renderTemplate, templateVariableNames } from '../../common/utils/template-render';/*** A `data` value that is a POINTER rather than bytes. `metadata.media.data` holds `base64 || dto.url!`@@ -161,12 +161,9 @@ function stripBatchInlineMedia(row: MessageBatchRow, exceedsBudget: (encodedByteconst media = (content as Record<string, unknown>)[key] as { base64?: unknown } | null | undefined;if (!media || typeof media !== 'object' || typeof media.base64 !== 'string') continue;const template = media.base64;- // Asked of the renderer itself, so the placeholder grammar is the one the send path substitutes.- const named = template.includes('{')- ? Object.keys(variables).filter(- name => variables[name] != null && renderTemplate(template, { [name]: '' }) !== template,- )- : [];+ // One pass over the template with the renderer's own grammar, whatever the number of variables.+ const used = new Set(templateVariableNames(template));+ const named = Object.keys(variables).filter(name => used.has(name) && variables[name] != null);const scaffolding = renderTemplate(template, Object.fromEntries(named.map(name => [name, ''])));const encodedBytes = named.reduce((bytes, name) => {