Skip to content

Commit

Permalink
Honor timezone offset for notifications (#149)
Browse files Browse the repository at this point in the history
Account for offset hours out of valid range on db side

Minor improvements and fixes

Closes #109
  • Loading branch information
LucaBernstein authored Jun 8, 2022
1 parent 7c08c7a commit 3db67ec
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 12 deletions.
8 changes: 4 additions & 4 deletions bot/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,17 +49,17 @@ Create a schedule to be notified of open transactions (i.e. not archived or dele
/{{.CONFIG_COMMAND}} notify - Get current notification status
/{{.CONFIG_COMMAND}} notify off - Disable reminder notifications
/{{.CONFIG_COMMAND}} notify <delay> <hour> - Notify of open transaction after <delay> days at <hour> of the day ({{.TZ}})
/{{.CONFIG_COMMAND}} notify <delay> <hour> - Notify of open transaction after <delay> days at <hour> of the day. Honors configured timezone offset (see below)
Set suggestion cache limits (i.e. only cache new values until limit is reached, then old ones get dismissed if new ones are added):
/{{.CONFIG_COMMAND}} limit - Get currently set cache limits
/{{.CONFIG_COMMAND}} limit <suggestionType> <amount>|off - Set or disable suggestion limit for a type
Set timezone offset from UTC for transactions where date is added automatically:
Timezone offset from {{.TZ}} to honor for notifications and current date (if set automatically) in new transactions:
/{{.CONFIG_COMMAND}} tz_offset - Get current timezone offset from UTC
/{{.CONFIG_COMMAND}} tz_offset <hours> - Set timezone offset from UTC, default 0
/{{.CONFIG_COMMAND}} tz_offset - Get current timezone offset from {{.TZ}} (default 0)
/{{.CONFIG_COMMAND}} tz_offset <hours> - Set timezone offset from {{.TZ}}
Reset your data stored by the bot. WARNING: This action is permanent!
Expand Down
32 changes: 24 additions & 8 deletions db/crud/auth_user.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,19 +181,35 @@ func (r *Repo) UserSetNotificationSetting(m *tb.Message, daysDelay, hour int) er

func (r *Repo) GetUsersToNotify() (*sql.Rows, error) {
return r.db.Query(`
SELECT overdue."tgChatId", overdue."count" overdue, COUNT(tx2.*) "allTx"
SELECT
overdue."tgChatId",
overdue."count" overdue,
COUNT(tx2.*) "allTx"
FROM
(
SELECT DISTINCT u."tgChatId", COUNT(tx.id)
FROM "auth::user" u, "bot::notificationSchedule" s, "bot::transaction" tx
WHERE u."tgChatId" = s."tgChatId" AND s."tgChatId" = tx."tgChatId"
AND tx.archived = FALSE
AND tx.created + INTERVAL '1 hour' * s."delayHours" <= NOW()
AND s."notificationHour" = $1
SELECT DISTINCT
u."tgChatId",
COUNT(tx.id)
FROM
"auth::user" u,
"bot::notificationSchedule" s,
"bot::transaction" tx,
"bot::userSetting" userset
WHERE
u."tgChatId" = s."tgChatId" AND
u."tgChatId" = tx."tgChatId" AND
u."tgChatId" = userset."tgChatId" AND
userset."setting" = 'user.tzOffset' AND
tx.archived = FALSE AND
MOD(s."notificationHour" + 24 + CASE WHEN userset."value" IS NULL THEN 0 ELSE userset."value"::DECIMAL END, 24) = $1 AND
tx.created + INTERVAL '1 hour' * s."delayHours" <= NOW()
GROUP BY u."tgChatId"
) AS overdue,
"bot::transaction" tx2
WHERE tx2."tgChatId" = overdue."tgChatId"
WHERE
tx2."tgChatId" = overdue."tgChatId" AND
tx2.archived = FALSE
GROUP BY overdue."tgChatId", overdue."count"
`, time.Now().Hour())
}

0 comments on commit 3db67ec

Please sign in to comment.