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

Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>
Signed-off-by: Joonas Kylmälä <joonas.kylmala@helsinki.fi>
Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>
This commit is contained in:
Tomas Cohen Arazi 2019-10-21 13:27:11 -03:00 committed by Martin Renvoize
parent 430be3e9f3
commit 255b9b98d0
No known key found for this signature in database
GPG Key ID: 422B469130441A0F
2 changed files with 88 additions and 6 deletions

View File

@ -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

View File

@ -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') ) {