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

(-)a/Koha/REST/V1/Checkouts.pm (+35 lines)
Lines 18-23 package Koha::REST::V1::Checkouts; Link Here
18
use Modern::Perl;
18
use Modern::Perl;
19
19
20
use Mojo::Base 'Mojolicious::Controller';
20
use Mojo::Base 'Mojolicious::Controller';
21
use Mojo::JSON;
21
22
22
use C4::Auth qw( haspermission );
23
use C4::Auth qw( haspermission );
23
use C4::Context;
24
use C4::Context;
Lines 127-132 sub renew { Link Here
127
    );
128
    );
128
}
129
}
129
130
131
sub renewability {
132
    my ($c, $args, $cb) = @_;
133
134
    my $user = $c->stash('koha.user');
135
136
    my $OpacRenewalAllowed;
137
    if ($user->borrowernumber == $borrowernumber) {
138
        $OpacRenewalAllowed = C4::Context->preference('OpacRenewalAllowed');
139
    }
140
141
    unless ($user && ($OpacRenewalAllowed
142
            || haspermission($user->userid, { circulate => "circulate_remaining_permissions" }))) {
143
        return $c->$cb({error => "You don't have the required permission"}, 403);
144
    }
145
146
    my $checkout_id = $args->{checkout_id};
147
    my $checkout = Koha::Issues->find($checkout_id);
148
149
    if (!$checkout) {
150
        return $c->$cb({
151
            error => "Checkout doesn't exist"
152
        }, 404);
153
    }
154
155
    my $borrowernumber = $checkout->borrowernumber;
156
    my $itemnumber = $checkout->itemnumber;
157
158
    my ($can_renew, $error) = C4::Circulation::CanBookBeRenewed(
159
        $borrowernumber, $itemnumber);
160
161
    return $c->$cb({ renewable => Mojo::JSON->true, error => undef }, 200) if $can_renew;
162
    return $c->$cb({ renewable => Mojo::JSON->false, error => $error }, 200);
163
}
164
130
=head3 _to_api
165
=head3 _to_api
131
166
132
Helper function that maps a hashref of Koha::Checkout attributes into REST api
167
Helper function that maps a hashref of Koha::Checkout attributes into REST api
(-)a/api/v1/swagger/definitions.json (+3 lines)
Lines 32-37 Link Here
32
  "patron_balance": {
32
  "patron_balance": {
33
    "$ref": "definitions/patron_balance.json"
33
    "$ref": "definitions/patron_balance.json"
34
  },
34
  },
35
  "renewability": {
36
    "$ref": "definitions/renewability.json"
37
  },
35
  "vendor": {
38
  "vendor": {
36
    "$ref": "definitions/vendor.json"
39
    "$ref": "definitions/vendor.json"
37
  },
40
  },
(-)a/api/v1/swagger/definitions/renewability.json (+13 lines)
Line 0 Link Here
1
{
2
  "type": "object",
3
  "properties": {
4
    "renewable": {
5
      "type": "boolean",
6
      "description": "Renewability status; true = renewable, false = not renewable"
7
    },
8
    "error": {
9
      "type": ["string", "null"],
10
      "description": "Description on false renewability."
11
    }
12
  }
13
}
(-)a/api/v1/swagger/paths.json (+3 lines)
Lines 41-46 Link Here
41
  "/libraries/{library_id}": {
41
  "/libraries/{library_id}": {
42
    "$ref": "paths/libraries.json#/~1libraries~1{library_id}"
42
    "$ref": "paths/libraries.json#/~1libraries~1{library_id}"
43
  },
43
  },
44
  "/checkouts/{checkout_id}/renewability": {
45
    "$ref": "paths/checkouts.json#/~1checkouts~1{checkout_id}~1renewability"
46
  },
44
  "/patrons": {
47
  "/patrons": {
45
    "$ref": "paths/patrons.json#/~1patrons"
48
    "$ref": "paths/patrons.json#/~1patrons"
46
  },
49
  },
(-)a/api/v1/swagger/paths/checkouts.json (+31 lines)
Lines 92-96 Link Here
92
        }
92
        }
93
      }
93
      }
94
    }
94
    }
95
  },
96
  "/checkouts/{checkout_id}/renewability": {
97
    "get": {
98
      "operationId": "renewabilityCheckout",
99
      "tags": ["patrons", "checkouts"],
100
      "parameters": [{
101
        "$ref": "../parameters.json#/checkoutIdPathParam"
102
      }],
103
      "produces": ["application/json"],
104
      "responses": {
105
        "200": {
106
          "description": "Checkout renewability",
107
          "schema": { "$ref": "../definitions.json#/renewability" }
108
        },
109
        "403": {
110
          "description": "Forbidden",
111
          "schema": { "$ref": "../definitions.json#/error" }
112
        },
113
        "404": {
114
          "description": "Checkout not found",
115
          "schema": { "$ref": "../definitions.json#/error" }
116
        }
117
      },
118
      "x-koha-authorization": {
119
        "allow-owner": true,
120
        "allow-guarantor": true,
121
        "permissions": {
122
          "circulate": "circulate_remaining_permissions"
123
        }
124
      }
125
    }
95
  }
126
  }
96
}
127
}
(-)a/t/db_dependent/api/v1/checkouts.t (-2 / +12 lines)
Lines 17-23 Link Here
17
17
18
use Modern::Perl;
18
use Modern::Perl;
19
19
20
use Test::More tests => 53;
20
use Test::More tests => 59;
21
use Test::MockModule;
21
use Test::MockModule;
22
use Test::Mojo;
22
use Test::Mojo;
23
use t::lib::Mocks;
23
use t::lib::Mocks;
Lines 144-149 $t->post_ok( "//$unauth_userid:$unauth_password@/api/v1/checkouts/" . $issue3->i Link Here
144
              required_permissions => { circulate => "circulate_remaining_permissions" }
144
              required_permissions => { circulate => "circulate_remaining_permissions" }
145
            });
145
            });
146
146
147
$tx = $t->ua->build_tx(GET => "/api/v1/checkouts/" . $issue2->issue_id . "/renewability");
148
$tx->req->cookies({name => 'CGISESSID', value => $patron_session->id});
149
$t->request_ok($tx)
150
  ->status_is(200)
151
  ->json_is({ renewable => Mojo::JSON->true, error => undef });
152
147
$t->post_ok( "//$userid:$password@/api/v1/checkouts/" . $issue2->issue_id . "/renewal" )
153
$t->post_ok( "//$userid:$password@/api/v1/checkouts/" . $issue2->issue_id . "/renewal" )
148
  ->status_is(201)
154
  ->status_is(201)
149
  ->json_is('/due_date' => output_pref({ dateformat => "rfc3339", dt => $expected_datedue}) )
155
  ->json_is('/due_date' => output_pref({ dateformat => "rfc3339", dt => $expected_datedue}) )
Lines 154-156 $t->post_ok( "//$userid:$password@/api/v1/checkouts/" . $issue1->issue_id . "/re Link Here
154
  ->status_is(403)
160
  ->status_is(403)
155
  ->json_is({ error => 'Renewal not authorized (too_many)' });
161
  ->json_is({ error => 'Renewal not authorized (too_many)' });
156
162
157
- 
163
$tx = $t->ua->build_tx(GET => "/api/v1/checkouts/" . $issue2->issue_id . "/renewability");
164
$tx->req->cookies({name => 'CGISESSID', value => $patron_session->id});
165
$t->request_ok($tx)
166
  ->status_is(200)
167
  ->json_is({ renewable => Mojo::JSON->false, error => 'too_many' });

Return to bug 17003