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

(-)a/Koha/REST/V1/Checkouts.pm (+61 lines)
Lines 284-289 sub add { Link Here
284
    };
284
    };
285
}
285
}
286
286
287
=head3 delete
288
289
Removing the checked out item from the issues table and adding it to the
290
old_issues table
291
292
=cut
293
294
sub delete {
295
296
    my $c = shift->openapi->valid_input or return;
297
298
    my $body      = $c->validation->param('body');
299
    my $item_id   = $body->{item_id};
300
301
    return try {
302
        my $item = Koha::Items->find($item_id);
303
        unless ($item) {
304
            return $c->render(
305
                status  => 409,
306
                openapi => {
307
                    error      => 'Item not found'
308
                }
309
            );
310
        }
311
        my $checkout = Koha::Checkouts->find({ itemnumber => $item_id });
312
        unless ( $checkout ) {
313
            return $c->render(
314
                status => 400,
315
                openapi => { error => "Item not checked out" }
316
            );
317
        }
318
        my $library_id = $c->validation->output->{library_id};
319
        unless ( $library_id ) {
320
           $library_id = $checkout->patron->branchcode;
321
        }
322
        my $library = Koha::Libraries->find( $library_id );
323
324
        try {
325
            my $barcode = $item->barcode;
326
            my ( $returned ) = C4::Circulation::AddReturn( $barcode, $library_id );
327
328
            unless ( $returned ) {
329
                return $c->render(
330
                    status => 403,
331
                    openapi => { error => "Checkin not allowed" }
332
                );
333
            }
334
335
            $c->res->headers->location( $c->req->url->to_string );
336
            return $c->render(
337
                status  => 201,
338
                openapi => $item->to_api
339
            );
340
        }
341
        catch {
342
            $c->unhandled_exception($_);
343
        };
344
    }
345
 }
346
347
287
=head3 get_renewals
348
=head3 get_renewals
288
349
289
List Koha::Checkout::Renewals
350
List Koha::Checkout::Renewals
(-)a/api/v1/swagger/paths/checkouts.yaml (+36 lines)
Lines 122-127 Link Here
122
    x-koha-authorization:
122
    x-koha-authorization:
123
      permissions:
123
      permissions:
124
        circulate: circulate_remaining_permissions
124
        circulate: circulate_remaining_permissions
125
  delete:
126
    x-mojo-to: Checkouts#delete
127
    operationId: deletecheckout
128
    tags:
129
      - checkouts
130
      - items
131
    summary: Check in an item
132
    parameters:
133
      - name: body
134
        in: body
135
        description: A JSON object containing information about the checkout
136
        required: true
137
        schema:
138
          $ref: "../swagger.yaml#/definitions/checkout"
139
    consumes:
140
      - application/json
141
    produces:
142
      - application/json
143
    responses:
144
      "201":
145
        description: Item returned
146
      "400":
147
        description: Item not checked out
148
        schema:
149
          $ref: "../swagger.yaml#/definitions/error"
150
      "403":
151
        description: Checkin not allowed
152
        schema:
153
          $ref: "../swagger.yaml#/definitions/error"
154
      "409":
155
        description: Item not found
156
        schema:
157
          $ref: "../swagger.yaml#/definitions/error"
158
    x-koha-authorization:
159
      permissions:
160
        circulate: circulate_remaining_permissions
125
"/checkouts/{checkout_id}":
161
"/checkouts/{checkout_id}":
126
  get:
162
  get:
127
    x-mojo-to: Checkouts#get
163
    x-mojo-to: Checkouts#get
(-)a/api/v1/swagger/swagger.yaml (+6 lines)
Lines 544-549 parameters: Link Here
544
    in: query
544
    in: query
545
    name: item_id
545
    name: item_id
546
    type: integer
546
    type: integer
547
  library_id_qp:
548
    description: Internal library identifier. If not provided, will ues checkout patron's home library
549
    in: query
550
    name: library_id
551
    required: false
552
    type: string
547
  job_id_pp:
553
  job_id_pp:
548
    description: Job internal identifier
554
    description: Job internal identifier
