-
Notifications
You must be signed in to change notification settings - Fork 12
/
tenant_write_repository.go
393 lines (365 loc) · 14.5 KB
/
tenant_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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
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/enum"
"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/neo4jutil"
"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 TenantBillingProfileCreateFields struct {
Id string `json:"id"`
CreatedAt time.Time `json:"createdAt"`
SourceFields model.Source `json:"sourceFields"`
Phone string `json:"phone"`
LegalName string `json:"legalName"`
AddressLine1 string `json:"addressLine1"`
AddressLine2 string `json:"addressLine2"`
AddressLine3 string `json:"addressLine3"`
Locality string `json:"locality"`
Country string `json:"country"`
Region string `json:"region"`
Zip string `json:"zip"`
VatNumber string `json:"vatNumber"`
SendInvoicesFrom string `json:"sendInvoicesFrom"`
SendInvoicesBcc string `json:"sendInvoicesBcc"`
CanPayWithPigeon bool `json:"canPayWithPigeon"`
CanPayWithBankTransfer bool `json:"canPayWithBankTransfer"`
Check bool `json:"check"`
}
type TenantBillingProfileUpdateFields struct {
Id string `json:"id"`
UpdatedAt time.Time `json:"updatedAt"`
Phone string `json:"phone"`
LegalName string `json:"legalName"`
AddressLine1 string `json:"addressLine1"`
AddressLine2 string `json:"addressLine2"`
AddressLine3 string `json:"addressLine3"`
Locality string `json:"locality"`
Country string `json:"country"`
Region string `json:"region"`
Zip string `json:"zip"`
VatNumber string `json:"vatNumber"`
SendInvoicesFrom string `json:"sendInvoicesFrom"`
SendInvoicesBcc string `json:"sendInvoicesBcc"`
CanPayWithPigeon bool `json:"canPayWithPigeon"`
CanPayWithBankTransfer bool `json:"canPayWithBankTransfer"`
Check bool `json:"check"`
UpdatePhone bool `json:"updatePhone"`
UpdateLegalName bool `json:"updateLegalName"`
UpdateAddressLine1 bool `json:"updateAddressLine1"`
UpdateAddressLine2 bool `json:"updateAddressLine2"`
UpdateAddressLine3 bool `json:"updateAddressLine3"`
UpdateLocality bool `json:"updateLocality"`
UpdateCountry bool `json:"updateCountry"`
UpdateRegion bool `json:"updateRegion"`
UpdateZip bool `json:"updateZip"`
UpdateVatNumber bool `json:"updateVatNumber"`
UpdateSendInvoicesFrom bool `json:"updateSendInvoicesFrom"`
UpdateSendInvoicesBcc bool `json:"updateSendInvoicesBcc"`
UpdateCanPayWithPigeon bool `json:"updateCanPayWithPigeon"`
UpdateCanPayWithBankTransfer bool `json:"updateCanPayWithBankTransfer"`
UpdateCheck bool `json:"updateCheck"`
}
type TenantSettingsFields struct {
UpdatedAt time.Time `json:"updatedAt"`
LogoRepositoryFileId string `json:"logoRepositoryFileId"`
BaseCurrency enum.Currency `json:"baseCurrency"`
InvoicingEnabled bool `json:"invoicingEnabled"`
InvoicingPostpaid bool `json:"invoicingPostpaid"`
UpdateLogoRepositoryFileId bool `json:"updateLogoRepositoryFileId"`
UpdateInvoicingEnabled bool `json:"updateInvoicingEnabled"`
UpdateInvoicingPostpaid bool `json:"updateInvoicingPostpaid"`
UpdateBaseCurrency bool `json:"updateBaseCurrency"`
}
type TenantWriteRepository interface {
CreateTenantBillingProfile(ctx context.Context, tenant string, data TenantBillingProfileCreateFields) error
UpdateTenantBillingProfile(ctx context.Context, tenant string, data TenantBillingProfileUpdateFields) error
UpdateTenantSettings(ctx context.Context, tenant string, data TenantSettingsFields) error
HardDeleteTenant(ctx context.Context, tenant string) error
}
type tenantWriteRepository struct {
driver *neo4j.DriverWithContext
database string
}
func NewTenantWriteRepository(driver *neo4j.DriverWithContext, database string) TenantWriteRepository {
return &tenantWriteRepository{
driver: driver,
database: database,
}
}
func (r *tenantWriteRepository) CreateTenantBillingProfile(ctx context.Context, tenant string, data TenantBillingProfileCreateFields) error {
span, ctx := opentracing.StartSpanFromContext(ctx, "TenantWriteRepository.CreateTenantBillingProfile")
defer span.Finish()
tracing.SetNeo4jRepositorySpanTags(span, tenant)
cypher := fmt.Sprintf(`MATCH (t:Tenant {name:$tenant})
MERGE (t)-[:HAS_BILLING_PROFILE]->(tbp:TenantBillingProfile {id:$billingProfileId})
ON CREATE SET
tbp:TenantBillingProfile_%s,
tbp.createdAt=$createdAt,
tbp.updatedAt=$updatedAt,
tbp.source=$source,
tbp.sourceOfTruth=$sourceOfTruth,
tbp.appSource=$appSource,
tbp.phone=$phone,
tbp.legalName=$legalName,
tbp.addressLine1=$addressLine1,
tbp.addressLine2=$addressLine2,
tbp.addressLine3=$addressLine3,
tbp.locality=$locality,
tbp.country=$country,
tbp.region=$region,
tbp.zip=$zip,
tbp.vatNumber=$vatNumber,
tbp.sendInvoicesFrom=$sendInvoicesFrom,
tbp.sendInvoicesBcc=$sendInvoicesBcc,
tbp.canPayWithPigeon=$canPayWithPigeon,
tbp.canPayWithBankTransfer=$canPayWithBankTransfer,
tbp.check=$check
`, tenant)
params := map[string]any{
"tenant": tenant,
"billingProfileId": data.Id,
"createdAt": data.CreatedAt,
"updatedAt": data.CreatedAt,
"source": data.SourceFields.Source,
"sourceOfTruth": data.SourceFields.Source,
"appSource": data.SourceFields.AppSource,
"phone": data.Phone,
"legalName": data.LegalName,
"addressLine1": data.AddressLine1,
"addressLine2": data.AddressLine2,
"addressLine3": data.AddressLine3,
"locality": data.Locality,
"country": data.Country,
"region": data.Region,
"zip": data.Zip,
"vatNumber": data.VatNumber,
"sendInvoicesFrom": data.SendInvoicesFrom,
"sendInvoicesBcc": data.SendInvoicesBcc,
"canPayWithPigeon": data.CanPayWithPigeon,
"canPayWithBankTransfer": data.CanPayWithBankTransfer,
"check": data.Check,
}
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 *tenantWriteRepository) UpdateTenantBillingProfile(ctx context.Context, tenant string, data TenantBillingProfileUpdateFields) error {
span, ctx := opentracing.StartSpanFromContext(ctx, "TenantWriteRepository.UpdateTenantBillingProfile")
defer span.Finish()
tracing.SetNeo4jRepositorySpanTags(span, tenant)
tracing.LogObjectAsJson(span, "data", data)
cypher := `MATCH (:Tenant {name:$tenant})-[:HAS_BILLING_PROFILE]->(tbp:TenantBillingProfile {id:$billingProfileId})
SET tbp.updatedAt=$updatedAt
`
params := map[string]any{
"tenant": tenant,
"billingProfileId": data.Id,
"updatedAt": data.UpdatedAt,
}
if data.UpdatePhone {
cypher += `,tbp.phone=$phone`
params["phone"] = data.Phone
}
if data.UpdateLegalName {
cypher += `,tbp.legalName=$legalName`
params["legalName"] = data.LegalName
}
if data.UpdateAddressLine1 {
cypher += `,tbp.addressLine1=$addressLine1`
params["addressLine1"] = data.AddressLine1
}
if data.UpdateAddressLine2 {
cypher += `,tbp.addressLine2=$addressLine2`
params["addressLine2"] = data.AddressLine2
}
if data.UpdateAddressLine3 {
cypher += `,tbp.addressLine3=$addressLine3`
params["addressLine3"] = data.AddressLine3
}
if data.UpdateLocality {
cypher += `,tbp.locality=$locality`
params["locality"] = data.Locality
}
if data.UpdateCountry {
cypher += `,tbp.country=$country`
params["country"] = data.Country
}
if data.UpdateRegion {
cypher += `,tbp.region=$region`
params["region"] = data.Region
}
if data.UpdateZip {
cypher += `,tbp.zip=$zip`
params["zip"] = data.Zip
}
if data.UpdateVatNumber {
cypher += `,tbp.vatNumber=$vatNumber`
params["vatNumber"] = data.VatNumber
}
if data.UpdateSendInvoicesFrom {
cypher += `,tbp.sendInvoicesFrom=$sendInvoicesFrom`
params["sendInvoicesFrom"] = data.SendInvoicesFrom
}
if data.UpdateSendInvoicesBcc {
cypher += `,tbp.sendInvoicesBcc=$sendInvoicesBcc`
params["sendInvoicesBcc"] = data.SendInvoicesBcc
}
if data.UpdateCanPayWithPigeon {
cypher += `,tbp.canPayWithPigeon=$canPayWithPigeon`
params["canPayWithPigeon"] = data.CanPayWithPigeon
}
if data.UpdateCanPayWithBankTransfer {
cypher += `,tbp.canPayWithBankTransfer=$canPayWithBankTransfer`
params["canPayWithBankTransfer"] = data.CanPayWithBankTransfer
}
if data.UpdateCheck {
cypher += `,tbp.check=$check`
params["check"] = data.Check
}
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 *tenantWriteRepository) UpdateTenantSettings(ctx context.Context, tenant string, data TenantSettingsFields) error {
span, ctx := opentracing.StartSpanFromContext(ctx, "TenantWriteRepository.UpdateTenantBillingProfile")
defer span.Finish()
tracing.SetNeo4jRepositorySpanTags(span, tenant)
tracing.LogObjectAsJson(span, "data", data)
cypher := `MATCH (t:Tenant {name:$tenant})
MERGE (t)-[:HAS_SETTINGS]->(ts:TenantSettings {tenant:$tenant})
ON CREATE SET
ts.id=randomUUID(),
ts.createdAt=$now
SET
ts.updatedAt=$updatedAt`
params := map[string]any{
"tenant": tenant,
"updatedAt": data.UpdatedAt,
"now": utils.Now(),
}
if data.UpdateInvoicingEnabled {
cypher += ", ts.invoicingEnabled=$invoicingEnabled"
params["invoicingEnabled"] = data.InvoicingEnabled
}
if data.UpdateInvoicingPostpaid {
cypher += ", ts.invoicingPostpaid=$invoicingPostpaid"
params["invoicingPostpaid"] = data.InvoicingPostpaid
}
if data.UpdateBaseCurrency {
cypher += ", ts.baseCurrency=$baseCurrency"
params["baseCurrency"] = data.BaseCurrency.String()
}
if data.UpdateLogoRepositoryFileId {
cypher += ", ts.logoRepositoryFileId=$logoRepositoryFileId"
params["logoRepositoryFileId"] = data.LogoRepositoryFileId
}
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 *tenantWriteRepository) HardDeleteTenant(ctx context.Context, tenant string) error {
span, ctx := opentracing.StartSpanFromContext(ctx, "TenantWriteRepository.HardDelete")
defer span.Finish()
tracing.SetNeo4jRepositorySpanTags(span, tenant)
tracing.LogObjectAsJson(span, "tenant", tenant)
nodeWithTenantSuffix := []string{
neo4jutil.NodeLabelTenantBillingProfile,
neo4jutil.NodeLabelBankAccount,
neo4jutil.NodeLabelTimelineEvent,
neo4jutil.NodeLabelContact,
neo4jutil.NodeLabelCustomField,
neo4jutil.NodeLabelJobRole,
neo4jutil.NodeLabelEmail,
neo4jutil.NodeLabelLocation,
neo4jutil.NodeLabelInteractionEvent,
neo4jutil.NodeLabelInteractionSession,
neo4jutil.NodeLabelNote,
neo4jutil.NodeLabelLogEntry,
neo4jutil.NodeLabelOrganization,
neo4jutil.NodeLabelBillingProfile,
neo4jutil.NodeLabelMasterPlan,
neo4jutil.NodeLabelMasterPlanMilestone,
neo4jutil.NodeLabelAction,
neo4jutil.NodeLabelPageView,
neo4jutil.NodeLabelPhoneNumber,
neo4jutil.NodeLabelTag,
neo4jutil.NodeLabelIssue,
neo4jutil.NodeLabelUser,
neo4jutil.NodeLabelAnalysis,
neo4jutil.NodeLabelAttachment,
neo4jutil.NodeLabelMeeting,
neo4jutil.NodeLabelSocial,
neo4jutil.NodeLabelActionItem,
neo4jutil.NodeLabelComment,
neo4jutil.NodeLabelContract,
neo4jutil.NodeLabelDeletedContract,
neo4jutil.NodeLabelServiceLineItem,
neo4jutil.NodeLabelOpportunity,
neo4jutil.NodeLabelInvoicingCycle,
neo4jutil.NodeLabelExternalSystem,
neo4jutil.NodeLabelInvoice,
neo4jutil.NodeLabelInvoiceLine,
neo4jutil.NodeLabelOrganizationPlan,
neo4jutil.NodeLabelOrganizationPlanMilestone,
neo4jutil.NodeLabelReminder,
neo4jutil.NodeLabelOrder,
neo4jutil.NodeLabelOffering,
}
//drop nodes with NodeLabel_Tenant
for _, nodeLabel := range nodeWithTenantSuffix {
err := utils.ExecuteWriteQuery(ctx, *r.driver, fmt.Sprintf(`MATCH (n:%s_%s) DETACH DELETE n;`, nodeLabel, tenant), nil)
if err != nil {
tracing.TraceErr(span, err)
return err
}
}
//drop TenantSettings
err := utils.ExecuteWriteQuery(ctx, *r.driver, `MATCH (t:TenantSettings{tenant: $tenant}) DETACH DELETE t`, map[string]any{"tenant": tenant})
if err != nil {
tracing.TraceErr(span, err)
return err
}
//drop workspaces
err = utils.ExecuteWriteQuery(ctx, *r.driver, `MATCH (w:Workspace)<-[r:HAS_WORKSPACE]-(t:Tenant{name: $tenant}) DELETE r, w`, map[string]any{"tenant": tenant})
if err != nil {
tracing.TraceErr(span, err)
return err
}
//drop tenant
err = utils.ExecuteWriteQuery(ctx, *r.driver, `MATCH (t:Tenant{name: $tenant}) DELETE t`, map[string]any{"tenant": tenant})
if err != nil {
tracing.TraceErr(span, err)
return err
}
//clear Player nodes not linked to a user in the system
err = utils.ExecuteWriteQuery(ctx, *r.driver,
`match (p:Player)
optional match (p)-[r]-(u:User)
with p, r, u
where u is null
delete p`, nil)
if err != nil {
tracing.TraceErr(span, err)
return err
}
return err
}