-
Notifications
You must be signed in to change notification settings - Fork 72
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #383 from RailsEventStore/revert-382-revert-377-av…
…ailable-vat-rates Revert "Revert "Allow creating custom vat rates""
- Loading branch information
Showing
34 changed files
with
458 additions
and
79 deletions.
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
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 |
---|---|---|
@@ -1,11 +1,16 @@ | ||
module Taxes | ||
class SetVatRate < Infra::Command | ||
attribute :product_id, Infra::Types::UUID | ||
attribute :vat_rate, Infra::Types::VatRate | ||
attribute :vat_rate_code, Infra::Types::String | ||
end | ||
|
||
class DetermineVatRate < Infra::Command | ||
attribute :product_id, Infra::Types::UUID | ||
attribute :order_id, Infra::Types::UUID | ||
end | ||
end | ||
|
||
class AddAvailableVatRate < Infra::Command | ||
attribute :available_vat_rate_id, Infra::Types::UUID | ||
attribute :vat_rate, Infra::Types::VatRate | ||
end | ||
end |
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
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
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
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 |
---|---|---|
@@ -0,0 +1,30 @@ | ||
require_relative "test_helper" | ||
|
||
module Taxes | ||
class VatRateCatalogTest < Test | ||
class VatRateByCodeTest < VatRateCatalogTest | ||
def setup | ||
@vat_rate = Infra::Types::VatRate.new(code: "50", rate: 50) | ||
add_available_vat_rate(@vat_rate) | ||
end | ||
|
||
def test_returns_available_vat_rate | ||
assert_equal @vat_rate, catalog.vat_rate_by_code("50") | ||
end | ||
|
||
def test_returns_nil_when_vat_rate_is_not_available | ||
assert_nil catalog.vat_rate_by_code("60") | ||
end | ||
end | ||
|
||
private | ||
|
||
def catalog | ||
VatRateCatalog.new(@event_store) | ||
end | ||
|
||
def add_available_vat_rate(vat_rate, available_vat_rate_id = SecureRandom.uuid) | ||
run_command(AddAvailableVatRate.new(available_vat_rate_id: available_vat_rate_id, vat_rate: vat_rate)) | ||
end | ||
end | ||
end |
56 changes: 56 additions & 0 deletions
56
rails_application/app/controllers/available_vat_rates_controller.rb
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 |
---|---|---|
@@ -0,0 +1,56 @@ | ||
class AvailableVatRatesController < ApplicationController | ||
class AvailableVatRateForm | ||
include ActiveModel::Model | ||
include ActiveModel::Validations | ||
|
||
attr_reader :code, :rate | ||
|
||
def initialize(params) | ||
@code = params[:code] | ||
@rate = params[:rate] | ||
end | ||
|
||
validates :code, presence: true | ||
validates :rate, presence: true, numericality: { greater_than: 0 } | ||
end | ||
|
||
def new | ||
end | ||
|
||
def create | ||
available_vat_rate_id = SecureRandom.uuid | ||
available_vat_rate_form = AvailableVatRateForm.new(available_vat_rate_params) | ||
|
||
unless available_vat_rate_form.valid? | ||
return render "new", locals: { errors: available_vat_rate_form.errors }, status: :unprocessable_entity | ||
end | ||
|
||
add_available_vat_rate(available_vat_rate_form.code, available_vat_rate_form.rate, available_vat_rate_id) | ||
rescue Taxes::VatRateAlreadyExists | ||
flash.now[:notice] = "VAT rate already exists" | ||
render "new", status: :unprocessable_entity | ||
else | ||
redirect_to available_vat_rates_path, notice: "VAT rate was successfully created" | ||
end | ||
|
||
def index | ||
@available_vat_rates = VatRates::AvailableVatRate.all | ||
end | ||
|
||
private | ||
|
||
def add_available_vat_rate(code, rate, available_vat_rate_id) | ||
command_bus.(add_available_vat_rate_cmd(code, rate, available_vat_rate_id)) | ||
end | ||
|
||
def add_available_vat_rate_cmd(code, rate, available_vat_rate_id) | ||
Taxes::AddAvailableVatRate.new( | ||
available_vat_rate_id: available_vat_rate_id, | ||
vat_rate: Infra::Types::VatRate.new(code: code, rate: rate) | ||
) | ||
end | ||
|
||
def available_vat_rate_params | ||
params.permit(:code, :rate) | ||
end | ||
end |
Oops, something went wrong.