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

(-)a/Koha/REST/V1/Patrons/Checkouts.pm (+66 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 => {
47
                error => 'Patron not found'
48
            }
49
        );
50
    }
51
52
    return try {
53
54
        my $checkouts = $c->objects->search( $patron->checkouts );
55
56
        return $c->render(
57
            status  => 200,
58
            openapi => $checkouts
59
        );
60
    }
61
    catch {
62
        $c->unhandled_exception($_);
63
    };
64
}
65
66
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 / +93 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
29
my $schema  = Koha::Database->new->schema;
30
my $builder = t::lib::TestBuilder->new();
31
32
my $t = Test::Mojo->new('Koha::REST::V1');
33
34
t::lib::Mocks::mock_preference( 'RESTBasicAuth', 1 );
35
36
subtest 'Patron checkouts list() tests' => sub {
37
38
    plan tests => 13;
39
40
    $schema->storage->txn_begin;
41
42
    my $librarian = $builder->build_object({
43
        class => 'Koha::Patrons',
44
        value => { flags => 2 }
45
    });
46
    my $password = 'thePassword123';
47
    $librarian->set_password({ password => $password, skip_validation => 1 });
48
    my $userid = $librarian->userid;
49
50
    my $patron = $builder->build_object({
51
        class => 'Koha::Patrons',
52
        value => { flags => 0 }
53
    });
54
55
    $t->get_ok("//$userid:$password@/api/v1/patrons/" . $patron->id . '/checkouts')
56
      ->status_is(200)
57
      ->json_is([]);
58
59
    my $date_due = DateTime->now->add(weeks => 2);
60
    my $item1 = $builder->build_sample_item;
61
    my $item2 = $builder->build_sample_item;
62
63
    my $issue1 = C4::Circulation::AddIssue($patron, $item1->barcode, $date_due);
64
    my $issue2 = C4::Circulation::AddIssue($patron, $item2->barcode, $date_due);
65
66
    $t->get_ok("//$userid:$password@/api/v1/patrons/" . $patron->id . '/checkouts')
67
      ->status_is(200)
68
      ->json_is('/0/item_id' => $item1->itemnumber)
69
      ->json_is('/1/item_id' => $item2->itemnumber);
70
71
    my $non_existent_patron = $builder->build_object({ class => 'Koha::Patrons' });
72
    my $non_existent_patron_id = $non_existent_patron->id;
73
    $non_existent_patron->delete;
74
75
    $t->get_ok("//$userid:$password@/api/v1/patrons/" . $non_existent_patron_id . '/checkouts')
76
      ->status_is( 404 )
77
      ->json_is( '/error' => 'Patron not found' );
78
79
    my $unauthorized_patron = $builder->build_object({
80
        class => 'Koha::Patrons',
81
        value => { flags => 0 }
82
    });
83
    my $unauthorized_password = 'thePassword456';
84
85
    $unauthorized_patron->set_password({
86
            password => $unauthorized_password, skip_validation => 1
87
    });
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 )
92
      ->json_is( '/error' => 'Authorization failure. Missing required permission(s).' );
93
}

Return to bug 22613