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_set = Koha::Tickets->new;
42 my $tickets = $c->objects->search($tickets_set);
43 return $c->render( status => 200, openapi => $tickets );
46 $c->unhandled_exception($_);
56 my $c = shift->openapi->valid_input or return;
59 my $ticket = Koha::Tickets->find( $c->validation->param('ticket_id') );
63 openapi => { error => "Ticket not found" }
67 return $c->render( status => 200, openapi => $ticket->to_api );
70 $c->unhandled_exception($_);
79 my $c = shift->openapi->valid_input or return;
80 my $patron = $c->stash('koha.user');
83 my $body = $c->validation->param('body');
85 # Set reporter from session
86 $body->{reporter_id} = $patron->id;
87 # FIXME: We should allow impersonation at a later date to
88 # allow an API user to submit on behalf of a user
90 my $ticket = Koha::Ticket->new_from_api($body)->store;
91 $ticket->discard_changes;
92 $c->res->headers->location(
93 $c->req->url->to_string . '/' . $ticket->id );
96 openapi => $ticket->to_api
100 $c->unhandled_exception($_);
109 my $c = shift->openapi->valid_input or return;
111 my $ticket = Koha::Tickets->find( $c->validation->param('ticket_id') );
113 if ( not defined $ticket ) {
116 openapi => { error => "Object not found" }
121 $ticket->set_from_api( $c->validation->param('body') );
123 return $c->render( status => 200, openapi => $ticket->to_api );
126 $c->unhandled_exception($_);
135 my $c = shift->openapi->valid_input or return;
137 my $ticket = Koha::Tickets->find( $c->validation->param('ticket_id') );
138 if ( not defined $ticket ) {
141 openapi => { error => "Object not found" }
153 $c->unhandled_exception($_);
162 my $c = shift->openapi->valid_input or return;
165 my $ticket = Koha::Tickets->find( $c->validation->param('ticket_id') );
169 openapi => { error => "Ticket not found" }
173 my $updates_set = $ticket->updates;
174 my $updates = $c->objects->search($updates_set);
175 return $c->render( status => 200, openapi => $updates );
178 $c->unhandled_exception($_);
187 my $c = shift->openapi->valid_input or return;
188 my $patron = $c->stash('koha.user');
190 my $ticket_id_param = $c->validation->param('ticket_id');
191 my $ticket_update = $c->validation->param('body');
192 $ticket_update->{ticket_id} //= $ticket_id_param;
194 if ( $ticket_update->{ticket_id} != $ticket_id_param ) {
197 openapi => { error => "Ticket Mismatch" }
201 # Set reporter from session
202 $ticket_update->{user_id} = $patron->id;
203 # FIXME: We should allow impersonation at a later date to
204 # allow an API user to submit on behalf of a user
207 my $state = delete $ticket_update->{state};
210 my $update = Koha::Ticket::Update->new_from_api($ticket_update)->store;
211 $update->discard_changes;
213 # Update ticket state if needed
214 if ( defined($state) && $state eq 'resolved' ) {
215 my $ticket = $update->ticket;
218 resolver_id => $update->user_id,
219 resolved_date => $update->date
224 # Optionally add to message_queue here to notify reporter
225 if ( $update->public ) {
227 ( defined($state) && $state eq 'resolved' )
230 my $letter = C4::Letters::GetPreparedLetter(
232 letter_code => $notice,
233 branchcode => $update->user->branchcode,
234 tables => { ticket_updates => $update->id }
238 my $message_id = C4::Letters::EnqueueLetter(
241 borrowernumber => $update->ticket->reporter_id,
242 message_transport_type => 'email',
249 $c->res->headers->location(
250 $c->req->url->to_string . '/' . $update->id );
253 openapi => $update->to_api
257 $c->unhandled_exception($_);