Commit e5299844 authored by Petter Goksøyr Åsen's avatar Petter Goksøyr Åsen Committed by Petter Goksøyr Åsen
Browse files

DEICH-5941 add endpoint to search patrons by birthdate

Limited to patrons below upper age limit on children (<15 year olds)
parent 4918e70f
......@@ -97,6 +97,33 @@ sub Find {
return $self;
}
sub FindByBirthdate {
my ($self, $birthdate) = @_;
my $dbh = $self->dbh;
my $q = "
WITH t AS (SELECT upperagelimit from categories where categorycode = 'B')
SELECT
dateofbirth,
TIMESTAMPDIFF(YEAR, dateofbirth, CURDATE()) AS age,
CONCAT(surname, ', ', firstname) AS name,
categorycode
FROM borrowers, t
WHERE
deleted_at IS NULL AND
dateofbirth = ? AND
TIMESTAMPDIFF(YEAR, dateofbirth, CURDATE()) < t.upperagelimit";
my $sth = $dbh->prepare($q);
warn "a";
$sth->execute($birthdate) or Deichman::Exception::Patron->throw($dbh->errstr);
warn "b";
my @patrons;
while (my $p = $sth->fetchrow_hashref) {
push @patrons, $p;
}
$self->{patrons} = \@patrons;
return $self;
}
sub GetByCardNumber {
my ($self, $cardnumber) = @_;
$cardnumber or Deichman::Exception::Patron::NotFound->throw();
......
......@@ -219,6 +219,7 @@ sub PermissionsEnabledPath {
["/purresak/", ["superlibrarian","borrowers"]],
["/reserves/", ["superlibrarian","borrowers"]],
["/reports", ["superlibrarian","circulate"]],
["/patronsearch", ["superlibrarian", "borrowers"]],
);
foreach my $p (@paths) {
if ($path and substr($path, 0, length($p->[0])) eq $p->[0]) {
......
......@@ -108,6 +108,7 @@ mount "RESTAPI::Patron::PasswordRecovery";
mount "RESTAPI::Patron::Payment";
mount "RESTAPI::Patron::Reserve";
mount "RESTAPI::Patron::ValidatePIN";
mount "RESTAPI::Patron::Search";
mount "RESTAPI::PickList";
mount "RESTAPI::PrintSlip";
mount "RESTAPI::Reports";
......
package RESTAPI::Patron::Search;
use strict;
use warnings;
use Raisin::API;
use HTTP::Status qw(:constants);
use Types::Standard qw(HashRef Any Int Str);
use Try::Tiny;
use Deichman::Patron;
use Deichman::Exception;
desc "Patron search";
resource patronsearch => sub {
resource findbybirthdate => sub {
params(
requires("birthdate", type => Str, desc => "patron birthdate in the format: %YYYY-%mm-%dd"),
);
post sub {
warn "yo der";
my $params = shift;
my $p = Deichman::Patron->new()->FindByBirthdate($params->{birthdate});
try {
{ patrons => $p->{patrons} }
} catch {
app->logg(error => $_->description);
res->status(HTTP_INTERNAL_SERVER_ERROR);
};
};
};
};
1;
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment