Bug 32942: (QA follow-up) Moving Suggestion->STATUS check to Suggestion::store
[koha.git] / 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 = $c->objects->find( Koha::Suggestions->new, $c->param('suggestion_id') );
67
68         unless ($suggestion) {
69             return $c->render(
70                 status  => 404,
71                 openapi => { error => "Suggestion not found." }
72             );
73         }
74
75         return $c->render(
76             status  => 200,
77             openapi => $suggestion
78         );
79     }
80     catch {
81         $c->unhandled_exception($_);
82     };
83 }
84
85 =head3 add
86
87 Controller method that handles adding a new Koha::Suggestion object
88
89 =cut
90
91 sub add {
92     my $c = shift->openapi->valid_input or return;
93
94     my $body = $c->req->json;
95
96     my $overrides = $c->stash('koha.overrides');
97
98     unless ( $overrides->{any} ) {
99
100         unless ( $overrides->{max_total} ) {
101
102             if (   C4::Context->preference('MaxTotalSuggestions') ne ''
103                 && C4::Context->preference('NumberOfSuggestionDays') ne '' )
104             {
105                 my $max_total = C4::Context->preference('MaxTotalSuggestions');
106                 my $days_range = C4::Context->preference('NumberOfSuggestionDays');
107
108                 if ( $max_total and $days_range ) {
109
110                     my $total = Koha::Suggestions->search({ suggestedby => $body->{suggested_by} })
111                                                  ->filter_by_suggested_days_range( $days_range )
112                                                  ->count;
113
114                     if ( $total >= $max_total ) {
115                         return $c->render(
116                             status  => 400,
117                             openapi => {
118                                 error       => "Reached the maximum suggestions limit",
119                                 error_code  => 'max_total_reached'
120                             }
121                         );
122                     }
123                 }
124             }
125         }
126
127         unless ( $overrides->{max_pending} ) {
128             if ( C4::Context->preference('MaxOpenSuggestions') ne '' ) {
129                 my $total_pending = Koha::Suggestions->search({ suggestedby => $body->{suggested_by} })
130                                                   ->filter_by_pending
131                                                   ->count;
132                 if ( $total_pending >= C4::Context->preference('MaxOpenSuggestions') ) {
133                     return $c->render(
134                         status  => 400,
135                         openapi => {
136                             error       => "Reached the maximum pending suggestions limit",
137                             error_code  => 'max_pending_reached'
138                         }
139                     );
140                 }
141             }
142         }
143     }
144
145     return try {
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 );
149
150         return $c->render(
151             status  => 201,
152             openapi => $suggestion->to_api
153         );
154     }
155     catch {
156         $c->unhandled_exception($_);
157     };
158 }
159
160 =head3 update
161
162 Controller method that handles modifying Koha::Suggestion object
163
164 =cut
165
166 sub update {
167     my $c = shift->openapi->valid_input or return;
168
169     my $suggestion = Koha::Suggestions->find( $c->param('suggestion_id') );
170
171     return $c->render(
172         status  => 404,
173         openapi => { error => 'Suggestion not found.' }
174     ) unless $suggestion;
175
176     return try {
177
178         my $body = $c->req->json;
179
180         $suggestion->set_from_api( $body )->store;
181         $suggestion->discard_changes;
182
183         return $c->render(
184             status  => 200,
185             openapi => $suggestion->to_api
186         );
187     }
188     catch {
189         $c->unhandled_exception($_);
190     };
191
192 }
193
194 =head3 delete
195
196 Controller method that handles removing a Koha::Suggestion object
197
198 =cut
199
200 sub delete {
201     my $c = shift->openapi->valid_input or return;
202
203     my $suggestion = Koha::Suggestions->find( $c->param('suggestion_id') );
204
205     return $c->render(
206         status  => 404,
207         openapi => { error => 'Suggestion not found.' }
208     ) unless $suggestion;
209
210     return try {
211         $suggestion->delete;
212         return $c->render(
213             status  => 204,
214             openapi => q{}
215         );
216     }
217     catch {
218         $c->unhandled_exception($_);
219     };
220 }
221
222 =head3 list_managers
223
224 Return the list of possible suggestions' managers
225
226 =cut
227
228 sub list_managers {
229     my $c = shift->openapi->valid_input or return;
230
231     return try {
232
233         my $patrons_rs = Koha::Patrons->search->filter_by_have_permission('suggestions.suggestions_manage');
234         my $patrons    = $c->objects->search( $patrons_rs );
235
236         return $c->render(
237             status  => 200,
238             openapi => $patrons
239         );
240     }
241     catch {
242         $c->unhandled_exception($_);
243     };
244 }
245
246 1;