Bug 17003: Add API route to get checkout's renewability
[koha.git] / Koha / REST / V1 / Checkouts.pm
1 package Koha::REST::V1::Checkouts;
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 use Mojo::JSON;
22
23 use C4::Auth qw( haspermission );
24 use C4::Context;
25 use C4::Circulation;
26 use Koha::Checkouts;
27
28 use Try::Tiny;
29
30 =head1 NAME
31
32 Koha::REST::V1::Checkout
33
34 =head1 API
35
36 =head2 Methods
37
38 =head3 list
39
40 List Koha::Checkout objects
41
42 =cut
43
44 sub list {
45     my $c = shift->openapi->valid_input or return;
46     try {
47         my $checkouts_set = Koha::Checkouts->new;
48         my $checkouts = $c->objects->search( $checkouts_set, \&_to_model, \&_to_api );
49         return $c->render( status => 200, openapi => $checkouts );
50     } catch {
51         if ( $_->isa('DBIx::Class::Exception') ) {
52             return $c->render(
53                 status => 500,
54                 openapi => { error => $_->{msg} }
55             );
56         } else {
57             return $c->render(
58                 status => 500,
59                 openapi => { error => "Something went wrong, check the logs." }
60             );
61         }
62     };
63 }
64
65 =head3 get
66
67 get one checkout
68
69 =cut
70
71 sub get {
72     my $c = shift->openapi->valid_input or return;
73
74     my $checkout = Koha::Checkouts->find( $c->validation->param('checkout_id') );
75
76     unless ($checkout) {
77         return $c->render(
78             status => 404,
79             openapi => { error => "Checkout doesn't exist" }
80         );
81     }
82
83     return $c->render(
84         status => 200,
85         openapi => _to_api($checkout->TO_JSON)
86     );
87 }
88
89 =head3 renew
90
91 Renew a checkout
92
93 =cut
94
95 sub renew {
96     my $c = shift->openapi->valid_input or return;
97
98     my $checkout_id = $c->validation->param('checkout_id');
99     my $checkout = Koha::Checkouts->find( $checkout_id );
100
101     unless ($checkout) {
102         return $c->render(
103             status => 404,
104             openapi => { error => "Checkout doesn't exist" }
105         );
106     }
107
108     my $borrowernumber = $checkout->borrowernumber;
109     my $itemnumber = $checkout->itemnumber;
110
111     my ($can_renew, $error) = C4::Circulation::CanBookBeRenewed(
112         $borrowernumber, $itemnumber);
113
114     if (!$can_renew) {
115         return $c->render(
116             status => 403,
117             openapi => { error => "Renewal not authorized ($error)" }
118         );
119     }
120
121     AddRenewal($borrowernumber, $itemnumber, $checkout->branchcode);
122     $checkout = Koha::Checkouts->find($checkout_id);
123
124     $c->res->headers->location( $c->req->url->to_string );
125     return $c->render(
126         status => 201,
127         openapi => _to_api( $checkout->TO_JSON )
128     );
129 }
130
131 sub renewability {
132     my ($c, $args, $cb) = @_;
133
134     my $user = $c->stash('koha.user');
135
136     my $OpacRenewalAllowed;
137     if ($user->borrowernumber == $borrowernumber) {
138         $OpacRenewalAllowed = C4::Context->preference('OpacRenewalAllowed');
139     }
140
141     unless ($user && ($OpacRenewalAllowed
142             || haspermission($user->userid, { circulate => "circulate_remaining_permissions" }))) {
143         return $c->$cb({error => "You don't have the required permission"}, 403);
144     }
145
146     my $checkout_id = $args->{checkout_id};
147     my $checkout = Koha::Issues->find($checkout_id);
148
149     if (!$checkout) {
150         return $c->$cb({
151             error => "Checkout doesn't exist"
152         }, 404);
153     }
154
155     my $borrowernumber = $checkout->borrowernumber;
156     my $itemnumber = $checkout->itemnumber;
157
158     my ($can_renew, $error) = C4::Circulation::CanBookBeRenewed(
159         $borrowernumber, $itemnumber);
160
161     return $c->$cb({ renewable => Mojo::JSON->true, error => undef }, 200) if $can_renew;
162     return $c->$cb({ renewable => Mojo::JSON->false, error => $error }, 200);
163 }
164
165 =head3 _to_api
166
167 Helper function that maps a hashref of Koha::Checkout attributes into REST api
168 attribute names.
169
170 =cut
171
172 sub _to_api {
173     my $checkout = shift;
174
175     foreach my $column ( keys %{ $Koha::REST::V1::Checkouts::to_api_mapping } ) {
176         my $mapped_column = $Koha::REST::V1::Checkouts::to_api_mapping->{$column};
177         if ( exists $checkout->{ $column } && defined $mapped_column )
178         {
179             $checkout->{ $mapped_column } = delete $checkout->{ $column };
180         }
181         elsif ( exists $checkout->{ $column } && !defined $mapped_column ) {
182             delete $checkout->{ $column };
183         }
184     }
185     return $checkout;
186 }
187
188 =head3 _to_model
189
190 Helper function that maps REST api objects into Koha::Checkouts
191 attribute names.
192
193 =cut
194
195 sub _to_model {
196     my $checkout = shift;
197
198     foreach my $attribute ( keys %{ $Koha::REST::V1::Checkouts::to_model_mapping } ) {
199         my $mapped_attribute = $Koha::REST::V1::Checkouts::to_model_mapping->{$attribute};
200         if ( exists $checkout->{ $attribute } && defined $mapped_attribute )
201         {
202             $checkout->{ $mapped_attribute } = delete $checkout->{ $attribute };
203         }
204         elsif ( exists $checkout->{ $attribute } && !defined $mapped_attribute )
205         {
206             delete $checkout->{ $attribute };
207         }
208     }
209     return $checkout;
210 }
211
212 =head2 Global variables
213
214 =head3 $to_api_mapping
215
216 =cut
217
218 our $to_api_mapping = {
219     issue_id        => 'checkout_id',
220     borrowernumber  => 'patron_id',
221     itemnumber      => 'item_id',
222     date_due        => 'due_date',
223     branchcode      => 'library_id',
224     returndate      => 'checkin_date',
225     lastreneweddate => 'last_renewed_date',
226     issuedate       => 'checkout_date',
227     notedate        => 'note_date',
228 };
229
230 =head3 $to_model_mapping
231
232 =cut
233
234 our $to_model_mapping = {
235     checkout_id       => 'issue_id',
236     patron_id         => 'borrowernumber',
237     item_id           => 'itemnumber',
238     due_date          => 'date_due',
239     library_id        => 'branchcode',
240     checkin_date      => 'returndate',
241     last_renewed_date => 'lastreneweddate',
242     checkout_date     => 'issuedate',
243     note_date         => 'notedate',
244 };
245
246 1;