-
Notifications
You must be signed in to change notification settings - Fork 12
/
issue_write_repository.go
358 lines (323 loc) · 14.9 KB
/
issue_write_repository.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 repository
import (
"context"
"fmt"
"github.com/neo4j/neo4j-go-driver/v5/neo4j"
"github.com/openline-ai/openline-customer-os/packages/server/customer-os-common-module/utils"
"github.com/openline-ai/openline-customer-os/packages/server/customer-os-neo4j-repository/constants"
"github.com/openline-ai/openline-customer-os/packages/server/customer-os-neo4j-repository/model"
"github.com/openline-ai/openline-customer-os/packages/server/customer-os-neo4j-repository/tracing"
"github.com/opentracing/opentracing-go"
"github.com/opentracing/opentracing-go/log"
"time"
)
type IssueCreateFields struct {
CreatedAt time.Time `json:"createdAt"`
UpdatedAt time.Time `json:"updatedAt"`
SourceFields model.Source `json:"sourceFields"`
GroupId string `json:"groupId"`
Subject string `json:"subject"`
Description string `json:"description"`
Status string `json:"status"`
Priority string `json:"priority"`
ReportedByOrganizationId string `json:"reportedByOrganizationId"`
SubmittedByOrganizationId string `json:"submittedByOrganizationId"`
SubmittedByUserId string `json:"submittedByUserId"`
}
type IssueUpdateFields struct {
GroupId string `json:"groupId"`
Subject string `json:"subject"`
Description string `json:"description"`
Status string `json:"status"`
Priority string `json:"priority"`
UpdatedAt time.Time `json:"updatedAt"`
Source string `json:"source"`
}
type IssueWriteRepository interface {
Create(ctx context.Context, tenant, issueId string, data IssueCreateFields) error
Update(ctx context.Context, tenant, issueId string, data IssueUpdateFields) error
AddUserAssignee(ctx context.Context, tenant, issueId, userId string, at time.Time) error
RemoveUserAssignee(ctx context.Context, tenant, issueId, userId string, at time.Time) error
AddUserFollower(ctx context.Context, tenant, issueId, userId string, at time.Time) error
RemoveUserFollower(ctx context.Context, tenant, issueId, userId string, at time.Time) error
ReportedByOrganizationWithGroupId(ctx context.Context, tenant, organizationId, groupId string) error
RemoveReportedByOrganizationWithGroupId(ctx context.Context, tenant, organizationId, groupId string) error
LinkUnthreadIssuesToOrganizationByGroupId(ctx context.Context) error
}
type issueWriteRepository struct {
driver *neo4j.DriverWithContext
database string
}
func NewIssueWriteRepository(driver *neo4j.DriverWithContext, database string) IssueWriteRepository {
return &issueWriteRepository{
driver: driver,
database: database,
}
}
func (r *issueWriteRepository) Create(ctx context.Context, tenant, issueId string, data IssueCreateFields) error {
span, ctx := opentracing.StartSpanFromContext(ctx, "IssueWriteRepository.Create")
defer span.Finish()
tracing.SetNeo4jRepositorySpanTags(span, tenant)
span.SetTag(tracing.SpanTagEntityId, issueId)
tracing.LogObjectAsJson(span, "data", data)
cypher := fmt.Sprintf(`MATCH (t:Tenant {name:$tenant})
MERGE (t)<-[:ISSUE_BELONGS_TO_TENANT]-(i:Issue {id:$issueId})
ON CREATE SET
i:Issue_%s,
i:TimelineEvent,
i:TimelineEvent_%s,
i.createdAt=$createdAt,
i.updatedAt=$updatedAt,
i.source=$source,
i.sourceOfTruth=$sourceOfTruth,
i.appSource=$appSource,
i.groupId=$groupId,
i.subject=$subject,
i.description=$description,
i.status=$status,
i.priority=$priority
ON MATCH SET
i.groupId = CASE WHEN i.sourceOfTruth=$sourceOfTruth OR $overwrite=true OR i.groupId is null OR i.groupId = '' THEN $groupId ELSE i.groupId END,
i.subject = CASE WHEN i.sourceOfTruth=$sourceOfTruth OR $overwrite=true OR i.subject is null OR i.subject = '' THEN $subject ELSE i.subject END,
i.description = CASE WHEN i.sourceOfTruth=$sourceOfTruth OR $overwrite=true OR i.description is null OR i.description = '' THEN $description ELSE i.description END,
i.status = CASE WHEN i.sourceOfTruth=$sourceOfTruth OR $overwrite=true OR i.status is null OR i.status = '' THEN $status ELSE i.status END,
i.priority = CASE WHEN i.sourceOfTruth=$sourceOfTruth OR $overwrite=true OR i.priority is null OR i.priority = '' THEN $priority ELSE i.priority END,
i.updatedAt = $updatedAt,
i.sourceOfTruth = case WHEN $overwrite=true THEN $sourceOfTruth ELSE i.sourceOfTruth END,
i.syncedWithEventStore = true
WITH i, t
OPTIONAL MATCH (t)<-[:ORGANIZATION_BELONGS_TO_TENANT]-(o:Organization {id:$reportedByOrganizationId})
WHERE $reportedByOrganizationId <> ""
FOREACH (ignore IN CASE WHEN o IS NOT NULL THEN [1] ELSE [] END |
MERGE (i)-[:REPORTED_BY]->(o))
WITH i, t
OPTIONAL MATCH (t)<-[:ORGANIZATION_BELONGS_TO_TENANT]-(o:Organization {id:$submittedByOrganizationId})
WHERE $submittedByOrganizationId <> ""
FOREACH (ignore IN CASE WHEN o IS NOT NULL THEN [1] ELSE [] END |
MERGE (i)-[:SUBMITTED_BY]->(o))
WITH i, t
OPTIONAL MATCH (t)<-[:USER_BELONGS_TO_TENANT]-(u:User {id:$submittedByUserId})
WHERE $submittedByUserId <> ""
FOREACH (ignore IN CASE WHEN u IS NOT NULL THEN [1] ELSE [] END |
MERGE (i)-[:SUBMITTED_BY]->(u))
`, tenant, tenant)
params := map[string]any{
"tenant": tenant,
"issueId": issueId,
"createdAt": data.CreatedAt,
"updatedAt": data.UpdatedAt,
"source": data.SourceFields.Source,
"sourceOfTruth": data.SourceFields.SourceOfTruth,
"appSource": data.SourceFields.AppSource,
"groupId": data.GroupId,
"subject": data.Subject,
"description": data.Description,
"status": data.Status,
"priority": data.Priority,
"reportedByOrganizationId": data.ReportedByOrganizationId,
"submittedByOrganizationId": data.SubmittedByOrganizationId,
"submittedByUserId": data.SubmittedByUserId,
"overwrite": data.SourceFields.Source == constants.SourceOpenline,
}
span.LogFields(log.String("cypher", cypher))
tracing.LogObjectAsJson(span, "params", params)
err := utils.ExecuteWriteQuery(ctx, *r.driver, cypher, params)
if err != nil {
tracing.TraceErr(span, err)
}
return err
}
func (r *issueWriteRepository) Update(ctx context.Context, tenant, issueId string, data IssueUpdateFields) error {
span, ctx := opentracing.StartSpanFromContext(ctx, "IssueWriteRepository.Update")
defer span.Finish()
tracing.SetNeo4jRepositorySpanTags(span, tenant)
span.SetTag(tracing.SpanTagEntityId, issueId)
tracing.LogObjectAsJson(span, "data", data)
cypher := `MATCH (t:Tenant {name:$tenant})<-[:ISSUE_BELONGS_TO_TENANT]-(i:Issue {id:$issueId})
SET
i.groupId = CASE WHEN i.sourceOfTruth=$sourceOfTruth OR $overwrite=true OR i.groupId is null OR i.groupId = '' THEN $groupId ELSE i.groupId END,
i.subject = CASE WHEN i.sourceOfTruth=$sourceOfTruth OR $overwrite=true OR i.subject is null OR i.subject = '' THEN $subject ELSE i.subject END,
i.description = CASE WHEN i.sourceOfTruth=$sourceOfTruth OR $overwrite=true OR i.description is null OR i.description = '' THEN $description ELSE i.description END,
i.status = CASE WHEN i.sourceOfTruth=$sourceOfTruth OR $overwrite=true OR i.status is null OR i.status = '' THEN $status ELSE i.status END,
i.priority = CASE WHEN i.sourceOfTruth=$sourceOfTruth OR $overwrite=true OR i.priority is null OR i.priority = '' THEN $priority ELSE i.priority END,
i.updatedAt = $updatedAt,
i.sourceOfTruth = case WHEN $overwrite=true THEN $sourceOfTruth ELSE i.sourceOfTruth END,
i.syncedWithEventStore = true`
params := map[string]any{
"tenant": tenant,
"issueId": issueId,
"updatedAt": data.UpdatedAt,
"groupId": data.GroupId,
"subject": data.Subject,
"description": data.Description,
"status": data.Status,
"priority": data.Priority,
"sourceOfTruth": data.Source,
"overwrite": data.Source == constants.SourceOpenline,
}
span.LogFields(log.String("cypher", cypher))
tracing.LogObjectAsJson(span, "params", params)
err := utils.ExecuteWriteQuery(ctx, *r.driver, cypher, params)
if err != nil {
tracing.TraceErr(span, err)
}
return err
}
func (r *issueWriteRepository) AddUserAssignee(ctx context.Context, tenant, issueId, userId string, at time.Time) error {
span, ctx := opentracing.StartSpanFromContext(ctx, "IssueWriteRepository.AddUserAssignee")
defer span.Finish()
tracing.SetNeo4jRepositorySpanTags(span, tenant)
span.SetTag(tracing.SpanTagEntityId, issueId)
span.LogFields(log.String("userId", userId), log.Object("at", at))
cypher := `MATCH (t:Tenant {name:$tenant})<-[:ISSUE_BELONGS_TO_TENANT]-(i:Issue {id:$issueId}),
(t)<-[:USER_BELONGS_TO_TENANT]-(u:User {id:$userId})
MERGE (i)-[:ASSIGNED_TO]->(u)
ON CREATE SET i.updatedAt = $updatedAt`
params := map[string]any{
"tenant": tenant,
"issueId": issueId,
"updatedAt": at,
"userId": userId,
}
span.LogFields(log.String("cypher", cypher))
tracing.LogObjectAsJson(span, "params", params)
err := utils.ExecuteWriteQuery(ctx, *r.driver, cypher, params)
if err != nil {
tracing.TraceErr(span, err)
}
return err
}
func (r *issueWriteRepository) AddUserFollower(ctx context.Context, tenant, issueId, userId string, at time.Time) error {
span, ctx := opentracing.StartSpanFromContext(ctx, "IssueWriteRepository.AddUserFollower")
defer span.Finish()
tracing.SetNeo4jRepositorySpanTags(span, tenant)
span.SetTag(tracing.SpanTagEntityId, issueId)
span.LogFields(log.String("userId", userId), log.Object("at", at))
cypher := `MATCH (t:Tenant {name:$tenant})<-[:ISSUE_BELONGS_TO_TENANT]-(i:Issue {id:$issueId}),
(t)<-[:USER_BELONGS_TO_TENANT]-(u:User {id:$userId})
MERGE (i)-[:FOLLOWED_BY]->(u)
ON CREATE SET i.updatedAt = $updatedAt`
params := map[string]any{
"tenant": tenant,
"issueId": issueId,
"updatedAt": at,
"userId": userId,
}
span.LogFields(log.String("cypher", cypher))
tracing.LogObjectAsJson(span, "params", params)
err := utils.ExecuteWriteQuery(ctx, *r.driver, cypher, params)
if err != nil {
tracing.TraceErr(span, err)
}
return err
}
func (r *issueWriteRepository) RemoveUserAssignee(ctx context.Context, tenant, issueId, userId string, at time.Time) error {
span, ctx := opentracing.StartSpanFromContext(ctx, "IssueWriteRepository.RemoveUserAssignee")
defer span.Finish()
tracing.SetNeo4jRepositorySpanTags(span, tenant)
span.SetTag(tracing.SpanTagEntityId, issueId)
span.LogFields(log.String("userId", userId), log.Object("at", at))
cypher := `MATCH (t:Tenant {name:$tenant})<-[:ISSUE_BELONGS_TO_TENANT]-(i:Issue {id:$issueId}),
(t)<-[:USER_BELONGS_TO_TENANT]-(u:User {id:$userId}),
(i)-[r:ASSIGNED_TO]->(u)
SET i.updatedAt = $updatedAt
DELETE r`
params := map[string]any{
"tenant": tenant,
"issueId": issueId,
"updatedAt": at,
"userId": userId,
}
span.LogFields(log.String("cypher", cypher))
tracing.LogObjectAsJson(span, "params", params)
err := utils.ExecuteWriteQuery(ctx, *r.driver, cypher, params)
if err != nil {
tracing.TraceErr(span, err)
}
return err
}
func (r *issueWriteRepository) RemoveUserFollower(ctx context.Context, tenant, issueId, userId string, at time.Time) error {
span, ctx := opentracing.StartSpanFromContext(ctx, "IssueWriteRepository.RemoveUserFollower")
defer span.Finish()
tracing.SetNeo4jRepositorySpanTags(span, tenant)
span.SetTag(tracing.SpanTagEntityId, issueId)
span.LogFields(log.String("userId", userId), log.Object("at", at))
cypher := `MATCH (t:Tenant {name:$tenant})<-[:ISSUE_BELONGS_TO_TENANT]-(i:Issue {id:$issueId}),
(t)<-[:USER_BELONGS_TO_TENANT]-(u:User {id:$userId}),
(i)-[r:FOLLOWED_BY]->(u)
SET i.updatedAt = $updatedAt
DELETE r`
params := map[string]any{
"tenant": tenant,
"issueId": issueId,
"updatedAt": at,
"userId": userId,
}
span.LogFields(log.String("cypher", cypher))
tracing.LogObjectAsJson(span, "params", params)
err := utils.ExecuteWriteQuery(ctx, *r.driver, cypher, params)
if err != nil {
tracing.TraceErr(span, err)
}
return err
}
func (r *issueWriteRepository) ReportedByOrganizationWithGroupId(ctx context.Context, tenant, organizationId, groupId string) error {
span, ctx := opentracing.StartSpanFromContext(ctx, "IssueWriteRepository.ReportedByOrganizationWithGroupId")
defer span.Finish()
tracing.SetNeo4jRepositorySpanTags(span, tenant)
span.LogFields(log.String("organizationId", organizationId))
span.LogFields(log.String("groupId", groupId))
cypher := `MATCH (t:Tenant {name:$tenant})<-[:ORGANIZATION_BELONGS_TO_TENANT]-(o:Organization {id:$organizationId})
OPTIONAL MATCH (t)<-[:ISSUE_BELONGS_TO_TENANT]-(i:Issue {groupId:$groupId})
MERGE (i)-[:REPORTED_BY]->(o)`
params := map[string]any{
"tenant": tenant,
"organizationId": organizationId,
"groupId": groupId,
}
span.LogFields(log.String("cypher", cypher))
tracing.LogObjectAsJson(span, "params", params)
err := utils.ExecuteWriteQuery(ctx, *r.driver, cypher, params)
if err != nil {
tracing.TraceErr(span, err)
}
return err
}
func (r *issueWriteRepository) RemoveReportedByOrganizationWithGroupId(ctx context.Context, tenant, organizationId, groupId string) error {
span, ctx := opentracing.StartSpanFromContext(ctx, "IssueWriteRepository.RemoveReportedByOrganizationWithGroupId")
defer span.Finish()
tracing.SetNeo4jRepositorySpanTags(span, tenant)
span.LogFields(log.String("organizationId", organizationId))
span.LogFields(log.String("groupId", groupId))
cypher := `MATCH (t:Tenant {name:$tenant})<-[:ORGANIZATION_BELONGS_TO_TENANT]-(o:Organization {id:$organizationId})<-[r:REPORTED_BY]-(i:Issue{groupId:$groupId})
DELETE r`
params := map[string]any{
"tenant": tenant,
"organizationId": organizationId,
"groupId": groupId,
}
span.LogFields(log.String("cypher", cypher))
tracing.LogObjectAsJson(span, "params", params)
err := utils.ExecuteWriteQuery(ctx, *r.driver, cypher, params)
if err != nil {
tracing.TraceErr(span, err)
}
return err
}
func (r *issueWriteRepository) LinkUnthreadIssuesToOrganizationByGroupId(ctx context.Context) error {
span, ctx := opentracing.StartSpanFromContext(ctx, "IssueWriteRepository.LinkUnthreadIssuesToOrganizationByGroupId")
defer span.Finish()
cypher := `match (t:Tenant)<-[:EXTERNAL_SYSTEM_BELONGS_TO_TENANT]-(e:ExternalSystem{id:"unthread"})<-[:IS_LINKED_WITH]-(i:Issue)
with t, i
match (t)<-[:ORGANIZATION_BELONGS_TO_TENANT]-(o:Organization{slackChannelId: i.groupId})
where not (i)-[:REPORTED_BY]->(o)
MERGE (i)-[:REPORTED_BY]->(o)`
params := map[string]any{}
span.LogFields(log.String("cypher", cypher))
tracing.LogObjectAsJson(span, "params", params)
err := utils.ExecuteWriteQuery(ctx, *r.driver, cypher, params)
if err != nil {
tracing.TraceErr(span, err)
}
return err
}