@@ -, +, @@ email/sms notices fail --- Koha/Notice/Message.pm | 36 +++++ Koha/Notice/Messages.pm | 20 ++- ...d_RestrictPatronsWithFailedNotices_syspref.perl | 6 + installer/data/mysql/mandatory/sysprefs.sql | 1 + .../prog/en/modules/admin/preferences/patrons.pref | 8 ++ .../restrict_patrons_with_failed_notices.pl | 152 +++++++++++++++++++++ t/db_dependent/Koha/Notices.t | 90 +++++++++++- 7 files changed, 310 insertions(+), 3 deletions(-) create mode 100644 installer/data/mysql/atomicupdate/bug_23295-add_RestrictPatronsWithFailedNotices_syspref.perl create mode 100755 misc/cronjobs/restrict_patrons_with_failed_notices.pl --- a/Koha/Notice/Message.pm +++ a/Koha/Notice/Message.pm @@ -20,6 +20,7 @@ use Modern::Perl; use Carp; use Koha::Database; +use Koha::Patron::Debarments; use base qw(Koha::Object); @@ -33,6 +34,41 @@ Koha::Notice::Message - Koha notice message Object class, related to the message =cut +=head3 restrict_patron_when_notice_fails + + $failed_notice->restrict_patron_when_notice_fails; + +Places a restriction (debarment) on patrons with failed SMS and email notices. + +=cut + +sub restrict_patron_when_notice_fails { + my ( $self ) = @_; + + # Set the appropriate restriction (debarment) comment depending if the failed + # message is a SMS or email notice. If the failed notice is neither then + # return without placing a restriction + my $comment; + if ($self->message_transport_type eq 'email') { + $comment = 'Email address invalid'; + } elsif ($self->message_transport_type eq 'sms') { + $comment = 'SMS number invalid'; + } else { + return; + } + + AddDebarment( + { + borrowernumber => $self->borrowernumber, + type => 'SUSPENSION', + comment => $comment, + expiration => undef, + } + ); + + return $self; +} + =head3 type =cut --- a/Koha/Notice/Messages.pm +++ a/Koha/Notice/Messages.pm @@ -20,7 +20,6 @@ use Modern::Perl; use Carp; use Koha::Database; - use Koha::Notice::Message; use base qw(Koha::Objects); @@ -35,6 +34,25 @@ Koha::Notice::Message - Koha notice message Object class, related to the message =cut +=head3 get_failed_notices + + my $failed_notices = Koha::Notice::Messages->get_failed_notices({ days => 7 }); + +Returns a hashref of all notices that have failed to send in the last X days, as specified in the 'days' parameter. +If not specified, will default to the last 7 days. + +=cut + +sub get_failed_notices { + my ( $self, $params ) = @_; + my $days = $params->{days} ? $params->{days} : 7; + + return $self->search({ + time_queued => { -between => \"DATE_SUB(NOW(), INTERVAL $days DAY) AND NOW()" }, + status => "failed", + }); +} + =head3 type =cut --- a/installer/data/mysql/atomicupdate/bug_23295-add_RestrictPatronsWithFailedNotices_syspref.perl +++ a/installer/data/mysql/atomicupdate/bug_23295-add_RestrictPatronsWithFailedNotices_syspref.perl @@ -0,0 +1,6 @@ +$DBversion = 'XXX'; +if ( CheckVersion( $DBversion ) ) { + $dbh->do(q{ INSERT IGNORE INTO systempreferences (variable, value, options, explanation, type) VALUES ('RestrictPatronsWithFailedNotices', '0', NULL, 'If enabled then when SMS and email notices fail sending at the Koha level then a debarment will be applied to a patrons account', 'YesNo') }); + + NewVersion( $DBversion, 23295, "Add RestrictPatronsWithFailedNotices system preference" ); +} --- a/installer/data/mysql/mandatory/sysprefs.sql +++ a/installer/data/mysql/mandatory/sysprefs.sql @@ -563,6 +563,7 @@ INSERT INTO systempreferences ( `variable`, `value`, `options`, `explanation`, ` ('RESTOAuth2ClientCredentials','0',NULL,'If enabled, the OAuth2 client credentials flow is enabled for the REST API.','YesNo'), ('RESTPublicAnonymousRequests','1',NULL,'If enabled, the API will allow anonymous access to public routes that don\'t require authenticated access.','YesNo'), ('RESTPublicAPI','1',NULL,'If enabled, the REST API will expose the /public endpoints.','YesNo'), +('RestrictPatronsWithFailedNotices', '0', NULL, 'If enabled then when SMS and email notices fail sending at the Koha level then a debarment will be applied to a patrons account', 'YesNo'), ('RestrictedPageLocalIPs','',NULL,'Beginning of IP addresses considered as local (comma separated ex: "127.0.0,127.0.2")','Free'), ('RestrictedPageContent','',NULL,'HTML content of the restricted page','TextArea'), ('RestrictedPageTitle','',NULL,'Title of the restricted page (breadcrumb and header)','Free'), --- a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/patrons.pref +++ a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/patrons.pref @@ -304,6 +304,14 @@ Patrons: yes: Allow no: "Don't allow" - staff to set the ability for a patron's fines to be viewed by linked patrons in the OPAC. + - + - pref: RestrictPatronsWithFailedNotices + choices: + yes: Apply + no: "Don't apply" + - "a restriction to a patron when their email and SMS messages fail to send at the Koha level." + - "
NOTE: This system preference requires the misc/cronjobs/restrict_patrons_with_failed_notices.pl cronjob. Ask your system administrator to schedule it." + Privacy: - - Use the following URL --- a/misc/cronjobs/restrict_patrons_with_failed_notices.pl +++ a/misc/cronjobs/restrict_patrons_with_failed_notices.pl @@ -0,0 +1,152 @@ +#!/usr/bin/perl + +# Copyright 2020 Catalyst IT +# +# This file is part of Koha. +# +# Koha is free software; you can redistribute it and/or modify it +# under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# Koha is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Koha; if not, see . + +use Modern::Perl; + +use Getopt::Long; +use Pod::Usage; +use C4::Log; + +use C4::Context; +use Koha::Patron::Debarments; +use Koha::Patrons; +use C4::Letters; +use Koha::Notice::Message; + +# Getting options +my ($help, $verbose, $confirm); +GetOptions( + 'h|help' => \$help, + 'v|verbose' => \$verbose, + 'c|confirm' => \$confirm, +); + +pod2usage(1) if $help; + +if ( !C4::Context->preference('RestrictPatronsWithFailedNotices') and $verbose ) { + warn <<'END_WARN'; + +The 'RestrictPatronsWithFailedNotices' system preference is disabled. +This script requires the 'RestrictPatronsWithFailedNotices' system preference to be enabled. +Exiting cronjob + +END_WARN +} +cronlogaction(); + +my @failed_notices = Koha::Notice::Messages->get_failed_notices({ days => 7 }); + +if ( C4::Context->preference('RestrictPatronsWithFailedNotices') ) { + if ( @failed_notices ) { + say "There are borrowers with failed SMS or email notices" if $verbose; + + foreach my $failed_notice ( @failed_notices ) { + + # If failed notice is not a sms or email notice then skip to next failed notice + next unless ($failed_notice->message_transport_type eq 'sms' || $failed_notice->message_transport_type eq 'email'); + + # If failed sms or email notice has no recipient patron then skip to next failed + # notice + next unless $failed_notice->borrowernumber; + + # Find the patron recipient of the failed SMS or email notice. + my $patron = Koha::Patrons->find( $failed_notice->borrowernumber ); + + # Check if patron of failed SMS or email notice is already restricted due to having + # this happen before. If they are already restricted due to having invalid SMS or + # email address don't apply a new restriction (debarment) to their account. + if ( @{ Koha::Patron::Debarments::GetDebarments({ borrowernumber => $patron->borrowernumber, comment => 'SMS number invalid' }) } ) { + say "Patron " . $patron->borrowernumber . ":" . " " . $patron->firstname . " " . $patron->surname . " " . "is currently restricted due to having an invalid SMS number. No new restriction applied" if $verbose; + next; + } elsif ( @{ Koha::Patron::Debarments::GetDebarments({ borrowernumber => $patron->borrowernumber, comment => 'Email address invalid' }) }) { + say "Patron " . $patron->borrowernumber . ":" . " " . $patron->firstname . " " . $patron->surname . " " . "is currently restricted due to having an invalid email address. No new restriction applied" if $verbose; + next; + } + if ($confirm) { + # Patron has not been previously restricted for having failed SMS + # or email addresses apply a restriction now. + say "Applying restriction to patron " . $patron->borrowernumber . ":" . " " . $patron->firstname . " " . $patron->surname if $verbose; + $failed_notice->restrict_patron_when_notice_fails; + } + } + } else { + say "There are no failed SMS or email notices" if $verbose; + } +} + +exit(0); + +__END__ + +=head1 NAME + +restrict_patrons_with_failed_notices.pl + +=head1 SYNOPSIS + +./restrict_patrons_with_failed_notices.pl -h + +Use this script to creates a debarment for all patrons with failed SMS and email notices. + +The 'RestrictPatronsWithFailedNotices' syspref must be enabled for this script to place restrictions to patrons accounts. + +=head1 OPTIONS + +=over 8 + +=item B<-h|--help> + +Prints this help message + +=item B<-v|--verbose> + +Set the verbose flag + +=item B<-c|--confirm> + +The script will alter the database placing a restriction on patrons with failed SMS and email notices. + +=back + +=head1 AUTHOR + +Alex Buckley + +=head1 COPYRIGHT + +Copyright 2019 Catalyst IT + +=head1 LICENSE + +This file is part of Koha. + +Koha is free software; you can redistribute it and/or modify it +under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 3 of the License, or +(at your option) any later version. + +Koha is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with Koha; if not, see . + +=cut --- a/t/db_dependent/Koha/Notices.t +++ a/t/db_dependent/Koha/Notices.t @@ -19,7 +19,7 @@ use Modern::Perl; -use Test::More tests => 3; +use Test::More tests => 8; use Koha::Notice::Templates; use Koha::Database; @@ -66,5 +66,91 @@ $retrieved_template->delete; is( Koha::Notice::Templates->search->count, $nb_of_templates, 'Delete should have deleted the template' ); -$schema->storage->txn_rollback; +## Tests for Koha::Notice::Messages->get_failed_notices + +# Remove all existing messages in the message_queue +Koha::Notice::Messages->delete; + +# Make a patron +my $patron_category = $builder->build({ source => 'Category' })->{categorycode}; +my $borrowernumber = Koha::Patron->new({ + firstname => 'Jane', + surname => 'Smith', + categorycode => $patron_category, + branchcode => $library->{branchcode}, + smsalertnumber => '123', +})->store->borrowernumber; + +# With all notices removed from the message_queue table confirm get_failed_notices() returns 0 +my @failed_notices = Koha::Notice::Messages->get_failed_notices(); +is( @failed_notices, 0, 'No failed notices currently exist'); + +# Add a failed notice to the message_queue table +my $message = Koha::Notice::Message->new({ + borrowernumber => $borrowernumber, + subject => 'subject', + content => 'content', + message_transport_type => 'sms', + status => 'failed', + letter_code => 'just_a_code', + time_queued => \"NOW()", +})->store; + +# With one failed notice in the message_queue table confirm get_failed_notices() returns 1 +my @failed_notices2 = Koha::Notice::Messages->get_failed_notices(); +is( @failed_notices2, 1, 'One failed notice currently exists'); + +# Change failed notice status to 'pending' +$message->update({ status => 'pending' }); + +# With the 1 failed notice in the message_queue table marked 'pending' confirm get_failed_notices() returns 0 +my @failed_notices3 = Koha::Notice::Messages->get_failed_notices(); +is( @failed_notices3, 0, 'No failed notices currently existing, now the notice has been marked pending'); + + +## Tests for Koha::Notice::Message::restrict_patron_when_notice_fails + +# Empty the borrower_debarments table +my $dbh = C4::Context->dbh; +$dbh->do(q|DELETE FROM borrower_debarments|); +# Change the status of the notice back to 'failed' +$message->update({ status => 'failed' }); + +my @failed_notices4 = Koha::Notice::Messages->get_failed_notices(); + +# There should be one failed notice +if (@failed_notices4) { + # Restrict the borrower who has the failed notice + foreach my $failed_notice (@failed_notices4) { + if ($failed_notice->message_transport_type eq 'sms' || $failed_notice->message_transport_type eq 'email') { + $failed_notice->restrict_patron_when_notice_fails; + } + } +} + +# Confirm that the restrict_patron_when_notice_fails() has added a restriction to the patron +is(@{ Koha::Patron::Debarments::GetDebarments({ borrowernumber => $borrowernumber, comment => 'SMS number invalid' })}, 1, "Patron has a restriction placed on them"); + +# Restrict the borrower who has the failed notice +foreach my $failed_notice (@failed_notices4) { + if ($failed_notice->message_transport_type eq 'sms' || $failed_notice->message_transport_type eq 'email') { + + # If the borrower already has a debarment for failed SMS or email notice then don't apply + # a new debarment to their account + if ( @{ Koha::Patron::Debarments::GetDebarments({ borrowernumber => $borrowernumber, comment => 'SMS number invalid' }) } ) { + next; + } elsif ( @{ Koha::Patron::Debarments::GetDebarments({ borrowernumber => $borrowernumber, comment => 'Email address invalid' }) }) { + next; + } + + # Place the debarment if the borrower doesn't already have one for failed SMS or email + # notice + $failed_notice->restrict_patron_when_notice_fails; + } +} + +# Confirm that no new debarment is added to the borrower +is(@{ Koha::Patron::Debarments::GetDebarments({ borrowernumber => $borrowernumber, comment => 'SMS number invalid' })}, 1, "No new restriction has been placed on the patron"); + +$schema->storage->txn_rollback; --