-
Notifications
You must be signed in to change notification settings - Fork 12
/
log_entry_write_repository.go
140 lines (128 loc) · 5.04 KB
/
log_entry_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
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/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 LogEntryCreateFields struct {
Content string `json:"content"`
ContentType string `json:"contentType"`
StartedAt time.Time `json:"startedAt" `
AuthorUserId string `json:"authorUserId"`
LoggedOrganizationId string `json:"loggedOrganizationId"`
SourceFields model.Source `json:"sourceFields"`
CreatedAt time.Time `json:"createdAt"`
UpdatedAt time.Time `json:"updatedAt"`
}
type LogEntryUpdateFields struct {
Content string `json:"content"`
ContentType string `json:"contentType"`
StartedAt time.Time `json:"startedAt"`
UpdatedAt time.Time `json:"updatedAt"`
Source string `json:"source"`
LoggedOrganizationId string `json:"loggedOrganizationId"`
}
type LogEntryWriteRepository interface {
Create(ctx context.Context, tenant, logEntryId string, data LogEntryCreateFields) error
Update(ctx context.Context, tenant, logEntryId string, data LogEntryUpdateFields) error
}
type logEntryWriteRepository struct {
driver *neo4j.DriverWithContext
database string
}
func NewLogEntryWriteRepository(driver *neo4j.DriverWithContext, database string) LogEntryWriteRepository {
return &logEntryWriteRepository{
driver: driver,
database: database,
}
}
func (r *logEntryWriteRepository) Create(ctx context.Context, tenant, logEntryId string, data LogEntryCreateFields) error {
span, ctx := opentracing.StartSpanFromContext(ctx, "LogEntryWriteRepository.Create")
defer span.Finish()
tracing.SetNeo4jRepositorySpanTags(span, tenant)
span.SetTag(tracing.SpanTagEntityId, logEntryId)
tracing.LogObjectAsJson(span, "data", data)
cypher := fmt.Sprintf(`MATCH (t:Tenant {name:$tenant})<-[:ORGANIZATION_BELONGS_TO_TENANT]-(o:Organization {id:$orgId})
MERGE (l:LogEntry {id:$logEntryId})<-[:LOGGED]-(o)
ON CREATE SET
l:LogEntry_%s,
l:TimelineEvent,
l:TimelineEvent_%s,
l.createdAt=$createdAt,
l.updatedAt=$updatedAt,
l.startedAt=$startedAt,
l.source=$source,
l.sourceOfTruth=$sourceOfTruth,
l.appSource=$appSource,
l.content=$content,
l.contentType=$contentType
WITH l, t
OPTIONAL MATCH (t)<-[:USER_BELONGS_TO_TENANT]-(u:User {id:$authorUserId})
WHERE $authorUserId <> ""
FOREACH (ignore IN CASE WHEN u IS NOT NULL THEN [1] ELSE [] END |
MERGE (l)-[:CREATED_BY]->(u))
`, tenant, tenant)
params := map[string]any{
"tenant": tenant,
"logEntryId": logEntryId,
"orgId": data.LoggedOrganizationId,
"createdAt": data.CreatedAt,
"updatedAt": data.UpdatedAt,
"startedAt": data.StartedAt,
"source": data.SourceFields.Source,
"sourceOfTruth": data.SourceFields.SourceOfTruth,
"appSource": data.SourceFields.AppSource,
"content": data.Content,
"contentType": data.ContentType,
"authorUserId": data.AuthorUserId,
}
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 *logEntryWriteRepository) Update(ctx context.Context, tenant, logEntryId string, data LogEntryUpdateFields) error {
span, ctx := opentracing.StartSpanFromContext(ctx, "LogEntryWriteRepository.Create")
defer span.Finish()
tracing.SetNeo4jRepositorySpanTags(span, tenant)
span.SetTag(tracing.SpanTagEntityId, logEntryId)
tracing.LogObjectAsJson(span, "data", data)
cypher := fmt.Sprintf(`MATCH (l:LogEntry_%s {id:$logEntryId})
SET
l.updatedAt=$updatedAt,
l.startedAt=$startedAt,
l.sourceOfTruth=$sourceOfTruth,
l.content=$content,
l.contentType=$contentType
WITH l
OPTIONAL MATCH (t:Tenant {name:$tenant})<-[:ORGANIZATION_BELONGS_TO_TENANT]-(org:Organization {id:$orgId})
WHERE $orgId <> ""
FOREACH (ignore IN CASE WHEN org IS NOT NULL THEN [1] ELSE [] END |
MERGE (l)<-[:LOGGED]-(org))`, tenant)
params := map[string]any{
"tenant": tenant,
"logEntryId": logEntryId,
"updatedAt": data.UpdatedAt,
"startedAt": data.StartedAt,
"sourceOfTruth": data.Source,
"content": data.Content,
"contentType": data.ContentType,
"orgId": data.LoggedOrganizationId,
}
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
}