View | Details | Raw Unified | Return to bug 33690
Collapse All | Expand All

(-)a/Koha/REST/V1/Patrons.pm (+33 lines)
Lines 22-27 use Mojo::Base 'Mojolicious::Controller'; Link Here
22
use Koha::Database;
22
use Koha::Database;
23
use Koha::Exceptions;
23
use Koha::Exceptions;
24
use Koha::Patrons;
24
use Koha::Patrons;
25
use C4::Letters qw( GetPreparedLetter EnqueueLetter SendQueuedMessages );
25
26
26
use List::MoreUtils qw(any);
27
use List::MoreUtils qw(any);
27
use Scalar::Util qw( blessed );
28
use Scalar::Util qw( blessed );
Lines 114-119 sub add { Link Here
114
                my $extended_attributes = delete $body->{extended_attributes} // [];
115
                my $extended_attributes = delete $body->{extended_attributes} // [];
115
116
116
                my $patron = Koha::Patron->new_from_api($body)->store;
117
                my $patron = Koha::Patron->new_from_api($body)->store;
118
119
                if ( $c->req->headers->header('x-koha-welcome') ) {
120
121
                    # if we manage to find a valid email address, send notice
122
                    if ( $patron->notice_email_address ) {
123
                        my $letter = GetPreparedLetter(
124
                            module      => 'members',
125
                            letter_code => 'WELCOME',
126
                            branchcode  => $patron->branchcode,
127
                            ,
128
                            lang   => $patron->lang || 'default',
129
                            tables => {
130
                                'branches'  => $patron->branchcode,
131
                                'borrowers' => $patron->borrowernumber,
132
                            },
133
                            want_librarian => 1,
134
                        );
135
136
                        if ($letter) {
137
                            my $message_id = EnqueueLetter(
138
                                {
139
                                    letter                 => $letter,
140
                                    borrowernumber         => $patron->id,
141
                                    to_address             => $patron->notice_email_address,
142
                                    message_transport_type => 'email'
143
                                }
144
                            );
145
                            SendQueuedMessages( { message_id => $message_id } );
146
                        }
147
                    }
148
                }
149
117
                $patron->extended_attributes(
150
                $patron->extended_attributes(
118
                    [
151
                    [
119
                        map { { code => $_->{type}, attribute => $_->{value} } }
152
                        map { { code => $_->{type}, attribute => $_->{value} } }
(-)a/api/v1/swagger/paths/patrons.yaml (+5 lines)
Lines 405-410 Link Here
405
        required: true
405
        required: true
406
        schema:
406
        schema:
407
          $ref: "../swagger.yaml#/definitions/patron"
407
          $ref: "../swagger.yaml#/definitions/patron"
408
      - name: x-koha-welcome
409
        in: header
410
        required: false
411
        description: If set to 'email' triggers the sending of a welcome email
412
        type: string
408
    consumes:
413
    consumes:
409
      - application/json
414
      - application/json
410
    produces:
415
    produces:
(-)a/t/db_dependent/api/v1/patrons.t (-30 / +29 lines)
Lines 35-40 use Koha::Exceptions::Patron::Attribute; Link Here
35
use Koha::Old::Patrons;
35
use Koha::Old::Patrons;
36
use Koha::Patron::Attributes;
36
use Koha::Patron::Attributes;
37
use Koha::Patron::Debarments qw( AddDebarment );
37
use Koha::Patron::Debarments qw( AddDebarment );
38
use Koha::Notice::Messages;
38
39
39
use JSON qw(encode_json);
40
use JSON qw(encode_json);
40
41
Lines 302-308 subtest 'add() tests' => sub { Link Here
302
    $schema->storage->txn_rollback;
303
    $schema->storage->txn_rollback;
303
304
304
    subtest 'librarian access tests' => sub {
305
    subtest 'librarian access tests' => sub {
305
        plan tests => 24;
306
        plan tests => 25;
306
307
307
        $schema->storage->txn_begin;
308
        $schema->storage->txn_begin;
308
309
Lines 316-343 subtest 'add() tests' => sub { Link Here
316
317
317
        # Mock early, so existing mandatory attributes don't break all the tests
318
        # Mock early, so existing mandatory attributes don't break all the tests
318
        my $mocked_patron = Test::MockModule->new('Koha::Patron');
319
        my $mocked_patron = Test::MockModule->new('Koha::Patron');
319
        $mocked_patron->mock(
320
            'extended_attributes',
321
            sub {
322
323
                if ($extended_attrs_exception) {
324
                    if ( $extended_attrs_exception eq 'Koha::Exceptions::Patron::Attribute::NonRepeatable'
325
                        or $extended_attrs_exception eq 'Koha::Exceptions::Patron::Attribute::UniqueIDConstraint'
326
                      )
327
                    {
328
                        $extended_attrs_exception->throw(
329
                            attribute => Koha::Patron::Attribute->new(
330
                                { code => $code, attribute => $attr }
331
                            )
332
                        );
333
                    }
334
                    else {
335
                        $extended_attrs_exception->throw( type => $type );
336
                    }
337
                }
338
                return [];
339
            }
340
        );
341
320
342
        my $patron = $builder->build_object({ class => 'Koha::Patrons' });
321
        my $patron = $builder->build_object({ class => 'Koha::Patrons' });
343
        my $newpatron = $patron->to_api;
322
        my $newpatron = $patron->to_api;
Lines 403-420 subtest 'add() tests' => sub { Link Here
403
        # Set a date-time field
382
        # Set a date-time field
404
        $newpatron->{last_seen} = output_pref({ dt => dt_from_string->add( days => -1 ), dateformat => 'rfc3339' });
383
        $newpatron->{last_seen} = output_pref({ dt => dt_from_string->add( days => -1 ), dateformat => 'rfc3339' });
405
384
406
        $t->post_ok("//$userid:$password@/api/v1/patrons" => json => $newpatron)
385
        $t->post_ok("//$userid:$password@/api/v1/patrons" => { 'x-koha-welcome' => 'email' } => json => $newpatron)
407
          ->status_is(201, 'Patron created successfully')
386
          ->status_is(201, 'Patron created successfully')
408
          ->header_like(
387
          ->header_like(
409
            Location => qr|^\/api\/v1\/patrons/\d*|,
388
            Location => qr|^\/api\/v1\/patrons/\d*|,
410
            'SWAGGER3.4.1'
389
            'SWAGGER3.4.1'
411
          )
390
          )
412
          ->json_has('/patron_id', 'got a patron_id')
391
          ->json_has('/patron_id', 'got a patron_id')
413
          ->json_is( '/cardnumber'    => $newpatron->{ cardnumber })
392
          ->json_is( '/cardnumber'    => $newpatron->{cardnumber})
414
          ->json_is( '/surname'       => $newpatron->{ surname })
393
          ->json_is( '/surname'       => $newpatron->{surname})
415
          ->json_is( '/firstname'     => $newpatron->{ firstname })
394
          ->json_is( '/firstname'     => $newpatron->{firstname})
416
          ->json_is( '/date_of_birth' => $newpatron->{ date_of_birth }, 'Date field set (Bug 28585)' )
395
          ->json_is( '/date_of_birth' => $newpatron->{date_of_birth}, 'Date field set (Bug 28585)' )
417
          ->json_is( '/last_seen'     => $newpatron->{ last_seen }, 'Date-time field set (Bug 28585)' );
396
          ->json_is( '/last_seen'     => $newpatron->{last_seen}, 'Date-time field set (Bug 28585)' );
397
398
        my $p = Koha::Patrons->find( { cardnumber => $newpatron->{cardnumber} } );
399
        is( Koha::Notice::Messages->search({ borrowernumber => $p->borrowernumber })->count, 1 , "Patron got welcome notice" );
418
400
419
        $newpatron->{userid} = undef; # force regeneration
401
        $newpatron->{userid} = undef; # force regeneration
420
        warning_like {
402
        warning_like {
Lines 428-433 subtest 'add() tests' => sub { Link Here
428
410
429
            plan tests => 19;
411
            plan tests => 19;
430
412
413
            $mocked_patron->mock(
414
                'extended_attributes',
415
                sub {
416
417
                    if ($extended_attrs_exception) {
418
                        if (   $extended_attrs_exception eq 'Koha::Exceptions::Patron::Attribute::NonRepeatable'
419
                            or $extended_attrs_exception eq 'Koha::Exceptions::Patron::Attribute::UniqueIDConstraint' )
420
                        {
421
                            $extended_attrs_exception->throw(
422
                                attribute => Koha::Patron::Attribute->new( { code => $code, attribute => $attr } ) );
423
                        } else {
424
                            $extended_attrs_exception->throw( type => $type );
425
                        }
426
                    }
427
                    return [];
428
                }
429
            );
430
431
            my $patrons_count = Koha::Patrons->search->count;
431
            my $patrons_count = Koha::Patrons->search->count;
432
432
433
            $extended_attrs_exception = 'Koha::Exceptions::Patron::MissingMandatoryExtendedAttribute';
433
            $extended_attrs_exception = 'Koha::Exceptions::Patron::MissingMandatoryExtendedAttribute';
434
- 

Return to bug 33690