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 = $c->objects->find( Koha::Suggestions->new, $c->param('suggestion_id') );
68 unless ($suggestion) {
71 openapi => { error => "Suggestion not found." }
77 openapi => $suggestion
81 $c->unhandled_exception($_);
87 Controller method that handles adding a new Koha::Suggestion object
92 my $c = shift->openapi->valid_input or return;
94 my $body = $c->req->json;
96 my $overrides = $c->stash('koha.overrides');
98 unless ( $overrides->{any} ) {
100 unless ( $overrides->{max_total} ) {
102 if ( C4::Context->preference('MaxTotalSuggestions') ne ''
103 && C4::Context->preference('NumberOfSuggestionDays') ne '' )
105 my $max_total = C4::Context->preference('MaxTotalSuggestions');
106 my $days_range = C4::Context->preference('NumberOfSuggestionDays');
108 if ( $max_total and $days_range ) {
110 my $total = Koha::Suggestions->search({ suggestedby => $body->{suggested_by} })
111 ->filter_by_suggested_days_range( $days_range )
114 if ( $total >= $max_total ) {
118 error => "Reached the maximum suggestions limit",
119 error_code => 'max_total_reached'
127 unless ( $overrides->{max_pending} ) {
128 if ( C4::Context->preference('MaxOpenSuggestions') ne '' ) {
129 my $total_pending = Koha::Suggestions->search({ suggestedby => $body->{suggested_by} })
132 if ( $total_pending >= C4::Context->preference('MaxOpenSuggestions') ) {
136 error => "Reached the maximum pending suggestions limit",
137 error_code => 'max_pending_reached'
146 my $suggestion = Koha::Suggestion->new_from_api( $body )->store;
147 $suggestion->discard_changes;
148 $c->res->headers->location( $c->req->url->to_string . '/' . $suggestion->suggestionid );
152 openapi => $suggestion->to_api
156 $c->unhandled_exception($_);
162 Controller method that handles modifying Koha::Suggestion object
167 my $c = shift->openapi->valid_input or return;
169 my $suggestion = Koha::Suggestions->find( $c->param('suggestion_id') );
173 openapi => { error => 'Suggestion not found.' }
174 ) unless $suggestion;
178 my $body = $c->req->json;
180 $suggestion->set_from_api( $body )->store;
181 $suggestion->discard_changes;
185 openapi => $suggestion->to_api
189 $c->unhandled_exception($_);
196 Controller method that handles removing a Koha::Suggestion object
201 my $c = shift->openapi->valid_input or return;
203 my $suggestion = Koha::Suggestions->find( $c->param('suggestion_id') );
207 openapi => { error => 'Suggestion not found.' }
208 ) unless $suggestion;
218 $c->unhandled_exception($_);
224 Return the list of possible suggestions' managers
229 my $c = shift->openapi->valid_input or return;
233 my $patrons_rs = Koha::Patrons->search->filter_by_have_permission('suggestions.suggestions_manage');
234 my $patrons = $c->objects->search( $patrons_rs );
242 $c->unhandled_exception($_);