Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(session): delete chat_states and status_updates with the session

Yudhi Armyndharis3ee77d696a62
diff --git a/src/modules/session/session-engine-controls.ts b/src/modules/session/session-engine-controls.ts
index 0ff60b001..c9a137a73 100644
--- a/src/modules/session/session-engine-controls.ts
+++ b/src/modules/session/session-engine-controls.ts
@@ -7,6 +7,8 @@ import { MessageBatch } from '../message/entities/message-batch.entity';
import { Webhook } from '../webhook/entities/webhook.entity';
import { Template } from '../template/entities/template.entity';
import { BaileysStoredMessage } from '../../engine';
+import { ChatState } from '../../engine/adapters/baileys-chat-state.entity';
+import { StatusUpdate } from '../status-store/entities/status-update.entity';
import { EngineFactory } from '../../engine/engine.factory';
import { EngineRegistry } from '../../engine/engine-registry.service';
import { IWhatsAppEngine } from '../../engine/interfaces/whatsapp-engine.interface';
@@ -568,10 +570,13 @@ export class SessionEngineControls {
// better-sqlite3 defaults `foreign_keys` ON and TypeORM's driver re-asserts it at connection
// creation — so their explicit deletes are belt-and-braces rather than required; they stay
// because depending on a pragma neither this file nor a test pins is a thinner guarantee than
- // an explicit delete, and the ordering mirrors the restore path's explicit-clear.
+ // an explicit delete, and the ordering mirrors the restore path's explicit-clear. chat_states and
+ // status_updates are in the no-FK group too.
await this.host.dataSource().transaction(async manager => {
await manager.delete(Message, { sessionId: id });
await manager.delete(MessageBatch, { sessionId: id });
+ await manager.delete(ChatState, { sessionId: id });
+ await manager.delete(StatusUpdate, { sessionId: id });
await manager.delete(Webhook, { sessionId: id });
await manager.delete(Template, { sessionId: id });
await manager.delete(BaileysStoredMessage, { sessionId: id });
diff --git a/src/modules/session/session.service.spec.ts b/src/modules/session/session.service.spec.ts
index 1a96a2cce..8c56e3fdc 100644
--- a/src/modules/session/session.service.spec.ts
+++ b/src/modules/session/session.service.spec.ts
@@ -36,6 +36,8 @@ import { MessageBatch } from '../message/entities/message-batch.entity';
import { Webhook } from '../webhook/entities/webhook.entity';
import { Template } from '../template/entities/template.entity';
import { BaileysStoredMessage } from '../../engine/adapters/baileys-stored-message.entity';
+import { ChatState } from '../../engine/adapters/baileys-chat-state.entity';
+import { StatusUpdate } from '../status-store/entities/status-update.entity';
import { EngineFactory } from '../../engine/engine.factory';
import { EngineRegistry } from '../../engine/engine-registry.service';
import type { KeyedMutationQueue } from '../../common/utils/keyed-mutation-queue';
@@ -1155,6 +1157,9 @@ describe('SessionService', () => {
expect(managerDelete).toHaveBeenCalledWith(Webhook, { sessionId: 'sess-uuid-1' });
expect(managerDelete).toHaveBeenCalledWith(Template, { sessionId: 'sess-uuid-1' });
expect(managerDelete).toHaveBeenCalledWith(BaileysStoredMessage, { sessionId: 'sess-uuid-1' });
+ // chat_states and status_updates carry a plain sessionId with no FK at all.
+ expect(managerDelete).toHaveBeenCalledWith(ChatState, { sessionId: 'sess-uuid-1' });
+ expect(managerDelete).toHaveBeenCalledWith(StatusUpdate, { sessionId: 'sess-uuid-1' });
expect(managerRemove).toHaveBeenCalledWith(session);
});
});