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

(-)a/Koha/REST/V1/Patrons/Checkouts.pm (+61 lines)
Line 0 Link Here
1
package Koha::REST::V1::Patrons::Checkouts;
2
3
# This file is part of Koha.
4
#
5
# Koha is free software; you can redistribute it and/or modify it
6
# under the terms of the GNU General Public License as published by
7
# the Free Software Foundation; either version 3 of the License, or
8
# (at your option) any later version.
9
#
10
# Koha is distributed in the hope that it will be useful, but
11
# WITHOUT ANY WARRANTY; without even the implied warranty of
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
# GNU General Public License for more details.
14
#
15
# You should have received a copy of the GNU General Public License
16
# along with Koha; if not, see <http://www.gnu.org/licenses>.
17
18
use Modern::Perl;
19
20
use Mojo::Base 'Mojolicious::Controller';
21
22
use Koha::Patrons;
23
24
=head1 NAME
25
26
Koha::REST::V1::Patrons::Checkouts
27
28
=head1 API
29
30
=head2 Methods
31
32
=head3 list
33
34
Controller function that handles listing Koha::Checkout objects for the requested patron
35
36
=cut
37
38
sub list {
39
    my $c = shift->openapi->valid_input or return;
40
41
    my $patron = Koha::Patrons->find( $c->param('patron_id') );
42
43
    unless ($patron) {
44
        return $c->render(
45
            status  => 404,
46
            openapi => { error => 'Patron not found' }
47
        );
48
    }
49
50
    return try {
51
52
        return $c->render(
53
            status  => 200,
54
            openapi => $c->objects->search( $patron->checkouts ),
55
        );
56
    } catch {
57
        $c->unhandled_exception($_);
58
    };
59
}
60
61
1;
(-)a/api/v1/swagger/paths/patrons_checkouts.yaml (+63 lines)
Line 0 Link Here
1
---
2
"/patrons/{patron_id}/checkouts":
3
  get:
4
    x-mojo-to: Patrons::Checkouts#list
5
    operationId: getPatronCheckouts
6
    tags:
7
      - checkouts
8
    summary: List checkouts for a patron
9
    parameters:
10
      - $ref: "../swagger.yaml#/parameters/patron_id_pp"
11
      - $ref: "../swagger.yaml#/parameters/match"
12
      - $ref: "../swagger.yaml#/parameters/order_by"
13
      - $ref: "../swagger.yaml#/parameters/page"
14
      - $ref: "../swagger.yaml#/parameters/per_page"
15
      - $ref: "../swagger.yaml#/parameters/q_param"
16
      - $ref: "../swagger.yaml#/parameters/q_body"
17
      - $ref: "../swagger.yaml#/parameters/request_id_header"
18
      - name: x-koha-embed
19
        in: header
20
        required: false
21
        description: Embed list sent as a request header
22
        type: array
23
        items:
24
          type: string
25
          enum:
26
            - cancellation_requested
27
        collectionFormat: csv
28
    produces:
29
      - application/json
30
    responses:
31
      "200":
32
        description: The patron checkouts
33
        schema:
34
          type: array
35
          items:
36
            $ref: "../swagger.yaml#/definitions/checkout"
37
      "401":
38
        description: Authentication required
39
        schema:
40
          $ref: "../swagger.yaml#/definitions/error"
41
      "403":
42
        description: Access forbidden
43
        schema:
44
          $ref: "../swagger.yaml#/definitions/error"
45
      "404":
46
        description: Patron not found
47
        schema:
48
          $ref: "../swagger.yaml#/definitions/error"
49
      "500":
50
        description: |
51
          Internal server error. Possible `error_code` attribute values:
52
53
          * `internal_server_error`
54
        schema:
55
          $ref: "../swagger.yaml#/definitions/error"
56
      "503":
57
        description: Under maintenance
58
        schema:
59
          $ref: "../swagger.yaml#/definitions/error"
60
    x-koha-authorization:
61
      permissions:
62
        - circulate: circulate_remaining_permissions
63
        - circulate: manage_bookings
(-)a/api/v1/swagger/swagger.yaml (+2 lines)
Lines 427-432 paths: Link Here
427
    $ref: "./paths/patrons_extended_attributes.yaml#/~1patrons~1{patron_id}~1extended_attributes~1{extended_attribute_id}"
427
    $ref: "./paths/patrons_extended_attributes.yaml#/~1patrons~1{patron_id}~1extended_attributes~1{extended_attribute_id}"
428
  "/patrons/{patron_id}/holds":
428
  "/patrons/{patron_id}/holds":
429
    $ref: "./paths/patrons_holds.yaml#/~1patrons~1{patron_id}~1holds"
