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

(-)a/Koha/REST/V1/Patrons.pm (-33 / +56 lines)
Lines 273-286 sub update { Link Here
273
            and (  exists $body->{email}
273
            and (  exists $body->{email}
274
                or exists $body->{secondary_email}
274
                or exists $body->{secondary_email}
275
                or exists $body->{altaddress_email} )
275
                or exists $body->{altaddress_email} )
276
          )
276
            )
277
        {
277
        {
278
            foreach my $email_field ( qw(email secondary_email altaddress_email) ) {
278
            foreach my $email_field (qw(email secondary_email altaddress_email)) {
279
                my $exists_email = exists $body->{$email_field};
279
                my $exists_email = exists $body->{$email_field};
280
                next unless $exists_email;
280
                next unless $exists_email;
281
281
282
                # exists, verify if we are asked to change it
282
                # exists, verify if we are asked to change it
283
                my $put_email      = $body->{$email_field};
283
                my $put_email = $body->{$email_field};
284
284
                # As of writing this patch, 'email' is the only unmapped field
285
                # As of writing this patch, 'email' is the only unmapped field
285
                # (i.e. it preserves its name, hence this fallback)
286
                # (i.e. it preserves its name, hence this fallback)
286
                my $db_email_field = $patron->to_api_mapping->{$email_field} // 'email';
287
                my $db_email_field = $patron->to_api_mapping->{$email_field} // 'email';
Lines 289-316 sub update { Link Here
289
                return $c->render(
290
                return $c->render(
290
                    status  => 403,
291
                    status  => 403,
291
                    openapi => { error => "Not enough privileges to change a superlibrarian's email" }
292
                    openapi => { error => "Not enough privileges to change a superlibrarian's email" }
292
                  )
293
                    )
293
                  unless ( !defined $put_email and !defined $db_email )
294
                    unless ( !defined $put_email and !defined $db_email )
294
                  or (  defined $put_email
295
                    or (defined $put_email
295
                    and defined $db_email
296
                    and defined $db_email
296
                    and $put_email eq $db_email );
297
                    and $put_email eq $db_email );
297
            }
298
            }
298
        }
299
        }
299
300
300
        $patron->set_from_api($body)->store;
301
        $patron->_result->result_source->schema->txn_do(
301
        $patron->discard_changes;
302
            sub {
302
        return $c->render(
303
                # Remove `extended_attributes` before storing
303
            status  => 200,
304
                my $extended_attributes = delete $body->{extended_attributes};
304
            openapi => $c->objects->to_api($patron),
305
306
                if ($extended_attributes) {
307
                    my $rs = $patron->extended_attributes(
308
                        [ map { { code => $_->{type}, attribute => $_->{value} } } @{$extended_attributes} ] );
309
                }
310
311
                $patron->set_from_api($body)->store;
312
                $patron->discard_changes;
313
314
                return $c->render(
315
                    status  => 200,
316
                    openapi => $c->objects->to_api($patron),
317
                );
318
            }
305
        );
319
        );
306
    }
