feat: Update /tarea command parsing to use action-first format

main
borja (aider) 3 months ago
parent b1449bb80b
commit 869c695d0a

@ -113,22 +113,35 @@ export class WebhookServer {
const commandParts = messageText.trim().split(/\s+/); const commandParts = messageText.trim().split(/\s+/);
const command = commandParts[0].toLowerCase(); 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 = ''; let dueDate = '';
const dateRegex = /(\d{4}-\d{2}-\d{2})/;
const dateIndex = commandParts.findIndex(p => dateRegex.test(p));
if (dateIndex >= 0) { // Process remaining parts
const dateStr = commandParts[dateIndex]; for (let i = 2; i < commandParts.length; i++) {
const date = new Date(dateStr); 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(); const today = new Date();
today.setHours(0, 0, 0, 0); today.setHours(0, 0, 0, 0);
if (date >= today) { if (date >= today) {
dueDate = dateStr; dueDate = part;
commandParts.splice(dateIndex, 1); // Remove date from parts } else {
descriptionParts.push(part); // Keep past dates in description
} }
} else {
descriptionParts.push(part);
} }
}
const description = descriptionParts.join(' ');
console.log('🔍 Detected /tarea command:', { console.log('🔍 Detected /tarea command:', {
timestamp: new Date().toISOString(), timestamp: new Date().toISOString(),
@ -137,10 +150,6 @@ export class WebhookServer {
rawMessage: messageText 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:', { console.log('✅ Successfully parsed command:', {
action, action,
description, description,

@ -276,7 +276,12 @@ describe('WebhookServer', () => {
remoteJid: 'group123@g.us', remoteJid: 'group123@g.us',
participant: 'user123@s.whatsapp.net' 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( expect(consoleSpy).toHaveBeenCalledWith(
'🔍 Detected /tarea command:', '🔍 Detected /tarea command:',
expect.objectContaining({ expect.objectContaining({
rawMessage: '/tarea due:2025-04-30 Finish project' rawMessage: '/tarea nueva Finish project @user2 2025-04-30'
}) })
); );
expect(consoleSpy).toHaveBeenCalledWith( expect(consoleSpy).toHaveBeenCalledWith(
'✅ Successfully parsed command:', '✅ Successfully parsed command:',
expect.objectContaining({ expect.objectContaining({
action: 'due:2025-04-30', action: 'nueva',
description: 'Finish project', description: 'Finish project @user2',
dueDate: '2025-04-30', dueDate: '2025-04-30',
mentionCount: 0 mentionCount: 1
}) })
); );
}); });

@ -5,8 +5,8 @@ describe('CommandService', () => {
const testContext = { const testContext = {
sender: '1234567890@s.whatsapp.net', sender: '1234567890@s.whatsapp.net',
groupId: 'group-id@g.us', groupId: 'group-id@g.us',
message: '/tarea nueva Test task', message: '/tarea nueva Test task @user1 2025-12-31',
mentions: [] mentions: ['user1@s.whatsapp.net']
}; };
test('should ignore non-tarea commands', async () => { test('should ignore non-tarea commands', async () => {

Loading…
Cancel
Save