From c4d95ff7f85ce5e14bd7a18c4cec11fb4d3be26e Mon Sep 17 00:00:00 2001 From: Julian Maurice Date: Wed, 8 Dec 2021 19:21:41 +0100 Subject: [PATCH] Bug 29668: Add API route to add a basket Example usage: POST /api/v1/acquisitions/baskets { "vendor_id": 1, "name": "Basket #1", } Test plan: 1. Try requesting this endpoint with your favorite API tool (I recommend https://github.com/frigus02/RESTer) 2. Run `prove t/db_dependent/api/v1/acquisitions_baskets/post.t` Edit (tcohen): Re-did the commit with some fixes in the spec Signed-off-by: Tomas Cohen Arazi --- Koha/Acquisition/Basket.pm | 1 + Koha/REST/V1/Acquisitions/Baskets.pm | 26 ++++ api/v1/swagger/definitions/basket.yaml | 19 ++- api/v1/swagger/paths.yaml | 145 ++++++++++++++++++ .../swagger/paths/acquisitions_baskets.yaml | 61 +++++++- .../api/v1/acquisitions_baskets/post.t | 68 ++++++++ 6 files changed, 309 insertions(+), 11 deletions(-) create mode 100644 api/v1/swagger/paths.yaml create mode 100755 t/db_dependent/api/v1/acquisitions_baskets/post.t diff --git a/Koha/Acquisition/Basket.pm b/Koha/Acquisition/Basket.pm index a5452414b6e..a3804b926b9 100644 --- a/Koha/Acquisition/Basket.pm +++ b/Koha/Acquisition/Basket.pm @@ -286,6 +286,7 @@ sub to_api_mapping { return { basketno => 'basket_id', basketname => 'name', + note => 'internal_note', booksellernote => 'vendor_note', contractnumber => 'contract_id', creationdate => 'creation_date', diff --git a/Koha/REST/V1/Acquisitions/Baskets.pm b/Koha/REST/V1/Acquisitions/Baskets.pm index b7f39b81448..35dbea45b36 100644 --- a/Koha/REST/V1/Acquisitions/Baskets.pm +++ b/Koha/REST/V1/Acquisitions/Baskets.pm @@ -78,4 +78,30 @@ sub list { }; } +=head3 add + +Controller function that handles adding a new Koha::Acquisition::Basket object + +=cut + +sub add { + my $c = shift->openapi->valid_input or return; + + return try { + my $basket = Koha::Acquisition::Basket->new_from_api( $c->validation->param('body') ); + $basket->store->discard_changes; + + $c->res->headers->location( + $c->req->url->to_string . '/' . $basket->basketno + ); + + return $c->render( + status => 201, + openapi => $basket->to_api + ); + } catch { + $c->unhandled_exception($_); + }; +} + 1; diff --git a/api/v1/swagger/definitions/basket.yaml b/api/v1/swagger/definitions/basket.yaml index 2006079fd7a..0bf6c8b87ae 100644 --- a/api/v1/swagger/definitions/basket.yaml +++ b/api/v1/swagger/definitions/basket.yaml @@ -5,7 +5,9 @@ properties: type: integer description: Internal identifier for the basket name: - type: string + type: + - string + - "null" description: Basket name internal_note: type: @@ -34,6 +36,9 @@ properties: - "null" format: date description: The date the basket was closed + closed: + type: boolean + description: Is the basket closed ? vendor_id: type: integer description: Internal identifier for the vendor @@ -65,14 +70,6 @@ properties: standing: type: boolean description: If the orders in this basket are standing - closed: - type: boolean - description: Is the basket closed - note: - type: - - string - - "null" - description: basket notes create_items: type: - string @@ -84,4 +81,6 @@ properties: - null description: "When items should be created for orders in this basket (Options: 'ordering', 'receiving', 'cataloguing'. Null means system wide config)" -additionalProperties: false \ No newline at end of file +additionalProperties: false +required: + - vendor_id diff --git a/api/v1/swagger/paths.yaml b/api/v1/swagger/paths.yaml new file mode 100644 index 00000000000..08d4f87d332 --- /dev/null +++ b/api/v1/swagger/paths.yaml @@ -0,0 +1,145 @@ +--- +/oauth/token: + $ref: paths/oauth.yaml#/~1oauth~1token +"/acquisitions/baskets": + $ref: paths/acquisitions_baskets.yaml#/~1acquisitions~1baskets +/acquisitions/orders: + $ref: paths/acquisitions_orders.yaml#/~1acquisitions~1orders +"/acquisitions/orders/{order_id}": + $ref: paths/acquisitions_orders.yaml#/~1acquisitions~1orders~1{order_id} +/acquisitions/vendors: + $ref: paths/acquisitions_vendors.yaml#/~1acquisitions~1vendors +"/acquisitions/vendors/{vendor_id}": + $ref: paths/acquisitions_vendors.yaml#/~1acquisitions~1vendors~1{vendor_id} +/acquisitions/funds: + $ref: paths/acquisitions_funds.yaml#/~1acquisitions~1funds +"/article_requests/{article_request_id}": + $ref: paths/article_requests.yaml#/~1article_requests~1{article_request_id} +"/biblios/{biblio_id}": + $ref: paths/biblios.yaml#/~1biblios~1{biblio_id} +"/biblios/{biblio_id}/checkouts": + $ref: paths/biblios.yaml#/~1biblios~1{biblio_id}~1checkouts +"/biblios/{biblio_id}/items": + $ref: paths/biblios.yaml#/~1biblios~1{biblio_id}~1items +"/biblios/{biblio_id}/pickup_locations": + $ref: paths/biblios.yaml#/~1biblios~1{biblio_id}~1pickup_locations +"/cash_registers/{cash_register_id}/cashups": + $ref: paths/cash_registers.yaml#/~1cash_registers~1{cash_register_id}~1cashups +"/cashups/{cashup_id}": + $ref: paths/cash_registers.yaml#/~1cashups~1{cashup_id} +/checkouts: + $ref: paths/checkouts.yaml#/~1checkouts +"/checkouts/{checkout_id}": + $ref: paths/checkouts.yaml#/~1checkouts~1{checkout_id} +"/checkouts/{checkout_id}/renewal": + $ref: paths/checkouts.yaml#/~1checkouts~1{checkout_id}~1renewal +/circulation-rules/kinds: + $ref: paths/circulation-rules.yaml#/~1circulation-rules~1kinds +/cities: + $ref: paths/cities.yaml#/~1cities +"/cities/{city_id}": + $ref: paths/cities.yaml#/~1cities~1{city_id} +"/clubs/{club_id}/holds": + $ref: paths/clubs.yaml#/~1clubs~1{club_id}~1holds +/config/smtp_servers: + $ref: paths/config_smtp_servers.yaml#/~1config~1smtp_servers +"/config/smtp_servers/{smtp_server_id}": + $ref: paths/config_smtp_servers.yaml#/~1config~1smtp_servers~1{smtp_server_id} +/holds: + $ref: paths/holds.yaml#/~1holds +"/holds/{hold_id}": + $ref: paths/holds.yaml#/~1holds~1{hold_id} +"/holds/{hold_id}/priority": + $ref: paths/holds.yaml#/~1holds~1{hold_id}~1priority +"/holds/{hold_id}/suspension": + $ref: paths/holds.yaml#/~1holds~1{hold_id}~1suspension +"/holds/{hold_id}/pickup_locations": + $ref: paths/holds.yaml#/~1holds~1{hold_id}~1pickup_locations +"/holds/{hold_id}/pickup_location": + $ref: paths/holds.yaml#/~1holds~1{hold_id}~1pickup_location +/items: + $ref: paths/items.yaml#/~1items +"/items/{item_id}": + $ref: paths/items.yaml#/~1items~1{item_id} +"/items/{item_id}/pickup_locations": + $ref: paths/items.yaml#/~1items~1{item_id}~1pickup_locations +/libraries: + $ref: paths/libraries.yaml#/~1libraries +"/libraries/{library_id}": + $ref: paths/libraries.yaml#/~1libraries~1{library_id} +/transfer_limits: + $ref: paths/transfer_limits.yaml#/~1transfer_limits +"/transfer_limits/{limit_id}": + $ref: paths/transfer_limits.yaml#/~1transfer_limits~1{limit_id} +/transfer_limits/batch: + $ref: paths/transfer_limits.yaml#/~1transfer_limits~1batch +"/checkouts/{checkout_id}/allows_renewal": + $ref: paths/checkouts.yaml#/~1checkouts~1{checkout_id}~1allows_renewal +/advanced_editor/macros: + $ref: paths/advancededitormacros.yaml#/~1advanced_editor~1macros +"/advanced_editor/macros/{advancededitormacro_id}": + $ref: paths/advancededitormacros.yaml#/~1advanced_editor~1macros~1{advancededitormacro_id} +/advanced_editor/macros/shared: + $ref: paths/advancededitormacros.yaml#/~1advanced_editor~1macros~1shared +"/advanced_editor/macros/shared/{advancededitormacro_id}": + $ref: paths/advancededitormacros.yaml#/~1advanced_editor~1macros~1shared~1{advancededitormacro_id} +/patrons: + $ref: paths/patrons.yaml#/~1patrons +"/patrons/{patron_id}": + $ref: paths/patrons.yaml#/~1patrons~1{patron_id} +"/patrons/{patron_id}/account": + $ref: paths/patrons_account.yaml#/~1patrons~1{patron_id}~1account +"/patrons/{patron_id}/account/credits": + $ref: paths/patrons_account.yaml#/~1patrons~1{patron_id}~1account~1credits +"/patrons/{patron_id}/extended_attributes": + $ref: paths/patrons_extended_attributes.yaml#/~1patrons~1{patron_id}~1extended_attributes +"/patrons/{patron_id}/extended_attributes/{extended_attribute_id}": + $ref: paths/patrons_extended_attributes.yaml#/~1patrons~1{patron_id}~1extended_attributes~1{extended_attribute_id} +"/patrons/{patron_id}/holds": + $ref: paths/patrons_holds.yaml#/~1patrons~1{patron_id}~1holds +"/patrons/{patron_id}/password": + $ref: paths/patrons_password.yaml#/~1patrons~1{patron_id}~1password +/ill_backends: + $ref: paths/ill_backends.yaml#/~1ill_backends +"/ill_backends/{ill_backend_id}": + $ref: paths/ill_backends.yaml#/~1ill_backends~1{ill_backend_id} +/illrequests: + $ref: paths/illrequests.yaml#/~1illrequests +/import_batch_profiles: + $ref: paths/import_batch_profiles.yaml#/~1import_batch_profiles +"/import_batch_profiles/{import_batch_profile_id}": + $ref: paths/import_batch_profiles.yaml#/~1import_batch_profiles~1{import_batch_profile_id} +"/rotas/{rota_id}/stages/{stage_id}/position": + $ref: paths/rotas.yaml#/~1rotas~1{rota_id}~1stages~1{stage_id}~1position +"/public/biblios/{biblio_id}": + $ref: paths/biblios.yaml#/~1public~1biblios~1{biblio_id} +"/public/biblios/{biblio_id}/items": + $ref: paths/biblios.yaml#/~1public~1biblios~1{biblio_id}~1items +/public/libraries: + $ref: paths/libraries.yaml#/~1public~1libraries +"/public/libraries/{library_id}": + $ref: paths/libraries.yaml#/~1public~1libraries~1{library_id} +"/public/patrons/{patron_id}/article_requests/{article_request_id}": + $ref: paths/article_requests.yaml#/~1public~1patrons~1{patron_id}~1article_requests~1{article_request_id} +"/public/patrons/{patron_id}/password": + $ref: paths/public_patrons.yaml#/~1public~1patrons~1{patron_id}~1password +"/public/patrons/{patron_id}/guarantors/can_see_charges": + $ref: paths/public_patrons.yaml#/~1public~1patrons~1{patron_id}~1guarantors~1can_see_charges +"/public/patrons/{patron_id}/guarantors/can_see_checkouts": + $ref: paths/public_patrons.yaml#/~1public~1patrons~1{patron_id}~1guarantors~1can_see_checkouts +/quotes: + $ref: paths/quotes.yaml#/~1quotes +"/quotes/{quote_id}": + $ref: paths/quotes.yaml#/~1quotes~1{quote_id} +/return_claims: + $ref: paths/return_claims.yaml#/~1return_claims +"/return_claims/{claim_id}/notes": + $ref: paths/return_claims.yaml#/~1return_claims~1{claim_id}~1notes +"/return_claims/{claim_id}/resolve": + $ref: paths/return_claims.yaml#/~1return_claims~1{claim_id}~1resolve +"/return_claims/{claim_id}": + $ref: paths/return_claims.yaml#/~1return_claims~1{claim_id} +/suggestions: + $ref: paths/suggestions.yaml#/~1suggestions +"/suggestions/{suggestion_id}": + $ref: paths/suggestions.yaml#/~1suggestions~1{suggestion_id} diff --git a/api/v1/swagger/paths/acquisitions_baskets.yaml b/api/v1/swagger/paths/acquisitions_baskets.yaml index eab24251181..0139f2103a8 100644 --- a/api/v1/swagger/paths/acquisitions_baskets.yaml +++ b/api/v1/swagger/paths/acquisitions_baskets.yaml @@ -48,6 +48,65 @@ x-koha-authorization: permissions: acquisition: order_manage + post: + x-mojo-to: Acquisitions::Baskets#add + operationId: addBasket + tags: + - baskets + summary: Add basket + consumes: + - application/json + produces: + - application/json + parameters: + - + name: body + in: body + description: A basket + required: true + schema: + $ref: "../swagger.yaml#/definitions/basket" + responses: + "201": + description: A successfully created basket + schema: + items: + $ref: "../swagger.yaml#/definitions/basket" + "400": + description: Bad request + schema: + $ref: "../swagger.yaml#/definitions/error" + "401": + description: Authentication required + schema: + $ref: "../swagger.yaml#/definitions/error" + "403": + description: Access forbidden + schema: + $ref: "../swagger.yaml#/definitions/error" + "404": + description: Resource not found + schema: + $ref: "../swagger.yaml#/definitions/error" + "409": + description: Conflict in creating resource + schema: + $ref: "../swagger.yaml#/definitions/error" + "500": + description: | + Internal server error. Possible `error_code` attribute values: + + * `internal_server_error` + schema: + $ref: "../swagger.yaml#/definitions/error" + "503": + description: Under maintenance + schema: + $ref: "../swagger.yaml#/definitions/error" + x-koha-authorization: + permissions: + acquisition: + - order_manage /acquisitions/baskets/managers: get: x-mojo-to: Acquisitions::Baskets#list_managers @@ -107,4 +166,4 @@ $ref: "../swagger.yaml#/definitions/error" x-koha-authorization: permissions: - acquisition: order_manage \ No newline at end of file + acquisition: order_manage diff --git a/t/db_dependent/api/v1/acquisitions_baskets/post.t b/t/db_dependent/api/v1/acquisitions_baskets/post.t new file mode 100755 index 00000000000..8a7536d8822 --- /dev/null +++ b/t/db_dependent/api/v1/acquisitions_baskets/post.t @@ -0,0 +1,68 @@ +#!/usr/bin/env perl + +use Modern::Perl; + +use Test::More tests => 14; +use Test::Mojo; + +use t::lib::Mocks; +use t::lib::TestBuilder; + +my $schema = Koha::Database->new->schema; +my $builder = t::lib::TestBuilder->new; + +t::lib::Mocks::mock_preference( 'RESTBasicAuth', 1 ); + +my $t = Test::Mojo->new('Koha::REST::V1'); + +$schema->storage->txn_begin; + +my $bookseller = $builder->build_object( + { + class => 'Koha::Acquisition::Booksellers', + } +); +my $patron = $builder->build_object( + { + class => 'Koha::Patrons', + value => { flags => 0 } + } +); +my $password = 'thePassword123'; +$patron->set_password( { password => $password, skip_validation => 1 } ); +my $userid = $patron->userid; + +my $url = "//$userid:$password@/api/v1/acquisitions/baskets"; + +$t->post_ok($url, json => {}) + ->status_is(403); + +$schema->resultset('UserPermission')->create( + { + borrowernumber => $patron->borrowernumber, + module_bit => 11, + code => 'order_manage', + } +); + +$t->post_ok($url, json => {}) + ->status_is(400); + +$t->post_ok($url, json => { vendor_id => $bookseller->id }) + ->status_is(201) + ->json_has('/basket_id') + ->json_is('/vendor_id', $bookseller->id); + +my $basket = { + vendor_id => $bookseller->id, + name => 'Basket #1', + vendor_note => 'Vendor note', +}; +$t->post_ok($url, json => $basket) + ->status_is(201) + ->json_has('/basket_id') + ->json_is('/vendor_id', $basket->{vendor_id}) + ->json_is('/name', $basket->{name}) + ->json_is('/vendor_note', $basket->{vendor_note}); + +$schema->storage->txn_rollback; -- 2.49.0