-
Notifications
You must be signed in to change notification settings - Fork 0
/
accounts.go
134 lines (112 loc) · 3.55 KB
/
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
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
package firefly
import (
"context"
"fmt"
"github.com/AlekSi/pointer"
"github.com/pkg/errors"
"github.com/jfk9w/hoarder/internal/database"
"github.com/jfk9w/hoarder/internal/firefly"
"github.com/jfk9w/hoarder/internal/jobs"
. "github.com/jfk9w/hoarder/internal/jobs/tinkoff/internal/entities"
)
type accounts struct {
phone string
batchSize int
}
func (s accounts) TableName() string {
return "accounts"
}
func (s accounts) Sync(ctx jobs.Context, db database.DB, client firefly.Invoker) (ss []Interface, errs error) {
ctx = ctx.With("phone", s.phone)
var entities []Account
if err := db.WithContext(ctx).
Where("user_phone = ?", s.phone).
Preload("Currency").
Find(&entities).
Error; ctx.Error(&errs, err, "failed to select records") {
return
}
for _, entity := range entities {
ctx := ctx.With("id", entity.Id)
if entity.FireflyId != nil {
err := updateAccount(ctx, client, entity)
if !ctx.Error(&errs, err, "failed to update account") {
ss = append(ss, transactions{accountId: entity.Id, batchSize: s.batchSize})
}
continue
}
fireflyId, err := storeAccount(ctx, client, entity)
if ctx.Error(&errs, err, "failed to store account") {
continue
}
if err := db.WithContext(ctx).
Table(new(Account).TableName()).
Where("id = ?", entity.Id).
Update("firefly_id", fireflyId).
Error; ctx.Error(&errs, err, "failed to update firefly id in db") {
continue
}
ss = append(ss, transactions{accountId: entity.Id, batchSize: s.batchSize})
}
return
}
func updateAccount(ctx context.Context, client firefly.Invoker, account Account) error {
in := &firefly.AccountUpdate{
Name: getAccountName(account),
Active: firefly.NewOptBool(!account.Deleted),
}
out, err := client.UpdateAccount(ctx, in, firefly.UpdateAccountParams{ID: pointer.Get(account.FireflyId)})
if err != nil {
return err
}
switch out := out.(type) {
case *firefly.AccountSingle:
return nil
case exception:
return exception2error(out)
default:
return errors.Errorf("%s", out)
}
}
func storeAccount(ctx context.Context, client firefly.Invoker, account Account) (string, error) {
in := &firefly.AccountStore{
Name: getAccountName(account),
Type: firefly.ShortAccountTypePropertyAsset,
AccountNumber: firefly.NewOptNilString(account.Id),
Active: firefly.NewOptBool(!account.Deleted),
}
if currency := account.Currency; currency != nil {
if fireflyId := currency.FireflyId; fireflyId != nil {
in.CurrencyID = firefly.NewOptString(pointer.Get(currency.FireflyId))
} else {
in.CurrencyCode = firefly.NewOptString(currency.Name)
}
}
switch account.AccountType {
case "Current":
in.AccountRole = firefly.NewOptNilAccountRoleProperty(firefly.AccountRolePropertyDefaultAsset)
case "Saving":
in.AccountRole = firefly.NewOptNilAccountRoleProperty(firefly.AccountRolePropertySavingAsset)
case "Credit":
in.AccountRole = firefly.NewOptNilAccountRoleProperty(firefly.AccountRolePropertyCcAsset)
in.CreditCardType = firefly.NewOptNilCreditCardType(firefly.CreditCardTypeMonthlyFull)
if dueDate := account.DueDate; dueDate != nil {
in.MonthlyPaymentDate = firefly.NewOptNilDateTime(dueDate.Time())
}
}
out, err := client.StoreAccount(ctx, in, firefly.StoreAccountParams{})
if err != nil {
return "", err
}
switch out := out.(type) {
case *firefly.AccountSingle:
return out.Data.ID, nil
case exception:
return "", exception2error(out)
default:
return "", errors.Errorf("%s", out)
}
}
func getAccountName(account Account) string {
return fmt.Sprintf("%s (%s)", account.Name, account.Id)
}