File tree Expand file tree Collapse file tree 2 files changed +42
-0
lines changed
Expand file tree Collapse file tree 2 files changed +42
-0
lines changed Original file line number Diff line number Diff line change 1+ # frozen_string_literal: true
2+
3+ class SendEmail
4+ def call ( event )
5+ event_store . link ( event . event_id , stream_name : "Sales$#{ order_id ( event ) } " )
6+
7+ state = { }
8+
9+ event_store . read . stream ( "Sales$#{ order_id ( event ) } " ) . each do |event_in_stream |
10+ case event_in_stream
11+ when Ordering ::OrderPaid
12+ state [ :order_paid ] = true
13+ when Invoicing ::InvoiceGenerated
14+ state [ :invoice_generated ] = true
15+ end
16+ end
17+
18+ if state [ :order_paid ] && state [ :invoice_generated ]
19+ Rails . configuration . email_client . send_email ( order_id ( event ) )
20+ end
21+ end
22+
23+ private
24+
25+ def order_id ( event )
26+ case event
27+ when Ordering ::OrderPaid
28+ event . data [ :id ]
29+ when Invoicing ::InvoiceGenerated
30+ event . data [ :order_id ]
31+ end
32+ end
33+
34+ def event_store
35+ Rails . configuration . event_store
36+ end
37+ end
Original file line number Diff line number Diff line change @@ -21,6 +21,11 @@ def call(event_store, command_bus)
2121 Inventory ::UpdateProductCatalog ,
2222 to : [ Inventory ::StockLevelIncreased , Inventory ::StockLevelDecreased ]
2323 )
24+
25+ event_store . subscribe (
26+ SendEmail ,
27+ to : [ Ordering ::OrderPaid , Invoicing ::InvoiceGenerated ]
28+ )
2429 end
2530
2631 def self . available_vat_rates
You can’t perform that action at this time.
0 commit comments