From 8df00b3634a8b702c4a890a9aad1f2c4c30e95ea Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Mon, 23 May 2022 14:29:20 +0200 Subject: [PATCH] Bug 32030: Link agreements to a package Signed-off-by: Jonathan Field Signed-off-by: Martin Renvoize Signed-off-by: Kyle M Hall Signed-off-by: Tomas Cohen Arazi --- Koha/ERM/Package.pm | 26 ++++++ Koha/ERM/Package/Agreement.pm | 58 +++++++++++++ Koha/ERM/Package/Agreements.pm | 49 +++++++++++ Koha/REST/V1/ERM/Packages.pm | 6 ++ api/v1/swagger/definitions/erm_package.yaml | 5 ++ .../definitions/erm_package_agreement.yaml | 14 +++ api/v1/swagger/paths/erm_packages.yaml | 5 +- .../vue/components/ERM/PackageAgreements.vue | 87 +++++++++++++++++++ .../js/vue/components/ERM/PackagesFormAdd.vue | 9 +- koha-tmpl/intranet-tmpl/prog/js/vue/fetch.js | 6 +- 10 files changed, 260 insertions(+), 5 deletions(-) create mode 100644 Koha/ERM/Package/Agreement.pm create mode 100644 Koha/ERM/Package/Agreements.pm create mode 100644 api/v1/swagger/definitions/erm_package_agreement.yaml create mode 100644 koha-tmpl/intranet-tmpl/prog/js/vue/components/ERM/PackageAgreements.vue diff --git a/Koha/ERM/Package.pm b/Koha/ERM/Package.pm index 359e0906d3..52680c5367 100644 --- a/Koha/ERM/Package.pm +++ b/Koha/ERM/Package.pm @@ -21,6 +21,8 @@ use Koha::Database; use base qw(Koha::Object); +use Koha::ERM::Package::Agreements; + =head1 NAME Koha::ERM::Package - Koha ERM Package Object class @@ -29,8 +31,32 @@ Koha::ERM::Package - Koha ERM Package Object class =head2 Class Methods +=head3 package_agreements + +Returns the package agreements link for this package + =cut +sub package_agreements { + my ( $self, $package_agreements ) = @_; + + if ( $package_agreements ) { + my $schema = $self->_result->result_source->schema; + $schema->txn_do( + sub { + $self->package_agreements->delete; + + for my $package_agreement (@$package_agreements) { + $self->_result->add_to_erm_packages_agreements($package_agreement); + } + } + ); + } + + my $agreements_rs = $self->_result->erm_packages_agreements; + return Koha::ERM::Package::Agreements->_new_from_dbic($agreements_rs); +} + =head2 Internal methods =head3 _type diff --git a/Koha/ERM/Package/Agreement.pm b/Koha/ERM/Package/Agreement.pm new file mode 100644 index 0000000000..8b421ed398 --- /dev/null +++ b/Koha/ERM/Package/Agreement.pm @@ -0,0 +1,58 @@ +package Koha::ERM::Package::Agreement; + +# This file is part of Koha. +# +# Koha is free software; you can redistribute it and/or modify it +# under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# Koha is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Koha; if not, see . + +use Modern::Perl; + +use Koha::Database; + +use Koha::ERM::Agreement; + +use base qw(Koha::Object); + +=head1 NAME + +Koha::ERM::Package::Agreement - Koha Package Agreement Object class + +=head1 API + +=head2 Class Methods + +=cut + +=head3 agreement + +Return the agreement linked to this package agreement + +=cut + +sub agreement { + my ( $self ) = @_; + my $agreement_rs = $self->_result->agreement; + return Koha::ERM::Agreement->_new_from_dbic($agreement_rs); +} + +=head2 Internal methods + +=head3 _type + +=cut + +sub _type { + return 'ErmPackagesAgreement'; +} + +1; diff --git a/Koha/ERM/Package/Agreements.pm b/Koha/ERM/Package/Agreements.pm new file mode 100644 index 0000000000..62dbacc7b5 --- /dev/null +++ b/Koha/ERM/Package/Agreements.pm @@ -0,0 +1,49 @@ +package Koha::ERM::Package::Agreements; + +# This file is part of Koha. +# +# Koha is free software; you can redistribute it and/or modify it +# under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# Koha is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Koha; if not, see . + +use Modern::Perl; + + +use Koha::Database; + +use Koha::ERM::Package::Agreement; + +use base qw(Koha::Objects); + +=head1 NAME + +Koha::ERM::Package::Agreements - Koha Package Agreement Object set class + +=head1 API + +=head2 Class Methods + +=cut + +=head3 type + +=cut + +sub _type { + return 'ErmPackageAgreement'; +} + +sub object_class { + return 'Koha::ERM::Package::Agreement'; +} + +1; diff --git a/Koha/REST/V1/ERM/Packages.pm b/Koha/REST/V1/ERM/Packages.pm index 3e16a92ca8..721994bf2d 100644 --- a/Koha/REST/V1/ERM/Packages.pm +++ b/Koha/REST/V1/ERM/Packages.pm @@ -91,7 +91,10 @@ sub add { my $body = $c->validation->param('body'); + my $package_agreements = delete $body->{package_agreements} // []; + my $package = Koha::ERM::Package->new_from_api($body)->store; + $package->package_agreements($package_agreements); $c->res->headers->location($c->req->url->to_string . '/' . $package->package_id); return $c->render( @@ -163,7 +166,10 @@ sub update { my $body = $c->validation->param('body'); + my $package_agreements = delete $body->{package_agreements} // []; + $package->set_from_api($body)->store; + $package->package_agreements($package_agreements); $c->res->headers->location($c->req->url->to_string . '/' . $package->package_id); return $c->render( diff --git a/api/v1/swagger/definitions/erm_package.yaml b/api/v1/swagger/definitions/erm_package.yaml index 629b72a379..2ce6417e64 100644 --- a/api/v1/swagger/definitions/erm_package.yaml +++ b/api/v1/swagger/definitions/erm_package.yaml @@ -33,6 +33,11 @@ properties: type: - string - "null" + package_agreements: + type: array + description: agreements + items: + $ref: erm_package_agreement.yaml additionalProperties: false required: diff --git a/api/v1/swagger/definitions/erm_package_agreement.yaml b/api/v1/swagger/definitions/erm_package_agreement.yaml new file mode 100644 index 0000000000..73233b5923 --- /dev/null +++ b/api/v1/swagger/definitions/erm_package_agreement.yaml @@ -0,0 +1,14 @@ +--- +type: object +properties: + package_id: + type: integer + description: foreign key to package + readOnly: true + agreement_id: + description: foreign key to agreement + type: integer + +additionalProperties: false +required: + - agreement_id diff --git a/api/v1/swagger/paths/erm_packages.yaml b/api/v1/swagger/paths/erm_packages.yaml index 8ec0ef0135..cbea401097 100644 --- a/api/v1/swagger/paths/erm_packages.yaml +++ b/api/v1/swagger/paths/erm_packages.yaml @@ -173,6 +173,8 @@ x-koha-authorization: permissions: erm: 1 + x-koha-embed: + - agreements put: x-mojo-to: ERM::Packages#update operationId: updateErmPackages @@ -191,7 +193,6 @@ required: true schema: $ref: "../swagger.yaml#/definitions/erm_package" - responses: 200: description: A successfully updated package @@ -227,6 +228,8 @@ x-koha-authorization: permissions: erm: 1 + x-koha-embed: + - agreements delete: x-mojo-to: ERM::Packages#delete operationId: deleteErmPackages diff --git a/koha-tmpl/intranet-tmpl/prog/js/vue/components/ERM/PackageAgreements.vue b/koha-tmpl/intranet-tmpl/prog/js/vue/components/ERM/PackageAgreements.vue new file mode 100644 index 0000000000..544c874b03 --- /dev/null +++ b/koha-tmpl/intranet-tmpl/prog/js/vue/components/ERM/PackageAgreements.vue @@ -0,0 +1,87 @@ + + + \ No newline at end of file diff --git a/koha-tmpl/intranet-tmpl/prog/js/vue/components/ERM/PackagesFormAdd.vue b/koha-tmpl/intranet-tmpl/prog/js/vue/components/ERM/PackagesFormAdd.vue index 09a52c3f61..43a713104b 100644 --- a/koha-tmpl/intranet-tmpl/prog/js/vue/components/ERM/PackagesFormAdd.vue +++ b/koha-tmpl/intranet-tmpl/prog/js/vue/components/ERM/PackagesFormAdd.vue @@ -92,7 +92,7 @@ @@ -142,6 +142,7 @@ export default { external_package_id: null, package_type: '', content_type: '', + package_agreements: [], }, initialized: false, } @@ -174,6 +175,8 @@ export default { } delete erm_package.package_id + erm_package.package_agreements = erm_package.package_agreements.map(({ package_id, agreement, ...keepAttrs }) => keepAttrs) + const options = { method: method, body: JSON.stringify(erm_package), @@ -198,9 +201,9 @@ export default { }).catch(e => { console.log(e) }) }, }, - components: [ + components: { PackageAgreements, - ], + }, name: "PackagesFormAdd", } \ No newline at end of file diff --git a/koha-tmpl/intranet-tmpl/prog/js/vue/fetch.js b/koha-tmpl/intranet-tmpl/prog/js/vue/fetch.js index a321c5b565..07ab02a9ee 100644 --- a/koha-tmpl/intranet-tmpl/prog/js/vue/fetch.js +++ b/koha-tmpl/intranet-tmpl/prog/js/vue/fetch.js @@ -108,7 +108,11 @@ export const fetchPackage = async function (package_id) { if (!package_id) return; const apiUrl = "/api/v1/erm/packages/" + package_id; let erm_package; - await fetch(apiUrl) + await fetch(apiUrl, { + headers: { + "x-koha-embed": "package_agreements,package_agreements.agreement", + }, + }) .then((res) => res.json()) .then( (result) => { -- 2.39.5