From 2c61a7af9567792eb0c7b27f1e0ab275ae7a2809 Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Fri, 7 Apr 2023 11:35:06 +0200 Subject: [PATCH] Bug 30708: Do not allow deletion of processings that are used Sponsored-by: BULAC - http://www.bulac.fr/ Signed-off-by: BULAC - http://www.bulac.fr/ Signed-off-by: Heather Hernandez Signed-off-by: Laurence Rault Signed-off-by: Marcel de Rooy Signed-off-by: Tomas Cohen Arazi --- Koha/Preservation/Processing.pm | 20 ++++++++++ Koha/REST/V1/Preservation/Processings.pm | 9 ++++- .../Preservation/SettingsProcessings.vue | 16 ++++++-- .../Koha/Preservation/Processings.t | 37 ++++++++++++++++++- 4 files changed, 77 insertions(+), 5 deletions(-) diff --git a/Koha/Preservation/Processing.pm b/Koha/Preservation/Processing.pm index e3c5ef95dc..5fe6d2b85b 100644 --- a/Koha/Preservation/Processing.pm +++ b/Koha/Preservation/Processing.pm @@ -21,6 +21,8 @@ use Koha::Database; use base qw(Koha::Object); +use Koha::Preservation::Trains; +use Koha::Preservation::Train::Items; use Koha::Preservation::Processing::Attributes; =head1 NAME @@ -80,6 +82,24 @@ sub attributes { return Koha::Preservation::Processing::Attributes->_new_from_dbic($attributes_rs); } +=head3 can_be_deleted + +A processing can be deleted if it is not used from any trains or items. +Note that we do not enforce that in ->delete, the callers are supposed to deal with that correctly. + +=cut + +sub can_be_deleted { + my ($self) = @_; + + my $trains_using_it = Koha::Preservation::Trains->search( + { default_processing_id => $self->processing_id } )->count; + my $items_using_it = Koha::Preservation::Train::Items->search( + { processing_id => $self->processing_id } )->count; + + return ( $trains_using_it || $items_using_it ) ? 0 : 1; +} + =head2 Internal methods =head3 _type diff --git a/Koha/REST/V1/Preservation/Processings.pm b/Koha/REST/V1/Preservation/Processings.pm index 6b04e9b2f5..98a924b68a 100644 --- a/Koha/REST/V1/Preservation/Processings.pm +++ b/Koha/REST/V1/Preservation/Processings.pm @@ -227,7 +227,8 @@ sub update { sub delete { my $c = shift->openapi->valid_input or return; - my $processing = Koha::Preservation::Processings->find( $c->validation->param('processing_id') ); + my $processing_id = $c->validation->param('processing_id'); + my $processing = Koha::Preservation::Processings->find( $processing_id ); unless ($processing) { return $c->render( status => 404, @@ -235,6 +236,12 @@ sub delete { ); } + unless ($processing->can_be_deleted) { + return $c->render( + status => 409, + openapi => { error => "Processing is already used" }, + ); + } return try { $processing->delete; return $c->render( diff --git a/koha-tmpl/intranet-tmpl/prog/js/vue/components/Preservation/SettingsProcessings.vue b/koha-tmpl/intranet-tmpl/prog/js/vue/components/Preservation/SettingsProcessings.vue index ac7e54d7aa..5c0c9c5f18 100644 --- a/koha-tmpl/intranet-tmpl/prog/js/vue/components/Preservation/SettingsProcessings.vue +++ b/koha-tmpl/intranet-tmpl/prog/js/vue/components/Preservation/SettingsProcessings.vue @@ -44,8 +44,9 @@ import { APIClient } from "../../fetch/api-client.js" export default { setup() { - const { setConfirmationDialog, setMessage } = inject("mainStore") - return { setConfirmationDialog, setMessage } + const { setConfirmationDialog, setMessage, setError } = + inject("mainStore") + return { setConfirmationDialog, setMessage, setError } }, data() { return { @@ -90,7 +91,16 @@ export default { error => {} ) }, - error => {} + error => { + // FIXME We need a better way to do that + if (error.toString().match(/409/)) { + this.setError( + this.$__( + "This processing cannot be deleted, it is already in used." + ) + ) + } + } ) } ) diff --git a/t/db_dependent/Koha/Preservation/Processings.t b/t/db_dependent/Koha/Preservation/Processings.t index d0c1bb1d39..62e8542b05 100755 --- a/t/db_dependent/Koha/Preservation/Processings.t +++ b/t/db_dependent/Koha/Preservation/Processings.t @@ -17,7 +17,7 @@ use Modern::Perl; -use Test::More tests => 1; +use Test::More tests => 2; use Test::Exception; use Test::Warn; @@ -56,3 +56,38 @@ subtest 'attributes' => sub { $schema->storage->txn_rollback; }; + +subtest 'can_be_deleted' => sub { + + plan tests => 5; + + $schema->storage->txn_begin; + + my $processing = $builder->build_object( { class => 'Koha::Preservation::Processings' } ); + my $another_processing = $builder->build_object( { class => 'Koha::Preservation::Processings' } ); + + is( $processing->can_be_deleted, 1, 'processing is not used, it can be deleted' ); + + my $train = $builder->build_object( + { + class => 'Koha::Preservation::Trains', + value => { + not_for_loan => 42, + default_processing_id => $processing->processing_id, + closed_on => undef, + sent_on => undef, + received_on => undef + } + } + ); + + is( $processing->can_be_deleted, 0, 'processing is used, it cannot be deleted' ); + is( $another_processing->can_be_deleted, 1, 'processing is not used, it can be deleted' ); + + my $item = $builder->build_sample_item; + $train->add_item( { item_id => $item->itemnumber, processing_id => $another_processing->processing_id }, { skip_waiting_list_check => 1 } ); + is( $processing->can_be_deleted, 0, 'processing is used, it cannot be deleted' ); + is( $another_processing->can_be_deleted, 0, 'processing is used, it cannot be deleted' ); + + $schema->storage->txn_rollback; +}; -- 2.39.5