-
Notifications
You must be signed in to change notification settings - Fork 12
/
link_location.go
49 lines (40 loc) · 1.84 KB
/
link_location.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 LinkLocationCommandHandler interface {
Handle(ctx context.Context, command *command.LinkLocationCommand) error
}
type linkLocationCommandHandler struct {
log logger.Logger
es eventstore.AggregateStore
}
func NewLinkLocationCommandHandler(log logger.Logger, es eventstore.AggregateStore) LinkLocationCommandHandler {
return &linkLocationCommandHandler{log: log, es: es}
}
func (c *linkLocationCommandHandler) Handle(ctx context.Context, cmd *command.LinkLocationCommand) error {
span, ctx := opentracing.StartSpanFromContext(ctx, "linkLocationCommandHandler.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)
}