Bug 30719: (QA follow-up) Rename illbatches endpoint to 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( { borrowernumber => { -in => \@patron_ids } } );
56
57     # Get all branches associated with all our batches
58     # in one go
59     my $branches = {};
60     foreach my $batch (@batches) {
61         my $branch_id = $batch->branchcode;
62         $branches->{$branch_id} = 1;
63     }
64     my @branchcodes    = keys %{$branches};
65     my $branch_results = Koha::Libraries->search( { branchcode => { -in => \@branchcodes } } );
66
67     # Get all batch statuses associated with all our batches
68     # in one go
69     my $statuses = {};
70     foreach my $batch (@batches) {
71         my $code = $batch->statuscode;
72         $statuses->{$code} = 1;
73     }
74     my @statuscodes    = keys %{$statuses};
75     my $status_results = Koha::IllbatchStatuses->search( { code => { -in => \@statuscodes } } );
76
77     # Populate the response
78     my @to_return = ();
79     foreach my $it_batch (@batches) {
80         my $patron = $patron_results->find( { borrowernumber => $it_batch->borrowernumber } );
81         my $branch = $branch_results->find( { branchcode     => $it_batch->branchcode } );
82         my $status = $status_results->find( { code           => $it_batch->statuscode } );
83         push @to_return, {
84             batch_id       => $it_batch->id,
85             backend        => $it_batch->backend,
86             library_id     => $it_batch->branchcode,
87             name           => $it_batch->name,
88             statuscode     => $it_batch->statuscode,
89             patron_id      => $it_batch->borrowernumber,
90             patron         => $patron,
91             branch         => $branch,
92             status         => $status,
93             requests_count => $it_batch->requests_count
94         };
95     }
96
97     return $c->render( status => 200, openapi => \@to_return );
98 }
99
100 =head3 get
101
102 Get one batch
103
104 =cut
105
106 sub get {
107     my $c = shift->openapi->valid_input;
108
109     my $batchid = $c->param('ill_batch_id');
110
111     my $batch = Koha::Illbatches->find($batchid);
112
113     if ( not defined $batch ) {
114         return $c->render(
115             status  => 404,
116             openapi => { error => "ILL batch not found" }
117         );
118     }
119
120     return $c->render(
121         status  => 200,
122         openapi => {
123             batch_id       => $batch->id,
124             backend        => $batch->backend,
125             library_id     => $batch->branchcode,
126             name           => $batch->name,
127             statuscode     => $batch->statuscode,
128             patron_id      => $batch->borrowernumber,
129             patron         => $batch->patron->unblessed,
130             branch         => $batch->branch->unblessed,
131             status         => $batch->status->unblessed,
132             requests_count => $batch->requests_count
133         }
134     );
135 }
136
137 =head3 add
138
139 Add a new batch
140
141 =cut
142
143 sub add {
144     my $c = shift->openapi->valid_input or return;
145
146     my $body = $c->req->json;
147
148     # We receive cardnumber, so we need to look up the corresponding
149     # borrowernumber
150     my $patron = Koha::Patrons->find( { cardnumber => $body->{cardnumber} } );
151
152     if ( not defined $patron ) {
153         return $c->render(
154             status  => 404,
155             openapi => { error => "Patron with cardnumber " . $body->{cardnumber} . " not found" }
156         );
157     }
158
159     delete $body->{cardnumber};
160     $body->{borrowernumber} = $patron->borrowernumber;
161     $body->{branchcode}     = delete $body->{library_id};
162
163     return try {
164         my $batch = Koha::Illbatch->new($body);
165         $batch->create_and_log;
166         $c->res->headers->location( $c->req->url->to_string . '/' . $batch->id );
167
168         my $ret = {
169             batch_id       => $batch->id,
170             backend        => $batch->backend,
171             library_id     => $batch->branchcode,
172             name           => $batch->name,
173             statuscode     => $batch->statuscode,
174             patron_id      => $batch->borrowernumber,
175             patron         => $batch->patron->unblessed,
176             branch         => $batch->branch->unblessed,
177             status         => $batch->status->unblessed,
178             requests_count => 0
179         };
180
181         return $c->render(
182             status  => 201,
183             openapi => $ret
184         );
185     } catch {
186         if ( blessed $_ ) {
187             if ( $_->isa('Koha::Exceptions::Object::DuplicateID') ) {
188                 return $c->render(
189                     status  => 409,
190                     openapi => { error => "A batch named " . $body->{name} . " already exists" }
191                 );
192             }
193         }
194         $c->unhandled_exception($_);
195     };
196 }
197
198 =head3 update
199
200 Update a batch
201
202 =cut
203
204 sub update {
205     my $c = shift->openapi->valid_input or return;
206
207     my $batch = Koha::Illbatches->find( $c->param('ill_batch_id') );
208
209     if ( not defined $batch ) {
210         return $c->render(
211             status  => 404,
212             openapi => { error => "ILL batch not found" }
213         );
214     }
215
216     my $params = $c->req->json;
217     delete $params->{cardnumber};
218     $params->{borrowernumber} = delete $params->{patron_id}  if $params->{patron_id};
219     $params->{branchcode}     = delete $params->{library_id} if $params->{library_id};
220
221     return try {
222         $batch->update_and_log($params);
223
224         my $ret = {
225             batch_id       => $batch->id,
226             backend        => $batch->backend,
227             library_id     => $batch->branchcode,
228             name           => $batch->name,
229             statuscode     => $batch->statuscode,
230             patron_id      => $batch->borrowernumber,
231             patron         => $batch->patron->unblessed,
232             branch         => $batch->branch->unblessed,
233             status         => $batch->status->unblessed,
234             requests_count => $batch->requests_count
235         };
236
237         return $c->render(
238             status  => 200,
239             openapi => $ret
240         );
241     } catch {
242         $c->unhandled_exception($_);
243     };
244 }
245
246 =head3 delete
247
248 Delete a batch
249
250 =cut
251
252 sub delete {
253
254     my $c = shift->openapi->valid_input or return;
255
256     my $batch = Koha::Illbatches->find( $c->param('ill_batch_id') );
257
258     if ( not defined $batch ) {
259         return $c->render( status => 404, openapi => { error => "ILL batch not found" } );
260     }
261
262     return try {
263         $batch->delete_and_log;
264         return $c->render( status => 204, openapi => '' );
265     } catch {
266         $c->unhandled_exception($_);
267     };
268 }
269
270 1;