feat: add task ID validation, user mention handling, and error handling

main
brobert (aider) 3 months ago
parent 54c723449f
commit 6ac396e79f

@ -9,9 +9,16 @@ export function handleTaskCommand(body: string, sender: string, groupId: string,
const [action, ...args] = command.split(' '); const [action, ...args] = command.split(' ');
const description = args.join(' '); const description = args.join(' ');
const assignedUserMatch = description.match(/@\w+/); // Extract WhatsApp mention (format: @<phone_number>)
const assignedUserMatch = description.match(/@\d+/);
const assignedUser = assignedUserMatch ? assignedUserMatch[0] : null; const assignedUser = assignedUserMatch ? assignedUserMatch[0] : null;
// Validate phone number format
if (assignedUser && !/^@\d{8,}$/.test(assignedUser)) {
sendMessage(sender, 'Formato de mención inválido. Usa @ seguido del número de teléfono');
return;
}
const dueDateMatch = description.match(/\d{4}-\d{2}-\d{2}/); const dueDateMatch = description.match(/\d{4}-\d{2}-\d{2}/);
const dueDate = dueDateMatch ? dueDateMatch[0] : null; const dueDate = dueDateMatch ? dueDateMatch[0] : null;
@ -40,17 +47,23 @@ export function handleTaskCommand(body: string, sender: string, groupId: string,
sendMessage(sender, `❌ Error al crear la tarea: ${error.message}`); sendMessage(sender, `❌ Error al crear la tarea: ${error.message}`);
} }
} else if (action === 'asignar') { } else if (action === 'asignar') {
const taskId = args[0]; try {
if (assignedUser) { const taskId = validateTaskId(args[0]);
assignTask(parseInt(taskId), assignedUser); if (!assignedUser) {
sendMessage(sender, `Tarea ${taskId} asignada a ${assignedUser}`); throw new Error('Debes mencionar a un usuario para asignar la tarea. Ejemplo: /tarea asignar 1 @12345678');
} else {
sendMessage(sender, 'Debes mencionar a un usuario para asignar la tarea. Ejemplo: /tarea asignar 1 @usuario');
} }
} else if (action === 'lista' || (!action && args.length === 1 && !isNaN(Number(args[0])))) {
assignTask(taskId, assignedUser);
sendMessage(sender, `✅ Tarea ${taskId} asignada a ${assignedUser}`);
} catch (error) {
console.error('Error assigning task:', error);
sendMessage(sender, `❌ Error al asignar tarea: ${error.message}`);
}
} else if (action === 'lista' || (!action && args.length === 1)) {
// Handle both /tarea lista 14 and /tarea 14 // Handle both /tarea lista 14 and /tarea 14
const taskId = args[0]; try {
const task = getTaskById(parseInt(taskId)); const taskId = validateTaskId(args[0]);
const task = getTaskById(taskId);
if (!task) { if (!task) {
sendMessage(sender, `Tarea ${taskId} no encontrada`); sendMessage(sender, `Tarea ${taskId} no encontrada`);

@ -58,9 +58,22 @@ export function getPendingTasks(assignedTo: string) {
// Get task by ID // Get task by ID
export function getTaskById(taskId: number) { export function getTaskById(taskId: number) {
if (!Number.isInteger(taskId) || taskId <= 0) {
throw new Error('ID de tarea inválido');
}
const tasks = query( const tasks = query(
'SELECT * FROM tasks WHERE id = ?', 'SELECT * FROM tasks WHERE id = ?',
[taskId] [taskId]
); );
return tasks[0] || null; return tasks[0] || null;
} }
// Validate task ID from string input
export function validateTaskId(taskIdStr: string): number {
const taskId = parseInt(taskIdStr);
if (isNaN(taskId) || taskId <= 0) {
throw new Error('ID de tarea inválido. Debe ser un número positivo');
}
return taskId;
}

Loading…
Cancel
Save