diff --git a/apps/rails_application/config/initializers/rails_event_store.rb b/apps/rails_application/config/initializers/rails_event_store.rb index 806b45ab1..a0549dc40 100644 --- a/apps/rails_application/config/initializers/rails_event_store.rb +++ b/apps/rails_application/config/initializers/rails_event_store.rb @@ -2,9 +2,23 @@ require "arkency/command_bus" require_relative "../../lib/configuration" +require_relative "../../lib/transformations/refund_to_return_event_mapper" Rails.configuration.to_prepare do - Rails.configuration.event_store = Infra::EventStore.main + mapper = RubyEventStore::Mappers::PipelineMapper.new( + RubyEventStore::Mappers::Pipeline.new( + Infra::EventStore.preserve_types, + Transformations::RefundToReturnEventMapper.new( + 'Ordering::DraftRefundCreated' => 'Ordering::DraftReturnCreated', + 'Ordering::ItemAddedToRefund' => 'Ordering::ItemAddedToReturn', + 'Ordering::ItemRemovedFromRefund' => 'Ordering::ItemRemovedFromReturn' + ), + RubyEventStore::Mappers::Transformation::SymbolizeMetadataKeys.new, + to_domain_event: RubyEventStore::Mappers::Transformation::DomainEvent.new + ) + ) + + Rails.configuration.event_store = Infra::EventStore.main(mapper: mapper) Rails.configuration.command_bus = Arkency::CommandBus.new Configuration.new.call(Rails.configuration.event_store, Rails.configuration.command_bus) diff --git a/infra/lib/infra/event_store.rb b/infra/lib/infra/event_store.rb index 321933fe9..5fa55bb50 100644 --- a/infra/lib/infra/event_store.rb +++ b/infra/lib/infra/event_store.rb @@ -1,28 +1,19 @@ module Infra class EventStore < SimpleDelegator - def self.main - require_relative "../../../apps/rails_application/lib/transformations/refund_to_return_event_mapper" rescue nil + def self.main(mapper: nil) + mapper ||= default_mapper + client = RailsEventStore::JSONClient.new(mapper: mapper) + new(client) + end - begin - mapper = RubyEventStore::Mappers::PipelineMapper.new( - RubyEventStore::Mappers::Pipeline.new( - preserve_types, - Transformations::RefundToReturnEventMapper.new( - 'Ordering::DraftRefundCreated' => 'Ordering::DraftReturnCreated', - 'Ordering::ItemAddedToRefund' => 'Ordering::ItemAddedToReturn', - 'Ordering::ItemRemovedFromRefund' => 'Ordering::ItemRemovedFromReturn' - ), - RubyEventStore::Mappers::Transformation::SymbolizeMetadataKeys.new, - to_domain_event: RubyEventStore::Mappers::Transformation::DomainEvent.new - ) + def self.default_mapper + RubyEventStore::Mappers::PipelineMapper.new( + RubyEventStore::Mappers::Pipeline.new( + preserve_types, + RubyEventStore::Mappers::Transformation::SymbolizeMetadataKeys.new, + to_domain_event: RubyEventStore::Mappers::Transformation::DomainEvent.new ) - client = RailsEventStore::JSONClient.new(mapper: mapper) - rescue => e - puts "Mapper creation failed: #{e.message}" - client = RailsEventStore::JSONClient.new - end - - new(client) + ) end def self.in_memory @@ -53,8 +44,6 @@ def link_event_to_stream(event, stream, expected_version: :any) ) end - private - def self.preserve_types preserve_types = RubyEventStore::Mappers::Transformation::PreserveTypes.new