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 );
26 use Try::Tiny qw( catch try );
30 Koha::REST::V1::Patrons
38 Controller function that handles listing Koha::Patron objects
43 my $c = shift->openapi->valid_input or return;
48 my $restricted = delete $c->validation->output->{restricted};
49 $query->{debarred} = { '!=' => undef }
52 my $patrons_rs = Koha::Patrons->search($query);
53 my $patrons = $c->objects->search( $patrons_rs );
61 $c->unhandled_exception($_);
67 Controller function that handles retrieving a single Koha::Patron object
72 my $c = shift->openapi->valid_input or return;
75 my $patron_id = $c->validation->param('patron_id');
76 my $patron = $c->objects->find( Koha::Patrons->new, $patron_id );
81 openapi => { error => "Patron not found." }
91 $c->unhandled_exception($_);
97 Controller function that handles adding a new Koha::Patron object
102 my $c = shift->openapi->valid_input or return;
106 Koha::Database->new->schema->txn_do(
109 my $body = $c->validation->param('body');
111 my $extended_attributes = delete $body->{extended_attributes} // [];
113 my $patron = Koha::Patron->new_from_api($body)->store;
114 $patron->extended_attributes(
116 map { { code => $_->{type}, attribute => $_->{value} } }
117 @$extended_attributes
121 $c->res->headers->location($c->req->url->to_string . '/' . $patron->borrowernumber);
124 openapi => $patron->to_api
131 my $to_api_mapping = Koha::Patron->new->to_api_mapping;
134 if ( $_->isa('Koha::Exceptions::Object::DuplicateID') ) {
137 openapi => { error => $_->error, conflict => $_->duplicate_id }
140 elsif ( $_->isa('Koha::Exceptions::Object::FKConstraint') ) {
145 . $to_api_mapping->{ $_->broken_fk }
150 elsif ( $_->isa('Koha::Exceptions::BadParameter') ) {
155 . $to_api_mapping->{ $_->parameter }
161 $_->isa('Koha::Exceptions::Patron::MissingMandatoryExtendedAttribute')
166 openapi => { error => "$_" }
170 $_->isa('Koha::Exceptions::Patron::Attribute::InvalidType')
175 openapi => { error => "$_" }
179 $_->isa('Koha::Exceptions::Patron::Attribute::NonRepeatable')
184 openapi => { error => "$_" }
188 $_->isa('Koha::Exceptions::Patron::Attribute::UniqueIDConstraint')
193 openapi => { error => "$_" }
198 $c->unhandled_exception($_);
205 Controller function that handles updating a Koha::Patron object
210 my $c = shift->openapi->valid_input or return;
212 my $patron_id = $c->validation->param('patron_id');
213 my $patron = Koha::Patrons->find( $patron_id );
218 openapi => { error => "Patron not found" }
223 my $body = $c->validation->param('body');
224 my $user = $c->stash('koha.user');
227 $patron->is_superlibrarian
228 and !$user->is_superlibrarian
229 and ( exists $body->{email}
230 or exists $body->{secondary_email}
231 or exists $body->{altaddress_email} )
234 foreach my $email_field ( qw(email secondary_email altaddress_email) ) {
235 my $exists_email = exists $body->{$email_field};
236 next unless $exists_email;
238 # exists, verify if we are asked to change it
239 my $put_email = $body->{$email_field};
240 # As of writing this patch, 'email' is the only unmapped field
241 # (i.e. it preserves its name, hence this fallback)
242 my $db_email_field = $patron->to_api_mapping->{$email_field} // 'email';
243 my $db_email = $patron->$db_email_field;
247 openapi => { error => "Not enough privileges to change a superlibrarian's email" }
249 unless ( !defined $put_email and !defined $db_email )
250 or ( defined $put_email
251 and defined $db_email
252 and $put_email eq $db_email );
256 $patron->set_from_api($c->validation->param('body'))->store;
257 $patron->discard_changes;
258 return $c->render( status => 200, openapi => $patron->to_api );
261 unless ( blessed $_ && $_->can('rethrow') ) {
265 error => "Something went wrong, check Koha logs for details."
269 if ( $_->isa('Koha::Exceptions::Object::DuplicateID') ) {
272 openapi => { error => $_->error, conflict => $_->duplicate_id }
275 elsif ( $_->isa('Koha::Exceptions::Object::FKConstraint') ) {
278 openapi => { error => "Given " .
279 $patron->to_api_mapping->{$_->broken_fk}
280 . " does not exist" }
283 elsif ( $_->isa('Koha::Exceptions::MissingParameter') ) {
287 error => "Missing mandatory parameter(s)",
288 parameters => $_->parameter
292 elsif ( $_->isa('Koha::Exceptions::BadParameter') ) {
296 error => "Invalid parameter(s)",
297 parameters => $_->parameter
301 elsif ( $_->isa('Koha::Exceptions::NoChanges') ) {
304 openapi => { error => "No changes have been made" }
308 $c->unhandled_exception($_);
315 Controller function that handles deleting a Koha::Patron object
320 my $c = shift->openapi->valid_input or return;
322 my $patron = Koha::Patrons->find( $c->validation->param('patron_id') );
327 openapi => { error => "Patron not found" }
339 if ( blessed $_ && $_->isa('Koha::Exceptions::Patron::FailedDeleteAnonymousPatron') ) {
342 openapi => { error => "Anonymous patron cannot be deleted" }
346 $c->unhandled_exception($_);
350 =head3 guarantors_can_see_charges
352 Method for setting whether guarantors can see the patron's charges.
356 sub guarantors_can_see_charges {
357 my $c = shift->openapi->valid_input or return;
360 if ( C4::Context->preference('AllowPatronToSetFinesVisibilityForGuarantor') ) {
361 my $patron = $c->stash( 'koha.user' );
362 my $privacy_setting = ($c->req->json->{allowed}) ? 1 : 0;
364 $patron->privacy_guarantor_fines( $privacy_setting )->store;
376 'The current configuration doesn\'t allow the requested action.'
382 $c->unhandled_exception($_);
386 =head3 guarantors_can_see_checkouts
388 Method for setting whether guarantors can see the patron's checkouts.
392 sub guarantors_can_see_checkouts {
393 my $c = shift->openapi->valid_input or return;
396 if ( C4::Context->preference('AllowPatronToSetCheckoutsVisibilityForGuarantor') ) {
397 my $patron = $c->stash( 'koha.user' );
398 my $privacy_setting = ( $c->req->json->{allowed} ) ? 1 : 0;
400 $patron->privacy_guarantor_checkouts( $privacy_setting )->store;
412 'The current configuration doesn\'t allow the requested action.'
418 $c->unhandled_exception($_);