-
Notifications
You must be signed in to change notification settings - Fork 19
/
revise_order_orchestration_saga.go
123 lines (95 loc) · 3.86 KB
/
revise_order_orchestration_saga.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
package adapters
import (
"context"
"github.com/stackus/edat/core"
"github.com/stackus/edat/msg"
"github.com/stackus/edat/saga"
"github.com/stackus/ftgogo/order/internal/domain"
"github.com/stackus/ftgogo/serviceapis/accountingapi"
"github.com/stackus/ftgogo/serviceapis/kitchenapi"
"github.com/stackus/ftgogo/serviceapis/orderapi"
)
type reviseOrderSagaDefinition struct {
steps []saga.Step
}
func NewReviseOrderOrchestrationSaga(store saga.InstanceStore, publisher msg.CommandMessagePublisher, options ...saga.OrchestratorOption) *saga.Orchestrator {
definition := &reviseOrderSagaDefinition{}
definition.steps = []saga.Step{
saga.NewRemoteStep().
Action(definition.beginReviseOrder).
HandleActionReply(orderapi.BeginReviseOrderReply{}, definition.handleBeginReviseOrderReply).
Compensation(definition.undoReviseOrder),
saga.NewRemoteStep().
Action(definition.beginReviseTicket).
Compensation(definition.undoReviseTicket),
saga.NewRemoteStep().
Action(definition.reviseAuthorization),
saga.NewRemoteStep().
Action(definition.confirmTicketRevision),
saga.NewRemoteStep().
Action(definition.confirmOrderRevision),
}
return saga.NewOrchestrator(definition, store, publisher, options...)
}
func (s *reviseOrderSagaDefinition) SagaName() string {
return "orderservice.ReviseOrderSaga"
}
func (s *reviseOrderSagaDefinition) ReplyChannel() string {
return s.SagaName() + ".reply"
}
func (s *reviseOrderSagaDefinition) Steps() []saga.Step {
return s.steps
}
func (s *reviseOrderSagaDefinition) OnHook(saga.LifecycleHook, *saga.Instance) {}
func (s *reviseOrderSagaDefinition) beginReviseOrder(_ context.Context, data core.SagaData) msg.DomainCommand {
sagaData := data.(*domain.ReviseOrderSagaData)
return &orderapi.BeginReviseOrder{
OrderID: sagaData.OrderID,
RevisedQuantities: sagaData.RevisedQuantities,
}
}
func (s *reviseOrderSagaDefinition) undoReviseOrder(_ context.Context, data core.SagaData) msg.DomainCommand {
sagaData := data.(*domain.ReviseOrderSagaData)
return &orderapi.UndoReviseOrder{OrderID: sagaData.OrderID}
}
func (s *reviseOrderSagaDefinition) handleBeginReviseOrderReply(_ context.Context, data core.SagaData, reply core.Reply) error {
sagaData := data.(*domain.ReviseOrderSagaData)
reviseOrderReply := reply.(*orderapi.BeginReviseOrderReply)
sagaData.RevisedOrderTotal = reviseOrderReply.RevisedOrderTotal
return nil
}
func (s *reviseOrderSagaDefinition) beginReviseTicket(_ context.Context, data core.SagaData) msg.DomainCommand {
sagaData := data.(*domain.ReviseOrderSagaData)
return &kitchenapi.BeginReviseTicket{
TicketID: sagaData.TicketID,
RestaurantID: sagaData.RestaurantID,
RevisedQuantities: sagaData.RevisedQuantities,
}
}
func (s *reviseOrderSagaDefinition) undoReviseTicket(_ context.Context, data core.SagaData) msg.DomainCommand {
sagaData := data.(*domain.ReviseOrderSagaData)
return &kitchenapi.UndoReviseTicket{
TicketID: sagaData.TicketID,
RestaurantID: sagaData.RestaurantID,
}
}
func (s *reviseOrderSagaDefinition) reviseAuthorization(_ context.Context, data core.SagaData) msg.DomainCommand {
sagaData := data.(*domain.ReviseOrderSagaData)
return &accountingapi.ReviseAuthorizeOrder{
ConsumerID: sagaData.ConsumerID,
OrderID: sagaData.OrderID,
OrderTotal: sagaData.RevisedOrderTotal,
}
}
func (s *reviseOrderSagaDefinition) confirmTicketRevision(_ context.Context, data core.SagaData) msg.DomainCommand {
sagaData := data.(*domain.ReviseOrderSagaData)
return &kitchenapi.ConfirmReviseTicket{
TicketID: sagaData.TicketID,
RestaurantID: sagaData.RestaurantID,
RevisedQuantities: sagaData.RevisedQuantities,
}
}
func (s *reviseOrderSagaDefinition) confirmOrderRevision(_ context.Context, data core.SagaData) msg.DomainCommand {
sagaData := data.(*domain.ReviseOrderSagaData)
return &orderapi.ConfirmReviseOrder{OrderID: sagaData.OrderID}
}