-
Notifications
You must be signed in to change notification settings - Fork 0
/
actions_contract_show.go
99 lines (82 loc) · 2.69 KB
/
actions_contract_show.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
package horizon
import (
"gitlab.com/tokend/go/xdr"
"gitlab.com/tokend/horizon/render/hal"
"gitlab.com/tokend/horizon/render/problem"
"gitlab.com/tokend/horizon/resource"
"gitlab.com/tokend/horizon/resource/reviewablerequest"
"gitlab.com/tokend/regources"
)
// ContractShowAction represents singe contract by id
// with invoices requests
type ContractShowAction struct {
Action
ContractID int64
ContractRecord regources.Contract
}
// JSON is a method for actions.JSON
func (action *ContractShowAction) JSON() {
action.Do(
action.EnsureHistoryFreshness,
action.loadParams,
action.loadRecords,
action.checkAllowed,
func() {
hal.Render(action.W, action.ContractRecord)
},
)
}
func (action *ContractShowAction) checkAllowed() {
action.IsAllowed(action.ContractRecord.Contractor, action.ContractRecord.Customer, action.ContractRecord.Escrow)
}
func (action *ContractShowAction) loadParams() {
action.ContractID = action.GetInt64("id")
}
func (action *ContractShowAction) loadRecords() {
contract, err := action.HistoryQ().Contracts().ByID(action.ContractID)
if err != nil {
action.Log.WithError(err).Error("Failed to get contract record")
action.Err = &problem.ServerError
return
}
if contract == nil {
action.Err = &problem.NotFound
return
}
action.ContractRecord = resource.PopulateContract(*contract)
invoices, err := action.HistoryQ().ReviewableRequests().ByIDs(contract.Invoices).Select()
if err != nil {
action.Log.WithError(err).Error("Failed to get invoices records")
action.Err = &problem.ServerError
return
}
for _, invoice := range invoices {
res, err := reviewablerequest.PopulateReviewableRequest(&invoice)
if err != nil {
action.Log.WithError(err).Error("Failed to populate invoice request")
action.Err = &problem.ServerError
return
}
action.ContractRecord.Invoices = append(action.ContractRecord.Invoices, *res)
}
additionDetails, err := action.HistoryQ().ContractsDetails().ByContractID(contract.ID)
if err != nil {
action.Log.WithError(err).Error("Failed to get additional contract details records")
action.Err = &problem.ServerError
return
}
for _, detail := range additionDetails {
res := resource.PopulateContractDetails(detail)
action.ContractRecord.AdditionalDetails = append(action.ContractRecord.AdditionalDetails, res)
}
if (contract.State & int32(xdr.ContractStateDisputing)) == 0 {
return
}
dispute, err := action.HistoryQ().ContractDispute().ByContractID(contract.ID)
if (err != nil) || (dispute == nil) {
action.Log.WithError(err).Error("Failed to get contract dispute records")
action.Err = &problem.ServerError
return
}
action.ContractRecord.DisputeReason = resource.PopulateContractDispute(*dispute)
}