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

(-)a/Koha/REST/V1/Checkout.pm (+47 lines)
Lines 22-27 use Mojo::Base 'Mojolicious::Controller'; Link Here
22
use C4::Auth qw( haspermission );
22
use C4::Auth qw( haspermission );
23
use C4::Circulation;
23
use C4::Circulation;
24
use Koha::Issues;
24
use Koha::Issues;
25
use Koha::OldIssues;
25
26
26
sub list {
27
sub list {
27
    my ($c, $args, $cb) = @_;
28
    my ($c, $args, $cb) = @_;
Lines 94-97 sub renew { Link Here
94
    return $c->$cb($checkout->unblessed, 200);
95
    return $c->$cb($checkout->unblessed, 200);
95
}
96
}
96
97
98
sub listhistory {
99
    my ($c, $args, $cb) = @_;
100
101
    my $user = $c->stash('koha.user');
102
    unless ($user && haspermission($user->userid, { circulate => "circulate_remaining_permissions" })) {
103
        return $c->$cb({error => "You don't have the required permission"}, 403);
104
    }
105
106
    my $borrowernumber = $c->param('borrowernumber');
107
108
    if ($borrowernumber) {
109
        return $c->$cb({
110
            error => "Patron doesn't exist"
111
        }, 404) unless Koha::Patrons->find($borrowernumber);
112
    }
113
114
    # Retrieve all the issues in the history, but only the issue_id due to possible perfomance issues
115
    my $checkouts = Koha::OldIssues->search( 
116
      $borrowernumber ?
117
      { borrowernumber => $borrowernumber } : {},
118
      { columns => [qw/issue_id/]}
119
    );
120
121
    $c->$cb($checkouts->unblessed, 200);
122
}
123
124
sub gethistory {
125
    my ($c, $args, $cb) = @_;
126
127
    my $user = $c->stash('koha.user');
128
    unless ($user && haspermission($user->userid, { circulate => "circulate_remaining_permissions" })) {
129
        return $c->$cb({error => "You don't have the required permission"}, 403);
130
    }
131
132
    my $checkout_id = $args->{checkout_id};
133
    my $checkout = Koha::OldIssues->find($checkout_id);
134
135
    if (!$checkout) {
136
        return $c->$cb({
137
            error => "Checkout doesn't exist"
138
        }, 404);
139
    }
140
141
    return $c->$cb($checkout->unblessed, 200);
142
}
143
97
1;
144
1;
(-)a/api/v1/definitions/checkout.json (-2 / +2 lines)
Lines 6-16 Link Here
6
          "description": "internally assigned checkout identifier"
6
          "description": "internally assigned checkout identifier"
7
        },
7
        },
8
        "borrowernumber": {
8
        "borrowernumber": {
9
            "type": "string",
9
            "type": ["string", "null"],
10
            "description": "internally assigned user identifier"
10
            "description": "internally assigned user identifier"
11
        },
11
        },
12
        "itemnumber": {
12
        "itemnumber": {
13
            "type": "string",
13
            "type": ["string", "null"],
14
            "description": "internally assigned item identifier"
14
            "description": "internally assigned item identifier"
15
        },
15
        },
16
        "date_due": {
16
        "date_due": {
(-)a/api/v1/swagger.json (+66 lines)
Lines 426-431 Link Here
426
          }
426
          }
427
        }
427
        }
428
      }
428
      }
429
    },
430
    "/checkouts/history": {
431
      "get": {
432
        "operationId": "listhistoryCheckouts",
433
        "tags": ["borrowers", "checkouts"],
434
        "parameters": [
435
          {
436
            "name": "borrowernumber",
437
            "in": "query",
438
            "description": "Internal patron identifier",
439
            "required": false,
440
            "type": "integer"
441
          }
442
        ],
443
        "produces": [
444
          "application/json"
445
        ],
446
        "responses": {
447
          "200": {
448
            "description": "A list of checkouts history",
449
            "schema": {
450
              "$ref": "#/definitions/checkouts"
451
            }
452
          },
453
          "403": {
454
            "description": "Access forbidden",
455
            "schema": { "$ref": "#/definitions/error" }
456
          },
457
          "404": {
458
            "description": "Borrower not found",
459
            "schema": {
460
              "$ref": "#/definitions/error"
461
            }
462
          }
463
        }
464
      }
465
    },
