diff --git a/internal/repositories/task_repo.go b/internal/repositories/task_repo.go index 3167b16..9773174 100644 --- a/internal/repositories/task_repo.go +++ b/internal/repositories/task_repo.go @@ -564,10 +564,13 @@ func (r *TaskRepository) GetTaskStatistics(residenceIDs []uint) (*TaskStatistics return nil, err } - // Count overdue tasks (due date < now, no completions) + // Count overdue tasks: due_date or next_due_date < now, and NOT completed + // A task is "completed" if next_due_date IS NULL AND has at least one completion err = r.db.Model(&models.Task{}). - Where("residence_id IN ? AND is_cancelled = ? AND is_archived = ? AND due_date < ?", residenceIDs, false, false, now). - Where("id NOT IN (?)", r.db.Table("task_taskcompletion").Select("task_id")). + Where("residence_id IN ? AND is_cancelled = ? AND is_archived = ?", residenceIDs, false, false). + Where("(due_date < ? OR next_due_date < ?)", now, now). + // Exclude completed tasks: tasks with no next_due_date AND at least one completion + Where("NOT (next_due_date IS NULL AND EXISTS (SELECT 1 FROM task_taskcompletion tc WHERE tc.task_id = task_task.id))"). Count(&totalOverdue).Error if err != nil { return nil, err