/
issue_service.go
193 lines (160 loc) · 8.28 KB
/
issue_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
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/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/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"
)
type IssueService interface {
GetIssueSummaryByStatusForOrganization(ctx context.Context, organizationId string) (map[string]int64, error)
GetById(ctx context.Context, issueId string) (*entity.IssueEntity, error)
GetIssuesForInteractionEvents(ctx context.Context, ids []string) (*entity.IssueEntities, error)
GetSubmitterParticipantsForIssues(ctx context.Context, ids []string) (*entity.IssueParticipants, error)
GetReporterParticipantsForIssues(ctx context.Context, ids []string) (*entity.IssueParticipants, error)
GetAssigneeParticipantsForIssues(ctx context.Context, ids []string) (*entity.IssueParticipants, error)
GetFollowerParticipantsForIssues(ctx context.Context, ids []string) (*entity.IssueParticipants, error)
mapDbNodeToIssue(node dbtype.Node) *entity.IssueEntity
}
type issueService struct {
log logger.Logger
repositories *repository.Repositories
services *Services
}
func NewIssueService(log logger.Logger, repositories *repository.Repositories, services *Services) IssueService {
return &issueService{
log: log,
repositories: repositories,
services: services,
}
}
func (s *issueService) GetIssueSummaryByStatusForOrganization(ctx context.Context, organizationId string) (map[string]int64, error) {
span, ctx := opentracing.StartSpanFromContext(ctx, "IssueService.GetIssueSummaryByStatusForOrganization")
defer span.Finish()
tracing.SetDefaultServiceSpanTags(ctx, span)
span.LogFields(log.String("organizationId", organizationId))
return s.repositories.IssueRepository.GetIssueCountByStatusForOrganization(ctx, common.GetTenantFromContext(ctx), organizationId)
}
func (s *issueService) GetById(ctx context.Context, issueId string) (*entity.IssueEntity, error) {
span, ctx := opentracing.StartSpanFromContext(ctx, "IssueService.GetById")
defer span.Finish()
tracing.SetDefaultServiceSpanTags(ctx, span)
span.LogFields(log.String("issueId", issueId))
if issueDbNode, err := s.repositories.IssueRepository.GetById(ctx, common.GetTenantFromContext(ctx), issueId); err != nil {
return nil, err
} else {
return s.mapDbNodeToIssue(*issueDbNode), nil
}
}
func (s *issueService) GetIssuesForInteractionEvents(ctx context.Context, ids []string) (*entity.IssueEntities, error) {
span, ctx := opentracing.StartSpanFromContext(ctx, "IssueService.GetIssuesForInteractionEvents")
defer span.Finish()
tracing.SetDefaultServiceSpanTags(ctx, span)
span.LogFields(log.Object("ids", ids))
issues, err := s.repositories.IssueRepository.GetAllForInteractionEvents(ctx, common.GetTenantFromContext(ctx), ids)
if err != nil {
return nil, err
}
issueEntities := make(entity.IssueEntities, 0, len(issues))
for _, v := range issues {
issueEntity := s.mapDbNodeToIssue(*v.Node)
issueEntity.DataloaderKey = v.LinkedNodeId
issueEntities = append(issueEntities, *issueEntity)
}
return &issueEntities, nil
}
func (s *issueService) GetSubmitterParticipantsForIssues(ctx context.Context, issueIds []string) (*entity.IssueParticipants, error) {
span, ctx := opentracing.StartSpanFromContext(ctx, "IssueService.GetSubmitterParticipantsForIssues")
defer span.Finish()
tracing.SetDefaultServiceSpanTags(ctx, span)
span.LogFields(log.Object("issueIds", issueIds))
records, err := s.repositories.IssueRepository.GetSubmitterParticipantsForIssues(ctx, common.GetTenantFromContext(ctx), issueIds)
if err != nil {
return nil, err
}
issueParticipants := s.convertDbNodesToIssueParticipants(records)
span.LogFields(log.Int("result count", len(issueParticipants)))
return &issueParticipants, nil
}
func (s *issueService) GetReporterParticipantsForIssues(ctx context.Context, issueIds []string) (*entity.IssueParticipants, error) {
span, ctx := opentracing.StartSpanFromContext(ctx, "IssueService.GetReporterParticipantsForIssues")
defer span.Finish()
tracing.SetDefaultServiceSpanTags(ctx, span)
span.LogFields(log.Object("issueIds", issueIds))
records, err := s.repositories.IssueRepository.GetReporterParticipantsForIssues(ctx, common.GetTenantFromContext(ctx), issueIds)
if err != nil {
return nil, err
}
issueParticipants := s.convertDbNodesToIssueParticipants(records)
span.LogFields(log.Int("result count", len(issueParticipants)))
return &issueParticipants, nil
}
func (s *issueService) GetAssigneeParticipantsForIssues(ctx context.Context, issueIds []string) (*entity.IssueParticipants, error) {
span, ctx := opentracing.StartSpanFromContext(ctx, "IssueService.GetAssigneeParticipantsForIssues")
defer span.Finish()
tracing.SetDefaultServiceSpanTags(ctx, span)
span.LogFields(log.Object("issueIds", issueIds))
records, err := s.repositories.IssueRepository.GetAssigneeParticipantsForIssues(ctx, common.GetTenantFromContext(ctx), issueIds)
if err != nil {
return nil, err
}
issueParticipants := s.convertDbNodesToIssueParticipants(records)
span.LogFields(log.Int("result count", len(issueParticipants)))
return &issueParticipants, nil
}
func (s *issueService) GetFollowerParticipantsForIssues(ctx context.Context, issueIds []string) (*entity.IssueParticipants, error) {
span, ctx := opentracing.StartSpanFromContext(ctx, "IssueService.GetFollowerParticipantsForIssues")
defer span.Finish()
tracing.SetDefaultServiceSpanTags(ctx, span)
span.LogFields(log.Object("issueIds", issueIds))
records, err := s.repositories.IssueRepository.GetFollowerParticipantsForIssues(ctx, common.GetTenantFromContext(ctx), issueIds)
if err != nil {
return nil, err
}
issueParticipants := s.convertDbNodesToIssueParticipants(records)
span.LogFields(log.Int("result count", len(issueParticipants)))
return &issueParticipants, nil
}
func (s *issueService) mapDbNodeToIssue(node dbtype.Node) *entity.IssueEntity {
props := utils.GetPropsFromNode(node)
issue := entity.IssueEntity{
Id: utils.GetStringPropOrEmpty(props, "id"),
CreatedAt: utils.GetTimePropOrNow(props, "createdAt"),
UpdatedAt: utils.GetTimePropOrNow(props, "updatedAt"),
Subject: utils.GetStringPropOrEmpty(props, "subject"),
Status: utils.GetStringPropOrEmpty(props, "status"),
Priority: utils.GetStringPropOrEmpty(props, "priority"),
Description: utils.GetStringPropOrEmpty(props, "description"),
Source: neo4jentity.GetDataSource(utils.GetStringPropOrEmpty(props, "source")),
SourceOfTruth: neo4jentity.GetDataSource(utils.GetStringPropOrEmpty(props, "sourceOfTruth")),
AppSource: utils.GetStringPropOrEmpty(props, "appSource"),
}
return &issue
}
func (s *issueService) convertDbNodesToIssueParticipants(records []*utils.DbNodeAndId) entity.IssueParticipants {
issueParticipants := entity.IssueParticipants{}
for _, v := range records {
if slices.Contains(v.Node.Labels, neo4jutil.NodeLabelUser) {
participant := s.services.UserService.mapDbNodeToUserEntity(*v.Node)
participant.DataloaderKey = v.LinkedNodeId
issueParticipants = append(issueParticipants, participant)
} else if slices.Contains(v.Node.Labels, neo4jutil.NodeLabelContact) {
participant := s.services.ContactService.mapDbNodeToContactEntity(*v.Node)
participant.DataloaderKey = v.LinkedNodeId
issueParticipants = append(issueParticipants, participant)
} else if slices.Contains(v.Node.Labels, neo4jutil.NodeLabelOrganization) {
participant := s.services.OrganizationService.mapDbNodeToOrganizationEntity(*v.Node)
participant.DataloaderKey = v.LinkedNodeId
issueParticipants = append(issueParticipants, participant)
}
}
return issueParticipants
}