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;
47 my $patrons_rs = Koha::Patrons->new;
48 my $args = $c->validation->output;
51 # Extract reserved params
52 my ( $filtered_params, $reserved_params ) = $c->extract_reserved_params($args);
54 my $restricted = delete $filtered_params->{restricted};
56 # Merge sorting into query attributes
57 $c->dbic_merge_sorting(
59 attributes => $attributes,
60 params => $reserved_params,
61 result_set => $patrons_rs
65 # Merge pagination into query attributes
66 $c->dbic_merge_pagination(
68 filter => $attributes,
69 params => $reserved_params
73 if ( defined $filtered_params ) {
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 );
80 # translate 'restricted' => 'debarred'
81 $filtered_params->{debarred} = { '!=' => undef }
84 my $patrons = $patrons_rs->search( $filtered_params, $attributes );
85 my $total = $patrons_rs->search->count;
87 $c->add_pagination_headers(
89 total => ($patrons->is_paged ? $patrons->pager->total_entries : $patrons->count),
95 return $c->render( status => 200, openapi => $patrons->to_api );
98 $c->unhandled_exception($_);
105 Controller function that handles retrieving a single Koha::Patron object
110 my $c = shift->openapi->valid_input or return;
113 my $patron_id = $c->validation->param('patron_id');
114 my $patron = Koha::Patrons->find($patron_id);
117 return $c->render( status => 404, openapi => { error => "Patron not found." } );
120 return $c->render( status => 200, openapi => $patron->to_api );
123 $c->unhandled_exception($_);
129 Controller function that handles adding a new Koha::Patron object
134 my $c = shift->openapi->valid_input or return;
138 my $patron = Koha::Patron->new_from_api( $c->validation->param('body') )->store;
140 $c->res->headers->location( $c->req->url->to_string . '/' . $patron->borrowernumber );
143 openapi => $patron->to_api
148 my $to_api_mapping = Koha::Patron->new->to_api_mapping;
150 unless ( blessed $_ && $_->can('rethrow') ) {
153 openapi => { error => "Something went wrong, check Koha logs for details." }
156 if ( $_->isa('Koha::Exceptions::Object::DuplicateID') ) {
159 openapi => { error => $_->error, conflict => $_->duplicate_id }
162 elsif ( $_->isa('Koha::Exceptions::Object::FKConstraint') ) {
167 . $to_api_mapping->{ $_->broken_fk }
172 elsif ( $_->isa('Koha::Exceptions::BadParameter') ) {
177 . $to_api_mapping->{ $_->parameter }
183 $c->unhandled_exception($_);
191 Controller function that handles updating a Koha::Patron object
196 my $c = shift->openapi->valid_input or return;
198 my $patron_id = $c->validation->param('patron_id');
199 my $patron = Koha::Patrons->find( $patron_id );
204 openapi => { error => "Patron not found" }
209 my $body = $c->validation->param('body');
210 my $user = $c->stash('koha.user');
213 $patron->is_superlibrarian
214 and !$user->is_superlibrarian
215 and ( exists $body->{email}
216 or exists $body->{secondary_email}
217 or exists $body->{altaddress_email} )
220 foreach my $email_field ( qw(email secondary_email altaddress_email) ) {
221 my $exists_email = exists $body->{$email_field};
222 next unless $exists_email;
224 # exists, verify if we are asked to change it
225 my $put_email = $body->{$email_field};
226 # As of writing this patch, 'email' is the only unmapped field
227 # (i.e. it preserves its name, hence this fallback)
228 my $db_email_field = $patron->to_api_mapping->{$email_field} // 'email';
229 my $db_email = $patron->$db_email_field;
233 openapi => { error => "Not enough privileges to change a superlibrarian's email" }
235 unless ( !defined $put_email and !defined $db_email )
236 or ( defined $put_email
237 and defined $db_email
238 and $put_email eq $db_email );
242 $patron->set_from_api($c->validation->param('body'))->store;
243 $patron->discard_changes;
244 return $c->render( status => 200, openapi => $patron->to_api );
247 unless ( blessed $_ && $_->can('rethrow') ) {
251 error => "Something went wrong, check Koha logs for details."
255 if ( $_->isa('Koha::Exceptions::Object::DuplicateID') ) {
258 openapi => { error => $_->error, conflict => $_->duplicate_id }
261 elsif ( $_->isa('Koha::Exceptions::Object::FKConstraint') ) {
264 openapi => { error => "Given " .
265 $patron->to_api_mapping->{$_->broken_fk}
266 . " does not exist" }
269 elsif ( $_->isa('Koha::Exceptions::MissingParameter') ) {
273 error => "Missing mandatory parameter(s)",
274 parameters => $_->parameter
278 elsif ( $_->isa('Koha::Exceptions::BadParameter') ) {
282 error => "Invalid parameter(s)",
283 parameters => $_->parameter
287 elsif ( $_->isa('Koha::Exceptions::NoChanges') ) {
290 openapi => { error => "No changes have been made" }
294 $c->unhandled_exception($_);
301 Controller function that handles deleting a Koha::Patron object
306 my $c = shift->openapi->valid_input or return;
308 my $patron = Koha::Patrons->find( $c->validation->param('patron_id') );
313 openapi => { error => "Patron not found" }
325 if ( blessed $_ && $_->isa('Koha::Exceptions::Patron::FailedDeleteAnonymousPatron') ) {
328 openapi => { error => "Anonymous patron cannot be deleted" }
332 $c->unhandled_exception($_);
336 =head3 guarantors_can_see_charges
338 Method for setting whether guarantors can see the patron's charges.
342 sub guarantors_can_see_charges {
343 my $c = shift->openapi->valid_input or return;
346 if ( C4::Context->preference('AllowPatronToSetFinesVisibilityForGuarantor') ) {
347 my $patron = $c->stash( 'koha.user' );
348 my $privacy_setting = ($c->req->json->{allowed}) ? 1 : 0;
350 $patron->privacy_guarantor_fines( $privacy_setting )->store;
362 'The current configuration doesn\'t allow the requested action.'
368 $c->unhandled_exception($_);
372 =head3 guarantors_can_see_checkouts
374 Method for setting whether guarantors can see the patron's checkouts.
378 sub guarantors_can_see_checkouts {
379 my $c = shift->openapi->valid_input or return;
382 if ( C4::Context->preference('AllowPatronToSetCheckoutsVisibilityForGuarantor') ) {
383 my $patron = $c->stash( 'koha.user' );
384 my $privacy_setting = ( $c->req->json->{allowed} ) ? 1 : 0;
386 $patron->privacy_guarantor_checkouts( $privacy_setting )->store;
398 'The current configuration doesn\'t allow the requested action.'
404 $c->unhandled_exception($_);