429
    $ref: "./paths/patrons_holds.yaml#/~1patrons~1{patron_id}~1holds"
430
  "/patrons/{patron_id}/checkouts":
431
    $ref: "./paths/patrons_checkouts.yaml#/~1patrons~1{patron_id}~1checkouts"
430
  "/patrons/{patron_id}/password":
432
  "/patrons/{patron_id}/password":
431
    $ref: "./paths/patrons_password.yaml#/~1patrons~1{patron_id}~1password"
433
    $ref: "./paths/patrons_password.yaml#/~1patrons~1{patron_id}~1password"
432
  "/patrons/{patron_id}/password/expiration_date":
434
  "/patrons/{patron_id}/password/expiration_date":
(-)a/t/db_dependent/api/v1/patrons_checkouts.t (-1 / +92 lines)
Line 0 Link Here
0
- 
1
#!/usr/bin/env perl
2
3
# This file is part of Koha.
4
#
5
# Koha is free software; you can redistribute it and/or modify it
6
# under the terms of the GNU General Public License as published by
7
# the Free Software Foundation; either version 3 of the License, or
8
# (at your option) any later version.
9
#
10
# Koha is distributed in the hope that it will be useful, but
11
# WITHOUT ANY WARRANTY; without even the implied warranty of
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
# GNU General Public License for more details.
14
#
15
# You should have received a copy of the GNU General Public License
16
# along with Koha; if not, see <http://www.gnu.org/licenses>.
17
18
use Modern::Perl;
19
20
use Test::More tests => 1;
21
use Test::MockModule;
22
use Test::Mojo;
23
24
use t::lib::TestBuilder;
25
use t::lib::Mocks;
26
27
use Koha::Database;
28
use Koha::DateUtils qw(dt_from_string);
29
30
my $schema  = Koha::Database->new->schema;
31
my $builder = t::lib::TestBuilder->new();
32
33
my $t = Test::Mojo->new('Koha::REST::V1');
34
35
t::lib::Mocks::mock_preference( 'RESTBasicAuth', 1 );
36
37
subtest 'Patron checkouts list() tests' => sub {
38
39
    plan tests => 13;
40
41
    $schema->storage->txn_begin;
42
43
    my $librarian = $builder->build_object(
44
        {
45
            class => 'Koha::Patrons',
46
            value => { flags => 2 }
47
        }
48
    );
49
    my $password = 'thePassword123';
50
    $librarian->set_password( { password => $password, skip_validation => 1 } );
51
    my $userid = $librarian->userid;
52
53
    my $patron = $builder->build_object(
54
        {
55
            class => 'Koha::Patrons',
56
            value => { flags => 0 }
57
        }
58
    );
59
60
    $t->get_ok( "//$userid:$password@/api/v1/patrons/" . $patron->id . '/checkouts' )->status_is(200)->json_is( [] );
61
62
    my $date_due = dt_from_string->add( weeks => 2 );
63
    my $item1    = $builder->build_sample_item;
64
    my $item2    = $builder->build_sample_item;
65
66
    my $issue1 = C4::Circulation::AddIssue( $patron, $item1->barcode, $date_due );
67
    my $issue2 = C4::Circulation::AddIssue( $patron, $item2->barcode, $date_due );
68
69
    $t->get_ok( "//$userid:$password@/api/v1/patrons/" . $patron->id . '/checkouts' )->status_is(200)
70
        ->json_is( '/0/item_id' => $item1->itemnumber )->json_is( '/1/item_id' => $item2->itemnumber );
71
72
    my $non_existent_patron    = $builder->build_object( { class => 'Koha::Patrons' } );
73
    my $non_existent_patron_id = $non_existent_patron->id;
74
    $non_existent_patron->delete;
75
76
    $t->get_ok( "//$userid:$password@/api/v1/patrons/" . $non_existent_patron_id . '/checkouts' )->status_is(404)
77
        ->json_is( '/error' => 'Patron not found' );
78
79
    my $unauthorized_patron = $builder->build_object(
80
        {
81
            class => 'Koha::Patrons',
82
            value => { flags => 0 }
83
        }
84
    );
85
    my $unauthorized_password = 'thePassword456';
86
87
    $unauthorized_patron->set_password( { password => $unauthorized_password, skip_validation => 1 } );
88
    my $unauthorized_userid = $unauthorized_patron->userid;
89
90
    $t->get_ok( "//$unauthorized_userid:$unauthorized_password@/api/v1/patrons/" . $patron->id . '/checkouts' )
91
        ->status_is(403)->json_is( '/error' => 'Authorization failure. Missing required permission(s).' );
92
    }

Return to bug 22613