From d9ea4e67461bd9288508381ab6e1713df5885bce Mon Sep 17 00:00:00 2001
From: Tomas Cohen Arazi <tomascohen@theke.io>
Date: Wed, 23 Mar 2022 12:36:32 -0300
Subject: [PATCH] Bug 29346: Item action trigger

This patch makes the following actions trigger a holds queue rebuild for
the related biblio:

- Adding an item
- Updating an item
- Deleting an item

To test:
1. Apply this patch
2. Run:
   $ kshell
  k$ prove t/db_dependent/Koha/Item.t
=> SUCCESS: Tests pass! Background job scheduled
3. Sign off :-D

Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>
Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>
---
 Koha/Item.pm               | 13 +++++++++++++
 t/db_dependent/Koha/Item.t | 30 +++++++++++++++++++++++++++++-
 2 files changed, 42 insertions(+), 1 deletion(-)

diff --git a/Koha/Item.pm b/Koha/Item.pm
index 5760ebc05c5..c989a8df37e 100644
--- a/Koha/Item.pm
+++ b/Koha/Item.pm
@@ -30,6 +30,7 @@ use C4::Reserves;
 use C4::ClassSource qw( GetClassSort );
 use C4::Log qw( logaction );
 
+use Koha::BackgroundJob::BatchUpdateBiblioHoldsQueue;
 use Koha::Checkouts;
 use Koha::CirculationRules;
 use Koha::CoverImages;
@@ -212,6 +213,12 @@ sub store {
         unless $params->{skip_record_index};
     $self->get_from_storage->_after_item_action_hooks({ action => $action });
 
+    Koha::BackgroundJob::BatchUpdateBiblioHoldsQueue->new->enqueue(
+        {
+            biblio_ids => [ $self->biblionumber ]
+        }
+    );
+
     return $result;
 }
 
@@ -237,6 +244,12 @@ sub delete {
     logaction( "CATALOGUING", "DELETE", $self->itemnumber, "item" )
       if C4::Context->preference("CataloguingLog");
 
+    Koha::BackgroundJob::BatchUpdateBiblioHoldsQueue->new->enqueue(
+        {
+            biblio_ids => [ $self->biblionumber ]
+        }
+    );
+
     return $result;
 }
 
diff --git a/t/db_dependent/Koha/Item.t b/t/db_dependent/Koha/Item.t
index 38d3f2234d4..3a174dc8bca 100755
--- a/t/db_dependent/Koha/Item.t
+++ b/t/db_dependent/Koha/Item.t
@@ -22,6 +22,7 @@ use utf8;
 
 use Test::More tests => 15;
 use Test::Exception;
+use Test::MockModule;
 
 use C4::Biblio qw( GetMarcSubfieldStructure );
 use C4::Circulation qw( AddIssue AddReturn );
@@ -1170,7 +1171,7 @@ subtest 'columns_to_str' => sub {
 
 subtest 'store() tests' => sub {
 
-    plan tests => 1;
+    plan tests => 2;
 
     subtest '_set_found_trigger() tests' => sub {
 
@@ -1223,6 +1224,33 @@ subtest 'store() tests' => sub {
 
         $schema->storage->txn_rollback;
     };
+
+    subtest 'holds_queue update tests' => sub {
+
+        plan tests => 2;
+
+        $schema->storage->txn_begin;
+
+        my $biblio = $builder->build_sample_biblio;
+
+        my $mock = Test::MockModule->new('Koha::BackgroundJob::BatchUpdateBiblioHoldsQueue');
+        $mock->mock( 'enqueue', sub {
+            my ( $self, $args ) = @_;
+            is_deeply(
+                $args->{biblio_ids},
+                [ $biblio->id ],
+                '->store triggers a holds queue update for the related biblio'
+            );
+        } );
+
+        # new item
+        my $item = $builder->build_sample_item({ biblionumber => $biblio->id });
+
+        # updated item
+        $item->set({ reserves => 1 })->store;
+
+        $schema->storage->txn_rollback;
+    };
 };
 
 subtest 'Recalls tests' => sub {
-- 
2.36.0