feat: improve command parsing to handle multiple dates and invalid date formats

main
borja (aider) 2 months ago
parent b0f5a83b44
commit 17437c6e28

@ -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,13 +139,29 @@ 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
}
// 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();

@ -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
})
);
});
});

Loading…
Cancel
Save