-
Notifications
You must be signed in to change notification settings - Fork 72
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
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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( | ||
|
@@ -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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 }) | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.