From e3a4fa4a8cd65971ecaac1147a73d3efb550f276 Mon Sep 17 00:00:00 2001
From: Nick Clemens <nick@bywatersolutions.com>
Date: Tue, 7 Nov 2023 13:45:01 +0000
Subject: [PATCH] Bug 35277: Pseudonymize in a background job

This patch adds a new background job for pseduonymization

To test:
 1 - Download the benchmarking script
 2 - Ensure your koha has bcrypt_settings in koha-conf.xml
     See bug 28911
 3 - perl perf_check_pseudo.pl
 4 - Note the slowdown after pseudonymization enabled
 5 - Apply patches, restart all
 6 - perl perf_check_pseudo.pl
 7 - Note improvement
 8 - Enable pseudonymization in sytem preferences
 9 - Perform some checkouts and returns
10 - Verify the background jobs complete successfully
11 - Verify the pseudonymized_transactions table is updated correctly
---
 Koha/BackgroundJob.pm                         |  1 +
 Koha/BackgroundJob/PseudonymizeStatistic.pm   | 80 +++++++++++++++++++
 Koha/Statistic.pm                             |  4 +-
 api/v1/swagger/paths/items.yaml               |  2 +
 .../pseudonymize_statistic.inc                | 25 ++++++
 .../prog/en/modules/admin/background_jobs.tt  |  4 +
 6 files changed, 114 insertions(+), 2 deletions(-)
 create mode 100644 Koha/BackgroundJob/PseudonymizeStatistic.pm
 create mode 100644 koha-tmpl/intranet-tmpl/prog/en/includes/background_jobs/pseudonymize_statistic.inc

diff --git a/Koha/BackgroundJob.pm b/Koha/BackgroundJob.pm
index bcfa68baca1..7c2f735ada4 100644
--- a/Koha/BackgroundJob.pm
+++ b/Koha/BackgroundJob.pm
@@ -428,6 +428,7 @@ sub core_types_to_classes {
         stage_marc_for_import               => 'Koha::BackgroundJob::StageMARCForImport',
         marc_import_commit_batch            => 'Koha::BackgroundJob::MARCImportCommitBatch',
         marc_import_revert_batch            => 'Koha::BackgroundJob::MARCImportRevertBatch',
+        pseudonymize_statistic              => 'Koha::BackgroundJob::PseudonymizeStatistic',
     };
 }
 
diff --git a/Koha/BackgroundJob/PseudonymizeStatistic.pm b/Koha/BackgroundJob/PseudonymizeStatistic.pm
new file mode 100644
index 00000000000..3c78a0d9654
--- /dev/null
+++ b/Koha/BackgroundJob/PseudonymizeStatistic.pm
@@ -0,0 +1,80 @@
+package Koha::BackgroundJob::PseudonymizeStatistic;
+
+# 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 Koha::PseudonymizedTransactions;
+
+use base 'Koha::BackgroundJob';
+
+=head1 NAME
+
+Koha::BackgroundJob::BatchCancelHold - Batch cancel holds
+
+This is a subclass of Koha::BackgroundJob.
+
+=head1 API
+
+=head2 Class methods
+
+=head3 job_type
+
+Define the job type of this job: batch_hold_cancel
+
+=cut
+
+sub job_type {
+    return 'pseudonymize_statistic';
+}
+
+=head3 process
+
+Process the modification.
+
+=cut
+
+sub process {
+    my ( $self, $args ) = @_;
+    $self->start;
+    my $statistic = $args->{statistic};
+    my $stat_object = Koha::Statistic->new( $statistic );
+    Koha::PseudonymizedTransaction->new_from_statistic($stat_object)->store();
+    $self->finish({ data => "" }); # We want to clear the job data to avoid storing patron information
+
+}
+
+=head3 enqueue
+
+Enqueue the new job
+
+=cut
+
+sub enqueue {
+    my ( $self, $args ) = @_;
+
+    my $statistic = $args->{statistic};
+
+    $self->SUPER::enqueue(
+        {
+            job_size => 1, # Only handling one at time
+            job_args => { statistic => $statistic },
+            job_queue => 'default',
+        }
+    );
+}
+
+1;
diff --git a/Koha/Statistic.pm b/Koha/Statistic.pm
index c3deb71b2a1..1f95b3a54ff 100644
--- a/Koha/Statistic.pm
+++ b/Koha/Statistic.pm
@@ -23,7 +23,7 @@ use C4::Context;
 use Koha::Database;
 use Koha::DateUtils qw/dt_from_string/;
 use Koha::Items;
-use Koha::PseudonymizedTransaction;
+use Koha::BackgroundJob::PseudonymizeStatistic;
 
 use base qw(Koha::Object);
 
@@ -121,7 +121,7 @@ sub new {
 sub store {
     my ($self) = @_;
     $self->SUPER::store;
-    Koha::PseudonymizedTransaction->new_from_statistic($self)->store
+    Koha::BackgroundJob::PseudonymizeStatistic->new->enqueue({ statistic => $self->unblessed })
         if C4::Context->preference('Pseudonymization')
         && $self->borrowernumber    # Not a real transaction if the patron does not exist
                                     # For instance can be a transfer, or hold trigger
diff --git a/api/v1/swagger/paths/items.yaml b/api/v1/swagger/paths/items.yaml
index 3f4677a9a35..67c4cf31876 100644
--- a/api/v1/swagger/paths/items.yaml
+++ b/api/v1/swagger/paths/items.yaml
@@ -22,6 +22,8 @@
           enum:
             - +strings
             - biblio
+            - get_transfers
+            - current_branchtransfer
         collectionFormat: csv
       - $ref: "../swagger.yaml#/parameters/match"
       - $ref: "../swagger.yaml#/parameters/order_by"
diff --git a/koha-tmpl/intranet-tmpl/prog/en/includes/background_jobs/pseudonymize_statistic.inc b/koha-tmpl/intranet-tmpl/prog/en/includes/background_jobs/pseudonymize_statistic.inc
new file mode 100644
index 00000000000..e3d15372172
--- /dev/null
+++ b/koha-tmpl/intranet-tmpl/prog/en/includes/background_jobs/pseudonymize_statistic.inc
@@ -0,0 +1,25 @@
+[% USE KohaSpan %]
+[% BLOCK report %]
+    [% IF job.status == 'finished' %]
+        <div class="dialog message">
+            Statistic has been Pseudonymized.
+        </div>
+    [% ELSIF job.status == 'cancelled' %]
+        <div class="dialog message">
+            Pseudonymization has been cancelled.
+        </div>
+    [% ELSE %]
+        <div class="dialog message">
+            Pseudonymization is pending.
+        </div>
+    [% END %]
+[% END %]
+
+[% BLOCK detail %]
+        <div class="dialog message">
+            Pseudonymization.
+        </div> 
+[% END %]
+
+[% BLOCK js %]
+[% END %]
diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/background_jobs.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/background_jobs.tt
index 5ed845b13e7..11cec0dfcfe 100644
--- a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/background_jobs.tt
+++ b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/background_jobs.tt
@@ -209,6 +209,10 @@
                 '_id': 'batch_hold_cancel',
                 '_str': _("Batch hold cancellation")
             },
+            {
+                '_id': 'pseudonymize_statistic',
+                '_str': _("Pseudonymize statistic")
+            },
             {
                 '_id': 'create_eholdings_from_biblios',
                 '_str': _("Create eHolding titles")
-- 
2.30.2