Bug 35353: Add REST API endpoint to retrieve old holds
[koha.git] / Koha / REST / V1 / Patrons / Holds.pm
1 package Koha::REST::V1::Patrons::Holds;
2
3 # This file is part of Koha.
4 #
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.
9 #
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.
14 #
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>.
17
18 use Modern::Perl;
19
20 use Mojo::Base 'Mojolicious::Controller';
21
22 use Koha::Patrons;
23
24 =head1 NAME
25
26 Koha::REST::V1::Patrons::Holds
27
28 =head1 API
29
30 =head2 Methods
31
32 =head3 list
33
34 Controller function that handles listing Koha::Hold objects for the requested patron
35
36 =cut
37
38 sub list {
39     my $c = shift->openapi->valid_input or return;
40
41     my $patron = Koha::Patrons->find( $c->param('patron_id') );
42
43     unless ( $patron ) {
44         return $c->render(
45             status  => 404,
46             openapi => {
47                 error => 'Patron not found'
48             }
49         );
50     }
51
52     my $old = $c->param('old');
53     $c->req->params->remove('old');
54
55     return try {
56         my $holds_set =
57             $old
58             ? $patron->old_holds
59             : $patron->holds;
60
61         my $holds = $c->objects->search( $holds_set );
62         return $c->render( status => 200, openapi => $holds );
63     } catch {
64         $c->unhandled_exception($_);
65     };
66 }
67
68
69 =head3 delete_public
70
71 Controller function that handles cancelling a hold for the requested patron. Returns
72 a I<204> if cancelling took place, and I<202> if a cancellation request is recorded
73 instead.
74
75 =cut
76
77 sub delete_public {
78     my $c = shift->openapi->valid_input or return;
79
80     return try {
81         my $hold = $c->objects->find_rs( Koha::Holds->new, $c->param('hold_id') );
82
83         unless ( $hold and $c->param('patron_id') == $hold->borrowernumber ) {
84             return $c->render(
85                 status  => 404,
86                 openapi => { error => 'Object not found' }
87             );
88         }
89
90         if ( $hold->is_cancelable_from_opac ) {
91             $hold->cancel;
92             return $c->render(
93                 status  => 204,
94                 openapi => q{},
95             );
96         } elsif ( $hold->is_waiting and $hold->cancellation_requestable_from_opac ) {
97             $hold->add_cancellation_request;
98             return $c->render(
99                 status  => 202,
100                 openapi => q{},
101             );
102         } else {    # reject
103             return $c->render(
104                 status  => 403,
105                 openapi => { error => 'Cancellation forbidden' }
106             );
107         }
108     } catch {
109         $c->unhandled_exception($_);
110     };
111 }
112
113 1;