-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathactions_account.go
111 lines (94 loc) · 2.84 KB
/
actions_account.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
package horizon
import (
"time"
"gitlab.com/tokend/horizon/db2/core"
"gitlab.com/tokend/horizon/render/hal"
"gitlab.com/tokend/horizon/render/problem"
"gitlab.com/tokend/horizon/resource"
"gitlab.com/tokend/regources"
)
// This file contains the actions:
//
// AccountShowAction: details for single account (including stellar-core state)
// AccountShowAction renders a account summary found by its address.
type AccountShowAction struct {
Action
Address string
Resource resource.Account
}
// JSON is a method for actions.JSON
func (action *AccountShowAction) JSON() {
action.Do(
action.loadParams,
action.checkAllowed,
action.loadRecord,
action.loadBalances,
action.loadExternalSystemAccountIDs,
action.loadReferrals,
func() {
hal.Render(action.W, action.Resource)
},
)
}
func (action *AccountShowAction) loadParams() {
action.Address = action.GetString("id")
}
func (action *AccountShowAction) checkAllowed() {
action.IsAllowed(action.Address)
}
func (action *AccountShowAction) loadRecord() {
coreRecord, err := action.CoreQ().
Accounts().
ForAddresses(action.Address).
WithAccountKYC().
First()
if err != nil {
action.Log.WithError(err).Error("Failed to get account from core DB")
action.Err = &problem.ServerError
return
}
if coreRecord == nil {
action.Err = &problem.NotFound
return
}
action.Resource.Populate(action.Ctx, *coreRecord)
}
func (action *AccountShowAction) loadBalances() {
var balances []core.Balance
err := action.CoreQ().
BalancesByAddress(&balances, action.Address)
if err != nil {
action.Log.WithError(err).Error("Failed to get balances for account")
action.Err = &problem.ServerError
return
}
action.Resource.SetBalances(balances)
}
func (action *AccountShowAction) loadExternalSystemAccountIDs() {
exSysIDs, err := action.CoreQ().ExternalSystemAccountID().ForAccount(action.Address).Select()
if err != nil {
action.Log.WithError(err).Error("Failed to load external system account ids")
action.Err = &problem.ServerError
return
}
action.Resource.ExternalSystemAccounts = make([]regources.ExternalSystemAccountID, 0, len(exSysIDs))
for i := range exSysIDs {
if exSysIDs[i].ExpiresAt == nil || *exSysIDs[i].ExpiresAt >= time.Now().Unix() {
result := resource.PopulateExternalSystemAccountID(exSysIDs[i])
action.Resource.ExternalSystemAccounts = append(action.Resource.ExternalSystemAccounts, result)
}
}
}
func (action *AccountShowAction) loadReferrals() {
var coreReferrals []core.Account
err := action.CoreQ().Accounts().ForReferrer(action.Address).Select(&coreReferrals)
if err != nil {
action.Log.WithError(err).Error("Failed to load referrals")
action.Err = &problem.ServerError
return
}
action.Resource.Referrals = make([]resource.Referral, len(coreReferrals))
for i := range coreReferrals {
action.Resource.Referrals[i].Populate(coreReferrals[i])
}
}