Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(infra): record the sessions an import stops

Yudhi Armyndharis7a1cb24f09c2
diff --git a/src/modules/infra/infra-data.controller.spec.ts b/src/modules/infra/infra-data.controller.spec.ts
index f8f56825f..5f41e5f22 100644
--- a/src/modules/infra/infra-data.controller.spec.ts
+++ b/src/modules/infra/infra-data.controller.spec.ts
@@ -2071,6 +2071,30 @@ describe('InfraDataController audit trail — import emits only on a committed r
expect(actions).toContain(AuditAction.INFRA_DATA_IMPORTED);
});
+ it('records each orphan session it stops, even when the restore is then rolled back', async () => {
+ await seedSession('s1');
+ const audit = { logInfo: jest.fn().mockResolvedValue(null) };
+ const sessionService = {
+ getActiveSessionIds: () => ['ghost'],
+ stopOrphanEngines: jest.fn().mockResolvedValue({ stopped: ['ghost'], notRunning: [], failed: [] }),
+ };
+ const controller = new InfraDataController(
+ new InfraDataService(cfg as never, ds, audit as never, sessionService as never),
+ );
+ const dump = await controller.exportData();
+ // A repeated primary key fails its insert inside the transaction, which rolls the restore back.
+ dump.tables.sessions = [dump.tables.sessions[0], dump.tables.sessions[0]];
+
+ const res = await controller.importData({ tables: dump.tables, stopOrphans: true });
+
+ expect(res.imported).toBe(false);
+ const calls = audit.logInfo.mock.calls as Array<[AuditAction, { sessionId?: string; metadata?: unknown }]>;
+ expect(calls.filter(([action]) => action === AuditAction.INFRA_DATA_IMPORTED)).toEqual([]);
+ expect(calls.filter(([action]) => action === AuditAction.SESSION_STOPPED)).toEqual([
+ [AuditAction.SESSION_STOPPED, { sessionId: 'ghost', metadata: { via: 'infra_import_data' } }],
+ ]);
+ });
+
it('does NOT emit INFRA_DATA_IMPORTED when an empty backup is refused (no data changed)', async () => {
await seedSession('s1');
const audit = { logInfo: jest.fn().mockResolvedValue(null) };
diff --git a/src/modules/infra/infra-data.service.ts b/src/modules/infra/infra-data.service.ts
index 974634358..0398983f8 100644
--- a/src/modules/infra/infra-data.service.ts
+++ b/src/modules/infra/infra-data.service.ts
@@ -643,6 +643,14 @@ export class InfraDataService {
const result = await this.sessionService.stopOrphanEngines(orphanedEngines);
stoppedOrphanEngines = result.stopped;
failedOrphanEngines = result.failed;
+ // The teardown cannot be rolled back, and the import's own audit row is written only for a committed
+ // restore, so each stopped session is recorded here: a refused or failed restore would leave no trace.
+ for (const sessionId of result.stopped) {
+ await this.auditService?.logInfo(AuditAction.SESSION_STOPPED, {
+ sessionId,
+ metadata: { via: 'infra_import_data' },
+ });
+ }
if (failedOrphanEngines.length > 0) {
// Teardown failed for at least one orphan. The Map entry is removed regardless (see
// stopOrphanEngines), so the engine no longer holds a concurrency slot — but its underlying