/
commands.go
56 lines (49 loc) · 2.36 KB
/
commands.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 aggregate
import (
"context"
"github.com/openline-ai/openline-customer-os/packages/server/customer-os-common-module/utils"
"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/domain/interaction_session/command"
"github.com/openline-ai/openline-customer-os/packages/server/events-processing-platform/domain/interaction_session/event"
"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/tracing"
"github.com/opentracing/opentracing-go"
"github.com/opentracing/opentracing-go/log"
)
func (a *InteractionSessionAggregate) HandleCommand(ctx context.Context, cmd eventstore.Command) error {
span, ctx := opentracing.StartSpanFromContext(ctx, "InteractionSessionAggregate.HandleCommand")
defer span.Finish()
switch c := cmd.(type) {
case *command.UpsertInteractionSessionCommand:
if c.IsCreateCommand {
return a.createInteractionSession(ctx, c)
}
return nil
default:
tracing.TraceErr(span, eventstore.ErrInvalidCommandType)
return eventstore.ErrInvalidCommandType
}
}
func (a *InteractionSessionAggregate) createInteractionSession(ctx context.Context, cmd *command.UpsertInteractionSessionCommand) error {
span, _ := opentracing.StartSpanFromContext(ctx, "InteractionSessionAggregate.createInteractionSession")
defer span.Finish()
span.SetTag(tracing.SpanTagTenant, a.Tenant)
span.SetTag(tracing.SpanTagAggregateId, a.GetID())
span.SetTag(tracing.SpanTagEntityId, cmd.ObjectID)
span.LogFields(log.Int64("aggregateVersion", a.GetVersion()))
tracing.LogObjectAsJson(span, "command", cmd)
createdAtNotNil := utils.IfNotNilTimeWithDefault(cmd.CreatedAt, utils.Now())
updatedAtNotNil := utils.IfNotNilTimeWithDefault(cmd.UpdatedAt, createdAtNotNil)
cmd.Source.SetDefaultValues()
createEvent, err := event.NewInteractionSessionCreateEvent(a, cmd.DataFields, cmd.Source, cmd.ExternalSystem, createdAtNotNil, updatedAtNotNil)
if err != nil {
tracing.TraceErr(span, err)
return err
}
aggregate.EnrichEventWithMetadataExtended(&createEvent, span, aggregate.EventMetadata{
Tenant: a.Tenant,
UserId: cmd.GetLoggedInUserId(),
App: cmd.GetAppSource(),
})
return a.Apply(createEvent)
}