-
Notifications
You must be signed in to change notification settings - Fork 12
/
opportunity.resolvers.go
154 lines (132 loc) · 7.6 KB
/
opportunity.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
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"
"github.com/99designs/gqlgen/graphql"
"github.com/openline-ai/openline-customer-os/packages/server/customer-os-api/constants"
"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"
)
// OpportunityUpdate is the resolver for the opportunityUpdate field.
func (r *mutationResolver) OpportunityUpdate(ctx context.Context, input model.OpportunityUpdateInput) (*model.Opportunity, error) {
ctx, span := tracing.StartGraphQLTracerSpan(ctx, "MutationResolver.OpportunityUpdate", graphql.GetOperationContext(ctx))
defer span.Finish()
tracing.SetDefaultResolverSpanTags(ctx, span)
span.LogFields(log.String("request.opportunityId", input.OpportunityID))
err := r.Services.OpportunityService.Update(ctx, mapper.MapOpportunityUpdateInputToEntity(input))
if err != nil {
tracing.TraceErr(span, err)
graphql.AddErrorf(ctx, "Failed to update opportunity %s", input.OpportunityID)
return &model.Opportunity{ID: input.OpportunityID}, nil
}
opportunityEntity, err := r.Services.OpportunityService.GetById(ctx, input.OpportunityID)
if err != nil {
tracing.TraceErr(span, err)
graphql.AddErrorf(ctx, "Failed fetching opportunity details. Opportunity id: %s", input.OpportunityID)
return &model.Opportunity{ID: input.OpportunityID}, nil
}
return mapper.MapEntityToOpportunity(opportunityEntity), nil
}
// OpportunityRenewalUpdate is the resolver for the opportunityRenewalUpdate field.
func (r *mutationResolver) OpportunityRenewalUpdate(ctx context.Context, input model.OpportunityRenewalUpdateInput, ownerUserID *string) (*model.Opportunity, error) {
ctx, span := tracing.StartGraphQLTracerSpan(ctx, "MutationResolver.OpportunityRenewalUpdate", graphql.GetOperationContext(ctx))
defer span.Finish()
tracing.SetDefaultResolverSpanTags(ctx, span)
tracing.LogObjectAsJson(span, "input", input)
err := r.Services.OpportunityService.UpdateRenewal(ctx, input.OpportunityID, mapper.MapOpportunityRenewalLikelihoodFromModel(input.RenewalLikelihood), input.Amount, input.Comments, input.OwnerUserID, input.RenewalAdjustedRate, utils.IfNotNilStringWithDefault(input.AppSource, constants.AppSourceCustomerOsApi))
if err != nil {
tracing.TraceErr(span, err)
graphql.AddErrorf(ctx, "Failed to update opportunity renewal %s", input.OpportunityID)
return &model.Opportunity{ID: input.OpportunityID}, nil
}
opportunityEntity, err := r.Services.OpportunityService.GetById(ctx, input.OpportunityID)
if err != nil {
tracing.TraceErr(span, err)
graphql.AddErrorf(ctx, "Failed fetching opportunity details. Opportunity id: %s", input.OpportunityID)
return &model.Opportunity{ID: input.OpportunityID}, nil
}
return mapper.MapEntityToOpportunity(opportunityEntity), nil
}
// OpportunityRenewalUpdateAllForOrganization is the resolver for the opportunityRenewal_UpdateAllForOrganization field.
func (r *mutationResolver) OpportunityRenewalUpdateAllForOrganization(ctx context.Context, input model.OpportunityRenewalUpdateAllForOrganizationInput) (*model.Organization, error) {
ctx, span := tracing.StartGraphQLTracerSpan(ctx, "MutationResolver.OpportunityRenewalUpdate", graphql.GetOperationContext(ctx))
defer span.Finish()
tracing.SetDefaultResolverSpanTags(ctx, span)
tracing.LogObjectAsJson(span, "input", input)
if input.RenewalLikelihood != nil {
err := r.Services.OpportunityService.UpdateRenewalsForOrganization(ctx, input.OrganizationID, mapper.MapOpportunityRenewalLikelihoodFromModel(input.RenewalLikelihood), input.RenewalAdjustedRate)
if err != nil {
tracing.TraceErr(span, err)
graphql.AddErrorf(ctx, "Failed to update renewal opportunities for organization %s", input.OrganizationID)
return nil, nil
}
}
organizationEntity, err := r.Services.OrganizationService.GetById(ctx, input.OrganizationID)
if err != nil {
tracing.TraceErr(span, err)
graphql.AddErrorf(ctx, "Failed fetching organization details. Organization id: %s", input.OrganizationID)
return nil, nil
}
return mapper.MapEntityToOrganization(organizationEntity), nil
}
// CreatedBy is the resolver for the createdBy field.
func (r *opportunityResolver) CreatedBy(ctx context.Context, obj *model.Opportunity) (*model.User, error) {
ctx = tracing.EnrichCtxWithSpanCtxForGraphQL(ctx, graphql.GetOperationContext(ctx))
userEntityNillable, err := dataloader.For(ctx).GetUserCreatorForOpportunity(ctx, obj.ID)
if err != nil {
tracing.TraceErr(opentracing.SpanFromContext(ctx), err)
r.log.Errorf("error fetching user creator for opportunity %s: %s", obj.ID, err.Error())
graphql.AddErrorf(ctx, "error fetching user creator for opportunity %s", obj.ID)
return nil, nil
}
return mapper.MapEntityToUser(userEntityNillable), nil
}
// Owner is the resolver for the owner field.
func (r *opportunityResolver) Owner(ctx context.Context, obj *model.Opportunity) (*model.User, error) {
ctx = tracing.EnrichCtxWithSpanCtxForGraphQL(ctx, graphql.GetOperationContext(ctx))
userEntityNillable, err := dataloader.For(ctx).GetUserOwnerForOpportunity(ctx, obj.ID)
if err != nil {
tracing.TraceErr(opentracing.SpanFromContext(ctx), err)
r.log.Errorf("error fetching user owner for opportunity %s: %s", obj.ID, err.Error())
graphql.AddErrorf(ctx, "error fetching user owner for opportunity %s", obj.ID)
return nil, nil
}
return mapper.MapEntityToUser(userEntityNillable), nil
}
// ExternalLinks is the resolver for the externalLinks field.
func (r *opportunityResolver) ExternalLinks(ctx context.Context, obj *model.Opportunity) ([]*model.ExternalSystem, error) {
ctx = tracing.EnrichCtxWithSpanCtxForGraphQL(ctx, graphql.GetOperationContext(ctx))
entities, err := dataloader.For(ctx).GetExternalSystemsForOpportunity(ctx, obj.ID)
if err != nil {
tracing.TraceErr(opentracing.SpanFromContext(ctx), err)
r.log.Errorf("Failed to get external system for opportunity %s: %s", obj.ID, err.Error())
graphql.AddErrorf(ctx, "Failed to get external system for opportunity %s", obj.ID)
return nil, nil
}
return mapper.MapEntitiesToExternalSystems(entities), nil
}
// Opportunity is the resolver for the opportunity field.
func (r *queryResolver) Opportunity(ctx context.Context, id string) (*model.Opportunity, error) {
ctx, span := tracing.StartGraphQLTracerSpan(ctx, "QueryResolver.Opportunity", graphql.GetOperationContext(ctx))
defer span.Finish()
tracing.SetDefaultResolverSpanTags(ctx, span)
span.LogFields(log.String("request.issueID", id))
opportunityEntity, err := r.Services.OpportunityService.GetById(ctx, id)
if err != nil || opportunityEntity == nil {
tracing.TraceErr(span, err)
graphql.AddErrorf(ctx, "Opportunity with id %s not found", id)
return nil, err
}
return mapper.MapEntityToOpportunity(opportunityEntity), nil
}
// Opportunity returns generated.OpportunityResolver implementation.
func (r *Resolver) Opportunity() generated.OpportunityResolver { return &opportunityResolver{r} }
type opportunityResolver struct{ *Resolver }