From 57bab18ae8c82d1b8555bf1a2f1a1005bec34062 Mon Sep 17 00:00:00 2001 From: "borja (aider)" Date: Mon, 24 Mar 2025 10:34:52 +0100 Subject: [PATCH] fix: properly format task creation messages with text property --- src/bot/commands/task.ts | 32 ++++++++++++++++++++++---------- src/services/taskService.ts | 3 ++- 2 files changed, 24 insertions(+), 11 deletions(-) diff --git a/src/bot/commands/task.ts b/src/bot/commands/task.ts index 99abe20..3011a82 100644 --- a/src/bot/commands/task.ts +++ b/src/bot/commands/task.ts @@ -48,10 +48,15 @@ export async function handleTaskCommand(body: string, sender: string, groupId: s const dueDateMatch = description.match(/\d{4}-\d{2}-\d{2}/); const dueDate = dueDateMatch ? dueDateMatch[0] : null; - const cleanDescription = description - .replace(/@\w+/g, '') - .replace(/\d{4}-\d{2}-\d{2}/g, '') - .trim(); + // Extract and clean description - remove mentions and dates + let cleanDescription = description; + if (assignedUserMatch) { + cleanDescription = cleanDescription.replace(assignedUserMatch[0], '').trim(); + } + if (dueDateMatch) { + cleanDescription = cleanDescription.replace(dueDateMatch[0], '').trim(); + } + cleanDescription = cleanDescription.trim(); if (!action || action === 'mostrar') { // Handle bare /tarea or /tarea mostrar @@ -73,16 +78,23 @@ export async function handleTaskCommand(body: string, sender: string, groupId: s dueDate: dueDate || undefined }); - let message = `✅ Tarea creada:\nID: ${task.id}\nDescripción: ${task.description}`; - if (task.assignedTo) message += `\nAsignada a: ${task.assignedTo}`; - if (task.dueDate) message += `\nFecha límite: ${task.dueDate}`; - message += `\nCreada: ${task.createdAt}`; + // Format the message with proper text property + const message = { + text: `✅ Tarea creada:\nID: ${task.id}\nDescripción: ${task.description}` + + (task.assignedTo ? `\nAsignada a: ${formatUserMention(task.assignedTo)}` : '') + + (task.dueDate ? `\nFecha límite: ${task.dueDate}` : '') + + `\nCreada: ${task.createdAt}` + }; // Send confirmation privately to creator - sendMessage(sender, message); + sendMessage(sender, message.text); // Also notify assignee if different from creator if (assignedUser && assignedUser !== `@${sender.split('@')[0]}`) { - sendMessage(assignedUser, `📝 Se te ha asignado una nueva tarea:\n${message}`, [assignedUser.split('@')[0]]); + const assigneeMessage = { + text: `📝 Se te ha asignado una nueva tarea:\n${message.text}`, + mentions: [assignedUser] + }; + sendMessage(assignedUser, assigneeMessage.text, assigneeMessage.mentions); } } catch (error) { console.error('Error creating task:', error); diff --git a/src/services/taskService.ts b/src/services/taskService.ts index f2faa1a..a049615 100644 --- a/src/services/taskService.ts +++ b/src/services/taskService.ts @@ -14,7 +14,8 @@ export function createTask(sender: string, params: CreateTaskParams) { if (assignedTo && !assignedTo.endsWith('@g.us')) { assignedTo = normalizeUserIdentifier(assignedTo); } - if (!params.description || params.description.trim().length < 3) { + const cleanDescription = params.description?.trim() || ''; + if (cleanDescription.length < 3) { throw new Error('La descripción de la tarea debe tener al menos 3 caracteres'); }