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