/
invocing_cycle_create_handler.go
65 lines (55 loc) · 2.63 KB
/
invocing_cycle_create_handler.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
package invoicing_cycle
import (
"context"
"github.com/openline-ai/openline-customer-os/packages/server/customer-os-common-module/utils"
commonAggregate "github.com/openline-ai/openline-customer-os/packages/server/events-processing-platform/domain/common/aggregate"
"github.com/openline-ai/openline-customer-os/packages/server/events-processing-platform/eventstore"
"github.com/openline-ai/openline-customer-os/packages/server/events-processing-platform/logger"
"github.com/openline-ai/openline-customer-os/packages/server/events-processing-platform/tracing"
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/pkg/errors"
)
type CreateInvoicingCycleHandler interface {
Handle(ctx context.Context, baseRequest eventstore.BaseRequest, request *invoicingcyclepb.CreateInvoicingCycleTypeRequest) error
}
type createInvoicingCycleHandler struct {
log logger.Logger
es eventstore.AggregateStore
}
func NewCreateInvoicingCycleHandler(log logger.Logger, es eventstore.AggregateStore) CreateInvoicingCycleHandler {
return &createInvoicingCycleHandler{log: log, es: es}
}
func (h *createInvoicingCycleHandler) Handle(ctx context.Context, baseRequest eventstore.BaseRequest, request *invoicingcyclepb.CreateInvoicingCycleTypeRequest) error {
span, ctx := opentracing.StartSpanFromContext(ctx, "CreateInvoicingCycleHandler.Handle")
defer span.Finish()
tracing.SetCommandHandlerSpanTags(ctx, span, baseRequest.Tenant, baseRequest.LoggedInUserId)
tracing.LogObjectAsJson(span, "common", baseRequest)
tracing.LogObjectAsJson(span, "request", request)
invoicingCycleAggregate, err := LoadInvoicingCycleAggregate(ctx, h.es, baseRequest.Tenant, baseRequest.ObjectID)
if err != nil {
tracing.TraceErr(span, err)
return err
}
createEvent, err := NewInvoicingCycleCreateEvent(invoicingCycleAggregate, string(InvoicingCycleType(request.Type).StringValue()), utils.TimestampProtoToTimePtr(request.CreatedAt), baseRequest.SourceFields)
if err != nil {
tracing.TraceErr(span, err)
return errors.Wrap(err, "NewInvoicingCycleCreateEvent")
}
commonAggregate.EnrichEventWithMetadataExtended(&createEvent, span, commonAggregate.EventMetadata{
Tenant: request.Tenant,
UserId: request.LoggedInUserId,
App: request.SourceFields.AppSource,
})
err = invoicingCycleAggregate.Apply(createEvent)
if err != nil {
tracing.TraceErr(span, err)
return err
}
// Persist the changes to the event store
if err = h.es.Save(ctx, invoicingCycleAggregate); err != nil {
tracing.TraceErr(span, err)
return err
}
return nil
}