Bug 33974: (follow-up) Adapt the orders endpoint

In order to reduce the technical debt carried on the orders controller,
and to highlight the decisions made on the prior patch, I adapted the
list() orders controller using the new tools in town.

The result is this endpoint can now embed bibio, without needing to have
a custom piece of code.

To test:
1. Apply this patch
2. Run:
   $ ktd --shell
  k$ prove t/db_dependent/api/v1/*.t
=> SUCCESS: Tests pass :-D

Signed-off-by: Sam Lau <samalau@gmail.com>
Signed-off-by: Nick Clemens <nick@bywatersolutions.com>
Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>
Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>
This commit is contained in:
Tomás Cohen Arazi 2023-06-16 12:53:05 -03:00
parent 353f328ad3
commit c07e43cf2a
Signed by: tomascohen
GPG key ID: 0A272EA1B2F3C15F

View file

@ -61,132 +61,22 @@ sub list {
$orders_rs = $orders_rs->filter_by_id_including_transfers({ ordernumber => $order_id })
if $order_id;
my $args = $c->validation->output;
my $attributes = {};
my @query_fixers;
# Extract reserved params
my ( $filtered_params, $reserved_params, $path_params ) = $c->extract_reserved_params($args);
# Look for embeds
my $embed = $c->stash('koha.embed');
my $fixed_embed = clone($embed);
if ( exists $fixed_embed->{biblio} ) {
if ( exists $embed->{biblio} ) { # asked to embed biblio
my $fixed_embed = clone($embed);
# Add biblioitems to prefetch
# FIXME remove if we merge biblio + biblioitems
$fixed_embed->{biblio}->{children}->{biblioitem} = {};
$c->stash('koha.embed', $fixed_embed);
push @query_fixers, (sub{ Koha::Biblios->new->api_query_fixer( $_[0], 'biblio', $_[1] ) });
}
if ( exists $reserved_params->{_order_by} ) {
# _order_by passed, fix if required
for my $p ( @{$reserved_params->{_order_by}} ) {
$p = $c->table_name_fixer($p);
}
}
# Merge sorting into query attributes
$c->dbic_merge_sorting(
{
attributes => $attributes,
params => $reserved_params,
result_set => $orders_rs,
}
);
# If no pagination parameters are passed, default
$reserved_params->{_per_page} //= C4::Context->preference('RESTdefaultPageSize');
$reserved_params->{_page} //= 1;
unless ( $reserved_params->{_per_page} == -1 ) {
# Merge pagination into query attributes
$c->dbic_merge_pagination(
{
filter => $attributes,
params => $reserved_params
}
);
}
# Generate prefetches for embedded stuff
$c->dbic_merge_prefetch(
{
attributes => $attributes,
result_set => $orders_rs
}
);
# Call the to_model function by reference, if defined
if ( defined $filtered_params ) {
# Apply the mapping function to the passed params
$filtered_params = $orders_rs->attributes_from_api($filtered_params);
$filtered_params = $c->build_query_params( $filtered_params, $reserved_params );
}
if ( defined $path_params ) {
# Apply the mapping function to the passed params
$filtered_params //= {};
$path_params = $orders_rs->attributes_from_api($path_params);
foreach my $param (keys %{$path_params}) {
$filtered_params->{$param} = $path_params->{$param};
}
}
if ( defined $reserved_params->{q}
|| defined $reserved_params->{query} )
{
$filtered_params //={};
my @query_params_array;
my $json = JSON->new;
# q is defined as multi => JSON::Validator generates an array
# containing the string
foreach my $q ( @{ $reserved_params->{q} } ) {
push @query_params_array,
$json->decode( $c->table_name_fixer($q) )
if $q; # skip if exists but is empty
}
# query is already decoded by JSON::Validator at this point
push @query_params_array,
$json->decode(
$c->table_name_fixer(
$json->encode( $reserved_params->{query} )
)
) if $reserved_params->{query};
my $query_params;
if ( scalar(@query_params_array) > 1 ) {
$query_params = { '-and' => \@query_params_array };
}
else {
$query_params = $query_params_array[0];
}
$filtered_params = $c->merge_q_params( $filtered_params, $query_params, $orders_rs );
}
# Perform search
my $orders = $orders_rs->search( $filtered_params, $attributes );
my $total = $orders_rs->search->count;
$c->add_pagination_headers(
{
base_total => $total,
page => $reserved_params->{_page},
per_page => $reserved_params->{_per_page},
query_params => $args,
total => ( $orders->is_paged ? $orders->pager->total_entries : $orders->count ),
}
);
return $c->render(
status => 200,
openapi => $c->objects->to_api($orders)
openapi => $c->objects->search( $orders_rs, \@query_fixers ),
);
}
catch {