Merge pull request #4051 from luisramos0/subs_unpause

Sync subscription (create/update orders) when subscription is unpaused
This commit is contained in:
Kristina Lim
2019-08-08 01:09:09 +08:00
committed by GitHub
3 changed files with 35 additions and 21 deletions

View File

@@ -1,4 +1,5 @@
require 'open_food_network/permissions'
require 'open_food_network/proxy_order_syncer'
module Admin
class SubscriptionsController < ResourceController
@@ -32,21 +33,11 @@ module Admin
end
def create
form = SubscriptionForm.new(@subscription, params[:subscription])
if form.save
render_as_json @subscription
else
render json: { errors: form.json_errors }, status: :unprocessable_entity
end
save_form_and_render(false)
end
def update
form = SubscriptionForm.new(@subscription, params[:subscription])
if form.save
render_as_json @subscription, order_update_issues: form.order_update_issues
else
render json: { errors: form.json_errors }, status: :unprocessable_entity
end
save_form_and_render
end
def cancel
@@ -67,12 +58,26 @@ module Admin
end
def unpause
@subscription.update_attributes(paused_at: nil)
render_as_json @subscription
params[:subscription][:paused_at] = nil
save_form_and_render
end
private
def save_form_and_render(render_issues = true)
form = SubscriptionForm.new(@subscription, params[:subscription])
unless form.save
render json: { errors: form.json_errors }, status: :unprocessable_entity
return
end
if render_issues
render_as_json @subscription, order_update_issues: form.order_update_issues
else
render_as_json @subscription
end
end
def permissions
return @permissions unless @permissions.nil?
@permissions = OpenFoodNetwork::Permissions.new(spree_current_user)

View File

@@ -1092,12 +1092,12 @@ en:
orders:
number: Number
confirm_edit: Are you sure you want to edit this order? Doing so may make it more difficult to automatically sync changes to the subscription in the future.
confirm_cancel_msg: Are you sure you want to cancel this subscription? This action cannot be undone.
cancel_failure_msg: 'Sorry, cancellation failed!'
confirm_pause_msg: Are you sure you want to pause this subscription?
pause_failure_msg: 'Sorry, pausing failed!'
confirm_unpause_msg: Are you sure you want to unpause this subscription?
unpause_failure_msg: 'Sorry, unpausing failed!'
confirm_cancel_msg: "Are you sure you want to cancel this subscription? This action cannot be undone."
cancel_failure_msg: "Sorry, cancellation failed!"
confirm_pause_msg: "Are you sure you want to pause this subscription?"
pause_failure_msg: "Sorry, pausing failed!"
confirm_unpause_msg: "If you have an open Order Cycle in this subscription's schedule, an order will be created for this customer. Are you sure you want to unpause this subscription?"
unpause_failure_msg: "Sorry, unpausing failed!"
confirm_cancel_open_orders_msg: "Some orders for this subscription are currently open. The customer has already been notified that the order will be placed. Would you like to cancel these order(s) or keep them?"
resume_canceled_orders_msg: "Some orders for this subscription can be resumed right now. You can resume them from the orders dropdown."
yes_cancel_them: Cancel them

View File

@@ -583,7 +583,7 @@ describe Admin::SubscriptionsController, type: :controller do
end
context 'json' do
let(:params) { { format: :json, id: subscription.id } }
let(:params) { { format: :json, id: subscription.id, subscription: {} } }
context 'as a regular user' do
it 'redirects to unauthorized' do
@@ -664,6 +664,15 @@ describe Admin::SubscriptionsController, type: :controller do
expect(json_response['id']).to eq subscription.id
expect(subscription.reload.paused_at).to be nil
end
context "when there is an open OC and no associated orders exist yet for it (OC was opened when the subscription was paused)" do
it "creates an associated order" do
spree_put :unpause, params
expect(subscription.reload.paused_at).to be nil
expect(subscription.proxy_orders.size).to be 1
end
end
end
end
end