-
Notifications
You must be signed in to change notification settings - Fork 12
/
contract.resolvers.go
364 lines (314 loc) · 16.4 KB
/
contract.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
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"
"sort"
"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/repository"
"github.com/openline-ai/openline-customer-os/packages/server/customer-os-api/service"
"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"
neo4jentity "github.com/openline-ai/openline-customer-os/packages/server/customer-os-neo4j-repository/entity"
neo4jenum "github.com/openline-ai/openline-customer-os/packages/server/customer-os-neo4j-repository/enum"
opentracing "github.com/opentracing/opentracing-go"
"github.com/opentracing/opentracing-go/log"
)
// ContractLineItems is the resolver for the contractLineItems field.
func (r *contractResolver) ContractLineItems(ctx context.Context, obj *model.Contract) ([]*model.ServiceLineItem, error) {
ctx = tracing.EnrichCtxWithSpanCtxForGraphQL(ctx, graphql.GetOperationContext(ctx))
serviceLineItemEntities, err := dataloader.For(ctx).GetServiceLineItemsForContract(ctx, obj.ID)
if err != nil {
tracing.TraceErr(opentracing.SpanFromContext(ctx), err)
r.log.Errorf("Failed to get service line items for contract %s: %s", obj.ID, err.Error())
graphql.AddErrorf(ctx, "Failed to get service line items for contract %s", obj.ID)
return nil, nil
}
return mapper.MapEntitiesToServiceLineItems(serviceLineItemEntities), nil
}
// CreatedBy is the resolver for the createdBy field.
func (r *contractResolver) CreatedBy(ctx context.Context, obj *model.Contract) (*model.User, error) {
ctx = tracing.EnrichCtxWithSpanCtxForGraphQL(ctx, graphql.GetOperationContext(ctx))
userEntityNillable, err := dataloader.For(ctx).GetUserCreatorForContract(ctx, obj.ID)
if err != nil {
tracing.TraceErr(opentracing.SpanFromContext(ctx), err)
r.log.Errorf("error fetching user creator for service line item %s: %s", obj.ID, err.Error())
graphql.AddErrorf(ctx, "error fetching user creator for service line item %s", obj.ID)
return nil, nil
}
return mapper.MapEntityToUser(userEntityNillable), nil
}
// ExternalLinks is the resolver for the externalLinks field.
func (r *contractResolver) ExternalLinks(ctx context.Context, obj *model.Contract) ([]*model.ExternalSystem, error) {
ctx = tracing.EnrichCtxWithSpanCtxForGraphQL(ctx, graphql.GetOperationContext(ctx))
entities, err := dataloader.For(ctx).GetExternalSystemsForContract(ctx, obj.ID)
if err != nil {
tracing.TraceErr(opentracing.SpanFromContext(ctx), err)
r.log.Errorf("Failed to get external system for contract %s: %s", obj.ID, err.Error())
graphql.AddErrorf(ctx, "Failed to get external system for contract %s", obj.ID)
return nil, nil
}
return mapper.MapEntitiesToExternalSystems(entities), nil
}
// Opportunities is the resolver for the opportunities field.
func (r *contractResolver) Opportunities(ctx context.Context, obj *model.Contract) ([]*model.Opportunity, error) {
ctx = tracing.EnrichCtxWithSpanCtxForGraphQL(ctx, graphql.GetOperationContext(ctx))
opportunityEntities, err := dataloader.For(ctx).GetOpportunitiesForContract(ctx, obj.ID)
if err != nil {
tracing.TraceErr(opentracing.SpanFromContext(ctx), err)
r.log.Errorf("Failed to get opportunities for contract %s: %s", obj.ID, err.Error())
graphql.AddErrorf(ctx, "Failed to get opportunities for contract %s", obj.ID)
return nil, nil
}
return mapper.MapEntitiesToOpportunities(opportunityEntities), nil
}
// Owner is the resolver for the owner field.
func (r *contractResolver) Owner(ctx context.Context, obj *model.Contract) (*model.User, error) {
ctx, span := tracing.StartGraphQLTracerSpan(ctx, "ContractResolver.Owner", graphql.GetOperationContext(ctx))
defer span.Finish()
tracing.SetDefaultResolverSpanTags(ctx, span)
span.LogFields(log.String("request.contractID", obj.ID))
owner, err := r.Services.UserService.GetContactOwner(ctx, obj.ID)
if err != nil {
tracing.TraceErr(span, err)
graphql.AddErrorf(ctx, "Failed to get owner for contact %s", obj.ID)
return nil, err
}
if owner == nil {
return nil, nil
}
return mapper.MapEntityToUser(owner), err
}
// Attachments is the resolver for the attachments field.
func (r *contractResolver) Attachments(ctx context.Context, obj *model.Contract) ([]*model.Attachment, error) {
ctx = tracing.EnrichCtxWithSpanCtxForGraphQL(ctx, graphql.GetOperationContext(ctx))
attachmentEntities, err := dataloader.For(ctx).GetAttachmentsForContract(ctx, obj.ID)
if err != nil {
tracing.TraceErr(opentracing.SpanFromContext(ctx), err)
r.log.Errorf("Failed to get attachments for contract %s: %s", obj.ID, err.Error())
graphql.AddErrorf(ctx, "Failed to get attachments for contract %s", obj.ID)
return nil, nil
}
return mapper.MapEntitiesToAttachment(attachmentEntities), nil
}
// Invoices is the resolver for the invoices field.
func (r *contractResolver) Invoices(ctx context.Context, obj *model.Contract) ([]*model.Invoice, error) {
ctx = tracing.EnrichCtxWithSpanCtxForGraphQL(ctx, graphql.GetOperationContext(ctx))
invoiceEntities, err := dataloader.For(ctx).GetInvoicesForContract(ctx, obj.Metadata.ID)
if err != nil {
tracing.TraceErr(opentracing.SpanFromContext(ctx), err)
r.log.Errorf("failed to get invoices for contract %s: %s", obj.Metadata.ID, err.Error())
graphql.AddErrorf(ctx, "failed to get invoices for contract %s", obj.Metadata.ID)
return nil, nil
}
filteredInvoiceEntities := neo4jentity.InvoiceEntities{}
if invoiceEntities != nil {
for _, invoice := range *invoiceEntities {
if !invoice.DryRun && invoice.Status != neo4jenum.InvoiceStatusInitialized {
filteredInvoiceEntities = append(filteredInvoiceEntities, invoice)
}
}
}
sort.Slice(filteredInvoiceEntities, func(i, j int) bool {
return filteredInvoiceEntities[i].IssuedDate.After(filteredInvoiceEntities[j].IssuedDate)
})
return mapper.MapEntitiesToInvoices(&filteredInvoiceEntities), nil
}
// UpcomingInvoices is the resolver for the upcomingInvoices field.
func (r *contractResolver) UpcomingInvoices(ctx context.Context, obj *model.Contract) ([]*model.Invoice, error) {
ctx = tracing.EnrichCtxWithSpanCtxForGraphQL(ctx, graphql.GetOperationContext(ctx))
invoiceEntities, err := dataloader.For(ctx).GetInvoicesForContract(ctx, obj.Metadata.ID)
if err != nil {
tracing.TraceErr(opentracing.SpanFromContext(ctx), err)
r.log.Errorf("failed to get upcoming invoices for contract %s: %s", obj.Metadata.ID, err.Error())
graphql.AddErrorf(ctx, "failed to get upcoming invoices for contract %s", obj.Metadata.ID)
return nil, nil
}
upcomingInvoiceEntities := neo4jentity.InvoiceEntities{}
if invoiceEntities != nil {
for _, invoice := range *invoiceEntities {
if invoice.DryRun && invoice.Preview && !invoice.IssuedDate.Before(utils.Today()) && invoice.Status != neo4jenum.InvoiceStatusInitialized {
upcomingInvoiceEntities = append(upcomingInvoiceEntities, invoice)
}
}
}
sort.Slice(upcomingInvoiceEntities, func(i, j int) bool {
return upcomingInvoiceEntities[i].IssuedDate.Before(upcomingInvoiceEntities[j].IssuedDate)
})
return mapper.MapEntitiesToInvoices(&upcomingInvoiceEntities), nil
}
// ServiceLineItems is the resolver for the serviceLineItems field.
func (r *contractResolver) ServiceLineItems(ctx context.Context, obj *model.Contract) ([]*model.ServiceLineItem, error) {
ctx = tracing.EnrichCtxWithSpanCtxForGraphQL(ctx, graphql.GetOperationContext(ctx))
serviceLineItemEntities, err := dataloader.For(ctx).GetServiceLineItemsForContract(ctx, obj.ID)
if err != nil {
tracing.TraceErr(opentracing.SpanFromContext(ctx), err)
r.log.Errorf("Failed to get service line items for contract %s: %s", obj.ID, err.Error())
graphql.AddErrorf(ctx, "Failed to get service line items for contract %s", obj.ID)
return nil, nil
}
return mapper.MapEntitiesToServiceLineItems(serviceLineItemEntities), nil
}
// ContractCreate is the resolver for the contract_Create field.
func (r *mutationResolver) ContractCreate(ctx context.Context, input model.ContractInput) (*model.Contract, error) {
ctx, span := tracing.StartGraphQLTracerSpan(ctx, "MutationResolver.ContractCreate", graphql.GetOperationContext(ctx))
defer span.Finish()
tracing.SetDefaultResolverSpanTags(ctx, span)
tracing.LogObjectAsJson(span, "request.contractInput", input)
contractId, err := r.Services.ContractService.Create(ctx, &service.ContractCreateData{
Input: input,
ExternalReference: mapper.MapExternalSystemReferenceInputToRelationship(input.ExternalReference),
Source: neo4jentity.DataSourceOpenline,
AppSource: utils.IfNotNilStringWithDefault(input.AppSource, constants.AppSourceCustomerOsApi),
})
if err != nil {
tracing.TraceErr(span, err)
graphql.AddErrorf(ctx, "Failed to create contract")
return &model.Contract{ID: contractId}, err
}
createdContractEntity, err := r.Services.ContractService.GetById(ctx, contractId)
if err != nil {
tracing.TraceErr(span, err)
graphql.AddErrorf(ctx, "Contract details not yet available. Contract id: %s", contractId)
return &model.Contract{ID: contractId}, nil
}
span.LogFields(log.String("response.contractID", contractId))
return mapper.MapEntityToContract(createdContractEntity), nil
}
// ContractUpdate is the resolver for the contract_Update field.
func (r *mutationResolver) ContractUpdate(ctx context.Context, input model.ContractUpdateInput) (*model.Contract, error) {
ctx, span := tracing.StartGraphQLTracerSpan(ctx, "MutationResolver.ContractUpdate", graphql.GetOperationContext(ctx))
defer span.Finish()
tracing.SetDefaultResolverSpanTags(ctx, span)
span.LogFields(log.String("request.contractId", input.ContractID))
err := r.Services.ContractService.Update(ctx, input)
if err != nil {
tracing.TraceErr(span, err)
graphql.AddErrorf(ctx, "Failed to update contract %s", input.ContractID)
return &model.Contract{ID: input.ContractID}, nil
}
contractEntity, err := r.Services.ContractService.GetById(ctx, input.ContractID)
if err != nil {
tracing.TraceErr(span, err)
graphql.AddErrorf(ctx, "Failed fetching contract details. Contract id: %s", input.ContractID)
return &model.Contract{ID: input.ContractID}, nil
}
return mapper.MapEntityToContract(contractEntity), nil
}
// ContractDelete is the resolver for the contract_Delete field.
func (r *mutationResolver) ContractDelete(ctx context.Context, id string) (*model.DeleteResponse, error) {
ctx, span := tracing.StartGraphQLTracerSpan(ctx, "MutationResolver.ContractDelete", graphql.GetOperationContext(ctx))
defer span.Finish()
tracing.SetDefaultResolverSpanTags(ctx, span)
span.LogFields(log.String("request.id", id))
deletionCompleted, err := r.Services.ContractService.SoftDeleteContract(ctx, id)
if err != nil {
tracing.TraceErr(span, err)
graphql.AddErrorf(ctx, "failed to delete contract %s", id)
return &model.DeleteResponse{Accepted: false, Completed: false}, nil
}
return &model.DeleteResponse{Accepted: true, Completed: deletionCompleted}, nil
}
// ContractRenew is the resolver for the contract_Renew field.
func (r *mutationResolver) ContractRenew(ctx context.Context, input model.ContractRenewalInput) (*model.Contract, error) {
ctx, span := tracing.StartGraphQLTracerSpan(ctx, "MutationResolver.ContractRenew", graphql.GetOperationContext(ctx))
defer span.Finish()
tracing.SetDefaultResolverSpanTags(ctx, span)
tracing.LogObjectAsJson(span, "request.contractRenewalInput", input)
err := r.Services.ContractService.RenewContract(ctx, input.ContractID, input.RenewalDate)
if err != nil {
tracing.TraceErr(span, err)
graphql.AddErrorf(ctx, "Failed to renew contract %s", input.ContractID)
return &model.Contract{Metadata: &model.Metadata{ID: input.ContractID}}, nil
}
contractEntity, err := r.Services.ContractService.GetById(ctx, input.ContractID)
if err != nil {
tracing.TraceErr(span, err)
graphql.AddErrorf(ctx, "Failed fetching contract details. Contract id: %s", input.ContractID)
return &model.Contract{Metadata: &model.Metadata{ID: input.ContractID}}, nil
}
return mapper.MapEntityToContract(contractEntity), nil
}
// ContractAddAttachment is the resolver for the contract_AddAttachment field.
func (r *mutationResolver) ContractAddAttachment(ctx context.Context, contractID string, attachmentID string) (*model.Contract, error) {
ctx, span := tracing.StartGraphQLTracerSpan(ctx, "MutationResolver.ContractAddAttachment", graphql.GetOperationContext(ctx))
defer span.Finish()
tracing.SetDefaultResolverSpanTags(ctx, span)
span.LogFields(log.String("contractID", contractID))
span.LogFields(log.String("attachmentID", attachmentID))
_, err := r.Services.ContractService.GetById(ctx, contractID)
if err != nil {
tracing.TraceErr(span, err)
graphql.AddErrorf(ctx, "Failed fetching contract details. Contract id: %s", contractID)
return &model.Contract{ID: contractID}, nil
}
_, err = r.Services.AttachmentService.GetAttachmentById(ctx, attachmentID)
if err != nil {
tracing.TraceErr(span, err)
graphql.AddErrorf(ctx, "Failed fetching attachment details. Attachment id: %s", contractID)
return &model.Contract{ID: contractID}, nil
}
_, err = r.Services.AttachmentService.LinkNodeWithAttachment(ctx, repository.LINKED_WITH_CONTRACT, nil, attachmentID, contractID)
if err != nil {
tracing.TraceErr(span, err)
graphql.AddErrorf(ctx, "Failed to link attachment %s with contract %s", attachmentID, contractID)
return &model.Contract{ID: contractID}, nil
}
return mapper.MapEntityToContract(&neo4jentity.ContractEntity{Id: contractID}), nil
}
// ContractRemoveAttachment is the resolver for the contract_RemoveAttachment field.
func (r *mutationResolver) ContractRemoveAttachment(ctx context.Context, contractID string, attachmentID string) (*model.Contract, error) {
ctx, span := tracing.StartGraphQLTracerSpan(ctx, "MutationResolver.ContractRemoveAttachment", graphql.GetOperationContext(ctx))
defer span.Finish()
tracing.SetDefaultResolverSpanTags(ctx, span)
span.LogFields(log.String("contractID", contractID))
span.LogFields(log.String("attachmentID", attachmentID))
_, err := r.Services.ContractService.GetById(ctx, contractID)
if err != nil {
tracing.TraceErr(span, err)
graphql.AddErrorf(ctx, "Failed fetching contract details. Contract id: %s", contractID)
return &model.Contract{ID: contractID}, nil
}
_, err = r.Services.AttachmentService.GetAttachmentById(ctx, attachmentID)
if err != nil {
tracing.TraceErr(span, err)
graphql.AddErrorf(ctx, "Failed fetching attachment details. Attachment id: %s", contractID)
return &model.Contract{ID: contractID}, nil
}
_, err = r.Services.AttachmentService.UnlinkNodeWithAttachment(ctx, repository.LINKED_WITH_CONTRACT, nil, attachmentID, contractID)
if err != nil {
tracing.TraceErr(span, err)
graphql.AddErrorf(ctx, "Failed to remove attachment %s from contract %s", attachmentID, contractID)
return &model.Contract{ID: contractID}, nil
}
return mapper.MapEntityToContract(&neo4jentity.ContractEntity{Id: contractID}), nil
}
// Contract is the resolver for the contract field.
func (r *queryResolver) Contract(ctx context.Context, id string) (*model.Contract, error) {
ctx, span := tracing.StartGraphQLTracerSpan(ctx, "QueryResolver.Contract", graphql.GetOperationContext(ctx))
defer span.Finish()
tracing.SetDefaultResolverSpanTags(ctx, span)
span.LogFields(log.String("request.contractID", id))
if id == "" {
tracing.TraceErr(span, errors.New("Missing contract input id"))
graphql.AddErrorf(ctx, "Missing contract input id")
return nil, nil
}
contractEntityPtr, err := r.Services.ContractService.GetById(ctx, id)
if err != nil {
tracing.TraceErr(span, err)
graphql.AddErrorf(ctx, "Failed to get contract by id %s", id)
return nil, err
}
return mapper.MapEntityToContract(contractEntityPtr), nil
}
// Contract returns generated.ContractResolver implementation.
func (r *Resolver) Contract() generated.ContractResolver { return &contractResolver{r} }
type contractResolver struct{ *Resolver }