320
    } catch {
307
    catch {
308
        unless ( blessed $_ && $_->can('rethrow') ) {
321
        unless ( blessed $_ && $_->can('rethrow') ) {
309
            return $c->render(
322
            return $c->render(
310
                status  => 500,
323
                status  => 500,
311
                openapi => {
324
                openapi => { error => "Something went wrong, check Koha logs for details." }
312
                    error => "Something went wrong, check Koha logs for details."
313
                }
314
            );
325
            );
315
        }
326
        }
316
        if ( $_->isa('Koha::Exceptions::Object::DuplicateID') ) {
327
        if ( $_->isa('Koha::Exceptions::Object::DuplicateID') ) {
Lines 318-339 sub update { Link Here
318
                status  => 409,
329
                status  => 409,
319
                openapi => { error => $_->error, conflict => $_->duplicate_id }
330
                openapi => { error => $_->error, conflict => $_->duplicate_id }
320
            );
331
            );
321
        }
332
        } elsif ( $_->isa('Koha::Exceptions::Patron::InvalidUserid') ) {
322
        elsif ( $_->isa('Koha::Exceptions::Patron::InvalidUserid') ) {
323
            return $c->render(
333
            return $c->render(
324
                status  => 400,
334
                status  => 400,
325
                openapi => { error => "Problem with ". $_->userid }
335
                openapi => { error => "Problem with " . $_->userid }
326
            );
336
            );
327
        }
337
        } elsif ( $_->isa('Koha::Exceptions::Object::FKConstraint') ) {
328
        elsif ( $_->isa('Koha::Exceptions::Object::FKConstraint') ) {
329
            return $c->render(
338
            return $c->render(
330
                status  => 400,
339
                status  => 400,
331
                openapi => { error => "Given " .
340
                openapi => { error => "Given " . $patron->to_api_mapping->{ $_->broken_fk } . " does not exist" }
332
                            $patron->to_api_mapping->{$_->broken_fk}
333
                            . " does not exist" }
334
            );
341
            );
335
        }
342
        } elsif ( $_->isa('Koha::Exceptions::MissingParameter') ) {
336
        elsif ( $_->isa('Koha::Exceptions::MissingParameter') ) {
337
            return $c->render(
343
            return $c->render(
338
                status  => 400,
344
                status  => 400,
339
                openapi => {
345
                openapi => {
Lines 341-348 sub update { Link Here
341
                    parameters => $_->parameter
347
                    parameters => $_->parameter
342
                }
348
                }
343
            );
349
            );
344
        }
350
        } elsif ( $_->isa('Koha::Exceptions::BadParameter') ) {
345
        elsif ( $_->isa('Koha::Exceptions::BadParameter') ) {
346
            return $c->render(
351
            return $c->render(
347
                status  => 400,
352
                status  => 400,
348
                openapi => {
353
                openapi => {
Lines 350-365 sub update { Link Here
350
                    parameters => $_->parameter
355
                    parameters => $_->parameter
351
                }
356
                }
352
            );
357
            );
353
        }
358
        } elsif ( $_->isa('Koha::Exceptions::NoChanges') ) {
354
        elsif ( $_->isa('Koha::Exceptions::NoChanges') ) {
355
            return $c->render(
359
            return $c->render(
356
                status  => 204,
360
                status  => 204,
357
                openapi => { error => "No changes have been made" }
361
                openapi => { error => "No changes have been made" }
358
            );
362
            );
363
        } elsif ( $_->isa('Koha::Exceptions::Patron::Attribute::InvalidType') ) {
364
            return $c->render(
365
                status  => 400,
366
                openapi => { error => "$_", error_code => 'invalid_attribute_type' }
367
            );
368
        } elsif ( $_->isa('Koha::Exceptions::Patron::Attribute::UniqueIDConstraint') ) {
369
            return $c->render(
370
                status  => 400,
371
                openapi => { error => "$_", error_code => 'attribute_not_unique' }
372
            );
373
        } elsif ( $_->isa('Koha::Exceptions::Patron::Attribute::NonRepeatable') ) {
374
            return $c->render(
375
                status  => 400,
376
                openapi => { error => "$_", error_code => 'non_repeatable_attribute' }
377
            );
378
        } elsif ( $_->isa('Koha::Exceptions::Patron::MissingMandatoryExtendedAttribute') ) {
379
            return $c->render(
380
                status  => 400,
381
                openapi => { error => "$_", error_code => 'missing_mandatory_attribute' }
382
            );
359
        }
383
        }
360
        else {
384
361
            $c->unhandled_exception($_);
385
        $c->unhandled_exception($_);
362
        }
363
    };
386
    };
364
}
387
}
365
388
(-)a/api/v1/swagger/paths/patrons.yaml (-2 / +17 lines)
Lines 535-540 Link Here
535
        required: true
535
        required: true
536
        schema:
536
        schema:
537
          $ref: "../swagger.yaml#/definitions/patron"
537
          $ref: "../swagger.yaml#/definitions/patron"
538
      - name: x-koha-embed
539
        in: header
540
        required: false
541
        description: Embed list sent as a request header
542
        type: array
543
        items:
544
          type: string
545
          enum:
546
            - extended_attributes
547
        collectionFormat: csv
538
    consumes:
548
    consumes:
539
      - application/json
549
      - application/json
540
    produces:
550
    produces:
Lines 546-552 Link Here
546
          items:
556
          items:
547
            $ref: "../swagger.yaml#/definitions/patron"
557
            $ref: "../swagger.yaml#/definitions/patron"
548
      "400":
558
      "400":
549
        description: Bad parameter
559
        description: |
560
          Bad parameter. Possible `error_code` attribute values:
561
562
            * `invalid_attribute_type`
563
            * `attribute_not_unique`
564
            * `non_repeatable_attribute`
565
            * `missing_mandatory_attribute`
550
        schema:
566
        schema:
551
          $ref: "../swagger.yaml#/definitions/error"
567
          $ref: "../swagger.yaml#/definitions/error"
552
      "403":
568
      "403":
553
- 

Return to bug 36505