Skip to content

Commit d3ed3f1

Browse files
Vat rates are now scoped per store
1 parent 61d5fb7 commit d3ed3f1

File tree

16 files changed

+120
-7
lines changed

16 files changed

+120
-7
lines changed

ecommerce/stores/lib/stores.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
require_relative "stores/coupon_registration"
1212
require_relative "stores/invoice_registration"
1313
require_relative "stores/shipment_registration"
14+
require_relative "stores/vat_rate_registration"
1415

1516
module Stores
1617

@@ -25,6 +26,7 @@ def call(event_store, command_bus)
2526
command_bus.register(RegisterCoupon, CouponRegistration.new(event_store))
2627
command_bus.register(RegisterInvoice, InvoiceRegistration.new(event_store))
2728
command_bus.register(RegisterShipment, ShipmentRegistration.new(event_store))
29+
command_bus.register(RegisterVatRate, VatRateRegistration.new(event_store))
2830
end
2931
end
3032
end

ecommerce/stores/lib/stores/commands.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,9 @@ class RegisterShipment < Infra::Command
4242
attribute :store_id, Infra::Types::UUID
4343
attribute :shipment_id, Infra::Types::UUID
4444
end
45+
46+
class RegisterVatRate < Infra::Command
47+
attribute :store_id, Infra::Types::UUID
48+
attribute :vat_rate_id, Infra::Types::UUID
49+
end
4550
end

ecommerce/stores/lib/stores/events.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,9 @@ class ShipmentRegistered < Infra::Event
4444
attribute :shipment_id, Infra::Types::UUID
4545
end
4646

47+
class VatRateRegistered < Infra::Event
48+
attribute :store_id, Infra::Types::UUID
49+
attribute :vat_rate_id, Infra::Types::UUID
50+
end
51+
4752
end
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
module Stores
2+
class VatRateRegistration
3+
def initialize(event_store)
4+
@event_store = event_store
5+
end
6+
7+
def call(cmd)
8+
@event_store.publish(vat_rate_registered_event(cmd), stream_name: stream_name(cmd))
9+
end
10+
11+
private
12+
13+
def vat_rate_registered_event(cmd)
14+
VatRateRegistered.new(
15+
data: {
16+
store_id: cmd.store_id,
17+
vat_rate_id: cmd.vat_rate_id,
18+
}
19+
)
20+
end
21+
22+
def stream_name(cmd)
23+
"Stores::Store$#{cmd.store_id}"
24+
end
25+
end
26+
end
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
require_relative 'test_helper'
2+
module Stores
3+
class VatRateRegistrationTest < Test
4+
cover "Stores*"
5+
6+
def test_vat_rate_should_get_registered
7+
store_id = SecureRandom.uuid
8+
vat_rate_id = SecureRandom.uuid
9+
assert register_vat_rate(store_id, vat_rate_id)
10+
end
11+
12+
def test_should_publish_event
13+
store_id = SecureRandom.uuid
14+
vat_rate_id = SecureRandom.uuid
15+
vat_rate_registered = Stores::VatRateRegistered.new(data: { store_id: store_id, vat_rate_id: vat_rate_id })
16+
assert_events("Stores::Store$#{store_id}", vat_rate_registered) do
17+
register_vat_rate(store_id, vat_rate_id)
18+
end
19+
end
20+
21+
private
22+
23+
def register_vat_rate(store_id, vat_rate_id)
24+
run_command(RegisterVatRate.new(store_id: store_id, vat_rate_id: vat_rate_id))
25+
end
26+
end
27+
end

ecommerce/taxes/lib/taxes.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ module Taxes
99
class Configuration
1010
def call(event_store, command_bus)
1111
command_bus.register(SetVatRate, SetVatRateHandler.new(event_store))
12-
command_bus.register(AddAvailableVatRate, AddAvailableVatRateHandler.new(event_store))
12+
command_bus.register(AddAvailableVatRate, AddAvailableVatRateHandler.new(event_store, command_bus))
1313
command_bus.register(RemoveAvailableVatRate, RemoveAvailableVatRateHandler.new(event_store))
1414
end
1515
end

ecommerce/taxes/lib/taxes/commands.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ class SetVatRate < Infra::Command
77
class AddAvailableVatRate < Infra::Command
88
attribute :available_vat_rate_id, Infra::Types::UUID
99
attribute :vat_rate, Infra::Types::VatRate
10+
attribute :store_id, Infra::Types::UUID
1011
end
1112

1213
class RemoveAvailableVatRate < Infra::Command

ecommerce/taxes/lib/taxes/services.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,9 @@ def call(cmd)
2020
class AddAvailableVatRateHandler
2121
include Infra::Retry
2222

23-
def initialize(event_store)
23+
def initialize(event_store, command_bus)
2424
@event_store = event_store
25+
@command_bus = command_bus
2526
end
2627

2728
def call(cmd)
@@ -31,12 +32,13 @@ def call(cmd)
3132

3233
expected_version = event ? event_store.position_in_stream(event.event_id, stream_name(cmd)) : -1
3334
event_store.publish(available_vat_rate_added_event(cmd), stream_name: stream_name(cmd), expected_version: expected_version)
35+
command_bus.call(Stores::RegisterVatRate.new(store_id: cmd.store_id, vat_rate_id: cmd.available_vat_rate_id))
3436
end
3537
end
3638

3739
private
3840

39-
attr_reader :event_store
41+
attr_reader :event_store, :command_bus
4042

4143
def last_available_vat_rate_event(vat_rate_code)
4244
event_store

rails_application/.mutant.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ matcher:
2121
- Processes*
2222
- Invoices*
2323
- Shipments*
24+
- VatRates*
2425
ignore:
2526
- Coupons::Configuration#call
2627
- Orders::Configuration#call

rails_application/app/controllers/available_vat_rates_controller.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@ def add_available_vat_rate(code, rate, available_vat_rate_id)
5353
def add_available_vat_rate_cmd(code, rate, available_vat_rate_id)
5454
Taxes::AddAvailableVatRate.new(
5555
available_vat_rate_id: available_vat_rate_id,
56-
vat_rate: Infra::Types::VatRate.new(code: code, rate: rate)
56+
vat_rate: Infra::Types::VatRate.new(code: code, rate: rate),
57+
store_id: current_store_id
5758
)
5859
end
5960

0 commit comments

Comments
 (0)