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';
26 use List::MoreUtils qw(any);
27 use Scalar::Util qw( blessed );
28 use Try::Tiny qw( catch try );
32 Koha::REST::V1::Patrons
40 Controller function that handles listing Koha::Patron objects
45 my $c = shift->openapi->valid_input or return;
50 my $restricted = delete $c->validation->output->{restricted};
51 $query->{debarred} = { '!=' => undef }
54 my $patrons_rs = Koha::Patrons->search($query);
55 my $patrons = $c->objects->search( $patrons_rs );
63 $c->unhandled_exception($_);
69 Controller function that handles retrieving a single Koha::Patron object
74 my $c = shift->openapi->valid_input or return;
77 my $patron_id = $c->validation->param('patron_id');
78 my $patron = $c->objects->find( Koha::Patrons->search_limited, $patron_id );
83 openapi => { error => "Patron not found." }
93 $c->unhandled_exception($_);
99 Controller function that handles adding a new Koha::Patron object
104 my $c = shift->openapi->valid_input or return;
108 Koha::Database->new->schema->txn_do(
111 my $body = $c->validation->param('body');
113 my $extended_attributes = delete $body->{extended_attributes} // [];
115 my $patron = Koha::Patron->new_from_api($body)->store;
116 $patron->extended_attributes(
118 map { { code => $_->{type}, attribute => $_->{value} } }
119 @$extended_attributes
122 if ( C4::Context->preference('EnhancedMessagingPreferences') ) {
123 C4::Members::Messaging::SetMessagingPreferencesFromDefaults(
125 borrowernumber => $patron->borrowernumber,
126 categorycode => $patron->categorycode,
131 $c->res->headers->location($c->req->url->to_string . '/' . $patron->borrowernumber);
134 openapi => $patron->to_api
141 my $to_api_mapping = Koha::Patron->new->to_api_mapping;
144 if ( $_->isa('Koha::Exceptions::Object::DuplicateID') ) {
147 openapi => { error => $_->error, conflict => $_->duplicate_id }
150 elsif ( $_->isa('Koha::Exceptions::Object::FKConstraint') ) {
155 . $to_api_mapping->{ $_->broken_fk }
160 elsif ( $_->isa('Koha::Exceptions::BadParameter') ) {
165 . $to_api_mapping->{ $_->parameter }
171 $_->isa('Koha::Exceptions::Patron::MissingMandatoryExtendedAttribute')
176 openapi => { error => "$_" }
180 $_->isa('Koha::Exceptions::Patron::Attribute::InvalidType')
185 openapi => { error => "$_" }
189 $_->isa('Koha::Exceptions::Patron::Attribute::NonRepeatable')
194 openapi => { error => "$_" }
198 $_->isa('Koha::Exceptions::Patron::Attribute::UniqueIDConstraint')
203 openapi => { error => "$_" }
208 $c->unhandled_exception($_);
215 Controller function that handles updating a Koha::Patron object
220 my $c = shift->openapi->valid_input or return;
222 my $patron_id = $c->validation->param('patron_id');
223 my $patron = Koha::Patrons->find( $patron_id );
228 openapi => { error => "Patron not found" }
233 my $body = $c->validation->param('body');
234 my $user = $c->stash('koha.user');
237 $patron->is_superlibrarian
238 and !$user->is_superlibrarian
239 and ( exists $body->{email}
240 or exists $body->{secondary_email}
241 or exists $body->{altaddress_email} )
244 foreach my $email_field ( qw(email secondary_email altaddress_email) ) {
245 my $exists_email = exists $body->{$email_field};
246 next unless $exists_email;
248 # exists, verify if we are asked to change it
249 my $put_email = $body->{$email_field};
250 # As of writing this patch, 'email' is the only unmapped field
251 # (i.e. it preserves its name, hence this fallback)
252 my $db_email_field = $patron->to_api_mapping->{$email_field} // 'email';
253 my $db_email = $patron->$db_email_field;
257 openapi => { error => "Not enough privileges to change a superlibrarian's email" }
259 unless ( !defined $put_email and !defined $db_email )
260 or ( defined $put_email
261 and defined $db_email
262 and $put_email eq $db_email );
266 $patron->set_from_api($c->validation->param('body'))->store;
267 $patron->discard_changes;
268 return $c->render( status => 200, openapi => $patron->to_api );
271 unless ( blessed $_ && $_->can('rethrow') ) {
275 error => "Something went wrong, check Koha logs for details."
279 if ( $_->isa('Koha::Exceptions::Object::DuplicateID') ) {
282 openapi => { error => $_->error, conflict => $_->duplicate_id }
285 elsif ( $_->isa('Koha::Exceptions::Object::FKConstraint') ) {
288 openapi => { error => "Given " .
289 $patron->to_api_mapping->{$_->broken_fk}
290 . " does not exist" }
293 elsif ( $_->isa('Koha::Exceptions::MissingParameter') ) {
297 error => "Missing mandatory parameter(s)",
298 parameters => $_->parameter
302 elsif ( $_->isa('Koha::Exceptions::BadParameter') ) {
306 error => "Invalid parameter(s)",
307 parameters => $_->parameter
311 elsif ( $_->isa('Koha::Exceptions::NoChanges') ) {
314 openapi => { error => "No changes have been made" }
318 $c->unhandled_exception($_);
325 Controller function that handles deleting a Koha::Patron object
330 my $c = shift->openapi->valid_input or return;
332 my $patron = Koha::Patrons->find( $c->validation->param('patron_id') );
337 openapi => { error => "Patron not found" }
343 my $safe_to_delete = $patron->safe_to_delete;
345 if ( !$safe_to_delete ) {
346 # Pick the first error, if any
347 my ( $error ) = grep { $_->type eq 'error' } @{ $safe_to_delete->messages };
349 Koha::Exception->throw('Koha::Patron->safe_to_delete returned false but carried no error message');
352 my $error_descriptions = {
353 has_checkouts => 'Pending checkouts prevent deletion',
354 has_debt => 'Pending debts prevent deletion',
355 has_guarantees => 'Patron is a guarantor and it prevents deletion',
356 is_anonymous_patron => 'Anonymous patron cannot be deleted',
359 if ( any { $error->message eq $_ } keys %{$error_descriptions} ) {
363 error => $error_descriptions->{ $error->message },
364 error_code => $error->message,
368 Koha::Exception->throw( 'Koha::Patron->safe_to_delete carried an unexpected message: ' . $error->message );
372 return $patron->_result->result_source->schema->txn_do(
374 $patron->move_to_deleted;
385 $c->unhandled_exception($_);
389 =head3 guarantors_can_see_charges
391 Method for setting whether guarantors can see the patron's charges.
395 sub guarantors_can_see_charges {
396 my $c = shift->openapi->valid_input or return;
399 if ( C4::Context->preference('AllowPatronToSetFinesVisibilityForGuarantor') ) {
400 my $patron = $c->stash( 'koha.user' );
401 my $privacy_setting = ($c->req->json->{allowed}) ? 1 : 0;
403 $patron->privacy_guarantor_fines( $privacy_setting )->store;
415 'The current configuration doesn\'t allow the requested action.'
421 $c->unhandled_exception($_);
425 =head3 guarantors_can_see_checkouts
427 Method for setting whether guarantors can see the patron's checkouts.
431 sub guarantors_can_see_checkouts {
432 my $c = shift->openapi->valid_input or return;
435 if ( C4::Context->preference('AllowPatronToSetCheckoutsVisibilityForGuarantor') ) {
436 my $patron = $c->stash( 'koha.user' );
437 my $privacy_setting = ( $c->req->json->{allowed} ) ? 1 : 0;
439 $patron->privacy_guarantor_checkouts( $privacy_setting )->store;
451 'The current configuration doesn\'t allow the requested action.'
457 $c->unhandled_exception($_);