feat: notification enhancement
This commit is contained in:
@@ -44,30 +44,14 @@ func (r *ReminderRepo) SetTimezone(ctx context.Context, userID, tz string) error
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetEnabled returns whether the user wants sādhana reminders.
|
||||
func (r *ReminderRepo) GetEnabled(ctx context.Context, userID string) (bool, error) {
|
||||
var enabled bool
|
||||
err := r.pg.Pool.QueryRow(ctx,
|
||||
`SELECT sadhna_reminders_enabled FROM users WHERE id = $1`, userID).Scan(&enabled)
|
||||
if err != nil {
|
||||
return false, fmt.Errorf("get reminder pref: %w", err)
|
||||
}
|
||||
return enabled, nil
|
||||
}
|
||||
|
||||
// SetEnabled flips the user's reminder opt-in.
|
||||
func (r *ReminderRepo) SetEnabled(ctx context.Context, userID string, enabled bool) error {
|
||||
_, err := r.pg.Pool.Exec(ctx,
|
||||
`UPDATE users SET sadhna_reminders_enabled = $1 WHERE id = $2`, enabled, userID)
|
||||
if err != nil {
|
||||
return fmt.Errorf("set reminder pref: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// DueCandidates returns reminder-enabled users who haven't already had BOTH
|
||||
// reminders today, with the data the worker needs to decide. Defaults are
|
||||
// folded in (06:00 chant, 21:30 quote, UTC tz).
|
||||
//
|
||||
// "Reminder-enabled" now reads notification_prefs: a user drops out of the
|
||||
// scan only when every reminder kind × channel cell (3 × 3) is opted out.
|
||||
// Finer-grained opt-outs (e.g. email off, push on) still match here —
|
||||
// SendReminder enforces the per-channel prefs on delivery.
|
||||
func (r *ReminderRepo) DueCandidates(ctx context.Context) ([]ReminderCandidate, error) {
|
||||
rows, err := r.pg.Pool.Query(ctx, `
|
||||
SELECT
|
||||
@@ -92,7 +76,10 @@ func (r *ReminderRepo) DueCandidates(ctx context.Context) ([]ReminderCandidate,
|
||||
COALESCE(u.daily_chanting_rounds, 16),
|
||||
u.last_chant_incomplete_reminder_on
|
||||
FROM users u
|
||||
WHERE u.sadhna_reminders_enabled = TRUE
|
||||
WHERE (SELECT count(*) FROM notification_prefs np
|
||||
WHERE np.user_id = u.id
|
||||
AND np.kind IN ('chant_reminder', 'quote_reminder', 'chant_incomplete_reminder')
|
||||
AND np.enabled = FALSE) < 9
|
||||
AND (
|
||||
u.last_chant_reminder_on IS NULL OR u.last_chant_reminder_on < (now() AT TIME ZONE 'UTC')::date
|
||||
OR u.last_quote_reminder_on IS NULL OR u.last_quote_reminder_on < (now() AT TIME ZONE 'UTC')::date
|
||||
|
||||
Reference in New Issue
Block a user