Bug 30719: ILL Batches
[koha.git] / Koha / REST / V1 / Illbatches.pm
1 package Koha::REST::V1::Illbatches;
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::Illbatches;
23 use Koha::IllbatchStatuses;
24 use Koha::Illrequests;
25
26 use Try::Tiny qw( catch try );
27
28 =head1 NAME
29
30 Koha::REST::V1::Illbatches
31
32 =head2 Operations
33
34 =head3 list
35
36 Return a list of available ILL batches
37
38 =cut
39
40 sub list {
41     my $c = shift->openapi->valid_input;
42
43     #FIXME: This should be $c->objects-search
44     my @batches = Koha::Illbatches->search()->as_list;
45
46     #FIXME: Below should be coming from $c->objects accessors
47     # Get all patrons associated with all our batches
48     # in one go
49     my $patrons = {};
50     foreach my $batch(@batches) {
51         my $patron_id = $batch->borrowernumber;
52         $patrons->{$patron_id} = 1
53     };
54     my @patron_ids = keys %{$patrons};
55     my $patron_results = Koha::Patrons->search({
56         borrowernumber => { -in => \@patron_ids }
57     });
58
59     # Get all branches associated with all our batches
60     # in one go
61     my $branches = {};
62     foreach my $batch(@batches) {
63         my $branch_id = $batch->branchcode;
64         $branches->{$branch_id} = 1
65     };
66     my @branchcodes = keys %{$branches};
67     my $branch_results = Koha::Libraries->search({
68         branchcode => { -in => \@branchcodes }
69     });
70
71     # Get all batch statuses associated with all our batches
72     # in one go
73     my $statuses = {};
74     foreach my $batch(@batches) {
75         my $code = $batch->statuscode;
76         $statuses->{$code} = 1
77     };
78     my @statuscodes = keys %{$statuses};
79     my $status_results = Koha::IllbatchStatuses->search({
80         code => { -in => \@statuscodes }
81     });
82
83     # Populate the response
84     my @to_return = ();
85     foreach my $it_batch(@batches) {
86         my $patron = $patron_results->find({ borrowernumber => $it_batch->borrowernumber});
87         my $branch = $branch_results->find({ branchcode => $it_batch->branchcode });
88         my $status = $status_results->find({ code => $it_batch->statuscode });
89         push @to_return, {
90             %{$it_batch->unblessed},
91             patron         => $patron,
92             branch         => $branch,
93             status         => $status,
94             requests_count => $it_batch->requests_count
95         };
96     }
97
98     return $c->render( status => 200, openapi => \@to_return );
99 }
100
101 =head3 get
102
103 Get one batch
104
105 =cut
106
107 sub get {
108     my $c = shift->openapi->valid_input;
109
110     my $batchid = $c->validation->param('illbatch_id');
111
112     my $batch = Koha::Illbatches->find($batchid);
113
114     if (not defined $batch) {
115         return $c->render(
116             status => 404,
117             openapi => { error => "ILL batch not found" }
118         );
119     }
120
121     return $c->render(
122         status => 200,
123         openapi => {
124             %{$batch->unblessed},
125             patron         => $batch->patron->unblessed,
126             branch         => $batch->branch->unblessed,
127             status         => $batch->status->unblessed,
128             requests_count => $batch->requests_count
129         }
130     );
131 }
132
133 =head3 add
134
135 Add a new batch
136
137 =cut
138
139 sub add {
140     my $c = shift->openapi->valid_input or return;
141
142     my $body = $c->validation->param('body');
143
144     # We receive cardnumber, so we need to look up the corresponding
145     # borrowernumber
146     my $patron = Koha::Patrons->find({ cardnumber => $body->{cardnumber} });
147
148     if ( not defined $patron ) {
149         return $c->render(
150             status  => 404,
151             openapi => { error => "Patron with cardnumber " . $body->{cardnumber} . " not found" }
152         );
153     }
154
155     delete $body->{cardnumber};
156     $body->{borrowernumber} = $patron->borrowernumber;
157
158     return try {
159         my $batch = Koha::Illbatch->new( $body );
160         $batch->create_and_log;
161         $c->res->headers->location( $c->req->url->to_string . '/' . $batch->id );
162
163         my $ret = {
164             %{$batch->unblessed},
165             patron           => $batch->patron->unblessed,
166             branch           => $batch->branch->unblessed,
167             status           => $batch->status->unblessed,
168             requests_count   => 0
169         };
170
171         return $c->render(
172             status  => 201,
173             openapi => $ret
174         );
175     }
176     catch {
177         if ( blessed $_ ) {
178             if ( $_->isa('Koha::Exceptions::Object::DuplicateID') ) {
179                 return $c->render(
180                     status  => 409,
181                     openapi => { error => "A batch named " . $body->{name} . " already exists" }
182                 );
183             }
184         }
185         $c->unhandled_exception($_);
186     };
187 }
188
189 =head3 update
190
191 Update a batch
192
193 =cut
194
195 sub update {
196     my $c = shift->openapi->valid_input or return;
197
198     my $batch = Koha::Illbatches->find( $c->validation->param('illbatch_id') );
199
200     if ( not defined $batch ) {
201         return $c->render(
202             status  => 404,
203             openapi => { error => "ILL batch not found" }
204         );
205     }
206
207     my $params = $c->req->json;
208     delete $params->{cardnumber};
209
210     return try {
211         $batch->update_and_log( $params );
212
213         my $ret = {
214             %{$batch->unblessed},
215             patron         => $batch->patron->unblessed,
216             branch         => $batch->branch->unblessed,
217             status         => $batch->status->unblessed,
218             requests_count => $batch->requests_count
219         };
220
221         return $c->render(
222             status  => 200,
223             openapi => $ret
224         );
225     }
226     catch {
227         $c->unhandled_exception($_);
228     };
229 }
230
231 =head3 delete
232
233 Delete a batch
234
235 =cut
236
237 sub delete {
238
239     my $c = shift->openapi->valid_input or return;
240
241     my $batch = Koha::Illbatches->find( $c->validation->param( 'illbatch_id' ) );
242
243     if ( not defined $batch ) {
244         return $c->render( status => 404, openapi => { error => "ILL batch not found" } );
245     }
246
247     return try {
248         $batch->delete_and_log;
249         return $c->render( status => 204, openapi => '');
250     }
251     catch {
252         $c->unhandled_exception($_);
253     };
254 }
255
256 1;