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

(-)a/Koha/REST/V1/Checkouts.pm (+61 lines)
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 (+30 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: Checkout#delete
128
    operationId: deletecheckout
129
    tags:
130
      - checkouts
131
      - items
132
    summary: Check in an item
133
    parameters:
134
      - $ref: "../swagger.yaml#/parameters/item_id_pp"
135
      - $ref: "../swagger.yaml#/parameters/library_id_qp"
136
    produces:
137
      - application/json
138
    responses:
139
      "201":
140
        description: Item returned
141
      "400":
142
        description: Item not checked out
143
        schema:
144
          $ref: "../swagger.yaml#/definitions/error"
145
      "403":
146
        description: Checkin not allowed
147
        schema:
148
          $ref: "../swagger.yaml#/definitions/error"
149
      "409":
150
        description: Item not found
151
        schema:
152
          $ref: "../swagger.yaml#/definitions/error"
153
    x-koha-authorization:
154
      permissions:
155
        circulate: circulate_remaining_permissions
126
"/checkouts/{checkout_id}":
156
"/checkouts/{checkout_id}":
127
  get:
157
  get:
128
    x-mojo-to: Checkouts#get
158
    x-mojo-to: Checkouts#get
(-)a/api/v1/swagger/swagger.yaml (+8 lines)
Lines 199-204 paths: Link Here
199
    $ref: "./paths/checkouts.yaml#/~1checkouts~1{checkout_id}~1renewal"
199
    $ref: "./paths/checkouts.yaml#/~1checkouts~1{checkout_id}~1renewal"
200
  "/checkouts/availability":
200
  "/checkouts/availability":
201
    $ref: "./paths/checkouts.yaml#/~1checkouts~1availability"
201
    $ref: "./paths/checkouts.yaml#/~1checkouts~1availability"
202
  "/checkouts/{item_id}":
203
    $ref: "./paths/checkouts.yaml#/~1checkouts~1{item_id}"
202
  /circulation-rules/kinds:
204
  /circulation-rules/kinds:
203
    $ref: ./paths/circulation-rules.yaml#/~1circulation-rules~1kinds
205
    $ref: ./paths/circulation-rules.yaml#/~1circulation-rules~1kinds
204
  /cities:
206
  /cities:
Lines 528-533 parameters: Link Here
528
    in: query
530
    in: query
529
    name: item_id
531
    name: item_id
530
    type: integer
532
    type: integer
533
  library_id_qp:
534
    description: Internal library identifier. If not provided, will ues checkout patron's home library
535
    in: query
536
    name: library_id
537
    required: false
538
    type: string  
531
  job_id_pp:
539
  job_id_pp:
532
    description: Job internal identifier
540
    description: Job internal identifier
533
    in: path
541
    in: path
(-)a/t/db_dependent/api/v1/checkouts.t (-1 / +60 lines)
Lines 408-410 subtest 'add checkout' => sub { Link Here
408
408
409
    $schema->storage->txn_rollback;
409
    $schema->storage->txn_rollback;
410
};
410
};
411
- 
411
412
subtest 'delete checkout' => sub {
413
414
    plan tests => 4;
415
416
    $schema->storage->txn_begin;
417
    my $librarian = $builder->build_object(
418
        {
419
            class => 'Koha::Patrons',
420
            value => { flags => 2 }
421
        }
422
    );
423
    my $password = 'thePassword123';
424
    $librarian->set_password( { password => $password, skip_validation => 1 } );
425
    my $userid = $librarian->userid;
426
427
    my $patron = $builder->build_object(
428
        {
429
            class => 'Koha::Patrons',
430
            value => { flags => 0 }
431
        }
432
    );
433
    my $unauth_password = 'thePassword000';
434
    $patron->set_password(
435
        { password => $unauth_password, skip_validattion => 1 } );
436
    my $unauth_userid = $patron->userid;
437
438
    my $branch = $builder->build_sample_branch;
439
    my $library_id = $branch->id; 
440
    my $item1    = $builder->build_sample_item;
441
    my $item1_id = $item1->id;
442
443
444
    $t->delete_ok(
445
        "//$unauth_userid:$unauth_password@/api/v1/checkouts" => json =>
446
          { item_id => $item1_id, library_id => $library_id } )->status_is(403)
447
      ->json_is(
448
        {
449
            error => "checkin not allowed",
450
            required_permissions =>
451
              { circulate => "circulate_remaining_permissions" }
452
        }
453
      );
454
    
455
    $t->delete_ok( "//$userid:$password@/api/v1/checkouts" => json =>
456
          { item_id => $item1_id, patron_id => $library_id } )->status_is(400)
457
      ->json_is(
458
        '/error' => 'Item not checked out' );
459
460
    AddIssue( $patron, $item1->barcode );
461
    $t->delete_ok( "//$userid:$password@/api/v1/checkouts" => json =>
462
          { item_id => $item1_id, patron_id => $library_id } )->status_is(201);
463
464
    $item1->delete;
465
    $t->delete_ok( "//$userid:$password@/api/v1/checkouts/" => json =>
466
            { item_id => $item1_id, patron_id => $library_id } )->status_is(409)
467
      ->json_is(
468
        '/error' => 'Item not found' );    
469
    $schema->storage->txn_rollback;
470
};

Return to bug 24401