From 5855d7c54592cbb01f618547cddabcce0df04435 Mon Sep 17 00:00:00 2001 From: Callum Alpass Date: Mon, 9 Jun 2025 14:35:50 +1000 Subject: [PATCH] Enhance overdue task logic to check both due and scheduled dates MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit • In FilterService.ts: - Updated the overdue task filtering logic to include both due and scheduled dates. - Previously, only the due date was checked for tasks marked as overdue on the current day. - Now, if the task is due today and either the due date or scheduled date is overdue, the task will be flagged. • In CacheManager.ts: - Modified the logic for indexing overdue tasks. - Previously, only the due date was considered to determine if a task should be added to the overdue index. - Now, the method checks both due and scheduled dates for overdue comparisons before adding the task to the overdue index. • In helpers.ts: - Updated the isTaskOverdue() helper function to support both task.due and task.scheduled. - The function now safely attempts to parse and compare the dates for both fields. - If either date is found to be in the past relative to the start of today, the task is considered overdue. - Added error handling to log any issues encountered during date parsing. These changes improve overdue task detection by ensuring that both due and scheduled dates are taken into account, leading to more robust scheduling and task management. --- src/services/FilterService.ts | 19 ++++++++++++++++--- src/utils/CacheManager.ts | 25 +++++++++++++++++++++---- src/utils/helpers.ts | 35 +++++++++++++++++++++++------------ 3 files changed, 60 insertions(+), 19 deletions(-) diff --git a/src/services/FilterService.ts b/src/services/FilterService.ts index db2930b3..0dddeb1b 100644 --- a/src/services/FilterService.ts +++ b/src/services/FilterService.ts @@ -682,9 +682,22 @@ export class FilterService extends EventEmitter { } // If showing overdue tasks and this is today, include overdue tasks - if (includeOverdue && isToday(dateOnlyDate) && task.due) { - const taskDueDate = parseISO(task.due); - return isBefore(taskDueDate, dateOnlyDate); + if (includeOverdue && isToday(dateOnlyDate)) { + // Check if due date is overdue + if (task.due) { + const taskDueDate = parseISO(task.due); + if (isBefore(taskDueDate, dateOnlyDate)) { + return true; + } + } + + // Check if scheduled date is overdue + if (task.scheduled) { + const taskScheduledDate = parseISO(task.scheduled); + if (isBefore(taskScheduledDate, dateOnlyDate)) { + return true; + } + } } return false; diff --git a/src/utils/CacheManager.ts b/src/utils/CacheManager.ts index 6d7a2195..37e79e80 100644 --- a/src/utils/CacheManager.ts +++ b/src/utils/CacheManager.ts @@ -1381,13 +1381,30 @@ export class CacheManager { // Remove from overdue index first this.overdueTasks.delete(path); - // Add to overdue index if task is overdue - if (taskInfo.due && !taskInfo.recurrence) { - const taskDueDate = new Date(taskInfo.due); + // Add to overdue index if task is overdue (check both due and scheduled dates) + if (!taskInfo.recurrence) { const today = new Date(); today.setHours(0, 0, 0, 0); // Start of today - if (taskDueDate < today) { + let isOverdue = false; + + // Check due date + if (taskInfo.due) { + const taskDueDate = new Date(taskInfo.due); + if (taskDueDate < today) { + isOverdue = true; + } + } + + // Check scheduled date + if (!isOverdue && taskInfo.scheduled) { + const taskScheduledDate = new Date(taskInfo.scheduled); + if (taskScheduledDate < today) { + isOverdue = true; + } + } + + if (isOverdue) { this.overdueTasks.add(path); } } diff --git a/src/utils/helpers.ts b/src/utils/helpers.ts index f7be8c10..f6ace243 100644 --- a/src/utils/helpers.ts +++ b/src/utils/helpers.ts @@ -470,21 +470,32 @@ export function extractTaskInfo( } /** - * Checks if a task is overdue + * Checks if a task is overdue (either due date or scheduled date is in the past) */ -export function isTaskOverdue(task: {due?: string}): boolean { - if (!task.due) return false; +export function isTaskOverdue(task: {due?: string; scheduled?: string}): boolean { + const today = startOfDay(new Date()); - try { - // Safely parse the date string using date-fns - const dueDate = startOfDay(parseISO(task.due)); - const today = startOfDay(new Date()); - - return isBefore(dueDate, today); // Use date-fns for comparison - } catch (error) { - console.error(`Error parsing due date ${task.due}:`, error); - return false; + // Check due date + if (task.due) { + try { + const dueDate = startOfDay(parseISO(task.due)); + if (isBefore(dueDate, today)) return true; + } catch (error) { + console.error(`Error parsing due date ${task.due}:`, error); + } } + + // Check scheduled date + if (task.scheduled) { + try { + const scheduledDate = startOfDay(parseISO(task.scheduled)); + if (isBefore(scheduledDate, today)) return true; + } catch (error) { + console.error(`Error parsing scheduled date ${task.scheduled}:`, error); + } + } + + return false; } /**