Add timezone-aware daily digest notifications

The daily digest notification count was inconsistent with the kanban UI
because the server used UTC time while the client used local time.
A task due Dec 24 would appear overdue on the server (UTC Dec 25) but
still show as "due today" for the user (local Dec 24).

Changes:
- Add timezone column to notification_preference table
- Auto-capture user's timezone from X-Timezone header when fetching tasks
- Use stored timezone in HandleDailyDigest for accurate overdue calculation

The mobile app already sends X-Timezone on every request, so no client
changes are needed. The timezone is captured on each app launch when
the tasks API is called.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
Trey t
2025-12-24 22:48:34 -06:00
parent 12ae11ca59
commit 70a56e8e96
7 changed files with 74 additions and 2 deletions

View File

@@ -319,6 +319,23 @@ func (h *Handler) HandleDailyDigest(ctx context.Context, task *asynq.Task) error
var usersNotified int
for _, userID := range eligibleUserIDs {
// Get user's timezone from notification preferences for accurate overdue calculation
// This ensures the daily digest matches what the user sees in the kanban UI
var userNow time.Time
var prefs models.NotificationPreference
if err := h.db.Where("user_id = ?", userID).First(&prefs).Error; err == nil && prefs.Timezone != nil {
if loc, err := time.LoadLocation(*prefs.Timezone); err == nil {
// Use start of day in user's timezone (matches kanban behavior)
userNowInTz := time.Now().In(loc)
userNow = time.Date(userNowInTz.Year(), userNowInTz.Month(), userNowInTz.Day(), 0, 0, 0, 0, loc)
log.Debug().Uint("user_id", userID).Str("timezone", *prefs.Timezone).Time("user_now", userNow).Msg("Using user timezone for daily digest")
} else {
userNow = now // Fallback to UTC
}
} else {
userNow = now // Fallback to UTC if no timezone stored
}
// Get user's residence IDs (owned + member)
residenceIDs, err := h.residenceRepo.FindResidenceIDsByUser(userID)
if err != nil {
@@ -331,20 +348,21 @@ func (h *Handler) HandleDailyDigest(ctx context.Context, task *asynq.Task) error
}
// Query overdue tasks using canonical scopes (excludes in-progress)
// Uses userNow (timezone-aware) for accurate overdue detection
opts := repositories.TaskFilterOptions{
ResidenceIDs: residenceIDs,
IncludeInProgress: false, // Match kanban: in-progress tasks not in overdue column
PreloadCompletions: true,
}
overdueTasks, err := h.taskRepo.GetOverdueTasks(now, opts)
overdueTasks, err := h.taskRepo.GetOverdueTasks(userNow, opts)
if err != nil {
log.Error().Err(err).Uint("user_id", userID).Msg("Failed to get overdue tasks")
continue
}
// Query due-this-week tasks (7 days threshold)
dueSoonTasks, err := h.taskRepo.GetDueSoonTasks(now, 7, opts)
dueSoonTasks, err := h.taskRepo.GetDueSoonTasks(userNow, 7, opts)
if err != nil {
log.Error().Err(err).Uint("user_id", userID).Msg("Failed to get due-soon tasks")
continue