Add error message for case when schedule cannot be deleted

This commit is contained in:
Rob Harrington
2018-02-22 15:08:26 +11:00
committed by Maikel Linke
parent f9dbf61afc
commit a83bdf16c9
5 changed files with 31 additions and 8 deletions

View File

@@ -31,6 +31,13 @@ angular.module("admin.orderCycles").directive 'scheduleDialog', ($window, $compi
if confirm(t('are_you_sure'))
Schedules.remove(scope.schedule).$promise.then (data) ->
scope.close()
, (response) ->
errors = response.data.errors
if errors?
scope.errors.push errors[0]
else
scope.errors.push "Could not delete schedule: #{scope.schedule.name}"
scope.loadMore = ->
scope.showMore().then ->

View File

@@ -39,12 +39,6 @@ angular.module("admin.resources").factory "Schedules", ($q, $injector, RequestMo
orderCycle.schedules.splice(i, 1) for s, i in orderCycle.schedules by -1 when s.id == schedule.id
delete @byID[schedule.id]
StatusMessage.display 'success', "#{t('admin.order_cycles.index.deleted_schedule')}: '#{schedule.name}'"
, (response) =>
errors = response.data.errors
if errors?
InfoDialog.open 'error', errors[0]
else
InfoDialog.open 'error', "Could not delete schedule: #{schedule.name}"
index: ->
request = ScheduleResource.index (data) => @load(data)

View File

@@ -4,6 +4,7 @@ require 'open_food_network/proxy_order_syncer'
module Admin
class SchedulesController < ResourceController
before_filter :check_editable_order_cycle_ids, only: [:create, :update]
before_filter :check_dependent_subscriptions, only: [:destroy]
create.after :sync_subscriptions
update.after :sync_subscriptions
@@ -49,6 +50,11 @@ module Admin
@object.order_cycle_ids = result
end
def check_dependent_subscriptions
return if Subscription.where(schedule_id: @schedule).empty?
render json: { errors: [t('admin.schedules.destroy.associated_subscriptions_error')] }, status: :conflict
end
def permissions
return @permissions unless @permission.nil?
@permissions = OpenFoodNetwork::Permissions.new(spree_current_user)

View File

@@ -958,6 +958,10 @@ en:
why_dont_you_add_one: Why don't you add one? :)
no_matching_subscriptions: No matching subscriptions found
schedules:
destroy:
associated_subscriptions_error: This schedule cannot be deleted because it has associated subscriptions
stripe_connect_settings:
edit:
title: "Stripe Connect"

View File

@@ -200,8 +200,20 @@ describe Admin::SchedulesController, type: :controller do
context "where I manage at least one of the schedule's coordinators" do
before { params.merge!(id: coordinated_schedule.id) }
it "allows me to destroy the schedule" do
expect { spree_delete :destroy, params }.to change(Schedule, :count).by(-1)
context "when no dependent subscriptions are present" do
it "allows me to destroy the schedule" do
expect { spree_delete :destroy, params }.to change(Schedule, :count).by(-1)
end
end
context "when a dependent subscription is present" do
let!(:subscription) { create(:subscription, schedule: coordinated_schedule) }
it "returns an error message and prevents me from deleting the schedule" do
expect { spree_delete :destroy, params }.to_not change(Schedule, :count)
json_response = JSON.parse(response.body)
expect(json_response["errors"]).to include I18n.t('admin.schedules.destroy.associated_subscriptions_error')
end
end
end