From b735d4ad66ed6600a5f4c3de235aa4426726092e 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 (cherry picked from commit 40ca7aa06c166b005fda5b6ff38f4cbf20b3d081) Signed-off-by: Fridolin Somers (cherry picked from commit 8ea062497a04956811a7edf899b28019a89fd8c9) Signed-off-by: Matt Blenkinsop --- Koha/Exceptions/Suggestion.pm | 49 +++++++++++++++++++++++++++++++++++ Koha/REST/V1/Suggestions.pm | 4 --- Koha/Suggestion.pm | 14 ++++++++++ 3 files changed, 63 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 4bc27709d1..293aa28fbd 100644 --- a/Koha/REST/V1/Suggestions.pm +++ b/Koha/REST/V1/Suggestions.pm @@ -94,10 +94,6 @@ sub add { my $body = $c->validation->param('body'); - # 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 9d9e460a65..e45b71f988 100644 --- a/Koha/Suggestion.pm +++ b/Koha/Suggestion.pm @@ -23,6 +23,8 @@ use Modern::Perl; 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); @@ -46,6 +48,18 @@ a suggesteddate of today 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() ) { $self->suggesteddate( dt_from_string()->ymd ); } -- 2.39.5