-
Notifications
You must be signed in to change notification settings - Fork 12
/
commands.go
197 lines (169 loc) · 8.18 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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
package aggregate
import (
"context"
"github.com/openline-ai/openline-customer-os/packages/server/customer-os-common-module/utils"
neo4jenum "github.com/openline-ai/openline-customer-os/packages/server/customer-os-neo4j-repository/enum"
"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/opportunity/command"
"github.com/openline-ai/openline-customer-os/packages/server/events-processing-platform/domain/opportunity/event"
"github.com/openline-ai/openline-customer-os/packages/server/events-processing-platform/domain/opportunity/model"
"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"
)
// HandleCommand processes commands and applies the resulting events to the aggregate.
func (a *OpportunityAggregate) HandleCommand(ctx context.Context, cmd eventstore.Command) error {
span, ctx := opentracing.StartSpanFromContext(ctx, "OpportunityAggregate.HandleCommand")
defer span.Finish()
switch c := cmd.(type) {
case *command.CreateOpportunityCommand:
return a.createOpportunity(ctx, c)
case *command.UpdateRenewalOpportunityNextCycleDateCommand:
return a.updateRenewalOpportunityNextCycleDate(ctx, c)
case *command.UpdateOpportunityCommand:
return a.updateOpportunity(ctx, c)
case *command.CloseWinOpportunityCommand:
return a.closeWinOpportunity(ctx, c)
case *command.CloseLooseOpportunityCommand:
return a.closeLooseOpportunity(ctx, c)
default:
tracing.TraceErr(span, eventstore.ErrInvalidCommandType)
return eventstore.ErrInvalidCommandType
}
}
func (a *OpportunityAggregate) createOpportunity(ctx context.Context, cmd *command.CreateOpportunityCommand) error {
span, _ := opentracing.StartSpanFromContext(ctx, "OpportunityAggregate.createOpportunity")
defer span.Finish()
span.SetTag(tracing.SpanTagTenant, a.Tenant)
span.SetTag(tracing.SpanTagAggregateId, a.GetID())
span.LogFields(log.Int64("aggregateVersion", a.GetVersion()), log.Object("command", cmd))
createdAtNotNil := utils.IfNotNilTimeWithDefault(cmd.CreatedAt, utils.Now())
updatedAtNotNil := utils.IfNotNilTimeWithDefault(cmd.UpdatedAt, createdAtNotNil)
cmd.Source.SetDefaultValues()
createEvent, err := event.NewOpportunityCreateEvent(a, cmd.DataFields, cmd.Source, cmd.ExternalSystem, createdAtNotNil, updatedAtNotNil)
if err != nil {
tracing.TraceErr(span, err)
return errors.Wrap(err, "NewOpportunityCreateEvent")
}
aggregate.EnrichEventWithMetadataExtended(&createEvent, span, aggregate.EventMetadata{
Tenant: a.Tenant,
UserId: cmd.LoggedInUserId,
App: cmd.Source.AppSource,
})
return a.Apply(createEvent)
}
func (a *OpportunityAggregate) updateRenewalOpportunityNextCycleDate(ctx context.Context, cmd *command.UpdateRenewalOpportunityNextCycleDateCommand) error {
span, _ := opentracing.StartSpanFromContext(ctx, "OpportunityAggregate.updateRenewalOpportunityNextCycleDate")
defer span.Finish()
span.SetTag(tracing.SpanTagTenant, a.Tenant)
span.SetTag(tracing.SpanTagAggregateId, a.GetID())
span.LogFields(log.Int64("aggregateVersion", a.GetVersion()), log.Object("command", cmd))
updatedAtNotNil := utils.IfNotNilTimeWithDefault(cmd.UpdatedAt, utils.Now())
// skip if no changes on aggregate
if a.Opportunity.RenewalDetails.RenewedAt != nil &&
cmd.RenewedAt != nil &&
a.Opportunity.RenewalDetails.RenewedAt.Equal(*cmd.RenewedAt) {
return nil
}
// if opportunity is not renewal or status is closed, return error
if a.Opportunity.InternalType != neo4jenum.OpportunityInternalTypeRenewal.String() {
err := errors.New(constants.Validate + ": Opportunity is not renewal")
tracing.TraceErr(span, err)
return err
} else if a.Opportunity.InternalStage != neo4jenum.OpportunityInternalStageOpen.String() {
err := errors.New(constants.Validate + ": Opportunity is closed")
tracing.TraceErr(span, err)
return err
}
updateRenewalNextCycleDateEvent, err := event.NewOpportunityUpdateNextCycleDateEvent(a, updatedAtNotNil, cmd.RenewedAt)
if err != nil {
tracing.TraceErr(span, err)
return errors.Wrap(err, "NewOpportunityUpdateRenewalNextCycleDateEvent")
}
aggregate.EnrichEventWithMetadataExtended(&updateRenewalNextCycleDateEvent, span, aggregate.EventMetadata{
Tenant: a.Tenant,
UserId: cmd.LoggedInUserId,
App: cmd.AppSource,
})
return a.Apply(updateRenewalNextCycleDateEvent)
}
func (a *OpportunityAggregate) updateOpportunity(ctx context.Context, cmd *command.UpdateOpportunityCommand) error {
span, _ := opentracing.StartSpanFromContext(ctx, "OpportunityAggregate.updateOpportunity")
defer span.Finish()
span.SetTag(tracing.SpanTagTenant, a.Tenant)
span.SetTag(tracing.SpanTagAggregateId, a.GetID())
span.LogFields(log.Int64("aggregateVersion", a.GetVersion()), log.Object("command", cmd))
updatedAtNotNil := utils.IfNotNilTimeWithDefault(cmd.UpdatedAt, utils.Now())
cmd.Source.SetDefaultValues()
// skip if no changes on aggregate
// case 1 skip if only amount to be updated, but no changes
if cmd.FieldMaskContainsOnly([]string{model.FieldMaskAmount, model.FieldMaskMaxAmount}) &&
a.Opportunity.Amount == cmd.DataFields.Amount &&
a.Opportunity.MaxAmount == cmd.DataFields.MaxAmount {
return nil
}
updateEvent, err := event.NewOpportunityUpdateEvent(a, cmd.DataFields, cmd.Source.Source, cmd.ExternalSystem, updatedAtNotNil, cmd.FieldsMask)
if err != nil {
tracing.TraceErr(span, err)
return errors.Wrap(err, "NewOpportunityUpdateEvent")
}
aggregate.EnrichEventWithMetadataExtended(&updateEvent, span, aggregate.EventMetadata{
Tenant: a.Tenant,
UserId: cmd.LoggedInUserId,
App: cmd.Source.AppSource,
})
return a.Apply(updateEvent)
}
func (a *OpportunityAggregate) closeWinOpportunity(ctx context.Context, cmd *command.CloseWinOpportunityCommand) error {
span, _ := opentracing.StartSpanFromContext(ctx, "OpportunityAggregate.closeWinOpportunity")
defer span.Finish()
span.SetTag(tracing.SpanTagTenant, a.Tenant)
span.SetTag(tracing.SpanTagAggregateId, a.GetID())
span.LogFields(log.Int64("aggregateVersion", a.GetVersion()), log.Object("command", cmd))
// skip if opportunity is already closed won
if a.Opportunity.InternalStage == neo4jenum.OpportunityInternalStageClosedWon.String() {
return nil
}
now := utils.Now()
updatedAtNotNil := utils.IfNotNilTimeWithDefault(cmd.UpdatedAt, now)
closedAtNotNil := utils.IfNotNilTimeWithDefault(cmd.ClosedAt, now)
closeWinEvent, err := event.NewOpportunityCloseWinEvent(a, updatedAtNotNil, closedAtNotNil)
if err != nil {
tracing.TraceErr(span, err)
return errors.Wrap(err, "NewOpportunityCloseWinEvent")
}
aggregate.EnrichEventWithMetadataExtended(&closeWinEvent, span, aggregate.EventMetadata{
Tenant: a.Tenant,
UserId: cmd.LoggedInUserId,
App: cmd.AppSource,
})
return a.Apply(closeWinEvent)
}
func (a *OpportunityAggregate) closeLooseOpportunity(ctx context.Context, cmd *command.CloseLooseOpportunityCommand) error {
span, _ := opentracing.StartSpanFromContext(ctx, "OpportunityAggregate.closeLooseOpportunity")
defer span.Finish()
span.SetTag(tracing.SpanTagTenant, a.Tenant)
span.SetTag(tracing.SpanTagAggregateId, a.GetID())
span.LogFields(log.Int64("aggregateVersion", a.GetVersion()), log.Object("command", cmd))
// skip if opportunity is already closed lost
if a.Opportunity.InternalStage == neo4jenum.OpportunityInternalStageClosedLost.String() {
return nil
}
now := utils.Now()
updatedAtNotNil := utils.IfNotNilTimeWithDefault(cmd.UpdatedAt, now)
closedAtNotNil := utils.IfNotNilTimeWithDefault(cmd.ClosedAt, now)
closeLooseEvent, err := event.NewOpportunityCloseLooseEvent(a, updatedAtNotNil, closedAtNotNil)
if err != nil {
tracing.TraceErr(span, err)
return errors.Wrap(err, "NewOpportunityCloseLooseEvent")
}
aggregate.EnrichEventWithMetadataExtended(&closeLooseEvent, span, aggregate.EventMetadata{
Tenant: a.Tenant,
UserId: cmd.LoggedInUserId,
App: cmd.AppSource,
})
return a.Apply(closeLooseEvent)
}