Bug 14708: (QA follow-up) Add tests
[koha.git] / Koha / REST / V1 / Patrons.pm
blob5eba4bdee34433d561fa67d79d2494471d4485f0
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>.
18 use Modern::Perl;
20 use Mojo::Base 'Mojolicious::Controller';
22 use Koha::DateUtils;
23 use Koha::Patrons;
25 use Scalar::Util qw(blessed);
26 use Try::Tiny;
28 =head1 NAME
30 Koha::REST::V1::Patrons
32 =head1 API
34 =head2 Methods
36 =head3 list
38 Controller function that handles listing Koha::Patron objects
40 =cut
42 sub list {
43 my $c = shift->openapi->valid_input or return;
45 return try {
47 my $patrons_rs = Koha::Patrons->new;
48 my $args = $c->validation->output;
49 my $attributes = {};
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 }
82 if $restricted;
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,
89 params => $args,
94 return $c->render( status => 200, openapi => $patrons->to_api );
96 catch {
97 $c->unhandled_exception($_);
102 =head3 get
104 Controller function that handles retrieving a single Koha::Patron object
106 =cut
108 sub get {
109 my $c = shift->openapi->valid_input or return;
111 return try {
112 my $patron_id = $c->validation->param('patron_id');
113 my $patron = Koha::Patrons->find($patron_id);
115 unless ($patron) {
116 return $c->render( status => 404, openapi => { error => "Patron not found." } );
119 return $c->render( status => 200, openapi => $patron->to_api );
121 catch {
122 $c->unhandled_exception($_);
126 =head3 add
128 Controller function that handles adding a new Koha::Patron object
130 =cut
132 sub add {
133 my $c = shift->openapi->valid_input or return;
135 return try {
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 );
140 return $c->render(
141 status => 201,
142 openapi => $patron->to_api
145 catch {
147 my $to_api_mapping = Koha::Patron->new->to_api_mapping;
149 unless ( blessed $_ && $_->can('rethrow') ) {
150 return $c->render(
151 status => 500,
152 openapi => { error => "Something went wrong, check Koha logs for details." }
155 if ( $_->isa('Koha::Exceptions::Object::DuplicateID') ) {
156 return $c->render(
157 status => 409,
158 openapi => { error => $_->error, conflict => $_->duplicate_id }
161 elsif ( $_->isa('Koha::Exceptions::Object::FKConstraint') ) {
162 return $c->render(
163 status => 400,
164 openapi => {
165 error => "Given "
166 . $to_api_mapping->{ $_->broken_fk }
167 . " does not exist"
171 elsif ( $_->isa('Koha::Exceptions::BadParameter') ) {
172 return $c->render(
173 status => 400,
174 openapi => {
175 error => "Given "
176 . $to_api_mapping->{ $_->parameter }
177 . " does not exist"
181 else {
182 $c->unhandled_exception($_);
188 =head3 update
190 Controller function that handles updating a Koha::Patron object
192 =cut
194 sub update {
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 );
200 unless ($patron) {
201 return $c->render(
202 status => 404,
203 openapi => { error => "Patron not found" }
207 return try {
208 my $body = $c->validation->param('body');
209 my $user = $c->stash('koha.user');
211 if (
212 $patron->is_superlibrarian
213 and !$user->is_superlibrarian
214 and ( exists $body->{email}
215 or exists $body->{secondary_email}
216 or exists $body->{altaddress_email} )
219 foreach my $email_field ( qw(email secondary_email altaddress_email) ) {
220 my $exists_email = exists $body->{$email_field};
221 next unless $exists_email;
223 # exists, verify if we are asked to change it
224 my $put_email = $body->{$email_field};
225 # As of writing this patch, 'email' is the only unmapped field
226 # (i.e. it preserves its name, hence this fallback)
227 my $db_email_field = $patron->to_api_mapping->{$email_field} // 'email';
228 my $db_email = $patron->$db_email_field;
230 return $c->render(
231 status => 403,
232 openapi => { error => "Not enough privileges to change a superlibrarian's email" }
234 unless ( !defined $put_email and !defined $db_email )
235 or ( defined $put_email
236 and defined $db_email
237 and $put_email eq $db_email );
241 $patron->set_from_api($c->validation->param('body'))->store;
242 $patron->discard_changes;
243 return $c->render( status => 200, openapi => $patron->to_api );
245 catch {
246 unless ( blessed $_ && $_->can('rethrow') ) {
247 return $c->render(
248 status => 500,
249 openapi => {
250 error => "Something went wrong, check Koha logs for details."
254 if ( $_->isa('Koha::Exceptions::Object::DuplicateID') ) {
255 return $c->render(
256 status => 409,
257 openapi => { error => $_->error, conflict => $_->duplicate_id }
260 elsif ( $_->isa('Koha::Exceptions::Object::FKConstraint') ) {
261 return $c->render(
262 status => 400,
263 openapi => { error => "Given " .
264 $patron->to_api_mapping->{$_->broken_fk}
265 . " does not exist" }
268 elsif ( $_->isa('Koha::Exceptions::MissingParameter') ) {
269 return $c->render(
270 status => 400,
271 openapi => {
272 error => "Missing mandatory parameter(s)",
273 parameters => $_->parameter
277 elsif ( $_->isa('Koha::Exceptions::BadParameter') ) {
278 return $c->render(
279 status => 400,
280 openapi => {
281 error => "Invalid parameter(s)",
282 parameters => $_->parameter
286 elsif ( $_->isa('Koha::Exceptions::NoChanges') ) {
287 return $c->render(
288 status => 204,
289 openapi => { error => "No changes have been made" }
292 else {
293 $c->unhandled_exception($_);
298 =head3 delete
300 Controller function that handles deleting a Koha::Patron object
302 =cut
304 sub delete {
305 my $c = shift->openapi->valid_input or return;
307 my $patron = Koha::Patrons->find( $c->validation->param('patron_id') );
309 unless ( $patron ) {
310 return $c->render(
311 status => 404,
312 openapi => { error => "Patron not found" }
316 return try {
318 $patron->delete;
319 return $c->render(
320 status => 204,
321 openapi => q{}
323 } catch {
324 if ( blessed $_ && $_->isa('Koha::Exceptions::Patron::FailedDeleteAnonymousPatron') ) {
325 return $c->render(
326 status => 403,
327 openapi => { error => "Anonymous patron cannot be deleted" }
331 $c->unhandled_exception($_);
335 =head3 guarantors_can_see_charges
337 Method for setting whether guarantors can see the patron's charges.
339 =cut
341 sub guarantors_can_see_charges {
342 my $c = shift->openapi->valid_input or return;
344 return try {
345 if ( C4::Context->preference('AllowPatronToSetFinesVisibilityForGuarantor') ) {
346 my $patron = $c->stash( 'koha.user' );
347 my $privacy_setting = ($c->req->json->{allowed}) ? 1 : 0;
349 $patron->privacy_guarantor_fines( $privacy_setting )->store;
351 return $c->render(
352 status => 200,
353 openapi => {}
356 else {
357 return $c->render(
358 status => 403,
359 openapi => {
360 error =>
361 'The current configuration doesn\'t allow the requested action.'
366 catch {
367 $c->unhandled_exception($_);
371 =head3 guarantors_can_see_checkouts
373 Method for setting whether guarantors can see the patron's checkouts.
375 =cut
377 sub guarantors_can_see_checkouts {
378 my $c = shift->openapi->valid_input or return;
380 return try {
381 if ( C4::Context->preference('AllowPatronToSetCheckoutsVisibilityForGuarantor') ) {
382 my $patron = $c->stash( 'koha.user' );
383 my $privacy_setting = ( $c->req->json->{allowed} ) ? 1 : 0;
385 $patron->privacy_guarantor_checkouts( $privacy_setting )->store;
387 return $c->render(
388 status => 200,
389 openapi => {}
392 else {
393 return $c->render(
394 status => 403,
395 openapi => {
396 error =>
397 'The current configuration doesn\'t allow the requested action.'
402 catch {
403 $c->unhandled_exception($_);