-
Notifications
You must be signed in to change notification settings - Fork 12
/
order_read_repository.go
132 lines (115 loc) · 4.64 KB
/
order_read_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
package repository
import (
"context"
"github.com/neo4j/neo4j-go-driver/v5/neo4j"
"github.com/neo4j/neo4j-go-driver/v5/neo4j/db"
"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/tracing"
"github.com/opentracing/opentracing-go"
"github.com/opentracing/opentracing-go/log"
)
type OrderReadRepository interface {
GetById(ctx context.Context, tenant, id string) (*dbtype.Node, error)
GetMatchedOrderId(ctx context.Context, tenant, externalSystem, externalId string) (string, error)
GetAllForOrganizations(ctx context.Context, tenant string, organizationIds []string) ([]*utils.DbNodeAndId, error)
}
type orderReadRepository struct {
driver *neo4j.DriverWithContext
database string
}
func NewOrderReadRepository(driver *neo4j.DriverWithContext, database string) OrderReadRepository {
return &orderReadRepository{
driver: driver,
database: database,
}
}
func (r *orderReadRepository) GetById(ctx context.Context, tenant, id string) (*dbtype.Node, error) {
span, ctx := opentracing.StartSpanFromContext(ctx, "OrderReadRepository.GetMatchedOrderId")
defer span.Finish()
tracing.SetNeo4jRepositorySpanTags(span, tenant)
span.SetTag(tracing.SpanTagEntityId, id)
cypher := `MATCH (t:Tenant {name:$tenant})<-[:ORDER_BELONGS_TO_TENANT]-(o:Order {id:$id})
RETURN o limit 1`
params := map[string]interface{}{
"tenant": tenant,
"id": id,
}
span.LogFields(log.String("cypher", cypher))
session := r.prepareReadSession(ctx)
defer session.Close(ctx)
result, err := session.ExecuteRead(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
}
span.LogFields(log.Bool("result.found", result != nil))
return result.(*dbtype.Node), nil
}
func (r *orderReadRepository) GetMatchedOrderId(ctx context.Context, tenant, externalSystem, externalId string) (string, error) {
span, ctx := opentracing.StartSpanFromContext(ctx, "OrderReadRepository.GetMatchedOrderId")
defer span.Finish()
span.LogFields(log.String("externalSystem", externalSystem), log.String("externalId", externalId))
cypher := `MATCH (t:Tenant {name:$tenant})<-[:EXTERNAL_SYSTEM_BELONGS_TO_TENANT]-(e:ExternalSystem {id:$externalSystem})
OPTIONAL MATCH (e)<-[:IS_LINKED_WITH {externalId:$orderExternalId}]-(o:Order)
WITH o WHERE o IS NOT null
RETURN o.id ORDER BY o.createdAt limit 1`
params := map[string]interface{}{
"tenant": tenant,
"externalSystem": externalSystem,
"orderExternalId": externalId,
}
span.LogFields(log.String("cypher", cypher))
session := utils.NewNeo4jReadSession(ctx, *r.driver, utils.WithDatabaseName(r.database))
defer session.Close(ctx)
dbRecords, err := session.ExecuteRead(ctx, func(tx neo4j.ManagedTransaction) (any, error) {
queryResult, err := tx.Run(ctx, cypher, params)
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 *orderReadRepository) GetAllForOrganizations(ctx context.Context, tenant string, organizationIds []string) ([]*utils.DbNodeAndId, error) {
span, ctx := opentracing.StartSpanFromContext(ctx, "OrderReadRepository.GetAllForOrganizations")
defer span.Finish()
span.LogFields(log.Object("organizationIds", organizationIds))
cypher := `MATCH (t:Tenant {name:$tenant})<-[:ORGANIZATION_BELONGS_TO_TENANT]-(o:Organization)-[:HAS]->(or:Order)
WHERE o.id IN $organizationIds
RETURN or, o.id as organizationId ORDER BY or.createdAt`
params := map[string]interface{}{
"tenant": tenant,
"organizationIds": organizationIds,
}
span.LogFields(log.String("cypher", cypher))
session := utils.NewNeo4jReadSession(ctx, *r.driver, utils.WithDatabaseName(r.database))
defer session.Close(ctx)
result, err := session.ExecuteRead(ctx, func(tx neo4j.ManagedTransaction) (any, error) {
queryResult, err := tx.Run(ctx, cypher, params)
if err != nil {
return nil, err
}
return utils.ExtractAllRecordsAsDbNodeAndId(ctx, queryResult, err)
})
if err != nil {
return nil, err
}
return result.([]*utils.DbNodeAndId), err
}
func (r *orderReadRepository) prepareReadSession(ctx context.Context) neo4j.SessionWithContext {
return utils.NewNeo4jReadSession(ctx, *r.driver, utils.WithDatabaseName(r.database))
}