From 57f1742f2422e4e9a1ee7910e4c0fe1879fe800a Mon Sep 17 00:00:00 2001 From: luisramos0 Date: Wed, 17 Jul 2019 10:26:14 +0100 Subject: [PATCH 1/3] Save subscription form on subs unpause so that proxy orders and orders are synced --- app/controllers/admin/subscriptions_controller.rb | 10 ++++++++-- .../controllers/admin/subscriptions_controller_spec.rb | 9 +++++++++ 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/app/controllers/admin/subscriptions_controller.rb b/app/controllers/admin/subscriptions_controller.rb index 1233628dd6..a5d4f2aa54 100644 --- a/app/controllers/admin/subscriptions_controller.rb +++ b/app/controllers/admin/subscriptions_controller.rb @@ -1,4 +1,5 @@ require 'open_food_network/permissions' +require 'open_food_network/proxy_order_syncer' module Admin class SubscriptionsController < ResourceController @@ -67,8 +68,13 @@ module Admin end def unpause - @subscription.update_attributes(paused_at: nil) - render_as_json @subscription + params[:subscription][:paused_at] = nil + 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 end private diff --git a/spec/controllers/admin/subscriptions_controller_spec.rb b/spec/controllers/admin/subscriptions_controller_spec.rb index cf4af7c921..d3ea76dcd2 100644 --- a/spec/controllers/admin/subscriptions_controller_spec.rb +++ b/spec/controllers/admin/subscriptions_controller_spec.rb @@ -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 From 81482683cf2c5d42e7560179fced1808f7fbf956 Mon Sep 17 00:00:00 2001 From: luisramos0 Date: Wed, 17 Jul 2019 10:33:38 +0100 Subject: [PATCH 2/3] Extract method in subs controller --- .../admin/subscriptions_controller.rb | 35 +++++++++---------- .../admin/subscriptions_controller_spec.rb | 2 +- 2 files changed, 18 insertions(+), 19 deletions(-) diff --git a/app/controllers/admin/subscriptions_controller.rb b/app/controllers/admin/subscriptions_controller.rb index a5d4f2aa54..75225ee0cc 100644 --- a/app/controllers/admin/subscriptions_controller.rb +++ b/app/controllers/admin/subscriptions_controller.rb @@ -33,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 @@ -69,16 +59,25 @@ module Admin def unpause params[:subscription][:paused_at] = nil - 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 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) diff --git a/spec/controllers/admin/subscriptions_controller_spec.rb b/spec/controllers/admin/subscriptions_controller_spec.rb index d3ea76dcd2..445ec70ff7 100644 --- a/spec/controllers/admin/subscriptions_controller_spec.rb +++ b/spec/controllers/admin/subscriptions_controller_spec.rb @@ -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 From 3d6fd10e591b695b491e8808ada39a3e8a2c8ae7 Mon Sep 17 00:00:00 2001 From: luisramos0 Date: Wed, 17 Jul 2019 12:14:36 +0100 Subject: [PATCH 3/3] Add warning about creation of orders when unpausing a subscription --- config/locales/en.yml | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/config/locales/en.yml b/config/locales/en.yml index 015624df34..3379c55eb8 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -1079,12 +1079,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