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

(-)a/Koha/REST/V1/Checkouts.pm (+61 lines)
Lines 244-249 sub add { Link Here
244
    };
244
    };
245
}
245
}
246
246
247
=head3 delete
248
249
Removing the checked out item from the issues table and adding it to the
250
old_issues table
251
252
=cut
253
254
sub delete {
255
256
    my $c = shift->openapi->valid_input or return;
257
258
    my $body      = $c->validation->param('body');
259
    my $item_id   = $body->{item_id};
260
261
    return try {
262
        my $item = Koha::Items->find($item_id);
263
        unless ($item) {
264
            return $c->render(
265
                status  => 409,
266
                openapi => {
267
                    error      => 'Item not found'
268
                }
269
            );
270
        }
271
        my $checkout = Koha::Checkouts->find({ itemnumber => $item_id });
272
        unless ( $checkout ) {
273
            return $c->render(
274
                status => 400,
275
                openapi => { error => "Item not checked out" }
276
            );
277
        }
278
        my $library_id = $c->validation->output->{library_id};
279
        unless ( $library_id ) {
280
           $library_id = $checkout->patron->branchcode;
281
        }
282
        my $library = Koha::Libraries->find( $library_id );
283
284
        try {
285
            my $barcode = $item->barcode;
286
            my ( $returned ) = C4::Circulation::AddReturn( $barcode, $library_id );
287
288
            unless ( $returned ) {
289
                return $c->render(
290
                    status => 403,
291
                    openapi => { error => "Checkin not allowed" }
292
                );
293
            }
294
295
            $c->res->headers->location( $c->req->url->to_string );
296
            return $c->render(
297
                status  => 201,
298
                openapi => $item->to_api
299
            );
300
        }
301
        catch {
302
            $c->unhandled_exception($_);
303
        };
304
    }
305
 }
306
307
247
=head3 get_renewals
308
=head3 get_renewals
248
309
249
List Koha::Checkout::Renewals
310
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 528-533 parameters: Link Here
528
    in: query
528
    in: query
529
    name: item_id
529
    name: item_id
530
    type: integer
530
    type: integer
531
  library_id_qp:
532
    description: Internal library identifier. If not provided, will ues checkout patron's home library
533
    in: query
534
    name: library_id
535
    required: false
536
    type: string
531
  job_id_pp:
537
  job_id_pp:
532
    description: Job internal identifier
538
    description: Job internal identifier
533
    in: path
539
    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 375-380 subtest 'add checkout' => sub { Link Here
375
375
376
    my $item1    = $builder->build_sample_item;
376
    my $item1    = $builder->build_sample_item;
377
    my $item1_id = $item1->id;
377
    my $item1_id = $item1->id;
378
    my $item2    = $builder->build_sample_item;
379
    my $item2_id = $item2->id;
378
380
379
    my %issuingimpossible = ();
381
    my %issuingimpossible = ();
380
    my %needsconfirmation = ();
382
    my %needsconfirmation = ();
Lines 420-426 subtest 'add checkout' => sub { Link Here
420
    );
422
    );
421
    $t->post_ok(
423
    $t->post_ok(
422
        "//$userid:$password@/api/v1/checkouts?confirmation=$token" => json => {
424
        "//$userid:$password@/api/v1/checkouts?confirmation=$token" => json => {
423
            item_id            => $item1_id,
425
            item_id            => $item2_id,
424
            patron_id          => $patron_id
426
            patron_id          => $patron_id
425
        }
427
        }
426
    )->status_is(201)->or(sub { diag $t->tx->res->body });
428
    )->status_is(201)->or(sub { diag $t->tx->res->body });
Lines 428-430 subtest 'add checkout' => sub { Link Here
428
430
429
    $schema->storage->txn_rollback;
431
    $schema->storage->txn_rollback;
430
};
432
};
431
- 
433
434
subtest 'delete checkout' => sub {
435
436
    plan tests => 11;
437
438
    $schema->storage->txn_begin;
439
    my $librarian = $builder->build_object(
440
        {
441
            class => 'Koha::Patrons',
442
            value => { flags => 2 }
443
        }
444
    );
445
    my $password = 'thePassword123';
446
    $librarian->set_password( { password => $password, skip_validation => 1 } );
447
    my $userid = $librarian->userid;
448
449
    my $patron = $builder->build_object(
450
        {
451
            class => 'Koha::Patrons',
452
            value => { flags => 0 }
453
        }
454
    );
455
    my $unauth_password = 'thePassword000';
456
    $patron->set_password(
457
        { password => $unauth_password, skip_validattion => 1 } );
458
    my $unauth_userid = $patron->userid;
459
460
    my $branch = $builder->build_object( { class => 'Koha::Libraries' } );
461
    my $library_id = $branch->id;
462
    my $item1    = $builder->build_sample_item;
463
    my $item1_id = $item1->id;
464
465
466
    $t->delete_ok(
467
        "//$unauth_userid:$unauth_password@/api/v1/checkouts" => json =>
468
          { item_id => $item1_id, library_id => $library_id } )->status_is(403)
469
      ->json_is(
470
        {
471
            error => "Authorization failure. Missing required permission(s).",
472
            required_permissions =>
473
              { circulate => "circulate_remaining_permissions" }
474
        }
475
      );
476
477
    $t->delete_ok( "//$userid:$password@/api/v1/checkouts" => json =>
478
          { item_id => $item1_id, library_id => $library_id } )->status_is(400)
479
      ->json_is(
480
        '/error' => 'Item not checked out' );
481
482
    AddIssue( $patron->unblessed, $item1->barcode );
483
    $t->delete_ok( "//$userid:$password@/api/v1/checkouts" => json =>
484
          { item_id => $item1_id, library_id => $library_id } )->status_is(201);
485
486
    $item1->delete;
487
    $t->delete_ok( "//$userid:$password@/api/v1/checkouts/" => json =>
488
            { item_id => $item1_id, library_id => $library_id } )->status_is(409)
489
      ->json_is(
490
        '/error' => 'Item not found' );
491
    $schema->storage->txn_rollback;
492
};

Return to bug 24401