Bug 24545: Fix license statements
[srvgit] / Koha / REST / V1 / Patrons.pm
1 package Koha::REST::V1::Patrons;
2
3 # This file is part of Koha.
4 #
5 # Koha is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
17
18 use Modern::Perl;
19
20 use Mojo::Base 'Mojolicious::Controller';
21
22 use Koha::DateUtils;
23 use Koha::Patrons;
24
25 use Scalar::Util qw(blessed);
26 use Try::Tiny;
27
28 =head1 NAME
29
30 Koha::REST::V1::Patrons
31
32 =head1 API
33
34 =head2 Methods
35
36 =head3 list
37
38 Controller function that handles listing Koha::Patron objects
39
40 =cut
41
42 sub list {
43     my $c = shift->openapi->valid_input or return;
44
45     return try {
46
47         my $patrons_rs = Koha::Patrons->new;
48         my $args = $c->validation->output;
49         my $attributes = {};
50
51         # Extract reserved params
52         my ( $filtered_params, $reserved_params ) = $c->extract_reserved_params($args);
53
54         my $restricted = delete $filtered_params->{restricted};
55
56         # Merge sorting into query attributes
57         $c->dbic_merge_sorting(
58             {
59                 attributes => $attributes,
60                 params     => $reserved_params,
61                 result_set => $patrons_rs
62             }
63         );
64
65         # Merge pagination into query attributes
66         $c->dbic_merge_pagination(
67             {
68                 filter => $attributes,
69                 params => $reserved_params
70             }
71         );
72
73         if ( defined $filtered_params ) {
74
75             # Apply the mapping function to the passed params
76             $filtered_params = $patrons_rs->attributes_from_api($filtered_params);
77             $filtered_params = $c->build_query_params( $filtered_params, $reserved_params );
78         }
79
80         # translate 'restricted' => 'debarred'
81         $filtered_params->{debarred} = { '!=' => undef }
82           if $restricted;
83
84         my $patrons = $patrons_rs->search( $filtered_params, $attributes );
85         if ( $patrons_rs->is_paged ) {
86             $c->add_pagination_headers(
87                 {
88                     total  => $patrons->pager->total_entries,
89                     params => $args,
90                 }
91             );
92         }
93
94         return $c->render( status => 200, openapi => $patrons->to_api );
95     }
96     catch {
97         if ( $_->isa('DBIx::Class::Exception') ) {
98             return $c->render(
99                 status  => 500,
100                 openapi => { error => $_->{msg} }
101             );
102         }
103         else {
104             return $c->render(
105                 status  => 500,
106                 openapi => { error => "Something went wrong, check the logs." }
107             );
108         }
109     };
110 }
111
112
113 =head3 get
114
115 Controller function that handles retrieving a single Koha::Patron object
116
117 =cut
118
119 sub get {
120     my $c = shift->openapi->valid_input or return;
121
122     my $patron_id = $c->validation->param('patron_id');
123     my $patron    = Koha::Patrons->find($patron_id);
124
125     unless ($patron) {
126         return $c->render( status => 404, openapi => { error => "Patron not found." } );
127     }
128
129     return $c->render( status => 200, openapi => $patron->to_api );
130 }
131
132 =head3 add
133
134 Controller function that handles adding a new Koha::Patron object
135
136 =cut
137
138 sub add {
139     my $c = shift->openapi->valid_input or return;
140
141     return try {
142
143         my $patron = Koha::Patron->new_from_api( $c->validation->param('body') )->store;
144
145         $c->res->headers->location( $c->req->url->to_string . '/' . $patron->borrowernumber );
146         return $c->render(
147             status  => 201,
148             openapi => $patron->to_api
149         );
150     }
151     catch {
152
153         my $to_api_mapping = Koha::Patron->new->to_api_mapping;
154
155         unless ( blessed $_ && $_->can('rethrow') ) {
156             return $c->render(
157                 status  => 500,
158                 openapi => { error => "Something went wrong, check Koha logs for details." }
159             );
160         }
161         if ( $_->isa('Koha::Exceptions::Object::DuplicateID') ) {
162             return $c->render(
163                 status  => 409,
164                 openapi => { error => $_->error, conflict => $_->duplicate_id }
165             );
166         }
167         elsif ( $_->isa('Koha::Exceptions::Object::FKConstraint') ) {
168             return $c->render(
169                 status  => 400,
170                 openapi => {
171                           error => "Given "
172                         . $to_api_mapping->{ $_->broken_fk }
173                         . " does not exist"
174                 }
175             );
176         }
177         elsif ( $_->isa('Koha::Exceptions::BadParameter') ) {
178             return $c->render(
179                 status  => 400,
180                 openapi => {
181                           error => "Given "
182                         . $to_api_mapping->{ $_->parameter }
183                         . " does not exist"
184                 }
185             );
186         }
187         else {
188             return $c->render(
189                 status  => 500,
190                 openapi => { error => "Something went wrong, check Koha logs for details." }
191             );
192         }
193     };
194 }
195
196
197 =head3 update
198
199 Controller function that handles updating a Koha::Patron object
200
201 =cut
202
203 sub update {
204     my $c = shift->openapi->valid_input or return;
205
206     my $patron_id = $c->validation->param('patron_id');
207     my $patron    = Koha::Patrons->find( $patron_id );
208
209     unless ($patron) {
210          return $c->render(
211              status  => 404,
212              openapi => { error => "Patron not found" }
213          );
214      }
215
216     return try {
217
218         $patron->set_from_api($c->validation->param('body'))->store;
219         $patron->discard_changes;
220         return $c->render( status => 200, openapi => $patron->to_api );
221     }
222     catch {
223         unless ( blessed $_ && $_->can('rethrow') ) {
224             return $c->render(
225                 status  => 500,
226                 openapi => {
227                     error => "Something went wrong, check Koha logs for details."
228                 }
229             );
230         }
231         if ( $_->isa('Koha::Exceptions::Object::DuplicateID') ) {
232             return $c->render(
233                 status  => 409,
234                 openapi => { error => $_->error, conflict => $_->duplicate_id }
235             );
236         }
237         elsif ( $_->isa('Koha::Exceptions::Object::FKConstraint') ) {
238             return $c->render(
239                 status  => 400,
240                 openapi => { error => "Given " .
241                             $patron->to_api_mapping->{$_->broken_fk}
242                             . " does not exist" }
243             );
244         }
245         elsif ( $_->isa('Koha::Exceptions::MissingParameter') ) {
246             return $c->render(
247                 status  => 400,
248                 openapi => {
249                     error      => "Missing mandatory parameter(s)",
250                     parameters => $_->parameter
251                 }
252             );
253         }
254         elsif ( $_->isa('Koha::Exceptions::BadParameter') ) {
255             return $c->render(
256                 status  => 400,
257                 openapi => {
258                     error      => "Invalid parameter(s)",
259                     parameters => $_->parameter
260                 }
261             );
262         }
263         elsif ( $_->isa('Koha::Exceptions::NoChanges') ) {
264             return $c->render(
265                 status  => 204,
266                 openapi => { error => "No changes have been made" }
267             );
268         }
269         else {
270             return $c->render(
271                 status  => 500,
272                 openapi => {
273                     error =>
274                       "Something went wrong, check Koha logs for details."
275                 }
276             );
277         }
278     };
279 }
280
281 =head3 delete
282
283 Controller function that handles deleting a Koha::Patron object
284
285 =cut
286
287 sub delete {
288     my $c = shift->openapi->valid_input or return;
289
290     my $patron;
291
292     return try {
293         $patron = Koha::Patrons->find( $c->validation->param('patron_id') );
294
295         # check if loans, reservations, debarrment, etc. before deletion!
296         $patron->delete;
297         return $c->render( status => 200, openapi => {} );
298     }
299     catch {
300         unless ($patron) {
301             return $c->render(
302                 status  => 404,
303                 openapi => { error => "Patron not found" }
304             );
305         }
306         else {
307             return $c->render(
308                 status  => 500,
309                 openapi => {
310                     error =>
311                       "Something went wrong, check Koha logs for details."
312                 }
313             );
314         }
315     };
316 }
317
318 =head3 guarantors_can_see_charges
319
320 Method for setting whether guarantors can see the patron's charges.
321
322 =cut
323
324 sub guarantors_can_see_charges {
325     my $c = shift->openapi->valid_input or return;
326
327     return try {
328         if ( C4::Context->preference('AllowPatronToSetFinesVisibilityForGuarantor') ) {
329             my $patron = $c->stash( 'koha.user' );
330             my $privacy_setting = ($c->req->json->{allowed}) ? 1 : 0;
331
332             $patron->privacy_guarantor_fines( $privacy_setting )->store;
333
334             return $c->render(
335                 status  => 200,
336                 openapi => {}
337             );
338         }
339         else {
340             return $c->render(
341                 status  => 403,
342                 openapi => {
343                     error =>
344                       'The current configuration doesn\'t allow the requested action.'
345                 }
346             );
347         }
348     }
349     catch {
350         return $c->render(
351             status  => 500,
352             openapi => {
353                 error =>
354                   "Something went wrong, check Koha logs for details. $_"
355             }
356         );
357     };
358 }
359
360 =head3 guarantors_can_see_checkouts
361
362 Method for setting whether guarantors can see the patron's checkouts.
363
364 =cut
365
366 sub guarantors_can_see_checkouts {
367     my $c = shift->openapi->valid_input or return;
368
369     return try {
370         if ( C4::Context->preference('AllowPatronToSetCheckoutsVisibilityForGuarantor') ) {
371             my $patron = $c->stash( 'koha.user' );
372             my $privacy_setting = ( $c->req->json->{allowed} ) ? 1 : 0;
373
374             $patron->privacy_guarantor_checkouts( $privacy_setting )->store;
375
376             return $c->render(
377                 status  => 200,
378                 openapi => {}
379             );
380         }
381         else {
382             return $c->render(
383                 status  => 403,
384                 openapi => {
385                     error =>
386                       'The current configuration doesn\'t allow the requested action.'
387                 }
388             );
389         }
390     }
391     catch {
392         return $c->render(
393             status  => 500,
394             openapi => {
395                 error =>
396                   "Something went wrong, check Koha logs for details. $_"
397             }
398         );
399     };
400 }
401
402 1;