This repository has been archived by the owner on Oct 31, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
pull_account_balances.go
176 lines (146 loc) · 5.54 KB
/
pull_account_balances.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
package jobs
import (
"github.com/sirupsen/logrus"
"github.com/gocraft/work"
"github.com/harderthanitneedstobe/rest-api/v0/pkg/models"
"github.com/harderthanitneedstobe/rest-api/v0/pkg/repository"
"github.com/pkg/errors"
"github.com/plaid/plaid-go/plaid"
)
const (
EnqueuePullAccountBalances = "EnqueuePullAccountBalances"
PullAccountBalances = "PullAccountBalances"
)
type PullAccountBalanceWorkItem struct {
AccountID uint64 `pg:"account_id"`
LinkIDs []uint64 `pg:"link_ids,type:bigint[]"`
}
func (j *jobManagerBase) getPlaidLinksByAccount() ([]PullAccountBalanceWorkItem, error) {
// We need an accountId, and all of the bank accounts for that account that can be updated.
var accounts []PullAccountBalanceWorkItem
// Query the database for all accounts with bank accounts that have a link type of plaid.
_, err := j.db.Query(&accounts, `
SELECT
"links"."account_id"
array_agg("links"."link_id") "link_ids"
FROM "links"
WHERE "links"."link_type" = ? AND "links"."plaid_link_id" IS NOT NULL
GROUP BY "links"."account_id"
`, models.PlaidLinkType)
if err != nil {
return nil, errors.Wrap(err, "failed to retrieve accounts to update balances")
}
return accounts, nil
}
func (j *jobManagerBase) enqueuePullAccountBalances(job *work.Job) error {
log := j.getLogForJob(job)
accounts, err := j.getPlaidLinksByAccount()
if err != nil {
log.WithError(err).Errorf("failed to retrieve bank accounts that need to by synced")
return err
}
log.Infof("enqueueing %d account(s) for sync", len(accounts))
for _, account := range accounts {
for _, linkId := range account.LinkIDs {
accountLog := log.WithFields(logrus.Fields{
"accountId": account.AccountID,
"linkId": linkId,
})
accountLog.Trace("enqueueing for account balance update")
_, err = j.enqueueUniqueJob(PullAccountBalances, map[string]interface{}{
"accountId": account.AccountID,
"linkId": linkId,
})
if err != nil {
accountLog.WithError(err).Error("could not enqueue account, data will not be synced")
continue
}
accountLog.Trace("successfully enqueued account for account balance update")
}
}
return nil
}
func (j *jobManagerBase) pullAccountBalances(job *work.Job) error {
log := j.getLogForJob(job)
log.Infof("pulling account balances")
accountId, err := j.getAccountId(job)
if err != nil {
log.WithError(err).Error("could not run job, no account Id")
return err
}
linkId := uint64(job.ArgInt64("linkId"))
return j.getRepositoryForJob(job, func(repo repository.Repository) error {
link, err := repo.GetLink(linkId)
if err != nil {
log.WithError(err).Error("failed to retrieve link details to pull balances")
return err
}
if link.PlaidLink == nil {
err = errors.Errorf("cannot pull account balanaces for link without plaid info")
log.WithError(err).Errorf("failed to pull balances")
return err
}
bankAccounts, err := repo.GetBankAccountsByLinkId(linkId)
if err != nil {
log.WithError(err).Error("failed to retrieve bank account details to pull balances")
return err
}
groupedByPlaidAccessToken := map[string][]models.BankAccount{}
for _, bankAccount := range bankAccounts {
if bankAccount.Link == nil || bankAccount.Link.PlaidLink == nil {
// TODO (elliotcourant) Log something here maybe? This shouldn't happen so we might want to try to keep track
// of it if it does?
continue
}
accounts, ok := groupedByPlaidAccessToken[bankAccount.Link.PlaidLink.AccessToken]
if !ok {
// If the access token is not present, store it and create a new array.
groupedByPlaidAccessToken[bankAccount.Link.PlaidLink.AccessToken] = []models.BankAccount{
bankAccount,
}
// Keep moving along.
continue
}
// If the access token is already present, simply append this account.
accounts = append(accounts, bankAccount)
}
for accessToken, banks := range groupedByPlaidAccessToken {
// Gather the plaid account Ids so we can precisely query plaid.
plaidIdsToBankIds := map[string]uint64{}
itemBankAccountIds := make([]string, len(banks))
for i, bankAccount := range banks {
itemBankAccountIds[i] = bankAccount.PlaidAccountId
plaidIdsToBankIds[bankAccount.PlaidAccountId] = bankAccount.BankAccountId
}
log.Tracef("requesting information for %d bank account(s)", len(itemBankAccountIds))
result, err := j.plaidClient.GetAccountsWithOptions(
accessToken,
plaid.GetAccountsOptions{
AccountIDs: itemBankAccountIds,
},
)
if err != nil {
log.WithError(err).Error("failed to retrieve bank accounts from plaid")
continue // We don't want it to prevent others from being processed.
}
// TODO (elliotcourant) If we lift this array out of this loop then we could update all the bank accounts for all
// of an account's links in a single query which would be more efficient.
updatedBankAccounts := make([]models.BankAccount, len(result.Accounts))
for i, item := range result.Accounts {
// TODO (elliotcourant) Maybe add something here to compare balances to the existing account record? If there
// are no changes there is no need to update the account at all.
updatedBankAccounts[i] = models.BankAccount{
BankAccountId: plaidIdsToBankIds[item.AccountID],
AccountId: accountId,
AvailableBalance: int64(item.Balances.Available * 100),
CurrentBalance: int64(item.Balances.Current * 100),
}
}
if err := repo.UpdateBankAccounts(updatedBankAccounts); err != nil {
log.WithError(err).Error("failed to update bank account balances")
return err
}
}
return nil
})
}