-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
job_proposal.go
177 lines (141 loc) · 4.69 KB
/
job_proposal.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package resolver
import (
"context"
"strconv"
"github.com/graph-gophers/graphql-go"
"github.com/pkg/errors"
"github.com/smartcontractkit/chainlink/v2/core/services/feeds"
"github.com/smartcontractkit/chainlink/v2/core/web/loader"
)
var notFoundErrorMessage = "spec not found"
type JobProposalStatus string
const (
PENDING JobProposalStatus = "PENDING"
APPROVED JobProposalStatus = "APPROVED"
REJECTED JobProposalStatus = "REJECTED"
CANCELLED JobProposalStatus = "CANCELLED"
DELETED JobProposalStatus = "DELETED"
REVOKED JobProposalStatus = "REVOKED"
)
func ToJobProposalStatus(s feeds.JobProposalStatus) (JobProposalStatus, error) {
switch s {
case feeds.JobProposalStatusApproved:
return APPROVED, nil
case feeds.JobProposalStatusPending:
return PENDING, nil
case feeds.JobProposalStatusRejected:
return REJECTED, nil
case feeds.JobProposalStatusCancelled:
return CANCELLED, nil
case feeds.JobProposalStatusDeleted:
return DELETED, nil
case feeds.JobProposalStatusRevoked:
return REVOKED, nil
default:
return "", errors.New("invalid job proposal status")
}
}
// JobProposalResolver resolves the Job Proposal type
type JobProposalResolver struct {
jp *feeds.JobProposal
}
// NewJobProposal creates a new JobProposalResolver
func NewJobProposal(jp *feeds.JobProposal) *JobProposalResolver {
return &JobProposalResolver{jp: jp}
}
func NewJobProposals(jps []feeds.JobProposal) []*JobProposalResolver {
var resolvers []*JobProposalResolver
for i := range jps {
resolvers = append(resolvers, NewJobProposal(&jps[i]))
}
return resolvers
}
// ID resolves to the job proposal ID
func (r *JobProposalResolver) ID() graphql.ID {
return int64GQLID(r.jp.ID)
}
// Name resolves to the job proposal name
func (r *JobProposalResolver) Name() *string {
return r.jp.Name.Ptr()
}
// Status resolves to the job proposal Status
func (r *JobProposalResolver) Status() JobProposalStatus {
if status, err := ToJobProposalStatus(r.jp.Status); err == nil {
return status
}
return ""
}
// ExternalJobID resolves to the job proposal ExternalJobID
func (r *JobProposalResolver) ExternalJobID() *string {
if r.jp.ExternalJobID.Valid {
id := r.jp.ExternalJobID.UUID.String()
return &id
}
return nil
}
// JobID resolves to the job proposal JobID if it has an ExternalJobID
func (r *JobProposalResolver) JobID(ctx context.Context) (*string, error) {
if !r.jp.ExternalJobID.Valid {
return nil, nil
}
job, err := loader.GetJobByExternalJobID(ctx, r.jp.ExternalJobID.UUID.String())
if err != nil {
return nil, err
}
id := strconv.FormatInt(int64(job.ID), 10)
return &id, err
}
// FeedsManager resolves the job proposal's feeds manager object field.
func (r *JobProposalResolver) FeedsManager(ctx context.Context) (*FeedsManagerResolver, error) {
mgr, err := loader.GetFeedsManagerByID(ctx, strconv.FormatInt(r.jp.FeedsManagerID, 10))
if err != nil {
return nil, err
}
return NewFeedsManager(*mgr), nil
}
// MultiAddrs resolves to the job proposal MultiAddrs
func (r *JobProposalResolver) MultiAddrs() []string {
return r.jp.Multiaddrs
}
// PendingUpdate resolves to whether the job proposal has a pending update.
func (r *JobProposalResolver) PendingUpdate() bool {
return r.jp.PendingUpdate
}
// Specs returns all spec proposals associated with the proposal.
func (r *JobProposalResolver) Specs(ctx context.Context) ([]*JobProposalSpecResolver, error) {
specs, err := loader.GetSpecsByJobProposalID(ctx, strconv.FormatInt(r.jp.ID, 10))
if err != nil {
return nil, err
}
return NewJobProposalSpecs(specs), nil
}
// LatestSpec returns the spec with the highest version number.
func (r *JobProposalResolver) LatestSpec(ctx context.Context) (*JobProposalSpecResolver, error) {
spec, err := loader.GetLatestSpecByJobProposalID(ctx, strconv.FormatInt(r.jp.ID, 10))
if err != nil {
return nil, err
}
return NewJobProposalSpec(spec), nil
}
// RemoteUUID returns the remote FMS UUID of the proposal.
func (r *JobProposalResolver) RemoteUUID(ctx context.Context) string {
return r.jp.RemoteUUID.String()
}
// -- GetJobProposal Query --
// JobProposalPayloadResolver resolves the job proposal payload type
type JobProposalPayloadResolver struct {
jp *feeds.JobProposal
NotFoundErrorUnionType
}
// NewJobProposalPayload creates a new job proposal payload
func NewJobProposalPayload(jp *feeds.JobProposal, err error) *JobProposalPayloadResolver {
e := NotFoundErrorUnionType{err: err, message: "job proposal not found"}
return &JobProposalPayloadResolver{jp: jp, NotFoundErrorUnionType: e}
}
// ToJobProposal resolves to the job proposal resolver
func (r *JobProposalPayloadResolver) ToJobProposal() (*JobProposalResolver, bool) {
if r.err == nil {
return NewJobProposal(r.jp), true
}
return nil, false
}