Add per-residence overdue_count to API response
Adds overdueCount field to each residence in my-residences endpoint, enabling the mobile app to show pulsing icons on individual residence cards that have overdue tasks. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -502,3 +502,37 @@ func (r *TaskRepository) GetTaskStatistics(residenceIDs []uint) (*TaskStatistics
|
||||
TasksDueNextMonth: int(tasksDueNextMonth),
|
||||
}, nil
|
||||
}
|
||||
|
||||
// GetOverdueCountByResidence returns a map of residence ID to overdue task count.
|
||||
// Uses the task.scopes package for consistent filtering logic.
|
||||
func (r *TaskRepository) GetOverdueCountByResidence(residenceIDs []uint) (map[uint]int, error) {
|
||||
if len(residenceIDs) == 0 {
|
||||
return map[uint]int{}, nil
|
||||
}
|
||||
|
||||
now := time.Now().UTC()
|
||||
|
||||
// Query to get overdue count grouped by residence
|
||||
type result struct {
|
||||
ResidenceID uint
|
||||
Count int64
|
||||
}
|
||||
var results []result
|
||||
|
||||
err := r.db.Model(&models.Task{}).
|
||||
Select("residence_id, COUNT(*) as count").
|
||||
Scopes(task.ScopeForResidences(residenceIDs), task.ScopeOverdue(now)).
|
||||
Group("residence_id").
|
||||
Scan(&results).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Convert to map
|
||||
countMap := make(map[uint]int)
|
||||
for _, r := range results {
|
||||
countMap[r.ResidenceID] = int(r.Count)
|
||||
}
|
||||
|
||||
return countMap, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user