/
social_service.go
101 lines (89 loc) · 4.56 KB
/
social_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
package service
import (
"context"
"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/errors"
"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 SocialService interface {
CreateSocialForEntity(ctx context.Context, linkedEntityType entity.EntityType, linkedEntityId string, socialEntity entity.SocialEntity) (*entity.SocialEntity, error)
Update(ctx context.Context, entity entity.SocialEntity) (*entity.SocialEntity, error)
GetAllForEntities(ctx context.Context, linkedEntityType entity.EntityType, linkedEntityIds []string) (*entity.SocialEntities, error)
Remove(ctx context.Context, socialId string) error
}
type socialService struct {
log logger.Logger
repositories *repository.Repositories
}
func NewSocialService(log logger.Logger, repositories *repository.Repositories) SocialService {
return &socialService{
log: log,
repositories: repositories,
}
}
func (s *socialService) GetAllForEntities(ctx context.Context, linkedEntityType entity.EntityType, linkedEntityIds []string) (*entity.SocialEntities, error) {
span, ctx := opentracing.StartSpanFromContext(ctx, "SocialService.GetAllForEntities")
defer span.Finish()
tracing.SetDefaultServiceSpanTags(ctx, span)
span.LogFields(log.String("linkedEntityType", string(linkedEntityType)), log.Object("linkedEntityIds", linkedEntityIds))
socials, err := s.repositories.SocialRepository.GetAllForEntities(ctx, common.GetTenantFromContext(ctx), linkedEntityType, linkedEntityIds)
if err != nil {
return nil, err
}
socialEntities := make(entity.SocialEntities, 0, len(socials))
for _, v := range socials {
socialEntity := s.mapDbNodeToSocialEntity(*v.Node)
socialEntity.DataloaderKey = v.LinkedNodeId
socialEntities = append(socialEntities, *socialEntity)
}
return &socialEntities, nil
}
func (s *socialService) CreateSocialForEntity(ctx context.Context, linkedEntityType entity.EntityType, linkedEntityId string, socialEntity entity.SocialEntity) (*entity.SocialEntity, error) {
if linkedEntityType != entity.CONTACT && linkedEntityType != entity.ORGANIZATION {
return nil, errors.ErrInvalidEntityType
}
socialNode, err := s.repositories.SocialRepository.CreateSocialForEntity(ctx, common.GetTenantFromContext(ctx), linkedEntityType, linkedEntityId, socialEntity)
if err != nil {
return nil, err
}
return s.mapDbNodeToSocialEntity(*socialNode), nil
}
func (s *socialService) Update(ctx context.Context, socialEntity entity.SocialEntity) (*entity.SocialEntity, error) {
span, ctx := opentracing.StartSpanFromContext(ctx, "SocialService.Update")
defer span.Finish()
tracing.SetDefaultServiceSpanTags(ctx, span)
updatedLocationNode, err := s.repositories.SocialRepository.Update(ctx, common.GetTenantFromContext(ctx), socialEntity)
if err != nil {
return nil, err
}
return s.mapDbNodeToSocialEntity(*updatedLocationNode), nil
}
func (s *socialService) Remove(ctx context.Context, socialId string) error {
span, ctx := opentracing.StartSpanFromContext(ctx, "SocialService.Update")
defer span.Finish()
tracing.SetDefaultServiceSpanTags(ctx, span)
span.LogFields(log.String("socialId", socialId))
return s.repositories.SocialRepository.Remove(ctx, socialId)
}
func (s *socialService) mapDbNodeToSocialEntity(node dbtype.Node) *entity.SocialEntity {
props := utils.GetPropsFromNode(node)
return &entity.SocialEntity{
Id: utils.GetStringPropOrEmpty(props, "id"),
Url: utils.GetStringPropOrEmpty(props, "url"),
CreatedAt: utils.GetTimePropOrEpochStart(props, "createdAt"),
UpdatedAt: utils.GetTimePropOrEpochStart(props, "updatedAt"),
SourceFields: entity.SourceFields{
Source: neo4jentity.GetDataSource(utils.GetStringPropOrEmpty(props, "source")),
SourceOfTruth: neo4jentity.GetDataSource(utils.GetStringPropOrEmpty(props, "sourceOfTruth")),
AppSource: utils.GetStringPropOrEmpty(props, "appSource"),
},
}
}