-
Notifications
You must be signed in to change notification settings - Fork 12
/
invoicing_cycle_update_handler.go
65 lines (55 loc) · 2.63 KB
/
invoicing_cycle_update_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 UpdateInvoicingCycleHandler interface {
Handle(ctx context.Context, baseRequest eventstore.BaseRequest, request *invoicingcyclepb.UpdateInvoicingCycleTypeRequest) error
}
type updateInvoicingCycleHandler struct {
log logger.Logger
es eventstore.AggregateStore
}
func NewUpdateInvoicingCycleHandler(log logger.Logger, es eventstore.AggregateStore) UpdateInvoicingCycleHandler {
return &updateInvoicingCycleHandler{log: log, es: es}
}
func (h *updateInvoicingCycleHandler) Handle(ctx context.Context, baseRequest eventstore.BaseRequest, request *invoicingcyclepb.UpdateInvoicingCycleTypeRequest) error {
span, ctx := opentracing.StartSpanFromContext(ctx, "UpdateInvoicingCycleHandler.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
}
updateEvent, err := NewInvoicingCycleUpdateEvent(invoicingCycleAggregate, string(InvoicingCycleType(request.Type).StringValue()), utils.TimestampProtoToTimePtr(request.UpdatedAt), baseRequest.SourceFields)
if err != nil {
tracing.TraceErr(span, err)
return errors.Wrap(err, "NewInvoicingCycleCreateEvent")
}
commonAggregate.EnrichEventWithMetadataExtended(&updateEvent, span, commonAggregate.EventMetadata{
Tenant: request.Tenant,
UserId: request.LoggedInUserId,
App: request.SourceFields.AppSource,
})
err = invoicingCycleAggregate.Apply(updateEvent)
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
}