-
Notifications
You must be signed in to change notification settings - Fork 12
/
update_organization_owner.go
122 lines (104 loc) · 4.85 KB
/
update_organization_owner.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
package command_handler
import (
"context"
"fmt"
"time"
"github.com/openline-ai/openline-customer-os/packages/server/customer-os-common-module/utils"
"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/config"
commonaggregate "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/organization/aggregate"
"github.com/openline-ai/openline-customer-os/packages/server/events-processing-platform/domain/organization/command"
"github.com/openline-ai/openline-customer-os/packages/server/events-processing-platform/domain/organization/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/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"
"github.com/pkg/errors"
)
type UpdateOrganizationOwnerCommandHandler interface {
Handle(ctx context.Context, command *command.UpdateOrganizationOwnerCommand) error
}
type updateOrganizationOwnerCommandHandler struct {
log logger.Logger
es eventstore.AggregateStore
ebs *eventstore.EventBufferService
cfg config.Utils
}
func NewUpdateOrganizationOwnerCommandHandler(log logger.Logger, es eventstore.AggregateStore, cfg config.Utils, ebs *eventstore.EventBufferService) UpdateOrganizationOwnerCommandHandler {
return &updateOrganizationOwnerCommandHandler{log: log, es: es, cfg: cfg, ebs: ebs}
}
func (h *updateOrganizationOwnerCommandHandler) Handle(ctx context.Context, cmd *command.UpdateOrganizationOwnerCommand) error {
span, ctx := opentracing.StartSpanFromContext(ctx, "UpdateOrganizationOwnerCommand.Handle")
defer span.Finish()
tracing.SetCommandHandlerSpanTags(ctx, span, cmd.Tenant, cmd.LoggedInUserId)
tracing.LogObjectAsJson(span, "command", cmd)
validationError, done := validator.Validate(cmd, span)
if done {
return validationError
}
for attempt := 0; attempt == 0 || attempt < h.cfg.RetriesOnOptimisticLockException; attempt++ {
organizationAggregate, err := aggregate.LoadOrganizationAggregate(ctx, h.es, cmd.Tenant, cmd.ObjectID, *eventstore.NewLoadAggregateOptions())
if err != nil {
return err
}
if err = organizationAggregate.HandleCommand(ctx, cmd); err != nil {
tracing.TraceErr(span, err)
return err
}
// create notification event Park it in the event buffer for notifications
event, err := createNotificationEvent(ctx, cmd, organizationAggregate)
if err != nil {
tracing.TraceErr(span, err)
return err
}
eventBufferUUID := fmt.Sprintf("%s-%s", cmd.ActorUserId, cmd.OrganizationId)
err = h.ebs.Park(*event, cmd.Tenant, eventBufferUUID, time.Now().UTC().Add(time.Second*30))
if err != nil {
tracing.TraceErr(span, err)
return err
}
// Save the aggregate to the event store
err = h.es.Save(ctx, organizationAggregate)
if err == nil {
return nil // Save successful
}
if eventstore.IsEventStoreErrorCodeWrongExpectedVersion(err) {
// Handle concurrency error
if attempt == h.cfg.RetriesOnOptimisticLockException-1 {
// If we have reached the maximum number of retries, return an error
tracing.TraceErr(span, err)
return err
}
span.LogFields(log.Int("retryAttempt", attempt+1))
time.Sleep(utils.BackOffExponentialDelay(attempt)) // backoffDelay is a function that increases the delay with each attempt
continue // Retry
} else {
// Some other error occurred
tracing.TraceErr(span, errors.Wrap(err, "failed to save organization aggregate"))
return err
}
}
err := errors.New("reached maximum number of retries")
tracing.TraceErr(span, err)
return err
}
func createNotificationEvent(ctx context.Context, cmd *command.UpdateOrganizationOwnerCommand, aggregate *aggregate.OrganizationAggregate) (*eventstore.Event, error) {
span, ctx := opentracing.StartSpanFromContext(ctx, "createNotificationEvent")
defer span.Finish()
tracing.SetCommandHandlerSpanTags(ctx, span, cmd.Tenant, cmd.LoggedInUserId)
tracing.LogObjectAsJson(span, "command", cmd)
updatedAt := utils.Now()
event, err := events.NewOrganizationOwnerUpdateNotificationEvent(aggregate, cmd.OwnerUserId, cmd.ActorUserId, cmd.OrganizationId, updatedAt)
if err != nil {
tracing.TraceErr(span, err)
return nil, errors.Wrap(err, "NewOrganizationOwnerUpdateNotificationEvent")
}
commonaggregate.EnrichEventWithMetadataExtended(&event, span, commonaggregate.EventMetadata{
Tenant: aggregate.GetTenant(),
UserId: cmd.LoggedInUserId,
App: cmd.AppSource,
})
return &event, nil
}