From 3f485ec4f7847db61b9fa5c4f45c9f43aa2a7455 Mon Sep 17 00:00:00 2001 From: marlena-b Date: Mon, 16 Sep 2024 17:54:06 +0200 Subject: [PATCH] Change client order product available to boolean, display available amount on admin view --- .../client_orders/configuration.rb | 4 ---- .../update_product_availability.rb | 5 ++++- .../app/views/client/orders/edit.html.erb | 2 +- .../app/views/orders/edit.html.erb | 6 ++---- ..._add_available_to_client_order_products.rb | 2 +- rails_application/db/schema.rb | 2 +- .../test/client_orders/product_test.rb | 21 ------------------- .../update_product_availability_test.rb | 19 ++++++++++++++--- .../test/integration/orders_test.rb | 4 ++-- 9 files changed, 27 insertions(+), 38 deletions(-) delete mode 100644 rails_application/test/client_orders/product_test.rb diff --git a/rails_application/app/read_models/client_orders/configuration.rb b/rails_application/app/read_models/client_orders/configuration.rb index 9cd8f903..43dbebda 100644 --- a/rails_application/app/read_models/client_orders/configuration.rb +++ b/rails_application/app/read_models/client_orders/configuration.rb @@ -30,10 +30,6 @@ def value class Product < ApplicationRecord self.table_name = "client_order_products" - - def unavailable? - available && available <= 0 - end end class Configuration diff --git a/rails_application/app/read_models/client_orders/update_product_availability.rb b/rails_application/app/read_models/client_orders/update_product_availability.rb index 4fdc5fd6..a3554630 100644 --- a/rails_application/app/read_models/client_orders/update_product_availability.rb +++ b/rails_application/app/read_models/client_orders/update_product_availability.rb @@ -1,7 +1,10 @@ module ClientOrders class UpdateProductAvailability def call(event) - Product.find_by(uid: event.data.fetch(:product_id)).update(available: event.data.fetch(:available)) + product = Product.find_by(uid: event.data.fetch(:product_id)) + available = event.data.fetch(:available) + + product.update(available: available.positive?) end end end diff --git a/rails_application/app/views/client/orders/edit.html.erb b/rails_application/app/views/client/orders/edit.html.erb index 0751dc48..0ef9f3d3 100644 --- a/rails_application/app/views/client/orders/edit.html.erb +++ b/rails_application/app/views/client/orders/edit.html.erb @@ -16,7 +16,7 @@ <% order_line = @order_lines&.find{|order_line| order_line.product_id == product.uid} %> <%= product.name %> - <% if product.unavailable? %> + <% unless product.available? %> out of stock <% end %> diff --git a/rails_application/app/views/orders/edit.html.erb b/rails_application/app/views/orders/edit.html.erb index 0352d6f4..cb9e4c5b 100644 --- a/rails_application/app/views/orders/edit.html.erb +++ b/rails_application/app/views/orders/edit.html.erb @@ -30,7 +30,7 @@ Product - + Stock Quantity Price Value @@ -43,9 +43,7 @@ <% order_line = @order_lines.find{|order_line| order_line.product_id == product.id} %> <%= product.name %> - <% if product.unavailable? %> - out of stock - <% end %> + <%= product.available || "-" %> "><%= order_line.try(&:quantity) || 0 %> <%= number_to_currency(product.price) %> diff --git a/rails_application/db/migrate/20240827090619_add_available_to_client_order_products.rb b/rails_application/db/migrate/20240827090619_add_available_to_client_order_products.rb index 66ce91e7..df1eb5bd 100644 --- a/rails_application/db/migrate/20240827090619_add_available_to_client_order_products.rb +++ b/rails_application/db/migrate/20240827090619_add_available_to_client_order_products.rb @@ -1,5 +1,5 @@ class AddAvailableToClientOrderProducts < ActiveRecord::Migration[7.2] def change - add_column :client_order_products, :available, :integer + add_column :client_order_products, :available, :boolean, default: true end end diff --git a/rails_application/db/schema.rb b/rails_application/db/schema.rb index 941de168..99d056d9 100644 --- a/rails_application/db/schema.rb +++ b/rails_application/db/schema.rb @@ -46,7 +46,7 @@ t.uuid "uid", null: false t.string "name" t.decimal "price", precision: 8, scale: 2 - t.integer "available" + t.boolean "available", default: true end create_table "client_orders", force: :cascade do |t| diff --git a/rails_application/test/client_orders/product_test.rb b/rails_application/test/client_orders/product_test.rb deleted file mode 100644 index 448ddbb0..00000000 --- a/rails_application/test/client_orders/product_test.rb +++ /dev/null @@ -1,21 +0,0 @@ -require "test_helper" - -module ClientOrders - class ProductTest < InMemoryTestCase - cover "ClientOrders*" - - def test_unavailable - product = Product.new(available: nil) - refute product.unavailable? - - product = Product.new(available: 0) - assert product.unavailable? - - product = Product.new(available: 1) - refute product.unavailable? - - product = Product.new(available: -1) - assert product.unavailable? - end - end -end diff --git a/rails_application/test/client_orders/update_product_availability_test.rb b/rails_application/test/client_orders/update_product_availability_test.rb index 40993ebb..16f072e1 100644 --- a/rails_application/test/client_orders/update_product_availability_test.rb +++ b/rails_application/test/client_orders/update_product_availability_test.rb @@ -8,10 +8,19 @@ def test_reflects_change product_id = prepare_product other_product_id = prepare_product - supply_product(product_id, 5) + assert_changes("Product.find_by_uid(product_id).available?", from: true, to: false) do + UpdateProductAvailability.new.call(availability_changed_event(product_id, -1)) + end - assert_equal 5, Product.find_by_uid(product_id).available - assert_nil Product.find_by_uid(other_product_id).available + assert_changes("Product.find_by_uid(product_id).available?", from: false, to: true) do + UpdateProductAvailability.new.call(availability_changed_event(product_id, 10)) + end + + assert_changes("Product.find_by_uid(product_id).available?", from: true, to: false) do + UpdateProductAvailability.new.call(availability_changed_event(product_id, 0)) + end + + assert Product.find_by_uid(other_product_id).available? end private @@ -37,5 +46,9 @@ def prepare_product def supply_product(product_id, quantity) run_command(Inventory::Supply.new(product_id: product_id, quantity: quantity)) end + + def availability_changed_event(product_id, available) + Inventory::AvailabilityChanged.new(data: { product_id: product_id, available: available }) + end end end diff --git a/rails_application/test/integration/orders_test.rb b/rails_application/test/integration/orders_test.rb index c9b8f472..1c231e1f 100644 --- a/rails_application/test/integration/orders_test.rb +++ b/rails_application/test/integration/orders_test.rb @@ -229,7 +229,7 @@ def test_shows_out_of_stock_badge get "/orders/new" follow_redirect! - assert_select "td span", text: "out of stock", count: 0 + assert_select "td span", "1" post "/orders/#{order_id}/add_item?product_id=#{async_remote_id}" post "/orders", @@ -243,7 +243,7 @@ def test_shows_out_of_stock_badge get "/orders/new" follow_redirect! - assert_select "td span", "out of stock" + assert_select "td span", "0" end private