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

(-)a/C4/Circulation.pm (-1 / +1 lines)
Lines 776-782 sub CanBookBeIssued { Link Here
776
    my $onsite_checkout     = $params->{onsite_checkout}     || 0;
776
    my $onsite_checkout     = $params->{onsite_checkout}     || 0;
777
    my $override_high_holds = $params->{override_high_holds} || 0;
777
    my $override_high_holds = $params->{override_high_holds} || 0;
778
778
779
    my $item_object = Koha::Items->find({barcode => $barcode });
779
    my $item_object = $params->{item} // Koha::Items->find({barcode => $barcode });
780
780
781
    # MANDATORY CHECKS - unless item exists, nothing else matters
781
    # MANDATORY CHECKS - unless item exists, nothing else matters
782
    unless ( $item_object ) {
782
    unless ( $item_object ) {
(-)a/Koha/Item.pm (-1 lines)
Lines 341-347 sub move_to_deleted { Link Here
341
    return Koha::Database->new->schema->resultset('Deleteditem')->create($item_infos);
341
    return Koha::Database->new->schema->resultset('Deleteditem')->create($item_infos);
342
}
342
}
343
343
344
345
=head3 effective_itemtype
344
=head3 effective_itemtype
346
345
347
Returns the itemtype for the item based on whether item level itemtypes are set or not.
346
Returns the itemtype for the item based on whether item level itemtypes are set or not.
(-)a/Koha/REST/V1/Checkouts.pm (-1 / +150 lines)
Lines 19-28 use Modern::Perl; Link Here
19
19
20
use Mojo::Base 'Mojolicious::Controller';
20
use Mojo::Base 'Mojolicious::Controller';
21
use Mojo::JSON;
21
use Mojo::JSON;
22
use Mojo::JWT;
23
use Digest::MD5 qw( md5_base64 );
24
use Encode;
22
25
23
use C4::Auth qw( haspermission );
26
use C4::Auth qw( haspermission );
24
use C4::Context;
27
use C4::Context;
25
use C4::Circulation qw( AddRenewal );
28
use C4::Circulation qw( CanBookBeIssued AddIssue AddRenewal );
26
use Koha::Checkouts;
29
use Koha::Checkouts;
27
use Koha::Old::Checkouts;
30
use Koha::Old::Checkouts;
28
31
Lines 99-104 sub get { Link Here
99
    };
102
    };
100
}
103
}
101
104
105
=head3 get_availablity
106
107
Controller function that handles retrieval of Checkout availability
108
109
=cut
110
111
sub get_availability {
112
    my $c = shift->openapi->valid_input or return;
113
114
    my $patron = Koha::Patrons->find( $c->validation->param('patron_id') );
115
    my $inprocess = 0; # What does this do?
116
    my $ignore_reserves = 0; # Don't ignore reserves
117
    my $item   = Koha::Items->find( $c->validation->param('item_id') );
118
    my $params = {
119
        item => $item
120
    };
121
122
    my ( $impossible, $confirmation, $alerts, $messages ) =
123
      CanBookBeIssued( $patron, undef, undef, $inprocess, $ignore_reserves,
124
        $params );
125
126
    my $token;
127
    if (keys %{$confirmation}) {
128
        my $claims = { map { $_ => 1 } keys %{$confirmation} };
129
        my $secret =
130
          md5_base64( Encode::encode( 'UTF-8', C4::Context->config('pass') ) );
131
        $token = Mojo::JWT->new( claims => $claims, secret => $secret )->encode;
132
    }
133
134
    my $response = {
135
        blockers           => $impossible,
136
        confirms           => $confirmation,
137
        warnings           => { %{$alerts}, %{$messages} },
138
        confirmation_token => $token
139
    };
140
141
    return $c->render( status => 200, openapi => $response );
142
}
143
144
=head3 add
145
146
Add a new checkout
147
148
=cut
149
150
sub add {
151
    my $c = shift->openapi->valid_input or return;
152
153
    my $params = $c->validation->param('body');
154
    my $item_id = $params->{item_id};
155
    my $patron_id = $params->{patron_id};
156
    my $onsite = $params->{onsite_checkout};
157
158
    return try {
159
        my $item = Koha::Items->find( $item_id );
160
        unless ($item) {
161
            return $c->render(
162
                status  => 409,
163
                openapi => {
164
                    error      => 'Item not found',
165
                    error_code => 'ITEM_NOT_FOUND',
166
                }
167
            );
168
        }
169
170
        my $patron = Koha::Patrons->find( $patron_id );
171
        unless ($patron) {
172
            return $c->render(
173
                status  => 409,
174
                openapi => {
175
                    error      => 'Patron not found',
176
                    error_code => 'PATRON_NOT_FOUND',
177
                }
178
            );
179
        }
180
181
        my $inprocess = 0; # What does this do?
182
        my $ignore_reserves = 0; # Don't ignore reserves
183
        my $params = {
184
            item => $item
185
        };
186
187
        # Call 'CanBookBeIssued'
188
        my ( $impossible, $confirmation, $alerts, $messages ) =
189
          CanBookBeIssued( $patron, undef, undef, $inprocess, $ignore_reserves,
190
            $params );
191
192
        # * Fail for blockers - render 403
193
        if (keys %{$impossible}) {
194
            my @errors = keys %{$impossible};
195
            return $c->render(
196
                status => 403,
197
                openapi => { error => "Checkout not authorized (@errors)" }
198
            );
199
        }
200
201
        # * If confirmation required, check variable set above - render 412 if variable is false
202
        if (keys %{$confirmation}) {
203
            my $confirmed = 0;
204
205
            # Check for existance of confirmation token and if exists check validity
206
            if ( my $token = $c->validation->param('confirmation') ) {
207
                my $secret =
208
                  md5_base64(
209
                    Encode::encode( 'UTF-8', C4::Context->config('pass') ) );
210
                my $claims = try {
211
                    Mojo::JWT->new( secret => $secret )->decode($token);
212
                } catch {
213
                    return $c->render(
214
                        status  => 403,
215
                        openapi =>
216
                          { error => "Confirmation required" }
217
                    );
218
                };
219
220
                # check claims match
221
                $confirmed = 1
222
                  if ( sort keys %{$claims} eq sort keys %{$confirmation} );
223
            }
224
225
            unless ($confirmed) {
226
                return $c->render(
227
                    status  => 412,
228
                    openapi => { error => "Checkout requires confirmation" }
229
                );
230
            }
231
        }
232
233
        # Call 'AddIssue'
234
        my $checkout = AddIssue($patron->unblessed, $item->barcode);
235
236
        # Check return of AddIssue
237
        # Get checkout from AddIssue - render 201
238
        # Get error from AddIssue - render 400?
239
240
        $c->res->headers->location( $c->req->url->to_string . '/' . $checkout->id );
241
        return $c->render(
242
            status  => 201,
243
            openapi => $checkout->to_api
244
        );
245
    }
246
    catch {
247
        $c->unhandled_exception($_);
248
    };
249
}
250
102
=head3 get_renewals
251
=head3 get_renewals
103
252
104
List Koha::Checkout::Renewals
253
List Koha::Checkout::Renewals
(-)a/api/v1/swagger/paths/checkouts.yaml (+119 lines)
Lines 61-66 Link Here
61
    x-koha-authorization:
