diff --git a/src/server.ts b/src/server.ts index 253d0f8..aebcd49 100644 --- a/src/server.ts +++ b/src/server.ts @@ -113,23 +113,36 @@ export class WebhookServer { const commandParts = messageText.trim().split(/\s+/); const command = commandParts[0].toLowerCase(); - // Extract due date if present (format: YYYY-MM-DD) + if (commandParts.length < 2) { + console.log('⚠️ Invalid /tarea command - missing action'); + return; + } + + const action = commandParts[1].toLowerCase(); + let descriptionParts = []; let dueDate = ''; - const dateRegex = /(\d{4}-\d{2}-\d{2})/; - const dateIndex = commandParts.findIndex(p => dateRegex.test(p)); - if (dateIndex >= 0) { - const dateStr = commandParts[dateIndex]; - const date = new Date(dateStr); - const today = new Date(); - today.setHours(0, 0, 0, 0); - - if (date >= today) { - dueDate = dateStr; - commandParts.splice(dateIndex, 1); // Remove date from parts + // Process remaining parts + for (let i = 2; i < commandParts.length; i++) { + const part = commandParts[i]; + // Check for date (YYYY-MM-DD) + if (/^\d{4}-\d{2}-\d{2}$/.test(part)) { + const date = new Date(part); + const today = new Date(); + today.setHours(0, 0, 0, 0); + + if (date >= today) { + dueDate = part; + } else { + descriptionParts.push(part); // Keep past dates in description + } + } else { + descriptionParts.push(part); } } + const description = descriptionParts.join(' '); + console.log('🔍 Detected /tarea command:', { timestamp: new Date().toISOString(), from: data.key.participant, @@ -137,10 +150,6 @@ export class WebhookServer { rawMessage: messageText }); - // Extract action (first word after /tarea) - const action = commandParts.length > 1 ? commandParts[1] : 'none'; - const description = commandParts.slice(2).join(' '); - console.log('✅ Successfully parsed command:', { action, description, diff --git a/tests/unit/server.test.ts b/tests/unit/server.test.ts index 6120d5b..7781cb3 100644 --- a/tests/unit/server.test.ts +++ b/tests/unit/server.test.ts @@ -276,7 +276,12 @@ describe('WebhookServer', () => { remoteJid: 'group123@g.us', participant: 'user123@s.whatsapp.net' }, - message: { conversation: '/tarea due:2025-04-30 Finish project' } + message: { + conversation: '/tarea nueva Finish project @user2 2025-04-30', + contextInfo: { + mentionedJid: ['user2@s.whatsapp.net'] + } + } } }; @@ -285,16 +290,16 @@ describe('WebhookServer', () => { expect(consoleSpy).toHaveBeenCalledWith( '🔍 Detected /tarea command:', expect.objectContaining({ - rawMessage: '/tarea due:2025-04-30 Finish project' + rawMessage: '/tarea nueva Finish project @user2 2025-04-30' }) ); expect(consoleSpy).toHaveBeenCalledWith( '✅ Successfully parsed command:', expect.objectContaining({ - action: 'due:2025-04-30', - description: 'Finish project', + action: 'nueva', + description: 'Finish project @user2', dueDate: '2025-04-30', - mentionCount: 0 + mentionCount: 1 }) ); }); diff --git a/tests/unit/services/command.test.ts b/tests/unit/services/command.test.ts index d39d250..7cea35e 100644 --- a/tests/unit/services/command.test.ts +++ b/tests/unit/services/command.test.ts @@ -5,8 +5,8 @@ describe('CommandService', () => { const testContext = { sender: '1234567890@s.whatsapp.net', groupId: 'group-id@g.us', - message: '/tarea nueva Test task', - mentions: [] + message: '/tarea nueva Test task @user1 2025-12-31', + mentions: ['user1@s.whatsapp.net'] }; test('should ignore non-tarea commands', async () => {