-
Notifications
You must be signed in to change notification settings - Fork 12
/
user_repository.go
110 lines (94 loc) · 3.82 KB
/
user_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
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 UserRepository interface {
GetUserIdById(ctx context.Context, tenant, id string) (string, error)
GetUserIdByExternalId(ctx context.Context, tenant, externalId, externalSystemId string) (string, error)
GetUserIdByExternalOwnerId(ctx context.Context, tenant, externalOwnerId, externalSystemId string) (string, error)
}
type userRepository struct {
driver *neo4j.DriverWithContext
}
func NewUserRepository(driver *neo4j.DriverWithContext) UserRepository {
return &userRepository{
driver: driver,
}
}
func (r *userRepository) GetUserIdById(ctx context.Context, tenant, id string) (string, error) {
span, ctx := opentracing.StartSpanFromContext(ctx, "UserRepository.GetUserIdById")
defer span.Finish()
tracing.SetDefaultNeo4jRepositorySpanTags(ctx, span)
query := `MATCH (t:Tenant {name:$tenant})<-[:USER_BELONGS_TO_TENANT]-(u:User {id:$userId})
return u.id order by u.createdAt`
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,
"userId": id,
})
return utils.ExtractAllRecordsAsString(ctx, queryResult, err)
})
if err != nil {
return "", err
}
if len(records.([]string)) == 0 {
return "", nil
}
return records.([]string)[0], nil
}
func (r *userRepository) GetUserIdByExternalId(ctx context.Context, tenant, externalId, externalSystemId string) (string, error) {
span, ctx := opentracing.StartSpanFromContext(ctx, "UserRepository.GetUserIdByExternalId")
defer span.Finish()
tracing.SetDefaultNeo4jRepositorySpanTags(ctx, span)
query := `MATCH (t:Tenant {name:$tenant})<-[:EXTERNAL_SYSTEM_BELONGS_TO_TENANT]-(e:ExternalSystem {id:$externalSystemId})
MATCH (t)<-[:USER_BELONGS_TO_TENANT]-(u:User)-[:IS_LINKED_WITH {externalId:$externalId}]->(e)
return u.id order by u.createdAt`
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,
"externalId": externalId,
"externalSystemId": externalSystemId,
})
return utils.ExtractAllRecordsAsString(ctx, queryResult, err)
})
if err != nil {
return "", err
}
if len(records.([]string)) == 0 {
return "", nil
}
return records.([]string)[0], nil
}
func (r *userRepository) GetUserIdByExternalOwnerId(ctx context.Context, tenant, externalOwnerId, externalSystemId string) (string, error) {
span, ctx := opentracing.StartSpanFromContext(ctx, "UserRepository.GetUserIdByExternalOwnerId")
defer span.Finish()
tracing.SetDefaultNeo4jRepositorySpanTags(ctx, span)
query := `MATCH (t:Tenant {name:$tenant})<-[:EXTERNAL_SYSTEM_BELONGS_TO_TENANT]-(e:ExternalSystem {id:$externalSystemId})
MATCH (t)<-[:USER_BELONGS_TO_TENANT]-(u:User)-[:IS_LINKED_WITH {externalIdSecond:$externalOwnerId}]->(e)
return u.id order by u.createdAt`
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,
"externalOwnerId": externalOwnerId,
"externalSystemId": externalSystemId,
})
return utils.ExtractAllRecordsAsString(ctx, queryResult, err)
})
if err != nil {
return "", err
}
if len(records.([]string)) == 0 {
return "", nil
}
return records.([]string)[0], nil
}