61
    x-koha-authorization:
62
      permissions:
62
      permissions:
63
        circulate: circulate_remaining_permissions
63
        circulate: circulate_remaining_permissions
64
  post:
65
    x-mojo-to: Checkouts#add
66
    operationId: addCheckout
67
    tags:
68
      - checkouts
69
    summary: Add a new checkout
70
    parameters:
71
      - name: body
72
        in: body
73
        description: A JSON object containing informations about the new checkout
74
        required: true
75
        schema:
76
          $ref: "../swagger.yaml#/definitions/checkout"
77
    produces:
78
      - application/json
79
    responses:
80
      "201":
81
        description: Created new checkout
82
        schema:
83
          $ref: "../swagger.yaml#/definitions/checkout"
84
      "403":
85
        description: Cannot create checkout
86
        schema:
87
          $ref: "../swagger.yaml#/definitions/error"
88
      "404":
89
        description: Checkout not found
90
        schema:
91
          $ref: "../swagger.yaml#/definitions/error"
92
      "409":
93
        description: Conflict in creating resource
94
        schema:
95
          $ref: "../swagger.yaml#/definitions/error"
96
      "412":
97
        description: Precondition failed
98
        schema:
99
          $ref: "../swagger.yaml#/definitions/error"
100
      "500":
101
        description: |
102
          Internal server error. Possible `error_code` attribute values:
103
104
          * `internal_server_error`
105
        schema:
106
          $ref: "../swagger.yaml#/definitions/error"
107
      "503":
108
        description: Under maintenance
