From 65cffa9619a319447dfee8ec3f73ab82978696cb Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Wed, 3 Aug 2022 16:25:16 +0200 Subject: [PATCH] Bug 32030: Add/remove packages to/from EBSCO's holdings Signed-off-by: Jonathan Field Signed-off-by: Martin Renvoize Signed-off-by: Kyle M Hall Signed-off-by: Tomas Cohen Arazi --- Koha/REST/V1/ERM/EHoldings/Packages.pm | 17 ++++++ Koha/REST/V1/ERM/EHoldings/Packages/EBSCO.pm | 43 +++++++++++++ .../swagger/paths/erm_eholdings_packages.yaml | 61 +++++++++++++++++++ .../ERM/EHoldingsEBSCOPackagesShow.vue | 51 +++++++++++++++- 4 files changed, 170 insertions(+), 2 deletions(-) diff --git a/Koha/REST/V1/ERM/EHoldings/Packages.pm b/Koha/REST/V1/ERM/EHoldings/Packages.pm index 1e7fdf8b6d..9552e0c933 100644 --- a/Koha/REST/V1/ERM/EHoldings/Packages.pm +++ b/Koha/REST/V1/ERM/EHoldings/Packages.pm @@ -110,4 +110,21 @@ sub delete { } } +=head3 edit + +Controller function that handles editing a single Koha::ERM::EHoldings::Package object + +=cut + +sub edit { + my $c = shift->openapi->valid_input or return; + + my $provider = $c->validation->param('provider'); + if ( $provider eq 'ebsco' ) { + return Koha::REST::V1::ERM::EHoldings::Packages::EBSCO::edit($c); + } else { + die "invalid action"; + } +} + 1; diff --git a/Koha/REST/V1/ERM/EHoldings/Packages/EBSCO.pm b/Koha/REST/V1/ERM/EHoldings/Packages/EBSCO.pm index 938ebcd45e..3f3962856f 100644 --- a/Koha/REST/V1/ERM/EHoldings/Packages/EBSCO.pm +++ b/Koha/REST/V1/ERM/EHoldings/Packages/EBSCO.pm @@ -104,4 +104,47 @@ sub get { }; } +sub edit { + my $c = shift->openapi->valid_input or return; + + return try { + my $body = $c->validation->param('body'); + my $is_selected = $body->{is_selected}; + my ( $vendor_id, $package_id ) = split '-', + $c->validation->param('package_id'); + + my $ebsco = Koha::ERM::Providers::EBSCO->new; + my $t = try { + $ebsco->request( + PUT => '/vendors/' . $vendor_id . '/packages/' . $package_id, + undef, + { + isSelected => $is_selected, + } + ); + } + catch { + if ( blessed $_ ) { + if ( $_->isa('Koha::Exceptions::ObjectNotFound') ) { + return $c->render( + status => 404, + openapi => { error => $_->error } + ); + + } + } + + $c->unhandled_exception($_); + }; + + return $c->render( + status => 200, + openapi => { is_selected => $is_selected } # We don't want to refetch the resource to make sure it has been updated + ); + } + catch { + $c->unhandled_exception($_); + }; +} + 1; diff --git a/api/v1/swagger/paths/erm_eholdings_packages.yaml b/api/v1/swagger/paths/erm_eholdings_packages.yaml index 2cefd89fa2..7064e4a2f3 100644 --- a/api/v1/swagger/paths/erm_eholdings_packages.yaml +++ b/api/v1/swagger/paths/erm_eholdings_packages.yaml @@ -295,3 +295,64 @@ x-koha-authorization: permissions: erm: 1 + patch: + x-mojo-to: ERM::EHoldings::Packages#edit + operationId: editErmEHoldingsPackages + tags: + - eholdings + summary: Edit a package + produces: + - application/json + parameters: + - description: Provider name + in: path + name: provider + required: true + type: string + - description: A JSON object containing the fields to edit + in: body + name: body + required: true + schema: + type: object + properties: + is_selected: + description: Add or remove this package from remote holdings + type: boolean + additionalProperties: false + - $ref: "../swagger.yaml#/parameters/eholdings_package_id_pp" + responses: + 200: + description: What has been modified + schema: + type: object + properties: + is_selected: + description: Add or remove this package from remote holdings + type: boolean + additionalProperties: false + 401: + description: Authentication required + schema: + $ref: "../swagger.yaml#/definitions/error" + 403: + description: Access forbidden + schema: + $ref: "../swagger.yaml#/definitions/error" + 404: + description: Package not found + 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: + erm: 1 diff --git a/koha-tmpl/intranet-tmpl/prog/js/vue/components/ERM/EHoldingsEBSCOPackagesShow.vue b/koha-tmpl/intranet-tmpl/prog/js/vue/components/ERM/EHoldingsEBSCOPackagesShow.vue index 30d0bc13c0..3011dca051 100644 --- a/koha-tmpl/intranet-tmpl/prog/js/vue/components/ERM/EHoldingsEBSCOPackagesShow.vue +++ b/koha-tmpl/intranet-tmpl/prog/js/vue/components/ERM/EHoldingsEBSCOPackagesShow.vue @@ -3,6 +3,23 @@

{{ $t("Package .id", { id: erm_package.package_id }) }} + + Add to holdings + Remove from holdings

@@ -89,7 +106,7 @@ import EHoldingsPackageAgreements from "./EHoldingsEBSCOPackageAgreements.vue" import EHoldingsPackageTitlesList from "./EHoldingsEBSCOPackageTitlesList.vue" import { useAVStore } from "../../stores/authorised_values" -import { fetchEBSCOPackage } from "../../fetch" +import { fetchEBSCOPackage, checkError } from "../../fetch" export default { setup() { @@ -118,6 +135,7 @@ export default { package_agreements: [], }, initialized: false, + updating_is_selected: false, } }, beforeRouteEnter(to, from, next) { @@ -133,6 +151,35 @@ export default { const erm_package = await fetchEBSCOPackage(package_id) this.erm_package = erm_package this.initialized = true + this.updating_is_selected = false + }, + edit_selected(is_selected) { + this.updating_is_selected = true + fetch('/api/v1/erm/eholdings/ebsco/packages/' + this.erm_package.package_id, { + method: "PATCH", + body: JSON.stringify({ is_selected }), + headers: { + 'Accept': 'application/json', + 'Content-Type': 'application/json' + }, + }) + .then(checkError) + .then( + (result) => { + // Refresh the page. We should not need that actually. + this.getPackage(this.erm_package.package_id) + }, + ).catch( + (error) => { + setError(error) + } + ) + }, + add_to_holdings() { + this.edit_selected(true) + }, + remove_from_holdings() { + this.edit_selected(false) }, refreshAgreements() { // FIXME We could GET /erm/eholdings/packages/$package_id/agreements instead @@ -151,4 +198,4 @@ export default { fieldset.rows label { width: 25rem; } - \ No newline at end of file + -- 2.39.5