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

@@ -36,6 +36,12 @@ func (h *TaskHandler) ListTasks(c echo.Context) error {
user := c.Get(middleware.AuthUserKey).(*models.User) user := c.Get(middleware.AuthUserKey).(*models.User)
userNow := middleware.GetUserNow(c) 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) response, err := h.taskService.ListTasks(user.ID, userNow)
if err != nil { if err != nil {
return err return err

View File

@@ -33,6 +33,10 @@ type NotificationPreference struct {
TaskOverdueHour *int `gorm:"column:task_overdue_hour" json:"task_overdue_hour"` TaskOverdueHour *int `gorm:"column:task_overdue_hour" json:"task_overdue_hour"`
WarrantyExpiringHour *int `gorm:"column:warranty_expiring_hour" json:"warranty_expiring_hour"` WarrantyExpiringHour *int `gorm:"column:warranty_expiring_hour" json:"warranty_expiring_hour"`
DailyDigestHour *int `gorm:"column:daily_digest_hour" json:"daily_digest_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 // TableName returns the table name for GORM

View File

@@ -5,6 +5,7 @@ import (
"encoding/json" "encoding/json"
"errors" "errors"
"strconv" "strconv"
"time"
"gorm.io/gorm" "gorm.io/gorm"
@@ -237,6 +238,28 @@ func (s *NotificationService) UpdatePreferences(userID uint, req *UpdatePreferen
return NewNotificationPreferencesResponse(prefs), nil 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 === // === Device Registration ===
// RegisterDevice registers a device for push notifications // RegisterDevice registers a device for push notifications

View File

@@ -996,3 +996,14 @@ func (s *TaskService) GetFrequencies() ([]responses.TaskFrequencyResponse, error
} }
return result, nil 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)
}
}

View File

@@ -319,6 +319,23 @@ func (h *Handler) HandleDailyDigest(ctx context.Context, task *asynq.Task) error
var usersNotified int var usersNotified int
for _, userID := range eligibleUserIDs { 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) // Get user's residence IDs (owned + member)
residenceIDs, err := h.residenceRepo.FindResidenceIDsByUser(userID) residenceIDs, err := h.residenceRepo.FindResidenceIDsByUser(userID)
if err != nil { 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) // Query overdue tasks using canonical scopes (excludes in-progress)
// Uses userNow (timezone-aware) for accurate overdue detection
opts := repositories.TaskFilterOptions{ opts := repositories.TaskFilterOptions{
ResidenceIDs: residenceIDs, ResidenceIDs: residenceIDs,
IncludeInProgress: false, // Match kanban: in-progress tasks not in overdue column IncludeInProgress: false, // Match kanban: in-progress tasks not in overdue column
PreloadCompletions: true, PreloadCompletions: true,
} }
overdueTasks, err := h.taskRepo.GetOverdueTasks(now, opts) overdueTasks, err := h.taskRepo.GetOverdueTasks(userNow, opts)
if err != nil { if err != nil {
log.Error().Err(err).Uint("user_id", userID).Msg("Failed to get overdue tasks") log.Error().Err(err).Uint("user_id", userID).Msg("Failed to get overdue tasks")
continue continue
} }
// Query due-this-week tasks (7 days threshold) // 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 { if err != nil {
log.Error().Err(err).Uint("user_id", userID).Msg("Failed to get due-soon tasks") log.Error().Err(err).Uint("user_id", userID).Msg("Failed to get due-soon tasks")
continue continue

View File

@@ -0,0 +1,3 @@
-- Remove timezone column from notification preferences
ALTER TABLE notifications_notificationpreference
DROP COLUMN IF EXISTS timezone;

View File

@@ -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)';