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