From 2df2363be8c48d822cb1d9c09c47fad175a0ecb1 Mon Sep 17 00:00:00 2001
From: Tomas Cohen Arazi <tomascohen@theke.io>
Date: Wed, 16 Mar 2022 18:05:55 -0300
Subject: [PATCH] Bug 29346: Circulation actions triggers

This patch introduces triggers for real-time updating the holds queue at
check out and check in.

The following high-level methods are involved:

- C4::Circulation::AddIssue
- C4::Circulation::AddReturn

To test:
1. Apply this patch
2. Run:
   $ kshell
  k$ prove t/db_dependent/Circulation_holdsqueue.t
=> SUCCESS: Tests pass! Triggers are triggered
3. Sign off :-D

Note: I put the tests on a separate file because the other one was too
big already.

Sponsored-by: Montgomery County Public Libraries
Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>
Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>
---
 C4/Circulation.pm                       | 13 +++++
 t/db_dependent/Circulation_holdsqueue.t | 64 +++++++++++++++++++++++++
 2 files changed, 77 insertions(+)
 create mode 100755 t/db_dependent/Circulation_holdsqueue.t

diff --git a/C4/Circulation.pm b/C4/Circulation.pm
index d0f292650d..1bf3568321 100644
--- a/C4/Circulation.pm
+++ b/C4/Circulation.pm
@@ -41,6 +41,7 @@ use Algorithm::CheckDigits qw( CheckDigits );
 use Data::Dumper qw( Dumper );
 use Koha::Account;
 use Koha::AuthorisedValues;
+use Koha::BackgroundJob::BatchUpdateBiblioHoldsQueue;
 use Koha::Biblioitems;
 use Koha::DateUtils qw( dt_from_string output_pref );
 use Koha::Calendar;
@@ -1773,6 +1774,12 @@ sub AddIssue {
                     checkout => $issue->get_from_storage
                 }
             });
+
+            Koha::BackgroundJob::BatchUpdateBiblioHoldsQueue->new->enqueue(
+                {
+                    biblio_ids => [ $item_object->biblionumber ]
+                }
+            );
         }
     }
     return $issue;
@@ -2433,6 +2440,12 @@ sub AddReturn {
                 checkout=> $checkin
             }
         });
+
+        Koha::BackgroundJob::BatchUpdateBiblioHoldsQueue->new->enqueue(
+            {
+                biblio_ids => [ $item->biblionumber ]
+            }
+        );
     }
 
     return ( $doreturn, $messages, $issue, ( $patron ? $patron->unblessed : {} ));
diff --git a/t/db_dependent/Circulation_holdsqueue.t b/t/db_dependent/Circulation_holdsqueue.t
new file mode 100755
index 0000000000..f8a82ffd19
--- /dev/null
+++ b/t/db_dependent/Circulation_holdsqueue.t
@@ -0,0 +1,64 @@
+#!/usr/bin/perl
+
+# 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 <http://www.gnu.org/licenses>.
+
+use Modern::Perl;
+
+use Test::More tests => 1;
+use Test::MockModule;
+
+use C4::Circulation qw( AddIssue AddReturn );
+
+use Koha::Database;
+
+use t::lib::Mocks;
+use t::lib::TestBuilder;
+
+my $schema  = Koha::Database->schema;
+my $builder = t::lib::TestBuilder->new;
+
+subtest 'AddIssue() and AddReturn() real-time holds queue tests' => sub {
+
+    plan tests => 2;
+
+    $schema->storage->txn_begin;
+
+    my $library = $builder->build_object({ class => 'Koha::Libraries' });
+    my $patron  = $builder->build_object({ class => 'Koha::Patrons' });
+    my $item    = $builder->build_sample_item({ library => $library->id });
+
+    t::lib::Mocks::mock_userenv({ branchcode => $library->id });
+
+    my $action;
+
+    my $mock = Test::MockModule->new('Koha::BackgroundJob::BatchUpdateBiblioHoldsQueue');
+    $mock->mock( 'enqueue', sub {
+        my ( $self, $args ) = @_;
+        is_deeply(
+            $args->{biblio_ids},
+            [ $item->biblionumber ],
+            "$action triggers a holds queue update for the related biblio"
+        );
+    } );
+
+    $action = 'AddIssue';
+    AddIssue( $patron->unblessed, $item->barcode, );
+
+    $action = 'AddReturn';
+    AddReturn( $item->barcode );
+
+    $schema->storage->txn_rollback;
+};
-- 
2.34.1