feat: add reminder system with user preferences support
parent
7e0ebfe5ac
commit
68ace19f51
@ -0,0 +1,49 @@
|
|||||||
|
import { query } from '../database/db';
|
||||||
|
import { sendMessage } from '../bot/utils/messaging';
|
||||||
|
import { Task } from '../models/task';
|
||||||
|
import { getUserPreferences } from './userService';
|
||||||
|
|
||||||
|
export function getTasksNeedingReminders(): Task[] {
|
||||||
|
const now = new Date().toISOString();
|
||||||
|
const tasks = query(`
|
||||||
|
SELECT t.*
|
||||||
|
FROM tasks t
|
||||||
|
JOIN user_preferences up ON t.assigned_to = up.phone_number
|
||||||
|
WHERE t.completed = FALSE
|
||||||
|
AND (t.due_date IS NULL OR t.due_date > ?)
|
||||||
|
AND (t.last_reminder IS NULL OR t.last_reminder < datetime(?, '-1 day'))
|
||||||
|
AND up.reminders_enabled = TRUE
|
||||||
|
`, [now, now]);
|
||||||
|
|
||||||
|
// Also include tasks for users who haven't set preferences yet
|
||||||
|
const newUsersTasks = query(`
|
||||||
|
SELECT t.*
|
||||||
|
FROM tasks t
|
||||||
|
LEFT JOIN user_preferences up ON t.assigned_to = up.phone_number
|
||||||
|
WHERE t.completed = FALSE
|
||||||
|
AND (t.due_date IS NULL OR t.due_date > ?)
|
||||||
|
AND (t.last_reminder IS NULL OR t.last_reminder < datetime(?, '-1 day'))
|
||||||
|
AND up.phone_number IS NULL
|
||||||
|
`, [now, now]);
|
||||||
|
|
||||||
|
return [...tasks, ...newUsersTasks];
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function sendTaskReminder(task: Task) {
|
||||||
|
if (!task.assignedTo) return;
|
||||||
|
|
||||||
|
const message = `🔔 Recordatorio de tarea:\n` +
|
||||||
|
`ID: ${task.id}\n` +
|
||||||
|
`Descripción: ${task.description}\n` +
|
||||||
|
(task.dueDate ? `Fecha límite: ${task.dueDate}\n` : '') +
|
||||||
|
`Creada: ${task.createdAt}\n\n` +
|
||||||
|
`Usa /tarea completar ${task.id} cuando la termines.`;
|
||||||
|
|
||||||
|
try {
|
||||||
|
await sendMessage(task.assignedTo, message);
|
||||||
|
// Update last reminder timestamp
|
||||||
|
query('UPDATE tasks SET last_reminder = CURRENT_TIMESTAMP WHERE id = ?', [task.id]);
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Error sending reminder:', error);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue