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';
22 use C4::Members qw( ModMember );
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,
76 my @patrons = $patrons->as_list;
77 @patrons = map { _to_api( $_->TO_JSON ) } @patrons;
78 return $c->render( status => 200, openapi => \@patrons );
81 if ( $_->isa('DBIx::Class::Exception') ) {
84 openapi => { error => $_->{msg} }
90 openapi => { error => "Something went wrong, check the logs." }
99 Controller function that handles retrieving a single Koha::Patron object
104 my $c = shift->openapi->valid_input or return;
106 my $patron_id = $c->validation->param('patron_id');
107 my $patron = Koha::Patrons->find($patron_id);
110 return $c->render( status => 404, openapi => { error => "Patron not found." } );
113 return $c->render( status => 200, openapi => _to_api( $patron->TO_JSON ) );
118 Controller function that handles adding a new Koha::Patron object
123 my $c = shift->openapi->valid_input or return;
127 my $body = _to_model( $c->validation->param('body') );
129 my $patron = Koha::Patron->new( _to_model($body) )->store;
130 $patron = _to_api( $patron->TO_JSON );
132 return $c->render( status => 201, openapi => $patron );
135 unless ( blessed $_ && $_->can('rethrow') ) {
138 openapi => { error => "Something went wrong, check Koha logs for details." }
141 if ( $_->isa('Koha::Exceptions::Object::DuplicateID') ) {
144 openapi => { error => $_->error, conflict => $_->duplicate_id }
147 elsif ( $_->isa('Koha::Exceptions::Object::FKConstraint') ) {
152 . $Koha::REST::V1::Patrons::to_api_mapping->{ $_->broken_fk }
157 elsif ( $_->isa('Koha::Exceptions::BadParameter') ) {
162 . $Koha::REST::V1::Patrons::to_api_mapping->{ $_->parameter }
170 openapi => { error => "Something went wrong, check Koha logs for details." }
179 Controller function that handles updating a Koha::Patron object
184 my $c = shift->openapi->valid_input or return;
186 my $patron_id = $c->validation->param('patron_id');
187 my $patron = Koha::Patrons->find( $patron_id );
192 openapi => { error => "Patron not found" }
197 my $body = _to_model($c->validation->param('body'));
199 ## TODO: Use ModMember until it has been moved to Koha-namespace
200 # Add borrowernumber to $body, as required by ModMember
201 $body->{borrowernumber} = $patron_id;
203 if ( ModMember(%$body) ) {
204 # Fetch the updated Koha::Patron object
205 $patron->discard_changes;
206 return $c->render( status => 200, openapi => $patron );
212 error => 'Something went wrong, check Koha logs for details.'
218 unless ( blessed $_ && $_->can('rethrow') ) {
222 error => "Something went wrong, check Koha logs for details."
226 if ( $_->isa('Koha::Exceptions::Object::DuplicateID') ) {
229 openapi => { error => $_->error, conflict => $_->duplicate_id }
232 elsif ( $_->isa('Koha::Exceptions::Object::FKConstraint') ) {
235 openapi => { error => "Given " .
236 $Koha::REST::V1::Patrons::to_api_mapping->{$_->broken_fk}
237 . " does not exist" }
240 elsif ( $_->isa('Koha::Exceptions::MissingParameter') ) {
244 error => "Missing mandatory parameter(s)",
245 parameters => $_->parameter
249 elsif ( $_->isa('Koha::Exceptions::BadParameter') ) {
253 error => "Invalid parameter(s)",
254 parameters => $_->parameter
258 elsif ( $_->isa('Koha::Exceptions::NoChanges') ) {
261 openapi => { error => "No changes have been made" }
269 "Something went wrong, check Koha logs for details."
278 Controller function that handles deleting a Koha::Patron object
283 my $c = shift->openapi->valid_input or return;
288 $patron = Koha::Patrons->find( $c->validation->param('patron_id') );
290 # check if loans, reservations, debarrment, etc. before deletion!
291 my $res = $patron->delete;
292 return $c->render( status => 200, openapi => {} );
298 openapi => { error => "Patron not found" }
306 "Something went wrong, check Koha logs for details."
315 Helper function that maps unblessed Koha::Patron objects into REST api
322 my $patron_id = $patron->{ borrowernumber };
325 foreach my $column ( keys %{ $Koha::REST::V1::Patrons::to_api_mapping } ) {
326 my $mapped_column = $Koha::REST::V1::Patrons::to_api_mapping->{$column};
327 if ( exists $patron->{ $column }
328 && defined $mapped_column )
331 $patron->{ $mapped_column } = delete $patron->{ $column };
333 elsif ( exists $patron->{ $column }
334 && !defined $mapped_column )
337 delete $patron->{ $column };
341 # Calculate the 'restricted' field
342 my $patron_obj = Koha::Patrons->find( $patron_id );
343 $patron->{ restricted } = ($patron_obj->is_debarred) ? Mojo::JSON->true : Mojo::JSON->false;
350 Helper function that maps REST api objects into Koha::Patron
358 foreach my $attribute ( keys %{ $Koha::REST::V1::Patrons::to_model_mapping } ) {
359 my $mapped_attribute = $Koha::REST::V1::Patrons::to_model_mapping->{$attribute};
360 if ( exists $patron->{ $attribute }
361 && defined $mapped_attribute )
364 $patron->{ $mapped_attribute } = delete $patron->{ $attribute };
366 elsif ( exists $patron->{ $attribute }
367 && !defined $mapped_attribute )
369 # key => undef / to be deleted
370 delete $patron->{ $attribute };
374 # TODO: Get rid of this once write operations are based on Koha::Patron
375 if ( exists $patron->{lost} ) {
376 $patron->{lost} = ($patron->{lost}) ? 1 : 0;
379 if ( exists $patron->{ gonenoaddress} ) {
380 $patron->{gonenoaddress} = ($patron->{gonenoaddress}) ? 1 : 0;
386 =head2 Global variables
388 =head3 $to_api_mapping
392 our $to_api_mapping = {
393 borrowernotes => 'staff_notes',
394 borrowernumber => 'patron_id',
395 branchcode => 'library_id',
396 categorycode => 'category_id',
397 checkprevcheckout => 'check_previous_checkout',
398 contactfirstname => undef, # Unused
399 contactname => undef, # Unused
400 contactnote => 'altaddress_notes',
401 contacttitle => undef, # Unused
402 dateenrolled => 'date_enrolled',
403 dateexpiry => 'expiry_date',
404 dateofbirth => 'date_of_birth',
405 debarred => undef, # replaced by 'restricted'
406 debarredcomment => undef, # calculated, API consumers will use /restrictions instead
407 emailpro => 'secondary_email',
408 flags => undef, # permissions manipulation handled in /permissions
409 gonenoaddress => 'incorrect_address',
410 guarantorid => 'guarantor_id',
411 lastseen => 'last_seen',
412 lost => 'patron_card_lost',
413 opacnote => 'opac_notes',
414 othernames => 'other_name',
415 password => undef, # password manipulation handled in /password
416 phonepro => 'secondary_phone',
417 relationship => 'relationship_type',
419 smsalertnumber => 'sms_number',
420 sort1 => 'statistics_1',
421 sort2 => 'statistics_2',
422 streetnumber => 'street_number',
423 streettype => 'street_type',
424 zipcode => 'postal_code',
425 B_address => 'altaddress_address',
426 B_address2 => 'altaddress_address2',
427 B_city => 'altaddress_city',
428 B_country => 'altaddress_country',
429 B_email => 'altaddress_email',
430 B_phone => 'altaddress_phone',
431 B_state => 'altaddress_state',
432 B_streetnumber => 'altaddress_street_number',
433 B_streettype => 'altaddress_street_type',
434 B_zipcode => 'altaddress_postal_code',
435 altcontactaddress1 => 'altcontact_address',
436 altcontactaddress2 => 'altcontact_address2',
437 altcontactaddress3 => 'altcontact_city',
438 altcontactcountry => 'altcontact_country',
439 altcontactfirstname => 'altcontact_firstname',
440 altcontactphone => 'altcontact_phone',
441 altcontactsurname => 'altcontact_surname',
442 altcontactstate => 'altcontact_state',
443 altcontactzipcode => 'altcontact_postal_code'
446 =head3 $to_model_mapping
450 our $to_model_mapping = {
451 altaddress_notes => 'contactnote',
452 category_id => 'categorycode',
453 check_previous_checkout => 'checkprevcheckout',
454 date_enrolled => 'dateenrolled',
455 date_of_birth => 'dateofbirth',
456 expiry_date => 'dateexpiry',
458 guarantor_id => 'guarantorid',
459 incorrect_address => 'gonenoaddress',
460 last_seen => 'lastseen',
461 library_id => 'branchcode',
462 opac_notes => 'opacnote',
463 other_name => 'othernames',
464 patron_card_lost => 'lost',
465 patron_id => 'borrowernumber',
466 postal_code => 'zipcode',
467 relationship_type => 'relationship',
469 secondary_email => 'emailpro',
470 secondary_phone => 'phonepro',
471 sms_number => 'smsalertnumber',
472 staff_notes => 'borrowernotes',
473 statistics_1 => 'sort1',
474 statistics_2 => 'sort2',
475 street_number => 'streetnumber',
476 street_type => 'streettype',
477 altaddress_address => 'B_address',
478 altaddress_address2 => 'B_address2',
479 altaddress_city => 'B_city',
480 altaddress_country => 'B_country',
481 altaddress_email => 'B_email',
482 altaddress_phone => 'B_phone',
483 altaddress_state => 'B_state',
484 altaddress_street_number => 'B_streetnumber',
485 altaddress_street_type => 'B_streettype',
486 altaddress_postal_code => 'B_zipcode',
487 altcontact_firstname => 'altcontactfirstname',
488 altcontact_surname => 'altcontactsurname',
489 altcontact_address => 'altcontactaddress1',
490 altcontact_address2 => 'altcontactaddress2',
491 altcontact_city => 'altcontactaddress3',
492 altcontact_state => 'altcontactstate',
493 altcontact_postal_code => 'altcontactzipcode',
494 altcontact_country => 'altcontactcountry',
495 altcontact_phone => 'altcontactphone'