Bug 33837: filter_by_last_update: Add older_than and younger_than

Test plan:
Run t/db_dependent/Koha/Objects.t

Signed-off-by: Marcel de Rooy <m.de.rooy@rijksmuseum.nl>

Signed-off-by: Jonathan Druart <jonathan.druart@bugs.koha-community.org>
Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>
This commit is contained in:
Marcel de Rooy 2023-05-17 11:22:08 +00:00 committed by Tomas Cohen Arazi
parent d3e286b204
commit 587bcae366
No known key found for this signature in database
GPG Key ID: 0A272EA1B2F3C15F
2 changed files with 27 additions and 13 deletions

View File

@ -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});

View File

@ -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;