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