Lines 19-28
use Modern::Perl;
Link Here
|
19 |
|
19 |
|
20 |
use Mojo::Base 'Mojolicious::Controller'; |
20 |
use Mojo::Base 'Mojolicious::Controller'; |
21 |
use Mojo::JSON; |
21 |
use Mojo::JSON; |
|
|
22 |
use Mojo::JWT; |
23 |
use Digest::MD5 qw( md5_base64 ); |
24 |
use Encode; |
22 |
|
25 |
|
23 |
use C4::Auth qw( haspermission ); |
26 |
use C4::Auth qw( haspermission ); |
24 |
use C4::Context; |
27 |
use C4::Context; |
25 |
use C4::Circulation qw( AddRenewal ); |
28 |
use C4::Circulation qw( CanBookBeIssued AddIssue AddRenewal ); |
26 |
use Koha::Checkouts; |
29 |
use Koha::Checkouts; |
27 |
use Koha::Old::Checkouts; |
30 |
use Koha::Old::Checkouts; |
28 |
|
31 |
|
Lines 99-104
sub get {
Link Here
|
99 |
}; |
102 |
}; |
100 |
} |
103 |
} |
101 |
|
104 |
|
|
|
105 |
=head3 get_availablity |
106 |
|
107 |
Controller function that handles retrieval of Checkout availability |
108 |
|
109 |
=cut |
110 |
|
111 |
sub get_availability { |
112 |
my $c = shift->openapi->valid_input or return; |
113 |
|
114 |
my $patron = Koha::Patrons->find( $c->validation->param('patron_id') ); |
115 |
my $inprocess = 0; # What does this do? |
116 |
my $ignore_reserves = 0; # Don't ignore reserves |
117 |
my $item = Koha::Items->find( $c->validation->param('item_id') ); |
118 |
my $params = { |
119 |
item => $item |
120 |
}; |
121 |
|
122 |
my ( $impossible, $confirmation, $alerts, $messages ) = |
123 |
CanBookBeIssued( $patron, undef, undef, $inprocess, $ignore_reserves, |
124 |
$params ); |
125 |
|
126 |
my $token; |
127 |
if (keys %{$confirmation}) { |
128 |
my $claims = { map { $_ => 1 } keys %{$confirmation} }; |
129 |
my $secret = |
130 |
md5_base64( Encode::encode( 'UTF-8', C4::Context->config('pass') ) ); |
131 |
$token = Mojo::JWT->new( claims => $claims, secret => $secret )->encode; |
132 |
} |
133 |
|
134 |
my $response = { |
135 |
blockers => $impossible, |
136 |
confirms => $confirmation, |
137 |
warnings => { %{$alerts}, %{$messages} }, |
138 |
confirmation_token => $token |
139 |
}; |
140 |
|
141 |
return $c->render( status => 200, openapi => $response ); |
142 |
} |
143 |
|
144 |
=head3 add |
145 |
|
146 |
Add a new checkout |
147 |
|
148 |
=cut |
149 |
|
150 |
sub add { |
151 |
my $c = shift->openapi->valid_input or return; |
152 |
|
153 |
my $params = $c->validation->param('body'); |
154 |
my $item_id = $params->{item_id}; |
155 |
my $patron_id = $params->{patron_id}; |
156 |
my $onsite = $params->{onsite_checkout}; |
157 |
|
158 |
return try { |
159 |
my $item = Koha::Items->find( $item_id ); |
160 |
unless ($item) { |
161 |
return $c->render( |
162 |
status => 409, |
163 |
openapi => { |
164 |
error => 'Item not found', |
165 |
error_code => 'ITEM_NOT_FOUND', |
166 |
} |
167 |
); |
168 |
} |
169 |
|
170 |
my $patron = Koha::Patrons->find( $patron_id ); |
171 |
unless ($patron) { |
172 |
return $c->render( |
173 |
status => 409, |
174 |
openapi => { |
175 |
error => 'Patron not found', |
176 |
error_code => 'PATRON_NOT_FOUND', |
177 |
} |
178 |
); |
179 |
} |
180 |
|
181 |
my $inprocess = 0; # What does this do? |
182 |
my $ignore_reserves = 0; # Don't ignore reserves |
183 |
my $params = { |
184 |
item => $item |
185 |
}; |
186 |
|
187 |
# Call 'CanBookBeIssued' |
188 |
my ( $impossible, $confirmation, $alerts, $messages ) = |
189 |
CanBookBeIssued( $patron, undef, undef, $inprocess, $ignore_reserves, |
190 |
$params ); |
191 |
|
192 |
# * Fail for blockers - render 403 |
193 |
if (keys %{$impossible}) { |
194 |
my @errors = keys %{$impossible}; |
195 |
return $c->render( |
196 |
status => 403, |
197 |
openapi => { error => "Checkout not authorized (@errors)" } |
198 |
); |
199 |
} |
200 |
|
201 |
# * If confirmation required, check variable set above - render 412 if variable is false |
202 |
if (keys %{$confirmation}) { |
203 |
my $confirmed = 0; |
204 |
|
205 |
# Check for existance of confirmation token and if exists check validity |
206 |
if ( my $token = $c->validation->param('confirmation') ) { |
207 |
my $secret = |
208 |
md5_base64( |
209 |
Encode::encode( 'UTF-8', C4::Context->config('pass') ) ); |
210 |
my $claims = try { |
211 |
Mojo::JWT->new( secret => $secret )->decode($token); |
212 |
} catch { |
213 |
return $c->render( |
214 |
status => 403, |
215 |
openapi => |
216 |
{ error => "Confirmation required" } |
217 |
); |
218 |
}; |
219 |
|
220 |
# check claims match |
221 |
my $token_claims = join( / /, sort keys %{$claims} ); |
222 |
my $confirm_keys = join( / /, sort keys %{$confirmation} ); |
223 |
$confirmed = 1 if ( $token_claims eq $confirm_keys ); |
224 |
} |
225 |
|
226 |
unless ($confirmed) { |
227 |
return $c->render( |
228 |
status => 412, |
229 |
openapi => { error => "Confirmation error" } |
230 |
); |
231 |
} |
232 |
} |
233 |
|
234 |
# Call 'AddIssue' |
235 |
my $checkout = AddIssue($patron->unblessed, $item->barcode); |
236 |
|
237 |
# Check return of AddIssue |
238 |
# Get checkout from AddIssue - render 201 |
239 |
# Get error from AddIssue - render 400? |
240 |
|
241 |
$c->res->headers->location( $c->req->url->to_string . '/' . $checkout->id ); |
242 |
return $c->render( |
243 |
status => 201, |
244 |
openapi => $checkout->to_api |
245 |
); |
246 |
} |
247 |
catch { |
248 |
$c->unhandled_exception($_); |
249 |
}; |
250 |
} |
251 |
|
102 |
=head3 get_renewals |
252 |
=head3 get_renewals |
103 |
|
253 |
|
104 |
List Koha::Checkout::Renewals |
254 |
List Koha::Checkout::Renewals |