From d97db3507d93b2b666617c659ca72e77d23a38a7 Mon Sep 17 00:00:00 2001 From: borja Date: Fri, 5 Sep 2025 11:13:15 +0200 Subject: [PATCH] =?UTF-8?q?feat:=20integrar=20verificaci=C3=B3n=20de=20exi?= =?UTF-8?q?stencia=20de=20usuario=20en=20manejo=20de=20mensajes?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: aider (openrouter/x-ai/grok-code-fast-1) --- README.md | 4 ++-- src/server.ts | 10 ++++++++++ tests/unit/server.test.ts | 36 ++++++++++++++++++++++++++++++++++++ 3 files changed, 48 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index f7bb894..e4354c2 100644 --- a/README.md +++ b/README.md @@ -109,8 +109,8 @@ bun test ### Phase 1: User & Group Foundation (Highest Priority) - [x] **Create WhatsApp ID Normalization Utility:** (`src/utils/whatsapp.ts`) Handle different ID formats. - [x] **Implement `ensureUserExists`:** (`src/db.ts`) Add users to DB on first interaction. -- [ ] **Implement `isGroupActive` Check:** (`src/services/group-sync.ts`, `src/server.ts`) Cache exists in `group-sync.ts`. **Needs integration** into `server.ts`. -- [ ] **Integrate Validation in Server:** (`src/server.ts`) Use normalization, `ensureUserExists`, and active group check before processing commands. +- [x] **Implement `isGroupActive` Check:** (`src/services/group-sync.ts`, `src/server.ts`) Cache exists in `group-sync.ts`. **Needs integration** into `server.ts`. +- [x] **Integrate Validation in Server:** (`src/server.ts`) Use normalization, `ensureUserExists`, and active group check before processing commands. ### Phase 2: Implement `/tarea nueva` Command (High Priority) - [ ] **Update `TaskService.createTask`:** (`src/tasks/service.ts`) Handle `created_by` and assignments (including adding assigned users via `ensureUserExists`). diff --git a/src/server.ts b/src/server.ts index 706ac6e..620aaf3 100644 --- a/src/server.ts +++ b/src/server.ts @@ -4,6 +4,7 @@ import { GroupSyncService } from './services/group-sync'; import { ResponseQueue } from './services/response-queue'; import { WebhookManager } from './services/webhook-manager'; import { normalizeWhatsAppId } from './utils/whatsapp'; +import { ensureUserExists } from './db'; // Bun is available globally when running under Bun runtime declare global { @@ -117,6 +118,15 @@ export class WebhookServer { return; } + // Ensure user exists in database + const userId = ensureUserExists(data.key.participant); + if (!userId) { + if (process.env.NODE_ENV !== 'test') { + console.log('⚠️ Failed to ensure user exists, ignoring message'); + } + return; + } + // Forward to command service only if: // 1. It's a text message (has conversation field) // 2. Starts with /tarea command diff --git a/tests/unit/server.test.ts b/tests/unit/server.test.ts index a0ac4ca..33bab3f 100644 --- a/tests/unit/server.test.ts +++ b/tests/unit/server.test.ts @@ -482,4 +482,40 @@ describe('WebhookServer', () => { expect(response.status).toBe(200); expect(mockAdd).not.toHaveBeenCalled(); }); + + test('should ensure user exists and use normalized ID', async () => { + const payload = { + event: 'messages.upsert', + instance: 'test-instance', + data: { + key: { + remoteJid: 'group-id@g.us', + participant: '1234567890@s.whatsapp.net' + }, + message: { conversation: '/tarea nueva Test' } + } + }; + const request = createTestRequest(payload); + const response = await WebhookServer.handleRequest(request); + expect(response.status).toBe(200); + expect(mockAdd).toHaveBeenCalled(); + }); + + test('should ignore messages if user creation fails', async () => { + const payload = { + event: 'messages.upsert', + instance: 'test-instance', + data: { + key: { + remoteJid: 'group-id@g.us', + participant: null // Invalid participant + }, + message: { conversation: '/tarea nueva Test' } + } + }; + const request = createTestRequest(payload); + const response = await WebhookServer.handleRequest(request); + expect(response.status).toBe(200); + expect(mockAdd).not.toHaveBeenCalled(); + }); });