Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(infra): give the import's skip guards the archived table

Yudhi Armyndharis47667e8c2cc2
diff --git a/src/modules/infra/infra-data.controller.spec.ts b/src/modules/infra/infra-data.controller.spec.ts
index 70442933d..6da286f12 100644
--- a/src/modules/infra/infra-data.controller.spec.ts
+++ b/src/modules/infra/infra-data.controller.spec.ts
@@ -1345,6 +1345,51 @@ describe('InfraDataController.importData round-trips export-data (no silent mess
expect(await ds.getRepository(Message).count()).toBe(1);
});
+ it('refuses a backup whose template names of one session differ only by NUL, before any teardown', async () => {
+ await seedSession('s1');
+
+ // The restore drops NUL from a template name and (sessionId, name) is unique: importing both rows
+ // would fail the second insert and roll everything back, so the pre-flight refuses the backup.
+ const res = await controller.importData({
+ tables: {
+ sessions: [
+ {
+ id: 's1',
+ name: 'restored',
+ status: 'ready',
+ createdAt: '2026-01-01T00:00:00.000Z',
+ updatedAt: '2026-01-01T00:00:00.000Z',
+ },
+ ] as never,
+ templates: [
+ {
+ id: 't1',
+ sessionId: 's1',
+ name: 'promo',
+ body: 'a',
+ createdAt: '2026-01-01T00:00:00.000Z',
+ updatedAt: '2026-01-01T00:00:00.000Z',
+ },
+ {
+ id: 't2',
+ sessionId: 's1',
+ name: 'promo\u0000',
+ body: 'b',
+ createdAt: '2026-01-01T00:00:00.000Z',
+ updatedAt: '2026-01-01T00:00:00.000Z',
+ },
+ ] as never,
+ },
+ });
+
+ expect(res.imported).toBe(false);
+ expect(res.warnings).toEqual([
+ 'Skipped template t2: name "promo" without NUL characters collides with template t1 of session s1',
+ ]);
+ expect(await ds.getRepository(Session).count()).toBe(1);
+ expect(await ds.getRepository(Template).count()).toBe(0);
+ });
+
it('propagates a genuine clear-table failure (lock/IO) instead of committing a merged restore', async () => {
// Pre-existing data that must survive if a clear step fails.
await seedSession('s1');
diff --git a/src/modules/infra/infra-data.service.ts b/src/modules/infra/infra-data.service.ts
index 866429eb2..3c75d7e86 100644
--- a/src/modules/infra/infra-data.service.ts
+++ b/src/modules/infra/infra-data.service.ts
@@ -587,12 +587,12 @@ export class InfraDataService {
// before the orphan pre-flight: with no sessions in it every running engine reads as an orphan, and
// a stopOrphans retry would tear all of them down for a restore that was never going to happen.
// A row a skip guard vetoes is just as certain a rollback (see the warnings gate below), and the
- // guards read only the archived row, so run them here too. The in-transaction call stays as a backstop.
+ // guards read only the archived tables, so run them here too. The in-transaction call stays as a backstop.
const refusals = TABLE_IMPORTERS.every(importer => !data.tables[importer.key]?.length)
? [EMPTY_ARCHIVE_WARNING]
: TABLE_IMPORTERS.flatMap(importer =>
(data.tables[importer.key] ?? [])
- .map(row => importer.skip?.(row as never))
+ .map((row, _index, rows) => importer.skip?.(row as never, rows as never))
.filter((warning): warning is string => warning != null),
);
if (refusals.length > 0) {
@@ -864,7 +864,7 @@ export class InfraDataService {
// cannot carry, and this loop is the one place it is known — so the cast lives here rather
// than at each of the three uses below.
const row = untypedRow as never;
- const skipWarning = importer.skip?.(row);
+ const skipWarning = importer.skip?.(row, rows as never);
if (skipWarning != null) {
warnings.push(skipWarning);
continue;