-
Notifications
You must be signed in to change notification settings - Fork 12
/
invoicing_cycle_service.go
125 lines (111 loc) · 5.6 KB
/
invoicing_cycle_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
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"
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"
invoicingcyclepb "github.com/openline-ai/openline-customer-os/packages/server/events-processing-proto/gen/proto/go/api/grpc/v1/invoicing_cycle"
"github.com/opentracing/opentracing-go"
"github.com/opentracing/opentracing-go/log"
"github.com/pkg/errors"
)
type InvoicingCycleService interface {
CreateInvoicingCycle(ctx context.Context, invoicingCycleType invoicingcyclepb.InvoicingDateType) (string, error)
UpdateInvoicingCycle(ctx context.Context, id string, invoicingCycleType invoicingcyclepb.InvoicingDateType) error
GetInvoicingCycle(ctx context.Context) (*neo4jentity.InvoicingCycleEntity, error)
}
type invoicingCycleService struct {
log logger.Logger
repositories *repository.Repositories
grpcClients *grpc_client.Clients
}
func NewInvoicingCycleService(log logger.Logger, repositories *repository.Repositories, grpcClients *grpc_client.Clients) InvoicingCycleService {
return &invoicingCycleService{
log: log,
repositories: repositories,
grpcClients: grpcClients,
}
}
func (s *invoicingCycleService) CreateInvoicingCycle(ctx context.Context, invoicingCycleType invoicingcyclepb.InvoicingDateType) (string, error) {
span, ctx := opentracing.StartSpanFromContext(ctx, "InvoicingCycleService.CreateInvoicingCycle")
defer span.Finish()
tracing.SetDefaultServiceSpanTags(ctx, span)
span.LogFields(log.String("invoicingCycleType", string(invoicingCycleType)))
grpcRequest := invoicingcyclepb.CreateInvoicingCycleTypeRequest{
Tenant: common.GetTenantFromContext(ctx),
Type: invoicingCycleType,
LoggedInUserId: common.GetUserIdFromContext(ctx),
SourceFields: &commonpb.SourceFields{
Source: neo4jentity.DataSourceOpenline.String(),
AppSource: constants.AppSourceCustomerOsApi,
},
}
ctx = tracing.InjectSpanContextIntoGrpcMetadata(ctx, span)
response, err := utils.CallEventsPlatformGRPCWithRetry[*invoicingcyclepb.InvoicingCycleTypeResponse](func() (*invoicingcyclepb.InvoicingCycleTypeResponse, error) {
return s.grpcClients.InvoicingCycleClient.CreateInvoicingCycleType(ctx, &grpcRequest)
})
if err != nil {
tracing.TraceErr(span, err)
s.log.Errorf("Error from events processing: %s", err.Error())
return "", err
}
WaitForNodeCreatedInNeo4j(ctx, s.repositories, response.Id, neo4jutil.NodeLabelInvoicingCycle, span)
return response.Id, nil
}
func (s *invoicingCycleService) UpdateInvoicingCycle(ctx context.Context, id string, invoicingCycleType invoicingcyclepb.InvoicingDateType) error {
span, ctx := opentracing.StartSpanFromContext(ctx, "InvoicingCycleService.UpdateInvoicingCycle")
defer span.Finish()
tracing.SetDefaultServiceSpanTags(ctx, span)
span.SetTag(tracing.SpanTagEntityId, id)
span.LogFields(log.Object("invoicingCycleType", invoicingCycleType))
exists, err := s.repositories.Neo4jRepositories.CommonReadRepository.ExistsById(ctx, common.GetTenantFromContext(ctx), id, neo4jutil.NodeLabelInvoicingCycle)
if err != nil {
tracing.TraceErr(span, err)
return err
}
if !exists {
err = errors.New(fmt.Sprintf("Invoicing cycle with id {%s} not found", id))
tracing.TraceErr(span, err)
return err
}
grpcRequest := invoicingcyclepb.UpdateInvoicingCycleTypeRequest{
Tenant: common.GetTenantFromContext(ctx),
InvoicingCycleTypeId: id,
LoggedInUserId: common.GetUserIdFromContext(ctx),
Type: invoicingCycleType,
SourceFields: &commonpb.SourceFields{
AppSource: constants.AppSourceCustomerOsApi,
},
}
ctx = tracing.InjectSpanContextIntoGrpcMetadata(ctx, span)
_, err = utils.CallEventsPlatformGRPCWithRetry[*invoicingcyclepb.InvoicingCycleTypeResponse](func() (*invoicingcyclepb.InvoicingCycleTypeResponse, error) {
return s.grpcClients.InvoicingCycleClient.UpdateInvoicingCycleType(ctx, &grpcRequest)
})
if err != nil {
tracing.TraceErr(span, err)
s.log.Errorf("Error from events processing: %s", err.Error())
return err
}
return nil
}
func (s *invoicingCycleService) GetInvoicingCycle(ctx context.Context) (*neo4jentity.InvoicingCycleEntity, error) {
span, ctx := opentracing.StartSpanFromContext(ctx, "InvoicingCycleService.GetInvoicingCycle")
defer span.Finish()
tracing.SetDefaultServiceSpanTags(ctx, span)
if dbNode, err := s.repositories.Neo4jRepositories.InvoicingCycleReadRepository.GetInvoicingCycle(ctx, common.GetContext(ctx).Tenant); err != nil {
tracing.TraceErr(span, err)
wrappedErr := errors.Wrap(err, fmt.Sprintf("Invoicing cycle not found"))
return nil, wrappedErr
} else {
return neo4jmapper.MapDbNodeToInvoicingCycleEntity(dbNode), nil
}
}