-
Notifications
You must be signed in to change notification settings - Fork 12
/
commands.go
120 lines (99 loc) · 4.86 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
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/email/command"
"github.com/openline-ai/openline-customer-os/packages/server/events-processing-platform/domain/email/events"
"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 *EmailAggregate) HandleCommand(ctx context.Context, cmd eventstore.Command) error {
span, ctx := opentracing.StartSpanFromContext(ctx, "EmailAggregate.HandleCommand")
defer span.Finish()
switch c := cmd.(type) {
case *command.UpsertEmailCommand:
if c.IsCreateCommand {
return a.createEmail(ctx, c)
} else {
return a.updateEmail(ctx, c)
}
case *command.EmailValidatedCommand:
return a.emailValidated(ctx, c)
case *command.FailedEmailValidationCommand:
return a.failEmailValidation(ctx, c)
default:
tracing.TraceErr(span, eventstore.ErrInvalidCommandType)
return eventstore.ErrInvalidCommandType
}
}
func (a *EmailAggregate) createEmail(ctx context.Context, cmd *command.UpsertEmailCommand) error {
span, _ := opentracing.StartSpanFromContext(ctx, "EmailAggregate.createEmail")
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()
event, err := events.NewEmailCreateEvent(a, cmd.Tenant, cmd.RawEmail, cmd.Source, createdAtNotNil, updatedAtNotNil)
if err != nil {
tracing.TraceErr(span, err)
return errors.Wrap(err, "NewEmailCreateEvent")
}
aggregate.EnrichEventWithMetadata(&event, &span, a.Tenant, cmd.LoggedInUserId)
return a.Apply(event)
}
func (a *EmailAggregate) updateEmail(ctx context.Context, cmd *command.UpsertEmailCommand) error {
span, _ := opentracing.StartSpanFromContext(ctx, "EmailAggregate.updateEmail")
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
}
event, err := events.NewEmailUpdateEvent(a, cmd.RawEmail, cmd.Tenant, cmd.Source.Source, updatedAtNotNil)
if err != nil {
tracing.TraceErr(span, err)
return errors.Wrap(err, "NewEmailUpdateEvent")
}
aggregate.EnrichEventWithMetadata(&event, &span, a.Tenant, cmd.LoggedInUserId)
return a.Apply(event)
}
func (a *EmailAggregate) failEmailValidation(ctx context.Context, cmd *command.FailedEmailValidationCommand) error {
span, _ := opentracing.StartSpanFromContext(ctx, "EmailAggregate.updateEmail")
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.NewEmailFailedValidationEvent(a, cmd.Tenant, cmd.ValidationError)
if err != nil {
tracing.TraceErr(span, err)
return errors.Wrap(err, "NewEmailFailedValidationEvent")
}
aggregate.EnrichEventWithMetadata(&event, &span, a.Tenant, cmd.LoggedInUserId)
return a.Apply(event)
}
func (a *EmailAggregate) emailValidated(ctx context.Context, cmd *command.EmailValidatedCommand) error {
span, _ := opentracing.StartSpanFromContext(ctx, "EmailAggregate.emailValidated")
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.NewEmailValidatedEvent(a, cmd.Tenant, cmd.RawEmail, cmd.IsReachable, cmd.ValidationError,
cmd.Domain, cmd.Username, cmd.EmailAddress, cmd.AcceptsMail, cmd.CanConnectSmtp, cmd.HasFullInbox, cmd.IsCatchAll,
cmd.IsDeliverable, cmd.IsDisabled, cmd.IsValidSyntax)
if err != nil {
tracing.TraceErr(span, err)
return errors.Wrap(err, "NewEmailValidatedEvent")
}
aggregate.EnrichEventWithMetadata(&event, &span, a.Tenant, cmd.LoggedInUserId)
return a.Apply(event)
}