-
Notifications
You must be signed in to change notification settings - Fork 12
/
invoicing_cycle_write_repository.go
92 lines (81 loc) · 3.07 KB
/
invoicing_cycle_write_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
package repository
import (
"context"
"fmt"
"github.com/neo4j/neo4j-go-driver/v5/neo4j"
"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"
"time"
)
type InvoicingCycleWriteRepository interface {
CreateInvoicingCycleType(ctx context.Context, tenant, id, invoicingCycleType, source, appSource string, createdAt time.Time) error
UpdateInvoicingCycleType(ctx context.Context, tenant, id, invoicingCycleType string, updatedAt time.Time) error
}
type invoicingCycleWriteRepository struct {
driver *neo4j.DriverWithContext
database string
}
func NewInvoicingCycleWriteRepository(driver *neo4j.DriverWithContext, database string) InvoicingCycleWriteRepository {
return &invoicingCycleWriteRepository{
driver: driver,
database: database,
}
}
func (r *invoicingCycleWriteRepository) CreateInvoicingCycleType(ctx context.Context, tenant, id, invoicingCycleType, source, appSource string, createdAt time.Time) error {
span, ctx := opentracing.StartSpanFromContext(ctx, "InvoicingCycleWriteRepository.Create")
defer span.Finish()
tracing.SetNeo4jRepositorySpanTags(span, tenant)
span.SetTag(tracing.SpanTagEntityId, id)
cypher := fmt.Sprintf(`MATCH (t:Tenant {name:$tenant})
MERGE (t)<-[:INVOICING_CYCLE_BELONGS_TO_TENANT]-(ic:InvoicingCycle {id:$id})
ON CREATE SET
ic:InvoicingCycle_%s,
ic.createdAt=$createdAt,
ic.updatedAt=$updatedAt,
ic.source=$source,
ic.sourceOfTruth=$sourceOfTruth,
ic.appSource=$appSource,
ic.type=$type
`, tenant)
params := map[string]any{
"tenant": tenant,
"id": id,
"createdAt": createdAt,
"updatedAt": createdAt,
"source": source,
"sourceOfTruth": source,
"appSource": appSource,
"type": invoicingCycleType,
}
span.LogFields(log.String("cypher", cypher))
tracing.LogObjectAsJson(span, "params", params)
err := utils.ExecuteWriteQuery(ctx, *r.driver, cypher, params)
if err != nil {
tracing.TraceErr(span, err)
}
return err
}
func (r *invoicingCycleWriteRepository) UpdateInvoicingCycleType(ctx context.Context, tenant, id, invoicingCycleType string, updatedAt time.Time) error {
span, ctx := opentracing.StartSpanFromContext(ctx, "InvoicingCycleWriteRepository.Update")
defer span.Finish()
tracing.SetNeo4jRepositorySpanTags(span, tenant)
span.SetTag(tracing.SpanTagEntityId, id)
cypher := `MATCH (:Tenant {name:$tenant})<-[:INVOICING_CYCLE_BELONGS_TO_TENANT]-(ic:InvoicingCycle {id:$id})
SET ic.updatedAt=$updatedAt,
ic.type=$type`
params := map[string]any{
"tenant": tenant,
"id": id,
"updatedAt": updatedAt,
"type": invoicingCycleType,
}
span.LogFields(log.String("cypher", cypher))
tracing.LogObjectAsJson(span, "params", params)
err := utils.ExecuteWriteQuery(ctx, *r.driver, cypher, params)
if err != nil {
tracing.TraceErr(span, err)
}
return err
}