Skip to content

Commit

Permalink
Process manager
Browse files Browse the repository at this point in the history
  • Loading branch information
lukaszreszke committed Nov 14, 2024
1 parent e99bdc6 commit 7bd8459
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
37 changes: 37 additions & 0 deletions rails_application/app/services/send_email.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# frozen_string_literal: true

class SendEmail
def call(event)
event_store.link(event.event_id, stream_name: "Sales$#{order_id(event)}")

state = {}

event_store.read.stream("Sales$#{order_id(event)}").each do |event_in_stream|
case event_in_stream
when Ordering::OrderPaid
state[:order_paid] = true
when Invoicing::InvoiceGenerated
state[:invoice_generated] = true
end
end

if state[:order_paid] && state[:invoice_generated]
Rails.configuration.email_client.send_email(order_id(event))
end
end

private

def order_id(event)
case event
when Ordering::OrderPaid
event.data[:id]
when Invoicing::InvoiceGenerated
event.data[:order_id]
end
end

def event_store
Rails.configuration.event_store
end
end
5 changes: 5 additions & 0 deletions rails_application/lib/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ def call(event_store, command_bus)
Inventory::UpdateProductCatalog,
to: [Inventory::StockLevelIncreased, Inventory::StockLevelDecreased]
)

event_store.subscribe(
SendEmail,
to: [Ordering::OrderPaid, Invoicing::InvoiceGenerated]
)
end

def self.available_vat_rates
Expand Down

0 comments on commit 7bd8459

Please sign in to comment.