From 2d545c37a9e0bf978b1859f6573b03854062ed5f Mon Sep 17 00:00:00 2001 From: Marcel de Rooy Date: Wed, 17 May 2023 11:22:08 +0000 Subject: [PATCH] Bug 33837: filter_by_last_update: Add older_than and younger_than Content-Type: text/plain; charset=utf-8 Test plan: Run t/db_dependent/Koha/Objects.t Signed-off-by: Marcel de Rooy --- Koha/Objects.pm | 33 +++++++++++++++++++++------------ t/db_dependent/Koha/Objects.t | 7 ++++++- 2 files changed, 27 insertions(+), 13 deletions(-) diff --git a/Koha/Objects.pm b/Koha/Objects.pm index 6d1467c41d..9e8c90f224 100644 --- a/Koha/Objects.pm +++ b/Koha/Objects.pm @@ -233,14 +233,21 @@ sub update { =head3 filter_by_last_update my $filtered_objects = $objects->filter_by_last_update({ - days => $x, from => $date1, to => $date2, days_inclusive => 1, datetime => 1, + days => $days, from => $date1, to => $date2, days_inclusive => 1, + older_than => $days, younger_than => $days, + datetime => 1, }); -Note: days are exclusive by default (will be inclusive if days_inclusive is passed and set). -The parameters from and to are inclusive. They can be DateTime objects or date strings. -You should pass at least one of the parameters days, from or to. +You should pass at least one of the parameters: days, from, to, older_than, +younger_than. Make sure that they do not conflict with each other to get +meaningful results. +By default, from and to are inclusive and days is exclusive (unless you +passed the optional days_inclusive parameter). +By nature older_than and younger_than are exclusive. This cannot be changed. The optional parameter datetime allows datetime comparison. +The from and to parameters can be DateTime objects or date strings. + =cut sub filter_by_last_update { @@ -248,18 +255,20 @@ sub filter_by_last_update { my $timestamp_column_name = $params->{timestamp_column_name} || 'timestamp'; my $days_inclusive = $params->{days_inclusive} || 0; my $conditions; - Koha::Exceptions::MissingParameter->throw( - "Missing mandatory parameter: days or from or to") - unless exists $params->{days} - or exists $params->{from} - or exists $params->{to}; + Koha::Exceptions::MissingParameter->throw("Please pass: days|from|to|older_than|younger_than") + unless grep { exists $params->{$_} } qw/days from to older_than younger_than/; my $dtf = Koha::Database->new->schema->storage->datetime_parser; my $method = $params->{datetime} ? 'format_datetime' : 'format_date'; - if ( exists $params->{days} ) { + foreach my $p ( qw/days older_than younger_than/ ) { + next if !exists $params->{$p}; my $dt = Koha::DateUtils::dt_from_string(); - my $operator = $days_inclusive ? '<=' : '<'; - $conditions->{$operator} = $dtf->$method( $dt->subtract( days => $params->{days} ) ); + my $operator = $p eq 'days' && $days_inclusive + ? '<=' + : $p eq 'younger_than' + ? '>' + : '<'; + $conditions->{$operator} = $dtf->$method( $dt->subtract( days => $params->{$p} ) ); } if ( exists $params->{from} ) { my $from = ref($params->{from}) ? $params->{from} : dt_from_string($params->{from}); diff --git a/t/db_dependent/Koha/Objects.t b/t/db_dependent/Koha/Objects.t index c8b70efb4f..e62e802b9e 100755 --- a/t/db_dependent/Koha/Objects.t +++ b/t/db_dependent/Koha/Objects.t @@ -1188,7 +1188,7 @@ subtest "filter_by_last_update" => sub { ); }; - subtest 'Parameter datetime' => sub { + subtest 'Parameters datetime, older_than, younger_than' => sub { my $now = dt_from_string(); my $rs = Koha::Patrons->search({ borrowernumber => { -in => \@borrowernumbers } } ); $rs->update({ updated_on => $now->clone->subtract( hours => 25 ) }); @@ -1199,6 +1199,11 @@ subtest "filter_by_last_update" => sub { datetime => 1 })->count, 0, 'Yesterday, not truncated, one hour too late' ); is( $rs->filter_by_last_update({ timestamp_column_name => 'updated_on', from => $now->clone->subtract( hours => 25 ), datetime => 1 })->count, 6, 'Yesterday - 1h, not truncated, within time frame' ); + + is( $rs->filter_by_last_update({ timestamp_column_name => 'updated_on', younger_than => 2, older_than => 1 })->count, + 0, 'when using dates, we will find nothing' ); + is( $rs->filter_by_last_update({ timestamp_column_name => 'updated_on', younger_than => 2, older_than => 1, datetime => 1 })->count, + 6, 'when using datetime, we will find them all' ); }; $schema->storage->txn_rollback; -- 2.30.2