View | Details | Raw Unified | Return to bug 33360
Collapse All | Expand All

(-)a/Koha/Notice/Util.pm (+109 lines)
Line 0 Link Here
1
package Koha::Notice::Util;
2
3
# Copyright Rijksmuseum 2023
4
#
5
# This file is part of Koha.
6
#
7
# Koha is free software; you can redistribute it and/or modify it
8
# under the terms of the GNU General Public License as published by
9
# the Free Software Foundation; either version 3 of the License, or
10
# (at your option) any later version.
11
#
12
# Koha is distributed in the hope that it will be useful, but
13
# WITHOUT ANY WARRANTY; without even the implied warranty of
14
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
# GNU General Public License for more details.
16
#
17
# You should have received a copy of the GNU General Public License
18
# along with Koha; if not, see <http://www.gnu.org/licenses>.
19
20
use Modern::Perl;
21
use Data::Dumper qw/Dumper/;
22
23
use C4::Context;
24
use Koha::DateUtils qw/dt_from_string/;
25
use Koha::Notice::Messages;
26
27
=head1 NAME
28
29
Koha::Notice::Util - Utility class related to Koha notice messages
30
31
=head1 CLASS METHODS
32
33
=head2 load_domain_limits
34
35
    my $domain_limits = Koha::Notice::Util->load_domain_limits;
36
37
=cut
38
39
sub load_domain_limits {
40
    my ( $class ) = @_;
41
42
    my $domain_limits;
43
    my $entry = C4::Context->config('message_domain_limits');
44
    if( ref($entry) eq 'HASH' ) {
45
        if( exists $entry->{domain} ) {
46
            # Turn single hash entry into array
47
            $domain_limits = ref($entry->{domain}) eq 'HASH'
48
                ? [ $entry->{domain} ]
49
                : $entry->{domain};
50
            # Convert to hash structure by domain name
51
            $domain_limits = { map { lc $_->{name}, { limit => $_->{limit}, unit => $_->{unit}, count => 0 }} @$domain_limits };
52
        }
53
    }
54
    return _fill_domain_counts($domain_limits);
55
}
56
57
=head2 exceeds_limit
58
59
    my $boolean = Koha::Notice::Util->exceeds_limit( $to_address, $domain_limits );
