-
Notifications
You must be signed in to change notification settings - Fork 12
/
organization_plan.resolvers.go
421 lines (383 loc) · 18.9 KB
/
organization_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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
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"
"time"
"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"
)
// OrganizationPlanCreate is the resolver for the organizationPlan_Create field.
func (r *mutationResolver) OrganizationPlanCreate(ctx context.Context, input model.OrganizationPlanInput) (*model.OrganizationPlan, error) {
ctx, span := tracing.StartGraphQLTracerSpan(ctx, "MutationResolver.OrganizationPlanCreate", graphql.GetOperationContext(ctx))
defer span.Finish()
tracing.SetDefaultResolverSpanTags(ctx, span)
tracing.LogObjectAsJson(span, "input", input)
// Create empty organization plan
orgPlanId, err := r.Services.OrganizationPlanService.CreateOrganizationPlan(ctx, utils.IfNotNilString(input.Name), *input.MasterPlanID, input.OrganizationID)
if err != nil {
tracing.TraceErr(span, err)
graphql.AddErrorf(ctx, "Failed to create organization plan")
return &model.OrganizationPlan{ID: orgPlanId}, err
}
// get master plan milestones
masterPlanMilestonesEntities, err := r.Services.MasterPlanService.GetMasterPlanMilestonesForMasterPlans(ctx, []string{*input.MasterPlanID})
if err != nil {
tracing.TraceErr(span, err)
graphql.AddErrorf(ctx, "Failed to get master plan milestones")
return &model.OrganizationPlan{ID: orgPlanId}, err
}
// create organization plan milestones based on each master plan milestone
for _, masterPlanMilestoneEntity := range *masterPlanMilestonesEntities {
if masterPlanMilestoneEntity.Retired || masterPlanMilestoneEntity.Optional {
continue
}
msDueDate := time.Now().UTC().Add(time.Hour * time.Duration(masterPlanMilestoneEntity.DurationHours))
_, err := r.Services.OrganizationPlanService.CreateOrganizationPlanMilestone(
ctx,
orgPlanId,
input.OrganizationID,
masterPlanMilestoneEntity.Name,
&masterPlanMilestoneEntity.Order,
&msDueDate,
masterPlanMilestoneEntity.Optional,
false, // there is no ad-hoc milestone for master plan milestones
masterPlanMilestoneEntity.Items,
)
if err != nil {
tracing.TraceErr(span, err)
graphql.AddErrorf(ctx, "Failed to create organization plan milestone")
return &model.OrganizationPlan{ID: orgPlanId}, err
}
}
// get created organization plan
createdOrgPlanEntity, err := r.Services.OrganizationPlanService.GetOrganizationPlanById(ctx, orgPlanId)
if err != nil {
tracing.TraceErr(span, err)
graphql.AddErrorf(ctx, "Organization plan details not yet available. Organization plan id: %s", orgPlanId)
return &model.OrganizationPlan{ID: orgPlanId}, nil
}
// return created organization plan
span.LogFields(log.String("response.organizationPlanId", orgPlanId))
return mapper.MapEntityToOrganizationPlan(createdOrgPlanEntity), nil
}
// OrganizationPlanUpdate is the resolver for the organizationPlan_Update field.
func (r *mutationResolver) OrganizationPlanUpdate(ctx context.Context, input model.OrganizationPlanUpdateInput) (*model.OrganizationPlan, error) {
ctx, span := tracing.StartGraphQLTracerSpan(ctx, "MutationResolver.OrganizationPlanUpdate", graphql.GetOperationContext(ctx))
defer span.Finish()
tracing.SetDefaultResolverSpanTags(ctx, span)
tracing.LogObjectAsJson(span, "input", input)
err := r.Services.OrganizationPlanService.UpdateOrganizationPlan(ctx, input.ID, input.OrganizationID, input.Name, input.Retired, input.StatusDetails)
if err != nil {
tracing.TraceErr(span, err)
graphql.AddErrorf(ctx, "Failed to update organization plan")
return nil, err
}
updatedOrgPlanEntity, err := r.Services.OrganizationPlanService.GetOrganizationPlanById(ctx, input.ID)
if err != nil {
tracing.TraceErr(span, err)
graphql.AddErrorf(ctx, "Failed to get Organization plan with id %s", input.ID)
return nil, nil
}
return mapper.MapEntityToOrganizationPlan(updatedOrgPlanEntity), nil
}
// OrganizationPlanDuplicate is the resolver for the organizationPlan_Duplicate field.
func (r *mutationResolver) OrganizationPlanDuplicate(ctx context.Context, id string, organizationID string) (*model.OrganizationPlan, error) {
ctx, span := tracing.StartGraphQLTracerSpan(ctx, "MutationResolver.OrganizationPlanDuplicate", graphql.GetOperationContext(ctx))
defer span.Finish()
tracing.SetDefaultResolverSpanTags(ctx, span)
tracing.LogObjectAsJson(span, "id", id)
opId, err := r.Services.OrganizationPlanService.DuplicateOrganizationPlan(ctx, id, organizationID)
if err != nil {
tracing.TraceErr(span, err)
graphql.AddErrorf(ctx, "Failed to duplicate org plan")
return nil, err
}
createdOrgPlanEntity, err := r.Services.OrganizationPlanService.GetOrganizationPlanById(ctx, opId)
if err != nil {
tracing.TraceErr(span, err)
graphql.AddErrorf(ctx, "Organization plan details not yet available. Organization plan id: %s", opId)
return &model.OrganizationPlan{ID: opId}, nil
}
span.LogFields(log.String("response.organizationPlanId", opId))
return mapper.MapEntityToOrganizationPlan(createdOrgPlanEntity), nil
}
// OrganizationPlanMilestoneCreate is the resolver for the organizationPlanMilestone_Create field.
func (r *mutationResolver) OrganizationPlanMilestoneCreate(ctx context.Context, input model.OrganizationPlanMilestoneInput) (*model.OrganizationPlanMilestone, error) {
ctx, span := tracing.StartGraphQLTracerSpan(ctx, "MutationResolver.OrganizationPlanMilestoneCreate", graphql.GetOperationContext(ctx))
defer span.Finish()
tracing.SetDefaultResolverSpanTags(ctx, span)
tracing.LogObjectAsJson(span, "input", input)
opMilestoneId, err := r.Services.OrganizationPlanService.CreateOrganizationPlanMilestone(ctx, input.OrganizationPlanID, input.OrganizationID, utils.IfNotNilString(input.Name),
&input.Order, &input.DueDate, input.Optional, input.Adhoc, input.Items)
if err != nil {
tracing.TraceErr(span, err)
graphql.AddErrorf(ctx, "Failed to create organization plan milestone")
return &model.OrganizationPlanMilestone{ID: opMilestoneId}, err
}
createdMilestoneEntity, err := r.Services.OrganizationPlanService.GetOrganizationPlanMilestoneById(ctx, opMilestoneId)
if err != nil {
tracing.TraceErr(span, err)
graphql.AddErrorf(ctx, "Organization plan milestone details not yet available. Organization plan milestone id: %s", opMilestoneId)
return &model.OrganizationPlanMilestone{ID: opMilestoneId}, nil
}
span.LogFields(log.String("response.organizationPlanMilestoneId", opMilestoneId))
return mapper.MapEntityToOrganizationPlanMilestone(createdMilestoneEntity), nil
}
// OrganizationPlanMilestoneUpdate is the resolver for the organizationPlanMilestone_Update field.
func (r *mutationResolver) OrganizationPlanMilestoneUpdate(ctx context.Context, input model.OrganizationPlanMilestoneUpdateInput) (*model.OrganizationPlanMilestone, error) {
ctx, span := tracing.StartGraphQLTracerSpan(ctx, "MutationResolver.OrganizationPlanMilestoneUpdate", graphql.GetOperationContext(ctx))
defer span.Finish()
tracing.SetDefaultResolverSpanTags(ctx, span)
tracing.LogObjectAsJson(span, "input", input)
err := r.Services.OrganizationPlanService.UpdateOrganizationPlanMilestone(ctx, input.OrganizationID, input.OrganizationPlanID, input.ID, input.Name,
input.Order, input.DueDate, input.Items, input.Optional, input.Adhoc, input.Retired, input.StatusDetails)
if err != nil {
tracing.TraceErr(span, err)
graphql.AddErrorf(ctx, "Failed to update organization plan milestone")
return nil, err
}
updatedMilestoneEntity, err := r.Services.OrganizationPlanService.GetOrganizationPlanMilestoneById(ctx, input.ID)
if err != nil {
tracing.TraceErr(span, err)
graphql.AddErrorf(ctx, "Failed to get organization plan milestone with id %s", input.ID)
return nil, nil
}
return mapper.MapEntityToOrganizationPlanMilestone(updatedMilestoneEntity), nil
}
// OrganizationPlanMilestoneBulkUpdate is the resolver for the organizationPlanMilestone_BulkUpdate field.
func (r *mutationResolver) OrganizationPlanMilestoneBulkUpdate(ctx context.Context, input []*model.OrganizationPlanMilestoneUpdateInput) ([]*model.OrganizationPlanMilestone, error) {
ctx, span := tracing.StartGraphQLTracerSpan(ctx, "MutationResolver.OrganizationPlanMilestoneBulkUpdate", graphql.GetOperationContext(ctx))
defer span.Finish()
tracing.SetDefaultResolverSpanTags(ctx, span)
tracing.LogObjectAsJson(span, "input", input)
var updatedOrgPlanMilestoneEntities []*model.OrganizationPlanMilestone
var err error
for _, opms := range input {
err = r.Services.OrganizationPlanService.UpdateOrganizationPlanMilestone(ctx, opms.OrganizationID, opms.OrganizationPlanID, opms.ID, opms.Name,
opms.Order, opms.DueDate, opms.Items, opms.Optional, opms.Adhoc, opms.Retired, opms.StatusDetails)
if err != nil {
tracing.TraceErr(span, err)
graphql.AddErrorf(ctx, "Failed to update org plan milestone")
err = errors.Join(err)
}
updatedOPMilestoneEntity, errr := r.Services.OrganizationPlanService.GetOrganizationPlanMilestoneById(ctx, opms.ID)
if errr != nil {
tracing.TraceErr(span, errr)
graphql.AddErrorf(ctx, "Failed to get org plan milestone with id %s", opms.ID)
err = errors.Join(errr)
}
updatedOrgPlanMilestoneEntities = append(updatedOrgPlanMilestoneEntities, mapper.MapEntityToOrganizationPlanMilestone(updatedOPMilestoneEntity))
}
if err != nil {
return nil, err
}
return updatedOrgPlanMilestoneEntities, nil
}
// OrganizationPlanMilestoneReorder is the resolver for the organizationPlanMilestone_Reorder field.
func (r *mutationResolver) OrganizationPlanMilestoneReorder(ctx context.Context, input model.OrganizationPlanMilestoneReorderInput) (string, error) {
ctx, span := tracing.StartGraphQLTracerSpan(ctx, "MutationResolver.OrganizationPlanMilestoneReorder", graphql.GetOperationContext(ctx))
defer span.Finish()
tracing.SetDefaultResolverSpanTags(ctx, span)
tracing.LogObjectAsJson(span, "input", input)
err := r.Services.OrganizationPlanService.ReorderOrganizationPlanMilestones(ctx, input.OrganizationPlanID, input.OrganizationID, input.OrderedIds)
if err != nil {
tracing.TraceErr(span, err)
graphql.AddErrorf(ctx, "Failed to reorder org plan milestones")
return "", err
}
return input.OrganizationPlanID, nil
}
// OrganizationPlanMilestoneDuplicate is the resolver for the organizationPlanMilestone_Duplicate field.
func (r *mutationResolver) OrganizationPlanMilestoneDuplicate(ctx context.Context, organizationID string, organizationPlanID string, id string) (*model.OrganizationPlanMilestone, error) {
ctx, span := tracing.StartGraphQLTracerSpan(ctx, "MutationResolver.OrganizationPlanMilestoneDuplicate", graphql.GetOperationContext(ctx))
defer span.Finish()
tracing.SetDefaultResolverSpanTags(ctx, span)
span.LogFields(log.String("organizationPlanID", organizationPlanID), log.String("id", id))
opMilestoneId, err := r.Services.OrganizationPlanService.DuplicateOrganizationPlanMilestone(ctx, organizationPlanID, organizationID, id)
if err != nil {
tracing.TraceErr(span, err)
graphql.AddErrorf(ctx, "Failed to duplicate org plan milestone")
return &model.OrganizationPlanMilestone{ID: opMilestoneId}, err
}
createdMilestoneEntity, err := r.Services.OrganizationPlanService.GetOrganizationPlanMilestoneById(ctx, opMilestoneId)
if err != nil {
tracing.TraceErr(span, err)
graphql.AddErrorf(ctx, "Org plan milestone details not yet available. Org plan milestone id: %s", opMilestoneId)
return &model.OrganizationPlanMilestone{ID: opMilestoneId}, nil
}
span.LogFields(log.String("response.OrganizationPlanMilestoneId", opMilestoneId))
return mapper.MapEntityToOrganizationPlanMilestone(createdMilestoneEntity), nil
}
// Milestones is the resolver for the milestones field.
func (r *organizationPlanResolver) Milestones(ctx context.Context, obj *model.OrganizationPlan) ([]*model.OrganizationPlanMilestone, error) {
ctx = tracing.EnrichCtxWithSpanCtxForGraphQL(ctx, graphql.GetOperationContext(ctx))
orgPlanMilestonesEntities, err := dataloader.For(ctx).GetOrganizationPlanMilestonesForOrganizationPlan(ctx, obj.ID)
if err != nil {
tracing.TraceErr(opentracing.SpanFromContext(ctx), err)
r.log.Errorf("Failed to get org plan milestones for org plan %s: %s", obj.ID, err.Error())
graphql.AddErrorf(ctx, "Failed to get org plan milestones for org plan %s", obj.ID)
return nil, nil
}
allOrgPlanMilestones := mapper.MapEntitiesToOrganizationPlanMilestones(orgPlanMilestonesEntities)
// filter out retired milestones
var milestones []*model.OrganizationPlanMilestone
for _, orgPlanMilestone := range allOrgPlanMilestones {
if !orgPlanMilestone.Retired {
milestones = append(milestones, orgPlanMilestone)
}
}
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 *organizationPlanResolver) RetiredMilestones(ctx context.Context, obj *model.OrganizationPlan) ([]*model.OrganizationPlanMilestone, error) {
ctx = tracing.EnrichCtxWithSpanCtxForGraphQL(ctx, graphql.GetOperationContext(ctx))
orgPlanMilestonesEntities, err := dataloader.For(ctx).GetOrganizationPlanMilestonesForOrganizationPlan(ctx, obj.ID)
if err != nil {
tracing.TraceErr(opentracing.SpanFromContext(ctx), err)
r.log.Errorf("Failed to get retired org plan milestones for org plan %s: %s", obj.ID, err.Error())
graphql.AddErrorf(ctx, "Failed to get retired org plan milestones for org plan %s", obj.ID)
return nil, nil
}
allOrgPlanMilestones := mapper.MapEntitiesToOrganizationPlanMilestones(orgPlanMilestonesEntities)
// filter out non-retired milestones
var milestones []*model.OrganizationPlanMilestone
for _, orgPlanMilestone := range allOrgPlanMilestones {
if orgPlanMilestone.Retired {
milestones = append(milestones, orgPlanMilestone)
}
}
span := opentracing.SpanFromContext(ctx)
if span != nil {
span.LogFields(log.Int("response.length", len(milestones)))
}
return milestones, nil
}
// OrganizationPlan is the resolver for the organizationPlan field.
func (r *queryResolver) OrganizationPlan(ctx context.Context, id string) (*model.OrganizationPlan, error) {
ctx, span := tracing.StartGraphQLTracerSpan(ctx, "QueryResolver.OrganizationPlan", graphql.GetOperationContext(ctx))
defer span.Finish()
tracing.SetDefaultResolverSpanTags(ctx, span)
span.SetTag(tracing.SpanTagEntityId, id)
orgPlanEntity, err := r.Services.OrganizationPlanService.GetOrganizationPlanById(ctx, id)
if err != nil {
tracing.TraceErr(span, err)
graphql.AddErrorf(ctx, "Failed to get Org plan with id %s", id)
return nil, nil
}
if orgPlanEntity == nil || orgPlanEntity.Id == "" {
graphql.AddErrorf(ctx, "Org plan with id %s not found", id)
return nil, nil
}
op := mapper.MapEntityToOrganizationPlan(orgPlanEntity)
return op, nil
}
// OrganizationPlansForOrganization is the resolver for the organizationPlansForOrganization field.
func (r *queryResolver) OrganizationPlansForOrganization(ctx context.Context, organizationID string) ([]*model.OrganizationPlan, error) {
ctx, span := tracing.StartGraphQLTracerSpan(ctx, "QueryResolver.OrganizationPlansForOrganization", graphql.GetOperationContext(ctx))
defer span.Finish()
tracing.SetDefaultResolverSpanTags(ctx, span)
span.SetTag(tracing.SpanTagEntityId, organizationID)
orgPlanEntities, err := r.Services.OrganizationPlanService.GetOrganizationPlansForOrganization(ctx, organizationID)
if err != nil {
tracing.TraceErr(span, err)
graphql.AddErrorf(ctx, "Failed to get Org plans for organization %s", organizationID)
return nil, nil
}
opArray := mapper.MapEntitiesToOrganizationPlans(orgPlanEntities)
for _, op := range opArray {
span.SetTag(tracing.SpanTagEntityId, op.ID)
allMilestonesDone := true
planLate := false
planStarted := false
for _, m := range op.Milestones {
span.SetTag(tracing.SpanTagEntityId, m.ID)
now := time.Now().UTC()
due := m.DueDate
if now.After(due) && now.Day() != due.Day() {
planLate = true
switch m.StatusDetails.Status {
case model.OnboardingPlanMilestoneStatusDone:
case model.OnboardingPlanMilestoneStatusDoneLate:
planStarted = true
m.StatusDetails.Status = model.OnboardingPlanMilestoneStatusDoneLate
case model.OnboardingPlanMilestoneStatusStarted:
case model.OnboardingPlanMilestoneStatusStartedLate:
allMilestonesDone = false
planStarted = true
m.StatusDetails.Status = model.OnboardingPlanMilestoneStatusStartedLate
default:
allMilestonesDone = false
m.StatusDetails.Status = model.OnboardingPlanMilestoneStatusNotStartedLate
}
m.StatusDetails.UpdatedAt = time.Now().UTC()
stsDtls := &model.OrganizationPlanMilestoneStatusDetailsInput{
Status: m.StatusDetails.Status,
UpdatedAt: m.StatusDetails.UpdatedAt,
Text: m.StatusDetails.Text,
}
err = r.Services.OrganizationPlanService.UpdateOrganizationPlanMilestone(
ctx,
organizationID,
op.ID,
m.ID,
nil,
nil,
nil,
nil,
nil,
nil,
nil,
stsDtls,
)
if err != nil {
tracing.TraceErr(span, err)
graphql.AddErrorf(ctx, "Failed to update org plan milestone")
return nil, err
}
}
}
// update plan status in place for UI, the milestones updates will propagate status through the DB
if planLate {
if allMilestonesDone {
op.StatusDetails.Status = model.OnboardingPlanStatusDoneLate
} else if planStarted {
op.StatusDetails.Status = model.OnboardingPlanStatusLate
} else {
op.StatusDetails.Status = model.OnboardingPlanStatusNotStartedLate
}
}
}
return opArray, nil
}
// OrganizationPlans is the resolver for the organizationPlans field.
func (r *queryResolver) OrganizationPlans(ctx context.Context, retired *bool) ([]*model.OrganizationPlan, error) {
ctx, span := tracing.StartGraphQLTracerSpan(ctx, "QueryResolver.OrganizationPlans", graphql.GetOperationContext(ctx))
defer span.Finish()
tracing.SetDefaultResolverSpanTags(ctx, span)
orgPlanEntities, err := r.Services.OrganizationPlanService.GetOrganizationPlans(ctx, retired)
if err != nil {
tracing.TraceErr(span, err)
graphql.AddErrorf(ctx, "Failed to get Organization plans")
return nil, nil
}
return mapper.MapEntitiesToOrganizationPlans(orgPlanEntities), nil
}
// OrganizationPlan returns generated.OrganizationPlanResolver implementation.
func (r *Resolver) OrganizationPlan() generated.OrganizationPlanResolver {
return &organizationPlanResolver{r}
}
type organizationPlanResolver struct{ *Resolver }