-
Notifications
You must be signed in to change notification settings - Fork 12
/
log_entry_repository.go
88 lines (76 loc) · 2.89 KB
/
log_entry_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
package repository
import (
"context"
"fmt"
"github.com/neo4j/neo4j-go-driver/v5/neo4j"
"github.com/neo4j/neo4j-go-driver/v5/neo4j/db"
"github.com/openline-ai/openline-customer-os/packages/runner/sync-customer-os-data/entity"
"github.com/openline-ai/openline-customer-os/packages/runner/sync-customer-os-data/tracing"
"github.com/openline-ai/openline-customer-os/packages/server/customer-os-common-module/utils"
"github.com/opentracing/opentracing-go"
)
type LogEntryRepository interface {
GetMatchedLogEntryId(ctx context.Context, tenant string, logEntry entity.LogEntryData) (string, error)
GetLogEntryIdById(ctx context.Context, tenant, id string) (string, error)
}
type logEntryRepository struct {
driver *neo4j.DriverWithContext
}
func NewLogEntryRepository(driver *neo4j.DriverWithContext) LogEntryRepository {
return &logEntryRepository{
driver: driver,
}
}
func (r *logEntryRepository) GetMatchedLogEntryId(ctx context.Context, tenant string, logEntry entity.LogEntryData) (string, error) {
span, ctx := opentracing.StartSpanFromContext(ctx, "LogEntryRepository.GetMatchedLogEntryId")
defer span.Finish()
tracing.SetDefaultNeo4jRepositorySpanTags(ctx, span)
session := utils.NewNeo4jWriteSession(ctx, *r.driver)
defer session.Close(ctx)
query := `MATCH (t:Tenant {name:$tenant})<-[:EXTERNAL_SYSTEM_BELONGS_TO_TENANT]-(e:ExternalSystem {id:$externalSystem})
OPTIONAL MATCH (e)<-[:IS_LINKED_WITH {externalId:$logEntryExternalId}]-(l:LogEntry)
WITH l WHERE l is not null
return l.id order by l.createdAt limit 1`
dbRecords, err := session.ExecuteWrite(ctx, func(tx neo4j.ManagedTransaction) (any, error) {
queryResult, err := tx.Run(ctx, query,
map[string]interface{}{
"tenant": tenant,
"externalSystem": logEntry.ExternalSystem,
"logEntryExternalId": logEntry.ExternalId,
})
if err != nil {
return nil, err
}
return queryResult.Collect(ctx)
})
if err != nil {
return "", err
}
noteIDs := dbRecords.([]*db.Record)
if len(noteIDs) > 0 {
return noteIDs[0].Values[0].(string), nil
}
return "", nil
}
func (r *logEntryRepository) GetLogEntryIdById(ctx context.Context, tenant, id string) (string, error) {
span, ctx := opentracing.StartSpanFromContext(ctx, "LogEntryRepository.GetLogEntryIdById")
defer span.Finish()
tracing.SetDefaultNeo4jRepositorySpanTags(ctx, span)
query := fmt.Sprintf(`MATCH (l:LogEntry_%s {id:$logEntryId}) RETURN l.id order by l.createdAt`, tenant)
session := utils.NewNeo4jReadSession(ctx, *r.driver)
defer session.Close(ctx)
records, err := session.ExecuteRead(ctx, func(tx neo4j.ManagedTransaction) (any, error) {
queryResult, err := tx.Run(ctx, query, map[string]any{
"tenant": tenant,
"logEntryId": id,
})
return utils.ExtractAllRecordsAsString(ctx, queryResult, err)
})
if err != nil {
return "", err
}
if len(records.([]string)) == 0 {
return "", nil
}
return records.([]string)[0], nil
}