This commit is contained in:
Trey t
2025-12-07 10:41:55 -06:00
parent efc6118a98
commit f0c7b070d7

View File

@@ -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