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 $patrons_rs = Koha::Patrons->new;
48 my $args = $c->validation->output;
51 # Extract reserved params
52 my ( $filtered_params, $reserved_params ) = $c->extract_reserved_params($args);
54 my $restricted = delete $filtered_params->{restricted};
56 # Merge sorting into query attributes
57 $c->dbic_merge_sorting(
59 attributes => $attributes,
60 params => $reserved_params,
61 result_set => $patrons_rs
65 # Merge pagination into query attributes
66 $c->dbic_merge_pagination(
68 filter => $attributes,
69 params => $reserved_params
73 if ( defined $filtered_params ) {
75 # Apply the mapping function to the passed params
76 $filtered_params = $patrons_rs->attributes_from_api($filtered_params);
77 $filtered_params = $c->build_query_params( $filtered_params, $reserved_params );
80 # translate 'restricted' => 'debarred'
81 $filtered_params->{debarred} = { '!=' => undef }
84 my $patrons = $patrons_rs->search( $filtered_params, $attributes );
85 if ( $patrons_rs->is_paged ) {
86 $c->add_pagination_headers(
88 total => $patrons->pager->total_entries,
94 return $c->render( status => 200, openapi => $patrons->to_api );
97 if ( $_->isa('DBIx::Class::Exception') ) {
100 openapi => { error => $_->{msg} }
106 openapi => { error => "Something went wrong, check the logs." }
115 Controller function that handles retrieving a single Koha::Patron object
120 my $c = shift->openapi->valid_input or return;
122 my $patron_id = $c->validation->param('patron_id');
123 my $patron = Koha::Patrons->find($patron_id);
126 return $c->render( status => 404, openapi => { error => "Patron not found." } );
129 return $c->render( status => 200, openapi => $patron->to_api );
134 Controller function that handles adding a new Koha::Patron object
139 my $c = shift->openapi->valid_input or return;
143 my $patron = Koha::Patron->new_from_api( $c->validation->param('body') )->store;
145 $c->res->headers->location( $c->req->url->to_string . '/' . $patron->borrowernumber );
148 openapi => $patron->to_api
153 my $to_api_mapping = Koha::Patron->new->to_api_mapping;
155 unless ( blessed $_ && $_->can('rethrow') ) {
158 openapi => { error => "Something went wrong, check Koha logs for details." }
161 if ( $_->isa('Koha::Exceptions::Object::DuplicateID') ) {
164 openapi => { error => $_->error, conflict => $_->duplicate_id }
167 elsif ( $_->isa('Koha::Exceptions::Object::FKConstraint') ) {
172 . $to_api_mapping->{ $_->broken_fk }
177 elsif ( $_->isa('Koha::Exceptions::BadParameter') ) {
182 . $to_api_mapping->{ $_->parameter }
190 openapi => { error => "Something went wrong, check Koha logs for details." }
199 Controller function that handles updating a Koha::Patron object
204 my $c = shift->openapi->valid_input or return;
206 my $patron_id = $c->validation->param('patron_id');
207 my $patron = Koha::Patrons->find( $patron_id );
212 openapi => { error => "Patron not found" }
218 $patron->set_from_api($c->validation->param('body'))->store;
219 $patron->discard_changes;
220 return $c->render( status => 200, openapi => $patron->to_api );
223 unless ( blessed $_ && $_->can('rethrow') ) {
227 error => "Something went wrong, check Koha logs for details."
231 if ( $_->isa('Koha::Exceptions::Object::DuplicateID') ) {
234 openapi => { error => $_->error, conflict => $_->duplicate_id }
237 elsif ( $_->isa('Koha::Exceptions::Object::FKConstraint') ) {
240 openapi => { error => "Given " .
241 $patron->to_api_mapping->{$_->broken_fk}
242 . " does not exist" }
245 elsif ( $_->isa('Koha::Exceptions::MissingParameter') ) {
249 error => "Missing mandatory parameter(s)",
250 parameters => $_->parameter
254 elsif ( $_->isa('Koha::Exceptions::BadParameter') ) {
258 error => "Invalid parameter(s)",
259 parameters => $_->parameter
263 elsif ( $_->isa('Koha::Exceptions::NoChanges') ) {
266 openapi => { error => "No changes have been made" }
274 "Something went wrong, check Koha logs for details."
283 Controller function that handles deleting a Koha::Patron object
288 my $c = shift->openapi->valid_input or return;
293 $patron = Koha::Patrons->find( $c->validation->param('patron_id') );
295 # check if loans, reservations, debarrment, etc. before deletion!
297 return $c->render( status => 200, openapi => {} );
303 openapi => { error => "Patron not found" }
311 "Something went wrong, check Koha logs for details."
318 =head3 guarantors_can_see_charges
320 Method for setting whether guarantors can see the patron's charges.
324 sub guarantors_can_see_charges {
325 my $c = shift->openapi->valid_input or return;
328 if ( C4::Context->preference('AllowPatronToSetFinesVisibilityForGuarantor') ) {
329 my $patron = $c->stash( 'koha.user' );
330 my $privacy_setting = ($c->req->json->{allowed}) ? 1 : 0;
332 $patron->privacy_guarantor_fines( $privacy_setting )->store;
344 'The current configuration doesn\'t allow the requested action.'
354 "Something went wrong, check Koha logs for details. $_"
360 =head3 guarantors_can_see_checkouts
362 Method for setting whether guarantors can see the patron's checkouts.
366 sub guarantors_can_see_checkouts {
367 my $c = shift->openapi->valid_input or return;
370 if ( C4::Context->preference('AllowPatronToSetCheckoutsVisibilityForGuarantor') ) {
371 my $patron = $c->stash( 'koha.user' );
372 my $privacy_setting = ( $c->req->json->{allowed} ) ? 1 : 0;
374 $patron->privacy_guarantor_checkouts( $privacy_setting )->store;
386 'The current configuration doesn\'t allow the requested action.'
396 "Something went wrong, check Koha logs for details. $_"