Skip to content

Commit

Permalink
Change client order product available to boolean, display available a…
Browse files Browse the repository at this point in the history
…mount on admin view
  • Loading branch information
marlena-b committed Sep 16, 2024
1 parent fdede5b commit 3f485ec
Show file tree
Hide file tree
Showing 9 changed files with 27 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,6 @@ def value

class Product < ApplicationRecord
self.table_name = "client_order_products"

def unavailable?
available && available <= 0
end
end

class Configuration
Expand Down
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion rails_application/app/views/client/orders/edit.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
<% order_line = @order_lines&.find{|order_line| order_line.product_id == product.uid} %>
<td class="py-2"><%= product.name %></td>
<td class="py-2">
<% if product.unavailable? %>
<% unless product.available? %>
<span class="rounded-lg bg-yellow-400 text-yellow-900 px-2 py-0.5">out of stock</span>
<% end %>
</td>
Expand Down
6 changes: 2 additions & 4 deletions rails_application/app/views/orders/edit.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
<thead>
<tr class="border-b">
<th class="text-left py-2">Product</th>
<th class="text-left py-2"></th>
<th class="text-left py-2">Stock</th>
<th class="text-left py-2">Quantity</th>
<th class="text-left py-2">Price</th>
<th class="text-left py-2" colspan="3">Value</th>
Expand All @@ -43,9 +43,7 @@
<% order_line = @order_lines.find{|order_line| order_line.product_id == product.id} %>
<td class="py-2"><%= product.name %></td>
<td class="py-2">
<% if product.unavailable? %>
<span class="rounded-lg bg-yellow-400 text-yellow-900 px-2 py-0.5">out of stock</span>
<% end %>
<span><%= product.available || "-" %></span>
</td>
<td class="py-2" id="<%= "orders_order_#{product.id}_quantity" %>"><%= order_line.try(&:quantity) || 0 %></td>
<td class="py-2"><%= number_to_currency(product.price) %></td>
Expand Down
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion rails_application/db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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|
Expand Down
21 changes: 0 additions & 21 deletions rails_application/test/client_orders/product_test.rb

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
4 changes: 2 additions & 2 deletions rails_application/test/integration/orders_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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
Expand Down

0 comments on commit 3f485ec

Please sign in to comment.