Bug 17006: Add /patrons/{patron_id}/password
[koha.git] / Koha / REST / V1 / Hold.pm
blobbc25750c0fd3b2f5154fe778d20c0ba5b1b876d4
1 package Koha::REST::V1::Hold;
3 # This file is part of Koha.
5 # Koha is free software; you can redistribute it and/or modify it under the
6 # terms of the GNU General Public License as published by the Free Software
7 # Foundation; either version 3 of the License, or (at your option) any later
8 # version.
10 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
11 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
14 # You should have received a copy of the GNU General Public License along
15 # with Koha; if not, write to the Free Software Foundation, Inc.,
16 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 use Mojo::Base 'Mojolicious::Controller';
20 use C4::Biblio;
21 use C4::Reserves;
23 use Koha::Items;
24 use Koha::Patrons;
25 use Koha::Holds;
26 use Koha::DateUtils;
28 sub list {
29 my $c = shift->openapi->valid_input or return;
31 my $params = $c->req->query_params->to_hash;
32 my @valid_params = Koha::Holds->_resultset->result_source->columns;
33 foreach my $key (keys %$params) {
34 delete $params->{$key} unless grep { $key eq $_ } @valid_params;
36 my $holds = Koha::Holds->search($params);
38 return $c->render(status => 200, openapi => $holds);
41 sub add {
42 my $c = shift->openapi->valid_input or return;
44 my $body = $c->req->json;
46 my $borrowernumber = $body->{borrowernumber};
47 my $biblionumber = $body->{biblionumber};
48 my $itemnumber = $body->{itemnumber};
49 my $branchcode = $body->{branchcode};
50 my $expirationdate = $body->{expirationdate};
51 my $itemtype = $body->{itemtype};
53 my $borrower = Koha::Patrons->find($borrowernumber);
54 unless ($borrower) {
55 return $c->render( status => 404,
56 openapi => {error => "Borrower not found"} );
59 unless ($biblionumber or $itemnumber) {
60 return $c->render( status => 400, openapi => {
61 error => "At least one of biblionumber, itemnumber should be given"
62 } );
64 unless ($branchcode) {
65 return $c->render( status => 400,
66 openapi => { error => "Branchcode is required" } );
69 my $biblio;
70 if ($itemnumber) {
71 my $item = Koha::Items->find( $itemnumber );
72 $biblio = $item->biblio;
73 if ($biblionumber and $biblionumber != $biblio->biblionumber) {
74 return $c->render(
75 status => 400,
76 openapi => {
77 error => "Item $itemnumber doesn't belong to biblio $biblionumber"
78 });
80 $biblionumber ||= $biblio->biblionumber;
81 } else {
82 $biblio = Koha::Biblios->find( $biblionumber );
85 my $can_reserve =
86 $itemnumber
87 ? CanItemBeReserved( $borrowernumber, $itemnumber )
88 : CanBookBeReserved( $borrowernumber, $biblionumber );
90 unless ($can_reserve->{status} eq 'OK') {
91 return $c->render( status => 403, openapi => {
92 error => "Reserve cannot be placed. Reason: ". $can_reserve->{status}
93 } );
96 my $priority = C4::Reserves::CalculatePriority($biblionumber);
97 $itemnumber ||= undef;
99 # AddReserve expects date to be in syspref format
100 if ($expirationdate) {
101 $expirationdate = output_pref(dt_from_string($expirationdate, 'iso'));
104 my $reserve_id = C4::Reserves::AddReserve($branchcode, $borrowernumber,
105 $biblionumber, undef, $priority, undef, $expirationdate, undef,
106 $biblio->title, $itemnumber, undef, $itemtype);
108 unless ($reserve_id) {
109 return $c->render( status => 500, openapi => {
110 error => "Error while placing reserve. See Koha logs for details."
111 } );
114 my $reserve = Koha::Holds->find($reserve_id);
116 return $c->render( status => 201, openapi => $reserve );
119 sub edit {
120 my $c = shift->openapi->valid_input or return;
122 my $reserve_id = $c->validation->param('reserve_id');
123 my $hold = Koha::Holds->find( $reserve_id );
125 unless ($hold) {
126 return $c->render( status => 404,
127 openapi => {error => "Reserve not found"} );
130 my $body = $c->req->json;
132 my $branchcode = $body->{branchcode};
133 my $priority = $body->{priority};
134 my $suspend_until = $body->{suspend_until};
136 if ($suspend_until) {
137 $suspend_until = output_pref(dt_from_string($suspend_until, 'iso'));
140 my $params = {
141 reserve_id => $reserve_id,
142 branchcode => $branchcode,
143 rank => $priority,
144 suspend_until => $suspend_until,
145 itemnumber => $hold->itemnumber
148 C4::Reserves::ModReserve($params);
149 $hold = Koha::Holds->find($reserve_id);
151 return $c->render( status => 200, openapi => $hold );
154 sub delete {
155 my $c = shift->openapi->valid_input or return;
157 my $reserve_id = $c->validation->param('reserve_id');
158 my $hold = Koha::Holds->find( $reserve_id );
160 unless ($hold) {
161 return $c->render( status => 404, openapi => {error => "Reserve not found"} );
164 $hold->cancel;
166 return $c->render( status => 200, openapi => {} );