Skip to content

Commit

Permalink
Bug 14697: Use try/catch blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
kylemhall authored and tomascohen committed Oct 22, 2019
1 parent 1d0160e commit 0edfe37
Showing 1 changed file with 163 additions and 97 deletions.
260 changes: 163 additions & 97 deletions Koha/REST/V1/ReturnClaims.pm
Expand Up @@ -19,6 +19,8 @@ use Modern::Perl;

use Mojo::Base 'Mojolicious::Controller';

use Try::Tiny;

use Koha::Checkouts::ReturnClaims;
use Koha::Checkouts;
use Koha::DateUtils qw( dt_from_string output_pref );
Expand All @@ -36,40 +38,56 @@ Claim that a checked out item was returned.
=cut

sub claim_returned {
my $c = shift->openapi->valid_input or return;
my $body = $c->validation->param('body');

my $itemnumber = $body->{item_id};
my $charge_lost_fee = $body->{charge_lost_fee} ? 1 : 0;
my $created_by = $body->{created_by};
my $notes = $body->{notes};

my $checkout = Koha::Checkouts->find( { itemnumber => $itemnumber } );

return $c->render(
openapi => { error => "Not found - Checkout not found" },
status => 404
) unless $checkout;

my $claim = Koha::Checkouts::ReturnClaims->find(
{
issue_id => $checkout->id
my $c = shift->openapi->valid_input or return;
my $body = $c->validation->param('body');

return try {
my $itemnumber = $body->{item_id};
my $charge_lost_fee = $body->{charge_lost_fee} ? 1 : 0;
my $created_by = $body->{created_by};
my $notes = $body->{notes};

my $checkout = Koha::Checkouts->find( { itemnumber => $itemnumber } );

return $c->render(
openapi => { error => "Not found - Checkout not found" },
status => 404
) unless $checkout;

my $claim = Koha::Checkouts::ReturnClaims->find(
{
issue_id => $checkout->id
}
);
return $c->render(
openapi => { error => "Bad request - claim exists" },
status => 400
) if $claim;

$claim = $checkout->claim_returned(
{
charge_lost_fee => $charge_lost_fee,
created_by => $created_by,
notes => $notes,
}
);

return $c->render( openapi => $claim, status => 200 );
}
catch {
if ( $_->isa('DBIx::Class::Exception') ) {
return $c->render(
status => 500,
openapi => { error => $_->{msg} }
);
}
);
return $c->render(
openapi => { error => "Bad request - claim exists" },
status => 400
) if $claim;

$claim = $checkout->claim_returned(
{
charge_lost_fee => $charge_lost_fee,
created_by => $created_by,
notes => $notes,
else {
return $c->render(
status => 500,
openapi => { error => "Something went wrong, check the logs." }
);
}
);

return $c->render( openapi => $claim, status => 200 );
};
}

=head3 update_notes
Expand All @@ -83,41 +101,57 @@ sub update_notes {
my $input = $c->validation->output;
my $body = $c->validation->param('body');

my $id = $input->{claim_id};
my $updated_by = $body->{updated_by};
my $notes = $body->{notes};

$updated_by ||=
C4::Context->userenv ? C4::Context->userenv->{number} : undef;

my $claim = Koha::Checkouts::ReturnClaims->find($id);

return $c->render(
openapi => { error => "Not found - Claim not found" },
status => 404
) unless $claim;

$claim->set(
{
notes => $notes,
updated_by => $updated_by,
updated_on => dt_from_string(),
return try {
my $id = $input->{claim_id};
my $updated_by = $body->{updated_by};
my $notes = $body->{notes};

$updated_by ||=
C4::Context->userenv ? C4::Context->userenv->{number} : undef;

my $claim = Koha::Checkouts::ReturnClaims->find($id);

return $c->render(
openapi => { error => "Not found - Claim not found" },
status => 404
) unless $claim;

$claim->set(
{
notes => $notes,
updated_by => $updated_by,
updated_on => dt_from_string(),
}
);
$claim->store();

my $data = $claim->unblessed;

my $c_dt = dt_from_string( $data->{created_on} );
my $u_dt = dt_from_string( $data->{updated_on} );

$data->{created_on_formatted} = output_pref( { dt => $c_dt } );
$data->{updated_on_formatted} = output_pref( { dt => $u_dt } );

$data->{created_on} = $c_dt->iso8601;
$data->{updated_on} = $u_dt->iso8601;

return $c->render( openapi => $data, status => 200 );
}
catch {
if ( $_->isa('DBIx::Class::Exception') ) {
return $c->render(
status => 500,
openapi => { error => $_->{msg} }
);
}
);
$claim->store();

my $data = $claim->unblessed;

my $c_dt = dt_from_string( $data->{created_on} );
my $u_dt = dt_from_string( $data->{updated_on} );

$data->{created_on_formatted} = output_pref( { dt => $c_dt } );
$data->{updated_on_formatted} = output_pref( { dt => $u_dt } );

$data->{created_on} = $c_dt->iso8601;
$data->{updated_on} = $u_dt->iso8601;

return $c->render( openapi => $data, status => 200 );
else {
return $c->render(
status => 500,
openapi => { error => "Something went wrong, check the logs." }
);
}
};
}

=head3 resolve_claim
Expand All @@ -131,30 +165,46 @@ sub resolve_claim {
my $input = $c->validation->output;
my $body = $c->validation->param('body');

my $id = $input->{claim_id};
my $resolved_by = $body->{updated_by};
my $resolution = $body->{resolution};

$resolved_by ||=
C4::Context->userenv ? C4::Context->userenv->{number} : undef;

my $claim = Koha::Checkouts::ReturnClaims->find($id);

return $c->render(
openapi => { error => "Not found - Claim not found" },
status => 404
) unless $claim;

$claim->set(
{
resolution => $resolution,
resolved_by => $resolved_by,
resolved_on => dt_from_string(),
return try {
my $id = $input->{claim_id};
my $resolved_by = $body->{updated_by};
my $resolution = $body->{resolution};

$resolved_by ||=
C4::Context->userenv ? C4::Context->userenv->{number} : undef;

my $claim = Koha::Checkouts::ReturnClaims->find($id);

return $c->render(
openapi => { error => "Not found - Claim not found" },
status => 404
) unless $claim;

$claim->set(
{
resolution => $resolution,
resolved_by => $resolved_by,
resolved_on => dt_from_string(),
}
);
$claim->store();

return $c->render( openapi => $claim, status => 200 );
}
catch {
if ( $_->isa('DBIx::Class::Exception') ) {
return $c->render(
status => 500,
openapi => { error => $_->{msg} }
);
}
);
$claim->store();

return $c->render( openapi => $claim, status => 200 );
else {
return $c->render(
status => 500,
openapi => { error => "Something went wrong, check the logs." }
);
}
};
}

=head3 delete_claim
Expand All @@ -167,18 +217,34 @@ sub delete_claim {
my $c = shift->openapi->valid_input or return;
my $input = $c->validation->output;

my $id = $input->{claim_id};
return try {
my $id = $input->{claim_id};

my $claim = Koha::Checkouts::ReturnClaims->find($id);
my $claim = Koha::Checkouts::ReturnClaims->find($id);

return $c->render(
openapi => { error => "Not found - Claim not found" },
status => 404
) unless $claim;
return $c->render(
openapi => { error => "Not found - Claim not found" },
status => 404
) unless $claim;

$claim->delete();
$claim->delete();

return $c->render( openapi => $claim, status => 200 );
return $c->render( openapi => $claim, status => 200 );
}
catch {
if ( $_->isa('DBIx::Class::Exception') ) {
return $c->render(
status => 500,
openapi => { error => $_->{msg} }
);
}
else {
return $c->render(
status => 500,
openapi => { error => "Something went wrong, check the logs." }
);
}
};
}

1;

0 comments on commit 0edfe37

Please sign in to comment.