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! Signed-off-by: David Nind <david@davidnind.com> Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com> Bug 33690: Tidy test Signed-off-by: Jonathan Druart <jonathan.druart@bugs.koha-community.org> Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>
This commit is contained in:
parent
c914473e81
commit
79a5509b77
3 changed files with 67 additions and 29 deletions
|
@ -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 );
|
||||
|
@ -114,6 +115,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-welcome') ) {
|
||||
|
||||
# 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} } }
|
||||
|
|
|
@ -405,6 +405,11 @@
|
|||
required: true
|
||||
schema:
|
||||
$ref: "../swagger.yaml#/definitions/patron"
|
||||
- name: x-koha-welcome
|
||||
in: header
|
||||
required: false
|
||||
description: If set to 'email' triggers the sending of a welcome email
|
||||
type: string
|
||||
consumes:
|
||||
- application/json
|
||||
produces:
|
||||
|
|
|
@ -35,6 +35,7 @@ use Koha::Exceptions::Patron::Attribute;
|
|||
use Koha::Old::Patrons;
|
||||
use Koha::Patron::Attributes;
|
||||
use Koha::Patron::Debarments qw( AddDebarment );
|
||||
use Koha::Notice::Messages;
|
||||
|
||||
use JSON qw(encode_json);
|
||||
|
||||
|
@ -302,7 +303,7 @@ subtest 'add() tests' => sub {
|
|||
$schema->storage->txn_rollback;
|
||||
|
||||
subtest 'librarian access tests' => sub {
|
||||
plan tests => 24;
|
||||
plan tests => 25;
|
||||
|
||||
$schema->storage->txn_begin;
|
||||
|
||||
|
@ -316,28 +317,6 @@ subtest 'add() tests' => sub {
|
|||
|
||||
# Mock early, so existing mandatory attributes don't break all the tests
|
||||
my $mocked_patron = Test::MockModule->new('Koha::Patron');
|
||||
$mocked_patron->mock(
|
||||
'extended_attributes',
|
||||
sub {
|
||||
|
||||
if ($extended_attrs_exception) {
|
||||
if ( $extended_attrs_exception eq 'Koha::Exceptions::Patron::Attribute::NonRepeatable'
|
||||
or $extended_attrs_exception eq 'Koha::Exceptions::Patron::Attribute::UniqueIDConstraint'
|
||||
)
|
||||
{
|
||||
$extended_attrs_exception->throw(
|
||||
attribute => Koha::Patron::Attribute->new(
|
||||
{ code => $code, attribute => $attr }
|
||||
)
|
||||
);
|
||||
}
|
||||
else {
|
||||
$extended_attrs_exception->throw( type => $type );
|
||||
}
|
||||
}
|
||||
return [];
|
||||
}
|
||||
);
|
||||
|
||||
my $patron = $builder->build_object({ class => 'Koha::Patrons' });
|
||||
my $newpatron = $patron->to_api;
|
||||
|
@ -403,18 +382,21 @@ subtest 'add() tests' => sub {
|
|||
# Set a date-time field
|
||||
$newpatron->{last_seen} = output_pref({ dt => dt_from_string->add( days => -1 ), dateformat => 'rfc3339' });
|
||||
|
||||
$t->post_ok("//$userid:$password@/api/v1/patrons" => json => $newpatron)
|
||||
$t->post_ok("//$userid:$password@/api/v1/patrons" => { 'x-koha-welcome' => 'email' } => json => $newpatron)
|
||||
->status_is(201, 'Patron created successfully')
|
||||
->header_like(
|
||||
Location => qr|^\/api\/v1\/patrons/\d*|,
|
||||
'SWAGGER3.4.1'
|
||||
)
|
||||
->json_has('/patron_id', 'got a patron_id')
|
||||
->json_is( '/cardnumber' => $newpatron->{ cardnumber })
|
||||
->json_is( '/surname' => $newpatron->{ surname })
|
||||
->json_is( '/firstname' => $newpatron->{ firstname })
|
||||
->json_is( '/date_of_birth' => $newpatron->{ date_of_birth }, 'Date field set (Bug 28585)' )
|
||||
->json_is( '/last_seen' => $newpatron->{ last_seen }, 'Date-time field set (Bug 28585)' );
|
||||
->json_is( '/cardnumber' => $newpatron->{cardnumber})
|
||||
->json_is( '/surname' => $newpatron->{surname})
|
||||
->json_is( '/firstname' => $newpatron->{firstname})
|
||||
->json_is( '/date_of_birth' => $newpatron->{date_of_birth}, 'Date field set (Bug 28585)' )
|
||||
->json_is( '/last_seen' => $newpatron->{last_seen}, 'Date-time field set (Bug 28585)' );
|
||||
|
||||
my $p = Koha::Patrons->find( { cardnumber => $newpatron->{cardnumber} } );
|
||||
is( Koha::Notice::Messages->search({ borrowernumber => $p->borrowernumber })->count, 1 , "Patron got welcome notice" );
|
||||
|
||||
$newpatron->{userid} = undef; # force regeneration
|
||||
warning_like {
|
||||
|
@ -428,6 +410,24 @@ subtest 'add() tests' => sub {
|
|||
|
||||
plan tests => 19;
|
||||
|
||||
$mocked_patron->mock(
|
||||
'extended_attributes',
|
||||
sub {
|
||||
|
||||
if ($extended_attrs_exception) {
|
||||
if ( $extended_attrs_exception eq 'Koha::Exceptions::Patron::Attribute::NonRepeatable'
|
||||
or $extended_attrs_exception eq 'Koha::Exceptions::Patron::Attribute::UniqueIDConstraint' )
|
||||
{
|
||||
$extended_attrs_exception->throw(
|
||||
attribute => Koha::Patron::Attribute->new( { code => $code, attribute => $attr } ) );
|
||||
} else {
|
||||
$extended_attrs_exception->throw( type => $type );
|
||||
}
|
||||
}
|
||||
return [];
|
||||
}
|
||||
);
|
||||
|
||||
my $patrons_count = Koha::Patrons->search->count;
|
||||
|
||||
$extended_attrs_exception = 'Koha::Exceptions::Patron::MissingMandatoryExtendedAttribute';
|
||||
|
|
Loading…
Reference in a new issue