60
61
=cut
62
63
sub exceeds_limit {
64
    my ( $class, $to_address, $domain_limits ) = @_;
65
    return 0 if !$domain_limits;
66
    my $domain = q{};
67
    $domain = lc $1 if $to_address && $to_address =~ /@(.*)/;
68
    return 0 if !exists $domain_limits->{$domain};
69
    return 1 if $domain_limits->{$domain}->{count} >= $domain_limits->{$domain}->{limit};
70
    $domain_limits->{$domain}->{count}++;
71
    warn "Sending messages: domain $domain reached limit of ".
72
        $domain_limits->{$domain}->{limit}. '/'. $domain_limits->{$domain}->{unit}
73
        if $domain_limits->{$domain}->{count} == $domain_limits->{$domain}->{limit};
74
    return 0;
75
}
76
77
=head1 PRIVATE METHODS
78
79
=cut
80
81
sub _fill_domain_counts {
82
    my ( $limits ) = @_;
83
    return $limits if !$limits;
84
    my $dt_parser = Koha::Database->new->schema->storage->datetime_parser;
85
    foreach my $domain ( keys %$limits ) {
86
        my $start_dt = _convert_unit( undef, $limits->{$domain}->{unit} );
87
        $limits->{$domain}->{count} = Koha::Notice::Messages->search({
88
            message_transport_type => 'email',
89
            status => 'sent',
90
            to_address => { 'LIKE', '%'.$domain },
91
            updated_on => { '>=', $dt_parser->format_datetime($start_dt) }, # FIXME Would be nice if possible via filter_by_last_update
92
        })->count;
93
    }
94
    return $limits;
95
}
96
97
sub _convert_unit { # unit should be like \d+(m|h|d)
98
    my ( $dt, $unit ) = @_;
99
    $dt //= dt_from_string();
100
    if( $unit && $unit =~ /(\d+)([mhd])/ ) {
101
        my $abbrev = { m => 'minutes', h => 'hours', d => 'days' };
102
        foreach my $h ( 0, 1 ) { # try hour before too when subtract fails (like: change to summertime)
103
            eval { $dt->subtract( hours => $h )->subtract( $abbrev->{$2} => $1 ) } and last;
104
        }
105
    }
106
    return $dt;
107
}
108
109
1;
(-)a/t/db_dependent/Koha/Notice_Util.t (-1 / +109 lines)
Line 0 Link Here
0
- 
1
#!/usr/bin/perl
2
3
# Copyright 2023 Koha Development team
4
#
5
# This file is part of Koha
6
#
7
# Koha is free software; you can redistribute it and/or modify it
8
# under the terms of the GNU General Public License as published by
9
# the Free Software Foundation; either version 3 of the License, or
10
# (at your option) any later version.
11
#
12
# Koha is distributed in the hope that it will be useful, but
13
# WITHOUT ANY WARRANTY; without even the implied warranty of
14
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
# GNU General Public License for more details.
16
#
17
# You should have received a copy of the GNU General Public License
18
# along with Koha; if not, see <http://www.gnu.org/licenses>.
19
20
use Modern::Perl;
21
#use Data::Dumper qw/Dumper/;
22
use Test::More tests => 2;
23
use Test::MockModule;
24
use Test::Warn;
25
26
use C4::Context;
27
use Koha::Database;
28
use Koha::DateUtils qw/dt_from_string/;
29
use Koha::Notice::Util;
30
31
use t::lib::TestBuilder;
32
use t::lib::Mocks;
33
34
my $schema = Koha::Database->new->schema;
35
$schema->storage->txn_begin;
36
37
my $builder = t::lib::TestBuilder->new;
38
39
subtest 'load_domain_limits' => sub {
40
    plan tests => 12;
41
42
    my $domain_limits;
43
    t::lib::Mocks::mock_config( 'message_domain_limits', undef );
44
    is( Koha::Notice::Util->load_domain_limits, undef, 'koha-conf does not contain entry' );
45
    t::lib::Mocks::mock_config( 'message_domain_limits', q{} );
46
    is( Koha::Notice::Util->load_domain_limits, undef, 'koha-conf contains blank entry' );
47
    t::lib::Mocks::mock_config( 'message_domain_limits', { domain => { name => 'A', limit => 2, unit => '1d' } } );
48
    $domain_limits = Koha::Notice::Util->load_domain_limits;
49
    is( keys %$domain_limits, 1, 'koha-conf contains one domain' );
50
    is( $domain_limits->{a}->{limit}, 2, 'check limit of first entry' );
51
    is( $domain_limits->{a}->{unit}, '1d', 'check unit of first entry' );
52
    t::lib::Mocks::mock_config( 'message_domain_limits',
53
        { domain => [ { name => 'A', limit => 2, unit => '2d' }, { name => 'B', limit => 3, unit => '3h' } ] },
54
    );
55
    $domain_limits = Koha::Notice::Util->load_domain_limits;
56
    is( keys %$domain_limits, 2, 'koha-conf contains two domains' );
57
    is( $domain_limits->{b}->{limit}, 3, 'check limit of second entry' );
58
59
    # Check counting
60
    my @values = ( message_transport_type => 'email', status => 'sent' );
61
    my $today = dt_from_string();
62
    $builder->build_object({ class => 'Koha::Notice::Messages',
63
        value => { @values, to_address => 'a@A', updated_on => $today->clone->subtract( hours => 36 ) }});
64
    $builder->build_object({ class => 'Koha::Notice::Messages',
65
        value => { @values, to_address => 'b@A', updated_on => $today->clone->subtract( hours => 49 ) }});
66
    $builder->build_object({ class => 'Koha::Notice::Messages',
67
        value => { @values, to_address => 'c@A', updated_on => $today->clone->subtract( days => 3 ) }});
68
    $domain_limits = Koha::Notice::Util->load_domain_limits;
69
    is( $domain_limits->{a}->{count}, 1, 'Three messages to A, 1 within unit of 2d' );
70
    t::lib::Mocks::mock_config( 'message_domain_limits',
71
        { domain => [ { name => 'A', limit => 2, unit => '50h' }, { name => 'B', limit => 3, unit => '3h' } ] },
72
    );
73
    $domain_limits = Koha::Notice::Util->load_domain_limits;
74
    is( $domain_limits->{a}->{count}, 2, 'Three messages to A, 2 within unit of 50h' );
75
76
    # Date subtraction - edge case (start of summer time)
77
    my $mock_context = Test::MockModule->new('C4::Context');
78
    $mock_context->mock( 'tz', sub { return DateTime::TimeZone->new( name => 'Europe/Amsterdam' )} );
79
    my $dt = dt_from_string( '2023-03-31 02:30:00', 'iso', '+02:00' );
80
    is( Koha::Notice::Util::_convert_unit( $dt, '4d')->stringify, '2023-03-27T02:30:00', '02:30 is fine' );
81
    is( Koha::Notice::Util::_convert_unit( $dt, '1d')->stringify, '2023-03-26T01:30:00', 'Moved 02:30 to 01:30' );
82
    # Test bad unit
83
    is( Koha::Notice::Util::_convert_unit( $dt, 'y')->stringify, '2023-03-26T01:30:00', 'No further shift for bad unit' );
84
    $mock_context->unmock('tz');
85
};
86
87
subtest 'exceeds_limit' => sub {
88
    plan tests => 6;
89
90
    my $domain_limits;
91
92
    t::lib::Mocks::mock_config( 'message_domain_limits', undef );
93
    $domain_limits = Koha::Notice::Util->load_domain_limits;
94
    is( Koha::Notice::Util->exceeds_limit( 'marcel@koha.nl', $domain_limits ), 0, 'False when having no limits' );
95
96
    t::lib::Mocks::mock_config( 'message_domain_limits',
97
        { domain => [ { name => 'A', limit => 0, unit => '1d' }, { name => 'B', limit => 1, unit => '5h' } ] },
98
    );
99
    $domain_limits = Koha::Notice::Util->load_domain_limits;
100
    is( Koha::Notice::Util->exceeds_limit( '1@A', $domain_limits ), 1, 'Limit for A already reached' );
101
    my $result;
102
    warning_like { $result = Koha::Notice::Util->exceeds_limit( '2@B', $domain_limits ) }
103
        qr/Sending messages: domain b reached limit/, 'Check warn for reaching limit';
104
    is( $result, 0, 'Limit for B not yet exceeded' );
105
    is( Koha::Notice::Util->exceeds_limit( '3@B', $domain_limits ), 1, 'Limit for B already reached' );
106
    is( Koha::Notice::Util->exceeds_limit( '4@C', $domain_limits ), 0, 'No limits for C' );
107
};
108
109
$schema->storage->txn_rollback;

Return to bug 33360