From b523150252799f7246125fe5f55528ea9e10c95d Mon Sep 17 00:00:00 2001 From: Kyle M Hall Date: Thu, 19 Sep 2024 14:56:26 -0400 Subject: [PATCH] Bug 37968: Add ability to store branchcode for messages in the queue and send by branchcode We should be able to specify in the message queue who "owns" a message, and filter based on that data. We should limit it to the patron's branchcode for now, with the option to add sysprefs to control who "owns" each notice on a per-notice basis as a later enhancement as each notice has different things associated with it. Test Plan: 1) Run updatedatabase.pl 2) Enqueue some messages 3) Inspect the database table, note the message qeueue branchcode column contains the patron's home branch 4) Try using process_message_queue.pl with the new --branch filter 5) Note only the message for the selected branch are sent! --- C4/Letters.pm | 5 +++++ .../data/mysql/atomicupdate/bug_37968.pl | 22 +++++++++++++++++++ installer/data/mysql/kohastructure.sql | 2 ++ misc/cronjobs/process_message_queue.pl | 3 +++ 4 files changed, 32 insertions(+) create mode 100755 installer/data/mysql/atomicupdate/bug_37968.pl diff --git a/C4/Letters.pm b/C4/Letters.pm index 7bdbfc9bf24..a85a1db905c 100644 --- a/C4/Letters.pm +++ b/C4/Letters.pm @@ -953,6 +953,8 @@ sub EnqueueLetter { return; } + my $branchcode = $params->{borrowernumber} ? Koha::Patrons->find( $params->{borrowernumber} )->branchcode : undef; + # If we have any attachments we should encode then into the body. if ( $params->{'attachments'} ) { $params->{'letter'} = _add_attachments( @@ -984,6 +986,7 @@ sub EnqueueLetter { reply_address => $params->{reply_address}, content_type => $params->{letter}->{'content-type'}, failure_code => $params->{failure_code} || q{}, + branchcode => $params->{branchcode}, } )->store(); return $message->id; @@ -1017,6 +1020,7 @@ sub SendQueuedMessages { my $params = shift; my $limit = $params->{limit}; my $where = $params->{where}; + my $branchcode = $params->{branchcode}; Koha::Exceptions::BadParameter->throw("Parameter message_id cannot be empty if passed.") if ( exists( $params->{message_id} ) && !$params->{message_id} ); @@ -1025,6 +1029,7 @@ sub SendQueuedMessages { my $count_messages = 0; my $unsent_messages = Koha::Notice::Messages->search({ + $params->{branchcode} ? ( branchcode => $params->{branchcode} ) : (), status => 'pending', $params->{message_id} ? ( message_id => $params->{message_id} ) : (), $params->{borrowernumber} ? ( borrowernumber => $params->{borrowernumber} ) : (), diff --git a/installer/data/mysql/atomicupdate/bug_37968.pl b/installer/data/mysql/atomicupdate/bug_37968.pl new file mode 100755 index 00000000000..925232ebee3 --- /dev/null +++ b/installer/data/mysql/atomicupdate/bug_37968.pl @@ -0,0 +1,22 @@ +use Modern::Perl; +use Koha::Installer::Output qw(say_warning say_failure say_success say_info); + +return { + bug_number => "37968", + description => "Add ability to store branchcode for messages in the queue", + up => sub { + my ($args) = @_; + my ( $dbh, $out ) = @$args{qw(dbh out)}; + + try { + $dbh->do(q{ + ALTER TABLE message_queue + ADD COLUMN `branchcode` varchar(10) DEFAULT NULL COMMENT 'branch that "owns" this message' AFTER failure_code, + ADD CONSTRAINT `mq_borrowernumber` FOREIGN KEY (`borrowernumber`) REFERENCES `borrowers` (`borrowernumber`) ON DELETE CASCADE ON UPDATE CASCADE; + }); + say_success( $out, "Added column message_queue.branchcode" ); + } catch { + say_failure( $out, "Failed to add column message_queue.branchcode: $_" ); + }; + }, +}; diff --git a/installer/data/mysql/kohastructure.sql b/installer/data/mysql/kohastructure.sql index 2b2175fe2ff..0d4ae8e28d1 100644 --- a/installer/data/mysql/kohastructure.sql +++ b/installer/data/mysql/kohastructure.sql @@ -4679,6 +4679,7 @@ CREATE TABLE `message_queue` ( `reply_address` longtext DEFAULT NULL, `content_type` mediumtext DEFAULT NULL, `failure_code` mediumtext DEFAULT NULL, + `branchcode` varchar(10) DEFAULT NULL COMMENT 'branch that "owns" this message', PRIMARY KEY (`message_id`), KEY `borrowernumber` (`borrowernumber`), KEY `message_transport_type` (`message_transport_type`), @@ -4686,6 +4687,7 @@ CREATE TABLE `message_queue` ( CONSTRAINT `letter_fk` FOREIGN KEY (`letter_id`) REFERENCES `letter` (`id`) ON DELETE SET NULL ON UPDATE CASCADE, CONSTRAINT `messageq_ibfk_1` FOREIGN KEY (`borrowernumber`) REFERENCES `borrowers` (`borrowernumber`) ON DELETE CASCADE ON UPDATE CASCADE, CONSTRAINT `messageq_ibfk_2` FOREIGN KEY (`message_transport_type`) REFERENCES `message_transport_types` (`message_transport_type`) ON UPDATE CASCADE + CONSTRAINT `mq_borrowernumber` FOREIGN KEY (`borrowernumber`) REFERENCES `borrowers` (`borrowernumber`) ON DELETE CASCADE ON UPDATE CASCADE, ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; /*!40101 SET character_set_client = @saved_cs_client */; diff --git a/misc/cronjobs/process_message_queue.pl b/misc/cronjobs/process_message_queue.pl index 0c4ff176f7e..ade08426e0b 100755 --- a/misc/cronjobs/process_message_queue.pl +++ b/misc/cronjobs/process_message_queue.pl @@ -29,6 +29,7 @@ use Try::Tiny qw( catch try ); my $username = undef; my $password = undef; my $limit = undef; +my $branchcode = undef; my $method = 'LOGIN'; my $help = 0; my $verbose = 0; @@ -43,6 +44,7 @@ GetOptions( 'u|username:s' => \$username, 'p|password:s' => \$password, 'l|limit:s' => \$limit, + 'b|branchcode:s' => \$branchcode, 'm|method:s' => \$method, 'h|help|?' => \$help, 'v|verbose' => \$verbose, @@ -128,6 +130,7 @@ C4::Letters::SendQueuedMessages( type => \@type, letter_code => \@letter_code, where => $where, + branchcode => $branchcode, } ); -- 2.39.5 (Apple Git-154)