-
Notifications
You must be signed in to change notification settings - Fork 12
/
action_write_repository.go
158 lines (144 loc) · 6.35 KB
/
action_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
package repository
import (
"context"
"fmt"
"github.com/neo4j/neo4j-go-driver/v5/neo4j"
"github.com/neo4j/neo4j-go-driver/v5/neo4j/dbtype"
"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/enum"
"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 ActionWriteRepository interface {
Create(ctx context.Context, tenant, entityId string, entityType enum.EntityType, actionType enum.ActionType, content, metadata string, createdAt time.Time, appSource string) (*dbtype.Node, error)
CreateWithProperties(ctx context.Context, tenant, entityId string, entityType enum.EntityType, actionType enum.ActionType, content, metadata string, createdAt time.Time, appSource string, extraProperties map[string]any) (*dbtype.Node, error)
MergeByActionType(ctx context.Context, tenant, entityId string, entityType enum.EntityType, actionType enum.ActionType, content, metadata string, createdAt time.Time, appSource string) (*dbtype.Node, error)
}
type actionWriteRepository struct {
driver *neo4j.DriverWithContext
database string
}
func NewActionWriteRepository(driver *neo4j.DriverWithContext, database string) ActionWriteRepository {
return &actionWriteRepository{
driver: driver,
database: database,
}
}
func (r *actionWriteRepository) Create(ctx context.Context, tenant, entityId string, entityType enum.EntityType, actionType enum.ActionType, content, metadata string, createdAt time.Time, appSource string) (*dbtype.Node, error) {
return r.CreateWithProperties(ctx, tenant, entityId, entityType, actionType, content, metadata, createdAt, appSource, nil)
}
func (r *actionWriteRepository) CreateWithProperties(ctx context.Context, tenant, entityId string, entityType enum.EntityType, actionType enum.ActionType, content, metadata string, createdAt time.Time, appSource string, extraProperties map[string]any) (*dbtype.Node, error) {
span, ctx := opentracing.StartSpanFromContext(ctx, "ActionRepository.CreateWithProperties")
defer span.Finish()
tracing.SetNeo4jRepositorySpanTags(span, tenant)
span.LogFields(log.String("entityId", entityId),
log.String("entityType", entityType.String()),
log.String("actionType", string(actionType)),
log.String("content", content),
log.String("metadata", metadata),
log.Object("createdAt", createdAt),
log.Object("extraProperties", extraProperties))
cypher := fmt.Sprintf(`MATCH (n:%s_%s {id:$entityId}) `, entityType.Neo4jLabel(), tenant)
cypher += fmt.Sprintf(` MERGE (n)<-[:ACTION_ON]-(a:Action {id:randomUUID()})
ON CREATE SET a.type=$type,
a.content=$content,
a.metadata=$metadata,
a.createdAt=$createdAt,
a.source=$source,
a.sourceOfTruth=$sourceOfTruth,
a.appSource=$appSource,
a:Action_%s,
a:TimelineEvent,
a:TimelineEvent_%s`, tenant, tenant)
if extraProperties != nil && len(extraProperties) > 0 {
cypher += ` SET a += $extraProperties `
}
cypher += ` return a `
params := map[string]any{
"tenant": tenant,
"entityId": entityId,
"type": actionType,
"content": content,
"metadata": metadata,
"source": constants.SourceOpenline,
"sourceOfTruth": constants.SourceOpenline,
"appSource": appSource,
"createdAt": createdAt,
}
if extraProperties != nil && len(extraProperties) > 0 {
params["extraProperties"] = extraProperties
}
span.LogFields(log.String("cypher", cypher))
tracing.LogObjectAsJson(span, "params", params)
session := utils.NewNeo4jWriteSession(ctx, *r.driver)
defer session.Close(ctx)
result, err := session.ExecuteWrite(ctx, func(tx neo4j.ManagedTransaction) (any, error) {
if queryResult, err := tx.Run(ctx, cypher, params); err != nil {
return nil, err
} else {
return utils.ExtractSingleRecordFirstValueAsNode(ctx, queryResult, err)
}
})
if err != nil {
tracing.TraceErr(span, err)
return nil, err
}
return result.(*dbtype.Node), nil
}
func (r *actionWriteRepository) MergeByActionType(ctx context.Context, tenant, entityId string, entityType enum.EntityType, actionType enum.ActionType, content, metadata string, createdAt time.Time, appSource string) (*dbtype.Node, error) {
span, ctx := opentracing.StartSpanFromContext(ctx, "ActionRepository.MergeByActionType")
defer span.Finish()
tracing.SetNeo4jRepositorySpanTags(span, tenant)
span.LogFields(log.String("entityId", entityId),
log.String("entityType", entityType.String()),
log.String("actionType", string(actionType)),
log.String("content", content))
cypher := fmt.Sprintf(`MATCH (n:%s_%s {id:$entityId}) `, entityType.Neo4jLabel(), tenant)
cypher += fmt.Sprintf(`WITH n
OPTIONAL MATCH (n)<-[:ACTION_ON]-(checkA:Action {type:$type})
FOREACH (ignore IN CASE WHEN checkA IS NULL THEN [1] ELSE [] END |
MERGE (n)<-[:ACTION_ON]-(a:Action {id:randomUUID()})
ON CREATE SET a.type=$type,
a.content=$content,
a.metadata=$metadata,
a.createdAt=$createdAt,
a.source=$source,
a.sourceOfTruth=$sourceOfTruth,
a.appSource=$appSource,
a:Action_%s,
a:TimelineEvent,
a:TimelineEvent_%s)`, tenant, tenant)
cypher += ` WITH n
MATCH (n)<-[:ACTION_ON]-(act:Action {type:$type})
RETURN act `
params := map[string]any{
"tenant": tenant,
"entityId": entityId,
"type": actionType,
"content": content,
"metadata": metadata,
"source": constants.SourceOpenline,
"sourceOfTruth": constants.SourceOpenline,
"appSource": appSource,
"createdAt": createdAt,
}
span.LogFields(log.String("cypher", cypher))
tracing.LogObjectAsJson(span, "params", params)
session := utils.NewNeo4jWriteSession(ctx, *r.driver)
defer session.Close(ctx)
result, err := session.ExecuteWrite(ctx, func(tx neo4j.ManagedTransaction) (any, error) {
if queryResult, err := tx.Run(ctx, cypher, params); err != nil {
return nil, err
} else {
return utils.ExtractSingleRecordFirstValueAsNode(ctx, queryResult, err)
}
})
if err != nil {
tracing.TraceErr(span, err)
return nil, err
}
return result.(*dbtype.Node), nil
}