From 7a81358ad70b58ee4ac5ebda6e3aea63c8ce25db Mon Sep 17 00:00:00 2001 From: Pedro Amorim Date: Wed, 19 Oct 2022 11:29:19 +0000 Subject: [PATCH] Bug 32030: Add document to license - Preparation step Typo in fieldset id Signed-off-by: Jonathan Field Signed-off-by: Martin Renvoize Signed-off-by: Kyle M Hall Signed-off-by: Tomas Cohen Arazi --- Koha/ERM/Agreement.pm | 61 ++------- Koha/ERM/Agreement/Documents.pm | 49 ------- Koha/ERM/{Agreement => }/Document.pm | 6 +- Koha/ERM/Documents.pm | 121 ++++++++++++++++++ Koha/REST/V1/ERM/Documents.pm | 9 +- .../vue/components/ERM/AgreementsFormAdd.vue | 6 +- .../js/vue/components/ERM/AgreementsShow.vue | 2 +- .../{AgreementDocuments.vue => Documents.vue} | 6 +- 8 files changed, 145 insertions(+), 115 deletions(-) delete mode 100644 Koha/ERM/Agreement/Documents.pm rename Koha/ERM/{Agreement => }/Document.pm (87%) create mode 100644 Koha/ERM/Documents.pm rename koha-tmpl/intranet-tmpl/prog/js/vue/components/ERM/{AgreementDocuments.vue => Documents.vue} (97%) diff --git a/Koha/ERM/Agreement.pm b/Koha/ERM/Agreement.pm index 2c6692842f..b2ab338d87 100644 --- a/Koha/ERM/Agreement.pm +++ b/Koha/ERM/Agreement.pm @@ -31,7 +31,7 @@ use Koha::ERM::Agreement::Periods; use Koha::ERM::Agreement::UserRoles; use Koha::ERM::Agreement::Licenses; use Koha::ERM::Agreement::Relationships; -use Koha::ERM::Agreement::Documents; +use Koha::ERM::Documents; use Koha::ERM::EHoldings::Package::Agreements; =head1 NAME @@ -190,60 +190,19 @@ Returns the documents for this agreement =cut +=head3 documents + +Returns or updates the documents for this agreement + +=cut + sub documents { my ( $self, $documents ) = @_; - if ($documents) { - my $schema = $self->_result->result_source->schema; - $schema->txn_do( - sub { - my $existing_documents = $self->documents; - - # FIXME Here we are not deleting all the documents before recreating them, like we do for other related resources. - # As we do not want the content of the documents to transit over the network we need to use the document_id (and allow it in the API spec) - # to distinguish from each other - # Delete all the documents that are not part of the PUT request - my $modified_document_ids = [ map { $_->{document_id} || () } @$documents ]; - $self->documents->search( - { - @$modified_document_ids - ? ( - document_id => { - '-not_in' => $modified_document_ids - } - ) - : () - } - )->delete; - - for my $document (@$documents) { - if ( $document->{document_id} ) { - # The document already exists in DB - $existing_documents->find( $document->{document_id} ) - ->set( - { - file_description => $document->{file_description}, - physical_location => $document->{physical_location}, - uri => $document->{uri}, - notes => $document->{notes}, - } - )->store; - } - else { - # Creating a whole new document - my $file_content = decode_base64( $document->{file_content} ); - my $mt = MIME::Types->new(); - $document->{file_type} = $mt->mimeTypeOf( $document->{file_name} ); - $document->{uploaded_on} //= dt_from_string; - $document->{file_content} = $file_content; - $self->_result->add_to_erm_agreement_documents( $document); - } - } - } - ); + $self->documents->replace_with($documents, $self); } - my $documents_rs = $self->_result->erm_agreement_documents; - return Koha::ERM::Agreement::Documents->_new_from_dbic($documents_rs); + my $documents_rs = $self->_result->erm_documents; + return Koha::ERM::Documents->_new_from_dbic($documents_rs); } =head3 agreement_packages diff --git a/Koha/ERM/Agreement/Documents.pm b/Koha/ERM/Agreement/Documents.pm deleted file mode 100644 index 73aee775fa..0000000000 --- a/Koha/ERM/Agreement/Documents.pm +++ /dev/null @@ -1,49 +0,0 @@ -package Koha::ERM::Agreement::Documents; - -# 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::Document; - -use base qw(Koha::Objects); - -=head1 NAME - -Koha::ERM::Agreement::Documents- Koha Agreement Document Object set class - -=head1 API - -=head2 Class Methods - -=cut - -=head3 type - -=cut - -sub _type { - return 'ErmAgreementDocument'; -} - -sub object_class { - return 'Koha::ERM::Agreement::Document'; -} - -1; diff --git a/Koha/ERM/Agreement/Document.pm b/Koha/ERM/Document.pm similarity index 87% rename from Koha/ERM/Agreement/Document.pm rename to Koha/ERM/Document.pm index d83a1c7b71..b4f5a1f855 100644 --- a/Koha/ERM/Agreement/Document.pm +++ b/Koha/ERM/Document.pm @@ -1,4 +1,4 @@ -package Koha::ERM::Agreement::Document; +package Koha::ERM::Document; # This file is part of Koha. # @@ -23,7 +23,7 @@ use base qw(Koha::Object); =head1 NAME -Koha::ERM::Agreement::Document - Koha Agreement Document Object class +Koha::ERM::Document - Koha ERM Document Object class =head1 API @@ -49,7 +49,7 @@ sub to_api_mapping { =cut sub _type { - return 'ErmAgreementDocument'; + return 'ErmDocument'; } 1; diff --git a/Koha/ERM/Documents.pm b/Koha/ERM/Documents.pm new file mode 100644 index 0000000000..8da3c1d300 --- /dev/null +++ b/Koha/ERM/Documents.pm @@ -0,0 +1,121 @@ +package Koha::ERM::Documents; + +# 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 MIME::Base64 qw( decode_base64 ); +use MIME::Types; + +use Koha::Database; +use Koha::DateUtils qw( dt_from_string ); +use Koha::ERM::Document; + +use base qw(Koha::Objects); + +=head1 NAME + +Koha::ERM::Documents- Koha ERM Document Object set class + +=head1 API + +=head2 Class Methods + +=cut + +=head3 replace_with + +Replaces ERM Agreement or ERM License documents with new updated ones + +=cut + +sub replace_with { + my ( $self, $documents, $obj ) = @_; + my $schema = $obj->_result->result_source->schema; + $schema->txn_do( + sub { + my $existing_documents = $obj->documents; + + # FIXME Here we are not deleting all the documents before recreating them, like we do for other related resources. + # As we do not want the content of the documents to transit over the network we need to use the document_id (and allow it in the API spec) + # to distinguish from each other + # Delete all the documents that are not part of the PUT request + my $document_ids_after_update = [ map { $_->{document_id} || () } @$documents ]; + $obj->documents->search( + { + @$document_ids_after_update + ? ( + document_id => { + '-not_in' => $document_ids_after_update + } + ) + : () + } + )->delete; + + for my $document (@$documents) { + my $file_content = defined($document->{file_content}) ? decode_base64( $document->{file_content} ) : ""; + my $mt = MIME::Types->new(); + if ( $document->{document_id} ) { + # The document already exists in DB + $existing_documents->find( $document->{document_id} ) + ->set( + { + #Check if existing document had its file changed + length $file_content + ? ( + file_content => $file_content, + file_type => $mt->mimeTypeOf( $document->{file_name} ), + file_name => $document->{file_name}, + uploaded_on => dt_from_string, + ) + : (), + file_description => $document->{file_description}, + physical_location => $document->{physical_location}, + uri => $document->{uri}, + notes => $document->{notes}, + } + )->store; + } + else { + # Creating a whole new document + $document->{file_type} = $mt->mimeTypeOf( $document->{file_name} ); + $document->{uploaded_on} //= dt_from_string; + $document->{file_content} = $file_content; + $obj->_result->add_to_erm_documents( $document); + } + } + } + ); +} + +=head3 type + +=cut + +sub _type { + return 'ErmDocument'; +} + +=head3 object_class + +=cut + +sub object_class { + return 'Koha::ERM::Document'; +} + +1; diff --git a/Koha/REST/V1/ERM/Documents.pm b/Koha/REST/V1/ERM/Documents.pm index 47594f58b9..1e0575f87e 100644 --- a/Koha/REST/V1/ERM/Documents.pm +++ b/Koha/REST/V1/ERM/Documents.pm @@ -19,7 +19,7 @@ use Modern::Perl; use Mojo::Base 'Mojolicious::Controller'; -use Koha::ERM::Agreement::Documents; +use Koha::ERM::Documents; use Scalar::Util qw( blessed ); use Try::Tiny qw( catch try ); @@ -30,7 +30,7 @@ use Try::Tiny qw( catch try ); =head3 get -Controller function that handles retrieving a single Koha::ERM::Agreement object +Controller function that handles retrieving a single Koha::ERM::Document object =cut @@ -38,13 +38,12 @@ sub get { my $c = shift->openapi->valid_input or return; return try { - my $agreement_id = $c->validation->param('agreement_id'); my $document_id = $c->validation->param('document_id'); # Do not use $c->objects->find here, we need the file_content - my $document = Koha::ERM::Agreement::Documents->find($document_id); + my $document = Koha::ERM::Documents->find($document_id); - if ( !$document || $document->agreement_id != $agreement_id) { + if ( !$document ) { return $c->render( status => 404, openapi => { error => "Document not found" } diff --git a/koha-tmpl/intranet-tmpl/prog/js/vue/components/ERM/AgreementsFormAdd.vue b/koha-tmpl/intranet-tmpl/prog/js/vue/components/ERM/AgreementsFormAdd.vue index 76f6aa8c1d..28e0fa92f3 100644 --- a/koha-tmpl/intranet-tmpl/prog/js/vue/components/ERM/AgreementsFormAdd.vue +++ b/koha-tmpl/intranet-tmpl/prog/js/vue/components/ERM/AgreementsFormAdd.vue @@ -155,7 +155,7 @@ av_agreement_relationships " /> - +
@@ -178,7 +178,7 @@ import AgreementPeriods from './AgreementPeriods.vue' import AgreementUserRoles from './AgreementUserRoles.vue' import AgreementLicenses from './AgreementLicenses.vue' import AgreementRelationships from './AgreementRelationships.vue' -import AgreementDocuments from './AgreementDocuments.vue' +import Documents from './Documents.vue' import { setMessage, setError, setWarning } from "../../messages" import { fetchAgreement } from '../../fetch' import { storeToRefs } from "pinia" @@ -358,7 +358,7 @@ export default { AgreementUserRoles, AgreementLicenses, AgreementRelationships, - AgreementDocuments, + Documents, }, name: "AgreementsFormAdd", } diff --git a/koha-tmpl/intranet-tmpl/prog/js/vue/components/ERM/AgreementsShow.vue b/koha-tmpl/intranet-tmpl/prog/js/vue/components/ERM/AgreementsShow.vue index dc41214f61..ece5f7f566 100644 --- a/koha-tmpl/intranet-tmpl/prog/js/vue/components/ERM/AgreementsShow.vue +++ b/koha-tmpl/intranet-tmpl/prog/js/vue/components/ERM/AgreementsShow.vue @@ -262,7 +262,7 @@ {{ document.file_name }} diff --git a/koha-tmpl/intranet-tmpl/prog/js/vue/components/ERM/AgreementDocuments.vue b/koha-tmpl/intranet-tmpl/prog/js/vue/components/ERM/Documents.vue similarity index 97% rename from koha-tmpl/intranet-tmpl/prog/js/vue/components/ERM/AgreementDocuments.vue rename to koha-tmpl/intranet-tmpl/prog/js/vue/components/ERM/Documents.vue index 986b61f85b..4d0eac20eb 100644 --- a/koha-tmpl/intranet-tmpl/prog/js/vue/components/ERM/AgreementDocuments.vue +++ b/koha-tmpl/intranet-tmpl/prog/js/vue/components/ERM/Documents.vue @@ -1,8 +1,8 @@