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

(-)a/Koha/REST/V1/Checkout.pm (+51 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
    my $checkouts;
109
110
    if ($borrowernumber) {
111
112
      $checkouts = Koha::OldIssues->search({
113
        borrowernumber => $borrowernumber
114
      });
115
116
    } else {
117
118
      # Retrieve all the issues in the history, but only the issue_id due to possible perfomance issues
119
      $checkouts = Koha::OldIssues->search({}, {
120
        columns => [qw/issue_id/],
121
      });
122
123
    }
124
125
    $c->$cb($checkouts->unblessed, 200);
126
}
127
128
sub gethistory {
129
    my ($c, $args, $cb) = @_;
130
131
    my $user = $c->stash('koha.user');
132
    unless ($user && haspermission($user->userid, { circulate => "circulate_remaining_permissions" })) {
133
        return $c->$cb({error => "You don't have the required permission"}, 403);
134
    }
135
136
    my $checkout_id = $args->{checkout_id};
137
    my $checkout = Koha::OldIssues->find($checkout_id);
138
139
    if (!$checkout) {
140
        return $c->$cb({
141
            error => "Checkout doesn't exist"
142
        }, 404);
143
    }
144
145
    return $c->$cb($checkout->unblessed, 200);
146
}
147
97
1;
148
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 / +147 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 => 21;
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
36
my $schema = Koha::Database->schema;
37
$schema->storage->txn_begin;
38
my $dbh = C4::Context->dbh;
39
my $builder = t::lib::TestBuilder->new;
40
$dbh->{RaiseError} = 1;
41
42
$ENV{REMOTE_ADDR} = '127.0.0.1';
43
my $t = Test::Mojo->new('Koha::REST::V1');
44
45
$dbh->do('DELETE FROM issues');
46
$dbh->do('DELETE FROM items');
47
$dbh->do('DELETE FROM issuingrules');
48
my $loggedinuser = $builder->build({ source => 'Borrower' });
49
50
$dbh->do(q{
51
    INSERT INTO user_permissions (borrowernumber, module_bit, code)
52
    VALUES (?, 1, 'circulate_remaining_permissions')
53
}, undef, $loggedinuser->{borrowernumber});
54
55
my $session = C4::Auth::get_session('');
56
$session->param('number', $loggedinuser->{ borrowernumber });
57
$session->param('id', $loggedinuser->{ userid });
58
$session->param('ip', '127.0.0.1');
59
$session->param('lasttime', time());
60
$session->flush;
61
62
my $borrower = $builder->build({ source => 'Borrower' });
63
my $borrowernumber = $borrower->{borrowernumber};
64
65
my $branchcode = $builder->build({ source => 'Branch' })->{ branchcode };
66
my $module = new Test::MockModule('C4::Context');
67
$module->mock('userenv', sub { { branch => $branchcode } });
68
69
my $tx = $t->ua->build_tx(GET => "/api/v1/checkouts/history?borrowernumber=$borrowernumber");
70
$tx->req->cookies({name => 'CGISESSID', value => $session->id});
71
$t->request_ok($tx)
72
  ->status_is(200)
73
  ->json_is([]);
74
75
my $notexisting_borrowernumber = $borrowernumber + 1;
76
$tx = $t->ua->build_tx(GET => "/api/v1/checkouts/history?borrowernumber=$notexisting_borrowernumber");
77
$tx->req->cookies({name => 'CGISESSID', value => $session->id});
78
$t->request_ok($tx)
79
  ->status_is(200)
80
  ->json_is([]);
81
82
my $biblionumber = create_biblio('RESTful Web APIs');
83
my $itemnumber1 = create_item($biblionumber, 'TEST000001');
84
my $itemnumber2 = create_item($biblionumber, 'TEST000002');
85
86
my $date_due = DateTime->now->add(weeks => 2);
87
88
my $issueId = 7654321;
89
my $issue1;
90
$issue1 = Koha::OldIssue->new({ issue_id => $issueId, borrowernumber => $borrowernumber, itemnumber => $itemnumber1, date_due => $date_due});
91
$issue1->store();
92
93
my $date_due1 = Koha::DateUtils::dt_from_string( $issue1->date_due );
94
95
$tx = $t->ua->build_tx(GET => "/api/v1/checkouts/history?borrowernumber=$borrowernumber");
96
$tx->req->cookies({name => 'CGISESSID', value => $session->id});
97
$t->request_ok($tx)
98
  ->status_is(200)
99
  ->json_is('/0/borrowernumber' => $borrowernumber)
100
  ->json_is('/0/issue_id' => $issueId)
101
  ->json_is('/0/itemnumber' => $itemnumber1)
102
  ->json_is('/0/date_due' => $date_due1->ymd . ' ' . $date_due1->hms)
103
  ->json_hasnt('/1');
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('/date_due' => $date_due1->ymd . ' ' . $date_due1->hms);
110
111
$issue1->delete();
112
113
$tx = $t->ua->build_tx(GET => "/api/v1/checkouts/history/" . $issue1->issue_id);
114
$tx->req->cookies({name => 'CGISESSID', value => $session->id});
115
$t->request_ok($tx)
116
  ->status_is(404);
117
118
$tx = $t->ua->build_tx(GET => "/api/v1/checkouts/history?borrowernumber=$borrowernumber");
119
$tx->req->cookies({name => 'CGISESSID', value => $session->id});
120
$t->request_ok($tx)
121
  ->status_is(200)
122
  ->json_hasnt('/0');
123
124
sub create_biblio {
125
    my ($title) = @_;
126
127
    my $record = new MARC::Record;
128
    $record->append_fields(
129
        new MARC::Field('200', ' ', ' ', a => $title),
130
    );
131
132
    my ($biblionumber) = C4::Biblio::AddBiblio($record, '');
133
134
    return $biblionumber;
135
}
136
137
sub create_item {
138
    my ($biblionumber, $barcode) = @_;
139
140
    my $item = {
141
        barcode => $barcode,
142
    };
143
144
    my $itemnumber = C4::Items::AddItem($item, $biblionumber);
145
146
    return $itemnumber;
147
}

Return to bug 17005