109
        schema:
110
          $ref: "../swagger.yaml#/definitions/error"
64
"/checkouts/{checkout_id}":
111
"/checkouts/{checkout_id}":
65
  get:
112
  get:
66
    x-mojo-to: Checkouts#get
113
    x-mojo-to: Checkouts#get
Lines 275-277 Link Here
275
    x-koha-authorization:
322
    x-koha-authorization:
276
      permissions:
323
      permissions:
277
        circulate: circulate_remaining_permissions
324
        circulate: circulate_remaining_permissions
325
"/public/checkouts":
326
  post:
327
    x-mojo-to: Checkouts#add
328
    operationId: addCheckoutPublic
329
    tags:
330
      - checkouts
331
    summary: Add a new checkout
332
    parameters:
333
      - name: body
334
        in: body
335
        description: A JSON object containing informations about the new checkout
336
        required: true
337
        schema:
338
          $ref: "../swagger.yaml#/definitions/checkout"
339
    produces:
340
      - application/json
341
    responses:
342
      "201":
343
        description: Created new checkout
344
        schema:
345
          $ref: "../swagger.yaml#/definitions/checkout"
346
      "403":
347
        description: Cannot create checkout
348
        schema:
349
          $ref: "../swagger.yaml#/definitions/error"
350
      "404":
351
        description: Checkout not found
352
        schema:
353
          $ref: "../swagger.yaml#/definitions/error"
354
      "500":
355
        description: |
356
          Internal server error. Possible `error_code` attribute values:
357
358
          * `internal_server_error`
359
        schema:
360
          $ref: "../swagger.yaml#/definitions/error"
361
      "503":
362
        description: Under maintenance
363
        schema:
364
          $ref: "../swagger.yaml#/definitions/error"
365
"/public/checkouts/availability":
366
  get:
367
    x-mojo-to: Checkouts#get_availability
368
    operationId: availabilityCheckoutsPublic
369
    tags:
370
      - checkouts
371
    summary: Get checkout availability
372
    parameters:
373
      - $ref: "../swagger.yaml#/parameters/patron_id_qp"
374
      - $ref: "../swagger.yaml#/parameters/item_id_qp"
375
    produces:
376
      - application/json
377
    responses:
378
      "200":
379
        description: Availability
380
        schema:
381
          type: "object"
382
      "403":
383
        description: Access forbidden
384
        schema:
385
          $ref: "../swagger.yaml#/definitions/error"
386
      "500":
387
        description: |
388
          Internal server error. Possible `error_code` attribute values:
389
390
          * `internal_server_error`
391
        schema:
392
          $ref: "../swagger.yaml#/definitions/error"
393
      "503":
394
        description: Under maintenance
395
        schema:
396
          $ref: "../swagger.yaml#/definitions/error"
(-)a/api/v1/swagger/swagger.yaml (-1 / +9 lines)
Lines 205-210 paths: Link Here
205
    $ref: "./paths/patrons_password.yaml#/~1patrons~1{patron_id}~1password~1expiration_date"
205
    $ref: "./paths/patrons_password.yaml#/~1patrons~1{patron_id}~1password~1expiration_date"
206
  "/public/biblios/{biblio_id}":
206
  "/public/biblios/{biblio_id}":
207
    $ref: "./paths/biblios.yaml#/~1public~1biblios~1{biblio_id}"
207
    $ref: "./paths/biblios.yaml#/~1public~1biblios~1{biblio_id}"
208
  "/public/checkouts":
209
    $ref: ./paths/checkouts.yaml#/~1public~1checkouts
210
  "/public/checkouts/availability":
211
    $ref: ./paths/checkouts.yaml#/~1public~1checkouts~1availability
208
  "/public/items":
212
  "/public/items":
209
    $ref: "./paths/items.yaml#/~1public~1items"
213
    $ref: "./paths/items.yaml#/~1public~1items"
210
  "/public/biblios/{biblio_id}/items":
214
  "/public/biblios/{biblio_id}/items":
Lines 326-331 parameters: Link Here
326
    name: item_id
330
    name: item_id
327
    required: true
331
    required: true
328
    type: integer
332
    type: integer
333
  item_id_qp:
334
    description: Internal item identifier
335
    in: query
336
    name: item_id
337
    type: integer
329
  library_id_pp:
338
  library_id_pp:
330
    description: Internal library identifier
339
    description: Internal library identifier
331
    in: path
340
    in: path
332
- 

Return to bug 30979