Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(session): send session:created plugins the API shape of a session

Yudhi Armyndharisb2a00e56d5bc
diff --git a/docs/19-plugin-architecture.md b/docs/19-plugin-architecture.md
index e528a7a18..6b6d546dd 100644
--- a/docs/19-plugin-architecture.md
+++ b/docs/19-plugin-architecture.md
@@ -460,6 +460,10 @@ export type HookEvent =
| 'ingress:error';
```
+`session:created` carries the new session in the shape the REST API returns it (`id`, `name`, `status`,
+timestamps and so on), never the stored `proxyUrl` or `config`; `session:deleted` carries
+`{ id, name, phone, pushName }`.
+
### Hook context and result
```typescript
diff --git a/src/modules/session/session.service.spec.ts b/src/modules/session/session.service.spec.ts
index dc3aa9b5b..1a96a2cce 100644
--- a/src/modules/session/session.service.spec.ts
+++ b/src/modules/session/session.service.spec.ts
@@ -30,6 +30,7 @@ import {
isKnownTerminalEngineFailure,
} from '../../engine/terminal-engine-failure';
import { Session, SessionStatus } from './entities/session.entity';
+import { SessionResponseDto } from './dto';
import { Message, MessageDirection, MessageStatus } from '../message/entities/message.entity';
import { MessageBatch } from '../message/entities/message-batch.entity';
import { Webhook } from '../webhook/entities/webhook.entity';
@@ -720,11 +721,31 @@ describe('SessionService', () => {
expect(repository.create).toHaveBeenCalledWith(expect.objectContaining({ status: SessionStatus.CREATED }));
expect(hookManager.execute).toHaveBeenCalledWith(
'session:created',
- session,
+ expect.objectContaining({ id: session.id, name: 'test-session', status: SessionStatus.CREATED }),
expect.objectContaining({ sessionId: session.id }),
);
});
+ it('hands session:created plugins the API shape, never the proxy credentials or config', async () => {
+ const session = createMockSession({
+ proxyUrl: 'http://alice:[email protected]:8080',
+ proxyType: 'http',
+ config: { webhookSecret: 'hush' },
+ });
+ (repository.findOne as jest.Mock).mockResolvedValue(null);
+ (repository.create as jest.Mock).mockReturnValue(session);
+ (repository.save as jest.Mock).mockResolvedValue(session);
+
+ const result = await service.create({ name: 'test-session', proxyUrl: 'http://alice:[email protected]:8080' });
+
+ const calls = (hookManager.execute as jest.Mock).mock.calls as [string, unknown][];
+ const payload = calls.find(([event]) => event === 'session:created')?.[1];
+ expect(payload).toEqual(SessionResponseDto.fromEntity(session, false));
+ expect(JSON.stringify(payload)).not.toMatch(/s3cret|hush|proxy/);
+ // The caller still gets the saved entity (the controller maps it for the response).
+ expect(result).toBe(session);
+ });
+
it('should throw ConflictException if session name already exists', async () => {
(repository.findOne as jest.Mock).mockResolvedValue(createMockSession());
diff --git a/src/modules/session/session.service.ts b/src/modules/session/session.service.ts
index 7138189a1..4da6caa25 100644
--- a/src/modules/session/session.service.ts
+++ b/src/modules/session/session.service.ts
@@ -21,6 +21,7 @@ import {
SessionConfigResponseDto,
UpdateSessionConfigDto,
SessionProxyResponseDto,
+ SessionResponseDto,
UpdateSessionProxyDto,
projectSessionProxy,
} from './dto';
@@ -318,8 +319,10 @@ export class SessionService implements OnModuleDestroy, OnModuleInit, OnApplicat
action: 'create',
});
- // Execute hook after session created (outside transaction since hooks do external I/O)
- await this.hookManager.execute('session:created', saved, {
+ // Execute hook after session created (outside transaction since hooks do external I/O). Plugins get
+ // the session as the REST API returns it, not the entity: the entity carries proxyUrl (credentials
+ // allowed) and the config blob, which no response ever exposes.
+ await this.hookManager.execute('session:created', SessionResponseDto.fromEntity(saved, this.isActive(saved.id)), {
sessionId: saved.id,
source: 'SessionService',
});