Bug 16330: (QA follow-up) ModMember requires borrowernumber
[koha.git] / Koha / REST / V1 / Patrons.pm
1 package Koha::REST::V1::Patrons;
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 C4::Members qw( AddMember ModMember );
23 use Koha::Patrons;
24
25 use Scalar::Util qw(blessed);
26 use Try::Tiny;
27
28 =head1 NAME
29
30 Koha::REST::V1::Patrons
31
32 =head1 API
33
34 =head2 Methods
35
36 =head3 list
37
38 Controller function that handles listing Koha::Patron objects
39
40 =cut
41
42 sub list {
43     my $c = shift->openapi->valid_input or return;
44
45     return try {
46         my $patrons_set = Koha::Patrons->new;
47         my @patrons = $c->objects->search( $patrons_set )->as_list;
48         return $c->render( status => 200, openapi => \@patrons );
49     }
50     catch {
51         if ( $_->isa('DBIx::Class::Exception') ) {
52             return $c->render( status => 500,
53                 openapi => { error => $_->{msg} } );
54         }
55         else {
56             return $c->render(
57                 status  => 500,
58                 openapi => { error => "Something went wrong, check the logs." }
59             );
60         }
61     };
62 }
63
64 =head3 get
65
66 Controller function that handles retrieving a single Koha::Patron object
67
68 =cut
69
70 sub get {
71     my $c = shift->openapi->valid_input or return;
72
73     my $borrowernumber = $c->validation->param('borrowernumber');
74     my $patron = Koha::Patrons->find($borrowernumber);
75
76     unless ($patron) {
77         return $c->render(status => 404, openapi => { error => "Patron not found." });
78     }
79
80     return $c->render(status => 200, openapi => $patron);
81 }
82
83 =head3 add
84
85 Controller function that handles adding a new Koha::Patron object
86
87 =cut
88
89 sub add {
90     my $c = shift->openapi->valid_input or return;
91
92     return try {
93         my $body = $c->validation->param('body');
94
95         Koha::Patron->new($body)->_validate;
96
97         # TODO: Use AddMember until it has been moved to Koha-namespace
98         my $borrowernumber = AddMember(%$body);
99         my $patron         = Koha::Patrons->find($borrowernumber);
100
101         return $c->render( status => 201, openapi => $patron );
102     }
103     catch {
104         unless ( blessed $_ && $_->can('rethrow') ) {
105             return $c->render(
106                 status  => 500,
107                 openapi => {
108                     error => "Something went wrong, check Koha logs for details."
109                 }
110             );
111         }
112         if ( $_->isa('Koha::Exceptions::Patron::DuplicateObject') ) {
113             return $c->render(
114                 status  => 409,
115                 openapi => { error => $_->error, conflict => $_->conflict }
116             );
117         }
118         elsif ( $_->isa('Koha::Exceptions::Library::BranchcodeNotFound') ) {
119             return $c->render(
120                 status  => 400,
121                 openapi => { error => "Given branchcode does not exist" }
122             );
123         }
124         elsif ( $_->isa('Koha::Exceptions::Category::CategorycodeNotFound') ) {
125             return $c->render(
126                 status  => 400,
127                 openapi => { error => "Given categorycode does not exist" }
128             );
129         }
130         else {
131             return $c->render(
132                 status  => 500,
133                 openapi => {
134                     error => "Something went wrong, check Koha logs for details."
135                 }
136             );
137         }
138     };
139 }
140
141 =head3 update
142
143 Controller function that handles updating a Koha::Patron object
144
145 =cut
146
147 sub update {
148     my $c = shift->openapi->valid_input or return;
149
150     my $patron = Koha::Patrons->find( $c->validation->param('borrowernumber') );
151
152     return try {
153         my $body = $c->validation->param('body');
154
155         $patron->set( _to_model($body) )->_validate;
156
157         ## TODO: Use ModMember until it has been moved to Koha-namespace
158         # Add borrowernumber to $body, as required by ModMember
159         $body->{borrowernumber} = $patron->borrowernumber;
160
161         if ( ModMember(%$body) ) {
162             return $c->render( status => 200, openapi => $patron );
163         }
164         else {
165             return $c->render(
166                 status  => 500,
167                 openapi => {
168                     error => 'Something went wrong, check Koha logs for details.'
169                 }
170             );
171         }
172     }
173     catch {
174         unless ($patron) {
175             return $c->render(
176                 status  => 404,
177                 openapi => { error => "Patron not found" }
178             );
179         }
180         unless ( blessed $_ && $_->can('rethrow') ) {
181             return $c->render(
182                 status  => 500,
183                 openapi => {
184                     error => "Something went wrong, check Koha logs for details."
185                 }
186             );
187         }
188         if ( $_->isa('Koha::Exceptions::Patron::DuplicateObject') ) {
189             return $c->render(
190                 status  => 409,
191                 openapi => { error => $_->error, conflict => $_->conflict }
192             );
193         }
194         elsif ( $_->isa('Koha::Exceptions::Library::BranchcodeNotFound') ) {
195             return $c->render(
196                 status  => 400,
197                 openapi => { error => "Given branchcode does not exist" }
198             );
199         }
200         elsif ( $_->isa('Koha::Exceptions::Category::CategorycodeNotFound') ) {
201             return $c->render(
202                 status  => 400,
203                 openapi => { error => "Given categorycode does not exist" }
204             );
205         }
206         elsif ( $_->isa('Koha::Exceptions::MissingParameter') ) {
207             return $c->render(
208                 status  => 400,
209                 openapi => {
210                     error      => "Missing mandatory parameter(s)",
211                     parameters => $_->parameter
212                 }
213             );
214         }
215         elsif ( $_->isa('Koha::Exceptions::BadParameter') ) {
216             return $c->render(
217                 status  => 400,
218                 openapi => {
219                     error      => "Invalid parameter(s)",
220                     parameters => $_->parameter
221                 }
222             );
223         }
224         elsif ( $_->isa('Koha::Exceptions::NoChanges') ) {
225             return $c->render(
226                 status  => 204,
227                 openapi => { error => "No changes have been made" }
228             );
229         }
230         else {
231             return $c->render(
232                 status  => 500,
233                 openapi => {
234                     error =>
235                       "Something went wrong, check Koha logs for details."
236                 }
237             );
238         }
239     };
240 }
241
242 =head3 delete
243
244 Controller function that handles deleting a Koha::Patron object
245
246 =cut
247
248 sub delete {
249     my $c = shift->openapi->valid_input or return;
250
251     my $patron;
252
253     return try {
254         $patron = Koha::Patrons->find( $c->validation->param('borrowernumber') );
255
256         # check if loans, reservations, debarrment, etc. before deletion!
257         my $res = $patron->delete;
258         return $c->render( status => 200, openapi => {} );
259     }
260     catch {
261         unless ($patron) {
262             return $c->render(
263                 status  => 404,
264                 openapi => { error => "Patron not found" }
265             );
266         }
267         else {
268             return $c->render(
269                 status  => 500,
270                 openapi => {
271                     error =>
272                       "Something went wrong, check Koha logs for details."
273                 }
274             );
275         }
276     };
277 }
278
279 =head3 _to_model
280
281 Helper function that maps REST api objects into Koha::Patron
282 attribute names.
283
284 =cut
285
286 sub _to_model {
287     my $params = shift;
288
289     $params->{lost} = ($params->{lost}) ? 1 : 0;
290     $params->{gonenoaddress} = ($params->{gonenoaddress}) ? 1 : 0;
291
292     return $params;
293 }
294
295 1;