-
Notifications
You must be signed in to change notification settings - Fork 12
/
social_write_repository.go
100 lines (89 loc) · 3.41 KB
/
social_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
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 SocialFields struct {
SocialId string `json:"socialId"`
Url string `json:"url"`
CreatedAt time.Time `json:"createdAt"`
UpdatedAt time.Time `json:"updatedAt"`
SourceFields model.Source `json:"sourceFields"`
}
type SocialWriteRepository interface {
MergeSocialFor(ctx context.Context, tenant, linkedEntityId, linkedEntityNodeLabel string, data SocialFields) error
Remove(ctx context.Context, tenant, socialId string) error
}
type socialWriteRepository struct {
driver *neo4j.DriverWithContext
database string
}
func NewSocialWriteRepository(driver *neo4j.DriverWithContext, database string) SocialWriteRepository {
return &socialWriteRepository{
driver: driver,
database: database,
}
}
func (r *socialWriteRepository) MergeSocialFor(ctx context.Context, tenant, linkedEntityId, linkedEntityNodeLabel string, data SocialFields) error {
span, ctx := opentracing.StartSpanFromContext(ctx, "SocialWriteRepository.MergeSocialFor")
defer span.Finish()
tracing.SetNeo4jRepositorySpanTags(span, tenant)
span.LogFields(log.String("linkedEntityId", linkedEntityId), log.String("linkedEntityNodeLabel", linkedEntityNodeLabel))
tracing.LogObjectAsJson(span, "data", data)
cypher := fmt.Sprintf(`
MATCH (e:%s {id:$entityId})
OPTIONAL MATCH (e)-[r:HAS]->(checkSoc:Social {url:$url})
FOREACH (ignore IN CASE WHEN checkSoc IS NULL OR checkSoc.id = $id THEN [1] ELSE [] END |
MERGE (e)-[:HAS]->(soc:Social {id:$id})
ON CREATE SET
soc.createdAt=$createdAt,
soc.updatedAt=$updatedAt,
soc.source=$source,
soc.sourceOfTruth=$sourceOfTruth,
soc.appSource=$appSource,
soc.url=$url,
soc.syncedWithEventStore=true,
soc:Social_%s
ON MATCH SET
soc.syncedWithEventStore=true)`, linkedEntityNodeLabel+"_"+tenant, tenant)
params := map[string]any{
"entityId": linkedEntityId,
"id": data.SocialId,
"createdAt": data.CreatedAt,
"updatedAt": data.UpdatedAt,
"url": data.Url,
"source": data.SourceFields.Source,
"sourceOfTruth": data.SourceFields.SourceOfTruth,
"appSource": data.SourceFields.AppSource,
}
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 *socialWriteRepository) Remove(ctx context.Context, tenant, socialId string) error {
span, ctx := opentracing.StartSpanFromContext(ctx, "SocialWriteRepository.MergeSocialFor")
defer span.Finish()
tracing.SetNeo4jRepositorySpanTags(span, tenant)
cypher := fmt.Sprintf(`MATCH (soc:Social_%s {id:$socialId}) DETACH DELETE soc`, tenant)
params := map[string]any{
"socialId": socialId,
}
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
}