=head3 getPrefix
my $prefix = $abstract->getPrefix( {
- brw_cat => $brw_cat,
- branch => $branch_code,
+ branch => $branch_code
} );
Return the ILL prefix as defined by our $params: either per borrower category,
sub getPrefix {
my ( $self, $params ) = @_;
- my $brn_prefixes = $self->_config->getPrefixes('branch');
- my $brw_prefixes = $self->_config->getPrefixes('brw_cat');
-
- return $brw_prefixes->{$params->{brw_cat}}
- || $brn_prefixes->{$params->{branch}}
- || $brw_prefixes->{default}
- || ""; # "the empty prefix"
+ my $brn_prefixes = $self->_config->getPrefixes();
+ return $brn_prefixes->{$params->{branch}} || ""; # "the empty prefix"
}
=head3 get_type
sub id_prefix {
my ( $self ) = @_;
- my $brw = $self->patron;
- my $brw_cat = "dummy";
- $brw_cat = $brw->categorycode
- unless ( 'HASH' eq ref($brw) && $brw->{deleted} );
my $prefix = $self->getPrefix( {
- brw_cat => $brw_cat,
branch => $self->branchcode,
} );
$prefix .= "-" if ( $prefix );
Overloaded I<TO_JSON> method that takes care of inserting calculated values
into the unblessed representation of the object.
+TODO: This method does nothing and is not called anywhere. However, bug 74325
+touches it, so keeping this for now until both this and bug 74325 are merged,
+at which point we can sort it out and remove it completely
+
=cut
sub TO_JSON {
use Mojo::Base 'Mojolicious::Controller';
use Koha::Illrequests;
+use Koha::Illrequestattributes;
+use Koha::Libraries;
+use Koha::Patrons;
use Koha::Libraries;
=head1 NAME
my $c = shift->openapi->valid_input or return;
my $args = $c->req->params->to_hash // {};
- my $filter;
- my $output = [];
# Create a hash where all keys are embedded values
# Enables easy checking
my %embed;
+ my $args_arr = (ref $args->{embed} eq 'ARRAY') ? $args->{embed} : [ $args->{embed} ];
if (defined $args->{embed}) {
- %embed = map { $_ => 1 } @{$args->{embed}};
+ %embed = map { $_ => 1 } @{$args_arr};
delete $args->{embed};
}
- for my $filter_param ( keys %$args ) {
- my @values = split(/,/, $args->{$filter_param});
- $filter->{$filter_param} = \@values;
+ my $requests = Koha::Illrequests->unblessed;
+
+ # Identify patrons & branches that
+ # we're going to need and get them
+ my $to_fetch = {};
+ $to_fetch->{patrons} = {} if $embed{patron};
+ $to_fetch->{branches} = {} if $embed{library};
+ $to_fetch->{capabilities} = {} if $embed{capabilities};
+ foreach my $req(@{$requests}) {
+ $to_fetch->{patrons}->{$req->{borrowernumber}} = 1 if $embed{patron};
+ $to_fetch->{branches}->{$req->{branchcode}} = 1 if $embed{library};
+ $to_fetch->{capabilities}->{$req->{backend}} = 1 if $embed{capabilities};
}
- my $requests = Koha::Illrequests->search($filter);
+ # Fetch the patrons we need
+ my $patron_arr = [];
+ if ($embed{patron}) {
+ my @patron_ids = keys %{$to_fetch->{patrons}};
+ if (scalar @patron_ids > 0) {
+ my $where = {
+ borrowernumber => { -in => \@patron_ids }
+ };
+ $patron_arr = Koha::Patrons->search($where)->unblessed;
+ }
+ }
- if ( scalar (keys %embed) )
- {
- # Need to embed stuff
- my @results = map { $_->TO_JSON(\%embed) } $requests->as_list;
- return $c->render( status => 200, openapi => \@results );
+ # Fetch the branches we need
+ my $branch_arr = [];
+ if ($embed{library}) {
+ my @branchcodes = keys %{$to_fetch->{branches}};
+ if (scalar @branchcodes > 0) {
+ my $where = {
+ branchcode => { -in => \@branchcodes }
+ };
+ $branch_arr = Koha::Libraries->search($where)->unblessed;
+ }
}
- else
- {
- return $c->render( status => 200, openapi => $requests );
+
+ # Fetch the capabilities we need
+ if ($embed{capabilities}) {
+ my @backends = keys %{$to_fetch->{capabilities}};
+ if (scalar @backends > 0) {
+ foreach my $bc(@backends) {
+ my $backend = Koha::Illrequest->new->load_backend($bc);
+ $to_fetch->{$bc} = $backend->capabilities;
+ }
+ }
}
+
+
+ # Now we've got all associated users and branches,
+ # we can augment the request objects
+ foreach my $req(@{$requests}) {
+ my $r = Koha::Illrequests->new->find($req->{illrequest_id});
+ $req->{id_prefix} = $r->id_prefix;
+ foreach my $p(@{$patron_arr}) {
+ if ($p->{borrowernumber} == $req->{borrowernumber}) {
+ $req->{patron} = {
+ firstname => $p->{firstname},
+ surname => $p->{surname},
+ cardnumber => $p->{cardnumber}
+ };
+ last;
+ }
+ }
+ foreach my $b(@{$branch_arr}) {
+ if ($b->{branchcode} eq $req->{branchcode}) {
+ $req->{library} = $b;
+ last;
+ }
+ }
+ if ($embed{metadata}) {
+ my $metadata = Koha::Illrequestattributes->search(
+ { illrequest_id => $req->{illrequest_id} },
+ { columns => [qw/type value/] }
+ )->unblessed;
+ my $meta_hash = {};
+ foreach my $meta(@{$metadata}) {
+ $meta_hash->{$meta->{type}} = $meta->{value};
+ }
+ $req->{metadata} = $meta_hash;
+ }
+ if ($embed{capabilities}) {
+ $req->{capabilities} = $to_fetch->{$req->{backend}};
+ }
+ }
+
+ return $c->render( status => 200, openapi => $requests );
}
1;