diff --git a/app/controllers/orders_controller.rb b/app/controllers/orders_controller.rb index 0dc05e21..f713527c 100644 --- a/app/controllers/orders_controller.rb +++ b/app/controllers/orders_controller.rb @@ -8,4 +8,15 @@ def show @order = Order.find(params[:id]) @order_items = @order.order_items end + + def destroy + @order = Order.find(params[:id]) + @order.update(status: :cancelled) + @order.order_items.each do |order_item| + order_item.update(fulfilled: false) + end + flash[:notice] = "#{@order.id} has been cancelled." + + redirect_to profile_orders_path + end end diff --git a/app/models/order_item.rb b/app/models/order_item.rb index 83edd5ad..d7a2b3a5 100644 --- a/app/models/order_item.rb +++ b/app/models/order_item.rb @@ -5,7 +5,24 @@ class OrderItem < ApplicationRecord validates_presence_of :quantity, :price validates_inclusion_of :fulfilled, in: [true, false] + after_save :update_inventory + def sub_total quantity * price end + + private + + def update_inventory + if saved_change_to_fulfilled? + + if fulfilled + new_inventory = item.inventory - quantity + item.update(inventory: new_inventory) + else + new_inventory = item.inventory + quantity + item.update(inventory: new_inventory) + end + end + end end diff --git a/app/views/orders/show.html.erb b/app/views/orders/show.html.erb index 3781e631..41b22c14 100644 --- a/app/views/orders/show.html.erb +++ b/app/views/orders/show.html.erb @@ -17,3 +17,9 @@

Number of Items: <%= @order.item_count %>

Grand Total: <%= number_to_currency(@order.grand_total, unit: "$") %>

+ + +<%= button_to "Cancel Order", profile_order_path(@order), method: "delete", disabled: !@order.pending? %> +<% if !@order.pending? %> +

You can only cancel orders that are pending!

+<% end %> diff --git a/config/routes.rb b/config/routes.rb index 552e2e55..b6783d66 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -18,8 +18,12 @@ get '/profile', to: 'users#show' get '/profile/edit', to: 'users#edit' patch '/profile/edit', to: 'users#update' - get '/profile/orders', to: 'orders#index' - get '/profile/orders/:id', to: 'orders#show', as: :profile_order + scope :profile do + # resources :users, only: [:show, :edit, :update], as: :profile + resources :orders, only: [:show, :index, :destroy], as: :profile_orders + # get '/profile/orders', to: 'orders#index' + # get '/profile/orders/:id', to: 'orders#show' + end get '/dashboard', to: 'merchants#show' diff --git a/spec/features/users/default/orders_show_spec.rb b/spec/features/users/default/orders_show_spec.rb index 8912d32d..44e9fdb5 100644 --- a/spec/features/users/default/orders_show_spec.rb +++ b/spec/features/users/default/orders_show_spec.rb @@ -61,5 +61,47 @@ expect(page).to have_content("Number of Items: #{@order_1.item_count}") expect(page).to have_content("Grand Total: $#{@order_1.grand_total}") end + + it 'I can cancel the order if it is still pending' do + @order_1.update!(status: :pending) + @item_2.update!(inventory: 3) + @order_item_2.update!(fulfilled: true) + @item_2.reload + @order_item_2.reload + expect(@item_2.inventory).to eq(2) + + visit profile_order_path(@order_1) + + expect(page).to have_content("Current Status: Pending") + expect(page).to have_button("Cancel Order") + + click_button "Cancel Order" + + @order_item_1.reload + @order_item_2.reload + @order_item_3.reload + expect(@order_item_1.fulfilled).to be false + expect(@order_item_2.fulfilled).to be false + expect(@order_item_3.fulfilled).to be false + + @item_2.reload + expect(@item_2.inventory).to eq(3) + + expect(current_path).to eq(profile_orders_path) + + expect(page).to have_content("#{@order_1.id} has been cancelled.") + + within("#order-#{@order_1.id}") do + expect(page).to have_content("Current Status: Cancelled") + end + end + + it 'Should have a disabled cancel button if the order is not pending' do + visit profile_order_path(@order_1) + + expect(page).to have_content("Current Status: Shipped") + expect(page).to have_button("Cancel Order", disabled: true) + expect(page).to have_content("You can only cancel orders that are pending!") + end end end