/
add_parent.go
49 lines (40 loc) · 1.82 KB
/
add_parent.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
package command_handler
import (
"context"
"github.com/openline-ai/openline-customer-os/packages/server/customer-os-common-module/validator"
"github.com/openline-ai/openline-customer-os/packages/server/events-processing-platform/domain/organization/aggregate"
"github.com/openline-ai/openline-customer-os/packages/server/events-processing-platform/domain/organization/command"
"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"
"github.com/opentracing/opentracing-go"
)
type AddParentCommandHandler interface {
Handle(ctx context.Context, command *command.AddParentCommand) error
}
type addParentCommandHandler struct {
log logger.Logger
es eventstore.AggregateStore
}
func NewAddParentCommandHandler(log logger.Logger, es eventstore.AggregateStore) AddParentCommandHandler {
return &addParentCommandHandler{log: log, es: es}
}
func (c *addParentCommandHandler) Handle(ctx context.Context, cmd *command.AddParentCommand) error {
span, ctx := opentracing.StartSpanFromContext(ctx, "addParentCommandHandler.Handle")
defer span.Finish()
tracing.SetCommandHandlerSpanTags(ctx, span, cmd.Tenant, cmd.LoggedInUserId)
tracing.LogObjectAsJson(span, "command", cmd)
validationError, done := validator.Validate(cmd, span)
if done {
return validationError
}
organizationAggregate, err := aggregate.LoadOrganizationAggregate(ctx, c.es, cmd.Tenant, cmd.ObjectID, *eventstore.NewLoadAggregateOptions())
if err != nil {
return err
}
if err = organizationAggregate.HandleCommand(ctx, cmd); err != nil {
tracing.TraceErr(span, err)
return err
}
return c.es.Save(ctx, organizationAggregate)
}