/
aggregate.go
251 lines (232 loc) · 8.27 KB
/
aggregate.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
package aggregate
import (
"github.com/openline-ai/openline-customer-os/packages/server/customer-os-common-module/utils"
"github.com/openline-ai/openline-customer-os/packages/server/events-processing-platform/constants"
"github.com/openline-ai/openline-customer-os/packages/server/events-processing-platform/domain/common/aggregate"
cmnmod "github.com/openline-ai/openline-customer-os/packages/server/events-processing-platform/domain/common/model"
"github.com/openline-ai/openline-customer-os/packages/server/events-processing-platform/domain/contact/event"
"github.com/openline-ai/openline-customer-os/packages/server/events-processing-platform/domain/contact/models"
"github.com/openline-ai/openline-customer-os/packages/server/events-processing-platform/eventstore"
"github.com/pkg/errors"
"strings"
)
const (
ContactAggregateType eventstore.AggregateType = "contact"
)
type ContactAggregate struct {
*aggregate.CommonTenantIdAggregate
Contact *models.Contact
}
func NewContactAggregateWithTenantAndID(tenant, id string) *ContactAggregate {
contactAggregate := ContactAggregate{}
contactAggregate.CommonTenantIdAggregate = aggregate.NewCommonAggregateWithTenantAndId(ContactAggregateType, tenant, id)
contactAggregate.SetWhen(contactAggregate.When)
contactAggregate.Contact = &models.Contact{}
contactAggregate.Tenant = tenant
return &contactAggregate
}
func (a *ContactAggregate) When(evt eventstore.Event) error {
switch evt.GetEventType() {
case event.ContactCreateV1:
return a.onContactCreate(evt)
case event.ContactUpdateV1:
return a.onContactUpdate(evt)
case event.ContactPhoneNumberLinkV1:
return a.onPhoneNumberLink(evt)
case event.ContactEmailLinkV1:
return a.onEmailLink(evt)
case event.ContactLocationLinkV1:
return a.onLocationLink(evt)
case event.ContactOrganizationLinkV1:
return a.onOrganizationLink(evt)
case event.ContactAddSocialV1:
return a.onAddSocial(evt)
default:
if strings.HasPrefix(evt.GetEventType(), constants.EsInternalStreamPrefix) {
return nil
}
err := eventstore.ErrInvalidEventType
err.EventType = evt.GetEventType()
return err
}
}
func (a *ContactAggregate) onContactCreate(evt eventstore.Event) error {
var eventData event.ContactCreateEvent
if err := evt.GetJsonData(&eventData); err != nil {
return errors.Wrap(err, "GetJsonData")
}
a.Contact.FirstName = eventData.FirstName
a.Contact.LastName = eventData.LastName
a.Contact.Prefix = eventData.Prefix
a.Contact.Name = eventData.Name
a.Contact.Description = eventData.Description
a.Contact.Timezone = eventData.Timezone
a.Contact.ProfilePhotoUrl = eventData.ProfilePhotoUrl
a.Contact.Source = cmnmod.Source{
Source: eventData.Source,
SourceOfTruth: eventData.SourceOfTruth,
AppSource: eventData.AppSource,
}
a.Contact.CreatedAt = eventData.CreatedAt
a.Contact.UpdatedAt = eventData.UpdatedAt
if eventData.ExternalSystem.Available() {
a.Contact.ExternalSystems = []cmnmod.ExternalSystem{eventData.ExternalSystem}
}
return nil
}
func (a *ContactAggregate) onContactUpdate(evt eventstore.Event) error {
var eventData event.ContactUpdateEvent
if err := evt.GetJsonData(&eventData); err != nil {
return errors.Wrap(err, "GetJsonData")
}
if eventData.Source != a.Contact.Source.SourceOfTruth && a.Contact.Source.SourceOfTruth == constants.SourceOpenline {
if a.Contact.Name == "" {
a.Contact.Name = eventData.Name
}
if a.Contact.FirstName == "" {
a.Contact.FirstName = eventData.FirstName
}
if a.Contact.LastName == "" {
a.Contact.LastName = eventData.LastName
}
if a.Contact.Timezone == "" {
a.Contact.Timezone = eventData.Timezone
}
if a.Contact.ProfilePhotoUrl == "" {
a.Contact.ProfilePhotoUrl = eventData.ProfilePhotoUrl
}
if a.Contact.Prefix == "" {
a.Contact.Prefix = eventData.Prefix
}
} else {
a.Contact.Name = eventData.Name
a.Contact.FirstName = eventData.FirstName
a.Contact.LastName = eventData.LastName
a.Contact.Prefix = eventData.Prefix
a.Contact.Description = eventData.Description
a.Contact.Timezone = eventData.Timezone
a.Contact.ProfilePhotoUrl = eventData.ProfilePhotoUrl
}
a.Contact.UpdatedAt = eventData.UpdatedAt
if eventData.Source == constants.SourceOpenline {
a.Contact.Source.SourceOfTruth = eventData.Source
}
if eventData.ExternalSystem.Available() {
found := false
for _, externalSystem := range a.Contact.ExternalSystems {
if externalSystem.ExternalSystemId == eventData.ExternalSystem.ExternalSystemId &&
externalSystem.ExternalId == eventData.ExternalSystem.ExternalId {
found = true
externalSystem.ExternalUrl = eventData.ExternalSystem.ExternalUrl
externalSystem.SyncDate = eventData.ExternalSystem.SyncDate
externalSystem.ExternalSource = eventData.ExternalSystem.ExternalSource
if eventData.ExternalSystem.ExternalIdSecond != "" {
externalSystem.ExternalIdSecond = eventData.ExternalSystem.ExternalIdSecond
}
}
}
if !found {
a.Contact.ExternalSystems = append(a.Contact.ExternalSystems, eventData.ExternalSystem)
}
}
return nil
}
func (a *ContactAggregate) onPhoneNumberLink(evt eventstore.Event) error {
var eventData event.ContactLinkPhoneNumberEvent
if err := evt.GetJsonData(&eventData); err != nil {
return errors.Wrap(err, "GetJsonData")
}
if a.Contact.PhoneNumbers == nil {
a.Contact.PhoneNumbers = make(map[string]models.ContactPhoneNumber)
}
a.Contact.PhoneNumbers[eventData.PhoneNumberId] = models.ContactPhoneNumber{
Label: eventData.Label,
Primary: eventData.Primary,
}
a.Contact.UpdatedAt = eventData.UpdatedAt
return nil
}
func (a *ContactAggregate) onEmailLink(evt eventstore.Event) error {
var eventData event.ContactLinkEmailEvent
if err := evt.GetJsonData(&eventData); err != nil {
return errors.Wrap(err, "GetJsonData")
}
if a.Contact.Emails == nil {
a.Contact.Emails = make(map[string]models.ContactEmail)
}
a.Contact.Emails[eventData.EmailId] = models.ContactEmail{
Label: eventData.Label,
Primary: eventData.Primary,
}
a.Contact.UpdatedAt = eventData.UpdatedAt
return nil
}
func (a *ContactAggregate) onLocationLink(evt eventstore.Event) error {
var eventData event.ContactLinkLocationEvent
if err := evt.GetJsonData(&eventData); err != nil {
return errors.Wrap(err, "GetJsonData")
}
a.Contact.Locations = utils.AddToListIfNotExists(a.Contact.Locations, eventData.LocationId)
return nil
}
func (a *ContactAggregate) onOrganizationLink(evt eventstore.Event) error {
var eventData event.ContactLinkWithOrganizationEvent
if err := evt.GetJsonData(&eventData); err != nil {
return errors.Wrap(err, "GetJsonData")
}
if a.Contact.JobRolesByOrganization == nil {
a.Contact.JobRolesByOrganization = make(map[string]models.JobRole)
}
jobRole, found := a.Contact.JobRolesByOrganization[eventData.OrganizationId]
if !found {
a.Contact.JobRolesByOrganization[eventData.OrganizationId] = models.JobRole{
JobTitle: eventData.JobTitle,
Primary: eventData.Primary,
Description: eventData.Description,
StartedAt: eventData.StartedAt,
EndedAt: eventData.EndedAt,
Source: cmnmod.Source{
Source: eventData.SourceFields.Source,
SourceOfTruth: eventData.SourceFields.SourceOfTruth,
AppSource: eventData.SourceFields.AppSource,
},
CreatedAt: eventData.CreatedAt,
}
} else {
if eventData.SourceFields.Source != jobRole.Source.SourceOfTruth && jobRole.Source.SourceOfTruth == constants.SourceOpenline {
if jobRole.JobTitle == "" {
jobRole.JobTitle = eventData.JobTitle
}
if jobRole.Description == "" {
jobRole.Description = eventData.Description
}
if jobRole.StartedAt == nil {
jobRole.StartedAt = eventData.StartedAt
}
if jobRole.EndedAt == nil {
jobRole.EndedAt = eventData.EndedAt
}
} else {
jobRole.JobTitle = eventData.JobTitle
jobRole.Primary = eventData.Primary
jobRole.Description = eventData.Description
jobRole.StartedAt = eventData.StartedAt
jobRole.EndedAt = eventData.EndedAt
}
}
a.Contact.UpdatedAt = eventData.UpdatedAt
return nil
}
func (a *ContactAggregate) onAddSocial(evt eventstore.Event) error {
var eventData event.AddSocialEvent
if err := evt.GetJsonData(&eventData); err != nil {
return errors.Wrap(err, "GetJsonData")
}
if a.Contact.Socials == nil {
a.Contact.Socials = make(map[string]models.Social)
}
a.Contact.Socials[eventData.SocialId] = models.Social{
Url: eventData.Url,
}
return nil
}