Bug 32030: Add document to license - Preparation step
Typo in fieldset id 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>
This commit is contained in:
parent
b8e49f13ef
commit
7a81358ad7
8 changed files with 145 additions and 115 deletions
|
@ -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
|
||||
|
|
|
@ -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 <http://www.gnu.org/licenses>.
|
||||
|
||||
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;
|
|
@ -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;
|
121
Koha/ERM/Documents.pm
Normal file
121
Koha/ERM/Documents.pm
Normal file
|
@ -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 <http://www.gnu.org/licenses>.
|
||||
|
||||
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;
|
|
@ -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" }
|
||||
|
|
|
@ -155,7 +155,7 @@
|
|||
av_agreement_relationships
|
||||
"
|
||||
/>
|
||||
<AgreementDocuments :documents="agreement.documents" />
|
||||
<Documents :documents="agreement.documents" />
|
||||
</ol>
|
||||
</fieldset>
|
||||
<fieldset class="action">
|
||||
|
@ -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",
|
||||
}
|
||||
|
|
|
@ -262,7 +262,7 @@
|
|||
</span>
|
||||
<a
|
||||
download
|
||||
:href="`/api/v1/erm/agreements/${agreement.agreement_id}/documents/${document.document_id}/file/content`"
|
||||
:href="`/api/v1/erm/documents/${document.document_id}/file/content`"
|
||||
>
|
||||
{{ document.file_name }}
|
||||
<i class="fa fa-download"></i>
|
||||
|
|
|
@ -1,8 +1,8 @@
|
|||
<template>
|
||||
<fieldset class="rows" id="agreement_documents">
|
||||
<fieldset class="rows" id="documents">
|
||||
<legend>{{ $t("Documents") }}</legend>
|
||||
<fieldset
|
||||
:id="`agreement_period_${counter}`"
|
||||
:id="`document_${counter}`"
|
||||
class="rows"
|
||||
v-for="(document, counter) in documents"
|
||||
v-bind:key="counter"
|
||||
|
@ -139,7 +139,7 @@ export default {
|
|||
this.documents.splice(counter, 1)
|
||||
}
|
||||
},
|
||||
name: 'AgreementDocuments',
|
||||
name: 'Documents',
|
||||
props: {
|
||||
documents: Array
|
||||
},
|
Loading…
Reference in a new issue