-
Notifications
You must be signed in to change notification settings - Fork 12
/
meeting_service.go
475 lines (413 loc) · 18.8 KB
/
meeting_service.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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
package service
import (
"context"
"fmt"
"github.com/neo4j/neo4j-go-driver/v5/neo4j"
"github.com/neo4j/neo4j-go-driver/v5/neo4j/dbtype"
"github.com/openline-ai/openline-customer-os/packages/server/customer-os-api/entity"
"github.com/openline-ai/openline-customer-os/packages/server/customer-os-api/graph/model"
"github.com/openline-ai/openline-customer-os/packages/server/customer-os-api/mapper"
"github.com/openline-ai/openline-customer-os/packages/server/customer-os-api/repository"
"github.com/openline-ai/openline-customer-os/packages/server/customer-os-common-module/common"
"github.com/openline-ai/openline-customer-os/packages/server/customer-os-common-module/logger"
"github.com/openline-ai/openline-customer-os/packages/server/customer-os-common-module/tracing"
"github.com/openline-ai/openline-customer-os/packages/server/customer-os-common-module/utils"
neo4jentity "github.com/openline-ai/openline-customer-os/packages/server/customer-os-neo4j-repository/entity"
neo4jmapper "github.com/openline-ai/openline-customer-os/packages/server/customer-os-neo4j-repository/mapper"
"github.com/openline-ai/openline-customer-os/packages/server/customer-os-neo4j-repository/neo4jutil"
"github.com/opentracing/opentracing-go"
"github.com/opentracing/opentracing-go/log"
"golang.org/x/exp/slices"
"reflect"
"time"
)
type MeetingService interface {
mapDbNodeToMeetingEntity(node dbtype.Node) *entity.MeetingEntity
Update(ctx context.Context, input *MeetingUpdateData) (*entity.MeetingEntity, error)
Create(ctx context.Context, newMeeting *MeetingCreateData) (*entity.MeetingEntity, error)
LinkAttendedBy(ctx context.Context, meetingID string, participant MeetingParticipant) error
UnlinkAttendedBy(ctx context.Context, meetingID string, participant MeetingParticipant) error
LinkAttachment(ctx context.Context, meetingID string, attachmentID string) (*entity.MeetingEntity, error)
UnlinkAttachment(ctx context.Context, meetingID string, attachmentID string) (*entity.MeetingEntity, error)
LinkRecordingAttachment(ctx context.Context, meetingID string, attachmentID string) (*entity.MeetingEntity, error)
UnlinkRecordingAttachment(ctx context.Context, meetingID string, attachmentID string) (*entity.MeetingEntity, error)
GetMeetingById(ctx context.Context, meetingId string) (*entity.MeetingEntity, error)
GetMeetingForInteractionEvent(ctx context.Context, interactionEventId string) (*entity.MeetingEntity, error)
GetMeetingsForInteractionEvents(ctx context.Context, ids []string) (*entity.MeetingEntities, error)
GetParticipantsForMeetings(ctx context.Context, ids []string, relation entity.MeetingRelation) (*entity.MeetingParticipants, error)
FindAll(ctx context.Context, externalSystemID string, externalID *string, page, limit int, filter *model.Filter, sortBy []*model.SortBy) (*utils.Pagination, error)
}
type MeetingParticipant struct {
ContactId *string
UserId *string
OrganizationId *string
}
type MeetingCreateData struct {
MeetingEntity *entity.MeetingEntity
CreatedBy []MeetingParticipant
AttendedBy []MeetingParticipant
NoteInput *model.NoteInput
ExternalReference *neo4jentity.ExternalSystemEntity
}
type MeetingUpdateData struct {
MeetingEntity *entity.MeetingEntity
NoteEntity *entity.NoteEntity
Meeting *string
ExternalReference *neo4jentity.ExternalSystemEntity
}
type meetingService struct {
log logger.Logger
repositories *repository.Repositories
services *Services
}
func NewMeetingService(log logger.Logger, repositories *repository.Repositories, services *Services) MeetingService {
return &meetingService{
log: log,
repositories: repositories,
services: services,
}
}
func (s *meetingService) Create(ctx context.Context, newMeeting *MeetingCreateData) (*entity.MeetingEntity, error) {
session := utils.NewNeo4jWriteSession(ctx, s.getNeo4jDriver())
defer session.Close(ctx)
queryResult, err := session.ExecuteWrite(ctx, s.createMeetingInDBTxWork(ctx, newMeeting))
if err != nil {
return nil, err
}
for _, participant := range newMeeting.CreatedBy {
if participant.ContactId != nil {
s.services.OrganizationService.UpdateLastTouchpointByContactId(ctx, *participant.ContactId)
}
if participant.OrganizationId != nil {
s.services.OrganizationService.UpdateLastTouchpoint(ctx, *participant.OrganizationId)
}
}
for _, participant := range newMeeting.AttendedBy {
if participant.ContactId != nil {
s.services.OrganizationService.UpdateLastTouchpointByContactId(ctx, *participant.ContactId)
}
if participant.OrganizationId != nil {
s.services.OrganizationService.UpdateLastTouchpoint(ctx, *participant.OrganizationId)
}
}
return s.mapDbNodeToMeetingEntity(*queryResult.(*dbtype.Node)), nil
}
func (s *meetingService) Update(ctx context.Context, input *MeetingUpdateData) (*entity.MeetingEntity, error) {
session := utils.NewNeo4jWriteSession(ctx, s.getNeo4jDriver())
defer session.Close(ctx)
queryResult, err := session.ExecuteWrite(ctx, func(tx neo4j.ManagedTransaction) (any, error) {
meetingDbNode, err := s.repositories.MeetingRepository.Update(ctx, tx, common.GetTenantFromContext(ctx), input.MeetingEntity)
tenant := common.GetContext(ctx).Tenant
if input.ExternalReference != nil {
err := s.repositories.ExternalSystemRepository.LinkNodeWithExternalSystemInTx(ctx, tx, tenant, input.MeetingEntity.Id, neo4jutil.NodeLabelMeeting, *input.ExternalReference)
if err != nil {
return nil, err
}
}
if err != nil {
return nil, err
}
if input.NoteEntity != nil {
_, err := s.services.NoteService.UpdateNote(ctx, input.NoteEntity)
if err != nil {
return nil, err
}
}
return meetingDbNode, nil
})
if err != nil {
return nil, err
}
return s.mapDbNodeToMeetingEntity(*queryResult.(*dbtype.Node)), nil
}
func (s *meetingService) LinkAttachment(ctx context.Context, meetingID string, attachmentID string) (*entity.MeetingEntity, error) {
node, err := s.services.AttachmentService.LinkNodeWithAttachment(ctx, repository.LINKED_WITH_MEETING, nil, attachmentID, meetingID)
if err != nil {
return nil, err
}
return s.mapDbNodeToMeetingEntity(*node), nil
}
func (s *meetingService) UnlinkAttachment(ctx context.Context, meetingID string, attachmentID string) (*entity.MeetingEntity, error) {
node, err := s.services.AttachmentService.UnlinkNodeWithAttachment(ctx, repository.LINKED_WITH_MEETING, nil, attachmentID, meetingID)
if err != nil {
return nil, err
}
return s.mapDbNodeToMeetingEntity(*node), nil
}
func (s *meetingService) LinkRecordingAttachment(ctx context.Context, meetingID string, attachmentID string) (*entity.MeetingEntity, error) {
recording := repository.LINKED_NATURE_RECORDING
node, err := s.services.AttachmentService.LinkNodeWithAttachment(ctx, repository.LINKED_WITH_MEETING, &recording, attachmentID, meetingID)
if err != nil {
return nil, err
}
return s.mapDbNodeToMeetingEntity(*node), nil
}
func (s *meetingService) UnlinkRecordingAttachment(ctx context.Context, meetingID string, attachmentID string) (*entity.MeetingEntity, error) {
recording := repository.LINKED_NATURE_RECORDING
node, err := s.services.AttachmentService.UnlinkNodeWithAttachment(ctx, repository.LINKED_WITH_MEETING, &recording, attachmentID, meetingID)
if err != nil {
return nil, err
}
return s.mapDbNodeToMeetingEntity(*node), nil
}
func (s *meetingService) LinkAttendedBy(ctx context.Context, meetingID string, participant MeetingParticipant) error {
session := utils.NewNeo4jReadSession(ctx, s.getNeo4jDriver())
defer session.Close(ctx)
_, err := session.ExecuteWrite(ctx, func(tx neo4j.ManagedTransaction) (interface{}, error) {
tenant := common.GetContext(ctx).Tenant
err := s.linkAttendedByTxWork(ctx, tx, tenant, meetingID, participant, entity.ATTENDED_BY)
return nil, err
})
if participant.ContactId != nil {
s.services.OrganizationService.UpdateLastTouchpointByContactId(ctx, *participant.ContactId)
}
if participant.OrganizationId != nil {
s.services.OrganizationService.UpdateLastTouchpoint(ctx, *participant.OrganizationId)
}
return err
}
func (s *meetingService) UnlinkAttendedBy(ctx context.Context, meetingID string, participant MeetingParticipant) error {
session := utils.NewNeo4jReadSession(ctx, s.getNeo4jDriver())
defer session.Close(ctx)
_, err := session.ExecuteWrite(ctx, func(tx neo4j.ManagedTransaction) (interface{}, error) {
tenant := common.GetContext(ctx).Tenant
err := s.unlinkAttendedByTxWork(ctx, tx, tenant, meetingID, participant, entity.ATTENDED_BY)
return nil, err
})
if participant.ContactId != nil {
s.services.OrganizationService.UpdateLastTouchpointByContactId(ctx, *participant.ContactId)
}
if participant.OrganizationId != nil {
s.services.OrganizationService.UpdateLastTouchpoint(ctx, *participant.OrganizationId)
}
return err
}
func (s *meetingService) GetMeetingById(ctx context.Context, id string) (*entity.MeetingEntity, error) {
session := utils.NewNeo4jReadSession(ctx, s.getNeo4jDriver())
defer session.Close(ctx)
queryResult, err := session.ExecuteRead(ctx, func(tx neo4j.ManagedTransaction) (interface{}, error) {
result, err := tx.Run(ctx, fmt.Sprintf(`
MATCH (m:Meeting_%s {id:$id}) RETURN m`,
common.GetTenantFromContext(ctx)),
map[string]interface{}{
"id": id,
})
record, err := result.Single(ctx)
if err != nil {
return nil, err
}
return record.Values[0], nil
})
if err != nil {
return nil, err
}
return s.mapDbNodeToMeetingEntity(queryResult.(dbtype.Node)), nil
}
func (s *meetingService) createMeetingInDBTxWork(ctx context.Context, newMeeting *MeetingCreateData) func(tx neo4j.ManagedTransaction) (any, error) {
return func(tx neo4j.ManagedTransaction) (any, error) {
tenant := common.GetContext(ctx).Tenant
meetingDbNode, err := s.repositories.MeetingRepository.Create(ctx, tx, common.GetTenantFromContext(ctx), newMeeting.MeetingEntity)
if err != nil {
return nil, err
}
var meetingId = utils.GetPropsFromNode(*meetingDbNode)["id"].(string)
for _, createdBy := range newMeeting.CreatedBy {
err := s.linkAttendedByTxWork(ctx, tx, tenant, meetingId, createdBy, entity.CREATED_BY)
if err != nil {
return nil, err
}
}
for _, attendedBy := range newMeeting.AttendedBy {
err := s.linkAttendedByTxWork(ctx, tx, tenant, meetingId, attendedBy, entity.ATTENDED_BY)
if err != nil {
return nil, err
}
}
if newMeeting.NoteInput != nil {
toEntity := mapper.MapNoteInputToEntity(newMeeting.NoteInput)
_, err := s.repositories.NoteRepository.CreateNoteForMeetingTx(ctx, tx, tenant, meetingId, toEntity)
if err != nil {
return nil, err
}
}
if newMeeting.ExternalReference != nil {
err := s.repositories.ExternalSystemRepository.LinkNodeWithExternalSystemInTx(ctx, tx, tenant, meetingId, neo4jutil.NodeLabelMeeting, *newMeeting.ExternalReference)
if err != nil {
return nil, err
}
}
return meetingDbNode, nil
}
}
func (s *meetingService) linkAttendedByTxWork(ctx context.Context, tx neo4j.ManagedTransaction, tenantName, meetingId string, participant MeetingParticipant, relationType entity.MeetingRelation) error {
var err error
if participant.ContactId != nil {
err = s.repositories.MeetingRepository.LinkWithParticipantInTx(ctx, tx, tenantName, meetingId, *participant.ContactId, entity.CONTACT, relationType)
} else if participant.UserId != nil {
err = s.repositories.MeetingRepository.LinkWithParticipantInTx(ctx, tx, tenantName, meetingId, *participant.UserId, entity.USER, relationType)
} else if participant.OrganizationId != nil {
err = s.repositories.MeetingRepository.LinkWithParticipantInTx(ctx, tx, tenantName, meetingId, *participant.OrganizationId, entity.ORGANIZATION, relationType)
}
if err != nil {
return err
}
return nil
}
func (s *meetingService) unlinkAttendedByTxWork(ctx context.Context, tx neo4j.ManagedTransaction, tenantName, meetingId string, participant MeetingParticipant, relationType entity.MeetingRelation) error {
var err error
if participant.ContactId != nil {
err = s.repositories.MeetingRepository.UnlinkParticipantInTx(ctx, tx, tenantName, meetingId, *participant.ContactId, entity.CONTACT, relationType)
} else if participant.UserId != nil {
err = s.repositories.MeetingRepository.UnlinkParticipantInTx(ctx, tx, tenantName, meetingId, *participant.UserId, entity.USER, relationType)
} else if participant.OrganizationId != nil {
err = s.repositories.MeetingRepository.UnlinkParticipantInTx(ctx, tx, tenantName, meetingId, *participant.OrganizationId, entity.ORGANIZATION, relationType)
}
if err != nil {
return err
}
return nil
}
// createdAt takes priority over startedAt
func (s *meetingService) migrateStartedAt(props map[string]any) time.Time {
if props["createdAt"] != nil {
return utils.GetTimePropOrNow(props, "createdAt")
}
if props["startedAt"] != nil {
return utils.GetTimePropOrNow(props, "startedAt")
}
return time.Now()
}
func (s *meetingService) mapDbNodeToMeetingEntity(node dbtype.Node) *entity.MeetingEntity {
props := utils.GetPropsFromNode(node)
status := entity.GetMeetingStatus(utils.GetStringPropOrEmpty(props, "status"))
meetingEntity := entity.MeetingEntity{
Id: utils.GetStringPropOrEmpty(props, "id"),
Name: utils.GetStringPropOrNil(props, "name"),
ConferenceUrl: utils.GetStringPropOrNil(props, "conferenceUrl"),
MeetingExternalUrl: utils.GetStringPropOrNil(props, "meetingExternalUrl"),
Agenda: utils.GetStringPropOrNil(props, "agenda"),
AgendaContentType: utils.GetStringPropOrNil(props, "agendaContentType"),
CreatedAt: s.migrateStartedAt(props),
UpdatedAt: utils.GetTimePropOrNow(props, "updatedAt"),
StartedAt: utils.GetTimePropOrNil(props, "startedAt"),
EndedAt: utils.GetTimePropOrNil(props, "endedAt"),
Recording: utils.GetStringPropOrNil(props, "recording"),
AppSource: utils.GetStringPropOrEmpty(props, "appSource"),
Source: neo4jentity.GetDataSource(utils.GetStringPropOrEmpty(props, "source")),
SourceOfTruth: neo4jentity.GetDataSource(utils.GetStringPropOrEmpty(props, "sourceOfTruth")),
Status: &status,
}
return &meetingEntity
}
func MapMeetingParticipantInputToParticipant(participant *model.MeetingParticipantInput) MeetingParticipant {
meetingParticipant := MeetingParticipant{
UserId: participant.UserID,
ContactId: participant.ContactID,
OrganizationId: participant.OrganizationID,
}
return meetingParticipant
}
func MapMeetingParticipantInputListToParticipant(input []*model.MeetingParticipantInput) []MeetingParticipant {
var inputData []MeetingParticipant
for _, participant := range input {
inputData = append(inputData, MapMeetingParticipantInputToParticipant(participant))
}
return inputData
}
func (s *meetingService) GetParticipantsForMeetings(ctx context.Context, ids []string, relation entity.MeetingRelation) (*entity.MeetingParticipants, error) {
records, err := s.repositories.MeetingRepository.GetParticipantsForMeetings(ctx, common.GetTenantFromContext(ctx), ids, relation)
if err != nil {
return nil, err
}
interactionEventParticipants := s.convertDbNodesToMeetingParticipants(records)
return &interactionEventParticipants, nil
}
func (s *meetingService) GetMeetingForInteractionEvent(ctx context.Context, interactionEventId string) (*entity.MeetingEntity, error) {
record, err := s.repositories.MeetingRepository.GetMeetingForInteractionEvent(ctx, common.GetTenantFromContext(ctx), interactionEventId)
if err != nil {
return nil, err
}
if record == nil {
return nil, nil
}
return s.mapDbNodeToMeetingEntity(*record), nil
}
func (s *meetingService) GetMeetingsForInteractionEvents(ctx context.Context, ids []string) (*entity.MeetingEntities, error) {
span, ctx := opentracing.StartSpanFromContext(ctx, "MeetingService.GetMeetingsForInteractionEvents")
defer span.Finish()
tracing.SetDefaultServiceSpanTags(ctx, span)
span.LogFields(log.Object("ids", ids))
issues, err := s.repositories.MeetingRepository.GetAllForInteractionEvents(ctx, common.GetTenantFromContext(ctx), ids)
if err != nil {
return nil, err
}
meetingEntities := make(entity.MeetingEntities, 0, len(issues))
for _, v := range issues {
meetingEntity := s.mapDbNodeToMeetingEntity(*v.Node)
meetingEntity.DataloaderKey = v.LinkedNodeId
meetingEntities = append(meetingEntities, *meetingEntity)
}
return &meetingEntities, nil
}
func (s *meetingService) FindAll(ctx context.Context, externalSystemID string, externalID *string, page, limit int, filter *model.Filter, sortBy []*model.SortBy) (*utils.Pagination, error) {
session := utils.NewNeo4jReadSession(ctx, s.getNeo4jDriver())
defer session.Close(ctx)
var paginatedResult = utils.Pagination{
Limit: limit,
Page: page,
}
cypherSort, err := buildSort(sortBy, reflect.TypeOf(entity.MeetingEntity{}))
if err != nil {
return nil, err
}
cypherFilter, err := buildFilter(filter, reflect.TypeOf(entity.MeetingEntity{}))
if err != nil {
return nil, err
}
dbNodesWithTotalCount, err := s.repositories.MeetingRepository.GetPaginatedMeetings(
ctx, session,
externalSystemID,
externalID,
common.GetContext(ctx).Tenant,
common.GetContext(ctx).UserEmail,
paginatedResult.GetSkip(),
paginatedResult.GetLimit(),
cypherFilter,
cypherSort)
if err != nil {
return nil, err
}
paginatedResult.SetTotalRows(dbNodesWithTotalCount.Count)
meetings := make(entity.MeetingEntities, 0, len(dbNodesWithTotalCount.Nodes))
for _, v := range dbNodesWithTotalCount.Nodes {
meetings = append(meetings, *s.mapDbNodeToMeetingEntity(*v))
}
paginatedResult.SetRows(&meetings)
return &paginatedResult, nil
}
func (s *meetingService) convertDbNodesToMeetingParticipants(records []*utils.DbNodeWithRelationAndId) entity.MeetingParticipants {
meetingParticipants := entity.MeetingParticipants{}
for _, v := range records {
if slices.Contains(v.Node.Labels, neo4jutil.NodeLabelUser) {
participant := s.services.UserService.mapDbNodeToUserEntity(*v.Node)
participant.DataloaderKey = v.LinkedNodeId
meetingParticipants = append(meetingParticipants, participant)
} else if slices.Contains(v.Node.Labels, neo4jutil.NodeLabelContact) {
participant := s.services.ContactService.mapDbNodeToContactEntity(*v.Node)
participant.DataloaderKey = v.LinkedNodeId
meetingParticipants = append(meetingParticipants, participant)
} else if slices.Contains(v.Node.Labels, neo4jutil.NodeLabelOrganization) {
participant := neo4jmapper.MapDbNodeToOrganizationEntity(v.Node)
participant.DataloaderKey = v.LinkedNodeId
meetingParticipants = append(meetingParticipants, participant)
} else if slices.Contains(v.Node.Labels, neo4jutil.NodeLabelEmail) {
participant := s.services.EmailService.mapDbNodeToEmailEntity(*v.Node)
participant.DataloaderKey = v.LinkedNodeId
meetingParticipants = append(meetingParticipants, participant)
}
}
return meetingParticipants
}
func (s *meetingService) getNeo4jDriver() neo4j.DriverWithContext {
return *s.repositories.Drivers.Neo4jDriver
}