-
Notifications
You must be signed in to change notification settings - Fork 0
/
actions_contract_index.go
115 lines (106 loc) · 3.21 KB
/
actions_contract_index.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
package horizon
import (
"gitlab.com/tokend/horizon/db2"
"gitlab.com/tokend/horizon/render/hal"
"gitlab.com/tokend/horizon/render/problem"
"gitlab.com/tokend/horizon/resource"
"gitlab.com/tokend/regources"
)
// ContractIndexAction renders a page of contracts
// filters by startTime, endTime, disputing state,
// contractorID, customerID
type ContractIndexAction struct {
Action
PagingParams db2.PageQuery
StartTime *int64
EndTime *int64
Disputing *bool
Completed *bool
Source string
Counterparty string
EscrowID string
ContractNumber string
ContractsRecords []regources.Contract
Page hal.Page
}
// JSON is a method for actions.JSON
func (action *ContractIndexAction) JSON() {
action.Do(
action.loadParams,
action.checkAllowed,
action.loadRecords,
action.loadPage,
func() {
hal.Render(action.W, action.Page)
},
)
}
func (action *ContractIndexAction) checkAllowed() {
action.IsAllowed(action.Source, action.EscrowID)
}
func (action *ContractIndexAction) loadParams() {
action.ValidateCursorAsDefault()
action.StartTime = action.GetOptionalInt64("start_time")
action.EndTime = action.GetOptionalInt64("end_time")
action.Disputing = action.GetOptionalBool("disputing")
action.Completed = action.GetOptionalBool("completed")
action.Counterparty = action.GetString("counterparty")
action.Source = action.GetString("source")
action.ContractNumber = action.GetString("contract_number")
action.EscrowID = action.GetString("escrow")
action.PagingParams = action.GetPageQuery()
action.Page.Filters = map[string]string{
"disputing": action.GetString("disputing"),
"completed": action.GetString("completed"),
"start_time": action.GetString("start_time"),
"end_time": action.GetString("end_time"),
"counterparty": action.Counterparty,
"source": action.Source,
"contract_number": action.ContractNumber,
"escrow": action.EscrowID,
}
}
func (action *ContractIndexAction) loadRecords() {
q := action.HistoryQ().Contracts()
if action.StartTime != nil {
q = q.ByStartTime(*action.StartTime)
}
if action.EndTime != nil {
q = q.ByEndTime(*action.EndTime)
}
if action.Disputing != nil {
q = q.ByDisputeState(*action.Disputing)
}
if action.Completed != nil {
q = q.ByCompletedState(*action.Completed)
}
if action.Counterparty != "" {
q = q.ByCounterpartyID(action.Counterparty)
}
if action.Source != "" {
q = q.ByCounterpartyID(action.Source)
}
if action.EscrowID != "" {
q = q.ByEscrowID(action.EscrowID)
}
if action.ContractNumber != "" {
q = q.ByContractNumber(action.ContractNumber)
}
historyContracts, err := q.Page(action.PagingParams).Select()
if err != nil {
action.Log.WithError(err).Error("Failed to get contracts records")
action.Err = &problem.ServerError
return
}
for _, contract := range historyContracts {
action.Page.Add(resource.PopulateContract(contract))
}
}
func (action *ContractIndexAction) loadPage() {
action.Page.BaseURL = action.BaseURL()
action.Page.BasePath = action.Path()
action.Page.Limit = action.PagingParams.Limit
action.Page.Cursor = action.PagingParams.Cursor
action.Page.Order = action.PagingParams.Order
action.Page.PopulateLinks()
}