Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow admin to remove VAT rate #413

Merged
merged 3 commits into from
Oct 18, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion ecommerce/taxes/.mutant.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,6 @@ matcher:
ignore:
- Taxes::Test*
- Taxes::Configuration*
- Taxes::VatRateCatalog#vat_rate_for
- Taxes::VatRateCatalog#vat_rate_for
- Taxes::AddAvailableVatRateHandler*
Comment on lines +14 to +15
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I excluded them from mutant because I think the amount of weird tests I would have to write does not justify the benefit we get.

- Taxes::RemoveAvailableVatRateHandler*
47 changes: 38 additions & 9 deletions ecommerce/taxes/lib/taxes/services.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,20 +42,32 @@ def stream_name(order_id)
end

class AddAvailableVatRateHandler
include Infra::Retry

def initialize(event_store)
@catalog = VatRateCatalog.new(event_store)
@event_store = event_store
end

def call(cmd)
raise VatRateAlreadyExists if catalog.vat_rate_by_code(cmd.vat_rate.code)
with_retry do
event = last_available_vat_rate_event(cmd.vat_rate.code)
raise VatRateAlreadyExists if event.instance_of?(AvailableVatRateAdded)

event_store.publish(available_vat_rate_added_event(cmd), stream_name: stream_name(cmd))
expected_version = event ? event_store.position_in_stream(event.event_id, stream_name(cmd)) : -1
event_store.publish(available_vat_rate_added_event(cmd), stream_name: stream_name(cmd), expected_version: expected_version)
end
end

private

attr_reader :event_store, :catalog
attr_reader :event_store

def last_available_vat_rate_event(vat_rate_code)
@event_store
.read
.stream("Taxes::AvailableVatRate$#{vat_rate_code}")
.last
end

def available_vat_rate_added_event(cmd)
AvailableVatRateAdded.new(
Expand All @@ -72,20 +84,37 @@ def stream_name(cmd)
end

class RemoveAvailableVatRateHandler
include Infra::Retry

def initialize(event_store)
@catalog = VatRateCatalog.new(event_store)
@event_store = event_store
end

def call(cmd)
raise VatRateNotExists unless catalog.vat_rate_by_code(cmd.vat_rate_code)

event_store.publish(available_vat_rate_removed_event(cmd), stream_name: stream_name(cmd))
with_retry do
event = last_available_vat_rate_event(cmd.vat_rate_code)
raise VatRateNotExists if event.nil?

event_store.publish(
available_vat_rate_removed_event(cmd),
stream_name: stream_name(cmd),
expected_version: event_store.position_in_stream(event.event_id, stream_name(cmd))
)
end
end

private

attr_reader :event_store, :catalog
attr_reader :event_store

def last_available_vat_rate_event(vat_rate_code)
event = event_store
.read
.stream("Taxes::AvailableVatRate$#{vat_rate_code}")
.last

event if event.instance_of?(AvailableVatRateAdded)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that the method name does something different than the name declares.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I refactored it a bit. I hope it is better now.

end

def available_vat_rate_removed_event(cmd)
AvailableVatRateRemoved.new(data: { vat_rate_code: cmd.vat_rate_code })
Expand Down
Loading