Skip to content

Commit

Permalink
Merge pull request #24 from LucaBernstein/no-cached-date-hints
Browse files Browse the repository at this point in the history
Don't show suggestions for past dates
  • Loading branch information
LucaBernstein authored Nov 24, 2021
2 parents 81c7772 + 132be70 commit c0b7fa8
Show file tree
Hide file tree
Showing 7 changed files with 103 additions and 39 deletions.
15 changes: 3 additions & 12 deletions bot/suggestions.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"fmt"
"strings"

"github.com/LucaBernstein/beancount-bot-tg/helpers"
. "github.com/LucaBernstein/beancount-bot-tg/helpers"
tb "gopkg.in/tucnak/telebot.v2"
)

Expand Down Expand Up @@ -32,7 +32,7 @@ func (bc *BotController) suggestionsHandler(m *tb.Message) {
return
}

if !helpers.ArrayContainsC(allowedSuggestionTypes(), suggType, false) {
if !ArrayContainsC(AllowedSuggestionTypes(), suggType, false) {
bc.suggestionsHelp(m)
return
}
Expand All @@ -50,7 +50,7 @@ func (bc *BotController) suggestionsHandler(m *tb.Message) {
}

func (bc *BotController) suggestionsHelp(m *tb.Message) {
suggestionTypes := strings.Join(allowedSuggestionTypes(), ", ")
suggestionTypes := strings.Join(AllowedSuggestionTypes(), ", ")
bc.Bot.Send(m.Sender, fmt.Sprintf(`Usage help for /suggestions:
/suggestions list <type>
/suggestions add <type> <value>
Expand Down Expand Up @@ -94,12 +94,3 @@ func (bc *BotController) suggestionsHandleRemove(m *tb.Message, t string, value
}
bc.Bot.Send(m.Sender, "Successfully removed suggestion(s)")
}

func allowedSuggestionTypes() []string {
return []string{
STX_ACCF,
STX_ACCT,
STX_DESC,
STX_DATE,
}
}
33 changes: 6 additions & 27 deletions bot/transactionBuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"unicode/utf8"

"github.com/LucaBernstein/beancount-bot-tg/db/crud"
"github.com/LucaBernstein/beancount-bot-tg/helpers"
. "github.com/LucaBernstein/beancount-bot-tg/helpers"
tb "gopkg.in/tucnak/telebot.v2"
)

Expand Down Expand Up @@ -96,14 +96,6 @@ type SimpleTx struct {
step int
}

const (
STX_DESC = "txDesc"
STX_DATE = "txDate"
STX_ACCF = "accFrom"
STX_AMTF = "amountFrom"
STX_ACCT = "accTo"
)

func CreateSimpleTx() Tx {
return (&SimpleTx{
stepDetails: make(map[command]Input),
Expand All @@ -112,7 +104,7 @@ func CreateSimpleTx() Tx {
addStep("from", "Please enter the account the money came from (or select one from the list)", HandleRaw).
addStep("to", "Please enter the account the money went to (or select one from the list)", HandleRaw).
addStep("description", "Please enter a description (or select one from the list)", HandleRaw).
addStep("date", "Please enter the transaction data in the format YYYY-MM-DD (or select one from the list, e.g. 'today')", HandleDate)
addStep("date", "Please enter the transaction data in the format YYYY-MM-DD (or type 't' / 'today')", HandleDate)
}

func (tx *SimpleTx) addStep(command command, hint string, handler func(m *tb.Message) (string, error)) Tx {
Expand Down Expand Up @@ -146,9 +138,9 @@ func (tx *SimpleTx) EnrichHint(r *crud.Repo, m *tb.Message, i Input) *Hint {
return tx.hintDescription(r, m, i.hint)
}
if i.key == "date" {
return tx.hintDate(r, m, i.hint)
return tx.hintDate(i.hint)
}
if helpers.ArrayContains([]string{"from", "to"}, i.key) {
if ArrayContains([]string{"from", "to"}, i.key) {
return tx.hintAccount(r, m, i)
}
return i.hint
Expand Down Expand Up @@ -182,21 +174,8 @@ func (tx *SimpleTx) hintDescription(r *crud.Repo, m *tb.Message, h *Hint) *Hint
return h
}

func (tx *SimpleTx) hintDate(r *crud.Repo, m *tb.Message, h *Hint) *Hint {
res, err := r.GetCacheHints(m, STX_DATE)
if err != nil {
log.Printf("Error occurred getting cached hint (hintDate): %s", err.Error())
}
selection := []string{"today"}
today := time.Now().Format(BEANCOUNT_DATE_FORMAT)
// Sort out today's date
for _, v := range res {
if v != today {
selection = append(selection, v)
}
}

h.KeyboardOptions = selection
func (tx *SimpleTx) hintDate(h *Hint) *Hint {
h.KeyboardOptions = []string{"today"}
return h
}

Expand Down
4 changes: 4 additions & 0 deletions db/crud/bot_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ func (r *Repo) PutCacheHints(m *tb.Message, values map[string]string) error {
}

for key, value := range values {
if !helpers.ArrayContains(helpers.AllowedSuggestionTypes(), key) {
// Don't cache non-suggestible data
continue
}
if helpers.ArrayContains(CACHE_LOCAL[m.Chat.ID][key], value) {
// TODO: Update all as single statement
_, err = r.db.Exec(`
Expand Down
51 changes: 51 additions & 0 deletions db/crud/bot_cache_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package crud_test

import (
"log"
"testing"

"github.com/DATA-DOG/go-sqlmock"
"github.com/LucaBernstein/beancount-bot-tg/db/crud"
"github.com/LucaBernstein/beancount-bot-tg/helpers"
tb "gopkg.in/tucnak/telebot.v2"
)

func TestCacheOnlySuggestible(t *testing.T) {
// create test dependencies
chat := &tb.Chat{ID: 12345}
db, mock, err := sqlmock.New()
if err != nil {
log.Fatal(err)
}
defer db.Close()
mock.
ExpectQuery(`
SELECT "type", "value"
FROM "bot::cache"
WHERE "tgChatId" = ?`).
WithArgs(chat.ID).
WillReturnRows(sqlmock.NewRows([]string{}))
// Should only insert description suggestion into db cache
mock.
ExpectExec(`INSERT INTO "bot::cache"`).
WithArgs(chat.ID, helpers.STX_DESC, "description_value").
WillReturnResult(sqlmock.NewResult(1, 1))
mock.
ExpectQuery(`
SELECT "type", "value"
FROM "bot::cache"
WHERE "tgChatId" = ?`).
WithArgs(chat.ID).
WillReturnRows(sqlmock.NewRows([]string{}))

bc := crud.NewRepo(db)
message := &tb.Message{Chat: chat}
err = bc.PutCacheHints(message, map[string]string{helpers.STX_DATE: "2021-01-01", helpers.STX_AMTF: "1234", helpers.STX_DESC: "description_value"})
if err != nil {
t.Errorf("PutCacheHints unexpectedly threw an error: %s", err.Error())
}

if err := mock.ExpectationsWereMet(); err != nil {
t.Errorf("there were unfulfilled expectations: %s", err)
}
}
1 change: 1 addition & 0 deletions db/migrations/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ func Migrate(db *sql.DB) {

migrationWrapper(v1, 1)(db)
migrationWrapper(v2, 2)(db)
migrationWrapper(v3, 3)(db)

fmt.Println("Migrations ran through. Schema version:", schema(db))
}
Expand Down
21 changes: 21 additions & 0 deletions db/migrations/v3.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package migrations

import (
"database/sql"
"log"
)

func v3(db *sql.Tx) {
v3CleanCache(db)
}

func v3CleanCache(db *sql.Tx) {
sqlStatement := `
DELETE FROM "bot::cache"
WHERE "type" NOT IN ('accTo', 'accFrom', 'txDesc')
`
_, err := db.Exec(sqlStatement)
if err != nil {
log.Fatal(err)
}
}
17 changes: 17 additions & 0 deletions helpers/constants.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package helpers

const (
STX_DESC = "txDesc"
STX_DATE = "txDate"
STX_ACCF = "accFrom"
STX_AMTF = "amountFrom"
STX_ACCT = "accTo"
)

func AllowedSuggestionTypes() []string {
return []string{
STX_ACCF,
STX_ACCT,
STX_DESC,
}
}

0 comments on commit c0b7fa8

Please sign in to comment.