Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(infra): refuse a vetoed backup row before stopping orphans
Yudhi Armyndhariseb03664ef747
diff --git a/src/modules/infra/infra-data.controller.spec.ts b/src/modules/infra/infra-data.controller.spec.tsindex 1d453ce4f..70442933d 100644--- a/src/modules/infra/infra-data.controller.spec.ts+++ b/src/modules/infra/infra-data.controller.spec.ts@@ -2467,6 +2467,32 @@ describe('InfraDataController.importData status_updates + runtime reconciliationexpect(await ds.getRepository(Session).count()).toBe(1);});+ it('refuses an archive holding a row the import would skip before stopOrphans tears down any engine', async () => {+ // A skipped row always rolls the restore back, and the teardown cannot be undone with it.+ await seedSession('s1');+ const stopOrphanEngines = jest.fn().mockResolvedValue({ stopped: ['ghost'], notRunning: [], failed: [] });+ const controller = build({+ sessionService: { getActiveSessionIds: () => ['ghost'], stopOrphanEngines },+ });+ const dump = await controller.exportData();+ const filters = { conditions: [{ field: 'type', operator: 'is', value: ['text'], negate: true }] };++ const res = await controller.importData({+ tables: {+ ...dump.tables,+ webhooks: [{ id: 'wh1', sessionId: 's1', url: 'https://example.com/hook', events: [], filters }] as never,+ },+ stopOrphans: true,+ });++ expect(res.imported).toBe(false);+ expect(res.warnings).toEqual([expect.stringContaining('Skipped webhook wh1')]);+ expect(stopOrphanEngines).not.toHaveBeenCalled();+ expect(res.stoppedOrphanEngines).toEqual([]);+ expect(res.orphanedEngines).toEqual([]);+ expect(await ds.getRepository(Session).count()).toBe(1);+ });+it('still reports the engines it already stopped when the import rolls back', async () => {// The pre-flight teardown runs BEFORE the transaction opens and cannot be rolled back with it.// An operator reading imported:false plus empty orphan arrays would conclude nothing happened,@@ -2763,8 +2789,8 @@ describe('InfraDataController.importData rejects a malformed table value', () =>// the guard hardening into refusing shapes the endpoint supports: an absent table (a partial// archive) and an empty one (a table that legitimately has no rows).it.each([- ['omits a table', { sessions: [{ id: 's1' }] }],- ['carries an empty table', { sessions: [{ id: 's1' }], messages: [] }],+ ['omits a table', { sessions: [{ id: 's1', name: 's1' }] }],+ ['carries an empty table', { sessions: [{ id: 's1', name: 's1' }], messages: [] }],])('does not reject an archive that %s', async (_label, tables) => {await expect(controller().importData({ tables } as never)).rejects.not.toThrow(/must be an array/);});diff --git a/src/modules/infra/infra-data.service.ts b/src/modules/infra/infra-data.service.tsindex f62c67a1b..866429eb2 100644--- a/src/modules/infra/infra-data.service.ts+++ b/src/modules/infra/infra-data.service.ts@@ -586,11 +586,20 @@ export class InfraDataService {// An archive with no rows is always refused (see the totalRestored check below), so refuse it here,// 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.- if (TABLE_IMPORTERS.every(importer => !data.tables[importer.key]?.length)) {+ // 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.+ 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))+ .filter((warning): warning is string => warning != null),+ );+ if (refusals.length > 0) {return {imported: false,counts: Object.fromEntries(TABLE_IMPORTERS.map(importer => [importer.key, 0] as const)) as TableCounts,- warnings: [EMPTY_ARCHIVE_WARNING],+ warnings: refusals,notices: [],restartRequired: false,orphanedEngines: [],