-
Notifications
You must be signed in to change notification settings - Fork 0
/
statements.go
49 lines (38 loc) · 1.09 KB
/
statements.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
package loaders
import (
"github.com/jfk9w-go/tinkoff-api"
"github.com/jfk9w/hoarder/internal/database"
"github.com/jfk9w/hoarder/internal/jobs"
. "github.com/jfk9w/hoarder/internal/jobs/tinkoff/internal/entities"
)
type statements struct {
accountId string
batchSize int
}
func (l statements) TableName() string {
return new(Statement).TableName()
}
func (l statements) Load(ctx jobs.Context, client Client, db database.DB) (ls []Interface, errs error) {
ctx = ctx.With("account_id", l.accountId)
out, err := client.Statements(ctx, &tinkoff.StatementsIn{Account: l.accountId})
if ctx.Error(&errs, err, "failed to get data from api") {
return
}
if len(out) == 0 {
return
}
entities, err := database.ToViaJSON[[]Statement](out)
if ctx.Error(&errs, err, "entity conversion failed") {
return
}
for i := range entities {
entities[i].AccountId = l.accountId
}
if err := db.WithContext(ctx).
UpsertInBatches(entities, l.batchSize).
Error; ctx.Error(&errs, err, "failed to update entities in db") {
return
}
ctx.Info("updated entities in db", "count", len(entities))
return
}