From 40ca7aa06c166b005fda5b6ff38f4cbf20b3d081 Mon Sep 17 00:00:00 2001 From: Arthur Suzuki Date: Fri, 18 Aug 2023 13:16:53 +0200 Subject: [PATCH] Bug 32942: (QA follow-up) Moving Suggestion->STATUS check to Suggestion::store Signed-off-by: Martin Renvoize Signed-off-by: Tomas Cohen Arazi --- Koha/Exceptions/Suggestion.pm | 49 +++++++++++++++++++++++++++++++++++ Koha/REST/V1/Suggestions.pm | 4 --- Koha/Suggestion.pm | 11 ++++++++ 3 files changed, 60 insertions(+), 4 deletions(-) create mode 100644 Koha/Exceptions/Suggestion.pm diff --git a/Koha/Exceptions/Suggestion.pm b/Koha/Exceptions/Suggestion.pm new file mode 100644 index 0000000000..04e50b18ed --- /dev/null +++ b/Koha/Exceptions/Suggestion.pm @@ -0,0 +1,49 @@ +package Koha::Exceptions::Suggestion; + +# This file is part of Koha. +# +# Koha is free software; you can redistribute it and/or modify it +# under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# Koha is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Koha; if not, see . + +use Modern::Perl; + +use Koha::Exception; + +use Exception::Class ( + 'Koha::Exceptions::Suggestion' => { + isa => 'Koha::Exception', + }, + 'Koha::Exceptions::Suggestion::StatusForbidden' => { + isa => 'Koha::Exceptions::Suggestion', + description => 'This status is forbidden, check authorised values "SUGGEST"', + fields => ['STATUS'] + } +); + +=head1 NAME + +Koha::Exceptions::Suggestion - Base class for Suggestion exceptions + +=head1 Exceptions + +=head2 Koha::Exceptions::Suggestion + +Generic Suggestion exception + +=head2 Koha::Exceptions::Suggestion::StatusIsUnknown + +Exception to be used when a purchase suggestion tries to be saved and the status doesn't belong to the list of authorised_values. + +=cut + +1; diff --git a/Koha/REST/V1/Suggestions.pm b/Koha/REST/V1/Suggestions.pm index 67bcbe04fd..ba21194a54 100644 --- a/Koha/REST/V1/Suggestions.pm +++ b/Koha/REST/V1/Suggestions.pm @@ -93,10 +93,6 @@ sub add { my $body = $c->req->json; - # FIXME: This should be handled in Koha::Suggestion->store - $body->{'status'} = 'ASKED' - unless defined $body->{'status'}; - my $overrides = $c->stash('koha.overrides'); unless ( $overrides->{any} ) { diff --git a/Koha/Suggestion.pm b/Koha/Suggestion.pm index b9fe4f1529..f4d4156675 100644 --- a/Koha/Suggestion.pm +++ b/Koha/Suggestion.pm @@ -25,6 +25,8 @@ use C4::Letters; use Koha::Database; use Koha::DateUtils qw( dt_from_string ); use Koha::Patrons; +use Koha::AuthorisedValues; +use Koha::Exceptions::Suggestion; use base qw(Koha::Object); @@ -49,6 +51,15 @@ sub store { my ($self) = @_; $self->STATUS("ASKED") unless $self->STATUS; + my @status_constants = qw(ASKED CHECKED ACCEPTED REJECTED); + Koha::Exceptions::Suggestion::StatusForbidden->throw( STATUS => $self->STATUS ) + unless ( grep { $self->STATUS eq $_ } @status_constants ) + || Koha::AuthorisedValues->search( + { + category => 'SUGGEST_STATUS', + authorised_value => $self->STATUS + } + )->count; $self->branchcode(undef) if defined $self->branchcode && $self->branchcode eq ''; unless ( $self->suggesteddate() ) { -- 2.39.5