466
    "/checkouts/history/{checkout_id}": {
467
      "get": {
468
        "operationId": "gethistoryCheckout",
469
        "tags": ["borrowers", "checkouts"],
470
        "parameters": [
471
          {
472
            "name": "checkout_id",
473
            "in": "path",
474
            "description": "Internal checkout identifier",
475
            "required": true,
476
            "type": "integer"
477
          }
478
        ],
479
        "produces": ["application/json"],
480
        "responses": {
481
          "200": {
482
            "description": "Got borrower's checkout",
483
            "schema": { "$ref": "#/definitions/checkout" }
484
          },
485
          "403": {
486
            "description": "Access forbidden",
487
            "schema": { "$ref": "#/definitions/error" }
488
          },
489
          "404": {
490
            "description": "Checkout not found",
491
            "schema": { "$ref": "#/definitions/error" }
492
          }
493
        }
494
      }
429
    }
495
    }
430
  },
496
  },
431
  "definitions": {
497
  "definitions": {
(-)a/t/db_dependent/api/v1/checkoutshistory.t (-1 / +160 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 under the
6
# terms of the GNU General Public License as published by the Free Software
7
# Foundation; either version 3 of the License, or (at your option) any later
8
# version.
9
#
10
# Koha is distributed in the hope that it will be useful, but WITHOUT ANY
11
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12
# A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
13
#
14
# You should have received a copy of the GNU General Public License along
15
# with Koha; if not, write to the Free Software Foundation, Inc.,
16
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17
18
use Modern::Perl;
19
20
use Test::More tests => 27;
21
use Test::MockModule;
22
use Test::Mojo;
23
use t::lib::TestBuilder;
24
25
use DateTime;
26
use MARC::Record;
27
28
use C4::Context;
29
use C4::Biblio;
30
use C4::Circulation;
31
use C4::Items;
32
33
use Koha::Database;
34
use Koha::Patron;
35
use Koha::OldIssue;
36
use Koha::OldIssues;
37
38
my $schema = Koha::Database->schema;
39
$schema->storage->txn_begin;
40
my $dbh = C4::Context->dbh;
41
my $builder = t::lib::TestBuilder->new;
42
$dbh->{RaiseError} = 1;
43
44
$ENV{REMOTE_ADDR} = '127.0.0.1';
45
my $t = Test::Mojo->new('Koha::REST::V1');
46
47
$dbh->do('DELETE FROM issues');
48
$dbh->do('DELETE FROM items');
49
$dbh->do('DELETE FROM issuingrules');
50
my $loggedinuser = $builder->build({ source => 'Borrower' });
51
52
$dbh->do(q{
53
    INSERT INTO user_permissions (borrowernumber, module_bit, code)
54
    VALUES (?, 1, 'circulate_remaining_permissions')
55
}, undef, $loggedinuser->{borrowernumber});
56
57
my $session = C4::Auth::get_session('');
58
$session->param('number', $loggedinuser->{ borrowernumber });
59
$session->param('id', $loggedinuser->{ userid });
60
$session->param('ip', '127.0.0.1');
61
$session->param('lasttime', time());
62
$session->flush;
63
64
my $borrower = $builder->build({ source => 'Borrower' });
65
my $borrowernumber = $borrower->{borrowernumber};
66
67
my $branchcode = $builder->build({ source => 'Branch' })->{ branchcode };
68
my $module = new Test::MockModule('C4::Context');
69
$module->mock('userenv', sub { { branch => $branchcode } });
70
71
my $tx = $t->ua->build_tx(GET => "/api/v1/checkouts/history?borrowernumber=$borrowernumber");
72
$tx->req->cookies({name => 'CGISESSID', value => $session->id});
73
$t->request_ok($tx)
74
  ->status_is(200)
75
  ->json_is([]);
76
77
my $notexisting_borrowernumber = $borrowernumber + 1;
78
$tx = $t->ua->build_tx(GET => "/api/v1/checkouts/history?borrowernumber=$notexisting_borrowernumber");
79
$tx->req->cookies({name => 'CGISESSID', value => $session->id});
80
$t->request_ok($tx)
81
  ->status_is(404)
82
  ->json_has('/error');
83
84
my $biblionumber = create_biblio('RESTful Web APIs');
85
my $itemnumber1 = create_item($biblionumber, 'TEST000001');
86
my $itemnumber2 = create_item($biblionumber, 'TEST000002');
87
88
my $date_due = DateTime->now->add(weeks => 2);
89
90
my $issueId = Koha::OldIssues->count({}) + 1500;
91
my $issue1;
92
$issue1 = Koha::OldIssue->new({ issue_id => $issueId, borrowernumber => $borrowernumber, itemnumber => $itemnumber1, date_due => $date_due});
93
$issue1->store();
94
95
my $date_due1 = Koha::DateUtils::dt_from_string( $issue1->date_due );
96
97
$tx = $t->ua->build_tx(GET => "/api/v1/checkouts/history?borrowernumber=$borrowernumber");
98
$tx->req->cookies({name => 'CGISESSID', value => $session->id});
99
$t->request_ok($tx)
100
  ->status_is(200)
101
  ->json_is('/0/issue_id' => $issueId)
102
  ->json_hasnt('/1')
103
  ->json_hasnt('/error');
104
105
$tx = $t->ua->build_tx(GET => "/api/v1/checkouts/history/" . $issue1->issue_id);
106
$tx->req->cookies({name => 'CGISESSID', value => $session->id});
107
$t->request_ok($tx)
108
  ->status_is(200)
109
  ->json_is('/borrowernumber' => $borrowernumber)
110
  ->json_is('/itemnumber' => $itemnumber1)
111
  ->json_is('/date_due' => $date_due1->ymd . ' ' . $date_due1->hms)
112
  ->json_hasnt('/error');
113
114
$issue1->delete();
115
116
$tx = $t->ua->build_tx(GET => "/api/v1/checkouts/history/" . $issue1->issue_id);
117
$tx->req->cookies({name => 'CGISESSID', value => $session->id});
118
$t->request_ok($tx)
119
  ->status_is(404)
120
  ->json_has('/error');
121
122
$tx = $t->ua->build_tx(GET => "/api/v1/checkouts/history?borrowernumber=$borrowernumber");
123
$tx->req->cookies({name => 'CGISESSID', value => $session->id});
124
$t->request_ok($tx)
125
  ->status_is(200)
126
  ->json_hasnt('/0')
127
  ->json_hasnt('/error');
128
129
Koha::Patrons->find($borrowernumber)->delete();
130
131
$tx = $t->ua->build_tx(GET => "/api/v1/checkouts/history?borrowernumber=$borrowernumber");
132
$tx->req->cookies({name => 'CGISESSID', value => $session->id});
133
$t->request_ok($tx)
134
  ->status_is(404)
135
  ->json_has('/error');
136
137
sub create_biblio {
138
    my ($title) = @_;
139
140
    my $record = new MARC::Record;
141
    $record->append_fields(
142
        new MARC::Field('200', ' ', ' ', a => $title),
143
    );
144
145
    my ($biblionumber) = C4::Biblio::AddBiblio($record, '');
146
147
    return $biblionumber;
148
}
149
150
sub create_item {
151
    my ($biblionumber, $barcode) = @_;
152
153
    my $item = {
154
        barcode => $barcode,
155
    };
156
157
    my $itemnumber = C4::Items::AddItem($item, $biblionumber);
158
159
    return $itemnumber;
160
}

Return to bug 17005