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 113-118 sub add { Link Here
113
                my $extended_attributes = delete $body->{extended_attributes} // [];
114
                my $extended_attributes = delete $body->{extended_attributes} // [];
114
115
115
                my $patron = Koha::Patron->new_from_api($body)->store;
116
                my $patron = Koha::Patron->new_from_api($body)->store;
117
118
                if ( $c->req->headers->header('x-koha-welcome') ) {
119
120
                    # if we manage to find a valid email address, send notice
121
                    if ( $patron->notice_email_address ) {
122
                        my $letter = GetPreparedLetter(
123
                            module      => 'members',
124
                            letter_code => 'WELCOME',
125
                            branchcode  => $patron->branchcode,
126
                            ,
127
                            lang   => $patron->lang || 'default',
128
                            tables => {
129
                                'branches'  => $patron->branchcode,
130
                                'borrowers' => $patron->borrowernumber,
131
                            },
132
                            want_librarian => 1,
133
                        );
134
135
                        if ($letter) {
136
                            my $message_id = EnqueueLetter(
137
                                {
138
                                    letter                 => $letter,
139
                                    borrowernumber         => $patron->id,
140
                                    to_address             => $patron->notice_email_address,
141
                                    message_transport_type => 'email'
142
                                }
143
                            );
144
                            SendQueuedMessages( { message_id => $message_id } );
145
                        }
146
                    }
147
                }
148
116
                $patron->extended_attributes(
149
                $patron->extended_attributes(
117
                    [
150
                    [
118
                        map { { code => $_->{type}, attribute => $_->{value} } }
151
                        map { { code => $_->{type}, attribute => $_->{value} } }
(-)a/api/v1/swagger/paths/patrons.yaml (+5 lines)
Lines 406-411 Link Here
406
        required: true
406
        required: true
407
        schema:
407
        schema:
408
          $ref: "../swagger.yaml#/definitions/patron"
408
          $ref: "../swagger.yaml#/definitions/patron"
409
      - name: x-koha-welcome
410
        in: header
411
        required: false
412
        description: If set to 'email' triggers the sending of a welcome email
413
        type: string
409
    consumes:
414
    consumes:
410
      - application/json
415
      - application/json
411
    produces:
416
    produces:
(-)a/t/db_dependent/api/v1/patrons.t (-30 / +34 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
                        {
422
                            $extended_attrs_exception->throw(
423
                                attribute => Koha::Patron::Attribute->new(
424
                                    { code => $code, attribute => $attr }
425
                                )
426
                            );
427
                        }
428
                        else {
429
                            $extended_attrs_exception->throw( type => $type );
430
                        }
431
                    }
432
                    return [];
433
                }
434
            );
435
431
            my $patrons_count = Koha::Patrons->search->count;
436
            my $patrons_count = Koha::Patrons->search->count;
432
437
433
            $extended_attrs_exception = 'Koha::Exceptions::Patron::MissingMandatoryExtendedAttribute';
438
            $extended_attrs_exception = 'Koha::Exceptions::Patron::MissingMandatoryExtendedAttribute';
434
- 

Return to bug 33690