-
Notifications
You must be signed in to change notification settings - Fork 12
/
commands.go
148 lines (120 loc) · 6.09 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package aggregate
import (
"context"
"fmt"
"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/constants"
"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/location/command"
"github.com/openline-ai/openline-customer-os/packages/server/events-processing-platform/domain/location/events"
"github.com/openline-ai/openline-customer-os/packages/server/events-processing-platform/domain/location/models"
"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"
"github.com/pkg/errors"
)
func (a *LocationAggregate) HandleCommand(ctx context.Context, cmd eventstore.Command) error {
span, ctx := opentracing.StartSpanFromContext(ctx, "LocationAggregate.HandleCommand")
defer span.Finish()
switch c := cmd.(type) {
case *command.UpsertLocationCommand:
if c.IsCreateCommand {
return a.createLocation(ctx, c)
} else {
return a.updateLocation(ctx, c)
}
case *command.FailedLocationValidationCommand:
return a.failLocationValidation(ctx, c)
case *command.SkippedLocationValidationCommand:
return a.skipLocationValidation(ctx, c)
case *command.LocationValidatedCommand:
return a.locationValidated(ctx, c)
default:
tracing.TraceErr(span, eventstore.ErrInvalidCommandType)
return eventstore.ErrInvalidCommandType
}
}
func (a *LocationAggregate) createLocation(ctx context.Context, cmd *command.UpsertLocationCommand) error {
span, _ := opentracing.StartSpanFromContext(ctx, "LocationAggregate.createLocation")
defer span.Finish()
span.SetTag(tracing.SpanTagTenant, a.Tenant)
span.SetTag(tracing.SpanTagAggregateId, a.GetID())
span.LogFields(log.Int64("aggregateVersion", a.GetVersion()), log.String("command", fmt.Sprintf("%+v", cmd)))
createdAtNotNil := utils.IfNotNilTimeWithDefault(cmd.CreatedAt, utils.Now())
updatedAtNotNil := utils.IfNotNilTimeWithDefault(cmd.UpdatedAt, createdAtNotNil)
cmd.Source.SetDefaultValues()
locationAddress := models.LocationAddress{}
locationAddress.From(cmd.LocationAddressFields)
event, err := events.NewLocationCreateEvent(a, cmd.Name, cmd.RawAddress, cmd.Source, createdAtNotNil, updatedAtNotNil, locationAddress)
if err != nil {
tracing.TraceErr(span, err)
return errors.Wrap(err, "NewLocationCreateEvent")
}
aggregate.EnrichEventWithMetadata(&event, &span, a.Tenant, cmd.LoggedInUserId)
return a.Apply(event)
}
func (a *LocationAggregate) updateLocation(ctx context.Context, cmd *command.UpsertLocationCommand) error {
span, _ := opentracing.StartSpanFromContext(ctx, "LocationAggregate.updateLocation")
defer span.Finish()
span.SetTag(tracing.SpanTagTenant, a.Tenant)
span.SetTag(tracing.SpanTagAggregateId, a.GetID())
span.LogFields(log.Int64("aggregateVersion", a.GetVersion()), log.String("command", fmt.Sprintf("%+v", cmd)))
updatedAtNotNil := utils.IfNotNilTimeWithDefault(cmd.UpdatedAt, utils.Now())
if cmd.Source.Source == "" {
cmd.Source.Source = constants.SourceOpenline
}
locationAddress := models.LocationAddress{}
locationAddress.From(cmd.LocationAddressFields)
event, err := events.NewLocationUpdateEvent(a, cmd.Name, cmd.RawAddress, cmd.Source.Source, updatedAtNotNil, locationAddress)
if err != nil {
tracing.TraceErr(span, err)
return errors.Wrap(err, "NewLocationUpdateEvent")
}
aggregate.EnrichEventWithMetadata(&event, &span, a.Tenant, cmd.LoggedInUserId)
return a.Apply(event)
}
func (a *LocationAggregate) failLocationValidation(ctx context.Context, cmd *command.FailedLocationValidationCommand) error {
span, _ := opentracing.StartSpanFromContext(ctx, "LocationAggregate.failLocationValidation")
defer span.Finish()
span.SetTag(tracing.SpanTagTenant, a.Tenant)
span.SetTag(tracing.SpanTagAggregateId, a.GetID())
span.LogFields(log.Int64("aggregateVersion", a.GetVersion()), log.String("command", fmt.Sprintf("%+v", cmd)))
event, err := events.NewLocationFailedValidationEvent(a, cmd.RawAddress, cmd.Country, cmd.ValidationError)
if err != nil {
tracing.TraceErr(span, err)
return errors.Wrap(err, "NewLocationFailedValidationEvent")
}
aggregate.EnrichEventWithMetadata(&event, &span, a.Tenant, cmd.LoggedInUserId)
return a.Apply(event)
}
func (a *LocationAggregate) skipLocationValidation(ctx context.Context, cmd *command.SkippedLocationValidationCommand) error {
span, _ := opentracing.StartSpanFromContext(ctx, "LocationAggregate.SkipLocationValidation")
defer span.Finish()
span.SetTag(tracing.SpanTagTenant, a.Tenant)
span.SetTag(tracing.SpanTagAggregateId, a.GetID())
span.LogFields(log.Int64("aggregateVersion", a.GetVersion()), log.String("command", fmt.Sprintf("%+v", cmd)))
event, err := events.NewLocationSkippedValidationEvent(a, cmd.RawAddress, cmd.ValidationSkipReason)
if err != nil {
tracing.TraceErr(span, err)
return errors.Wrap(err, "NewLocationSkippedValidationEvent")
}
aggregate.EnrichEventWithMetadata(&event, &span, a.Tenant, cmd.LoggedInUserId)
return a.Apply(event)
}
func (a *LocationAggregate) locationValidated(ctx context.Context, cmd *command.LocationValidatedCommand) error {
span, _ := opentracing.StartSpanFromContext(ctx, "LocationAggregate.locationValidated")
defer span.Finish()
span.SetTag(tracing.SpanTagTenant, a.Tenant)
span.SetTag(tracing.SpanTagAggregateId, a.GetID())
span.LogFields(log.Int64("aggregateVersion", a.GetVersion()), log.String("command", fmt.Sprintf("%+v", cmd)))
locationAddress := models.LocationAddress{}
locationAddress.From(cmd.LocationAddressFields)
event, err := events.NewLocationValidatedEvent(a, cmd.RawAddress, cmd.CountryForValidation, locationAddress)
if err != nil {
tracing.TraceErr(span, err)
return errors.Wrap(err, "NewLocationValidatedEvent")
}
aggregate.EnrichEventWithMetadata(&event, &span, a.Tenant, cmd.LoggedInUserId)
return a.Apply(event)
}