From e943b5ad1231e7ee64b2ccb9648e3247711ff297 Mon Sep 17 00:00:00 2001
From: Tomas Cohen Arazi <tomascohen@theke.io>
Date: Wed, 3 Mar 2021 16:37:09 -0300
Subject: [PATCH] Bug 27854: Clean GET /patrons controller

The controller method was written a lot of time before the enhancements
we added to the objects.search helper, and it is now much easier to
handle the 'restricted' param use case.

No need to do it like that anymore.

This patch fetches the 'restricted' param from the query parameters and
cleans it from the validated data, so we can just pass the resultset to
$c->objects->search as in all other controllers.

And we had tests for the expected behavior, so testing this is as easy
as:

1. Run:
   $ kshell
  k$ prove t/db_dependent/api/v1/patrons.t
=> SUCCESS: Tests pass!
2. Apply this patch
3. Repeat 1
=> SUCCESS: Tests still pass! Of course!
4. Sign off :-D

Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>

Signed-off-by: David Nind <david@davidnind.com>
Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>
---
 Koha/REST/V1/Patrons.pm | 56 +++++++----------------------------------
 1 file changed, 9 insertions(+), 47 deletions(-)

diff --git a/Koha/REST/V1/Patrons.pm b/Koha/REST/V1/Patrons.pm
index b37452ec82..f82af2c8f9 100644
--- a/Koha/REST/V1/Patrons.pm
+++ b/Koha/REST/V1/Patrons.pm
@@ -44,62 +44,24 @@ sub list {
 
     return try {
 
-        my $patrons_rs = Koha::Patrons->new;
-        my $args = $c->validation->output;
-        my $attributes = {};
+        my $query = {};
+        my $restricted = delete $c->validation->output->{restricted};
+        $query->{debarred} = { '!=' => undef }
+            if $restricted;
 
-        # Extract reserved params
-        my ( $filtered_params, $reserved_params ) = $c->extract_reserved_params($args);
+        my $patrons_rs = Koha::Patrons->search($query);
+        my $patrons    = $c->objects->search( $patrons_rs );
 
-        my $restricted = delete $filtered_params->{restricted};
-
-        # Merge sorting into query attributes
-        $c->dbic_merge_sorting(
-            {
-                attributes => $attributes,
-                params     => $reserved_params,
-                result_set => $patrons_rs
-            }
-        );
-
-        # Merge pagination into query attributes
-        $c->dbic_merge_pagination(
-            {
-                filter => $attributes,
-                params => $reserved_params
-            }
-        );
-
-        if ( defined $filtered_params ) {
-
-            # Apply the mapping function to the passed params
-            $filtered_params = $patrons_rs->attributes_from_api($filtered_params);
-            $filtered_params = $c->build_query_params( $filtered_params, $reserved_params );
-        }
-
-        # translate 'restricted' => 'debarred'
-        $filtered_params->{debarred} = { '!=' => undef }
-          if $restricted;
-
-        my $patrons = $patrons_rs->search( $filtered_params, $attributes );
-        my $total   = $patrons_rs->search->count;
-
-        $c->add_pagination_headers(
-            {
-                total      => ($patrons->is_paged ? $patrons->pager->total_entries : $patrons->count),
-                base_total => $total,
-                params => $args,
-            }
+        return $c->render(
+            status  => 200,
+            openapi => $patrons
         );
-
-        return $c->render( status => 200, openapi => $patrons->to_api );
     }
     catch {
         $c->unhandled_exception($_);
     };
 }
 
-
 =head3 get
 
 Controller function that handles retrieving a single Koha::Patron object
-- 
2.20.1