6b061dca66b3fe1f21383e3e05a9107bb0ffe61e
[srvgit] / 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 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.
9 #
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.
13 #
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.
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
62         if ( $item_id and $biblio_id ) {
63
64             # check they are consistent
65             unless ( Koha::Items->search( { itemnumber => $item_id, biblionumber => $biblio_id } )
66                 ->count > 0 )
67             {
68                 return $c->render(
69                     status  => 400,
70                     openapi => { error => "Item $item_id doesn't belong to biblio $biblio_id" }
71                 );
72             }
73             else {
74                 $biblio = Koha::Biblios->find($biblio_id);
75             }
76         }
77         elsif ($item_id) {
78             my $item = Koha::Items->find($item_id);
79
80             unless ($item) {
81                 return $c->render(
82                     status  => 404,
83                     openapi => { error => "item_id not found." }
84                 );
85             }
86             else {
87                 $biblio = $item->biblio;
88             }
89         }
90         elsif ($biblio_id) {
91             $biblio = Koha::Biblios->find($biblio_id);
92         }
93         else {
94             return $c->render(
95                 status  => 400,
96                 openapi => { error => "At least one of biblio_id, item_id should be given" }
97             );
98         }
99
100         unless ($biblio) {
101             return $c->render(
102                 status  => 400,
103                 openapi => "Biblio not found."
104             );
105         }
106
107         # AddReserve expects date to be in syspref format
108         if ($expiration_date) {
109             $expiration_date = output_pref( dt_from_string( $expiration_date, 'rfc3339' ) );
110         }
111
112         my $club_hold = Koha::Club::Hold::add({
113             club_id => $club_id,
114             biblio_id => $biblio->biblionumber,
115             item_id => $item_id,
116             pickup_library_id => $pickup_library_id,
117             expiration_date => $expiration_date,
118             notes => $notes,
119             item_type => $item_type
120         });
121
122         return $c->render(
123             status  => 201,
124             openapi => $club_hold->to_api
125         );
126     }
127     catch {
128         if ( blessed $_ and $_->isa('Koha::Exceptions::Object') ) {
129             if ( $_->isa('Koha::Exceptions::Object::FKConstraint') ) {
130                 my $broken_fk = $_->broken_fk;
131
132                 if ( grep { $_ eq $broken_fk } keys %{$Koha::REST::V1::Clubs::Holds::to_api_mapping} ) {
133                     $c->render(
134                         status  => 404,
135                         openapi => $Koha::REST::V1::Clubs::Holds::to_api_mapping->{$broken_fk} . ' not found.'
136                     );
137                 }
138                 else {
139                     return $c->render(
140                         status  => 500,
141                         openapi => { error => "Uncaught exception: $_" }
142                     );
143                 }
144             }
145             else {
146                 return $c->render(
147                     status  => 500,
148                     openapi => { error => "$_" }
149                 );
150             }
151         }
152         elsif (blessed $_ and $_->isa('Koha::Exceptions::ClubHold')) {
153             return $c->render(
154                 status  => 500,
155                 openapi => { error => $_->description }
156             );
157         }
158         else {
159             return $c->render(
160                 status  => 500,
161                 openapi => { error => "Something went wrong. check the logs." }
162             );
163         }
164     };
165 }
166
167 =head3 _to_api
168
169 Helper function that maps unblessed Koha::Club::Hold objects into REST api
170 attribute names.
171
172 =cut
173
174 sub _to_api {
175     my $club_hold    = shift;
176
177     # Rename attributes
178     foreach my $column ( keys %{ $Koha::REST::V1::Clubs::Holds::to_api_mapping } ) {
179         my $mapped_column = $Koha::REST::V1::Clubs::Holds::to_api_mapping->{$column};
180         if (    exists $club_hold->{ $column }
181              && defined $mapped_column )
182         {
183             # key != undef
184             $club_hold->{ $mapped_column } = delete $club_hold->{ $column };
185         }
186         elsif (    exists $club_hold->{ $column }
187                 && !defined $mapped_column )
188         {
189             # key == undef
190             delete $club_hold->{ $column };
191         }
192     }
193
194     # Calculate the 'restricted' field
195     return $club_hold;
196 }
197
198 =head3 $to_api_mapping
199
200 =cut
201
202 our $to_api_mapping = {
203     id => 'club_hold_id',
204     club_id => 'club_id',
205     biblio_id => 'biblio_id',
206     item_id => 'item_id'
207 };
208
209
210 1;