Bug 32997: (QA follow-up) values => authorised_values
[srvgit] / Koha / REST / V1 / Suggestions.pm
1 package Koha::REST::V1::Suggestions;
2
3 # This file is part of Koha.
4 #
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
8 # version.
9 #
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.
13 #
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>.
16
17 use Modern::Perl;
18
19 use Mojo::Base 'Mojolicious::Controller';
20
21 use Koha::Suggestions;
22
23 use Try::Tiny;
24
25 =head1 NAME
26
27 Koha::REST::V1::Suggestion
28
29 =head1 API
30
31 =head2 Methods
32
33 =head3 list
34
35 Controller method that handles listing Koha::Suggestion objects
36
37 =cut
38
39 sub list {
40     my $c = shift->openapi->valid_input or return;
41
42     return try {
43
44         my $suggestions = $c->objects->search( Koha::Suggestions->new );
45
46         return $c->render(
47             status  => 200,
48             openapi => $suggestions
49         );
50     }
51     catch {
52         $c->unhandled_exception($_);
53     };
54 }
55
56 =head3 get
57
58 Controller method that handles retrieving a single Koha::Suggestion object
59
60 =cut
61
62 sub get {
63     my $c = shift->openapi->valid_input or return;
64
65     return try {
66         my $suggestion_id = $c->validation->param('suggestion_id');
67         my $suggestion = $c->objects->find( Koha::Suggestions->new, $suggestion_id );
68
69         unless ($suggestion) {
70             return $c->render(
71                 status  => 404,
72                 openapi => { error => "Suggestion not found." }
73             );
74         }
75
76         return $c->render(
77             status  => 200,
78             openapi => $suggestion
79         );
80     }
81     catch {
82         $c->unhandled_exception($_);
83     };
84 }
85
86 =head3 add
87
88 Controller method that handles adding a new Koha::Suggestion object
89
90 =cut
91
92 sub add {
93     my $c = shift->openapi->valid_input or return;
94
95     my $body = $c->validation->param('body');
96
97     # FIXME: This should be handled in Koha::Suggestion->store
98     $body->{'status'} = 'ASKED'
99         unless defined $body->{'status'};
100
101     my $overrides = $c->stash('koha.overrides');
102
103     unless ( $overrides->{any} ) {
104
105         unless ( $overrides->{max_total} ) {
106
107             if (   C4::Context->preference('MaxTotalSuggestions') ne ''
108                 && C4::Context->preference('NumberOfSuggestionDays') ne '' )
109             {
110                 my $max_total = C4::Context->preference('MaxTotalSuggestions');
111                 my $days_range = C4::Context->preference('NumberOfSuggestionDays');
112
113                 if ( $max_total and $days_range ) {
114
115                     my $total = Koha::Suggestions->search({ suggestedby => $body->{suggested_by} })
116                                                  ->filter_by_suggested_days_range( $days_range )
117                                                  ->count;
118
119                     if ( $total >= $max_total ) {
120                         return $c->render(
121                             status  => 400,
122                             openapi => {
123                                 error       => "Reached the maximum suggestions limit",
124                                 error_code  => 'max_total_reached'
125                             }
126                         );
127                     }
128                 }
129             }
130         }
131
132         unless ( $overrides->{max_pending} ) {
133             if ( C4::Context->preference('MaxOpenSuggestions') ne '' ) {
134                 my $total_pending = Koha::Suggestions->search({ suggestedby => $body->{suggested_by} })
135                                                   ->filter_by_pending
136                                                   ->count;
137                 if ( $total_pending >= C4::Context->preference('MaxOpenSuggestions') ) {
138                     return $c->render(
139                         status  => 400,
140                         openapi => {
141                             error       => "Reached the maximum pending suggestions limit",
142                             error_code  => 'max_pending_reached'
143                         }
144                     );
145                 }
146             }
147         }
148     }
149
150     return try {
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 );
154
155         return $c->render(
156             status  => 201,
157             openapi => $suggestion->to_api
158         );
159     }
160     catch {
161         $c->unhandled_exception($_);
162     };
163 }
164
165 =head3 update
166
167 Controller method that handles modifying Koha::Suggestion object
168
169 =cut
170
171 sub update {
172     my $c = shift->openapi->valid_input or return;
173
174     my $suggestion_id = $c->validation->param('suggestion_id');
175     my $suggestion = Koha::Suggestions->find( $suggestion_id );
176
177     return $c->render(
178         status  => 404,
179         openapi => { error => 'Suggestion not found.' }
180     ) unless $suggestion;
181
182     return try {
183
184         my $body = $c->validation->param('body');
185
186         $suggestion->set_from_api( $body )->store;
187         $suggestion->discard_changes;
188
189         return $c->render(
190             status  => 200,
191             openapi => $suggestion->to_api
192         );
193     }
194     catch {
195         $c->unhandled_exception($_);
196     };
197
198 }
199
200 =head3 delete
201
202 Controller method that handles removing a Koha::Suggestion object
203
204 =cut
205
206 sub delete {
207     my $c = shift->openapi->valid_input or return;
208
209     my $suggestion_id = $c->validation->param('suggestion_id');
210     my $suggestion = Koha::Suggestions->find( $suggestion_id );
211
212     return $c->render(
213         status  => 404,
214         openapi => { error => 'Suggestion not found.' }
215     ) unless $suggestion;
216
217     return try {
218         $suggestion->delete;
219         return $c->render(
220             status  => 204,
221             openapi => q{}
222         );
223     }
224     catch {
225         $c->unhandled_exception($_);
226     };
227 }
228
229 =head3 list_managers
230
231 Return the list of possible suggestions' managers
232
233 =cut
234
235 sub list_managers {
236     my $c = shift->openapi->valid_input or return;
237
238     return try {
239
240         my $patrons_rs = Koha::Patrons->search->filter_by_have_permission('suggestions.suggestions_manage');
241         my $patrons    = $c->objects->search( $patrons_rs );
242
243         return $c->render(
244             status  => 200,
245             openapi => $patrons
246         );
247     }
248     catch {
249         $c->unhandled_exception($_);
250     };
251 }
252
253 1;