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

(-)a/Koha/REST/V1/Checkouts.pm (-1 / +62 lines)
Lines 25-31 use Encode; Link Here
25
25
26
use C4::Auth qw( haspermission );
26
use C4::Auth qw( haspermission );
27
use C4::Context;
27
use C4::Context;
28
use C4::Circulation qw( AddIssue AddRenewal );
28
use C4::Circulation qw( AddIssue AddRenewal CanBookBeRenewed );
29
use Koha::Checkouts;
29
use Koha::Checkouts;
30
use Koha::Old::Checkouts;
30
use Koha::Old::Checkouts;
31
31
Lines 259-264 sub add { Link Here
259
    };
259
    };
260
}
260
}
261
261
262
=head3 delete
263
264
Removing the checked out item from the issues table and adding it to the
265
old_issues table
266
267
=cut
268
269
sub delete {
270
271
    my $c = shift->openapi->valid_input or return;
272
273
    my $body      = $c->validation->param('body');
274
    my $item_id   = $body->{item_id};
275
276
    return try {
277
        my $item = Koha::Items->find($item_id);
278
        unless ($item) {
279
            return $c->render(
280
                status  => 409,
281
                openapi => {
282
                    error      => 'Item not found'
283
                }
284
            );
285
        }
286
        my $checkout = Koha::Checkouts->find({ itemnumber => $item_id });
287
        unless ( $checkout ) {
288
            return $c->render(
289
                status => 400,
290
                openapi => { error => "Item not checked out" }
291
            );
292
        }
293
        my $library_id = $c->validation->output->{library_id};
294
        unless ( $library_id ) {
295
           $library_id = $checkout->patron->branchcode;
296
        }
297
        my $library = Koha::Libraries->find( $library_id );
298
299
        try {
300
            my $barcode = $item->barcode;
301
            my ( $returned ) = C4::Circulation::AddReturn( $barcode, $library_id );
302
303
            unless ( $returned ) {
304
                return $c->render(
305
                    status => 403,
306
                    openapi => { error => "Checkin not allowed" }
307
                );
308
            }
309
310
            $c->res->headers->location( $c->req->url->to_string );
311
            return $c->render(
312
                status  => 201,
313
                openapi => $item->to_api
314
            );
315
        }
316
        catch {
317
            $c->unhandled_exception($_);
318
        };
319
    }
320
 }
321
322
262
=head3 get_renewals
323
=head3 get_renewals
263
324
264
List Koha::Checkout::Renewals
325
List Koha::Checkout::Renewals
(-)a/api/v1/swagger/paths/checkouts.yaml (+36 lines)
Lines 123-128 Link Here
123
    x-koha-authorization:
123
    x-koha-authorization:
124
      permissions:
124
      permissions:
125
        circulate: circulate_remaining_permissions
125
        circulate: circulate_remaining_permissions
126
  delete:
127
    x-mojo-to: Checkouts#delete
128
    operationId: deletecheckout
129
    tags:
130
      - checkouts
131
      - items
132
    summary: Check in an item
133
    parameters:
134
      - name: body
135
        in: body
136
        description: A JSON object containing information about the checkout
137
        required: true
138
        schema:
139
          $ref: "../swagger.yaml#/definitions/checkout"
140
    consumes:
141
      - application/json
142
    produces:
143
      - application/json
144
    responses:
145
      "201":
146
        description: Item returned
147
      "400":
148
        description: Item not checked out
149
        schema:
150
          $ref: "../swagger.yaml#/definitions/error"
151
      "403":
152
        description: Checkin not allowed
153
        schema:
154
          $ref: "../swagger.yaml#/definitions/error"
155
      "409":
156
        description: Item not found
157
        schema:
158
          $ref: "../swagger.yaml#/definitions/error"
159
    x-koha-authorization:
160
      permissions:
161
        circulate: circulate_remaining_permissions
126
"/checkouts/{checkout_id}":
162
"/checkouts/{checkout_id}":
127
  get:
163
  get:
128
    x-mojo-to: Checkouts#get
164
    x-mojo-to: Checkouts#get
(-)a/api/v1/swagger/swagger.yaml (+6 lines)
Lines 526-531 parameters: Link Here
526
    in: query
526
    in: query
527
    name: item_id
527
    name: item_id
528
    type: integer
528
    type: integer
529
  library_id_qp:
530
    description: Internal library identifier. If not provided, will ues checkout patron's home library
531
    in: query
532
    name: library_id
533
    required: false
534
    type: string
