-
Notifications
You must be signed in to change notification settings - Fork 12
/
location_repository.go
49 lines (43 loc) · 1.48 KB
/
location_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
package repository
import (
"context"
"github.com/neo4j/neo4j-go-driver/v5/neo4j"
"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 LocationRepository interface {
GetAllCrossTenants(ctx context.Context, size int) ([]*utils.DbNodeAndId, error)
}
type locationRepository struct {
driver *neo4j.DriverWithContext
}
func NewLocationRepository(driver *neo4j.DriverWithContext) LocationRepository {
return &locationRepository{
driver: driver,
}
}
func (r *locationRepository) GetAllCrossTenants(ctx context.Context, size int) ([]*utils.DbNodeAndId, error) {
span, ctx := opentracing.StartSpanFromContext(ctx, "LocationRepository.GetAllCrossTenants")
defer span.Finish()
tracing.SetDefaultNeo4jRepositorySpanTags(ctx, span)
session := utils.NewNeo4jReadSession(ctx, *r.driver)
defer session.Close(ctx)
result, err := session.ExecuteRead(ctx, func(tx neo4j.ManagedTransaction) (any, error) {
if queryResult, err := tx.Run(ctx, `
MATCH (l:Location)--(t:Tenant)
WHERE (l.syncedWithEventStore is null or l.syncedWithEventStore=false)
RETURN l, t.name limit $size`,
map[string]any{
"size": size,
}); err != nil {
return nil, err
} else {
return utils.ExtractAllRecordsAsDbNodeAndId(ctx, queryResult, err)
}
})
if err != nil {
return nil, err
}
return result.([]*utils.DbNodeAndId), err
}