Skip to content

Commit c0b7fa8

Browse files
Merge pull request #24 from LucaBernstein/no-cached-date-hints
Don't show suggestions for past dates
2 parents 81c7772 + 132be70 commit c0b7fa8

File tree

7 files changed

+103
-39
lines changed

7 files changed

+103
-39
lines changed

bot/suggestions.go

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import (
44
"fmt"
55
"strings"
66

7-
"github.com/LucaBernstein/beancount-bot-tg/helpers"
7+
. "github.com/LucaBernstein/beancount-bot-tg/helpers"
88
tb "gopkg.in/tucnak/telebot.v2"
99
)
1010

@@ -32,7 +32,7 @@ func (bc *BotController) suggestionsHandler(m *tb.Message) {
3232
return
3333
}
3434

35-
if !helpers.ArrayContainsC(allowedSuggestionTypes(), suggType, false) {
35+
if !ArrayContainsC(AllowedSuggestionTypes(), suggType, false) {
3636
bc.suggestionsHelp(m)
3737
return
3838
}
@@ -50,7 +50,7 @@ func (bc *BotController) suggestionsHandler(m *tb.Message) {
5050
}
5151

5252
func (bc *BotController) suggestionsHelp(m *tb.Message) {
53-
suggestionTypes := strings.Join(allowedSuggestionTypes(), ", ")
53+
suggestionTypes := strings.Join(AllowedSuggestionTypes(), ", ")
5454
bc.Bot.Send(m.Sender, fmt.Sprintf(`Usage help for /suggestions:
5555
/suggestions list <type>
5656
/suggestions add <type> <value>
@@ -94,12 +94,3 @@ func (bc *BotController) suggestionsHandleRemove(m *tb.Message, t string, value
9494
}
9595
bc.Bot.Send(m.Sender, "Successfully removed suggestion(s)")
9696
}
97-
98-
func allowedSuggestionTypes() []string {
99-
return []string{
100-
STX_ACCF,
101-
STX_ACCT,
102-
STX_DESC,
103-
STX_DATE,
104-
}
105-
}

bot/transactionBuilder.go

Lines changed: 6 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
"unicode/utf8"
1111

1212
"github.com/LucaBernstein/beancount-bot-tg/db/crud"
13-
"github.com/LucaBernstein/beancount-bot-tg/helpers"
13+
. "github.com/LucaBernstein/beancount-bot-tg/helpers"
1414
tb "gopkg.in/tucnak/telebot.v2"
1515
)
1616

@@ -96,14 +96,6 @@ type SimpleTx struct {
9696
step int
9797
}
9898

99-
const (
100-
STX_DESC = "txDesc"
101-
STX_DATE = "txDate"
102-
STX_ACCF = "accFrom"
103-
STX_AMTF = "amountFrom"
104-
STX_ACCT = "accTo"
105-
)
106-
10799
func CreateSimpleTx() Tx {
108100
return (&SimpleTx{
109101
stepDetails: make(map[command]Input),
@@ -112,7 +104,7 @@ func CreateSimpleTx() Tx {
112104
addStep("from", "Please enter the account the money came from (or select one from the list)", HandleRaw).
113105
addStep("to", "Please enter the account the money went to (or select one from the list)", HandleRaw).
114106
addStep("description", "Please enter a description (or select one from the list)", HandleRaw).
115-
addStep("date", "Please enter the transaction data in the format YYYY-MM-DD (or select one from the list, e.g. 'today')", HandleDate)
107+
addStep("date", "Please enter the transaction data in the format YYYY-MM-DD (or type 't' / 'today')", HandleDate)
116108
}
117109

118110
func (tx *SimpleTx) addStep(command command, hint string, handler func(m *tb.Message) (string, error)) Tx {
@@ -146,9 +138,9 @@ func (tx *SimpleTx) EnrichHint(r *crud.Repo, m *tb.Message, i Input) *Hint {
146138
return tx.hintDescription(r, m, i.hint)
147139
}
148140
if i.key == "date" {
149-
return tx.hintDate(r, m, i.hint)
141+
return tx.hintDate(i.hint)
150142
}
151-
if helpers.ArrayContains([]string{"from", "to"}, i.key) {
143+
if ArrayContains([]string{"from", "to"}, i.key) {
152144
return tx.hintAccount(r, m, i)
153145
}
154146
return i.hint
@@ -182,21 +174,8 @@ func (tx *SimpleTx) hintDescription(r *crud.Repo, m *tb.Message, h *Hint) *Hint
182174
return h
183175
}
184176

185-
func (tx *SimpleTx) hintDate(r *crud.Repo, m *tb.Message, h *Hint) *Hint {
186-
res, err := r.GetCacheHints(m, STX_DATE)
187-
if err != nil {
188-
log.Printf("Error occurred getting cached hint (hintDate): %s", err.Error())
189-
}
190-
selection := []string{"today"}
191-
today := time.Now().Format(BEANCOUNT_DATE_FORMAT)
192-
// Sort out today's date
193-
for _, v := range res {
194-
if v != today {
195-
selection = append(selection, v)
196-
}
197-
}
198-
199-
h.KeyboardOptions = selection
177+
func (tx *SimpleTx) hintDate(h *Hint) *Hint {
178+
h.KeyboardOptions = []string{"today"}
200179
return h
201180
}
202181

db/crud/bot_cache.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ func (r *Repo) PutCacheHints(m *tb.Message, values map[string]string) error {
2323
}
2424

2525
for key, value := range values {
26+
if !helpers.ArrayContains(helpers.AllowedSuggestionTypes(), key) {
27+
// Don't cache non-suggestible data
28+
continue
29+
}
2630
if helpers.ArrayContains(CACHE_LOCAL[m.Chat.ID][key], value) {
2731
// TODO: Update all as single statement
2832
_, err = r.db.Exec(`

db/crud/bot_cache_test.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package crud_test
2+
3+
import (
4+
"log"
5+
"testing"
6+
7+
"github.com/DATA-DOG/go-sqlmock"
8+
"github.com/LucaBernstein/beancount-bot-tg/db/crud"
9+
"github.com/LucaBernstein/beancount-bot-tg/helpers"
10+
tb "gopkg.in/tucnak/telebot.v2"
11+
)
12+
13+
func TestCacheOnlySuggestible(t *testing.T) {
14+
// create test dependencies
15+
chat := &tb.Chat{ID: 12345}
16+
db, mock, err := sqlmock.New()
17+
if err != nil {
18+
log.Fatal(err)
19+
}
20+
defer db.Close()
21+
mock.
22+
ExpectQuery(`
23+
SELECT "type", "value"
24+
FROM "bot::cache"
25+
WHERE "tgChatId" = ?`).
26+
WithArgs(chat.ID).
27+
WillReturnRows(sqlmock.NewRows([]string{}))
28+
// Should only insert description suggestion into db cache
29+
mock.
30+
ExpectExec(`INSERT INTO "bot::cache"`).
31+
WithArgs(chat.ID, helpers.STX_DESC, "description_value").
32+
WillReturnResult(sqlmock.NewResult(1, 1))
33+
mock.
34+
ExpectQuery(`
35+
SELECT "type", "value"
36+
FROM "bot::cache"
37+
WHERE "tgChatId" = ?`).
38+
WithArgs(chat.ID).
39+
WillReturnRows(sqlmock.NewRows([]string{}))
40+
41+
bc := crud.NewRepo(db)
42+
message := &tb.Message{Chat: chat}
43+
err = bc.PutCacheHints(message, map[string]string{helpers.STX_DATE: "2021-01-01", helpers.STX_AMTF: "1234", helpers.STX_DESC: "description_value"})
44+
if err != nil {
45+
t.Errorf("PutCacheHints unexpectedly threw an error: %s", err.Error())
46+
}
47+
48+
if err := mock.ExpectationsWereMet(); err != nil {
49+
t.Errorf("there were unfulfilled expectations: %s", err)
50+
}
51+
}

db/migrations/controller.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ func Migrate(db *sql.DB) {
1212

1313
migrationWrapper(v1, 1)(db)
1414
migrationWrapper(v2, 2)(db)
15+
migrationWrapper(v3, 3)(db)
1516

1617
fmt.Println("Migrations ran through. Schema version:", schema(db))
1718
}

db/migrations/v3.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package migrations
2+
3+
import (
4+
"database/sql"
5+
"log"
6+
)
7+
8+
func v3(db *sql.Tx) {
9+
v3CleanCache(db)
10+
}
11+
12+
func v3CleanCache(db *sql.Tx) {
13+
sqlStatement := `
14+
DELETE FROM "bot::cache"
15+
WHERE "type" NOT IN ('accTo', 'accFrom', 'txDesc')
16+
`
17+
_, err := db.Exec(sqlStatement)
18+
if err != nil {
19+
log.Fatal(err)
20+
}
21+
}

helpers/constants.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package helpers
2+
3+
const (
4+
STX_DESC = "txDesc"
5+
STX_DATE = "txDate"
6+
STX_ACCF = "accFrom"
7+
STX_AMTF = "amountFrom"
8+
STX_ACCT = "accTo"
9+
)
10+
11+
func AllowedSuggestionTypes() []string {
12+
return []string{
13+
STX_ACCF,
14+
STX_ACCT,
15+
STX_DESC,
16+
}
17+
}

0 commit comments

Comments
 (0)