This repository has been archived by the owner on Feb 19, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sync_trxs.go
82 lines (66 loc) · 2.22 KB
/
sync_trxs.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
package main
import (
"context"
"github.com/Seklfreak/nordigen-lunchmoney-sync/lunchmoney"
"github.com/Seklfreak/nordigen-lunchmoney-sync/nordigen"
"github.com/pkg/errors"
"go.uber.org/zap"
)
func syncAccount(
ctx context.Context,
nordigenAccountID string,
lunchmoneyAssetID int,
nordigenClient *nordigen.Client,
lunchmoneyClient *lunchmoney.Client,
log *zap.Logger,
) error {
// fetch account details from Nordigen
account, err := nordigenClient.GetAccountDetails(ctx, nordigenAccountID)
if err != nil {
return errors.Wrap(err, "failed to fetch account details from Nordigen")
}
// fetch transactions from Nordigen
transactions, err := nordigenClient.Transactions(ctx, nordigenAccountID)
if err != nil {
return errors.Wrap(err, "failed to fetch transactions from Nordigen")
}
log.Info("fetched transactions from Nordigen", zap.Int("total", len(transactions.Booked)))
// prepare transactions to insert
lunchmoneyTransactions := make([]*lunchmoney.Transaction, 0, len(transactions.Booked))
for _, trx := range transactions.Booked {
lmTrx, err := createLunchmoneyTrx(trx, account, lunchmoneyAssetID)
if err != nil {
return errors.Wrapf(err, "failed to create Lunchmoney transaction for Nordigen transaction %s", trx.TransactionID)
}
if lmTrx != nil {
lunchmoneyTransactions = append(lunchmoneyTransactions, lmTrx)
}
}
for _, trx := range lunchmoneyTransactions {
log.Debug("prepared transaction", zap.Any("transaction", trx))
}
// split all into chunks
chunkSize := 50
chunks := make([][]*lunchmoney.Transaction, 0, chunkSize)
for i := 0; i < len(lunchmoneyTransactions); i += chunkSize {
end := i + chunkSize
if end > len(lunchmoneyTransactions) {
end = len(lunchmoneyTransactions)
}
chunks = append(chunks, lunchmoneyTransactions[i:end])
}
// insert transactions
for _, chunk := range chunks {
inserted, err := lunchmoneyClient.InsertTransactions(ctx, chunk)
if err != nil {
return errors.Wrapf(err, "failed to insert transactions")
}
log.Info("inserted transactions",
zap.Int("inserted_count", inserted),
zap.Int("chunk_size", len(chunk)),
zap.String("nordigen_account_id", nordigenAccountID),
zap.Int("lunchmoney_asset_id", lunchmoneyAssetID),
)
}
return nil
}