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 44-49 Link Here
44
  "/libraries/{library_id}": {
44
  "/libraries/{library_id}": {
45
    "$ref": "paths/libraries.json#/~1libraries~1{library_id}"
45
    "$ref": "paths/libraries.json#/~1libraries~1{library_id}"
46
  },
46
  },
47
  "/checkouts/{checkout_id}/renewability": {
48
    "$ref": "paths/checkouts.json#/~1checkouts~1{checkout_id}~1renewability"
49
  },
47
  "/patrons": {
50
  "/patrons": {
48
    "$ref": "paths/patrons.json#/~1patrons"
51
    "$ref": "paths/patrons.json#/~1patrons"
49
  },
52
  },
(-)a/api/v1/swagger/paths/checkouts.json (+31 lines)
Lines 96-100 Link Here
96
        }
96
        }
97
      }
97
      }
98
    }
98
    }
99
  },
100
  "/checkouts/{checkout_id}/renewability": {
101
    "get": {
102
      "operationId": "renewabilityCheckout",
103
      "tags": ["patrons", "checkouts"],
104
      "parameters": [{
105
        "$ref": "../parameters.json#/checkoutIdPathParam"
106
      }],
107
      "produces": ["application/json"],
108
      "responses": {
109
        "200": {
110
          "description": "Checkout renewability",
111
          "schema": { "$ref": "../definitions.json#/renewability" }
112
        },
113
        "403": {
114
          "description": "Forbidden",
115
          "schema": { "$ref": "../definitions.json#/error" }
116
        },
117
        "404": {
118
          "description": "Checkout not found",
119
          "schema": { "$ref": "../definitions.json#/error" }
120
        }
121
      },
122
      "x-koha-authorization": {
123
        "allow-owner": true,
124
        "allow-guarantor": true,
125
        "permissions": {
126
          "circulate": "circulate_remaining_permissions"
127
        }
128
      }
129
    }
99
  }
130
  }
100
}
131
}
(-)a/t/db_dependent/api/v1/checkouts.t (-1 / +11 lines)
Lines 169-174 $t->post_ok( "//$unauth_userid:$unauth_password@/api/v1/checkouts/" . $issue3->i Link Here
169
              required_permissions => { circulate => "circulate_remaining_permissions" }
169
              required_permissions => { circulate => "circulate_remaining_permissions" }
170
            });
170
            });
171
171
172
$tx = $t->ua->build_tx(GET => "/api/v1/checkouts/" . $issue2->issue_id . "/renewability");
173
$tx->req->cookies({name => 'CGISESSID', value => $patron_session->id});
174
$t->request_ok($tx)
175
  ->status_is(200)
176
  ->json_is({ renewable => Mojo::JSON->true, error => undef });
177
172
$t->post_ok( "//$userid:$password@/api/v1/checkouts/" . $issue2->issue_id . "/renewal" )
178
$t->post_ok( "//$userid:$password@/api/v1/checkouts/" . $issue2->issue_id . "/renewal" )
173
  ->status_is(201)
179
  ->status_is(201)
174
  ->json_is('/due_date' => output_pref({ dateformat => "rfc3339", dt => $expected_datedue}) )
180
  ->json_is('/due_date' => output_pref({ dateformat => "rfc3339", dt => $expected_datedue}) )
Lines 179-181 $t->post_ok( "//$userid:$password@/api/v1/checkouts/" . $issue1->issue_id . "/re Link Here
179
  ->status_is(403)
185
  ->status_is(403)
180
  ->json_is({ error => 'Renewal not authorized (too_many)' });
186
  ->json_is({ error => 'Renewal not authorized (too_many)' });
181
187
182
- 
188
$tx = $t->ua->build_tx(GET => "/api/v1/checkouts/" . $issue2->issue_id . "/renewability");
189
$tx->req->cookies({name => 'CGISESSID', value => $patron_session->id});
190
$t->request_ok($tx)
191
  ->status_is(200)
192
  ->json_is({ renewable => Mojo::JSON->false, error => 'too_many' });

Return to bug 17003