-
Notifications
You must be signed in to change notification settings - Fork 12
/
billing_profile_write_repository.go
239 lines (215 loc) · 9.59 KB
/
billing_profile_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
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
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/model"
"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 BillingProfileCreateFields struct {
OrganizationId string `json:"organizationId"`
LegalName string `json:"legalName"`
TaxId string `json:"taxId"`
CreatedAt time.Time `json:"createdAt"`
UpdatedAt time.Time `json:"updatedAt"`
SourceFields model.Source `json:"sourceFields"`
}
type BillingProfileUpdateFields struct {
OrganizationId string `json:"organizationId"`
UpdatedAt time.Time `json:"updatedAt"`
LegalName string `json:"legalName"`
TaxId string `json:"taxId"`
UpdateLegalName bool `json:"updateLegalName"`
UpdateTaxId bool `json:"updateTaxId"`
}
type BillingProfileWriteRepository interface {
Create(ctx context.Context, tenant, billingProfileId string, data BillingProfileCreateFields) error
Update(ctx context.Context, tenant, billingProfileId string, data BillingProfileUpdateFields) error
LinkEmailToBillingProfile(ctx context.Context, tenant, organizationId, billingProfileId, emailId string, primary bool, updatedAt time.Time) error
UnlinkEmailFromBillingProfile(ctx context.Context, tenant, organizationId, billingProfileId, emailId string, updatedAt time.Time) error
LinkLocationToBillingProfile(ctx context.Context, tenant, organizationId, billingProfileId, locationId string, updatedAt time.Time) error
UnlinkLocationFromBillingProfile(ctx context.Context, tenant, organizationId, billingProfileId, locationId string, updatedAt time.Time) error
}
type billingProfileWriteRepository struct {
driver *neo4j.DriverWithContext
database string
}
func NewBillingProfileWriteRepository(driver *neo4j.DriverWithContext, database string) BillingProfileWriteRepository {
return &billingProfileWriteRepository{
driver: driver,
database: database,
}
}
func (r *billingProfileWriteRepository) Create(ctx context.Context, tenant, billingProfileId string, data BillingProfileCreateFields) error {
span, ctx := opentracing.StartSpanFromContext(ctx, "BillingProfileWriteRepository.Create")
defer span.Finish()
tracing.SetNeo4jRepositorySpanTags(span, tenant)
span.SetTag(tracing.SpanTagEntityId, billingProfileId)
tracing.LogObjectAsJson(span, "data", data)
cypher := fmt.Sprintf(`MATCH (t:Tenant {name:$tenant})<-[:ORGANIZATION_BELONGS_TO_TENANT]-(org:Organization {id:$orgId})
MERGE (bp:BillingProfile {id:$billingProfileId})<-[:HAS_BILLING_PROFILE]-(org)
ON CREATE SET
bp:BillingProfile_%s,
bp.createdAt=$createdAt,
bp.updatedAt=$updatedAt,
bp.source=$source,
bp.sourceOfTruth=$sourceOfTruth,
bp.appSource=$appSource,
bp.legalName=$legalName,
bp.taxId=$taxId`, tenant)
params := map[string]any{
"tenant": tenant,
"billingProfileId": billingProfileId,
"orgId": data.OrganizationId,
"createdAt": data.CreatedAt,
"updatedAt": data.UpdatedAt,
"source": data.SourceFields.Source,
"sourceOfTruth": data.SourceFields.Source,
"appSource": data.SourceFields.AppSource,
"legalName": data.LegalName,
"taxId": data.TaxId,
}
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 *billingProfileWriteRepository) Update(ctx context.Context, tenant, billingProfileId string, data BillingProfileUpdateFields) error {
span, ctx := opentracing.StartSpanFromContext(ctx, "BillingProfileWriteRepository.Create")
defer span.Finish()
tracing.SetNeo4jRepositorySpanTags(span, tenant)
span.SetTag(tracing.SpanTagEntityId, billingProfileId)
tracing.LogObjectAsJson(span, "data", data)
cypher := `MATCH (:Tenant {name:$tenant})<-[:ORGANIZATION_BELONGS_TO_TENANT]-(:Organization {id:$orgId})-[:HAS_BILLING_PROFILE]->(bp:BillingProfile {id:$billingProfileId})
SET bp.updatedAt=$updatedAt
`
params := map[string]any{
"tenant": tenant,
"billingProfileId": billingProfileId,
"orgId": data.OrganizationId,
"updatedAt": data.UpdatedAt,
}
if data.UpdateLegalName {
cypher += ", bp.legalName=$legalName"
params["legalName"] = data.LegalName
}
if data.UpdateTaxId {
cypher += ", bp.taxId=$taxId"
params["taxId"] = data.TaxId
}
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 *billingProfileWriteRepository) LinkEmailToBillingProfile(ctx context.Context, tenant, organizationId, billingProfileId, emailId string, primary bool, updatedAt time.Time) error {
span, ctx := opentracing.StartSpanFromContext(ctx, "BillingProfileWriteRepository.LinkEmailToBillingProfile")
defer span.Finish()
tracing.SetNeo4jRepositorySpanTags(span, tenant)
span.SetTag(tracing.SpanTagEntityId, billingProfileId)
cypher := `MATCH (t:Tenant {name:$tenant})<-[:ORGANIZATION_BELONGS_TO_TENANT]-(org:Organization {id:$orgId})-[:HAS_BILLING_PROFILE]->(bp:BillingProfile {id:$billingProfileId}),
(t)<-[:EMAIL_ADDRESS_BELONGS_TO_TENANT]->(e:Email {id:$emailId})
MERGE (e)<-[rel:HAS]-(bp)
SET
bp.updatedAt=$updatedAt,
rel.primary=$primary
WITH bp
OPTIONAL MATCH (bp)-[rel2:HAS]->(oe:Email)
WHERE oe.id <> $emailId AND $primary = true
SET rel2.primary=false`
params := map[string]any{
"tenant": tenant,
"billingProfileId": billingProfileId,
"orgId": organizationId,
"emailId": emailId,
"updatedAt": updatedAt,
"primary": primary,
}
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 *billingProfileWriteRepository) UnlinkEmailFromBillingProfile(ctx context.Context, tenant, organizationId, billingProfileId, emailId string, updatedAt time.Time) error {
span, ctx := opentracing.StartSpanFromContext(ctx, "BillingProfileWriteRepository.UnlinkEmailFromBillingProfile")
defer span.Finish()
tracing.SetNeo4jRepositorySpanTags(span, tenant)
span.SetTag(tracing.SpanTagEntityId, billingProfileId)
cypher := `MATCH (:Tenant {name:$tenant})<-[:ORGANIZATION_BELONGS_TO_TENANT]-(:Organization {id:$orgId})-[:HAS_BILLING_PROFILE]->(bp:BillingProfile {id:$billingProfileId})-[rel:HAS]->(e:Email {id:$emailId})
SET bp.updatedAt=$updatedAt
DELETE rel`
params := map[string]any{
"tenant": tenant,
"billingProfileId": billingProfileId,
"orgId": organizationId,
"emailId": emailId,
"updatedAt": updatedAt,
}
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 *billingProfileWriteRepository) LinkLocationToBillingProfile(ctx context.Context, tenant, organizationId, billingProfileId, locationId string, updatedAt time.Time) error {
span, ctx := opentracing.StartSpanFromContext(ctx, "BillingProfileWriteRepository.LinkLocationToBillingProfile")
defer span.Finish()
tracing.SetNeo4jRepositorySpanTags(span, tenant)
span.SetTag(tracing.SpanTagEntityId, billingProfileId)
cypher := `MATCH (t:Tenant {name:$tenant})<-[:ORGANIZATION_BELONGS_TO_TENANT]-(org:Organization {id:$orgId})-[:HAS_BILLING_PROFILE]->(bp:BillingProfile {id:$billingProfileId}),
(t)<-[:LOCATION_BELONGS_TO_TENANT]->(loc:Location {id:$locationId})
MERGE (loc)<-[rel:HAS]-(bp)
SET bp.updatedAt=$updatedAt`
params := map[string]any{
"tenant": tenant,
"billingProfileId": billingProfileId,
"orgId": organizationId,
"locationId": locationId,
"updatedAt": updatedAt,
}
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 *billingProfileWriteRepository) UnlinkLocationFromBillingProfile(ctx context.Context, tenant, organizationId, billingProfileId, locationId string, updatedAt time.Time) error {
span, ctx := opentracing.StartSpanFromContext(ctx, "BillingProfileWriteRepository.UnlinkLocationFromBillingProfile")
defer span.Finish()
tracing.SetNeo4jRepositorySpanTags(span, tenant)
span.SetTag(tracing.SpanTagEntityId, billingProfileId)
cypher := `MATCH (:Tenant {name:$tenant})<-[:ORGANIZATION_BELONGS_TO_TENANT]-(:Organization {id:$orgId})-[:HAS_BILLING_PROFILE]->(bp:BillingProfile {id:$billingProfileId})-[rel:HAS]->(loc:Location {id:$locationId})
SET bp.updatedAt=$updatedAt
DELETE rel`
params := map[string]any{
"tenant": tenant,
"billingProfileId": billingProfileId,
"orgId": organizationId,
"locationId": locationId,
"updatedAt": updatedAt,
}
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
}