Lines 23-28
use Koha::Patrons;
Link Here
|
23 |
use Koha::Patron::Categories; |
23 |
use Koha::Patron::Categories; |
24 |
use Koha::Libraries; |
24 |
use Koha::Libraries; |
25 |
|
25 |
|
|
|
26 |
use Scalar::Util qw(blessed); |
27 |
use Try::Tiny; |
28 |
|
26 |
sub list { |
29 |
sub list { |
27 |
my ($c, $args, $cb) = @_; |
30 |
my ($c, $args, $cb) = @_; |
28 |
|
31 |
|
Lines 55-158
sub get {
Link Here
|
55 |
sub add { |
58 |
sub add { |
56 |
my ($c, $args, $cb) = @_; |
59 |
my ($c, $args, $cb) = @_; |
57 |
|
60 |
|
58 |
my $body = $c->req->json; |
61 |
try { |
|
|
62 |
my $body = $c->req->json; |
59 |
|
63 |
|
60 |
# patron cardnumber and/or userid unique? |
64 |
if ($body->{password}) { $body->{password} = hash_password($body->{password}) }; # bcrypt password if given |
61 |
if ($body->{cardnumber} || $body->{userid}) { |
|
|
62 |
my $patron = Koha::Patrons->find({cardnumber => $body->{cardnumber}, userid => $body->{userid} }); |
63 |
if ($patron) { |
64 |
return $c->$cb({ |
65 |
error => "Patron cardnumber and userid must be unique", |
66 |
conflict => { cardnumber => $patron->cardnumber, userid => $patron->userid } |
67 |
}, 409); |
68 |
} |
69 |
} |
70 |
|
65 |
|
71 |
my $branch = Koha::Libraries->find({branchcode => $body->{branchcode} }); |
66 |
my $patron = Koha::Patron->new($body)->validate->store; |
72 |
unless ($branch) { |
67 |
return $c->$cb($patron->unblessed, 201); |
73 |
return $c->$cb({error => "Library with branchcode \"" . $body->{branchcode} . "\" does not exist"}, 404); |
|
|
74 |
} |
68 |
} |
75 |
my $category = Koha::Patron::Categories->find({ categorycode => $body->{categorycode} }); |
69 |
catch { |
76 |
unless ($category) { |
70 |
unless (blessed $_ && $_->can('rethrow')) { |
77 |
return $c->$cb({error => "Patron category \"" . $body->{categorycode} . "\" does not exist"}, 404); |
71 |
return $c->$cb({error => "Something went wrong, check Koha logs for details."}, 500); |
78 |
} |
72 |
} |
79 |
# All OK - save new patron |
73 |
if ($_->isa('Koha::Exceptions::Patron::DuplicateObject')) { |
80 |
|
74 |
return $c->$cb({ error => $_->error, conflict => $_->conflict }, 409); |
81 |
if ($body->{password}) { $body->{password} = hash_password($body->{password}) }; # bcrypt password if given |
75 |
} |
82 |
|
76 |
elsif ($_->isa('Koha::Exceptions::Library::BranchcodeNotFound')) { |
83 |
my $patron = eval { |
77 |
return $c->$cb({ error => "Library with branchcode \"".$_->branchcode."\" does not exist" }, 404); |
84 |
Koha::Patron->new($body)->store; |
78 |
} |
|
|
79 |
elsif ($_->isa('Koha::Exceptions::Category::CategorycodeNotFound')) { |
80 |
return $c->$cb({error => "Patron category \"".$_->categorycode."\" does not exist"}, 404); |
81 |
} |
82 |
else { |
83 |
return $c->$cb({error => "Something went wrong, check Koha logs for details."}, 500); |
84 |
} |
85 |
}; |
85 |
}; |
86 |
|
|
|
87 |
unless ($patron) { |
88 |
return $c->$cb({error => "Something went wrong, check Koha logs for details"}, 500); |
89 |
} |
90 |
|
91 |
return $c->$cb($patron->unblessed, 201); |
92 |
} |
86 |
} |
93 |
|
87 |
|
94 |
sub edit { |
88 |
sub edit { |
95 |
my ($c, $args, $cb) = @_; |
89 |
my ($c, $args, $cb) = @_; |
96 |
|
90 |
|
97 |
my $patron = Koha::Patrons->find($args->{borrowernumber}); |
91 |
my $patron; |
98 |
unless ($patron) { |
92 |
try { |
99 |
return $c->$cb({error => "Patron not found"}, 404); |
93 |
$patron = Koha::Patrons->find($args->{borrowernumber}); |
100 |
} |
94 |
my $body = $c->req->json; |
101 |
|
95 |
|
102 |
my $body = $c->req->json; |
96 |
if ($body->{password}) { $body->{password} = hash_password($body->{password}) }; # bcrypt password if given |
103 |
|
|
|
104 |
# Can we change userid and/or cardnumber? in that case check that they are altered first |
105 |
if ($body->{cardnumber} || $body->{userid}) { |
106 |
if ( ($body->{cardnumber} && $body->{cardnumber} ne $patron->cardnumber) || ($body->{userid} && $body->{userid} ne $patron->userid) ) { |
107 |
my $conflictingPatron = Koha::Patrons->find({cardnumber => $body->{cardnumber}, userid => $body->{userid} }); |
108 |
if ($conflictingPatron) { |
109 |
return $c->$cb({ |
110 |
error => "Patron cardnumber and userid must be unique", |
111 |
conflict => { cardnumber => $conflictingPatron->cardnumber, userid => $conflictingPatron->userid } |
112 |
}, 409); |
113 |
} |
114 |
} |
115 |
} |
116 |
|
97 |
|
117 |
if ($body->{branchcode}) { |
98 |
die unless $patron->set($body)->validate; |
118 |
my $branch = Koha::Libraries->find({branchcode => $body->{branchcode} }); |
|
|
119 |
unless ($branch) { |
120 |
return $c->$cb({error => "Library with branchcode \"" . $body->{branchcode} . "\" does not exist"}, 404); |
121 |
} |
122 |
} |
123 |
|
99 |
|
124 |
if ($body->{categorycode}) { |
100 |
return $c->$cb({}, 204) unless $patron->is_changed; # No Content = No changes made |
125 |
my $category = Koha::Patron::Categories->find({ categorycode => $body->{categorycode} }); |
101 |
$patron->store; |
126 |
unless ($category) { |
102 |
return $c->$cb($patron->unblessed, 200); |
127 |
return $c->$cb({error => "Patron category \"" . $body->{categorycode} . "\" does not exist"}, 404); |
|
|
128 |
} |
129 |
} |
103 |
} |
130 |
# ALL OK - Update patron |
104 |
catch { |
131 |
# Perhaps limit/validate what should be updated here? flags, et.al. |
105 |
unless ($patron) { |
132 |
if ($body->{password}) { $body->{password} = hash_password($body->{password}) }; # bcrypt password if given |
106 |
return $c->$cb({error => "Patron not found"}, 404); |
133 |
|
|
|
134 |
my $updatedpatron = eval { |
135 |
$patron->set($body); |
136 |
}; |
137 |
|
138 |
if ($updatedpatron) { |
139 |
if ($updatedpatron->is_changed) { |
140 |
|
141 |
my $res = eval { |
142 |
$updatedpatron->store; |
143 |
}; |
144 |
|
145 |
unless ($res) { |
146 |
return $c->$cb({error => "Something went wrong, check Koha logs for details"}, 500); |
147 |
} |
148 |
return $c->$cb($res->unblessed, 200); |
149 |
|
150 |
} else { |
151 |
return $c->$cb({}, 204); # No Content = No changes made |
152 |
} |
107 |
} |
153 |
} else { |
108 |
unless (blessed $_ && $_->can('rethrow')) { |
154 |
return $c->$cb({error => "Something went wrong, check Koha logs for details"}, 500); |
109 |
return $c->$cb({error => "Something went wrong, check Koha logs for details."}, 500); |
155 |
} |
110 |
} |
|
|
111 |
if ($_->isa('Koha::Exceptions::Patron::DuplicateObject')) { |
112 |
return $c->$cb({ error => $_->error, conflict => $_->conflict }, 409); |
113 |
} |
114 |
elsif ($_->isa('Koha::Exceptions::Library::BranchcodeNotFound')) { |
115 |
return $c->$cb({ error => "Library with branchcode \"".$_->branchcode."\" does not exist" }, 404); |
116 |
} |
117 |
elsif ($_->isa('Koha::Exceptions::Category::CategorycodeNotFound')) { |
118 |
return $c->$cb({error => "Patron category \"".$_->categorycode."\" does not exist"}, 404); |
119 |
} |
120 |
else { |
121 |
return $c->$cb({error => "Something went wrong, check Koha logs for details."}, 500); |
122 |
} |
123 |
}; |
156 |
} |
124 |
} |
157 |
|
125 |
|
158 |
sub delete { |
126 |
sub delete { |