-
Notifications
You must be signed in to change notification settings - Fork 0
/
actions_reviewable_request_index.go
142 lines (120 loc) · 3.65 KB
/
actions_reviewable_request_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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package horizon
import (
"gitlab.com/tokend/go/xdr"
"gitlab.com/tokend/horizon/db2"
"gitlab.com/tokend/horizon/db2/history"
"gitlab.com/tokend/horizon/render/hal"
"gitlab.com/tokend/horizon/render/problem"
"gitlab.com/tokend/horizon/resource/reviewablerequest"
"gitlab.com/tokend/regources"
)
// ReviewableRequestIndexAction renders slice of reviewable requests
type ReviewableRequestIndexAction struct {
Action
CustomFilter func(action *ReviewableRequestIndexAction)
CustomCheckAllowed func(action *ReviewableRequestIndexAction)
q history.ReviewableRequestQI
Reviewer string
Requestor string
State *int64
UpdatedAfter *int64
HistRecords []history.ReviewableRequest
Records []regources.ReviewableRequest
RequestTypes []xdr.ReviewableRequestType
PagingParams db2.PageQuery
Page hal.Page
DisablePaging bool
}
// JSON is a method for actions.JSON
func (action *ReviewableRequestIndexAction) JSON() {
action.Do(
action.EnsureHistoryFreshness,
action.loadParams,
action.checkAllowed,
action.loadRecord,
action.loadPage,
func() {
hal.Render(action.W, action.Page)
},
)
}
func (action *ReviewableRequestIndexAction) loadParams() {
action.DisablePaging = false
action.PagingParams = action.GetPageQuery()
if action.PagingParams.Cursor == "" {
action.DisablePaging = true
}
action.Reviewer = action.GetString("reviewer")
action.Requestor = action.GetString("requestor")
action.State = action.GetOptionalInt64("state")
action.UpdatedAfter = action.GetOptionalInt64("updated_after")
action.Page.Filters = map[string]string{
"reviewer": action.Reviewer,
"requestor": action.Requestor,
"state": action.GetString("state"),
"updated_after": action.GetString("updated_after"),
}
}
func (action *ReviewableRequestIndexAction) checkAllowed() {
if action.CustomCheckAllowed != nil {
action.CustomCheckAllowed(action)
return
}
ownersOfData := []string{action.App.CoreInfo.AdminAccountID}
if action.Requestor != "" {
ownersOfData = append(ownersOfData, action.Requestor)
}
if action.Reviewer != "" {
ownersOfData = append(ownersOfData, action.Reviewer)
}
action.IsAllowed(ownersOfData...)
}
func (action *ReviewableRequestIndexAction) loadRecord() {
if !action.DisablePaging {
return
}
action.q = action.HistoryQ().ReviewableRequests()
if action.CustomFilter != nil {
action.CustomFilter(action)
}
if action.Err != nil {
return
}
if action.Reviewer != "" {
action.q = action.q.ForReviewer(action.Reviewer)
}
if action.Requestor != "" {
action.q = action.q.ForRequestor(action.Requestor)
}
if action.State != nil {
action.q = action.q.ForState(*action.State)
}
if action.UpdatedAfter != nil {
action.q = action.q.UpdatedAfter(*action.UpdatedAfter)
}
action.q = action.q.ForTypes(action.RequestTypes)
var err error
action.HistRecords, err = action.q.Page(action.PagingParams).Select()
if err != nil {
action.Log.WithError(err).Error("failed to load reviewable requests")
action.Err = &problem.ServerError
return
}
}
func (action *ReviewableRequestIndexAction) loadPage() {
for i := range action.HistRecords {
res, err := reviewablerequest.PopulateReviewableRequest(&action.HistRecords[i])
if err != nil {
action.Log.WithError(err).Error("Failed to populate reviewable request")
action.Err = &problem.ServerError
return
}
action.Page.Add(res)
}
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()
}