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
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
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 $c->unhandled_exception($_);
104 Controller function that handles retrieving a single Koha::Patron object
109 my $c = shift->openapi->valid_input or return;
112 my $patron_id = $c->validation->param('patron_id');
113 my $patron = Koha::Patrons->find($patron_id);
116 return $c->render( status => 404, openapi => { error => "Patron not found." } );
119 return $c->render( status => 200, openapi => $patron->to_api );
122 $c->unhandled_exception($_);
128 Controller function that handles adding a new Koha::Patron object
133 my $c = shift->openapi->valid_input or return;
137 my $patron = Koha::Patron->new_from_api( $c->validation->param('body') )->store;
139 $c->res->headers->location( $c->req->url->to_string . '/' . $patron->borrowernumber );
142 openapi => $patron->to_api
147 my $to_api_mapping = Koha::Patron->new->to_api_mapping;
149 unless ( blessed $_ && $_->can('rethrow') ) {
152 openapi => { error => "Something went wrong, check Koha logs for details." }
155 if ( $_->isa('Koha::Exceptions::Object::DuplicateID') ) {
158 openapi => { error => $_->error, conflict => $_->duplicate_id }
161 elsif ( $_->isa('Koha::Exceptions::Object::FKConstraint') ) {
166 . $to_api_mapping->{ $_->broken_fk }
171 elsif ( $_->isa('Koha::Exceptions::BadParameter') ) {
176 . $to_api_mapping->{ $_->parameter }
182 $c->unhandled_exception($_);
190 Controller function that handles updating a Koha::Patron object
195 my $c = shift->openapi->valid_input or return;
197 my $patron_id = $c->validation->param('patron_id');
198 my $patron = Koha::Patrons->find( $patron_id );
203 openapi => { error => "Patron not found" }
209 $patron->set_from_api($c->validation->param('body'))->store;
210 $patron->discard_changes;
211 return $c->render( status => 200, openapi => $patron->to_api );
214 unless ( blessed $_ && $_->can('rethrow') ) {
218 error => "Something went wrong, check Koha logs for details."
222 if ( $_->isa('Koha::Exceptions::Object::DuplicateID') ) {
225 openapi => { error => $_->error, conflict => $_->duplicate_id }
228 elsif ( $_->isa('Koha::Exceptions::Object::FKConstraint') ) {
231 openapi => { error => "Given " .
232 $patron->to_api_mapping->{$_->broken_fk}
233 . " does not exist" }
236 elsif ( $_->isa('Koha::Exceptions::MissingParameter') ) {
240 error => "Missing mandatory parameter(s)",
241 parameters => $_->parameter
245 elsif ( $_->isa('Koha::Exceptions::BadParameter') ) {
249 error => "Invalid parameter(s)",
250 parameters => $_->parameter
254 elsif ( $_->isa('Koha::Exceptions::NoChanges') ) {
257 openapi => { error => "No changes have been made" }
261 $c->unhandled_exception($_);
268 Controller function that handles deleting a Koha::Patron object
273 my $c = shift->openapi->valid_input or return;
278 $patron = Koha::Patrons->find( $c->validation->param('patron_id') );
280 # check if loans, reservations, debarrment, etc. before deletion!
291 openapi => { error => "Patron not found" }
295 $c->unhandled_exception($_);
300 =head3 guarantors_can_see_charges
302 Method for setting whether guarantors can see the patron's charges.
306 sub guarantors_can_see_charges {
307 my $c = shift->openapi->valid_input or return;
310 if ( C4::Context->preference('AllowPatronToSetFinesVisibilityForGuarantor') ) {
311 my $patron = $c->stash( 'koha.user' );
312 my $privacy_setting = ($c->req->json->{allowed}) ? 1 : 0;
314 $patron->privacy_guarantor_fines( $privacy_setting )->store;
326 'The current configuration doesn\'t allow the requested action.'
332 $c->unhandled_exception($_);
336 =head3 guarantors_can_see_checkouts
338 Method for setting whether guarantors can see the patron's checkouts.
342 sub guarantors_can_see_checkouts {
343 my $c = shift->openapi->valid_input or return;
346 if ( C4::Context->preference('AllowPatronToSetCheckoutsVisibilityForGuarantor') ) {
347 my $patron = $c->stash( 'koha.user' );
348 my $privacy_setting = ( $c->req->json->{allowed} ) ? 1 : 0;
350 $patron->privacy_guarantor_checkouts( $privacy_setting )->store;
362 'The current configuration doesn\'t allow the requested action.'
368 $c->unhandled_exception($_);