-
Notifications
You must be signed in to change notification settings - Fork 12
/
comment_service.go
67 lines (60 loc) · 2.84 KB
/
comment_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
package service
import (
"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"
"golang.org/x/net/context"
)
type CommentService interface {
GetCommentsForIssues(ctx context.Context, issueIds []string) (*entity.CommentEntities, error)
}
type commentService struct {
log logger.Logger
repositories *repository.Repositories
}
func NewCommentService(log logger.Logger, repositories *repository.Repositories) CommentService {
return &commentService{
log: log,
repositories: repositories,
}
}
func (s *commentService) GetCommentsForIssues(ctx context.Context, issueIds []string) (*entity.CommentEntities, error) {
span, ctx := opentracing.StartSpanFromContext(ctx, "CommentService.GetCommentsForIssues")
defer span.Finish()
tracing.SetDefaultServiceSpanTags(ctx, span)
span.LogFields(log.Object("issueIds", issueIds))
comments, err := s.repositories.Neo4jRepositories.CommentReadRepository.GetAllForIssues(ctx, common.GetTenantFromContext(ctx), issueIds)
if err != nil {
tracing.TraceErr(span, err)
return nil, err
}
commentEntities := entity.CommentEntities{}
for _, v := range comments {
commentEntity := s.mapDbNodeToCommentEntity(*v.Node)
commentEntity.DataloaderKey = v.LinkedNodeId
commentEntities = append(commentEntities, *commentEntity)
}
span.LogFields(log.Int("result count", len(commentEntities)))
return &commentEntities, nil
}
func (s *commentService) mapDbNodeToCommentEntity(node dbtype.Node) *entity.CommentEntity {
props := utils.GetPropsFromNode(node)
comment := entity.CommentEntity{
Id: utils.GetStringPropOrEmpty(props, "id"),
Content: utils.GetStringPropOrEmpty(props, "content"),
ContentType: utils.GetStringPropOrEmpty(props, "contentType"),
CreatedAt: utils.GetTimePropOrEpochStart(props, "createdAt"),
UpdatedAt: utils.GetTimePropOrEpochStart(props, "updatedAt"),
AppSource: utils.GetStringPropOrEmpty(props, "appSource"),
Source: neo4jentity.GetDataSource(utils.GetStringPropOrEmpty(props, "source")),
SourceOfTruth: neo4jentity.GetDataSource(utils.GetStringPropOrEmpty(props, "sourceOfTruth")),
}
return &comment
}