-
Notifications
You must be signed in to change notification settings - Fork 12
/
calendar_service.go
73 lines (65 loc) · 2.98 KB
/
calendar_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
package service
import (
"context"
"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/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"
"github.com/opentracing/opentracing-go"
"github.com/opentracing/opentracing-go/log"
)
type CalendarService interface {
GetAllForUsers(ctx context.Context, userIds []string) (*entity.CalendarEntities, error)
}
type calendarService struct {
log logger.Logger
repositories *repository.Repositories
services *Services
}
func NewCalendarService(log logger.Logger, repositories *repository.Repositories, services *Services) CalendarService {
return &calendarService{
log: log,
repositories: repositories,
services: services,
}
}
func (s *calendarService) getDriver() neo4j.DriverWithContext {
return *s.repositories.Drivers.Neo4jDriver
}
func (s *calendarService) GetAllForUsers(ctx context.Context, userIds []string) (*entity.CalendarEntities, error) {
span, ctx := opentracing.StartSpanFromContext(ctx, "CalendarService.GetAllForUsers")
defer span.Finish()
tracing.SetDefaultServiceSpanTags(ctx, span)
span.LogFields(log.Object("userIds", userIds))
calendars, err := s.repositories.CalendarRepository.GetAllForUsers(ctx, common.GetTenantFromContext(ctx), userIds)
if err != nil {
return nil, err
}
calendarEntities := entity.CalendarEntities{}
for _, v := range calendars {
calendarEntity := s.mapDbNodeToCalendarEntity(*v.Node)
calendarEntity.DataloaderKey = v.LinkedNodeId
calendarEntities = append(calendarEntities, *calendarEntity)
}
return &calendarEntities, nil
}
func (s *calendarService) mapDbNodeToCalendarEntity(node dbtype.Node) *entity.CalendarEntity {
props := utils.GetPropsFromNode(node)
result := entity.CalendarEntity{
Id: utils.GetStringPropOrEmpty(props, "id"),
CalType: utils.GetStringPropOrEmpty(props, "calType"),
Link: utils.GetStringPropOrEmpty(props, "link"),
Primary: utils.GetBoolPropOrFalse(props, "primary"),
Source: neo4jentity.GetDataSource(utils.GetStringPropOrEmpty(props, "source")),
SourceOfTruth: neo4jentity.GetDataSource(utils.GetStringPropOrEmpty(props, "sourceOfTruth")),
AppSource: utils.GetStringPropOrEmpty(props, "appSource"),
CreatedAt: utils.GetTimePropOrEpochStart(props, "createdAt"),
UpdatedAt: utils.GetTimePropOrEpochStart(props, "updatedAt"),
}
return &result
}