Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(session): answer GET presence with a JSON null when unset
Yudhi Armyndharis2bd23655470f
diff --git a/src/modules/session/session.controller.http.spec.ts b/src/modules/session/session.controller.http.spec.tsnew file mode 100644index 000000000..f61aff440--- /dev/null+++ b/src/modules/session/session.controller.http.spec.ts@@ -0,0 +1,52 @@+import type { INestApplication } from '@nestjs/common';+import { Test } from '@nestjs/testing';+import request from 'supertest';+import type { App } from 'supertest/types';+import { SessionController } from './session.controller';+import { SessionService } from './session.service';+import { AuditService } from '../audit/audit.service';+import { ChatScopeService } from '../auth/chat-scope.service';++// Nest answers a handler's returned null with an empty body, so the documented JSON `null` can only+// be seen through a real Nest/Express app.+describe('SessionController over HTTP: GET presence', () => {+ let app: INestApplication<App>;+ const getPresence = jest.fn();+ const url = '/sessions/0a941dac-a965-45e7-b318-74ae8be134f0/presence/[email protected]';++ beforeAll(async () => {+ const mod = await Test.createTestingModule({+ controllers: [SessionController],+ providers: [+ { provide: SessionService, useValue: { getPresence } },+ { provide: AuditService, useValue: {} },+ ChatScopeService,+ ],+ }).compile();+ app = mod.createNestApplication({ logger: false });+ await app.init();+ });++ afterAll(async () => {+ await app.close();+ });++ it('answers a JSON null when nothing has been reported', async () => {+ getPresence.mockResolvedValue(null);+ const res = await request(app.getHttpServer()).get(url);+ expect(res.status).toBe(200);+ expect(res.headers['content-type']).toMatch(/^application\/json/);+ expect(res.text).toBe('null');+ });++ it('answers the presence with observedAt as an ISO timestamp', async () => {+ getPresence.mockResolvedValue({ chatId: '[email protected]', participants: [], observedAt: 1_786_000_000_000 });+ const res = await request(app.getHttpServer()).get(url);+ expect(res.status).toBe(200);+ expect(res.body).toEqual({+ chatId: '[email protected]',+ participants: [],+ observedAt: new Date(1_786_000_000_000).toISOString(),+ });+ });+});diff --git a/src/modules/session/session.controller.ts b/src/modules/session/session.controller.tsindex 5eef5aad1..fd47dc326 100644--- a/src/modules/session/session.controller.ts+++ b/src/modules/session/session.controller.ts@@ -12,7 +12,9 @@ import {HttpStatus,ParseUUIDPipe,BadRequestException,+ Res,} from '@nestjs/common';+import type { Response } from 'express';import { ApiTags, ApiOperation, ApiResponse, ApiParam, ApiQuery } from '@nestjs/swagger';import { SessionService } from './session.service';import {@@ -635,9 +637,14 @@ export class SessionController {async getPresence(@Param('sessionId', ParseUUIDPipe) id: string,@Param('chatId') chatId: string,- ): Promise<ChatPresenceResponseDto | null> {+ @Res() res: Response,+ ): Promise<void> {const presence = await this.sessionService.getPresence(id, chatId);- return presence ? { ...presence, observedAt: new Date(presence.observedAt) } : null;+ const body: ChatPresenceResponseDto | null = presence+ ? { ...presence, observedAt: new Date(presence.observedAt) }+ : null;+ // Written directly: Nest answers a returned null with an empty body, not the JSON `null` above.+ res.json(body);}@ChatScoped('fenced')