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

Change VAT Rate code from integer to string #392

Merged
merged 1 commit into from
Sep 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
18 changes: 9 additions & 9 deletions rails_application/app/controllers/products_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ class ProductForm

attribute :name, :string
attribute :price, :decimal
attribute :vat_rate, :string
attribute :vat_rate_code, :string
attribute :product_id, :string

validates :name, presence: true
validates :price, presence: true, numericality: { greater_than: 0 }
validates :vat_rate, presence: true, numericality: { greater_than: 0 }
validates :vat_rate_code, presence: true
validates :product_id, presence: true
end

Expand Down Expand Up @@ -43,8 +43,8 @@ def create
if product_form.price.present?
set_product_price(product_form.product_id, product_form.price)
end
if product_form.vat_rate.present?
set_product_vat_rate(product_form.product_id, product_form.vat_rate)
if product_form.vat_rate_code.present?
set_product_vat_rate(product_form.product_id, product_form.vat_rate_code)
end
end

Expand Down Expand Up @@ -91,8 +91,8 @@ def set_future_product_price(product_id, price, valid_since)
command_bus.(set_product_future_price_cmd(product_id, price, valid_since))
end

def set_product_vat_rate(product_id, vat_rate)
command_bus.(set_product_vat_rate_cmd(product_id, vat_rate))
def set_product_vat_rate(product_id, vat_rate_code)
command_bus.(set_product_vat_rate_cmd(product_id, vat_rate_code))
end

def set_product_name(product_id, name)
Expand All @@ -111,8 +111,8 @@ def set_product_price_cmd(product_id, price)
Pricing::SetPrice.new(product_id: product_id, price: price)
end

def set_product_vat_rate_cmd(product_id, vat_rate)
Taxes::SetVatRate.new(product_id: product_id, vat_rate_code: vat_rate)
def set_product_vat_rate_cmd(product_id, vat_rate_code)
Taxes::SetVatRate.new(product_id: product_id, vat_rate_code: vat_rate_code)
end

def set_product_future_price_cmd(product_id, price, valid_since)
Expand All @@ -124,6 +124,6 @@ def set_product_future_price_cmd(product_id, price, valid_since)
end

def product_params
params.permit(:name, :price, :vat_rate, :product_id).to_h.symbolize_keys.slice(:price, :vat_rate, :product_id, :name)
params.permit(:name, :price, :vat_rate_code, :product_id).to_h.symbolize_keys.slice(:price, :vat_rate_code, :name, :product_id)
end
end
2 changes: 1 addition & 1 deletion rails_application/app/views/products/new.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
<label for="vat_rate" class="block font-bold">
VAT rate
</label>
<%= select_tag :vat_rate, options_from_collection_for_select(VatRates::AvailableVatRate.all, :code, :code), class: "mt-1 focus:ring-blue-500 focus:border-blue-500 block shadow-sm sm:text-sm border-gray-300 rounded-md", data: { turbo_permanent: true } %>
<%= select_tag :vat_rate_code, options_from_collection_for_select(VatRates::AvailableVatRate.all, :code, :code), class: "mt-1 focus:ring-blue-500 focus:border-blue-500 block shadow-sm sm:text-sm border-gray-300 rounded-md", data: { turbo_permanent: true } %>
</div>

<% if defined?(errors) %>
Expand Down
31 changes: 6 additions & 25 deletions rails_application/test/integration/products_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ def setup
end

def test_happy_path
add_available_vat_rate(10)
add_available_vat_rate(10, "10S")
register_customer("Arkency")
product_id = SecureRandom.uuid

Expand All @@ -20,7 +20,7 @@ def test_happy_path
"product_id" => product_id,
"name" => "product name",
:price => "20.01",
"vat_rate" => "10"
"vat_rate_code" => "10S"
}
follow_redirect!

Expand Down Expand Up @@ -59,7 +59,7 @@ def test_does_not_crash_when_setting_products_price_to_0
"product_id" => product_id,
"name" => "product name",
"price" => "0",
"vat_rate" => "10"
"vat_rate_code" => "10"
}

assert_response :unprocessable_entity
Expand All @@ -77,30 +77,11 @@ def test_does_not_crash_when_vat_rate_is_absent
"authenticity_token" => "[FILTERED]",
"product_id" => product_id,
"name" => "product name",
"price" => "100",
}

assert_response :unprocessable_entity
assert_select "span", "Vat rate can't be blank"
end

def test_does_not_crash_when_vat_rate_is_not_a_number
register_customer("Arkency")
product_id = SecureRandom.uuid

get "/products/new"
assert_select "h1", "New Product"
post "/products",
params: {
"authenticity_token" => "[FILTERED]",
"product_id" => product_id,
"name" => "product name",
"price" => "100",
"vat_rate" =>"abc"
"price" => "100"
}

assert_response :unprocessable_entity
assert_select "span", "Vat rate is not a number"
assert_select "span", "Vat rate code can't be blank"
end

def test_does_not_crash_when_name_is_not_present
Expand All @@ -114,7 +95,7 @@ def test_does_not_crash_when_name_is_not_present
"authenticity_token" => "[FILTERED]",
"product_id" => product_id,
"price" => "100",
"vat_rate" =>"10"
"vat_rate_code" =>"10"
}

assert_response :unprocessable_entity
Expand Down
4 changes: 2 additions & 2 deletions rails_application/test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,9 @@ def register_customer(name)
customer_id
end

def register_product(name, price, vat_rate)
def register_product(name, price, vat_rate_code)
product_id = SecureRandom.uuid
post "/products", params: { product_id: product_id, name: name, price: price, vat_rate: vat_rate }
post "/products", params: { product_id: product_id, name: name, price: price, vat_rate_code: vat_rate_code }
product_id
end

Expand Down
Loading