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';
24 use Scalar::Util qw(blessed);
29 Koha::REST::V1::Patrons
37 Controller function that handles listing Koha::Patron objects
42 my $c = shift->openapi->valid_input or return;
46 my $args = $c->validation->output;
47 my ( $params, $reserved_params ) = $c->extract_reserved_params( $args );
49 # Merge sorting into query attributes
50 $c->dbic_merge_sorting({ attributes => $attributes, params => $reserved_params });
52 # Merge pagination into query attributes
53 $c->dbic_merge_pagination({ filter => $attributes, params => $reserved_params });
55 my $restricted = $args->{restricted};
57 $params = _to_model($params)
59 # deal with string params
60 $params = $c->build_query_params( $params, $reserved_params );
62 # translate 'restricted' => 'debarred'
63 $params->{debarred} = { '!=' => undef }
66 my $patrons = Koha::Patrons->search( $params, $attributes );
67 if ( $patrons->is_paged ) {
68 $c->add_pagination_headers(
70 total => $patrons->pager->total_entries,
75 my @patrons = $patrons->as_list;
76 @patrons = map { _to_api( $_->TO_JSON ) } @patrons;
77 return $c->render( status => 200, openapi => \@patrons );
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 => _to_api( $patron->TO_JSON ) );
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;
129 $patron = _to_api( $patron->TO_JSON );
131 return $c->render( status => 201, openapi => $patron );
134 unless ( blessed $_ && $_->can('rethrow') ) {
137 openapi => { error => "Something went wrong, check Koha logs for details." }
140 if ( $_->isa('Koha::Exceptions::Object::DuplicateID') ) {
143 openapi => { error => $_->error, conflict => $_->duplicate_id }
146 elsif ( $_->isa('Koha::Exceptions::Object::FKConstraint') ) {
151 . $Koha::REST::V1::Patrons::to_api_mapping->{ $_->broken_fk }
156 elsif ( $_->isa('Koha::Exceptions::BadParameter') ) {
161 . $Koha::REST::V1::Patrons::to_api_mapping->{ $_->parameter }
169 openapi => { error => "Something went wrong, check Koha logs for details." }
178 Controller function that handles updating a Koha::Patron object
183 my $c = shift->openapi->valid_input or return;
185 my $patron_id = $c->validation->param('patron_id');
186 my $patron = Koha::Patrons->find( $patron_id );
191 openapi => { error => "Patron not found" }
196 my $body = _to_model($c->validation->param('body'));
198 $patron->set($body)->store;
199 $patron->discard_changes;
200 return $c->render( status => 200, openapi => $patron );
203 unless ( blessed $_ && $_->can('rethrow') ) {
207 error => "Something went wrong, check Koha logs for details."
211 if ( $_->isa('Koha::Exceptions::Object::DuplicateID') ) {
214 openapi => { error => $_->error, conflict => $_->duplicate_id }
217 elsif ( $_->isa('Koha::Exceptions::Object::FKConstraint') ) {
220 openapi => { error => "Given " .
221 $Koha::REST::V1::Patrons::to_api_mapping->{$_->broken_fk}
222 . " does not exist" }
225 elsif ( $_->isa('Koha::Exceptions::MissingParameter') ) {
229 error => "Missing mandatory parameter(s)",
230 parameters => $_->parameter
234 elsif ( $_->isa('Koha::Exceptions::BadParameter') ) {
238 error => "Invalid parameter(s)",
239 parameters => $_->parameter
243 elsif ( $_->isa('Koha::Exceptions::NoChanges') ) {
246 openapi => { error => "No changes have been made" }
254 "Something went wrong, check Koha logs for details."
263 Controller function that handles deleting a Koha::Patron object
268 my $c = shift->openapi->valid_input or return;
273 $patron = Koha::Patrons->find( $c->validation->param('patron_id') );
275 # check if loans, reservations, debarrment, etc. before deletion!
276 my $res = $patron->delete;
277 return $c->render( status => 200, openapi => {} );
283 openapi => { error => "Patron not found" }
291 "Something went wrong, check Koha logs for details."
300 Helper function that maps unblessed Koha::Patron objects into REST api
307 my $patron_id = $patron->{ borrowernumber };
310 foreach my $column ( keys %{ $Koha::REST::V1::Patrons::to_api_mapping } ) {
311 my $mapped_column = $Koha::REST::V1::Patrons::to_api_mapping->{$column};
312 if ( exists $patron->{ $column }
313 && defined $mapped_column )
316 $patron->{ $mapped_column } = delete $patron->{ $column };
318 elsif ( exists $patron->{ $column }
319 && !defined $mapped_column )
322 delete $patron->{ $column };
326 # Calculate the 'restricted' field
327 my $patron_obj = Koha::Patrons->find( $patron_id );
328 $patron->{ restricted } = ($patron_obj->is_debarred) ? Mojo::JSON->true : Mojo::JSON->false;
335 Helper function that maps REST api objects into Koha::Patron
343 foreach my $attribute ( keys %{ $Koha::REST::V1::Patrons::to_model_mapping } ) {
344 my $mapped_attribute = $Koha::REST::V1::Patrons::to_model_mapping->{$attribute};
345 if ( exists $patron->{ $attribute }
346 && defined $mapped_attribute )
349 $patron->{ $mapped_attribute } = delete $patron->{ $attribute };
351 elsif ( exists $patron->{ $attribute }
352 && !defined $mapped_attribute )
354 # key => undef / to be deleted
355 delete $patron->{ $attribute };
359 # TODO: Get rid of this once write operations are based on Koha::Patron
360 if ( exists $patron->{lost} ) {
361 $patron->{lost} = ($patron->{lost}) ? 1 : 0;
364 if ( exists $patron->{ gonenoaddress} ) {
365 $patron->{gonenoaddress} = ($patron->{gonenoaddress}) ? 1 : 0;
371 =head2 Global variables
373 =head3 $to_api_mapping
377 our $to_api_mapping = {
378 borrowernotes => 'staff_notes',
379 borrowernumber => 'patron_id',
380 branchcode => 'library_id',
381 categorycode => 'category_id',
382 checkprevcheckout => 'check_previous_checkout',
383 contactfirstname => undef, # Unused
384 contactname => undef, # Unused
385 contactnote => 'altaddress_notes',
386 contacttitle => undef, # Unused
387 dateenrolled => 'date_enrolled',
388 dateexpiry => 'expiry_date',
389 dateofbirth => 'date_of_birth',
390 debarred => undef, # replaced by 'restricted'
391 debarredcomment => undef, # calculated, API consumers will use /restrictions instead
392 emailpro => 'secondary_email',
393 flags => undef, # permissions manipulation handled in /permissions
394 gonenoaddress => 'incorrect_address',
395 guarantorid => 'guarantor_id',
396 lastseen => 'last_seen',
397 lost => 'patron_card_lost',
398 opacnote => 'opac_notes',
399 othernames => 'other_name',
400 password => undef, # password manipulation handled in /password
401 phonepro => 'secondary_phone',
402 relationship => 'relationship_type',
404 smsalertnumber => 'sms_number',
405 sort1 => 'statistics_1',
406 sort2 => 'statistics_2',
407 streetnumber => 'street_number',
408 streettype => 'street_type',
409 zipcode => 'postal_code',
410 B_address => 'altaddress_address',
411 B_address2 => 'altaddress_address2',
412 B_city => 'altaddress_city',
413 B_country => 'altaddress_country',
414 B_email => 'altaddress_email',
415 B_phone => 'altaddress_phone',
416 B_state => 'altaddress_state',
417 B_streetnumber => 'altaddress_street_number',
418 B_streettype => 'altaddress_street_type',
419 B_zipcode => 'altaddress_postal_code',
420 altcontactaddress1 => 'altcontact_address',
421 altcontactaddress2 => 'altcontact_address2',
422 altcontactaddress3 => 'altcontact_city',
423 altcontactcountry => 'altcontact_country',
424 altcontactfirstname => 'altcontact_firstname',
425 altcontactphone => 'altcontact_phone',
426 altcontactsurname => 'altcontact_surname',
427 altcontactstate => 'altcontact_state',
428 altcontactzipcode => 'altcontact_postal_code'
431 =head3 $to_model_mapping
435 our $to_model_mapping = {
436 altaddress_notes => 'contactnote',
437 category_id => 'categorycode',
438 check_previous_checkout => 'checkprevcheckout',
439 date_enrolled => 'dateenrolled',
440 date_of_birth => 'dateofbirth',
441 expiry_date => 'dateexpiry',
443 guarantor_id => 'guarantorid',
444 incorrect_address => 'gonenoaddress',
445 last_seen => 'lastseen',
446 library_id => 'branchcode',
447 opac_notes => 'opacnote',
448 other_name => 'othernames',
449 patron_card_lost => 'lost',
450 patron_id => 'borrowernumber',
451 postal_code => 'zipcode',
452 relationship_type => 'relationship',
454 secondary_email => 'emailpro',
455 secondary_phone => 'phonepro',
456 sms_number => 'smsalertnumber',
457 staff_notes => 'borrowernotes',
458 statistics_1 => 'sort1',
459 statistics_2 => 'sort2',
460 street_number => 'streetnumber',
461 street_type => 'streettype',
462 altaddress_address => 'B_address',
463 altaddress_address2 => 'B_address2',
464 altaddress_city => 'B_city',
465 altaddress_country => 'B_country',
466 altaddress_email => 'B_email',
467 altaddress_phone => 'B_phone',
468 altaddress_state => 'B_state',
469 altaddress_street_number => 'B_streetnumber',
470 altaddress_street_type => 'B_streettype',
471 altaddress_postal_code => 'B_zipcode',
472 altcontact_firstname => 'altcontactfirstname',
473 altcontact_surname => 'altcontactsurname',
474 altcontact_address => 'altcontactaddress1',
475 altcontact_address2 => 'altcontactaddress2',
476 altcontact_city => 'altcontactaddress3',
477 altcontact_state => 'altcontactstate',
478 altcontact_postal_code => 'altcontactzipcode',
479 altcontact_country => 'altcontactcountry',
480 altcontact_phone => 'altcontactphone'