Bug 27330: Return 403 if club has no enrollments
[koha.git] / Koha / REST / V1 / Clubs / Holds.pm
1 package Koha::REST::V1::Clubs::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 C4::Biblio;
23 use C4::Reserves;
24
25 use Koha::Items;
26 use Koha::Patrons;
27 use Koha::Holds;
28 use Koha::Clubs;
29 use Koha::Club::Hold;
30 use Koha::DateUtils;
31
32 use Scalar::Util qw(blessed);
33 use Try::Tiny;
34 use List::Util 'shuffle';
35
36 =head1 API
37
38 =head2 Class methods
39
40 =head3 add
41
42 Method that handles adding a new Koha::Hold object
43
44 =cut
45
46 sub add {
47     my $c = shift->openapi->valid_input or return;
48
49     return try {
50         my $body    = $c->validation->param('body');
51         my $club_id = $c->validation->param('club_id');
52
53         my $biblio;
54
55         my $biblio_id         = $body->{biblio_id};
56         my $pickup_library_id = $body->{pickup_library_id};
57         my $item_id           = $body->{item_id};
58         my $item_type         = $body->{item_type};
59         my $expiration_date   = $body->{expiration_date};
60         my $notes             = $body->{notes};
61         my $default_patron_home = $body->{default_patron_home};
62
63         if ( $item_id and $biblio_id ) {
64
65             # check they are consistent
66             unless ( Koha::Items->search( { itemnumber => $item_id, biblionumber => $biblio_id } )
67                 ->count > 0 )
68             {
69                 return $c->render(
70                     status  => 400,
71                     openapi => { error => "Item $item_id doesn't belong to biblio $biblio_id" }
72                 );
73             }
74             else {
75                 $biblio = Koha::Biblios->find($biblio_id);
76             }
77         }
78         elsif ($item_id) {
79             my $item = Koha::Items->find($item_id);
80
81             unless ($item) {
82                 return $c->render(
83                     status  => 404,
84                     openapi => { error => "item_id not found." }
85                 );
86             }
87             else {
88                 $biblio = $item->biblio;
89             }
90         }
91         elsif ($biblio_id) {
92             $biblio = Koha::Biblios->find($biblio_id);
93         }
94         else {
95             return $c->render(
96                 status  => 400,
97                 openapi => { error => "At least one of biblio_id, item_id should be given" }
98             );
99         }
100
101         unless ($biblio) {
102             return $c->render(
103                 status  => 400,
104                 openapi => "Biblio not found."
105             );
106         }
107
108         # AddReserve expects date to be in syspref format
109         if ($expiration_date) {
110             $expiration_date = output_pref( dt_from_string( $expiration_date, 'rfc3339' ) );
111         }
112
113         my $club_hold = Koha::Club::Hold::add({
114             club_id => $club_id,
115             biblio_id => $biblio->biblionumber,
116             item_id => $item_id,
117             pickup_library_id => $pickup_library_id,
118             expiration_date => $expiration_date,
119             notes => $notes,
120             item_type => $item_type,
121             default_patron_home => $default_patron_home
122         });
123
124         return $c->render(
125             status  => 201,
126             openapi => $club_hold->to_api
127         );
128     }
129     catch {
130         if ( blessed $_ ) {
131             if ($_->isa('Koha::Exceptions::ClubHold::NoPatrons')) {
132                 return $c->render(
133                     status  => 403,
134                     openapi => { error => $_->description }
135                 );
136             }
137         }
138
139         $c->unhandled_exception($_);
140     };
141 }
142
143 =head3 _to_api
144
145 Helper function that maps unblessed Koha::Club::Hold objects into REST api
146 attribute names.
147
148 =cut
149
150 sub _to_api {
151     my $club_hold    = shift;
152
153     # Rename attributes
154     foreach my $column ( keys %{ $Koha::REST::V1::Clubs::Holds::to_api_mapping } ) {
155         my $mapped_column = $Koha::REST::V1::Clubs::Holds::to_api_mapping->{$column};
156         if (    exists $club_hold->{ $column }
157              && defined $mapped_column )
158         {
159             # key != undef
160             $club_hold->{ $mapped_column } = delete $club_hold->{ $column };
161         }
162         elsif (    exists $club_hold->{ $column }
163                 && !defined $mapped_column )
164         {
165             # key == undef
166             delete $club_hold->{ $column };
167         }
168     }
169
170     # Calculate the 'restricted' field
171     return $club_hold;
172 }
173
174 =head3 $to_api_mapping
175
176 =cut
177
178 our $to_api_mapping = {
179     id => 'club_hold_id',
180     club_id => 'club_id',
181     biblio_id => 'biblio_id',
182     item_id => 'item_id'
183 };
184
185
186 1;