Skip to content

Commit

Permalink
Include timezone offset in message (#165)
Browse files Browse the repository at this point in the history
Closes #153
  • Loading branch information
LucaBernstein authored Jul 24, 2022
1 parent 37c9c3f commit 50cd867
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
6 changes: 6 additions & 0 deletions bot/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,12 @@ func (bc *BotController) configHandleTag(m *tb.Message, params ...string) {

func (bc *BotController) configHandleNotification(m *tb.Message, params ...string) {
var tz, _ = time.Now().Zone()
userTzOffset := bc.Repo.UserGetTzOffset(m)
if userTzOffset < 0 {
tz += strconv.Itoa(userTzOffset)
} else {
tz += "+" + strconv.Itoa(userTzOffset)
}
if len(params) == 0 {
// GET schedule
daysDelay, hour, err := bc.Repo.UserGetNotificationSetting(m)
Expand Down
33 changes: 31 additions & 2 deletions bot/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,10 @@ func TestConfigHandleNotification(t *testing.T) {

tz, _ := time.Now().Zone()

mock.
ExpectQuery(`SELECT "value" FROM "bot::userSetting"`).
WithArgs(chat.ID, helpers.USERSET_TZOFF).
WillReturnRows(sqlmock.NewRows([]string{"value"}).AddRow("-7"))
mock.ExpectQuery(`SELECT "delayHours", "notificationHour" FROM "bot::notificationSchedule"`).
WithArgs(chat.ID).
WillReturnRows(sqlmock.NewRows([]string{"delayHours", "notificationHour"}))
Expand All @@ -178,38 +182,63 @@ func TestConfigHandleNotification(t *testing.T) {
t.Errorf("Notifications should be disabled: %s", bot.LastSentWhat)
}

mock.
ExpectQuery(`SELECT "value" FROM "bot::userSetting"`).
WithArgs(chat.ID, helpers.USERSET_TZOFF).
WillReturnRows(sqlmock.NewRows([]string{"value"}).AddRow("3"))
mock.ExpectQuery(`SELECT "delayHours", "notificationHour" FROM "bot::notificationSchedule"`).
WithArgs(chat.ID).
WillReturnRows(sqlmock.NewRows([]string{"delayHours", "notificationHour"}).AddRow(24, 18))
bc.commandConfig(&tb.Message{Text: "/config notify", Chat: chat})
if !strings.Contains(fmt.Sprintf("%v", bot.LastSentWhat),
"The bot will notify you daily at hour 18 ("+tz+") if transactions are open for more than 1 day") {
"The bot will notify you daily at hour 18 ("+tz+"+3) if transactions are open for more than 1 day") {
t.Errorf("Notifications should be disabled: %s", bot.LastSentWhat)
}

mock.
ExpectQuery(`SELECT "value" FROM "bot::userSetting"`).
WithArgs(chat.ID, helpers.USERSET_TZOFF).
WillReturnRows(sqlmock.NewRows([]string{"value"}).AddRow("14"))
bc.commandConfig(&tb.Message{Text: "/config notify 17", Chat: chat})
if !strings.Contains(fmt.Sprintf("%v", bot.LastSentWhat), "invalid parameter") {
t.Errorf("Single number as param should not be allowed: %s", bot.LastSentWhat)
}

mock.
ExpectQuery(`SELECT "value" FROM "bot::userSetting"`).
WithArgs(chat.ID, helpers.USERSET_TZOFF).
WillReturnRows(sqlmock.NewRows([]string{"value"}).AddRow("0"))
mock.ExpectExec(`DELETE FROM "bot::notificationSchedule"`).WithArgs(chat.ID).WillReturnResult(sqlmock.NewResult(1, 1))
bc.commandConfig(&tb.Message{Text: "/config notify off", Chat: chat})
if !strings.Contains(fmt.Sprintf("%v", bot.LastSentWhat), "Successfully disabled notifications") {
t.Errorf("Single param should be allowed for 'off' to disable notifications: %s", bot.LastSentWhat)
}

mock.
ExpectQuery(`SELECT "value" FROM "bot::userSetting"`).
WithArgs(chat.ID, helpers.USERSET_TZOFF).
WillReturnRows(sqlmock.NewRows([]string{"value"}).AddRow("-2"))
mock.ExpectExec(`DELETE FROM "bot::notificationSchedule"`).WithArgs(chat.ID).WillReturnResult(sqlmock.NewResult(1, 1))
mock.ExpectExec(`INSERT INTO "bot::notificationSchedule"`).WithArgs(chat.ID, 4*24, 23).WillReturnResult(sqlmock.NewResult(1, 1))
// Recursively called:
mock.
ExpectQuery(`SELECT "value" FROM "bot::userSetting"`).
WithArgs(chat.ID, helpers.USERSET_TZOFF).
WillReturnRows(sqlmock.NewRows([]string{"value"}).AddRow("-2"))
mock.ExpectQuery(`SELECT "delayHours", "notificationHour" FROM "bot::notificationSchedule"`).
WithArgs(chat.ID).
WillReturnRows(sqlmock.NewRows([]string{"delayHours", "notificationHour"}).AddRow(4*24, 23))
bc.commandConfig(&tb.Message{Text: "/config notify 4 23", Chat: chat})
if !strings.Contains(fmt.Sprintf("%v", bot.LastSentWhat),
"The bot will notify you daily at hour 23 ("+tz+") if transactions are open for more than 4 days") {
"The bot will notify you daily at hour 23 ("+tz+"-2) if transactions are open for more than 4 days") {
t.Errorf("Should successfully set notification: %s", bot.LastSentWhat)
}

// Invalid hour (0-23)
mock.
ExpectQuery(`SELECT "value" FROM "bot::userSetting"`).
WithArgs(chat.ID, helpers.USERSET_TZOFF).
WillReturnRows(sqlmock.NewRows([]string{"value"}).AddRow("1"))
bc.commandConfig(&tb.Message{Text: "/config notify 4 24", Chat: chat})
if !strings.Contains(fmt.Sprintf("%v", bot.LastSentWhat),
"invalid hour (24 is out of valid range 1-23)") {
Expand Down

0 comments on commit 50cd867

Please sign in to comment.