-
Notifications
You must be signed in to change notification settings - Fork 12
/
log_entry.resolvers.go
358 lines (327 loc) · 14.1 KB
/
log_entry.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
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"
"fmt"
"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/common"
commonTracing "github.com/openline-ai/openline-customer-os/packages/server/customer-os-common-module/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"
grpccommon "github.com/openline-ai/openline-customer-os/packages/server/events-processing-proto/gen/proto/go/api/grpc/v1/common"
logentrypb "github.com/openline-ai/openline-customer-os/packages/server/events-processing-proto/gen/proto/go/api/grpc/v1/log_entry"
opentracing "github.com/opentracing/opentracing-go"
"github.com/opentracing/opentracing-go/log"
"google.golang.org/protobuf/types/known/timestamppb"
)
// CreatedBy is the resolver for the createdBy field.
func (r *logEntryResolver) CreatedBy(ctx context.Context, obj *model.LogEntry) (*model.User, error) {
ctx = tracing.EnrichCtxWithSpanCtxForGraphQL(ctx, graphql.GetOperationContext(ctx))
userEntityNillable, err := dataloader.For(ctx).GetUserAuthorForLogEntry(ctx, obj.ID)
if err != nil {
tracing.TraceErr(opentracing.SpanFromContext(ctx), err)
r.log.Errorf("Error fetching user author for log entry %s: %s", obj.ID, err.Error())
graphql.AddErrorf(ctx, "Error fetching user author for log entry %s", obj.ID)
return nil, nil
}
return mapper.MapEntityToUser(userEntityNillable), nil
}
// Tags is the resolver for the tags field.
func (r *logEntryResolver) Tags(ctx context.Context, obj *model.LogEntry) ([]*model.Tag, error) {
ctx = tracing.EnrichCtxWithSpanCtxForGraphQL(ctx, graphql.GetOperationContext(ctx))
tagEntities, err := dataloader.For(ctx).GetTagsForLogEntry(ctx, obj.ID)
if err != nil {
tracing.TraceErr(opentracing.SpanFromContext(ctx), err)
r.log.Errorf("Failed to get tags for log entry %s: %s", obj.ID, err.Error())
graphql.AddErrorf(ctx, "Failed to get tags for log entry %s", obj.ID)
return nil, nil
}
return mapper.MapEntitiesToTags(tagEntities), nil
}
// ExternalLinks is the resolver for the externalLinks field.
func (r *logEntryResolver) ExternalLinks(ctx context.Context, obj *model.LogEntry) ([]*model.ExternalSystem, error) {
ctx = tracing.EnrichCtxWithSpanCtxForGraphQL(ctx, graphql.GetOperationContext(ctx))
entities, err := dataloader.For(ctx).GetExternalSystemsForLogEntry(ctx, obj.ID)
if err != nil {
tracing.TraceErr(opentracing.SpanFromContext(ctx), err)
r.log.Errorf("Failed to get external systems for log entry %s: %s", obj.ID, err.Error())
graphql.AddErrorf(ctx, "Failed to get external systems for log entry %s", obj.ID)
return nil, err
}
return mapper.MapEntitiesToExternalSystems(entities), nil
}
// LogEntryCreateForOrganization is the resolver for the logEntry_CreateForOrganization field.
func (r *mutationResolver) LogEntryCreateForOrganization(ctx context.Context, organizationID string, input model.LogEntryInput) (string, error) {
ctx, span := tracing.StartGraphQLTracerSpan(ctx, "MutationResolver.LogEntryCreateForOrganization", graphql.GetOperationContext(ctx))
defer span.Finish()
tracing.SetDefaultResolverSpanTags(ctx, span)
span.LogFields(log.String("organizationID", organizationID), log.Object("input", input))
organizationEntity, err := r.Services.OrganizationService.GetById(ctx, organizationID)
if err != nil || organizationEntity == nil {
if err == nil {
err = fmt.Errorf("organization not found")
}
tracing.TraceErr(span, err)
graphql.AddErrorf(ctx, "Organization not found")
return "", nil
}
ctx = commonTracing.InjectSpanContextIntoGrpcMetadata(ctx, span)
response, err := utils.CallEventsPlatformGRPCWithRetry[*logentrypb.LogEntryIdGrpcResponse](func() (*logentrypb.LogEntryIdGrpcResponse, error) {
return r.Clients.LogEntryClient.UpsertLogEntry(ctx, &logentrypb.UpsertLogEntryGrpcRequest{
Tenant: common.GetTenantFromContext(ctx),
UserId: common.GetUserIdFromContext(ctx),
Content: utils.IfNotNilString(input.Content),
ContentType: utils.IfNotNilString(input.ContentType),
StartedAt: timestamppb.New(utils.IfNotNilTimeWithDefault(input.StartedAt, utils.Now())),
SourceFields: &grpccommon.SourceFields{
AppSource: constants.AppSourceCustomerOsApi,
Source: string(neo4jentity.DataSourceOpenline),
},
LoggedOrganizationId: utils.StringPtr(organizationID),
AuthorUserId: utils.StringPtr(common.GetUserIdFromContext(ctx)),
})
})
if err != nil {
tracing.TraceErr(span, err)
graphql.AddErrorf(ctx, "Error creating log entry")
return "", nil
}
for _, tag := range input.Tags {
tagId := GetTagId(ctx, r.Services, tag.ID, tag.Name)
if tagId == "" {
tagEntity, _ := CreateTag(ctx, r.Services, tag.Name)
if tagEntity != nil {
tagId = tagEntity.Id
}
}
if tagId != "" {
_, err = utils.CallEventsPlatformGRPCWithRetry[*logentrypb.LogEntryIdGrpcResponse](func() (*logentrypb.LogEntryIdGrpcResponse, error) {
return r.Clients.LogEntryClient.AddTag(ctx, &logentrypb.AddTagGrpcRequest{
Tenant: common.GetTenantFromContext(ctx),
UserId: common.GetUserIdFromContext(ctx),
Id: response.Id,
TagId: tagId,
})
})
if err != nil {
tracing.TraceErr(span, err)
graphql.AddErrorf(ctx, "Error adding tag to log entry")
return "", nil
}
}
}
return response.Id, nil
}
// LogEntryUpdate is the resolver for the logEntry_Update field.
func (r *mutationResolver) LogEntryUpdate(ctx context.Context, id string, input model.LogEntryUpdateInput) (string, error) {
ctx, span := tracing.StartGraphQLTracerSpan(ctx, "MutationResolver.LogEntryUpdate", graphql.GetOperationContext(ctx))
defer span.Finish()
tracing.SetDefaultResolverSpanTags(ctx, span)
span.LogFields(log.String("logEntryId", id), log.Object("input", input))
logEntryEntity, err := r.Services.LogEntryService.GetById(ctx, id)
if err != nil || logEntryEntity == nil {
if err == nil {
err = fmt.Errorf("Log entry %s not found", id)
}
tracing.TraceErr(span, err)
graphql.AddErrorf(ctx, "Log entry %s not found", id)
return "", nil
}
grpcRequestMessage := logentrypb.UpsertLogEntryGrpcRequest{
Id: id,
Tenant: common.GetTenantFromContext(ctx),
UserId: common.GetUserIdFromContext(ctx),
Content: utils.IfNotNilString(input.Content),
ContentType: utils.IfNotNilString(input.ContentType),
SourceFields: &grpccommon.SourceFields{
SourceOfTruth: string(neo4jentity.DataSourceOpenline),
},
}
if input.StartedAt != nil {
grpcRequestMessage.StartedAt = timestamppb.New(*input.StartedAt)
}
ctx = commonTracing.InjectSpanContextIntoGrpcMetadata(ctx, span)
response, err := utils.CallEventsPlatformGRPCWithRetry[*logentrypb.LogEntryIdGrpcResponse](func() (*logentrypb.LogEntryIdGrpcResponse, error) {
return r.Clients.LogEntryClient.UpsertLogEntry(ctx, &grpcRequestMessage)
})
if err != nil {
tracing.TraceErr(span, err)
graphql.AddErrorf(ctx, "Error updating log entry")
return "", nil
}
return response.Id, nil
}
// LogEntryAddTag is the resolver for the logEntry_AddTag field.
func (r *mutationResolver) LogEntryAddTag(ctx context.Context, id string, input model.TagIDOrNameInput) (string, error) {
ctx, span := tracing.StartGraphQLTracerSpan(ctx, "MutationResolver.LogEntryAddTag", graphql.GetOperationContext(ctx))
defer span.Finish()
tracing.SetDefaultResolverSpanTags(ctx, span)
span.LogFields(log.String("logEntryId", id), log.Object("input", input))
logEntryEntity, err := r.Services.LogEntryService.GetById(ctx, id)
if err != nil || logEntryEntity == nil {
if err == nil {
err = fmt.Errorf("Log entry %s not found", id)
}
tracing.TraceErr(span, err)
graphql.AddErrorf(ctx, "Log entry %s not found", id)
return "", nil
}
tagId := GetTagId(ctx, r.Services, input.ID, input.Name)
if tagId == "" {
tagEntity, _ := CreateTag(ctx, r.Services, input.Name)
if tagEntity != nil {
tagId = tagEntity.Id
}
}
if tagId != "" {
ctx = commonTracing.InjectSpanContextIntoGrpcMetadata(ctx, span)
_, err = utils.CallEventsPlatformGRPCWithRetry[*logentrypb.LogEntryIdGrpcResponse](func() (*logentrypb.LogEntryIdGrpcResponse, error) {
return r.Clients.LogEntryClient.AddTag(ctx, &logentrypb.AddTagGrpcRequest{
Tenant: common.GetTenantFromContext(ctx),
UserId: common.GetUserIdFromContext(ctx),
Id: id,
TagId: tagId,
})
})
if err != nil {
tracing.TraceErr(span, err)
graphql.AddErrorf(ctx, "Error adding tag to log entry")
return id, nil
}
}
return id, nil
}
// LogEntryRemoveTag is the resolver for the logEntry_RemoveTag field.
func (r *mutationResolver) LogEntryRemoveTag(ctx context.Context, id string, input model.TagIDOrNameInput) (string, error) {
ctx, span := tracing.StartGraphQLTracerSpan(ctx, "MutationResolver.LogEntryAddTag", graphql.GetOperationContext(ctx))
defer span.Finish()
tracing.SetDefaultResolverSpanTags(ctx, span)
span.LogFields(log.String("logEntryId", id), log.Object("input", input))
logEntryEntity, err := r.Services.LogEntryService.GetById(ctx, id)
if err != nil || logEntryEntity == nil {
if err == nil {
err = fmt.Errorf("Log entry %s not found", id)
}
tracing.TraceErr(span, err)
graphql.AddErrorf(ctx, "Log entry %s not found", id)
return "", nil
}
tagId := GetTagId(ctx, r.Services, input.ID, input.Name)
if tagId != "" {
ctx = commonTracing.InjectSpanContextIntoGrpcMetadata(ctx, span)
_, err = utils.CallEventsPlatformGRPCWithRetry[*logentrypb.LogEntryIdGrpcResponse](func() (*logentrypb.LogEntryIdGrpcResponse, error) {
return r.Clients.LogEntryClient.RemoveTag(ctx, &logentrypb.RemoveTagGrpcRequest{
Tenant: common.GetTenantFromContext(ctx),
UserId: common.GetUserIdFromContext(ctx),
Id: id,
TagId: tagId,
})
})
if err != nil {
tracing.TraceErr(span, err)
graphql.AddErrorf(ctx, "Error removing tag from log entry")
return id, nil
}
}
return id, nil
}
// LogEntryResetTags is the resolver for the logEntry_ResetTags field.
func (r *mutationResolver) LogEntryResetTags(ctx context.Context, id string, input []*model.TagIDOrNameInput) (string, error) {
ctx, span := tracing.StartGraphQLTracerSpan(ctx, "MutationResolver.LogEntryResetTags", graphql.GetOperationContext(ctx))
defer span.Finish()
tracing.SetDefaultResolverSpanTags(ctx, span)
span.LogFields(log.String("logEntryId", id), log.Object("input", input))
logEntryEntity, err := r.Services.LogEntryService.GetById(ctx, id)
if err != nil || logEntryEntity == nil {
if err == nil {
err = fmt.Errorf("Log entry %s not found", id)
}
tracing.TraceErr(span, err)
graphql.AddErrorf(ctx, "Log entry %s not found", id)
return "", nil
}
tags, err := r.Services.TagService.GetTagsForLogEntries(ctx, []string{logEntryEntity.Id})
if err != nil {
tracing.TraceErr(span, err)
graphql.AddErrorf(ctx, "Error getting tags for log entry")
return id, nil
}
currentTagIds := []string{}
for _, tag := range *tags {
currentTagIds = append(currentTagIds, tag.Id)
}
newTagIds := []string{}
for _, inputTag := range input {
tagId := GetTagId(ctx, r.Services, inputTag.ID, inputTag.Name)
if tagId == "" {
tagEntity, _ := CreateTag(ctx, r.Services, inputTag.Name)
if tagEntity != nil {
tagId = tagEntity.Id
}
}
if tagId != "" {
newTagIds = append(newTagIds, tagId)
}
}
ctx = commonTracing.InjectSpanContextIntoGrpcMetadata(ctx, span)
for _, currentTagId := range currentTagIds {
if !utils.Contains(newTagIds, currentTagId) {
_, err = utils.CallEventsPlatformGRPCWithRetry[*logentrypb.LogEntryIdGrpcResponse](func() (*logentrypb.LogEntryIdGrpcResponse, error) {
return r.Clients.LogEntryClient.RemoveTag(ctx, &logentrypb.RemoveTagGrpcRequest{
Tenant: common.GetTenantFromContext(ctx),
UserId: common.GetUserIdFromContext(ctx),
Id: id,
TagId: currentTagId,
})
})
if err != nil {
tracing.TraceErr(span, err)
graphql.AddErrorf(ctx, "Error removing tag from log entry")
return id, nil
}
}
}
for _, newTagId := range newTagIds {
if !utils.Contains(currentTagIds, newTagId) {
_, err = utils.CallEventsPlatformGRPCWithRetry[*logentrypb.LogEntryIdGrpcResponse](func() (*logentrypb.LogEntryIdGrpcResponse, error) {
return r.Clients.LogEntryClient.AddTag(ctx, &logentrypb.AddTagGrpcRequest{
Tenant: common.GetTenantFromContext(ctx),
UserId: common.GetUserIdFromContext(ctx),
Id: id,
TagId: newTagId,
})
})
if err != nil {
tracing.TraceErr(span, err)
graphql.AddErrorf(ctx, "Error adding tag to log entry")
return id, nil
}
}
}
return id, nil
}
// LogEntry is the resolver for the logEntry field.
func (r *queryResolver) LogEntry(ctx context.Context, id string) (*model.LogEntry, error) {
ctx, span := tracing.StartGraphQLTracerSpan(ctx, "QueryResolver.LogEntry", graphql.GetOperationContext(ctx))
defer span.Finish()
tracing.SetDefaultResolverSpanTags(ctx, span)
span.LogFields(log.String("request.logEntryId", id))
logEntryEntity, err := r.Services.LogEntryService.GetById(ctx, id)
if err != nil {
tracing.TraceErr(span, err)
graphql.AddErrorf(ctx, "Failed getting log entry with id %s", id)
return nil, nil
}
return mapper.MapEntityToLogEntry(logEntryEntity), nil
}
// LogEntry returns generated.LogEntryResolver implementation.
func (r *Resolver) LogEntry() generated.LogEntryResolver { return &logEntryResolver{r} }
type logEntryResolver struct{ *Resolver }