From 70a56e8e96d9d118dc506c911bdf95f1b6824216 Mon Sep 17 00:00:00 2001 From: Trey t Date: Wed, 24 Dec 2025 22:48:34 -0600 Subject: [PATCH] Add timezone-aware daily digest notifications MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- internal/handlers/task_handler.go | 6 +++++ internal/models/notification.go | 4 ++++ internal/services/notification_service.go | 23 +++++++++++++++++++ internal/services/task_service.go | 11 +++++++++ internal/worker/jobs/handler.go | 22 ++++++++++++++++-- ...ezone_to_notification_preferences.down.sql | 3 +++ ...imezone_to_notification_preferences.up.sql | 7 ++++++ 7 files changed, 74 insertions(+), 2 deletions(-) create mode 100644 migrations/011_add_timezone_to_notification_preferences.down.sql create mode 100644 migrations/011_add_timezone_to_notification_preferences.up.sql diff --git a/internal/handlers/task_handler.go b/internal/handlers/task_handler.go index c6a065e..41a9b06 100644 --- a/internal/handlers/task_handler.go +++ b/internal/handlers/task_handler.go @@ -36,6 +36,12 @@ func (h *TaskHandler) ListTasks(c echo.Context) error { user := c.Get(middleware.AuthUserKey).(*models.User) userNow := middleware.GetUserNow(c) + // Auto-capture timezone from header for background job calculations (e.g., daily digest) + // This runs in a goroutine to avoid blocking the response + if tzHeader := c.Request().Header.Get("X-Timezone"); tzHeader != "" { + go h.taskService.UpdateUserTimezone(user.ID, tzHeader) + } + response, err := h.taskService.ListTasks(user.ID, userNow) if err != nil { return err diff --git a/internal/models/notification.go b/internal/models/notification.go index 092c53d..43f62ad 100644 --- a/internal/models/notification.go +++ b/internal/models/notification.go @@ -33,6 +33,10 @@ type NotificationPreference struct { TaskOverdueHour *int `gorm:"column:task_overdue_hour" json:"task_overdue_hour"` WarrantyExpiringHour *int `gorm:"column:warranty_expiring_hour" json:"warranty_expiring_hour"` DailyDigestHour *int `gorm:"column:daily_digest_hour" json:"daily_digest_hour"` + + // User timezone for background job calculations (IANA name, e.g., "America/Los_Angeles") + // Auto-captured from X-Timezone header on API calls + Timezone *string `gorm:"column:timezone;type:varchar(50)" json:"timezone"` } // TableName returns the table name for GORM diff --git a/internal/services/notification_service.go b/internal/services/notification_service.go index b873889..3b5d1f3 100644 --- a/internal/services/notification_service.go +++ b/internal/services/notification_service.go @@ -5,6 +5,7 @@ import ( "encoding/json" "errors" "strconv" + "time" "gorm.io/gorm" @@ -237,6 +238,28 @@ func (s *NotificationService) UpdatePreferences(userID uint, req *UpdatePreferen return NewNotificationPreferencesResponse(prefs), nil } +// UpdateUserTimezone updates the user's timezone for background job calculations. +// This is called automatically when the user makes API calls (e.g., fetching tasks). +// The timezone should be an IANA timezone name (e.g., "America/Los_Angeles"). +func (s *NotificationService) UpdateUserTimezone(userID uint, timezone string) { + // Validate timezone is a valid IANA name + if _, err := time.LoadLocation(timezone); err != nil { + return // Invalid timezone, skip silently + } + + // Get or create preferences and update timezone + prefs, err := s.notificationRepo.GetOrCreatePreferences(userID) + if err != nil { + return // Skip silently on error + } + + // Only update if timezone changed (avoid unnecessary DB writes) + if prefs.Timezone == nil || *prefs.Timezone != timezone { + prefs.Timezone = &timezone + _ = s.notificationRepo.UpdatePreferences(prefs) + } +} + // === Device Registration === // RegisterDevice registers a device for push notifications diff --git a/internal/services/task_service.go b/internal/services/task_service.go index 5f7aa13..f8f2d46 100644 --- a/internal/services/task_service.go +++ b/internal/services/task_service.go @@ -996,3 +996,14 @@ func (s *TaskService) GetFrequencies() ([]responses.TaskFrequencyResponse, error } return result, nil } + +// === Timezone === + +// UpdateUserTimezone updates the user's timezone for background job calculations. +// This is called from handlers when the X-Timezone header is present. +// Delegates to NotificationService since timezone is stored in notification preferences. +func (s *TaskService) UpdateUserTimezone(userID uint, timezone string) { + if s.notificationService != nil { + s.notificationService.UpdateUserTimezone(userID, timezone) + } +} diff --git a/internal/worker/jobs/handler.go b/internal/worker/jobs/handler.go index c8940c0..fe0a647 100644 --- a/internal/worker/jobs/handler.go +++ b/internal/worker/jobs/handler.go @@ -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 diff --git a/migrations/011_add_timezone_to_notification_preferences.down.sql b/migrations/011_add_timezone_to_notification_preferences.down.sql new file mode 100644 index 0000000..4c9139d --- /dev/null +++ b/migrations/011_add_timezone_to_notification_preferences.down.sql @@ -0,0 +1,3 @@ +-- Remove timezone column from notification preferences +ALTER TABLE notifications_notificationpreference +DROP COLUMN IF EXISTS timezone; diff --git a/migrations/011_add_timezone_to_notification_preferences.up.sql b/migrations/011_add_timezone_to_notification_preferences.up.sql new file mode 100644 index 0000000..2eabcee --- /dev/null +++ b/migrations/011_add_timezone_to_notification_preferences.up.sql @@ -0,0 +1,7 @@ +-- Add timezone column to notification preferences +-- Stores IANA timezone name (e.g., "America/Los_Angeles") for background job calculations +ALTER TABLE notifications_notificationpreference +ADD COLUMN timezone VARCHAR(50); + +-- Add comment for documentation +COMMENT ON COLUMN notifications_notificationpreference.timezone IS 'IANA timezone name for daily digest calculations (e.g., America/Los_Angeles)';