-
Notifications
You must be signed in to change notification settings - Fork 12
/
opportunity_service.go
236 lines (211 loc) · 11.1 KB
/
opportunity_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
package service
import (
"context"
"fmt"
"github.com/openline-ai/openline-customer-os/packages/server/customer-os-api/constants"
"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/grpc_client"
"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"
neo4jenum "github.com/openline-ai/openline-customer-os/packages/server/customer-os-neo4j-repository/enum"
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"
commonpb "github.com/openline-ai/openline-customer-os/packages/server/events-processing-proto/gen/proto/go/api/grpc/v1/common"
opportunitypb "github.com/openline-ai/openline-customer-os/packages/server/events-processing-proto/gen/proto/go/api/grpc/v1/opportunity"
"github.com/opentracing/opentracing-go"
"github.com/opentracing/opentracing-go/log"
"github.com/pkg/errors"
)
type OpportunityService interface {
Update(ctx context.Context, opportunity *neo4jentity.OpportunityEntity) error
UpdateRenewal(ctx context.Context, opportunityId string, renewalLikelihood neo4jenum.RenewalLikelihood, amount *float64, comments *string, ownerUserId *string, adjustedRate *int64, appSource string) error
GetById(ctx context.Context, id string) (*neo4jentity.OpportunityEntity, error)
GetOpportunitiesForContracts(ctx context.Context, contractIds []string) (*neo4jentity.OpportunityEntities, error)
UpdateRenewalsForOrganization(ctx context.Context, organizationId string, renewalLikelihood neo4jenum.RenewalLikelihood, renewalAdjustedRate *int64) error
}
type opportunityService struct {
log logger.Logger
repositories *repository.Repositories
grpcClients *grpc_client.Clients
services *Services
}
func NewOpportunityService(log logger.Logger, repositories *repository.Repositories, grpcClients *grpc_client.Clients, services *Services) OpportunityService {
return &opportunityService{
log: log,
repositories: repositories,
grpcClients: grpcClients,
services: services,
}
}
func (s *opportunityService) GetById(ctx context.Context, opportunityId string) (*neo4jentity.OpportunityEntity, error) {
span, ctx := opentracing.StartSpanFromContext(ctx, "OpportunityService.GetById")
defer span.Finish()
tracing.SetDefaultServiceSpanTags(ctx, span)
span.LogFields(log.String("opportunityId", opportunityId))
if opportunityDbNode, err := s.repositories.Neo4jRepositories.OpportunityReadRepository.GetOpportunityById(ctx, common.GetContext(ctx).Tenant, opportunityId); err != nil {
tracing.TraceErr(span, err)
wrappedErr := errors.Wrap(err, fmt.Sprintf("opportunity with id {%s} not found", opportunityId))
return nil, wrappedErr
} else {
return neo4jmapper.MapDbNodeToOpportunityEntity(opportunityDbNode), nil
}
}
func (s *opportunityService) GetOpportunitiesForContracts(ctx context.Context, contractIDs []string) (*neo4jentity.OpportunityEntities, error) {
span, ctx := opentracing.StartSpanFromContext(ctx, "OpportunityService.GetOpportunitiesForContracts")
defer span.Finish()
span.LogFields(log.Object("contractIDs", contractIDs))
opportunities, err := s.repositories.Neo4jRepositories.OpportunityReadRepository.GetForContracts(ctx, common.GetTenantFromContext(ctx), contractIDs)
if err != nil {
return nil, err
}
opportunityEntities := make(neo4jentity.OpportunityEntities, 0, len(opportunities))
for _, v := range opportunities {
opportunityEntity := neo4jmapper.MapDbNodeToOpportunityEntity(v.Node)
opportunityEntity.DataloaderKey = v.LinkedNodeId
opportunityEntities = append(opportunityEntities, *opportunityEntity)
}
return &opportunityEntities, nil
}
func (s *opportunityService) Update(ctx context.Context, opportunity *neo4jentity.OpportunityEntity) error {
span, ctx := opentracing.StartSpanFromContext(ctx, "OpportunityService.Update")
defer span.Finish()
tracing.SetDefaultServiceSpanTags(ctx, span)
span.LogFields(log.Object("opportunity", opportunity))
if opportunity == nil {
err := fmt.Errorf("(OpportunityService.Update) opportunity entity is nil")
s.log.Error(err.Error())
tracing.TraceErr(span, err)
return err
} else if opportunity.Id == "" {
err := fmt.Errorf("(OpportunityService.Update) opportunity id is missing")
s.log.Error(err.Error())
tracing.TraceErr(span, err)
return err
}
opportunityExists, _ := s.repositories.Neo4jRepositories.CommonReadRepository.ExistsById(ctx, common.GetTenantFromContext(ctx), opportunity.Id, neo4jutil.NodeLabelOpportunity)
if !opportunityExists {
err := fmt.Errorf("(OpportunityService.Update) opportunity with id {%s} not found", opportunity.Id)
s.log.Error(err.Error())
tracing.TraceErr(span, err)
return err
}
opportunityUpdateRequest := opportunitypb.UpdateOpportunityGrpcRequest{
Tenant: common.GetTenantFromContext(ctx),
Id: opportunity.Id,
LoggedInUserId: common.GetUserIdFromContext(ctx),
Name: opportunity.Name,
Amount: opportunity.Amount,
ExternalType: opportunity.ExternalType,
ExternalStage: opportunity.ExternalStage,
GeneralNotes: opportunity.GeneralNotes,
NextSteps: opportunity.NextSteps,
EstimatedCloseDate: utils.ConvertTimeToTimestampPtr(opportunity.EstimatedClosedAt),
SourceFields: &commonpb.SourceFields{
Source: string(opportunity.Source),
AppSource: utils.StringFirstNonEmpty(opportunity.AppSource, constants.AppSourceCustomerOsApi),
},
}
ctx = tracing.InjectSpanContextIntoGrpcMetadata(ctx, span)
_, err := utils.CallEventsPlatformGRPCWithRetry[*opportunitypb.OpportunityIdGrpcResponse](func() (*opportunitypb.OpportunityIdGrpcResponse, error) {
return s.grpcClients.OpportunityClient.UpdateOpportunity(ctx, &opportunityUpdateRequest)
})
if err != nil {
tracing.TraceErr(span, err)
s.log.Errorf("Error from events processing: %s", err.Error())
return err
}
return nil
}
func (s *opportunityService) UpdateRenewal(ctx context.Context, opportunityId string, renewalLikelihood neo4jenum.RenewalLikelihood, amount *float64, comments *string, ownerUserId *string, adjustedRate *int64, appSource string) error {
span, ctx := opentracing.StartSpanFromContext(ctx, "OpportunityService.UpdateRenewal")
defer span.Finish()
tracing.SetDefaultServiceSpanTags(ctx, span)
span.LogFields(log.String("opportunityId", opportunityId), log.Object("renewalLikelihood", renewalLikelihood), log.Object("amount", amount), log.Object("comments", comments), log.String("appSource", appSource))
if opportunityId == "" {
err := fmt.Errorf("(OpportunityService.UpdateRenewal) opportunity id is missing")
s.log.Error(err.Error())
tracing.TraceErr(span, err)
return err
}
opportunityExists, _ := s.repositories.Neo4jRepositories.CommonReadRepository.ExistsById(ctx, common.GetTenantFromContext(ctx), opportunityId, neo4jutil.NodeLabelOpportunity)
if !opportunityExists {
err := fmt.Errorf("(OpportunityService.UpdateRenewal) opportunity with id {%s} not found", opportunityId)
s.log.Error(err.Error())
tracing.TraceErr(span, err)
return err
}
fieldsMask := make([]opportunitypb.OpportunityMaskField, 0)
opportunityRenewalUpdateRequest := opportunitypb.UpdateRenewalOpportunityGrpcRequest{
Tenant: common.GetTenantFromContext(ctx),
Id: opportunityId,
LoggedInUserId: common.GetUserIdFromContext(ctx),
OwnerUserId: utils.IfNotNilString(ownerUserId),
SourceFields: &commonpb.SourceFields{
Source: string(neo4jentity.DataSourceOpenline),
AppSource: appSource,
},
}
if amount != nil {
opportunityRenewalUpdateRequest.Amount = *amount
fieldsMask = append(fieldsMask, opportunitypb.OpportunityMaskField_OPPORTUNITY_PROPERTY_AMOUNT)
}
if comments != nil {
opportunityRenewalUpdateRequest.Comments = *comments
fieldsMask = append(fieldsMask, opportunitypb.OpportunityMaskField_OPPORTUNITY_PROPERTY_COMMENTS)
}
if adjustedRate != nil {
opportunityRenewalUpdateRequest.RenewalAdjustedRate = *adjustedRate
fieldsMask = append(fieldsMask, opportunitypb.OpportunityMaskField_OPPORTUNITY_PROPERTY_ADJUSTED_RATE)
}
switch renewalLikelihood {
case neo4jenum.RenewalLikelihoodHigh:
opportunityRenewalUpdateRequest.RenewalLikelihood = opportunitypb.RenewalLikelihood_HIGH_RENEWAL
case neo4jenum.RenewalLikelihoodMedium:
opportunityRenewalUpdateRequest.RenewalLikelihood = opportunitypb.RenewalLikelihood_MEDIUM_RENEWAL
case neo4jenum.RenewalLikelihoodLow:
opportunityRenewalUpdateRequest.RenewalLikelihood = opportunitypb.RenewalLikelihood_LOW_RENEWAL
case neo4jenum.RenewalLikelihoodZero:
opportunityRenewalUpdateRequest.RenewalLikelihood = opportunitypb.RenewalLikelihood_ZERO_RENEWAL
default:
opportunityRenewalUpdateRequest.RenewalLikelihood = opportunitypb.RenewalLikelihood_ZERO_RENEWAL
}
fieldsMask = append(fieldsMask, opportunitypb.OpportunityMaskField_OPPORTUNITY_PROPERTY_RENEWAL_LIKELIHOOD)
opportunityRenewalUpdateRequest.FieldsMask = fieldsMask
ctx = tracing.InjectSpanContextIntoGrpcMetadata(ctx, span)
_, err := utils.CallEventsPlatformGRPCWithRetry[*opportunitypb.OpportunityIdGrpcResponse](func() (*opportunitypb.OpportunityIdGrpcResponse, error) {
return s.grpcClients.OpportunityClient.UpdateRenewalOpportunity(ctx, &opportunityRenewalUpdateRequest)
})
if err != nil {
tracing.TraceErr(span, err)
s.log.Errorf("Error from events processing: %s", err.Error())
return err
}
return nil
}
func (s *opportunityService) UpdateRenewalsForOrganization(ctx context.Context, organizationId string, renewalLikelihood neo4jenum.RenewalLikelihood, renewalAdjustedRate *int64) error {
span, ctx := opentracing.StartSpanFromContext(ctx, "OpportunityService.UpdateRenewalsForOrganization")
defer span.Finish()
tracing.SetDefaultServiceSpanTags(ctx, span)
span.LogFields(log.String("organizationId", organizationId), log.String("renewalLikelihood", renewalLikelihood.String()), log.Object("renewalAdjustedRate", renewalAdjustedRate))
_, err := s.services.OrganizationService.GetById(ctx, organizationId)
if err != nil {
tracing.TraceErr(span, err)
return err
}
opportunityDbNodes, err := s.repositories.Neo4jRepositories.OpportunityReadRepository.GetActiveRenewalOpportunitiesForOrganization(ctx, common.GetTenantFromContext(ctx), organizationId)
if err != nil {
tracing.TraceErr(span, err)
return err
}
for _, opportunityDbNode := range opportunityDbNodes {
opportunity := neo4jmapper.MapDbNodeToOpportunityEntity(opportunityDbNode)
if err := s.UpdateRenewal(ctx, opportunity.Id, renewalLikelihood, nil, nil, nil, renewalAdjustedRate, constants.AppSourceCustomerOsApi); err != nil {
tracing.TraceErr(span, err)
return err
}
}
return nil
}