diff --git a/src/server.ts b/src/server.ts index 6323303..7c6c823 100644 --- a/src/server.ts +++ b/src/server.ts @@ -127,6 +127,9 @@ export class WebhookServer { let dueDate = ''; // Process remaining parts + const dateCandidates = []; + + // First collect all valid future dates for (let i = 2; i < commandParts.length; i++) { const part = commandParts[i]; // Check for date (YYYY-MM-DD) @@ -136,12 +139,28 @@ export class WebhookServer { today.setHours(0, 0, 0, 0); if (date >= today) { - dueDate = part; - continue; // Skip adding to description + dateCandidates.push({ index: i, date, part }); } - // Fall through to add invalid/past dates to description } - descriptionParts.push(part); + } + + // Use the last valid future date as due date + if (dateCandidates.length > 0) { + const lastDate = dateCandidates[dateCandidates.length - 1]; + dueDate = lastDate.part; + + // Add all parts except the last date to description + for (let i = 2; i < commandParts.length; i++) { + // Skip the due date part + if (i === lastDate.index) continue; + + // Add to description if it's a date candidate but not selected or a regular part + const part = commandParts[i]; + descriptionParts.push(part); + } + } else { + // No valid future dates, add all parts to description + descriptionParts = commandParts.slice(2); } const description = descriptionParts.join(' ').trim(); diff --git a/tests/unit/server.test.ts b/tests/unit/server.test.ts index 4fede56..6d91cbc 100644 --- a/tests/unit/server.test.ts +++ b/tests/unit/server.test.ts @@ -335,4 +335,115 @@ describe('WebhookServer', () => { expect(response.status).toBe(200); expect(mockAdd).toHaveBeenCalled(); }); + + test('should handle multiple dates in command (use last one as due date)', async () => { + const futureDate1 = getFutureDate(3); + const futureDate2 = getFutureDate(5); + const payload = { + event: 'messages.upsert', + instance: 'test-instance', + data: { + key: { + remoteJid: 'group-id@g.us', + participant: 'sender-id@s.whatsapp.net' + }, + message: { + conversation: `/tarea nueva Test task ${futureDate1} some text ${futureDate2}` + } + } + }; + + await WebhookServer.handleRequest(createTestRequest(payload)); + + expect(console.log).toHaveBeenCalledWith( + '✅ Successfully parsed command:', + expect.objectContaining({ + description: `Test task ${futureDate1} some text`, + dueDate: futureDate2 + }) + ); + }); + + test('should ignore past dates as due dates', async () => { + const pastDate = '2020-01-01'; + const futureDate = getFutureDate(2); + const payload = { + event: 'messages.upsert', + instance: 'test-instance', + data: { + key: { + remoteJid: 'group-id@g.us', + participant: 'sender-id@s.whatsapp.net' + }, + message: { + conversation: `/tarea nueva Test task ${pastDate} more text ${futureDate}` + } + } + }; + + await WebhookServer.handleRequest(createTestRequest(payload)); + + expect(console.log).toHaveBeenCalledWith( + '✅ Successfully parsed command:', + expect.objectContaining({ + description: `Test task ${pastDate} more text`, + dueDate: futureDate + }) + ); + }); + + test('should handle multiple past dates correctly', async () => { + const pastDate1 = '2020-01-01'; + const pastDate2 = '2021-01-01'; + const payload = { + event: 'messages.upsert', + instance: 'test-instance', + data: { + key: { + remoteJid: 'group-id@g.us', + participant: 'sender-id@s.whatsapp.net' + }, + message: { + conversation: `/tarea nueva Test task ${pastDate1} and ${pastDate2}` + } + } + }; + + await WebhookServer.handleRequest(createTestRequest(payload)); + + expect(console.log).toHaveBeenCalledWith( + '✅ Successfully parsed command:', + expect.objectContaining({ + description: `Test task ${pastDate1} and ${pastDate2}`, + dueDate: 'none' + }) + ); + }); + + test('should handle mixed valid and invalid date formats', async () => { + const futureDate = getFutureDate(2); + const payload = { + event: 'messages.upsert', + instance: 'test-instance', + data: { + key: { + remoteJid: 'group-id@g.us', + participant: 'sender-id@s.whatsapp.net' + }, + message: { + conversation: `/tarea nueva Test task 2023-13-01 (invalid) ${futureDate} 25/12/2023 (invalid)` + } + } + }; + + await WebhookServer.handleRequest(createTestRequest(payload)); + + expect(console.log).toHaveBeenCalledWith( + '✅ Successfully parsed command:', + expect.objectContaining({ + description: 'Test task 2023-13-01 (invalid) 25/12/2023 (invalid)', + dueDate: futureDate + }) + ); + }); });