Bug 32030: Add/remove packages to/from EBSCO's holdings
authorJonathan Druart <jonathan.druart@bugs.koha-community.org>
Wed, 3 Aug 2022 14:25:16 +0000 (16:25 +0200)
committerTomas Cohen Arazi <tomascohen@theke.io>
Tue, 8 Nov 2022 12:44:23 +0000 (09:44 -0300)
Signed-off-by: Jonathan Field <jonathan.field@ptfs-europe.com>
Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>
Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>
Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>
Koha/REST/V1/ERM/EHoldings/Packages.pm
Koha/REST/V1/ERM/EHoldings/Packages/EBSCO.pm
api/v1/swagger/paths/erm_eholdings_packages.yaml
koha-tmpl/intranet-tmpl/prog/js/vue/components/ERM/EHoldingsEBSCOPackagesShow.vue

index 1e7fdf8..9552e0c 100644 (file)
@@ -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;
index 938ebcd..3f39628 100644 (file)
@@ -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;
index 2cefd89..7064e4a 100644 (file)
     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
index 30d0bc1..3011dca 100644 (file)
@@ -3,6 +3,23 @@
     <div v-else-if="erm_package" id="packages_show">
         <h2>
             {{ $t("Package .id", { id: erm_package.package_id }) }}
+            <span v-if="!updating_is_selected">
+                <a
+                    v-if="!erm_package.is_selected"
+                    class="btn btn-default btn-xs"
+                    role="button"
+                    @click="add_to_holdings"
+                    ><font-awesome-icon icon="plus" /> Add to holdings</a
+                >
+                <a
+                    v-else
+                    class="btn btn-default btn-xs"
+                    role="button"
+                    id="remove-from-holdings"
+                    @click="remove_from_holdings"
+                    ><font-awesome-icon icon="minus" /> Remove from holdings</a
+                > </span
+            ><span v-else><font-awesome-icon icon="spinner" /></span>
         </h2>
         <div>
             <fieldset class="rows">
 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;
 }
-</style>
\ No newline at end of file
+</style>