From 111622efccfc5eb3e36c0d7efab656a66b64656e Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Thu, 30 Jun 2022 17:14:40 +0200 Subject: [PATCH] Bug 32030: Link external packages with agreements Signed-off-by: Jonathan Field Signed-off-by: Martin Renvoize Signed-off-by: Kyle M Hall Signed-off-by: Tomas Cohen Arazi --- Koha/ERM/EHoldings/Package.pm | 20 + Koha/ERM/Providers/EBSCO.pm | 31 ++ Koha/REST/V1/ERM/EHoldings/Packages/Local.pm | 9 +- .../definitions/erm_eholdings_package.yaml | 10 + installer/data/mysql/atomicupdate/erm.pl | 1 + installer/data/mysql/kohastructure.sql | 1 + .../js/vue/components/ERM/AgreementsList.vue | 389 ++++++++++-------- .../ERM/EHoldingsEBSCOPackageAgreements.vue | 157 ++++--- .../ERM/EHoldingsEBSCOPackagesShow.vue | 19 +- .../ERM/EHoldingsLocalPackagesFormAdd.vue | 41 +- .../ERM/EHoldingsLocalPackagesShow.vue | 11 - .../prog/js/vue/components/ERM/Modal.vue | 107 ----- koha-tmpl/intranet-tmpl/prog/js/vue/fetch.js | 47 +++ koha-tmpl/intranet-tmpl/prog/js/vue/routes.js | 2 +- 14 files changed, 464 insertions(+), 381 deletions(-) delete mode 100644 koha-tmpl/intranet-tmpl/prog/js/vue/components/ERM/Modal.vue diff --git a/Koha/ERM/EHoldings/Package.pm b/Koha/ERM/EHoldings/Package.pm index 6ee7975277..a324887589 100644 --- a/Koha/ERM/EHoldings/Package.pm +++ b/Koha/ERM/EHoldings/Package.pm @@ -84,6 +84,26 @@ sub vendor { return Koha::Acquisition::Bookseller->_new_from_dbic($rs); } +=head3 to_api_mapping + +=cut + +sub to_api_mapping { + my ( $self ) = @_; + + return { + external_id => undef, + ( # Do we really need this? + $self->external_id + ? ( + package_id => $self->external_id, + koha_internal_id => $self->package_id + ) + : () + ) + }; +} + =head2 Internal methods =head3 _type diff --git a/Koha/ERM/Providers/EBSCO.pm b/Koha/ERM/Providers/EBSCO.pm index 7df6569b07..1ed7956331 100644 --- a/Koha/ERM/Providers/EBSCO.pm +++ b/Koha/ERM/Providers/EBSCO.pm @@ -9,6 +9,8 @@ use List::Util qw( first ); use Koha::Exceptions; +use Koha::ERM::EHoldings::Packages; + sub new { my $class = shift; my $self = {}; @@ -80,8 +82,13 @@ sub build_vendor { sub build_package { my ( $self, $result ) = @_; + my $local_package = $self->get_local_package( + $result->{vendorId} . '-' . $result->{packageId} ); my $package = { package_id => $result->{vendorId} . '-' . $result->{packageId}, + ( $local_package + ? ( koha_internal_id => $local_package->package_id ) + : () ), name => $result->{packageName}, content_type => $result->{contentType}, # This does not exist in /vendors/1/packages/2/titles/3 created_on => undef, @@ -121,11 +128,18 @@ sub build_additional_params { return $additional_params; } +sub get_local_package { + my ( $self, $package_id ) = @_; + return Koha::ERM::EHoldings::Packages->find( + { provider => 'ebsco', external_id => $package_id } ); +} + sub embed { my ( $self, $object, $info, $embed_header ) = @_; $embed_header ||= q{}; my @embed_resources; + foreach my $embed_req ( split /\s*,\s*/, $embed_header ) { if ( $embed_req eq 'vendor.name' ) { $object->{vendor} = { name => $info->{vendorName}, }; @@ -145,6 +159,23 @@ sub embed { elsif ( $embed_req eq 'package.name' ) { $object->{package} = { name => $info->{packageName}, }; } + elsif ( $embed_req eq 'package_agreements.agreement' ) { + # How to deal with 'package_agreements.agreement'? + $object->{package_agreements} = []; + my $package_id = $info->{vendorId} . '-' . $info->{packageId}; + my $local_package = $self->get_local_package($package_id); + if ( $local_package ) { + for my $package_agreement ( + @{ $local_package->package_agreements->as_list } ) + { + push @{ $object->{package_agreements} }, + { + %{ $package_agreement->unblessed }, + agreement => $package_agreement->agreement->unblessed, + }; + } + } + } if ( $embed_req eq 'resources' || $embed_req eq 'resources.package' ) { push @embed_resources, $embed_req; } diff --git a/Koha/REST/V1/ERM/EHoldings/Packages/Local.pm b/Koha/REST/V1/ERM/EHoldings/Packages/Local.pm index 7bae58f605..f9dfa7022c 100644 --- a/Koha/REST/V1/ERM/EHoldings/Packages/Local.pm +++ b/Koha/REST/V1/ERM/EHoldings/Packages/Local.pm @@ -35,7 +35,8 @@ use Try::Tiny qw( catch try ); sub list { my $c = shift->openapi->valid_input or return; return try { - my $packages_set = Koha::ERM::EHoldings::Packages->new; + my $packages_set = + Koha::ERM::EHoldings::Packages->search( { 'me.external_id' => undef } ); my $packages = $c->objects->search($packages_set); return $c->render( status => 200, openapi => $packages ); } @@ -89,6 +90,7 @@ sub add { my $body = $c->validation->param('body'); my $package_agreements = delete $body->{package_agreements} // []; + delete $body->{external_id} unless $body->{external_id}; my $package = Koha::ERM::EHoldings::Package->new_from_api($body)->store; $package->package_agreements($package_agreements); @@ -164,9 +166,12 @@ sub update { my $body = $c->validation->param('body'); my $package_agreements = delete $body->{package_agreements} // []; - use Data::Printer colored => 1; warn p $package_agreements; + delete $body->{external_id} unless $body->{external_id}; $package->set_from_api($body)->store; + + # FIXME If there is no package_agreements and external_id is set, we could delete the row + # ie. It's coming from EBSCO and we don't have local data linked to it $package->package_agreements($package_agreements); $c->res->headers->location($c->req->url->to_string . '/' . $package->package_id); diff --git a/api/v1/swagger/definitions/erm_eholdings_package.yaml b/api/v1/swagger/definitions/erm_eholdings_package.yaml index b2e055cd1e..190e751772 100644 --- a/api/v1/swagger/definitions/erm_eholdings_package.yaml +++ b/api/v1/swagger/definitions/erm_eholdings_package.yaml @@ -13,11 +13,21 @@ properties: name: description: name of the package type: string + provider: + description: external id of the package + type: + - string + - "null" external_id: description: external id of the package type: - string - "null" + koha_internal_id: + description: internal id of the package + type: + - integer + - "null" package_type: description: type of the package type: diff --git a/installer/data/mysql/atomicupdate/erm.pl b/installer/data/mysql/atomicupdate/erm.pl index e9bf90a1f2..a67ca3b03b 100755 --- a/installer/data/mysql/atomicupdate/erm.pl +++ b/installer/data/mysql/atomicupdate/erm.pl @@ -191,6 +191,7 @@ return { `vendor_id` INT(11) DEFAULT NULL COMMENT 'foreign key to aqbooksellers', `name` VARCHAR(255) NOT NULL COMMENT 'name of the package', `external_id` VARCHAR(255) DEFAULT NULL COMMENT 'External key', + `provider` ENUM('ebsco') DEFAULT NULL COMMENT 'External provider', `package_type` VARCHAR(80) DEFAULT NULL COMMENT 'type of the package', `content_type` VARCHAR(80) DEFAULT NULL COMMENT 'type of the package', `created_on` timestamp NOT NULL DEFAULT current_timestamp() COMMENT 'date of creation of the package', diff --git a/installer/data/mysql/kohastructure.sql b/installer/data/mysql/kohastructure.sql index 8138f2e3a2..7674de170f 100644 --- a/installer/data/mysql/kohastructure.sql +++ b/installer/data/mysql/kohastructure.sql @@ -2902,6 +2902,7 @@ CREATE TABLE `erm_eholdings_packages` ( `vendor_id` INT(11) DEFAULT NULL COMMENT 'foreign key to aqbooksellers', `name` VARCHAR(255) NOT NULL COMMENT 'name of the package', `external_id` VARCHAR(255) DEFAULT NULL COMMENT 'External key', + `provider` ENUM('ebsco') DEFAULT NULL COMMENT 'External provider', `package_type` VARCHAR(80) DEFAULT NULL COMMENT 'type of the package', `content_type` VARCHAR(80) DEFAULT NULL COMMENT 'type of the package', `created_on` timestamp NOT NULL DEFAULT current_timestamp() COMMENT 'date of creation of the package', diff --git a/koha-tmpl/intranet-tmpl/prog/js/vue/components/ERM/AgreementsList.vue b/koha-tmpl/intranet-tmpl/prog/js/vue/components/ERM/AgreementsList.vue index 38fa2c8808..b33747382b 100644 --- a/koha-tmpl/intranet-tmpl/prog/js/vue/components/ERM/AgreementsList.vue +++ b/koha-tmpl/intranet-tmpl/prog/js/vue/components/ERM/AgreementsList.vue @@ -1,7 +1,7 @@