-
Notifications
You must be signed in to change notification settings - Fork 12
/
master_plan.resolvers.go
326 lines (289 loc) · 15.1 KB
/
master_plan.resolvers.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
package resolver
// This file will be automatically regenerated based on the schema, any resolver implementations
// will be copied through when generating and any unknown code will be moved to the end.
// Code generated by github.com/99designs/gqlgen version v0.17.46
import (
"context"
"errors"
"github.com/99designs/gqlgen/graphql"
"github.com/openline-ai/openline-customer-os/packages/server/customer-os-api/dataloader"
"github.com/openline-ai/openline-customer-os/packages/server/customer-os-api/graph/generated"
"github.com/openline-ai/openline-customer-os/packages/server/customer-os-api/graph/model"
"github.com/openline-ai/openline-customer-os/packages/server/customer-os-api/mapper"
"github.com/openline-ai/openline-customer-os/packages/server/customer-os-api/tracing"
"github.com/openline-ai/openline-customer-os/packages/server/customer-os-common-module/utils"
opentracing "github.com/opentracing/opentracing-go"
"github.com/opentracing/opentracing-go/log"
)
// Milestones is the resolver for the milestones field.
func (r *masterPlanResolver) Milestones(ctx context.Context, obj *model.MasterPlan) ([]*model.MasterPlanMilestone, error) {
ctx = tracing.EnrichCtxWithSpanCtxForGraphQL(ctx, graphql.GetOperationContext(ctx))
masterPlanMilestonesEntities, err := dataloader.For(ctx).GetMasterPlanMilestonesForMasterPlan(ctx, obj.ID)
if err != nil {
tracing.TraceErr(opentracing.SpanFromContext(ctx), err)
r.log.Errorf("Failed to get master plan milestones for master plan %s: %s", obj.ID, err.Error())
graphql.AddErrorf(ctx, "Failed to get master plan milestones for master plan %s", obj.ID)
return nil, nil
}
allMasterPlanMilestones := mapper.MapEntitiesToMasterPlanMilestones(masterPlanMilestonesEntities)
// filter out retired milestones
var milestones []*model.MasterPlanMilestone
for _, masterPlanMilestone := range allMasterPlanMilestones {
if !masterPlanMilestone.Retired {
milestones = append(milestones, masterPlanMilestone)
}
}
span := opentracing.SpanFromContext(ctx)
if span != nil {
span.LogFields(log.Int("response.length", len(milestones)))
}
return milestones, nil
}
// RetiredMilestones is the resolver for the retiredMilestones field.
func (r *masterPlanResolver) RetiredMilestones(ctx context.Context, obj *model.MasterPlan) ([]*model.MasterPlanMilestone, error) {
ctx = tracing.EnrichCtxWithSpanCtxForGraphQL(ctx, graphql.GetOperationContext(ctx))
masterPlanMilestonesEntities, err := dataloader.For(ctx).GetMasterPlanMilestonesForMasterPlan(ctx, obj.ID)
if err != nil {
tracing.TraceErr(opentracing.SpanFromContext(ctx), err)
r.log.Errorf("Failed to get retired master plan milestones for master plan %s: %s", obj.ID, err.Error())
graphql.AddErrorf(ctx, "Failed to get retired master plan milestones for master plan %s", obj.ID)
return nil, nil
}
allMasterPlanMilestones := mapper.MapEntitiesToMasterPlanMilestones(masterPlanMilestonesEntities)
// filter out non-retired milestones
var milestones []*model.MasterPlanMilestone
for _, masterPlanMilestone := range allMasterPlanMilestones {
if masterPlanMilestone.Retired {
milestones = append(milestones, masterPlanMilestone)
}
}
span := opentracing.SpanFromContext(ctx)
if span != nil {
span.LogFields(log.Int("response.length", len(milestones)))
}
return milestones, nil
}
// MasterPlanCreate is the resolver for the masterPlan_Create field.
func (r *mutationResolver) MasterPlanCreate(ctx context.Context, input model.MasterPlanInput) (*model.MasterPlan, error) {
ctx, span := tracing.StartGraphQLTracerSpan(ctx, "MutationResolver.MasterPlanCreate", graphql.GetOperationContext(ctx))
defer span.Finish()
tracing.SetDefaultResolverSpanTags(ctx, span)
tracing.LogObjectAsJson(span, "input", input)
masterPlanId, err := r.Services.MasterPlanService.CreateMasterPlan(ctx, utils.IfNotNilString(input.Name))
if err != nil {
tracing.TraceErr(span, err)
graphql.AddErrorf(ctx, "Failed to create master plan")
return &model.MasterPlan{ID: masterPlanId}, err
}
createdMasterPlanEntity, err := r.Services.MasterPlanService.GetMasterPlanById(ctx, masterPlanId)
if err != nil {
tracing.TraceErr(span, err)
graphql.AddErrorf(ctx, "Master plan details not yet available. Master plan id: %s", masterPlanId)
return &model.MasterPlan{ID: masterPlanId}, nil
}
span.LogFields(log.String("response.masterPlanId", masterPlanId))
return mapper.MapEntityToMasterPlan(createdMasterPlanEntity), nil
}
// MasterPlanCreateDefault is the resolver for the masterPlan_CreateDefault field.
func (r *mutationResolver) MasterPlanCreateDefault(ctx context.Context, input model.MasterPlanInput) (*model.MasterPlan, error) {
ctx, span := tracing.StartGraphQLTracerSpan(ctx, "MutationResolver.MasterPlanCreateDefault", graphql.GetOperationContext(ctx))
defer span.Finish()
tracing.SetDefaultResolverSpanTags(ctx, span)
tracing.LogObjectAsJson(span, "input", input)
masterPlanId, err := r.Services.MasterPlanService.CreateDefaultMasterPlan(ctx)
if err != nil {
tracing.TraceErr(span, err)
graphql.AddErrorf(ctx, "Failed to create default master plan")
return &model.MasterPlan{ID: masterPlanId}, err
}
createdMasterPlanEntity, err := r.Services.MasterPlanService.GetMasterPlanById(ctx, masterPlanId)
if err != nil {
tracing.TraceErr(span, err)
graphql.AddErrorf(ctx, "Master plan details not yet available. Master plan id: %s", masterPlanId)
return &model.MasterPlan{ID: masterPlanId}, nil
}
span.LogFields(log.String("response.masterPlanId", masterPlanId))
return mapper.MapEntityToMasterPlan(createdMasterPlanEntity), nil
}
// MasterPlanUpdate is the resolver for the masterPlan_Update field.
func (r *mutationResolver) MasterPlanUpdate(ctx context.Context, input model.MasterPlanUpdateInput) (*model.MasterPlan, error) {
ctx, span := tracing.StartGraphQLTracerSpan(ctx, "MutationResolver.MasterPlanUpdate", graphql.GetOperationContext(ctx))
defer span.Finish()
tracing.SetDefaultResolverSpanTags(ctx, span)
tracing.LogObjectAsJson(span, "input", input)
err := r.Services.MasterPlanService.UpdateMasterPlan(ctx, input.ID, input.Name, input.Retired)
if err != nil {
tracing.TraceErr(span, err)
graphql.AddErrorf(ctx, "Failed to update master plan")
return nil, err
}
updatedMasterPlanEntity, err := r.Services.MasterPlanService.GetMasterPlanById(ctx, input.ID)
if err != nil {
tracing.TraceErr(span, err)
graphql.AddErrorf(ctx, "Failed to get Master plan with id %s", input.ID)
return nil, nil
}
return mapper.MapEntityToMasterPlan(updatedMasterPlanEntity), nil
}
// MasterPlanDuplicate is the resolver for the masterPlan_Duplicate field.
func (r *mutationResolver) MasterPlanDuplicate(ctx context.Context, id string) (*model.MasterPlan, error) {
ctx, span := tracing.StartGraphQLTracerSpan(ctx, "MutationResolver.MasterPlanDuplicate", graphql.GetOperationContext(ctx))
defer span.Finish()
tracing.SetDefaultResolverSpanTags(ctx, span)
tracing.LogObjectAsJson(span, "id", id)
masterPlanId, err := r.Services.MasterPlanService.DuplicateMasterPlan(ctx, id)
if err != nil {
tracing.TraceErr(span, err)
graphql.AddErrorf(ctx, "Failed to duplicate master plan")
return nil, err
}
createdMasterPlanEntity, err := r.Services.MasterPlanService.GetMasterPlanById(ctx, masterPlanId)
if err != nil {
tracing.TraceErr(span, err)
graphql.AddErrorf(ctx, "Master plan details not yet available. Master plan id: %s", masterPlanId)
return &model.MasterPlan{ID: masterPlanId}, nil
}
span.LogFields(log.String("response.masterPlanId", masterPlanId))
return mapper.MapEntityToMasterPlan(createdMasterPlanEntity), nil
}
// MasterPlanMilestoneCreate is the resolver for the masterPlanMilestone_Create field.
func (r *mutationResolver) MasterPlanMilestoneCreate(ctx context.Context, input model.MasterPlanMilestoneInput) (*model.MasterPlanMilestone, error) {
ctx, span := tracing.StartGraphQLTracerSpan(ctx, "MutationResolver.MasterPlanMilestoneCreate", graphql.GetOperationContext(ctx))
defer span.Finish()
tracing.SetDefaultResolverSpanTags(ctx, span)
tracing.LogObjectAsJson(span, "input", input)
masterPlanMilestoneId, err := r.Services.MasterPlanService.CreateMasterPlanMilestone(ctx, input.MasterPlanID, utils.IfNotNilString(input.Name),
input.Order, input.DurationHours, input.Optional, input.Items)
if err != nil {
tracing.TraceErr(span, err)
graphql.AddErrorf(ctx, "Failed to create master plan milestone")
return &model.MasterPlanMilestone{ID: masterPlanMilestoneId}, err
}
createdMasterPlanMilestoneEntity, err := r.Services.MasterPlanService.GetMasterPlanMilestoneById(ctx, masterPlanMilestoneId)
if err != nil {
tracing.TraceErr(span, err)
graphql.AddErrorf(ctx, "Master plan milestone details not yet available. Master plan milestone id: %s", masterPlanMilestoneId)
return &model.MasterPlanMilestone{ID: masterPlanMilestoneId}, nil
}
span.LogFields(log.String("response.masterPlanMilestoneId", masterPlanMilestoneId))
return mapper.MapEntityToMasterPlanMilestone(createdMasterPlanMilestoneEntity), nil
}
// MasterPlanMilestoneUpdate is the resolver for the masterPlanMilestone_Update field.
func (r *mutationResolver) MasterPlanMilestoneUpdate(ctx context.Context, input model.MasterPlanMilestoneUpdateInput) (*model.MasterPlanMilestone, error) {
ctx, span := tracing.StartGraphQLTracerSpan(ctx, "MutationResolver.MasterPlanMilestoneUpdate", graphql.GetOperationContext(ctx))
defer span.Finish()
tracing.SetDefaultResolverSpanTags(ctx, span)
tracing.LogObjectAsJson(span, "input", input)
err := r.Services.MasterPlanService.UpdateMasterPlanMilestone(ctx, input.MasterPlanID, input.ID, input.Name,
input.Order, input.DurationHours, input.Items, input.Optional, input.Retired)
if err != nil {
tracing.TraceErr(span, err)
graphql.AddErrorf(ctx, "Failed to update master plan milestone")
return nil, err
}
updatedMasterPlanMilestoneEntity, err := r.Services.MasterPlanService.GetMasterPlanMilestoneById(ctx, input.ID)
if err != nil {
tracing.TraceErr(span, err)
graphql.AddErrorf(ctx, "Failed to get master plan milestone with id %s", input.ID)
return nil, nil
}
return mapper.MapEntityToMasterPlanMilestone(updatedMasterPlanMilestoneEntity), nil
}
// MasterPlanMilestoneBulkUpdate is the resolver for the masterPlanMilestone_BulkUpdate field.
func (r *mutationResolver) MasterPlanMilestoneBulkUpdate(ctx context.Context, input []*model.MasterPlanMilestoneUpdateInput) ([]*model.MasterPlanMilestone, error) {
ctx, span := tracing.StartGraphQLTracerSpan(ctx, "MutationResolver.MasterPlanMilestoneBulkUpdate", graphql.GetOperationContext(ctx))
defer span.Finish()
tracing.SetDefaultResolverSpanTags(ctx, span)
tracing.LogObjectAsJson(span, "input", input)
var updatedMasterPlanMilestoneEntities []*model.MasterPlanMilestone
var err error
for _, mpms := range input {
err = r.Services.MasterPlanService.UpdateMasterPlanMilestone(ctx, mpms.MasterPlanID, mpms.ID, mpms.Name,
mpms.Order, mpms.DurationHours, mpms.Items, mpms.Optional, mpms.Retired)
if err != nil {
tracing.TraceErr(span, err)
graphql.AddErrorf(ctx, "Failed to update master plan milestone")
err = errors.Join(err)
}
updatedMasterPlanMilestoneEntity, errr := r.Services.MasterPlanService.GetMasterPlanMilestoneById(ctx, mpms.ID)
if errr != nil {
tracing.TraceErr(span, errr)
graphql.AddErrorf(ctx, "Failed to get master plan milestone with id %s", mpms.ID)
err = errors.Join(errr)
}
updatedMasterPlanMilestoneEntities = append(updatedMasterPlanMilestoneEntities, mapper.MapEntityToMasterPlanMilestone(updatedMasterPlanMilestoneEntity))
}
if err != nil {
return nil, err
}
return updatedMasterPlanMilestoneEntities, nil
}
// MasterPlanMilestoneReorder is the resolver for the masterPlanMilestone_Reorder field.
func (r *mutationResolver) MasterPlanMilestoneReorder(ctx context.Context, input model.MasterPlanMilestoneReorderInput) (string, error) {
ctx, span := tracing.StartGraphQLTracerSpan(ctx, "MutationResolver.MasterPlanMilestoneReorder", graphql.GetOperationContext(ctx))
defer span.Finish()
tracing.SetDefaultResolverSpanTags(ctx, span)
tracing.LogObjectAsJson(span, "input", input)
err := r.Services.MasterPlanService.ReorderMasterPlanMilestones(ctx, input.MasterPlanID, input.OrderedIds)
if err != nil {
tracing.TraceErr(span, err)
graphql.AddErrorf(ctx, "Failed to reorder master plan milestones")
return "", err
}
return input.MasterPlanID, nil
}
// MasterPlanMilestoneDuplicate is the resolver for the masterPlanMilestone_Duplicate field.
func (r *mutationResolver) MasterPlanMilestoneDuplicate(ctx context.Context, masterPlanID string, id string) (*model.MasterPlanMilestone, error) {
ctx, span := tracing.StartGraphQLTracerSpan(ctx, "MutationResolver.MasterPlanMilestoneDuplicate", graphql.GetOperationContext(ctx))
defer span.Finish()
tracing.SetDefaultResolverSpanTags(ctx, span)
span.LogFields(log.String("masterPlanID", masterPlanID), log.String("id", id))
masterPlanMilestoneId, err := r.Services.MasterPlanService.DuplicateMasterPlanMilestone(ctx, masterPlanID, id)
if err != nil {
tracing.TraceErr(span, err)
graphql.AddErrorf(ctx, "Failed to duplicate master plan milestone")
return &model.MasterPlanMilestone{ID: masterPlanMilestoneId}, err
}
createdMasterPlanMilestoneEntity, err := r.Services.MasterPlanService.GetMasterPlanMilestoneById(ctx, masterPlanMilestoneId)
if err != nil {
tracing.TraceErr(span, err)
graphql.AddErrorf(ctx, "Master plan milestone details not yet available. Master plan milestone id: %s", masterPlanMilestoneId)
return &model.MasterPlanMilestone{ID: masterPlanMilestoneId}, nil
}
span.LogFields(log.String("response.masterPlanMilestoneId", masterPlanMilestoneId))
return mapper.MapEntityToMasterPlanMilestone(createdMasterPlanMilestoneEntity), nil
}
// MasterPlan is the resolver for the masterPlan field.
func (r *queryResolver) MasterPlan(ctx context.Context, id string) (*model.MasterPlan, error) {
ctx, span := tracing.StartGraphQLTracerSpan(ctx, "QueryResolver.MasterPlan", graphql.GetOperationContext(ctx))
defer span.Finish()
tracing.SetDefaultResolverSpanTags(ctx, span)
span.SetTag(tracing.SpanTagEntityId, id)
masterPlanEntity, err := r.Services.MasterPlanService.GetMasterPlanById(ctx, id)
if err != nil {
tracing.TraceErr(span, err)
graphql.AddErrorf(ctx, "Failed to get Master plan with id %s", id)
return nil, nil
}
if masterPlanEntity == nil || masterPlanEntity.Id == "" {
graphql.AddErrorf(ctx, "Master plan with id %s not found", id)
return nil, nil
}
return mapper.MapEntityToMasterPlan(masterPlanEntity), nil
}
// MasterPlans is the resolver for the masterPlans field.
func (r *queryResolver) MasterPlans(ctx context.Context, retired *bool) ([]*model.MasterPlan, error) {
ctx, span := tracing.StartGraphQLTracerSpan(ctx, "QueryResolver.MasterPlans", graphql.GetOperationContext(ctx))
defer span.Finish()
tracing.SetDefaultResolverSpanTags(ctx, span)
masterPlanEntities, err := r.Services.MasterPlanService.GetMasterPlans(ctx, retired)
if err != nil {
tracing.TraceErr(span, err)
graphql.AddErrorf(ctx, "Failed to get Master plans")
return nil, nil
}
return mapper.MapEntitiesToMasterPlans(masterPlanEntities), nil
}
// MasterPlan returns generated.MasterPlanResolver implementation.
func (r *Resolver) MasterPlan() generated.MasterPlanResolver { return &masterPlanResolver{r} }
type masterPlanResolver struct{ *Resolver }