-
Notifications
You must be signed in to change notification settings - Fork 12
/
add_tag.go
56 lines (47 loc) · 2.02 KB
/
add_tag.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
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/log_entry/aggregate"
cmd "github.com/openline-ai/openline-customer-os/packages/server/events-processing-platform/domain/log_entry/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"
"github.com/opentracing/opentracing-go/log"
)
type AddTagCommandHandler interface {
Handle(ctx context.Context, command *cmd.AddTagCommand) error
}
type addTagCommandHandler struct {
log logger.Logger
es eventstore.AggregateStore
}
func NewAddTagCommandHandler(log logger.Logger, es eventstore.AggregateStore) AddTagCommandHandler {
return &addTagCommandHandler{log: log, es: es}
}
func (c *addTagCommandHandler) Handle(ctx context.Context, command *cmd.AddTagCommand) error {
span, ctx := opentracing.StartSpanFromContext(ctx, "AddTagCommandHandler.Handle")
defer span.Finish()
tracing.SetCommandHandlerSpanTags(ctx, span, command.Tenant, command.LoggedInUserId)
span.LogFields(log.String("ObjectID", command.ObjectID))
if err := validator.GetValidator().Struct(command); err != nil {
tracing.TraceErr(span, err)
return err
}
logEntryAggregate, err := aggregate.LoadLogEntryAggregate(ctx, c.es, command.Tenant, command.ObjectID)
if err != nil {
tracing.TraceErr(span, err)
return err
}
if eventstore.IsAggregateNotFound(logEntryAggregate) {
tracing.TraceErr(span, eventstore.ErrAggregateNotFound)
return eventstore.ErrAggregateNotFound
} else {
if err = logEntryAggregate.HandleCommand(ctx, command); err != nil {
tracing.TraceErr(span, err)
return err
}
}
return c.es.Save(ctx, logEntryAggregate)
}