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 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
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.
14 # You should have received a copy of the GNU General Public License along
15 # with Koha; if not, write to the Free Software Foundation, Inc.,
16 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
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 $args = $c->validation->output;
48 my ( $params, $reserved_params ) = $c->extract_reserved_params( $args );
50 # Merge sorting into query attributes
51 $c->dbic_merge_sorting({ attributes => $attributes, params => $reserved_params });
53 # Merge pagination into query attributes
54 $c->dbic_merge_pagination({ filter => $attributes, params => $reserved_params });
56 my $restricted = $args->{restricted};
58 $params = _to_model($params)
60 # deal with string params
61 $params = $c->build_query_params( $params, $reserved_params );
63 # translate 'restricted' => 'debarred'
64 $params->{debarred} = { '!=' => undef }
67 my $patrons = Koha::Patrons->search( $params, $attributes );
68 if ( $patrons->is_paged ) {
69 $c->add_pagination_headers(
71 total => $patrons->pager->total_entries,
77 return $c->render( status => 200, openapi => $patrons->to_api );
80 if ( $_->isa('DBIx::Class::Exception') ) {
83 openapi => { error => $_->{msg} }
89 openapi => { error => "Something went wrong, check the logs." }
98 Controller function that handles retrieving a single Koha::Patron object
103 my $c = shift->openapi->valid_input or return;
105 my $patron_id = $c->validation->param('patron_id');
106 my $patron = Koha::Patrons->find($patron_id);
109 return $c->render( status => 404, openapi => { error => "Patron not found." } );
112 return $c->render( status => 200, openapi => $patron->to_api );
117 Controller function that handles adding a new Koha::Patron object
122 my $c = shift->openapi->valid_input or return;
126 my $body = _to_model( $c->validation->param('body') );
128 my $patron = Koha::Patron->new( _to_model($body) )->store;
130 return $c->render( status => 201, openapi => $patron->to_api );
133 unless ( blessed $_ && $_->can('rethrow') ) {
136 openapi => { error => "Something went wrong, check Koha logs for details." }
139 if ( $_->isa('Koha::Exceptions::Object::DuplicateID') ) {
142 openapi => { error => $_->error, conflict => $_->duplicate_id }
145 elsif ( $_->isa('Koha::Exceptions::Object::FKConstraint') ) {
150 . $Koha::REST::V1::Patrons::to_api_mapping->{ $_->broken_fk }
155 elsif ( $_->isa('Koha::Exceptions::BadParameter') ) {
160 . $Koha::REST::V1::Patrons::to_api_mapping->{ $_->parameter }
168 openapi => { error => "Something went wrong, check Koha logs for details." }
177 Controller function that handles updating a Koha::Patron object
182 my $c = shift->openapi->valid_input or return;
184 my $patron_id = $c->validation->param('patron_id');
185 my $patron = Koha::Patrons->find( $patron_id );
190 openapi => { error => "Patron not found" }
195 my $body = _to_model($c->validation->param('body'));
197 $patron->set($body)->store;
198 $patron->discard_changes;
199 return $c->render( status => 200, openapi => $patron );
202 unless ( blessed $_ && $_->can('rethrow') ) {
206 error => "Something went wrong, check Koha logs for details."
210 if ( $_->isa('Koha::Exceptions::Object::DuplicateID') ) {
213 openapi => { error => $_->error, conflict => $_->duplicate_id }
216 elsif ( $_->isa('Koha::Exceptions::Object::FKConstraint') ) {
219 openapi => { error => "Given " .
220 $Koha::REST::V1::Patrons::to_api_mapping->{$_->broken_fk}
221 . " does not exist" }
224 elsif ( $_->isa('Koha::Exceptions::MissingParameter') ) {
228 error => "Missing mandatory parameter(s)",
229 parameters => $_->parameter
233 elsif ( $_->isa('Koha::Exceptions::BadParameter') ) {
237 error => "Invalid parameter(s)",
238 parameters => $_->parameter
242 elsif ( $_->isa('Koha::Exceptions::NoChanges') ) {
245 openapi => { error => "No changes have been made" }
253 "Something went wrong, check Koha logs for details."
262 Controller function that handles deleting a Koha::Patron object
267 my $c = shift->openapi->valid_input or return;
272 $patron = Koha::Patrons->find( $c->validation->param('patron_id') );
274 # check if loans, reservations, debarrment, etc. before deletion!
275 my $res = $patron->delete;
276 return $c->render( status => 200, openapi => {} );
282 openapi => { error => "Patron not found" }
290 "Something went wrong, check Koha logs for details."
297 =head3 guarantors_can_see_charges
299 Method for setting whether guarantors can see the patron's charges.
303 sub guarantors_can_see_charges {
304 my $c = shift->openapi->valid_input or return;
307 if ( C4::Context->preference('AllowPatronToSetFinesVisibilityForGuarantor') ) {
308 my $patron = $c->stash( 'koha.user' );
309 my $privacy_setting = ($c->req->json->{allowed}) ? 1 : 0;
311 $patron->privacy_guarantor_fines( $privacy_setting )->store;
323 'The current configuration doesn\'t allow the requested action.'
333 "Something went wrong, check Koha logs for details. $_"
339 =head3 guarantors_can_see_checkouts
341 Method for setting whether guarantors can see the patron's checkouts.
345 sub guarantors_can_see_checkouts {
346 my $c = shift->openapi->valid_input or return;
349 if ( C4::Context->preference('AllowPatronToSetCheckoutsVisibilityForGuarantor') ) {
350 my $patron = $c->stash( 'koha.user' );
351 my $privacy_setting = ( $c->req->json->{allowed} ) ? 1 : 0;
353 $patron->privacy_guarantor_checkouts( $privacy_setting )->store;
365 'The current configuration doesn\'t allow the requested action.'
375 "Something went wrong, check Koha logs for details. $_"
381 =head2 Internal methods
385 Helper function that maps unblessed Koha::Patron objects into REST api
392 my $patron_id = $patron->{ borrowernumber };
395 foreach my $column ( keys %{ $Koha::REST::V1::Patrons::to_api_mapping } ) {
396 my $mapped_column = $Koha::REST::V1::Patrons::to_api_mapping->{$column};
397 if ( exists $patron->{ $column }
398 && defined $mapped_column )
401 $patron->{ $mapped_column } = delete $patron->{ $column };
403 elsif ( exists $patron->{ $column }
404 && !defined $mapped_column )
407 delete $patron->{ $column };
411 # Calculate the 'restricted' field
412 my $patron_obj = Koha::Patrons->find( $patron_id );
413 $patron->{ restricted } = ($patron_obj->is_debarred) ? Mojo::JSON->true : Mojo::JSON->false;
420 Helper function that maps REST api objects into Koha::Patron
428 foreach my $attribute ( keys %{ $Koha::REST::V1::Patrons::to_model_mapping } ) {
429 my $mapped_attribute = $Koha::REST::V1::Patrons::to_model_mapping->{$attribute};
430 if ( exists $patron->{ $attribute }
431 && defined $mapped_attribute )
434 $patron->{ $mapped_attribute } = delete $patron->{ $attribute };
436 elsif ( exists $patron->{ $attribute }
437 && !defined $mapped_attribute )
439 # key => undef / to be deleted
440 delete $patron->{ $attribute };
444 # TODO: Get rid of this once write operations are based on Koha::Patron
445 if ( exists $patron->{lost} ) {
446 $patron->{lost} = ($patron->{lost}) ? 1 : 0;
449 if ( exists $patron->{ gonenoaddress} ) {
450 $patron->{gonenoaddress} = ($patron->{gonenoaddress}) ? 1 : 0;
453 if ( exists $patron->{lastseen} ) {
454 $patron->{lastseen} = output_pref({ str => $patron->{lastseen}, dateformat => 'sql' });
457 if ( exists $patron->{updated_on} ) {
458 $patron->{updated_on} = output_pref({ str => $patron->{updated_on}, dateformat => 'sql' });
464 =head2 Global variables
466 =head3 $to_api_mapping
470 our $to_api_mapping = {
471 borrowernotes => 'staff_notes',
472 borrowernumber => 'patron_id',
473 branchcode => 'library_id',
474 categorycode => 'category_id',
475 checkprevcheckout => 'check_previous_checkout',
476 contactfirstname => undef, # Unused
477 contactname => undef, # Unused
478 contactnote => 'altaddress_notes',
479 contacttitle => undef, # Unused
480 dateenrolled => 'date_enrolled',
481 dateexpiry => 'expiry_date',
482 dateofbirth => 'date_of_birth',
483 debarred => undef, # replaced by 'restricted'
484 debarredcomment => undef, # calculated, API consumers will use /restrictions instead
485 emailpro => 'secondary_email',
486 flags => undef, # permissions manipulation handled in /permissions
487 gonenoaddress => 'incorrect_address',
488 guarantorid => 'guarantor_id',
489 lastseen => 'last_seen',
490 lost => 'patron_card_lost',
491 opacnote => 'opac_notes',
492 othernames => 'other_name',
493 password => undef, # password manipulation handled in /password
494 phonepro => 'secondary_phone',
495 relationship => 'relationship_type',
497 smsalertnumber => 'sms_number',
498 sort1 => 'statistics_1',
499 sort2 => 'statistics_2',
500 streetnumber => 'street_number',
501 streettype => 'street_type',
502 zipcode => 'postal_code',
503 B_address => 'altaddress_address',
504 B_address2 => 'altaddress_address2',
505 B_city => 'altaddress_city',
506 B_country => 'altaddress_country',
507 B_email => 'altaddress_email',
508 B_phone => 'altaddress_phone',
509 B_state => 'altaddress_state',
510 B_streetnumber => 'altaddress_street_number',
511 B_streettype => 'altaddress_street_type',
512 B_zipcode => 'altaddress_postal_code',
513 altcontactaddress1 => 'altcontact_address',
514 altcontactaddress2 => 'altcontact_address2',
515 altcontactaddress3 => 'altcontact_city',
516 altcontactcountry => 'altcontact_country',
517 altcontactfirstname => 'altcontact_firstname',
518 altcontactphone => 'altcontact_phone',
519 altcontactsurname => 'altcontact_surname',
520 altcontactstate => 'altcontact_state',
521 altcontactzipcode => 'altcontact_postal_code'
524 =head3 $to_model_mapping
528 our $to_model_mapping = {
529 altaddress_notes => 'contactnote',
530 category_id => 'categorycode',
531 check_previous_checkout => 'checkprevcheckout',
532 date_enrolled => 'dateenrolled',
533 date_of_birth => 'dateofbirth',
534 expiry_date => 'dateexpiry',
536 guarantor_id => 'guarantorid',
537 incorrect_address => 'gonenoaddress',
538 last_seen => 'lastseen',
539 library_id => 'branchcode',
540 opac_notes => 'opacnote',
541 other_name => 'othernames',
542 patron_card_lost => 'lost',
543 patron_id => 'borrowernumber',
544 postal_code => 'zipcode',
545 relationship_type => 'relationship',
547 secondary_email => 'emailpro',
548 secondary_phone => 'phonepro',
549 sms_number => 'smsalertnumber',
550 staff_notes => 'borrowernotes',
551 statistics_1 => 'sort1',
552 statistics_2 => 'sort2',
553 street_number => 'streetnumber',
554 street_type => 'streettype',
555 altaddress_address => 'B_address',
556 altaddress_address2 => 'B_address2',
557 altaddress_city => 'B_city',
558 altaddress_country => 'B_country',
559 altaddress_email => 'B_email',
560 altaddress_phone => 'B_phone',
561 altaddress_state => 'B_state',
562 altaddress_street_number => 'B_streetnumber',
563 altaddress_street_type => 'B_streettype',
564 altaddress_postal_code => 'B_zipcode',
565 altcontact_firstname => 'altcontactfirstname',
566 altcontact_surname => 'altcontactsurname',
567 altcontact_address => 'altcontactaddress1',
568 altcontact_address2 => 'altcontactaddress2',
569 altcontact_city => 'altcontactaddress3',
570 altcontact_state => 'altcontactstate',
571 altcontact_postal_code => 'altcontactzipcode',
572 altcontact_country => 'altcontactcountry',
573 altcontact_phone => 'altcontactphone'