1 package Koha::REST::V1::Patrons;
3 # This file is part of Koha.
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.
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.
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>.
20 use Mojo::Base 'Mojolicious::Controller';
25 use Scalar::Util qw(blessed);
30 Koha::REST::V1::Patrons
38 Controller function that handles listing Koha::Patron objects
43 my $c = shift->openapi->valid_input or return;
48 my $restricted = delete $c->validation->output->{restricted};
49 $query->{debarred} = { '!=' => undef }
52 my $patrons_rs = Koha::Patrons->search($query);
53 my $patrons = $c->objects->search( $patrons_rs );
61 $c->unhandled_exception($_);
67 Controller function that handles retrieving a single Koha::Patron object
72 my $c = shift->openapi->valid_input or return;
75 my $patron_id = $c->validation->param('patron_id');
76 my $patron = Koha::Patrons->find($patron_id);
79 return $c->render( status => 404, openapi => { error => "Patron not found." } );
82 return $c->render( status => 200, openapi => $patron->to_api );
85 $c->unhandled_exception($_);
91 Controller function that handles adding a new Koha::Patron object
96 my $c = shift->openapi->valid_input or return;
100 my $patron = Koha::Patron->new_from_api( $c->validation->param('body') )->store;
102 $c->res->headers->location( $c->req->url->to_string . '/' . $patron->borrowernumber );
105 openapi => $patron->to_api
110 my $to_api_mapping = Koha::Patron->new->to_api_mapping;
112 unless ( blessed $_ && $_->can('rethrow') ) {
115 openapi => { error => "Something went wrong, check Koha logs for details." }
118 if ( $_->isa('Koha::Exceptions::Object::DuplicateID') ) {
121 openapi => { error => $_->error, conflict => $_->duplicate_id }
124 elsif ( $_->isa('Koha::Exceptions::Object::FKConstraint') ) {
129 . $to_api_mapping->{ $_->broken_fk }
134 elsif ( $_->isa('Koha::Exceptions::BadParameter') ) {
139 . $to_api_mapping->{ $_->parameter }
145 $c->unhandled_exception($_);
153 Controller function that handles updating a Koha::Patron object
158 my $c = shift->openapi->valid_input or return;
160 my $patron_id = $c->validation->param('patron_id');
161 my $patron = Koha::Patrons->find( $patron_id );
166 openapi => { error => "Patron not found" }
171 my $body = $c->validation->param('body');
172 my $user = $c->stash('koha.user');
175 $patron->is_superlibrarian
176 and !$user->is_superlibrarian
177 and ( exists $body->{email}
178 or exists $body->{secondary_email}
179 or exists $body->{altaddress_email} )
182 foreach my $email_field ( qw(email secondary_email altaddress_email) ) {
183 my $exists_email = exists $body->{$email_field};
184 next unless $exists_email;
186 # exists, verify if we are asked to change it
187 my $put_email = $body->{$email_field};
188 # As of writing this patch, 'email' is the only unmapped field
189 # (i.e. it preserves its name, hence this fallback)
190 my $db_email_field = $patron->to_api_mapping->{$email_field} // 'email';
191 my $db_email = $patron->$db_email_field;
195 openapi => { error => "Not enough privileges to change a superlibrarian's email" }
197 unless ( !defined $put_email and !defined $db_email )
198 or ( defined $put_email
199 and defined $db_email
200 and $put_email eq $db_email );
204 $patron->set_from_api($c->validation->param('body'))->store;
205 $patron->discard_changes;
206 return $c->render( status => 200, openapi => $patron->to_api );
209 unless ( blessed $_ && $_->can('rethrow') ) {
213 error => "Something went wrong, check Koha logs for details."
217 if ( $_->isa('Koha::Exceptions::Object::DuplicateID') ) {
220 openapi => { error => $_->error, conflict => $_->duplicate_id }
223 elsif ( $_->isa('Koha::Exceptions::Object::FKConstraint') ) {
226 openapi => { error => "Given " .
227 $patron->to_api_mapping->{$_->broken_fk}
228 . " does not exist" }
231 elsif ( $_->isa('Koha::Exceptions::MissingParameter') ) {
235 error => "Missing mandatory parameter(s)",
236 parameters => $_->parameter
240 elsif ( $_->isa('Koha::Exceptions::BadParameter') ) {
244 error => "Invalid parameter(s)",
245 parameters => $_->parameter
249 elsif ( $_->isa('Koha::Exceptions::NoChanges') ) {
252 openapi => { error => "No changes have been made" }
256 $c->unhandled_exception($_);
263 Controller function that handles deleting a Koha::Patron object
268 my $c = shift->openapi->valid_input or return;
270 my $patron = Koha::Patrons->find( $c->validation->param('patron_id') );
275 openapi => { error => "Patron not found" }
287 if ( blessed $_ && $_->isa('Koha::Exceptions::Patron::FailedDeleteAnonymousPatron') ) {
290 openapi => { error => "Anonymous patron cannot be deleted" }
294 $c->unhandled_exception($_);
298 =head3 guarantors_can_see_charges
300 Method for setting whether guarantors can see the patron's charges.
304 sub guarantors_can_see_charges {
305 my $c = shift->openapi->valid_input or return;
308 if ( C4::Context->preference('AllowPatronToSetFinesVisibilityForGuarantor') ) {
309 my $patron = $c->stash( 'koha.user' );
310 my $privacy_setting = ($c->req->json->{allowed}) ? 1 : 0;
312 $patron->privacy_guarantor_fines( $privacy_setting )->store;
324 'The current configuration doesn\'t allow the requested action.'
330 $c->unhandled_exception($_);
334 =head3 guarantors_can_see_checkouts
336 Method for setting whether guarantors can see the patron's checkouts.
340 sub guarantors_can_see_checkouts {
341 my $c = shift->openapi->valid_input or return;
344 if ( C4::Context->preference('AllowPatronToSetCheckoutsVisibilityForGuarantor') ) {
345 my $patron = $c->stash( 'koha.user' );
346 my $privacy_setting = ( $c->req->json->{allowed} ) ? 1 : 0;
348 $patron->privacy_guarantor_checkouts( $privacy_setting )->store;
360 'The current configuration doesn\'t allow the requested action.'
366 $c->unhandled_exception($_);