fix: sync sadhna calendar with local timezone
This commit is contained in:
@@ -243,18 +243,22 @@ func (r *PostRepo) PostedDates(ctx context.Context, userID string, since time.Ti
|
||||
return out, rows.Err()
|
||||
}
|
||||
|
||||
// PostCountsByDate returns, for each UTC calendar day on or after `since`,
|
||||
// how many posts the user created that day. Keys are "YYYY-MM-DD"; days
|
||||
// with no posts are absent (so a missing key reads as zero). The sādhana
|
||||
// vine uses this both for the "posted a quote today" discipline and to
|
||||
// annotate each day's fold with the number of quotes shared.
|
||||
func (r *PostRepo) PostCountsByDate(ctx context.Context, userID string, since time.Time) (map[string]int, error) {
|
||||
// PostCountsByDate returns, for each calendar day on or after `since`, how
|
||||
// many posts the user created that day — bucketed in the given IANA timezone
|
||||
// (`tz`) so the day boundary matches the user's local midnight. Keys are
|
||||
// "YYYY-MM-DD"; days with no posts are absent (so a missing key reads as
|
||||
// zero). The sādhana page uses this for the "posted a quote today" discipline
|
||||
// and to annotate each day's fold with the number of quotes shared.
|
||||
func (r *PostRepo) PostCountsByDate(ctx context.Context, userID string, since time.Time, tz string) (map[string]int, error) {
|
||||
if tz == "" {
|
||||
tz = "UTC"
|
||||
}
|
||||
rows, err := r.pg.Pool.Query(ctx, `
|
||||
SELECT (created_at AT TIME ZONE 'UTC')::date AS d, count(*) AS n
|
||||
SELECT (created_at AT TIME ZONE $3)::date AS d, count(*) AS n
|
||||
FROM posts
|
||||
WHERE user_id = $1 AND created_at >= $2
|
||||
GROUP BY d`,
|
||||
userID, since,
|
||||
userID, since, tz,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("post counts by date: %w", err)
|
||||
|
||||
@@ -15,6 +15,20 @@ type SadhnaRepo struct{ pg *Postgres }
|
||||
|
||||
func NewSadhnaRepo(pg *Postgres) *SadhnaRepo { return &SadhnaRepo{pg: pg} }
|
||||
|
||||
// UserTimezone returns the user's stored IANA timezone, or "" if unset — the
|
||||
// caller falls back to a default. Kept here so the sādhana day boundary can
|
||||
// be the user's local midnight.
|
||||
func (r *SadhnaRepo) UserTimezone(ctx context.Context, userID string) (string, error) {
|
||||
var tz string
|
||||
err := r.pg.Pool.QueryRow(ctx,
|
||||
`SELECT COALESCE(NULLIF(timezone, ''), '') FROM users WHERE id = $1`, userID,
|
||||
).Scan(&tz)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return tz, nil
|
||||
}
|
||||
|
||||
// UpsertToday records (or updates) the given day's chanting progress for
|
||||
// the user. The caller passes the UTC calendar date explicitly (rather
|
||||
// than relying on the DB's CURRENT_DATE) so day bucketing stays identical
|
||||
|
||||
Reference in New Issue
Block a user