/
external_system_repository.go
49 lines (41 loc) · 1.54 KB
/
external_system_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"
"fmt"
"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"
"time"
)
type ExternalSystemRepository interface {
Merge(ctx context.Context, tenant, externalSystem string) error
}
type externalSystemRepository struct {
driver *neo4j.DriverWithContext
}
func NewExternalSystemRepository(driver *neo4j.DriverWithContext) ExternalSystemRepository {
return &externalSystemRepository{
driver: driver,
}
}
func (r *externalSystemRepository) Merge(ctx context.Context, tenant, externalSystem string) error {
span, ctx := opentracing.StartSpanFromContext(ctx, "ExternalSystemRepository.Merge")
defer span.Finish()
tracing.SetDefaultNeo4jRepositorySpanTags(ctx, span)
session := utils.NewNeo4jWriteSession(ctx, *r.driver)
defer session.Close(ctx)
query := "MATCH (t:Tenant {name:$tenant}) " +
" MERGE (t)<-[:EXTERNAL_SYSTEM_BELONGS_TO_TENANT]-(e:ExternalSystem {id:$externalSystem})" +
" ON CREATE SET e.name=$externalSystem, e.createdAt=$now, e.updatedAt=$now, e:%s "
_, err := session.ExecuteWrite(ctx, func(tx neo4j.ManagedTransaction) (any, error) {
_, err := tx.Run(ctx, fmt.Sprintf(query, "ExternalSystem_"+tenant),
map[string]interface{}{
"tenant": tenant,
"externalSystem": externalSystem,
"now": time.Now().UTC(),
})
return nil, err
})
return err
}