From a44589e5b5a2cde691e524dcaec14818155435e2 Mon Sep 17 00:00:00 2001 From: Tomas Cohen Arazi Date: Mon, 21 Oct 2019 13:27:11 -0300 Subject: [PATCH] Bug 23843: Add mapping to Koha::Patron This patch adds a to_api_mapping method to the class. This in effect enables calling ->to_api on the object. The mapping is borrowed from the API controller. It is not removed from the controller so we are able to verify (through the tests) that there is no behavior change. Once this is pushed we need to implement the counter-wise methods and clean the controllers. To test: 1. Run: $ kshell k$ prove t/db_dependent/api/v1/patrons.t => SUCCESS: Tests pass 2. Apply this patch 3. Repeat (1) => SUCCESS: Tests still pass! 4. Sign off :-D --- Koha/Patron.pm | 84 +++++++++++++++++++++++++++++++++++++++++ Koha/REST/V1/Patrons.pm | 10 ++--- 2 files changed, 88 insertions(+), 6 deletions(-) diff --git a/Koha/Patron.pm b/Koha/Patron.pm index fca796ff28..2655f1c90b 100644 --- a/Koha/Patron.pm +++ b/Koha/Patron.pm @@ -1476,6 +1476,90 @@ sub add_guarantor { )->store(); } +=head3 to_api + + my $json = $patron->to_api; + +Overloaded method that returns a JSON representation of the Koha::Patron object, +suitable for API output. + +=cut + +sub to_api { + my ( $self ) = @_; + + my $json_patron = $self->SUPER::to_api; + + $json_patron->{restricted} = ( $self->is_debarred ) + ? Mojo::JSON->true + : Mojo::JSON->false; + + return $json_patron; +} + +=head3 to_api_mapping + +This method returns the mapping for representing a Koha::Patron object +on the API. + +=cut + +sub to_api_mapping { + return { + borrowernotes => 'staff_notes', + borrowernumber => 'patron_id', + branchcode => 'library_id', + categorycode => 'category_id', + checkprevcheckout => 'check_previous_checkout', + contactfirstname => undef, # Unused + contactname => undef, # Unused + contactnote => 'altaddress_notes', + contacttitle => undef, # Unused + dateenrolled => 'date_enrolled', + dateexpiry => 'expiry_date', + dateofbirth => 'date_of_birth', + debarred => undef, # replaced by 'restricted' + debarredcomment => undef, # calculated, API consumers will use /restrictions instead + emailpro => 'secondary_email', + flags => undef, # permissions manipulation handled in /permissions + gonenoaddress => 'incorrect_address', + guarantorid => 'guarantor_id', + lastseen => 'last_seen', + lost => 'patron_card_lost', + opacnote => 'opac_notes', + othernames => 'other_name', + password => undef, # password manipulation handled in /password + phonepro => 'secondary_phone', + relationship => 'relationship_type', + sex => 'gender', + smsalertnumber => 'sms_number', + sort1 => 'statistics_1', + sort2 => 'statistics_2', + streetnumber => 'street_number', + streettype => 'street_type', + zipcode => 'postal_code', + B_address => 'altaddress_address', + B_address2 => 'altaddress_address2', + B_city => 'altaddress_city', + B_country => 'altaddress_country', + B_email => 'altaddress_email', + B_phone => 'altaddress_phone', + B_state => 'altaddress_state', + B_streetnumber => 'altaddress_street_number', + B_streettype => 'altaddress_street_type', + B_zipcode => 'altaddress_postal_code', + altcontactaddress1 => 'altcontact_address', + altcontactaddress2 => 'altcontact_address2', + altcontactaddress3 => 'altcontact_city', + altcontactcountry => 'altcontact_country', + altcontactfirstname => 'altcontact_firstname', + altcontactphone => 'altcontact_phone', + altcontactsurname => 'altcontact_surname', + altcontactstate => 'altcontact_state', + altcontactzipcode => 'altcontact_postal_code' + }; +} + =head2 Internal methods =head3 _type diff --git a/Koha/REST/V1/Patrons.pm b/Koha/REST/V1/Patrons.pm index 29331fc4d0..46ad80d01d 100644 --- a/Koha/REST/V1/Patrons.pm +++ b/Koha/REST/V1/Patrons.pm @@ -73,9 +73,8 @@ sub list { } ); } - my @patrons = $patrons->as_list; - @patrons = map { _to_api( $_->TO_JSON ) } @patrons; - return $c->render( status => 200, openapi => \@patrons ); + + return $c->render( status => 200, openapi => $patrons->to_api ); } catch { if ( $_->isa('DBIx::Class::Exception') ) { @@ -110,7 +109,7 @@ sub get { return $c->render( status => 404, openapi => { error => "Patron not found." } ); } - return $c->render( status => 200, openapi => _to_api( $patron->TO_JSON ) ); + return $c->render( status => 200, openapi => $patron->to_api ); } =head3 add @@ -127,9 +126,8 @@ sub add { my $body = _to_model( $c->validation->param('body') ); my $patron = Koha::Patron->new( _to_model($body) )->store; - $patron = _to_api( $patron->TO_JSON ); - return $c->render( status => 201, openapi => $patron ); + return $c->render( status => 201, openapi => $patron->to_api ); } catch { unless ( blessed $_ && $_->can('rethrow') ) { -- 2.23.0