forked from landakram/plaid-cli
-
Notifications
You must be signed in to change notification settings - Fork 2
/
airtable_transactions.go
166 lines (146 loc) · 4.12 KB
/
airtable_transactions.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
package main
import (
"fmt"
"log"
"os"
"time"
"github.com/brianloveswords/airtable"
"github.com/plaid/plaid-go/plaid"
)
type TransactionFields struct {
PlaidID string
// Used to dedupe, not for human consumption
AccountID string `json:"AccountIDDedupe"`
AccountIDLink airtable.RecordLink `json:"AccountID"`
Amount float64
Name string
MerchantName string
Pending bool
DateTime string
PlaidCategory1 string
PlaidCategory2 string
PlaidCategory3 string
Address string
}
type TransactionRecord struct {
airtable.Record
Fields TransactionFields
Typecast bool
}
func FetchAirtableTransactions() ([]TransactionRecord, error) {
log.Println("Fetching airtable transactions...")
client := airtable.Client{
APIKey: os.Getenv("AIRTABLE_KEY"),
BaseID: "appxCfKnRz94NZadj",
}
transactionsTable := client.Table("Transactions")
var airtableTransactions []TransactionRecord
err := transactionsTable.List(&airtableTransactions, &airtable.Options{})
log.Println("Fetched airtable transactions")
return airtableTransactions, err
}
func Sync(transactions []plaid.Transaction, airtableTransactions []TransactionRecord) error {
client := airtable.Client{
APIKey: os.Getenv("AIRTABLE_KEY"),
BaseID: "appxCfKnRz94NZadj",
}
transactionsTable := client.Table("Transactions")
plaidTransactions := make([]TransactionRecord, len(transactions))
for i, t := range transactions {
s := func(tags []string, n int) string {
if n >= len(tags) {
return ""
}
return tags[n]
}
address := t.Location.Address + " " + t.Location.City
plaidTransactions[i] = TransactionRecord{Fields: TransactionFields{
PlaidID: t.ID,
AccountID: t.AccountID,
AccountIDLink: airtable.RecordLink{t.AccountID},
Amount: t.Amount,
Name: t.Name,
MerchantName: t.MerchantName,
Pending: t.Pending,
DateTime: t.Date,
PlaidCategory1: s(t.Category, 0),
PlaidCategory2: s(t.Category, 1),
PlaidCategory3: s(t.Category, 2),
Address: address,
}, Typecast: true}
plaidTransactions[i].ID = t.ID
}
plaidArranged := byAccountIDbyTransactionID(plaidTransactions)
airtableArranged := byAccountIDbyTransactionID(airtableTransactions)
for accountID, transactions := range plaidArranged {
updates := updateAccount(transactions, airtableArranged[accountID])
// Update is delete + create
for _, t := range updates.ToDelete {
err := transactionsTable.Delete(&t)
if err != nil {
return err
}
}
for i, t := range updates.ToCreate {
err := transactionsTable.Create(&t)
if err != nil {
return err
}
fmt.Printf("Created %d/%d transactions\n", i + 1, len(updates.ToCreate))
}
for i, t := range updates.ToUpdate {
err := transactionsTable.Update(&t)
if err != nil {
return err
}
fmt.Printf("Updated %d/%d transactions\n", i + 1, len(updates.ToUpdate))
}
}
return nil
}
func byAccountIDbyTransactionID(ts []TransactionRecord) map[string]map[string]TransactionRecord {
ret := make(map[string]map[string]TransactionRecord)
for _, t := range ts {
byID, ok := ret[t.Fields.AccountID]
if !ok {
byID = make(map[string]TransactionRecord)
ret[t.Fields.AccountID] = byID
}
byID[t.Fields.PlaidID] = t
}
return ret
}
type AccountUpdate struct {
ToCreate []TransactionRecord
ToDelete []TransactionRecord
ToUpdate []TransactionRecord
}
func updateAccount(plaidTs, airtableTs map[string]TransactionRecord) AccountUpdate {
var u AccountUpdate
ids := make(map[string]struct{})
for id, t := range plaidTs {
ids[id] = struct{}{}
existing, ok := airtableTs[id]
if !ok {
u.ToCreate = append(u.ToCreate, t)
} else if existing.Fields.Pending != t.Fields.Pending ||
existing.Fields.Address != t.Fields.Address {
t.ID = existing.ID
u.ToUpdate = append(u.ToUpdate, t)
}
}
cutoff := time.Now().AddDate(0, -1, 0)
for id, t := range airtableTs {
if _, ok := ids[id]; !ok {
transactionTime, err := time.Parse("2006-01-02", t.Fields.DateTime)
if err != nil {
panic(err)
}
if transactionTime.After(cutoff) {
fmt.Println("Deleting", t)
u.ToDelete = append(u.ToDelete, t)
}
}
}
return u
}