Skip to content

Commit

Permalink
extract accounts names
Browse files Browse the repository at this point in the history
  • Loading branch information
mput committed Jun 23, 2024
1 parent 4a647e0 commit 60ab1c8
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 40 deletions.
43 changes: 36 additions & 7 deletions app/ledger/ledger.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,7 @@ type Transaction struct {
Date time.Time `json:"date"` // The date of the transaction
Description string `json:"description"` // A description of the transaction
Postings []Posting `json:"postings"` // A slice of postings that belong to this transaction
Comment string
}

func (t Transaction) toString() string {
Expand Down Expand Up @@ -267,18 +268,25 @@ func (b OpenAITransactionGenerator) GenerateTransaction(promptCtx PromptCtx) (Tr
return Transaction{}, nil
}

func (l *Ledger) extractAccounts() ([]string, error) {
accounts, err := l.execute("accounts")
if err != nil {
return nil, fmt.Errorf("unable to extract accounts: %v", err)
}
return strings.Split(strings.TrimSpace(accounts), "\n"), nil
}


// Receive a short free-text description of a transaction
// and returns a formatted transaction validated with the
// ledger file.
func (l *Ledger) ProposeTransaction(userInput string) (string, error) {
err := l.repo.Init()
defer l.repo.Free()
func (l *Ledger) proposeTransaction(userInput string) (Transaction, error) {
accounts, err := l.extractAccounts()

if err != nil {
return "", err
return Transaction{}, err
}

accounts := []string{"Assets:Cards:Wise-EUR", "Assets:Cards:Wise-USD", "Assets:Cards:Wise-RUB"}

promptCtx := PromptCtx{
Accounts: accounts,
Expand All @@ -287,11 +295,32 @@ func (l *Ledger) ProposeTransaction(userInput string) (string, error) {

trx, err := l.generator.GenerateTransaction(promptCtx)
if err != nil {
return "", fmt.Errorf("unable to generate transaction: %v", err)
return Transaction{}, fmt.Errorf("unable to generate transaction: %v", err)
}

return trx, nil

}

func (l *Ledger) ProposeTransaction(userInput string, times int) (tr Transaction,err error) {
if times <= 0 {
panic("times should be greater than 0")
}
err = l.repo.Init()
defer l.repo.Free()

if err != nil {
return tr, err
}

return trx.toString(), nil
for ; times > 0 ; times-- {
tr, err = l.proposeTransaction(userInput)
if err == nil {
return tr, nil
}
}

return tr, err
}


Expand Down
38 changes: 5 additions & 33 deletions app/ledger/ledger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package ledger
import (
"strings"
"testing"
"time"

"github.com/mput/teledger/app/repo"
"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -169,25 +168,8 @@ func TestLedger_ProposeTransaction(t *testing.T) {
t.Run("happy path", func(t *testing.T) {

mockedTransactionGenerator := &TransactionGeneratorMock{
GenerateTransactionFunc: func(promptCtx PromptCtx) (Transaction, error) {
dt, _ := time.Parse(time.RFC3339, "2014-11-12T11:45:26.371Z")
tr := Transaction{
Date: dt,
Description: "My tr",
Postings: []Posting{
Posting{
Account: "cash",
Amount: -30.43,
Currency: "EUR",
},
Posting{
Account: "taxi",
Amount: 30.43,
Currency: "EUR",
},
},
}
return tr, nil
GenerateTransactionFunc: func(_ PromptCtx) (Transaction, error) {
return Transaction{}, nil
},
}

Expand All @@ -204,28 +186,18 @@ func TestLedger_ProposeTransaction(t *testing.T) {
false,
)

tr, err := ledger.ProposeTransaction("20 Taco Bell")

_, err := ledger.ProposeTransaction("20 Taco Bell", 1)

assert.NoError(t, err)

assert.Equal(t,
`2014-11-12 My tr
cash -30.43 EUR
taxi 30.43 EUR
`,
tr,
)

assert.Equal(t, len(mockedTransactionGenerator.calls.GenerateTransaction), 1)


assert.Equal(t,
mockedTransactionGenerator.calls.GenerateTransaction[0].PromptCtx,
PromptCtx{
Accounts: []string{"Assets:Cards:Wise-EUR", "Assets:Cards:Wise-USD", "Assets:Cards:Wise-RUB"},
Accounts: []string{"Assets:Cash", "Equity"},
UserInput: "20 Taco Bell",
},
mockedTransactionGenerator.calls.GenerateTransaction[0].PromptCtx,
)


Expand Down

0 comments on commit 60ab1c8

Please sign in to comment.