From c4fdd6164b0d156432d23f082dd217fdc91dc71a Mon Sep 17 00:00:00 2001 From: Tomas Cohen Arazi Date: Mon, 25 Sep 2023 15:54:08 -0300 Subject: [PATCH] [ALTERNATE] Bug 34277: Add a way to filter patrons by their outstanding balance This patch adds to new `query parameters` to the `GET /patrons` route: * owes_less_than * owes_more_than This parameters will be used to build a query, based on Koha::Patrons->filter_by_amount_owed, to return only patrons matching the criteria. FIXME: Tests missing yet --- Koha/Patron.pm | 3 +++ Koha/REST/V1/Patrons.pm | 22 +++++++++++++++++----- api/v1/swagger/paths/patrons.yaml | 10 ++++++++++ 3 files changed, 30 insertions(+), 5 deletions(-) diff --git a/Koha/Patron.pm b/Koha/Patron.pm index 18e9d234e0e..7b4f8c8f09f 100644 --- a/Koha/Patron.pm +++ b/Koha/Patron.pm @@ -2150,6 +2150,9 @@ sub to_api { ? Mojo::JSON->true : Mojo::JSON->false; + # FIXME: introduced by Koha::Patrons->filter_by_amount_owed + delete $json_patron->{outstanding}; + return $json_patron; } diff --git a/Koha/REST/V1/Patrons.pm b/Koha/REST/V1/Patrons.pm index 0e04199b550..35f55fda640 100644 --- a/Koha/REST/V1/Patrons.pm +++ b/Koha/REST/V1/Patrons.pm @@ -46,21 +46,33 @@ sub list { return try { - my $query = {}; + my $query = {}; my $restricted = $c->param('restricted'); $c->req->params->remove('restricted'); $query->{debarred} = { '!=' => undef } if $restricted; + my $owes_less_than = $c->param('owes_less_than'); + my $owes_more_than = $c->param('owes_more_than'); + $c->req->params->remove('owes_less_than')->remove('owes_more_than'); + my $patrons_rs = Koha::Patrons->search($query); - my $patrons = $c->objects->search( $patrons_rs ); + + if ( defined $owes_less_than || defined $owes_more_than ) { + + $patrons_rs = $patrons_rs->filter_by_amount_owed( + { + less_than => $owes_less_than, + more_than => $owes_more_than, + } + ); + } return $c->render( status => 200, - openapi => $patrons + openapi => $c->objects->search($patrons_rs), ); - } - catch { + } catch { $c->unhandled_exception($_); }; } diff --git a/api/v1/swagger/paths/patrons.yaml b/api/v1/swagger/paths/patrons.yaml index accd6651cad..1f46a68a972 100644 --- a/api/v1/swagger/paths/patrons.yaml +++ b/api/v1/swagger/paths/patrons.yaml @@ -339,6 +339,16 @@ description: Search on login_attempts required: false type: string + - name: owes_less_than + in: query + description: Pick patrons owing less than the passed amount + type: number + required: false + - name: owes_more_than + in: query + description: Pick patrons owing more than the passed amount + type: number + required: false - $ref: "../swagger.yaml#/parameters/match" - $ref: "../swagger.yaml#/parameters/order_by" - $ref: "../swagger.yaml#/parameters/page" -- 2.42.0