-
Notifications
You must be signed in to change notification settings - Fork 12
/
contact_write_repository.go
198 lines (180 loc) · 8.9 KB
/
contact_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
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/constants"
"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 ContactCreateFields struct {
FirstName string `json:"firstName"`
LastName string `json:"lastName"`
Prefix string `json:"prefix"`
Description string `json:"description"`
Timezone string `json:"timezone"`
ProfilePhotoUrl string `json:"profilePhotoUrl"`
Name string `json:"name"`
CreatedAt time.Time `json:"createdAt"`
UpdatedAt time.Time `json:"updatedAt"`
SourceFields model.Source `json:"sourceFields"`
}
type ContactUpdateFields struct {
FirstName string `json:"firstName"`
LastName string `json:"lastName"`
Prefix string `json:"prefix"`
Description string `json:"description"`
Timezone string `json:"timezone"`
ProfilePhotoUrl string `json:"profilePhotoUrl"`
Name string `json:"name"`
UpdatedAt time.Time `json:"updatedAt"`
Source string `json:"source"`
UpdateFirstName bool `json:"updateFirstName"`
UpdateLastName bool `json:"updateLastName"`
UpdateName bool `json:"updateName"`
UpdatePrefix bool `json:"updatePrefix"`
UpdateDescription bool `json:"updateDescription"`
UpdateTimezone bool `json:"updateTimezone"`
UpdateProfilePhotoUrl bool `json:"updateProfilePhotoUrl"`
}
type ContactWriteRepository interface {
CreateContact(ctx context.Context, tenant, contactId string, data ContactCreateFields) error
CreateContactInTx(ctx context.Context, tx neo4j.ManagedTransaction, tenant, contactId string, data ContactCreateFields) error
UpdateContact(ctx context.Context, tenant, contactId string, data ContactUpdateFields) error
}
type contactWriteRepository struct {
driver *neo4j.DriverWithContext
database string
}
func NewContactWriteRepository(driver *neo4j.DriverWithContext, database string) ContactWriteRepository {
return &contactWriteRepository{
driver: driver,
database: database,
}
}
func (r *contactWriteRepository) CreateContact(ctx context.Context, tenant, contactId string, data ContactCreateFields) error {
span, ctx := opentracing.StartSpanFromContext(ctx, "ContactWriteRepository.CreateContact")
defer span.Finish()
tracing.SetNeo4jRepositorySpanTags(span, tenant)
span.SetTag(tracing.SpanTagEntityId, contactId)
session := utils.NewNeo4jWriteSession(ctx, *r.driver)
defer session.Close(ctx)
_, err := session.ExecuteWrite(ctx, func(tx neo4j.ManagedTransaction) (any, error) {
return nil, r.CreateContactInTx(ctx, tx, tenant, contactId, data)
})
return err
}
func (r *contactWriteRepository) CreateContactInTx(ctx context.Context, tx neo4j.ManagedTransaction, tenant, contactId string, data ContactCreateFields) error {
span, ctx := opentracing.StartSpanFromContext(ctx, "ContactWriteRepository.CreateContactInTx")
defer span.Finish()
tracing.SetNeo4jRepositorySpanTags(span, tenant)
span.SetTag(tracing.SpanTagEntityId, contactId)
cypher := fmt.Sprintf(`MATCH (t:Tenant {name:$tenant})
MERGE (t)<-[:CONTACT_BELONGS_TO_TENANT]-(c:Contact:Contact_%s {id:$id})
ON CREATE SET
c.firstName = $firstName,
c.lastName = $lastName,
c.prefix = $prefix,
c.description = $description,
c.timezone = $timezone,
c.profilePhotoUrl = $profilePhotoUrl,
c.name = $name,
c.source = $source,
c.sourceOfTruth = $sourceOfTruth,
c.appSource = $appSource,
c.createdAt = $createdAt,
c.updatedAt = $updatedAt,
c.syncedWithEventStore = true
ON MATCH SET
c.name = CASE WHEN c.sourceOfTruth=$sourceOfTruth OR $overwrite=true OR c.name is null OR c.name = '' THEN $name ELSE c.name END,
c.firstName = CASE WHEN c.sourceOfTruth=$sourceOfTruth OR $overwrite=true OR c.firstName is null OR c.firstName = '' THEN $firstName ELSE c.firstName END,
c.lastName = CASE WHEN c.sourceOfTruth=$sourceOfTruth OR $overwrite=true OR c.lastName is null OR c.lastName = '' THEN $lastName ELSE c.lastName END,
c.timezone = CASE WHEN c.sourceOfTruth=$sourceOfTruth OR $overwrite=true OR c.timezone is null OR c.timezone = '' THEN $timezone ELSE c.timezone END,
c.profilePhotoUrl = CASE WHEN c.sourceOfTruth=$sourceOfTruth OR $overwrite=true OR c.profilePhotoUrl is null OR c.profilePhotoUrl = '' THEN $profilePhotoUrl ELSE c.profilePhotoUrl END,
c.prefix = CASE WHEN c.sourceOfTruth=$sourceOfTruth OR $overwrite=true OR c.prefix is null OR c.prefix = '' THEN $prefix ELSE c.prefix END,
c.description = CASE WHEN c.sourceOfTruth=$sourceOfTruth OR $overwrite=true OR c.description is null OR c.description = '' THEN $description ELSE c.description END,
c.updatedAt = $updatedAt,
c.sourceOfTruth = case WHEN $overwrite=true THEN $sourceOfTruth ELSE c.sourceOfTruth END,
c.syncedWithEventStore = true
`, tenant)
params := map[string]any{
"id": contactId,
"firstName": data.FirstName,
"lastName": data.LastName,
"prefix": data.Prefix,
"description": data.Description,
"timezone": data.Timezone,
"profilePhotoUrl": data.ProfilePhotoUrl,
"name": data.Name,
"tenant": tenant,
"source": data.SourceFields.Source,
"sourceOfTruth": data.SourceFields.SourceOfTruth,
"appSource": data.SourceFields.AppSource,
"createdAt": data.CreatedAt,
"updatedAt": data.UpdatedAt,
"overwrite": data.SourceFields.SourceOfTruth == constants.SourceOpenline,
}
span.LogFields(log.String("cypher", cypher))
tracing.LogObjectAsJson(span, "params", params)
err := utils.ExecuteQueryInTx(ctx, tx, cypher, params)
if err != nil {
tracing.TraceErr(span, err)
}
return err
}
func (r *contactWriteRepository) UpdateContact(ctx context.Context, tenant, contactId string, data ContactUpdateFields) error {
span, ctx := opentracing.StartSpanFromContext(ctx, "ContactWriteRepository.UpdateContact")
defer span.Finish()
tracing.SetNeo4jRepositorySpanTags(span, tenant)
span.SetTag(tracing.SpanTagEntityId, contactId)
cypher := fmt.Sprintf(`MATCH (t:Tenant {name:$tenant})<-[:CONTACT_BELONGS_TO_TENANT]-(c:Contact:Contact_%s {id:$id})
SET c.updatedAt = $updatedAt,
c.sourceOfTruth = case WHEN $overwrite=true THEN $sourceOfTruth ELSE c.sourceOfTruth END,
c.syncedWithEventStore = true`, tenant)
params := map[string]any{
"id": contactId,
"tenant": tenant,
"updatedAt": data.UpdatedAt,
"sourceOfTruth": data.Source,
"overwrite": data.Source == constants.SourceOpenline,
}
if data.UpdateFirstName {
cypher += ", c.firstName = CASE WHEN c.sourceOfTruth=$sourceOfTruth OR $overwrite=true OR c.firstName is null OR c.firstName = '' THEN $firstName ELSE c.firstName END"
params["firstName"] = data.FirstName
}
if data.UpdateLastName {
cypher += ", c.lastName = CASE WHEN c.sourceOfTruth=$sourceOfTruth OR $overwrite=true OR c.lastName is null OR c.lastName = '' THEN $lastName ELSE c.lastName END"
params["lastName"] = data.LastName
}
if data.UpdateName {
cypher += ", c.name = CASE WHEN c.sourceOfTruth=$sourceOfTruth OR $overwrite=true OR c.name is null OR c.name = '' THEN $name ELSE c.name END"
params["name"] = data.Name
}
if data.UpdatePrefix {
cypher += ", c.prefix = CASE WHEN c.sourceOfTruth=$sourceOfTruth OR $overwrite=true OR c.prefix is null OR c.prefix = '' THEN $prefix ELSE c.prefix END"
params["prefix"] = data.Prefix
}
if data.UpdateDescription {
cypher += ", c.description = CASE WHEN c.sourceOfTruth=$sourceOfTruth OR $overwrite=true OR c.description is null OR c.description = '' THEN $description ELSE c.description END"
params["description"] = data.Description
}
if data.UpdateTimezone {
cypher += ", c.timezone = CASE WHEN c.sourceOfTruth=$sourceOfTruth OR $overwrite=true OR c.timezone is null OR c.timezone = '' THEN $timezone ELSE c.timezone END"
params["timezone"] = data.Timezone
}
if data.UpdateProfilePhotoUrl {
cypher += ", c.profilePhotoUrl = CASE WHEN c.sourceOfTruth=$sourceOfTruth OR $overwrite=true OR c.profilePhotoUrl is null OR c.profilePhotoUrl = '' THEN $profilePhotoUrl ELSE c.profilePhotoUrl END"
params["profilePhotoUrl"] = data.ProfilePhotoUrl
}
span.LogFields(log.String("query", cypher))
tracing.LogObjectAsJson(span, "params", params)
err := utils.ExecuteWriteQuery(ctx, *r.driver, cypher, params)
if err != nil {
tracing.TraceErr(span, err)
}
return err
}