549
    in: path
555
    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 => 105;
20
use Test::More tests => 106;
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 435-440 subtest 'add checkout' => sub { Link Here
435
435
436
    my $item1    = $builder->build_sample_item;
436
    my $item1    = $builder->build_sample_item;
437
    my $item1_id = $item1->id;
437
    my $item1_id = $item1->id;
438
    my $item2    = $builder->build_sample_item;
439
    my $item2_id = $item2->id;
438
440
439
    my %issuingimpossible = ();
441
    my %issuingimpossible = ();
440
    my %needsconfirmation = ();
442
    my %needsconfirmation = ();
Lines 471-477 subtest 'add checkout' => sub { Link Here
471
    my $token = Koha::Token->new->generate_jwt( { id => $librarian->id . ":" . $item1_id . ":confirm1:confirm2" } );
473
    my $token = Koha::Token->new->generate_jwt( { id => $librarian->id . ":" . $item1_id . ":confirm1:confirm2" } );
472
    $t->post_ok(
474
    $t->post_ok(
473
        "//$userid:$password@/api/v1/checkouts?confirmation=$token" => json => {
475
        "//$userid:$password@/api/v1/checkouts?confirmation=$token" => json => {
474
            item_id   => $item1_id,
476
            item_id   => $item2_id,
475
            patron_id => $patron_id
477
            patron_id => $patron_id
476
        }
478
        }
477
    )->status_is(201)->or( sub { diag $t->tx->res->body } );
479
    )->status_is(201)->or( sub { diag $t->tx->res->body } );
Lines 515-517 subtest 'add checkout' => sub { Link Here
515
517
516
    $schema->storage->txn_rollback;
518
    $schema->storage->txn_rollback;
517
};
519
};
518
- 
520
521
subtest 'delete checkout' => sub {
522
523
    plan tests => 11;
524
525
    $schema->storage->txn_begin;
526
    my $librarian = $builder->build_object(
527
        {
528
            class => 'Koha::Patrons',
529
            value => { flags => 2 }
530
        }
531
    );
532
    my $password = 'thePassword123';
533
    $librarian->set_password( { password => $password, skip_validation => 1 } );
534
    my $userid = $librarian->userid;
535
536
    my $patron = $builder->build_object(
537
        {
538
            class => 'Koha::Patrons',
539
            value => { flags => 0 }
540
        }
541
    );
542
    my $unauth_password = 'thePassword000';
543
    $patron->set_password(
544
        { password => $unauth_password, skip_validattion => 1 } );
545
    my $unauth_userid = $patron->userid;
546
547
    my $branch = $builder->build_object( { class => 'Koha::Libraries' } );
548
    my $library_id = $branch->id;
549
    my $item1    = $builder->build_sample_item;
550
    my $item1_id = $item1->id;
551
552
553
    $t->delete_ok(
554
        "//$unauth_userid:$unauth_password@/api/v1/checkouts" => json =>
555
          { item_id => $item1_id, library_id => $library_id } )->status_is(403)
556
      ->json_is(
557
        {
558
            error => "Authorization failure. Missing required permission(s).",
559
            required_permissions =>
560
              { circulate => "circulate_remaining_permissions" }
561
        }
562
      );
563
564
    $t->delete_ok( "//$userid:$password@/api/v1/checkouts" => json =>
565
          { item_id => $item1_id, library_id => $library_id } )->status_is(400)
566
      ->json_is(
567
        '/error' => 'Item not checked out' );
568
569
    AddIssue( $patron->unblessed, $item1->barcode );
570
    $t->delete_ok( "//$userid:$password@/api/v1/checkouts" => json =>
571
          { item_id => $item1_id, library_id => $library_id } )->status_is(201);
572
573
    $item1->delete;
574
    $t->delete_ok( "//$userid:$password@/api/v1/checkouts/" => json =>
575
            { item_id => $item1_id, library_id => $library_id } )->status_is(409)
576
      ->json_is(
577
        '/error' => 'Item not found' );
578
    $schema->storage->txn_rollback;
579
};

Return to bug 24401