Skip to content

Commit

Permalink
Merge pull request #68 from LucaBernstein/hint-enrich-currency
Browse files Browse the repository at this point in the history
Enrich amount hint with user currency
  • Loading branch information
LucaBernstein authored Dec 18, 2021
2 parents 5a03edf + 50267ea commit 4085bcb
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 10 deletions.
2 changes: 1 addition & 1 deletion bot/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ func (bc *BotController) commandCreateSimpleTx(m *tb.Message) {
if err != nil {
bc.Logf(ERROR, m, "Sending bot message failed: %s", err.Error())
}
tx, err = bc.State.SimpleTx(m) // create new tx
tx, err = bc.State.SimpleTx(m, bc.Repo.UserGetCurrency(m)) // create new tx
if err != nil {
_, err := bc.Bot.Send(m.Sender, "Something went wrong creating your transactions ("+err.Error()+"). Please check /help for usage."+
"\n\nYou can create a simple transaction using this command: /simple [YYYY-MM-DD]\ne.g. /simple 2021-01-24\n"+
Expand Down
4 changes: 4 additions & 0 deletions bot/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ func TestTextHandlingWithoutPriorState(t *testing.T) {
log.Fatal(err)
}
defer db.Close()
mock.
ExpectQuery(`SELECT "currency" FROM "auth::user" WHERE "tgChatId" = ?`).
WithArgs(chat.ID).
WillReturnRows(sqlmock.NewRows([]string{"currency"}).AddRow("TEST_CURRENCY"))
mock.
ExpectQuery(`SELECT "currency" FROM "auth::user" WHERE "tgChatId" = ?`).
WithArgs(chat.ID).
Expand Down
4 changes: 2 additions & 2 deletions bot/stateHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ func (s *StateHandler) Get(m *tb.Message) Tx {
return s.states[(chatId)(m.Chat.ID)]
}

func (s *StateHandler) SimpleTx(m *tb.Message) (Tx, error) {
tx, err := CreateSimpleTx(m)
func (s *StateHandler) SimpleTx(m *tb.Message, suggestedCur string) (Tx, error) {
tx, err := CreateSimpleTx(m, suggestedCur)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion bot/stateHandler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ func TestStateClearing(t *testing.T) {
message := &tb.Message{Chat: &tb.Chat{ID: 24}}
stateHandler := bot.NewStateHandler()

stateHandler.SimpleTx(message)
stateHandler.SimpleTx(message, "")
state := stateHandler.Get(message)
if state == nil {
t.Errorf("State from StateHandler before clearing was wrong, got: nil, want: not nil.")
Expand Down
4 changes: 2 additions & 2 deletions bot/transactionBuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,11 @@ type SimpleTx struct {
step int
}

func CreateSimpleTx(m *tb.Message) (Tx, error) {
func CreateSimpleTx(m *tb.Message, suggestedCur string) (Tx, error) {
tx := (&SimpleTx{
stepDetails: make(map[command]Input),
}).
addStep("amount", "Please enter the amount of money (e.g. '12.34' or '12.34 USD')", HandleFloat).
addStep("amount", fmt.Sprintf("Please enter the amount of money (e.g. '12.34' or '12.34 %s')", suggestedCur), HandleFloat).
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)
Expand Down
8 changes: 4 additions & 4 deletions bot/transactionBuilder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func TestHandleFloat(t *testing.T) {
}

func TestTransactionBuilding(t *testing.T) {
tx, err := bot.CreateSimpleTx(&tb.Message{Text: "/simple"})
tx, err := bot.CreateSimpleTx(&tb.Message{Text: "/simple"}, "")
if err != nil {
t.Errorf("Error creating simple tx: %s", err.Error())
}
Expand All @@ -63,7 +63,7 @@ func TestTransactionBuilding(t *testing.T) {
}

func TestTransactionBuildingCustomCurrencyInAmount(t *testing.T) {
tx, err := bot.CreateSimpleTx(&tb.Message{Text: "/simple"})
tx, err := bot.CreateSimpleTx(&tb.Message{Text: "/simple"}, "")
if err != nil {
t.Errorf("Error creating simple tx: %s", err.Error())
}
Expand All @@ -88,7 +88,7 @@ func TestTransactionBuildingCustomCurrencyInAmount(t *testing.T) {
}

func TestTransactionBuildingWithDate(t *testing.T) {
tx, err := bot.CreateSimpleTx(&tb.Message{Text: "/simple 2021-01-24"})
tx, err := bot.CreateSimpleTx(&tb.Message{Text: "/simple 2021-01-24"}, "")
if err != nil {
t.Errorf("Error creating simple tx: %s", err.Error())
}
Expand Down Expand Up @@ -118,7 +118,7 @@ func TestCountLeadingDigits(t *testing.T) {
}

func TestTaggedTransaction(t *testing.T) {
tx, _ := bot.CreateSimpleTx(&tb.Message{Text: "/simple 2021-01-24"})
tx, _ := bot.CreateSimpleTx(&tb.Message{Text: "/simple 2021-01-24"}, "")
tx.Input(&tb.Message{Text: "17.3456 USD_TEST"}) // amount
tx.Input(&tb.Message{Text: "Assets:Wallet"}) // from
tx.Input(&tb.Message{Text: "Expenses:Groceries"}) // to
Expand Down

0 comments on commit 4085bcb

Please sign in to comment.