1 package Koha::REST::V1::Tickets;
3 # This file is part of Koha.
5 # Koha is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
20 use Mojo::Base 'Mojolicious::Controller';
24 use Koha::Ticket::Update;
25 use Koha::Ticket::Updates;
27 use Try::Tiny qw( catch try );
38 my $c = shift->openapi->valid_input or return;
41 my $tickets = $c->objects->search(Koha::Tickets->new);
42 return $c->render( status => 200, openapi => $tickets );
45 $c->unhandled_exception($_);
55 my $c = shift->openapi->valid_input or return;
58 my $ticket = Koha::Tickets->find( $c->param('ticket_id') );
62 openapi => { error => "Ticket not found" }
66 return $c->render( status => 200, openapi => $ticket->to_api );
69 $c->unhandled_exception($_);
78 my $c = shift->openapi->valid_input or return;
79 my $patron = $c->stash('koha.user');
82 my $body = $c->req->json;
84 # Set reporter from session
85 $body->{reporter_id} = $patron->id;
86 # FIXME: We should allow impersonation at a later date to
87 # allow an API user to submit on behalf of a user
89 my $ticket = Koha::Ticket->new_from_api($body)->store;
90 $ticket->discard_changes;
91 $c->res->headers->location(
92 $c->req->url->to_string . '/' . $ticket->id );
95 openapi => $ticket->to_api
99 $c->unhandled_exception($_);
108 my $c = shift->openapi->valid_input or return;
110 my $ticket = Koha::Tickets->find( $c->param('ticket_id') );
112 if ( not defined $ticket ) {
115 openapi => { error => "Object not found" }
120 $ticket->set_from_api( $c->req->json );
122 return $c->render( status => 200, openapi => $ticket->to_api );
125 $c->unhandled_exception($_);
134 my $c = shift->openapi->valid_input or return;
136 my $ticket = Koha::Tickets->find( $c->param('ticket_id') );
137 if ( not defined $ticket ) {
140 openapi => { error => "Object not found" }
152 $c->unhandled_exception($_);
161 my $c = shift->openapi->valid_input or return;
164 my $ticket = Koha::Tickets->find( $c->param('ticket_id') );
168 openapi => { error => "Ticket not found" }
172 my $updates_set = $ticket->updates;
173 my $updates = $c->objects->search($updates_set);
174 return $c->render( status => 200, openapi => $updates );
177 $c->unhandled_exception($_);
186 my $c = shift->openapi->valid_input or return;
187 my $patron = $c->stash('koha.user');
189 my $ticket_id_param = $c->param('ticket_id');
190 my $ticket_update = $c->req->json;
191 $ticket_update->{ticket_id} //= $ticket_id_param;
193 if ( $ticket_update->{ticket_id} != $ticket_id_param ) {
196 openapi => { error => "Ticket Mismatch" }
200 # Set reporter from session
201 $ticket_update->{user_id} = $patron->id;
202 # FIXME: We should allow impersonation at a later date to
203 # allow an API user to submit on behalf of a user
206 my $state = delete $ticket_update->{state};
209 my $update = Koha::Ticket::Update->new_from_api($ticket_update)->store;
210 $update->discard_changes;
212 # Update ticket state if needed
213 if ( defined($state) && $state eq 'resolved' ) {
214 my $ticket = $update->ticket;
217 resolver_id => $update->user_id,
218 resolved_date => $update->date
223 # Optionally add to message_queue here to notify reporter
224 if ( $update->public ) {
226 ( defined($state) && $state eq 'resolved' )
229 my $letter = C4::Letters::GetPreparedLetter(
230 module => 'catalogue',
231 letter_code => $notice,
232 branchcode => $update->user->branchcode,
233 tables => { ticket_updates => $update->id }
237 my $message_id = C4::Letters::EnqueueLetter(
240 borrowernumber => $update->ticket->reporter_id,
241 message_transport_type => 'email',
248 $c->res->headers->location(
249 $c->req->url->to_string . '/' . $update->id );
252 openapi => $update->to_api
256 $c->unhandled_exception($_);