Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(infra): reload the chat-state cache after a data restore
Yudhi Armyndharisbb6535e5fa5d
diff --git a/src/modules/infra/infra-data.controller.spec.ts b/src/modules/infra/infra-data.controller.spec.tsindex 752588c0d..4727f29fa 100644--- a/src/modules/infra/infra-data.controller.spec.ts+++ b/src/modules/infra/infra-data.controller.spec.ts@@ -1740,12 +1740,20 @@ describe('InfraDataController.importData status_updates + runtime reconciliationlet ds: DataSource;const cfg = { get: (key: string, def?: unknown) => (key === 'dataDatabase.type' ? 'sqlite' : def) };- // Positional service constructor: (config, dataDs, auditService?, sessionService?, lidMappingStore?).- // The @Optional args trail the required ones; auditService is unused in these tests, so its slot- // stays undefined.- const build = (opts: { sessionService?: unknown; lidMappingStore?: unknown } = {}) =>+ // Positional service constructor: (config, dataDs, auditService?, sessionService?, lidMappingStore?,+ // ownership?, chatStateStore?). The @Optional args trail the required ones; auditService and+ // ownership are unused in these tests, so their slots stay undefined.+ const build = (opts: { sessionService?: unknown; lidMappingStore?: unknown; chatStateStore?: unknown } = {}) =>new InfraDataController(- new InfraDataService(cfg as never, ds, undefined, opts.sessionService as never, opts.lidMappingStore as never),+ new InfraDataService(+ cfg as never,+ ds,+ undefined,+ opts.sessionService as never,+ opts.lidMappingStore as never,+ undefined,+ opts.chatStateStore as never,+ ),);beforeEach(async () => {@@ -1955,22 +1963,26 @@ describe('InfraDataController.importData status_updates + runtime reconciliationexpect((await ds.getRepository(Message).findOneByOrFail({ id: 'm1' })).body).toBe('hello');});- it('reloads the in-memory lid mappings after a committed restore', async () => {+ it('reloads the in-memory lid mappings and chat states after a committed restore', async () => {await seedSession('s1');const lidMappingStore = { reload: jest.fn().mockResolvedValue(undefined) };- const controller = build({ lidMappingStore });+ const chatStateStore = { reload: jest.fn().mockResolvedValue(undefined) };+ const controller = build({ lidMappingStore, chatStateStore });const dump = await controller.exportData();const res = await controller.importData({ tables: dump.tables });expect(res.imported).toBe(true);expect(lidMappingStore.reload).toHaveBeenCalledTimes(1);+ expect(chatStateStore.reload).toHaveBeenCalledTimes(1);});- it('does NOT reload lid mappings when the import is refused (nothing committed)', async () => {+ it('does NOT reload lid mappings or chat states when the import is refused (nothing committed)', async () => {await seedSession('s1');const lidMappingStore = { reload: jest.fn().mockResolvedValue(undefined) };- const res = await build({ lidMappingStore }).importData({ tables: {} });+ const chatStateStore = { reload: jest.fn().mockResolvedValue(undefined) };+ const res = await build({ lidMappingStore, chatStateStore }).importData({ tables: {} });expect(res.imported).toBe(false);expect(lidMappingStore.reload).not.toHaveBeenCalled();+ expect(chatStateStore.reload).not.toHaveBeenCalled();});it('409s when a live engine would be orphaned by the replace — unless force=true', async () => {diff --git a/src/modules/infra/infra-data.service.ts b/src/modules/infra/infra-data.service.tsindex deae76223..39c9f32de 100644--- a/src/modules/infra/infra-data.service.ts+++ b/src/modules/infra/infra-data.service.ts@@ -8,6 +8,7 @@ import { AuditService } from '../audit/audit.service';import { AuditAction } from '../audit/entities/audit-log.entity';import { SessionService } from '../session/session.service';import { LidMappingStoreService } from '../../engine/identity/lid-mapping-store.service';+import { ChatStateStoreService } from '../../engine/adapters/baileys-chat-state-store.service';import { SessionOwnershipService } from '../session/session-ownership.service';import { Session as SessionEntity, SessionStatus } from '../session/entities/session.entity';import { In } from 'typeorm';@@ -291,6 +292,8 @@ export class InfraDataService {// direct-construction unit tests, and every use is `?.`-guarded.@Optional()private readonly ownership?: SessionOwnershipService,+ @Optional()+ private readonly chatStateStore?: ChatStateStoreService,) {}/**@@ -801,7 +804,10 @@ export class InfraDataService {// reach it — resolution would keep serving stale entries (and miss restored ones) until the next// process start. Reload from the new DB contents. Best-effort: a miss falls back to engine// re-resolution, so a reload failure degrades instead of failing the (already committed) import.+ // The chat-state mirror has the same shape: left stale, GET /chats would serve the old+ // archived/pinned/muted flags and the next live update would write them back over the restore.await this.lidMappingStore?.reload();+ await this.chatStateStore?.reload();// Audit the destructive replace-all restore, only on the committed-success path (the rollback /// refused-empty branches above return without emitting, since no data actually changed). Any