Bug 18290: Fix t/db_dependent/Koha/Object.t, Mojo::JSON::Bool is a JSON::PP::Boolean :)
[koha.git] / Koha / REST / V1 / Hold.pm
blob3ab15cd1949e6e7536be1ed97437cff0774537f1
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 Modern::Perl;
20 use Mojo::Base 'Mojolicious::Controller';
22 use C4::Biblio;
23 use C4::Reserves;
25 use Koha::Items;
26 use Koha::Patrons;
27 use Koha::Holds;
28 use Koha::DateUtils;
30 sub list {
31 my $c = shift->openapi->valid_input or return;
33 my $params = $c->req->query_params->to_hash;
34 my @valid_params = Koha::Holds->_resultset->result_source->columns;
35 foreach my $key (keys %$params) {
36 delete $params->{$key} unless grep { $key eq $_ } @valid_params;
38 my $holds = Koha::Holds->search($params);
40 return $c->render(status => 200, openapi => $holds);
43 sub add {
44 my $c = shift->openapi->valid_input or return;
46 my $body = $c->req->json;
48 my $borrowernumber = $body->{borrowernumber};
49 my $biblionumber = $body->{biblionumber};
50 my $itemnumber = $body->{itemnumber};
51 my $branchcode = $body->{branchcode};
52 my $expirationdate = $body->{expirationdate};
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 eq 'OK') {
91 return $c->render( status => 403, openapi => {
92 error => "Reserve cannot be placed. Reason: $can_reserve"
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);
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,
147 C4::Reserves::ModReserve($params);
148 $hold = Koha::Holds->find($reserve_id);
150 return $c->render( status => 200, openapi => $hold );
153 sub delete {
154 my $c = shift->openapi->valid_input or return;
156 my $reserve_id = $c->validation->param('reserve_id');
157 my $hold = Koha::Holds->find( $reserve_id );
159 unless ($hold) {
160 return $c->render( status => 404, openapi => {error => "Reserve not found"} );
163 $hold->cancel;
165 return $c->render( status => 200, openapi => {} );