forked from landakram/plaid-cli
-
Notifications
You must be signed in to change notification settings - Fork 2
/
airtable_accounts.go
66 lines (55 loc) · 1.27 KB
/
airtable_accounts.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
package main
import (
"fmt"
"github.com/brianloveswords/airtable"
"github.com/plaid/plaid-go/plaid"
"os"
)
type AccountFields struct {
AccountID string
Name string
Mask string
}
type AccountRecord struct {
airtable.Record
Fields AccountFields
}
func SyncAccounts(accounts []plaid.Account) error {
client := airtable.Client{
APIKey: os.Getenv("AIRTABLE_KEY"),
BaseID: "appxCfKnRz94NZadj",
}
accountsTable := client.Table("Accounts")
plaidAccounts := make([]AccountRecord, len(accounts))
for i, a := range accounts {
name := a.OfficialName
if name == "" {
name = a.Name
}
plaidAccounts[i] = AccountRecord{Fields: AccountFields{
AccountID: a.AccountID,
Name: name,
Mask: a.Mask,
}}
}
var airtableAccounts []AccountRecord
err := accountsTable.List(&airtableAccounts, &airtable.Options{})
if err != nil {
return err
}
existingIDs := map[string]struct{}{}
for _, account := range airtableAccounts {
existingIDs[account.Fields.AccountID] = struct{}{}
}
for i, account := range plaidAccounts {
if _, ok := existingIDs[account.Fields.AccountID]; ok {
continue
}
err := accountsTable.Create(&account)
if err != nil {
return err
}
fmt.Printf("Created %d/%d account\n", i, len(plaidAccounts))
}
return nil
}