529
  job_id_pp:
535
  job_id_pp:
530
    description: Job internal identifier
536
    description: Job internal identifier
531
    in: path
537
    in: path
(-)a/t/db_dependent/api/v1/checkouts.t (-3 / +64 lines)
Lines 17-23 Link Here
17
17
18
use Modern::Perl;
18
use Modern::Perl;
19
19
20
use Test::More tests => 101;
20
use Test::More tests => 102;
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 361-366 subtest 'add checkout' => sub { Link Here
361
361
362
    my $item1    = $builder->build_sample_item;
362
    my $item1    = $builder->build_sample_item;
363
    my $item1_id = $item1->id;
363
    my $item1_id = $item1->id;
364
    my $item2    = $builder->build_sample_item;
365
    my $item2_id = $item2->id;
364
366
365
    my %issuingimpossible = ();
367
    my %issuingimpossible = ();
366
    my %needsconfirmation = ();
368
    my %needsconfirmation = ();
Lines 400-406 subtest 'add checkout' => sub { Link Here
400
    my $token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJjb25maXJtMSI6MSwiY29uZmlybTIiOjF9.4QBpITwnIGOAfohyKjaFDoeBWnGmQTdyJrPn9pavArw";
402
    my $token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJjb25maXJtMSI6MSwiY29uZmlybTIiOjF9.4QBpITwnIGOAfohyKjaFDoeBWnGmQTdyJrPn9pavArw";
401
    $t->post_ok(
403
    $t->post_ok(
402
        "//$userid:$password@/api/v1/checkouts?confirmation=$token" => json => {
404
        "//$userid:$password@/api/v1/checkouts?confirmation=$token" => json => {
403
            item_id            => $item1_id,
405
            item_id            => $item2_id,
404
            patron_id          => $patron_id
406
            patron_id          => $patron_id
405
        }
407
        }
406
    )->status_is(201)->or(sub { diag $t->tx->res->body });
408
    )->status_is(201)->or(sub { diag $t->tx->res->body });
Lines 408-410 subtest 'add checkout' => sub { Link Here
408
410
409
    $schema->storage->txn_rollback;
411
    $schema->storage->txn_rollback;
410
};
412
};
411
- 
413
414
subtest 'delete checkout' => sub {
415
416
    plan tests => 11;
417
418
    $schema->storage->txn_begin;
419
    my $librarian = $builder->build_object(
420
        {
421
            class => 'Koha::Patrons',
422
            value => { flags => 2 }
423
        }
424
    );
425
    my $password = 'thePassword123';
426
    $librarian->set_password( { password => $password, skip_validation => 1 } );
427
    my $userid = $librarian->userid;
428
429
    my $patron = $builder->build_object(
430
        {
431
            class => 'Koha::Patrons',
432
            value => { flags => 0 }
433
        }
434
    );
435
    my $unauth_password = 'thePassword000';
436
    $patron->set_password(
437
        { password => $unauth_password, skip_validattion => 1 } );
438
    my $unauth_userid = $patron->userid;
439
440
    my $branch = $builder->build_object( { class => 'Koha::Libraries' } );
441
    my $library_id = $branch->id;
442
    my $item1    = $builder->build_sample_item;
443
    my $item1_id = $item1->id;
444
445
446
    $t->delete_ok(
447
        "//$unauth_userid:$unauth_password@/api/v1/checkouts" => json =>
448
          { item_id => $item1_id, library_id => $library_id } )->status_is(403)
449
      ->json_is(
450
        {
451
            error => "Authorization failure. Missing required permission(s).",
452
            required_permissions =>
453
              { circulate => "circulate_remaining_permissions" }
454
        }
455
      );
456
457
    $t->delete_ok( "//$userid:$password@/api/v1/checkouts" => json =>
458
          { item_id => $item1_id, library_id => $library_id } )->status_is(400)
459
      ->json_is(
460
        '/error' => 'Item not checked out' );
461
462
    AddIssue( $patron->unblessed, $item1->barcode );
463
    $t->delete_ok( "//$userid:$password@/api/v1/checkouts" => json =>
464
          { item_id => $item1_id, library_id => $library_id } )->status_is(201);
465
466
    $item1->delete;
467
    $t->delete_ok( "//$userid:$password@/api/v1/checkouts/" => json =>
468
            { item_id => $item1_id, library_id => $library_id } )->status_is(409)
469
      ->json_is(
470
        '/error' => 'Item not found' );
471
    $schema->storage->txn_rollback;
472
};

Return to bug 24401