mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-02-08 22:56:06 +00:00
Adding a #resume action to StandingOrderOrdersController
This commit is contained in:
@@ -5,6 +5,10 @@ angular.module("admin.standingOrders").controller "OrdersPanelController", ($sco
|
||||
if confirm(t('are_you_sure'))
|
||||
$scope.standingOrder.cancelOrder(order)
|
||||
|
||||
$scope.resumeOrder = (order) ->
|
||||
if confirm(t('are_you_sure'))
|
||||
$scope.standingOrder.resumeOrder(order)
|
||||
|
||||
$scope.orderCycleName = (id) ->
|
||||
OrderCycles.byID[id].name
|
||||
|
||||
|
||||
@@ -260,7 +260,7 @@ class AbilityDecorator
|
||||
can [:destroy], StandingLineItem do |standing_line_item|
|
||||
user.enterprises.include?(standing_line_item.standing_order.shop)
|
||||
end
|
||||
can [:admin, :cancel], StandingOrderOrder do |standing_order_order|
|
||||
can [:admin, :cancel, :resume], StandingOrderOrder do |standing_order_order|
|
||||
user.enterprises.include?(standing_order_order.standing_order.shop)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -185,6 +185,7 @@ Openfoodnetwork::Application.routes.draw do
|
||||
|
||||
resources :standing_order_orders, only: [], format: :json do
|
||||
put :cancel, on: :member
|
||||
put :resume, on: :member
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
@@ -62,4 +62,69 @@ describe Admin::StandingOrderOrdersController, type: :controller do
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe 'resume' do
|
||||
let!(:user) { create(:user, enterprise_limit: 10) }
|
||||
let!(:shop) { create(:distributor_enterprise) }
|
||||
let!(:order_cycle) { create(:simple_order_cycle, orders_close_at: 1.day.from_now) }
|
||||
let!(:payment_method) { create(:payment_method) }
|
||||
let!(:order) { create(:order, shipping_method: create(:shipping_method), order_cycle: order_cycle) }
|
||||
let!(:standing_order) { create(:standing_order_with_items, shop: shop, orders: [order]) }
|
||||
let!(:standing_order_order) { standing_order.standing_order_orders.first }
|
||||
|
||||
before do
|
||||
# Processing order to completion
|
||||
while !order.completed? do break unless order.next! end
|
||||
standing_order_order.update_attribute(:cancelled_at, Time.zone.now)
|
||||
order.cancel
|
||||
allow(controller).to receive(:spree_current_user) { user }
|
||||
end
|
||||
|
||||
context 'json' do
|
||||
let(:params) { { format: :json, id: standing_order_order.id } }
|
||||
|
||||
context 'as a regular user' do
|
||||
it 'redirects to unauthorized' do
|
||||
spree_put :resume, params
|
||||
expect(response).to redirect_to spree.unauthorized_path
|
||||
end
|
||||
end
|
||||
|
||||
context 'as an enterprise user' do
|
||||
context "without authorisation" do
|
||||
let!(:shop2) { create(:distributor_enterprise) }
|
||||
before { shop2.update_attributes(owner: user) }
|
||||
|
||||
it 'redirects to unauthorized' do
|
||||
spree_put :resume, params
|
||||
expect(response).to redirect_to spree.unauthorized_path
|
||||
end
|
||||
end
|
||||
|
||||
context "with authorisation" do
|
||||
before { shop.update_attributes(owner: user) }
|
||||
|
||||
context "when resuming succeeds" do
|
||||
it 'renders the resumed standing_order_order as json' do
|
||||
spree_get :resume, params
|
||||
json_response = JSON.parse(response.body)
|
||||
expect(json_response['status']).to eq "resumed"
|
||||
expect(json_response['id']).to eq standing_order_order.id
|
||||
expect(standing_order_order.reload.cancelled_at).to be nil
|
||||
end
|
||||
end
|
||||
|
||||
context "when resuming fails" do
|
||||
before { order_cycle.update_attributes(orders_close_at: 1.day.ago) }
|
||||
|
||||
it "shows an error" do
|
||||
spree_get :resume, params
|
||||
json_response = JSON.parse(response.body)
|
||||
expect(json_response['errors']).to eq ['Could not resume the order']
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user