1 package Koha::REST::V1::Suggestions;
3 # This file is part of Koha.
5 # Koha is free software; you can redistribute it and/or modify it under the
6 # terms of the GNU General Public License as published by the Free Software
7 # Foundation; either version 3 of the License, or (at your option) any later
10 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
11 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
14 # You should have received a copy of the GNU General Public License
15 # along with Koha; if not, see <http:°www.gnu.org/licenses>.
19 use Mojo::Base 'Mojolicious::Controller';
21 use Koha::Suggestions;
27 Koha::REST::V1::Suggestion
35 Controller method that handles listing Koha::Suggestion objects
40 my $c = shift->openapi->valid_input or return;
44 my $suggestions = $c->objects->search( Koha::Suggestions->new );
48 openapi => $suggestions
52 $c->unhandled_exception($_);
58 Controller method that handles retrieving a single Koha::Suggestion object
63 my $c = shift->openapi->valid_input or return;
66 my $suggestion_id = $c->validation->param('suggestion_id');
67 my $suggestion = $c->objects->find( Koha::Suggestions->new, $suggestion_id );
69 unless ($suggestion) {
72 openapi => { error => "Suggestion not found." }
78 openapi => $suggestion
82 $c->unhandled_exception($_);
88 Controller method that handles adding a new Koha::Suggestion object
93 my $c = shift->openapi->valid_input or return;
95 my $body = $c->validation->param('body');
97 # FIXME: This should be handled in Koha::Suggestion->store
98 $body->{'status'} = 'ASKED'
99 unless defined $body->{'status'};
101 my $overrides = $c->stash('koha.overrides');
103 unless ( $overrides->{any} ) {
105 unless ( $overrides->{max_total} ) {
107 if ( C4::Context->preference('MaxTotalSuggestions') ne ''
108 && C4::Context->preference('NumberOfSuggestionDays') ne '' )
110 my $max_total = C4::Context->preference('MaxTotalSuggestions');
111 my $days_range = C4::Context->preference('NumberOfSuggestionDays');
113 if ( $max_total and $days_range ) {
115 my $total = Koha::Suggestions->search({ suggestedby => $body->{suggested_by} })
116 ->filter_by_suggested_days_range( $days_range )
119 if ( $total >= $max_total ) {
123 error => "Reached the maximum suggestions limit",
124 error_code => 'max_total_reached'
132 unless ( $overrides->{max_pending} ) {
133 if ( C4::Context->preference('MaxOpenSuggestions') ne '' ) {
134 my $total_pending = Koha::Suggestions->search({ suggestedby => $body->{suggested_by} })
137 if ( $total_pending >= C4::Context->preference('MaxOpenSuggestions') ) {
141 error => "Reached the maximum pending suggestions limit",
142 error_code => 'max_pending_reached'
151 my $suggestion = Koha::Suggestion->new_from_api( $body )->store;
152 $suggestion->discard_changes;
153 $c->res->headers->location( $c->req->url->to_string . '/' . $suggestion->suggestionid );
157 openapi => $suggestion->to_api
161 $c->unhandled_exception($_);
167 Controller method that handles modifying Koha::Suggestion object
172 my $c = shift->openapi->valid_input or return;
174 my $suggestion_id = $c->validation->param('suggestion_id');
175 my $suggestion = Koha::Suggestions->find( $suggestion_id );
179 openapi => { error => 'Suggestion not found.' }
180 ) unless $suggestion;
184 my $body = $c->validation->param('body');
186 $suggestion->set_from_api( $body )->store;
187 $suggestion->discard_changes;
191 openapi => $suggestion->to_api
195 $c->unhandled_exception($_);
202 Controller method that handles removing a Koha::Suggestion object
207 my $c = shift->openapi->valid_input or return;
209 my $suggestion_id = $c->validation->param('suggestion_id');
210 my $suggestion = Koha::Suggestions->find( $suggestion_id );
214 openapi => { error => 'Suggestion not found.' }
215 ) unless $suggestion;
225 $c->unhandled_exception($_);
231 Return the list of possible suggestions' managers
236 my $c = shift->openapi->valid_input or return;
240 my $patrons_rs = Koha::Patrons->search->filter_by_have_permission('suggestions.suggestions_manage');
241 my $patrons = $c->objects->search( $patrons_rs );
249 $c->unhandled_exception($_);