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 Scalar::Util qw(blessed);
31 Koha::REST::V1::Patrons
39 Controller function that handles listing Koha::Patron objects
44 my $c = shift->openapi->valid_input or return;
49 my $restricted = delete $c->validation->output->{restricted};
50 $query->{debarred} = { '!=' => undef }
53 my $patrons_rs = Koha::Patrons->search($query);
54 my $patrons = $c->objects->search( $patrons_rs );
62 $c->unhandled_exception($_);
68 Controller function that handles retrieving a single Koha::Patron object
73 my $c = shift->openapi->valid_input or return;
76 my $patron_id = $c->validation->param('patron_id');
77 my $patron = $c->objects->find( Koha::Patrons->new, $patron_id );
82 openapi => { error => "Patron not found." }
92 $c->unhandled_exception($_);
98 Controller function that handles adding a new Koha::Patron object
103 my $c = shift->openapi->valid_input or return;
107 Koha::Database->new->schema->txn_do(
110 my $body = $c->validation->param('body');
112 my $extended_attributes = delete $body->{extended_attributes} // [];
114 my $patron = Koha::Patron->new_from_api($body)->store;
115 $patron->extended_attributes(
117 map { { code => $_->{type}, attribute => $_->{value} } }
118 @$extended_attributes
122 $c->res->headers->location($c->req->url->to_string . '/' . $patron->borrowernumber);
125 openapi => $patron->to_api
132 my $to_api_mapping = Koha::Patron->new->to_api_mapping;
135 if ( $_->isa('Koha::Exceptions::Object::DuplicateID') ) {
138 openapi => { error => $_->error, conflict => $_->duplicate_id }
141 elsif ( $_->isa('Koha::Exceptions::Object::FKConstraint') ) {
146 . $to_api_mapping->{ $_->broken_fk }
151 elsif ( $_->isa('Koha::Exceptions::BadParameter') ) {
156 . $to_api_mapping->{ $_->parameter }
162 $_->isa('Koha::Exceptions::Patron::MissingMandatoryExtendedAttribute')
167 openapi => { error => "$_" }
171 $_->isa('Koha::Exceptions::Patron::Attribute::InvalidType')
176 openapi => { error => "$_" }
180 $_->isa('Koha::Exceptions::Patron::Attribute::NonRepeatable')
185 openapi => { error => "$_" }
189 $_->isa('Koha::Exceptions::Patron::Attribute::UniqueIDConstraint')
194 openapi => { error => "$_" }
199 $c->unhandled_exception($_);
206 Controller function that handles updating a Koha::Patron object
211 my $c = shift->openapi->valid_input or return;
213 my $patron_id = $c->validation->param('patron_id');
214 my $patron = Koha::Patrons->find( $patron_id );
219 openapi => { error => "Patron not found" }
224 my $body = $c->validation->param('body');
225 my $user = $c->stash('koha.user');
228 $patron->is_superlibrarian
229 and !$user->is_superlibrarian
230 and ( exists $body->{email}
231 or exists $body->{secondary_email}
232 or exists $body->{altaddress_email} )
235 foreach my $email_field ( qw(email secondary_email altaddress_email) ) {
236 my $exists_email = exists $body->{$email_field};
237 next unless $exists_email;
239 # exists, verify if we are asked to change it
240 my $put_email = $body->{$email_field};
241 # As of writing this patch, 'email' is the only unmapped field
242 # (i.e. it preserves its name, hence this fallback)
243 my $db_email_field = $patron->to_api_mapping->{$email_field} // 'email';
244 my $db_email = $patron->$db_email_field;
248 openapi => { error => "Not enough privileges to change a superlibrarian's email" }
250 unless ( !defined $put_email and !defined $db_email )
251 or ( defined $put_email
252 and defined $db_email
253 and $put_email eq $db_email );
257 $patron->set_from_api($c->validation->param('body'))->store;
258 $patron->discard_changes;
259 return $c->render( status => 200, openapi => $patron->to_api );
262 unless ( blessed $_ && $_->can('rethrow') ) {
266 error => "Something went wrong, check Koha logs for details."
270 if ( $_->isa('Koha::Exceptions::Object::DuplicateID') ) {
273 openapi => { error => $_->error, conflict => $_->duplicate_id }
276 elsif ( $_->isa('Koha::Exceptions::Object::FKConstraint') ) {
279 openapi => { error => "Given " .
280 $patron->to_api_mapping->{$_->broken_fk}
281 . " does not exist" }
284 elsif ( $_->isa('Koha::Exceptions::MissingParameter') ) {
288 error => "Missing mandatory parameter(s)",
289 parameters => $_->parameter
293 elsif ( $_->isa('Koha::Exceptions::BadParameter') ) {
297 error => "Invalid parameter(s)",
298 parameters => $_->parameter
302 elsif ( $_->isa('Koha::Exceptions::NoChanges') ) {
305 openapi => { error => "No changes have been made" }
309 $c->unhandled_exception($_);
316 Controller function that handles deleting a Koha::Patron object
321 my $c = shift->openapi->valid_input or return;
323 my $patron = Koha::Patrons->find( $c->validation->param('patron_id') );
328 openapi => { error => "Patron not found" }
340 if ( blessed $_ && $_->isa('Koha::Exceptions::Patron::FailedDeleteAnonymousPatron') ) {
343 openapi => { error => "Anonymous patron cannot be deleted" }
347 $c->unhandled_exception($_);
351 =head3 guarantors_can_see_charges
353 Method for setting whether guarantors can see the patron's charges.
357 sub guarantors_can_see_charges {
358 my $c = shift->openapi->valid_input or return;
361 if ( C4::Context->preference('AllowPatronToSetFinesVisibilityForGuarantor') ) {
362 my $patron = $c->stash( 'koha.user' );
363 my $privacy_setting = ($c->req->json->{allowed}) ? 1 : 0;
365 $patron->privacy_guarantor_fines( $privacy_setting )->store;
377 'The current configuration doesn\'t allow the requested action.'
383 $c->unhandled_exception($_);
387 =head3 guarantors_can_see_checkouts
389 Method for setting whether guarantors can see the patron's checkouts.
393 sub guarantors_can_see_checkouts {
394 my $c = shift->openapi->valid_input or return;
397 if ( C4::Context->preference('AllowPatronToSetCheckoutsVisibilityForGuarantor') ) {
398 my $patron = $c->stash( 'koha.user' );
399 my $privacy_setting = ( $c->req->json->{allowed} ) ? 1 : 0;
401 $patron->privacy_guarantor_checkouts( $privacy_setting )->store;
413 'The current configuration doesn\'t allow the requested action.'
419 $c->unhandled_exception($_);