From a1e7082c9f081173f204d4d095581d18d1192f9e Mon Sep 17 00:00:00 2001
From: Alex Buckley <alexbuckley@catalyst.net.nz>
Date: Fri, 10 May 2024 06:14:56 +1200
Subject: [PATCH] Bug 36766: Make SFTP notice more customisable

SFTP_FAILURE and SFTP_SUCCESSFUL notices can be configured from Notices
and slip tool.

Sponsored-by: Horowhenua Libraries, Toi Ohomai Institute of Technology, Plant and Food Research Limited, Waitaki District Council, South Taranaki District Council New Zealand
---
 .../bug_36766-add_SFTP_notices.pl             | 20 +++++++++
 .../mysql/en/mandatory/sample_notices.yml     | 24 ++++++++++
 misc/cronjobs/sftp_file.pl                    | 45 +++++++++----------
 3 files changed, 64 insertions(+), 25 deletions(-)
 create mode 100644 installer/data/mysql/atomicupdate/bug_36766-add_SFTP_notices.pl

diff --git a/installer/data/mysql/atomicupdate/bug_36766-add_SFTP_notices.pl b/installer/data/mysql/atomicupdate/bug_36766-add_SFTP_notices.pl
new file mode 100644
index 00000000000..74b7fa88d6b
--- /dev/null
+++ b/installer/data/mysql/atomicupdate/bug_36766-add_SFTP_notices.pl
@@ -0,0 +1,20 @@
+use Modern::Perl;
+
+return {
+    bug_number  => "36766",
+    description => "Add command-line utility to SFTP a file to a remote server",
+    up          => sub {
+        my ($args) = @_;
+        my ( $dbh, $out ) = @$args{qw(dbh out)};
+
+        $dbh->do(q{
+            INSERT IGNORE INTO letter
+            (module,code,branchcode,name,is_html,title,content,message_transport_type,lang)
+            VALUES
+            ('commandline', 'SFTP_FAILURE', '', 'File SFTP failed', 0, 'The SFTP by sftp_file.pl failed', 'SFTP upload failed:\n\n<<sftp_status>>', 'email', 'default'),
+            ('commandline', 'SFTP_SUCCESS', '', 'File SFTP success', 0, 'The SFTP by sftp_file.pl was successful', 'SFTP upload succeeded', 'email', 'default')
+        });
+
+        say $out "Added new sample notices 'SFTP_FAILURE' and 'SFTP_SUCCESS'";
+    },
+};
diff --git a/installer/data/mysql/en/mandatory/sample_notices.yml b/installer/data/mysql/en/mandatory/sample_notices.yml
index ec0e2386dab..bbbd7417f61 100644
--- a/installer/data/mysql/en/mandatory/sample_notices.yml
+++ b/installer/data/mysql/en/mandatory/sample_notices.yml
@@ -1005,6 +1005,30 @@ tables:
             - ""
             - "<order>Ordernumber <<aqorders.ordernumber>> (<<biblio.title>>) (<<aqorders.quantity>> ordered) ($<<aqorders.listprice>> each) has not been received.</order>"
 
+        - module: commandline
+          code: SFTP_FAILURE
+          branchcode: ""
+          name: "File SFTP failed"
+          is_html: 0
+          title: "The SFTP by sftp_file.pl failed"
+          message_transport_type: email
+          lang: default
+          content:
+            - "SFTP upload failed:"
+            - ""
+            - "<<sftp_status>>"
+
+        - module: commandline
+          code: SFTP_SUCCESS
+          branchcode: ""
+          name: "File SFTP success"
+          is_html: 0
+          title: "The SFTP by sftp_file.pl was successful"
+          message_transport_type: email
+          lang: default
+          content:
+            - "SFTP upload succeeded"
+
         - module: ill
           code: ILL_PICKUP_READY
           branchcode: ""
diff --git a/misc/cronjobs/sftp_file.pl b/misc/cronjobs/sftp_file.pl
index 69d0686b20c..74cec5188fd 100755
--- a/misc/cronjobs/sftp_file.pl
+++ b/misc/cronjobs/sftp_file.pl
@@ -23,6 +23,7 @@ use Koha::Email;
 use Getopt::Long qw( GetOptions );
 use Pod::Usage qw( pod2usage );
 use Carp qw( carp );
+use C4::Letters qw( GetPreparedLetter EnqueueLetter );
 
 =head1 NAME
 
@@ -178,14 +179,6 @@ if ( $email ) {
     if ( !Koha::Email->is_valid($email) ) {
         return warn "The email address you defined in the --email parameter is invalid\n";
     }
-
-    $status_email = Koha::Email->new(
-        {
-            to        => $email,
-            from      => $admin_address,
-            subject   => '[OCLC] ' . C4::Context->config('database') . ' ' . $sftp_status,
-        }
-    );
 }
 
 # Do the SFTP upload
@@ -196,31 +189,33 @@ if (
             callback => sub { my ( $sftp, $data, $offset, $size ) = @_; warn "$offset of $size bytes transferred\n"; }
         )
 ) {
-    $sftp_status = 'successful';
     # Send success email
-    if ( $email ) {
-        $status_email_message = "OCLC upload was successful";
-    }
+    $sftp_status = 'SUCCESS';
     close $fh;
 } else {
-    $sftp_status = 'failed';
     # Send failure email
-    if ( $email ) {
-        $status_email_message = "OCLC upload failed\nOCLC error: " . $sftp->error;
-    }
+    $sftp_status = 'FAILURE';
 }
 
 # Send email confirming the success or failure of the SFTP
 if ( $email ) {
-    $status_email->text_body($status_email_message);
-    my $smtp_server = Koha::SMTP::Servers->get_default;
-    $email->transport( $smtp_server->transport );
-    try {
-        $email->send_or_die;
-    }
-    catch {
-        carp "Mail not sent: $_";
-    };
+    $status_email =  C4::Letters::GetPreparedLetter (
+        module => 'commandline',
+        letter_code => "SFTP_$sftp_status", #SFTP_SUCCESS, SFTP_FAILURE
+        message_transport_type => 'email',
+        substitute => {
+            sftp_error => $sftp->error
+        }
+    );
+
+    C4::Letters::EnqueueLetter(
+        {
+            letter                  => $status_email,
+            to_address              => $email,
+            from_address            => $admin_address,
+            message_transport_type  => 'email'
+        }
+    ) or warn "can't enqueue letter " . $status_email->{code};
 }
 
 cronlogaction({ action => 'End', info => "COMPLETED" });
-- 
2.39.5