From a9b51b9a85a54f50bc8c84059aa517b3cd220c39 Mon Sep 17 00:00:00 2001 From: Andrew Isherwood Date: Fri, 7 Feb 2020 15:21:49 +0000 Subject: [PATCH] Bug 24609: Create PUT /checkouts/{checkout_id} This patch creates a PUT /checkouts/{checkout_id} API route enabling checkouts to be updated --- Koha/REST/V1/Checkouts.pm | 43 ++++++++++++++++++++++++++++++++ api/v1/swagger/definitions/checkout.json | 14 +++++++++++ api/v1/swagger/paths/checkouts.json | 39 +++++++++++++++++++++++++++++ 3 files changed, 96 insertions(+) diff --git a/Koha/REST/V1/Checkouts.pm b/Koha/REST/V1/Checkouts.pm index c3ae141e67..9bd5221f19 100644 --- a/Koha/REST/V1/Checkouts.pm +++ b/Koha/REST/V1/Checkouts.pm @@ -227,4 +227,47 @@ sub allows_renewal { ); } +=head3 update + +Update a checkout + +=cut + +sub update { + my $c = shift->openapi->valid_input or return; + + my $checkout_id = $c->validation->param('checkout_id'); + my $checkout = Koha::Checkouts->find( $checkout_id ); + + unless ($checkout) { + return $c->render( + status => 404, + openapi => { error => "Checkout doesn't exist" } + ); + } + + return try { + my $params = $c->req->json; + $checkout->set_from_api( $params ); + $checkout->store(); + return $c->render( + status => 200, + openapi => $checkout->to_api + ); + } + catch { + unless ( blessed $_ && $_->can('rethrow') ) { + return $c->render( + status => 500, + openapi => { error => "Something went wrong, check Koha logs for details." } + ); + } + + return $c->render( + status => 500, + openapi => { error => "$_" } + ); + }; +} + 1; diff --git a/api/v1/swagger/definitions/checkout.json b/api/v1/swagger/definitions/checkout.json index 42b9598843..7088de0c65 100644 --- a/api/v1/swagger/definitions/checkout.json +++ b/api/v1/swagger/definitions/checkout.json @@ -3,13 +3,16 @@ "properties": { "checkout_id": { "type": "integer", + "readOnly": true, "description": "internally assigned checkout identifier" }, "patron_id": { + "readOnly": true, "$ref": "../x-primitives.json#/patron_id" }, "item_id": { "type": "integer", + "readOnly": true, "description": "internal identifier of checked out item" }, "due_date": { @@ -19,49 +22,60 @@ }, "library_id": { "type": ["string", "null"], + "readOnly": true, "description": "code of the library the item was checked out" }, "checkin_date": { "type": ["string", "null"], + "readOnly": true, "format": "date-time", "description": "Date the item was returned" }, "last_renewed_date": { "type": ["string", "null"], + "readOnly": true, "format": "date-time", "description": "Date the item was last renewed" }, "renewals": { "type": ["integer", "null"], + "readOnly": true, "description": "Number of renewals" }, "auto_renew": { "type": "boolean", + "readOnly": true, "description": "Auto renewal" }, "auto_renew_error": { "type": ["string", "null"], + "readOnly": true, "description": "Auto renewal error" }, "timestamp": { "type": "string", + "readOnly": true, "description": "Last update time" }, "checkout_date": { "type": "string", + "readOnly": true, "format": "date-time", "description": "Date the item was issued" }, "onsite_checkout": { "type": "boolean", + "readOnly": true, "description": "On site checkout" }, "note": { "type": ["string", "null"], + "readOnly": true, "description": "Issue note text" }, "note_date": { "type": ["string", "null"], + "readOnly": true, "format": "date", "description": "Datetime of the issue note" } diff --git a/api/v1/swagger/paths/checkouts.json b/api/v1/swagger/paths/checkouts.json index ca6d970dbb..e422f8a9f5 100644 --- a/api/v1/swagger/paths/checkouts.json +++ b/api/v1/swagger/paths/checkouts.json @@ -74,6 +74,45 @@ "circulate": "circulate_remaining_permissions" } } + }, + "put": { + "x-mojo-to": "Checkouts#update", + "operationId": "updateCheckout", + "tags": ["patrons", "checkouts"], + "parameters": [ + { + "$ref": "../parameters.json#/checkout_id_pp" + }, + { + "name": "body", + "in": "body", + "description": "A JSON object containing information on the checkout", + "required": true, + "schema": { + "$ref": "../definitions.json#/checkout" + } + } + ], + "produces": ["application/json"], + "responses": { + "200": { + "description": "Updated borrower's checkout", + "schema": { "$ref": "../definitions.json#/checkout" } + }, + "403": { + "description": "Access forbidden", + "schema": { "$ref": "../definitions.json#/error" } + }, + "404": { + "description": "Checkout not found", + "schema": { "$ref": "../definitions.json#/error" } + } + }, + "x-koha-authorization": { + "permissions": { + "circulate": "circulate_remaining_permissions" + } + } } }, "/checkouts/{checkout_id}/renewal": { -- 2.11.0