From 5e667bc97dfe8c1c15d9422a489523ba395f5351 Mon Sep 17 00:00:00 2001 From: Kyle M Hall Date: Fri, 5 May 2023 12:22:53 -0400 Subject: [PATCH] Bug 33690: Add ability to send welcome notice when creating patrons using the REST API It would be nice to have the ability to send a welcome notice when creating patrons via the API. Test Plan: 1) Apply this patch 2) Ensure you have a WELCOME notice 3) Create a new patron using the REST API ( api/v1/patrons ) 4) Note no welcome notice is sent to the patron ( you can check the notices tab for the patron ) 5) Repeat step 3, but send the header X-Koha-SendWelcomeEmail with a value of 1 as part of the POST 6) Note the welcome message for the patron is in their notices! --- Koha/REST/V1/Patrons.pm | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/Koha/REST/V1/Patrons.pm b/Koha/REST/V1/Patrons.pm index 1852d7ccd2..843065f3e0 100644 --- a/Koha/REST/V1/Patrons.pm +++ b/Koha/REST/V1/Patrons.pm @@ -22,6 +22,7 @@ use Mojo::Base 'Mojolicious::Controller'; use Koha::Database; use Koha::Exceptions; use Koha::Patrons; +use C4::Letters qw( GetPreparedLetter EnqueueLetter SendQueuedMessages ); use List::MoreUtils qw(any); use Scalar::Util qw( blessed ); @@ -113,6 +114,38 @@ sub add { my $extended_attributes = delete $body->{extended_attributes} // []; my $patron = Koha::Patron->new_from_api($body)->store; + + if ( $c->req->headers->header('X-Koha-SendWelcomeEmail') ) { + + # if we manage to find a valid email address, send notice + if ( $patron->notice_email_address ) { + my $letter = GetPreparedLetter( + module => 'members', + letter_code => 'WELCOME', + branchcode => $patron->branchcode, + , + lang => $patron->lang || 'default', + tables => { + 'branches' => $patron->branchcode, + 'borrowers' => $patron->borrowernumber, + }, + want_librarian => 1, + ); + + if ($letter) { + my $message_id = EnqueueLetter( + { + letter => $letter, + borrowernumber => $patron->id, + to_address => $patron->notice_email_address, + message_transport_type => 'email' + } + ); + SendQueuedMessages( { message_id => $message_id } ); + } + } + } + $patron->extended_attributes( [ map { { code => $_->{type}, attribute => $_->{value} } } -- 2.30.2