diff --git a/app/api/twenty-crm/tasks/route.ts b/app/api/twenty-crm/tasks/route.ts index e2711f4..4aa92d6 100644 --- a/app/api/twenty-crm/tasks/route.ts +++ b/app/api/twenty-crm/tasks/route.ts @@ -221,8 +221,11 @@ async function fetchTwentyTasks(): Promise { }); // Filter client-side for overdue tasks (dueAt < today) and not completed (status !== 'Done') - const todayForFilter = new Date(); - todayForFilter.setHours(0, 0, 0, 0); + // Use local date for comparison to avoid timezone issues + const now = new Date(); + const todayYear = now.getFullYear(); + const todayMonth = now.getMonth(); + const todayDay = now.getDate(); const tasks: TwentyTask[] = allTasks .filter((task: TwentyTask) => { @@ -237,15 +240,24 @@ async function fetchTwentyTasks(): Promise { return false; // Exclude tasks without due date } + // Parse the due date and compare dates (not times) + // Use local date comparison to avoid timezone issues const taskDueDate = new Date(task.dueAt); - taskDueDate.setHours(0, 0, 0, 0); + // Get local date components (year, month, day) ignoring time + const taskYear = taskDueDate.getFullYear(); + const taskMonth = taskDueDate.getMonth(); + const taskDay = taskDueDate.getDate(); - const isOverdue = taskDueDate < todayForFilter; - logger.debug('[TWENTY_CRM_TASKS] Task date check', { + // Compare dates: task is overdue if its date is before today's date + const isOverdue = taskYear < todayYear || + (taskYear === todayYear && taskMonth < todayMonth) || + (taskYear === todayYear && taskMonth === todayMonth && taskDay < todayDay); + + logger.error('[TWENTY_CRM_TASKS] Task date check', { id: task.id, dueAt: task.dueAt, - taskDueDate: taskDueDate.toISOString(), - today: todayForFilter.toISOString(), + taskDate: `${taskYear}-${String(taskMonth + 1).padStart(2, '0')}-${String(taskDay).padStart(2, '0')}`, + todayDate: `${todayYear}-${String(todayMonth + 1).padStart(2, '0')}-${String(todayDay).padStart(2, '0')}`, isOverdue, });