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 <heather_hernandez@nps.gov>
Signed-off-by: Laurence Rault <laurence.rault@biblibre.com>

Signed-off-by: Marcel de Rooy <m.de.rooy@rijksmuseum.nl>
Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>
This commit is contained in:
Jonathan Druart 2023-04-07 11:35:06 +02:00 committed by Tomas Cohen Arazi
parent eae94108e7
commit 2c61a7af95
Signed by: tomascohen
GPG key ID: 0A272EA1B2F3C15F
4 changed files with 77 additions and 5 deletions

View file

@ -21,6 +21,8 @@ use Koha::Database;
use base qw(Koha::Object); use base qw(Koha::Object);
use Koha::Preservation::Trains;
use Koha::Preservation::Train::Items;
use Koha::Preservation::Processing::Attributes; use Koha::Preservation::Processing::Attributes;
=head1 NAME =head1 NAME
@ -80,6 +82,24 @@ sub attributes {
return Koha::Preservation::Processing::Attributes->_new_from_dbic($attributes_rs); 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 =head2 Internal methods
=head3 _type =head3 _type

View file

@ -227,7 +227,8 @@ sub update {
sub delete { sub delete {
my $c = shift->openapi->valid_input or return; 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) { unless ($processing) {
return $c->render( return $c->render(
status => 404, 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 { return try {
$processing->delete; $processing->delete;
return $c->render( return $c->render(

View file

@ -44,8 +44,9 @@ import { APIClient } from "../../fetch/api-client.js"
export default { export default {
setup() { setup() {
const { setConfirmationDialog, setMessage } = inject("mainStore") const { setConfirmationDialog, setMessage, setError } =
return { setConfirmationDialog, setMessage } inject("mainStore")
return { setConfirmationDialog, setMessage, setError }
}, },
data() { data() {
return { return {
@ -90,7 +91,16 @@ export default {
error => {} 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."
)
)
}
}
) )
} }
) )

View file

@ -17,7 +17,7 @@
use Modern::Perl; use Modern::Perl;
use Test::More tests => 1; use Test::More tests => 2;
use Test::Exception; use Test::Exception;
use Test::Warn; use Test::Warn;
@ -56,3 +56,38 @@ subtest 'attributes' => sub {
$schema->storage->txn_rollback; $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;
};