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

(-)a/Koha/REST/V1/Checkout.pm (+35 lines)
Lines 18-23 package Koha::REST::V1::Checkout; 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 86-89 sub renew { Link Here
86
    return $c->$cb($checkout->unblessed, 200);
87
    return $c->$cb($checkout->unblessed, 200);
87
}
88
}
88
89
90
sub renewability {
91
    my ($c, $args, $cb) = @_;
92
93
    my $user = $c->stash('koha.user');
94
95
    my $checkout_id = $args->{checkout_id};
96
    my $checkout = Koha::Issues->find($checkout_id);
97
98
    if (!$checkout) {
99
        return $c->$cb({
100
            error => "Checkout doesn't exist"
101
        }, 404);
102
    }
103
104
    my $borrowernumber = $checkout->borrowernumber;
105
    my $itemnumber = $checkout->itemnumber;
106
107
    my $OpacRenewalAllowed;
108
    if ($user->borrowernumber == $borrowernumber) {
109
        $OpacRenewalAllowed = C4::Context->preference('OpacRenewalAllowed');
110
    }
111
112
    unless ($user && ($OpacRenewalAllowed
113
            || haspermission($user->userid, { circulate => "circulate_remaining_permissions" }))) {
114
        return $c->$cb({error => "You don't have the required permission"}, 403);
115
    }
116
117
    my ($can_renew, $error) = C4::Circulation::CanBookBeRenewed(
118
        $borrowernumber, $itemnumber);
119
120
    return $c->$cb({ renewable => Mojo::JSON->true, error => undef }, 200) if $can_renew;
121
    return $c->$cb({ renewable => Mojo::JSON->false, error => $error }, 200);
122
}
123
89
1;
124
1;
(-)a/api/v1/swagger/definitions.json (+3 lines)
Lines 19-23 Link Here
19
  },
19
  },
20
  "error": {
20
  "error": {
21
    "$ref": "definitions/error.json"
21
    "$ref": "definitions/error.json"
22
  },
23
  "renewability": {
24
    "$ref": "definitions/renewability.json"
22
  }
25
  }
23
}
26
}
(-)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 17-22 Link Here
17
  "/checkouts/{checkout_id}": {
17
  "/checkouts/{checkout_id}": {
18
    "$ref": "paths/checkouts.json#/~1checkouts~1{checkout_id}"
18
    "$ref": "paths/checkouts.json#/~1checkouts~1{checkout_id}"
19
  },
19
  },
20
  "/checkouts/{checkout_id}/renewability": {
21
    "$ref": "paths/checkouts.json#/~1checkouts~1{checkout_id}~1renewability"
22
  },
20
  "/patrons": {
23
  "/patrons": {
21
    "$ref": "paths/patrons.json#/~1patrons"
24
    "$ref": "paths/patrons.json#/~1patrons"
22
  },
25
  },
(-)a/api/v1/swagger/paths/checkouts.json (+28 lines)
Lines 93-97 Link Here
93
        }
93
        }
94
      }
94
      }
95
    }
95
    }
96
  },
97
  "/checkouts/{checkout_id}/renewability": {
98
    "get": {
99
      "operationId": "renewabilityCheckout",
100
      "tags": ["patrons", "checkouts"],
101
      "parameters": [{
102
          "name": "checkout_id",
103
          "in": "path",
104
          "description": "Internal checkout identifier",
105
          "required": true,
106
          "type": "integer"
107
      }],
108
      "produces": ["application/json"],
109
      "responses": {
110
        "200": {
111
          "description": "Checkout renewability",
112
          "schema": { "$ref": "../definitions.json#/renewability" }
113
        },
114
        "403": {
115
          "description": "Forbidden",
116
          "schema": { "$ref": "../definitions.json#/error" }
117
        },
118
        "404": {
119
          "description": "Checkout not found",
120
          "schema": { "$ref": "../definitions.json#/error" }
121
        }
122
      }
123
    }
96
  }
124
  }
97
}
125
}
(-)a/t/db_dependent/api/v1/checkouts.t (-2 / +19 lines)
Lines 17-23 Link Here
17
17
18
use Modern::Perl;
18
use Modern::Perl;
19
19
20
use Test::More tests => 57;
20
use Test::More tests => 66;
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 189-195 $t->request_ok($tx) Link Here
189
  ->status_is(403)
189
  ->status_is(403)
190
  ->json_is({ error => "Opac Renewal not allowed"	});
190
  ->json_is({ error => "Opac Renewal not allowed"	});
191
191
192
$tx = $t->ua->build_tx(GET => "/api/v1/checkouts/" . $issue2->issue_id . "/renewability");
193
$tx->req->cookies({name => 'CGISESSID', value => $patron_session->id});
194
$t->request_ok($tx)
195
  ->status_is(403)
196
  ->json_is({ error => "You don't have the required permission" });
197
192
t::lib::Mocks::mock_preference( "OpacRenewalAllowed", 1 );
198
t::lib::Mocks::mock_preference( "OpacRenewalAllowed", 1 );
199
$tx = $t->ua->build_tx(GET => "/api/v1/checkouts/" . $issue2->issue_id . "/renewability");
200
$tx->req->cookies({name => 'CGISESSID', value => $patron_session->id});
201
$t->request_ok($tx)
202
  ->status_is(200)
203
  ->json_is({ renewable => Mojo::JSON->true, error => undef });
204
193
$tx = $t->ua->build_tx(PUT => "/api/v1/checkouts/" . $issue2->issue_id);
205
$tx = $t->ua->build_tx(PUT => "/api/v1/checkouts/" . $issue2->issue_id);
194
$tx->req->cookies({name => 'CGISESSID', value => $patron_session->id});
206
$tx->req->cookies({name => 'CGISESSID', value => $patron_session->id});
195
$t->request_ok($tx)
207
$t->request_ok($tx)
Lines 202-207 $t->request_ok($tx) Link Here
202
  ->status_is(403)
214
  ->status_is(403)
203
  ->json_is({ error => 'Renewal not authorized (too_many)' });
215
  ->json_is({ error => 'Renewal not authorized (too_many)' });
204
216
217
$tx = $t->ua->build_tx(GET => "/api/v1/checkouts/" . $issue2->issue_id . "/renewability");
218
$tx->req->cookies({name => 'CGISESSID', value => $patron_session->id});
219
$t->request_ok($tx)
220
  ->status_is(200)
221
  ->json_is({ renewable => Mojo::JSON->false, error => 'too_many' });
222
205
sub create_biblio {
223
sub create_biblio {
206
    my ($title) = @_;
224
    my ($title) = @_;
207
225
208
- 

Return to bug 17003