-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtransaction.go
260 lines (214 loc) · 6.27 KB
/
transaction.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
package main
import (
"context"
"fmt"
"github.com/charmbracelet/bubbles/key"
"github.com/charmbracelet/bubbles/list"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/charmbracelet/log"
lm "github.com/icco/lunchmoney"
)
type transactionItem struct {
t *lm.Transaction
category *lm.Category
plaidAccount *lm.PlaidAccount
asset *lm.Asset
tags []*lm.Tag
}
func (t transactionItem) Title() string {
return t.t.Payee
}
func (t transactionItem) Description() string {
amount, err := t.t.ParsedAmount()
if err != nil {
return fmt.Sprintf("error parsing amount: %v", err)
}
var account string
if t.plaidAccount != nil {
account = t.plaidAccount.Name
} else if t.asset != nil {
account = t.asset.Name
}
tags := ""
for _, tag := range t.tags {
tags += tag.Name + ","
}
if tags == "" {
tags = "no tags"
}
return fmt.Sprintf("%s | %s | %s | %s | %s | %s",
t.t.Date,
t.category.Name,
amount.Display(),
account,
tags,
t.t.Status,
)
}
func (t transactionItem) FilterValue() string {
return fmt.Sprintf("%s %s %s", t.t.Payee, t.category.Name, t.t.Status)
}
type transactionListKeyMap struct {
categorizeTransaction key.Binding
filterUncleared key.Binding
}
func newTransactionListKeyMap() *transactionListKeyMap {
return &transactionListKeyMap{
categorizeTransaction: key.NewBinding(
key.WithKeys("c"),
key.WithHelp("c", "categorize transaction"),
),
filterUncleared: key.NewBinding(
key.WithKeys("u"),
key.WithHelp("u", "filter uncleared transactions"),
),
}
}
func updateTransactions(msg tea.Msg, m model) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case updateTransactionMsg:
log.Debug("updating transaction")
// create a copy of the transaction and update the status
// this keep the category, assets, plaidAccount, etc. intact
t, ok := m.transactions.SelectedItem().(transactionItem)
if !ok {
return m, nil
}
t.t = msg.t
// must set the new category on the transaction item
// in case that is what changed
// in future, we could check the fieldUpdated to see what changed
t.category = m.idToCategory[t.t.CategoryID]
setItemCmd := m.transactions.SetItem(m.transactions.Index(), t)
statusCmd := m.transactions.NewStatusMessage(
fmt.Sprintf("Updated %s for transaction: %s", msg.fieldUpdated, msg.t.Payee),
)
m.transactionsStats = newTransactionStats(m.transactions.Items())
// move the cursor down to the next item automatically
m.transactions.CursorDown()
return m, tea.Batch(setItemCmd, statusCmd)
case tea.KeyMsg:
if m.transactions.FilterState() == list.Filtering {
break
}
if key.Matches(msg, m.transactionsListKeys.filterUncleared) {
return filterUnclearedTransactions(m)
}
if key.Matches(msg, m.transactionsListKeys.categorizeTransaction) {
return categorizeTrans(&m)
}
}
var cmd tea.Cmd
m.transactions, cmd = m.transactions.Update(msg)
return m, cmd
}
func categorizeTrans(m *model) (tea.Model, tea.Cmd) {
// we know which transaction we're categorizing because we're
// updating the category for the transaction at the current index
t, ok := m.transactions.Items()[m.transactions.Index()].(transactionItem)
if !ok {
return m, nil
}
m.categoryForm = newCategorizeTransactionForm(m.categories)
m.categoryForm.SubmitCmd = func() tea.Msg {
return submitCategoryForm(*m, t)
}
m.sessionState = categorizeTransaction
return m, tea.Batch(m.categoryForm.Init(), tea.WindowSize())
}
func submitCategoryForm(m model, t transactionItem) tea.Msg {
ctx := context.Background()
categoryValue := m.categoryForm.Get("category")
cid64, isCategoryValueValid := categoryValue.(int64)
if !isCategoryValueValid {
log.Debug("invalid category value", "value", categoryValue)
return nil
}
cid := int(cid64)
log.Debug("updating transaction", "transaction", t.t.ID, "category", cid)
status := clearedStatus
resp, err := m.lmc.UpdateTransaction(ctx, t.t.ID, &lm.UpdateTransaction{CategoryID: &cid, Status: &status})
if err != nil {
log.Debug("updating transaction", "error", err)
return err
}
if !resp.Updated {
log.Debug("transaction not updated")
return nil
}
newT, err := m.lmc.GetTransaction(ctx, t.t.ID, &lm.TransactionFilters{DebitAsNegative: &m.debitsAsNegative})
if err != nil {
log.Debug("getting transaction", "error", err)
return err
}
// the transaction we get back from the API does not
// respect the debitAsNegative setting, so we will use
// the original transaction to update the category
t.t.CategoryID = newT.CategoryID
t.t.Status = newT.Status
return updateTransactionMsg{t: t.t, fieldUpdated: "category"}
}
func filterUnclearedTransactions(m model) (tea.Model, tea.Cmd) {
unclearedItems := make([]list.Item, 0)
for _, item := range m.transactions.Items() {
if t, ok := item.(transactionItem); ok && t.t.Status == "uncleared" {
unclearedItems = append(unclearedItems, item)
}
}
m.transactions.SetItems(unclearedItems)
m.transactionsStats = newTransactionStats(m.transactions.Items())
return m, nil
}
func transactionsView(m model) string {
return lipgloss.JoinVertical(lipgloss.Left,
m.transactions.View(),
m.transactionsStats.View(),
)
}
func newTransactionStats(ts []list.Item) *transactionsStats {
stats := transactionsStats{}
for _, t := range ts {
ti, ok := t.(transactionItem)
if !ok {
continue
}
if ti.t == nil {
continue
}
switch ti.t.Status {
case "pending":
stats.pending++
case "uncleared":
stats.uncleared++
case "cleared":
stats.cleared++
}
}
return &stats
}
type transactionsStats struct {
pending int
uncleared int
cleared int
}
// View renders the transactions stats in a single line.
func (t transactionsStats) View() string {
pending := lipgloss.NewStyle().
Foreground(lipgloss.Color("#7f7d78")).
MarginRight(2).
Render(fmt.Sprintf("%d pending", t.pending))
uncleared := lipgloss.NewStyle().
Foreground(lipgloss.Color("#e05951")).
MarginRight(2).
Render(fmt.Sprintf("%d uncleared", t.uncleared))
cleared := lipgloss.NewStyle().
Foreground(lipgloss.Color("#22ba46")).
MarginRight(2).
Render(fmt.Sprintf("%d cleared", t.cleared))
transactionStatus := lipgloss.JoinHorizontal(lipgloss.Left, pending, uncleared, cleared)
return lipgloss.NewStyle().
MarginTop(1).
MarginLeft(2).
Render(transactionStatus)
}