Bug 14697: (follow-up) Rely on the UNIQUE constraint and return 409 for issue_id
[srvgit] / Koha / REST / V1 / ReturnClaims.pm
1 package Koha::REST::V1::ReturnClaims;
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 Try::Tiny;
23
24 use Koha::Checkouts::ReturnClaims;
25 use Koha::Checkouts;
26 use Koha::DateUtils qw( dt_from_string output_pref );
27
28 =head1 NAME
29
30 Koha::REST::V1::ReturnClaims
31
32 =head2 Operations
33
34 =head3 claim_returned
35
36 Claim that a checked out item was returned.
37
38 =cut
39
40 sub claim_returned {
41     my $c    = shift->openapi->valid_input or return;
42     my $body = $c->validation->param('body');
43
44     return try {
45         my $itemnumber      = $body->{item_id};
46         my $charge_lost_fee = $body->{charge_lost_fee} ? 1 : 0;
47         my $created_by      = $body->{created_by};
48         my $notes           = $body->{notes};
49
50         my $user = $c->stash('koha.user');
51         $created_by //= $user->borrowernumber;
52
53         my $checkout = Koha::Checkouts->find( { itemnumber => $itemnumber } );
54
55         return $c->render(
56             openapi => { error => "Not found - Checkout not found" },
57             status  => 404
58         ) unless $checkout;
59
60         my $claim = $checkout->claim_returned(
61             {
62                 charge_lost_fee => $charge_lost_fee,
63                 created_by      => $created_by,
64                 notes           => $notes,
65             }
66         );
67
68         $c->res->headers->location($c->req->url->to_string . '/' . $claim->id );
69         return $c->render(
70             status  => 201,
71             openapi => $claim->to_api
72         );
73     }
74     catch {
75         if ( $_->isa('Koha::Exceptions::Object::DuplicateID') ) {
76             return $c->render(
77                 status  => 409,
78                 openapi => { error => "$_" }
79             );
80         }
81         elsif ( $_->isa('Koha::Exceptions::Checkouts::ReturnClaims::NoCreatedBy') ) {
82             return $c->render(
83                 status  => 400,
84                 openapi => { error => "Mandatory attribute created_by missing" }
85             );
86         }
87         else {
88             return $c->render(
89                 status  => 500,
90                 openapi => { error => "Something went wrong, check the logs." }
91             );
92         }
93     };
94 }
95
96 =head3 update_notes
97
98 Update the notes of an existing claim
99
100 =cut
101
102 sub update_notes {
103     my $c     = shift->openapi->valid_input or return;
104     my $input = $c->validation->output;
105     my $body  = $c->validation->param('body');
106
107     return try {
108         my $id         = $input->{claim_id};
109         my $updated_by = $body->{updated_by};
110         my $notes      = $body->{notes};
111
112         $updated_by ||=
113           C4::Context->userenv ? C4::Context->userenv->{number} : undef;
114
115         my $claim = Koha::Checkouts::ReturnClaims->find($id);
116
117         return $c->render(
118             openapi => { error => "Not found - Claim not found" },
119             status  => 404
120         ) unless $claim;
121
122         $claim->set(
123             {
124                 notes      => $notes,
125                 updated_by => $updated_by,
126                 updated_on => dt_from_string(),
127             }
128         );
129         $claim->store();
130
131         my $data = $claim->unblessed;
132
133         my $c_dt = dt_from_string( $data->{created_on} );
134         my $u_dt = dt_from_string( $data->{updated_on} );
135
136         $data->{created_on_formatted} = output_pref( { dt => $c_dt } );
137         $data->{updated_on_formatted} = output_pref( { dt => $u_dt } );
138
139         $data->{created_on} = $c_dt->iso8601;
140         $data->{updated_on} = $u_dt->iso8601;
141
142         return $c->render( openapi => $data, status => 200 );
143     }
144     catch {
145         if ( $_->isa('DBIx::Class::Exception') ) {
146             return $c->render(
147                 status  => 500,
148                 openapi => { error => $_->{msg} }
149             );
150         }
151         else {
152             return $c->render(
153                 status  => 500,
154                 openapi => { error => "Something went wrong, check the logs." }
155             );
156         }
157     };
158 }
159
160 =head3 resolve_claim
161
162 Marks a claim as resolved
163
164 =cut
165
166 sub resolve_claim {
167     my $c     = shift->openapi->valid_input or return;
168     my $input = $c->validation->output;
169     my $body  = $c->validation->param('body');
170
171     return try {
172         my $id          = $input->{claim_id};
173         my $resolved_by = $body->{updated_by};
174         my $resolution  = $body->{resolution};
175
176         $resolved_by ||=
177           C4::Context->userenv ? C4::Context->userenv->{number} : undef;
178
179         my $claim = Koha::Checkouts::ReturnClaims->find($id);
180
181         return $c->render(
182             openapi => { error => "Not found - Claim not found" },
183             status  => 404
184         ) unless $claim;
185
186         $claim->set(
187             {
188                 resolution  => $resolution,
189                 resolved_by => $resolved_by,
190                 resolved_on => dt_from_string(),
191             }
192         );
193         $claim->store();
194
195         return $c->render( openapi => $claim, status => 200 );
196     }
197     catch {
198         if ( $_->isa('DBIx::Class::Exception') ) {
199             return $c->render(
200                 status  => 500,
201                 openapi => { error => $_->{msg} }
202             );
203         }
204         else {
205             return $c->render(
206                 status  => 500,
207                 openapi => { error => "Something went wrong, check the logs." }
208             );
209         }
210     };
211 }
212
213 =head3 delete_claim
214
215 Deletes the claim from the database
216
217 =cut
218
219 sub delete_claim {
220     my $c     = shift->openapi->valid_input or return;
221     my $input = $c->validation->output;
222
223     return try {
224         my $id = $input->{claim_id};
225
226         my $claim = Koha::Checkouts::ReturnClaims->find($id);
227
228         return $c->render(
229             openapi => { error => "Not found - Claim not found" },
230             status  => 404
231         ) unless $claim;
232
233         $claim->delete();
234
235         return $c->render( openapi => $claim, status => 200 );
236     }
237     catch {
238         if ( $_->isa('DBIx::Class::Exception') ) {
239             return $c->render(
240                 status  => 500,
241                 openapi => { error => $_->{msg} }
242             );
243         }
244         else {
245             return $c->render(
246                 status  => 500,
247                 openapi => { error => "Something went wrong, check the logs." }
248             );
249         }
250     };
251 }
252
253 1;