Bug 31028: Add catalog concern management page to staff
[srvgit] / Koha / REST / V1 / Tickets.pm
1 package Koha::REST::V1::Tickets;
2
3 # This file is part of Koha.
4 #
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.
9 #
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.
14 #
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>.
17
18 use Modern::Perl;
19
20 use Mojo::Base 'Mojolicious::Controller';
21
22 use Koha::Ticket;
23 use Koha::Tickets;
24 use Koha::Ticket::Update;
25 use Koha::Ticket::Updates;
26
27 use Try::Tiny qw( catch try );
28
29 =head1 API
30
31 =head2 Methods
32
33 =head3 list
34
35 =cut
36
37 sub list {
38     my $c = shift->openapi->valid_input or return;
39
40     return try {
41         my $tickets_set = Koha::Tickets->new;
42         my $tickets     = $c->objects->search($tickets_set);
43         return $c->render( status => 200, openapi => $tickets );
44     }
45     catch {
46         $c->unhandled_exception($_);
47     };
48
49 }
50
51 =head3 get
52
53 =cut
54
55 sub get {
56     my $c = shift->openapi->valid_input or return;
57
58     return try {
59         my $ticket = Koha::Tickets->find( $c->validation->param('ticket_id') );
60         unless ($ticket) {
61             return $c->render(
62                 status  => 404,
63                 openapi => { error => "Ticket not found" }
64             );
65         }
66
67         return $c->render( status => 200, openapi => $ticket->to_api );
68     }
69     catch {
70         $c->unhandled_exception($_);
71     }
72 }
73
74 =head3 add
75
76 =cut
77
78 sub add {
79     my $c = shift->openapi->valid_input or return;
80     my $patron = $c->stash('koha.user');
81
82     return try {
83         my $body   = $c->validation->param('body');
84
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
89
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 );
94         return $c->render(
95             status  => 201,
96             openapi => $ticket->to_api
97         );
98     }
99     catch {
100         $c->unhandled_exception($_);
101     };
102 }
103
104 =head3 update
105
106 =cut
107
108 sub update {
109     my $c = shift->openapi->valid_input or return;
110
111     my $ticket = Koha::Tickets->find( $c->validation->param('ticket_id') );
112
113     if ( not defined $ticket ) {
114         return $c->render(
115             status  => 404,
116             openapi => { error => "Object not found" }
117         );
118     }
119
120     return try {
121         $ticket->set_from_api( $c->validation->param('body') );
122         $ticket->store();
123         return $c->render( status => 200, openapi => $ticket->to_api );
124     }
125     catch {
126         $c->unhandled_exception($_);
127     };
128 }
129
130 =head3 delete
131
132 =cut
133
134 sub delete {
135     my $c = shift->openapi->valid_input or return;
136
137     my $ticket = Koha::Tickets->find( $c->validation->param('ticket_id') );
138     if ( not defined $ticket ) {
139         return $c->render(
140             status  => 404,
141             openapi => { error => "Object not found" }
142         );
143     }
144
145     return try {
146         $ticket->delete;
147         return $c->render(
148             status  => 204,
149             openapi => q{}
150         );
151     }
152     catch {
153         $c->unhandled_exception($_);
154     };
155 }
156
157 =head3 list_updates
158
159 =cut
160
161 sub list_updates {
162     my $c = shift->openapi->valid_input or return;
163
164     return try {
165         my $ticket = Koha::Tickets->find( $c->validation->param('ticket_id') );
166         unless ($ticket) {
167             return $c->render(
168                 status  => 404,
169                 openapi => { error => "Ticket not found" }
170             );
171         }
172
173         my $updates_set = $ticket->updates;
174         my $updates     = $c->objects->search($updates_set);
175         return $c->render( status => 200, openapi => $updates );
176     }
177     catch {
178         $c->unhandled_exception($_);
179     };
180 }
181
182 =head3 add_update
183
184 =cut
185
186 sub add_update {
187     my $c = shift->openapi->valid_input or return;
188     my $patron = $c->stash('koha.user');
189
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;
193
194     if ( $ticket_update->{ticket_id} != $ticket_id_param ) {
195         return $c->render(
196             status  => 400,
197             openapi => { error => "Ticket Mismatch" }
198         );
199     }
200
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
205
206     return try {
207         my $state = delete $ticket_update->{state};
208
209         # Store update
210         my $update = Koha::Ticket::Update->new_from_api($ticket_update)->store;
211         $update->discard_changes;
212
213         # Update ticket state if needed
214         if ( defined($state) && $state eq 'resolved' ) {
215             my $ticket = $update->ticket;
216             $ticket->set(
217                 {
218                     resolver_id   => $update->user_id,
219                     resolved_date => $update->date
220                 }
221             )->store;
222         }
223
224         # Optionally add to message_queue here to notify reporter
225         if ( $update->public ) {
226             my $notice =
227               ( defined($state) && $state eq 'resolved' )
228               ? 'TICKET_RESOLVE'
229               : 'TICKET_UPDATE';
230             my $letter = C4::Letters::GetPreparedLetter(
231                 module      => 'catalog',
232                 letter_code => $notice,
233                 branchcode  => $update->user->branchcode,
234                 tables      => { ticket_updates => $update->id }
235             );
236
237             if ($letter) {
238                 my $message_id = C4::Letters::EnqueueLetter(
239                     {
240                         letter                 => $letter,
241                         borrowernumber         => $update->ticket->reporter_id,
242                         message_transport_type => 'email',
243                     }
244                 );
245             }
246         }
247
248         # Return
249         $c->res->headers->location(
250             $c->req->url->to_string . '/' . $update->id );
251         return $c->render(
252             status  => 201,
253             openapi => $update->to_api
254         );
255     }
256     catch {
257         $c->unhandled_exception($_);
258     };
259 }
260
261 1;