Skip to content

Commit

Permalink
Merge pull request #320 from kellnerd/import-annotation
Browse files Browse the repository at this point in the history
Support annotations for imported entities
  • Loading branch information
MonkeyDo authored Jul 17, 2024
2 parents 98f6ade + c0570ae commit ab54ec8
Show file tree
Hide file tree
Showing 12 changed files with 133 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/func/annotation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export function updateAnnotation(
if (newContent) {
return new Annotation({
content: newContent,
lastRevisionId: revision.get('id')
lastRevisionId: revision && revision.get('id')
}).save(null, {transacting});
}
return Promise.resolve(null);
Expand Down
8 changes: 6 additions & 2 deletions src/func/imports/create-import.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import {camelToSnake} from '../../util';
import {getAdditionalEntityProps} from '../entity';
import {getOriginSourceId} from './misc';
import {updateAliasSet} from '../alias';
import {updateAnnotation} from '../annotation';
import {updateDisambiguation} from '../disambiguation';
import {updateIdentifierSet} from '../identifier';
import {updateLanguageSet} from '../language';
Expand Down Expand Up @@ -60,6 +61,7 @@ type ExtraDataSetIds = Partial<{
/** IDs of all data sets which an entity can have. */
type DataSetIds = {
aliasSetId: number;
annotationId: number;
disambiguationId: number;
identifierSetId: number;
} & ExtraDataSetIds;
Expand Down Expand Up @@ -154,7 +156,7 @@ export function createImport(orm: ORM, importData: QueuedEntity, {

return orm.bookshelf.transaction<ImportResult>(async (transacting) => {
const {entityType} = importData;
const {alias, identifiers, disambiguation, source} = importData.data;
const {alias, annotation, identifiers, disambiguation, source} = importData.data;

// Get origin_source
let originSourceId: number = null;
Expand Down Expand Up @@ -197,9 +199,10 @@ export function createImport(orm: ORM, importData: QueuedEntity, {
}
}

const [aliasSet, identifierSet, disambiguationObj, entityExtraDataSets] =
const [aliasSet, annotationObj, identifierSet, disambiguationObj, entityExtraDataSets] =
await Promise.all([
updateAliasSet(orm, transacting, null, null, alias),
updateAnnotation(orm, transacting, null, annotation, null),
updateIdentifierSet(orm, transacting, null, identifiers),
updateDisambiguation(orm, transacting, null, disambiguation),
updateEntityExtraDataSets(orm, transacting, importData.data)
Expand All @@ -212,6 +215,7 @@ export function createImport(orm: ORM, importData: QueuedEntity, {
transacting,
{
aliasSetId: aliasSet && aliasSet.get('id'),
annotationId: annotationObj && annotationObj.get('id'),
disambiguationId: disambiguationObj && disambiguationObj.get('id'),
identifierSetId: identifierSet && identifierSet.get('id'),
...entityExtraDataSets
Expand Down
4 changes: 2 additions & 2 deletions src/types/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,8 @@ export type ParsedEntity =
| ParsedWork;

// TODO: drop redundant properties which are present in `data` and at the top level
export type QueuedEntity = {
data: ParsedEntity;
export type QueuedEntity<T extends ParsedEntity = ParsedEntity> = {
data: T;
entityType: EntityTypeString;
source: string;
lastEdited?: string;
Expand Down
2 changes: 2 additions & 0 deletions src/validators/author.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import {
type AreaStub,
validateAliases,
validateAnnotationSection,
validateIdentifiers,
validateNameSection,
validateSubmissionSection
Expand Down Expand Up @@ -103,6 +104,7 @@ export function validateAuthor(
validateIdentifiers(get(formData, 'identifierEditor', {}), identifierTypes);
validateNameSection(get(formData, 'nameSection', {}));
validateAuthorSection(get(formData, 'authorSection', {}));
validateAnnotationSection(get(formData, 'annotationSection', {}));
validateSubmissionSection(get(formData, 'submissionSection', {}));
}

Expand Down
21 changes: 16 additions & 5 deletions src/validators/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,19 +163,22 @@ export function validateNameSection(
validateNameSectionDisambiguation(get(values, 'disambiguation', null));
}

export function validateSubmissionSectionNote(value: any): void {
validateOptionalString(value, 'submissionSection.note');
export function validateAnnotationSectionContent(value: any): void {
validateOptionalString(value, 'annotationSection.content');
}

export function validateSubmissionSectionAnnotation(value: any): void {
validateOptionalString(value, 'submissionSection.annotation.content');
export function validateAnnotationSection(data: any): void {
validateAnnotationSectionContent(get(data, 'content', null));
}

export function validateSubmissionSectionNote(value: any): void {
validateOptionalString(value, 'submissionSection.note');
}

export function validateSubmissionSection(
data: any
): void {
validateSubmissionSectionNote(get(data, 'note', null));
validateSubmissionSectionAnnotation(get(data, 'annotation.content', null));
}

export function validateAuthorCreditRow(row: any): void {
Expand Down Expand Up @@ -214,6 +217,14 @@ export type IdentifierSection = Record<string, IdentifierT & {
type: number;
}>;

export type AnnotationSection = {
content?: string;
};

export type SubmissionSection = {
note?: string;
};

export type AuthorCreditRow = {
author: EntityStub;
joinPhrase?: string;
Expand Down
2 changes: 2 additions & 0 deletions src/validators/edition-group.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import {ValidationError, get, validatePositiveInteger} from './base';
import {
validateAliases,
validateAnnotationSection,
validateAuthorCreditSection,
validateAuthorCreditSectionMerge,
validateIdentifiers,
Expand Down Expand Up @@ -72,6 +73,7 @@ export function validateEditionGroup(
validateIdentifiers(get(formData, 'identifierEditor', {}), identifierTypes);
validateNameSection(get(formData, 'nameSection', {}));
validateEditionGroupSection(get(formData, 'editionGroupSection', {}));
validateAnnotationSection(get(formData, 'annotationSection', {}));
validateSubmissionSection(get(formData, 'submissionSection', {}));
}

Expand Down
2 changes: 2 additions & 0 deletions src/validators/edition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
type EntityStub,
type LanguageStub,
validateAliases,
validateAnnotationSection,
validateAuthorCreditSection,
validateAuthorCreditSectionMerge,
validateIdentifiers,
Expand Down Expand Up @@ -147,6 +148,7 @@ export function validateEdition(
validateIdentifiers(get(formData, 'identifierEditor', {}), identifierTypes);
validateNameSection(get(formData, 'nameSection', {}));
validateEditionSection(get(formData, 'editionSection', {}));
validateAnnotationSection(get(formData, 'annotationSection', {}));
validateSubmissionSection(get(formData, 'submissionSection', {}));
}

Expand Down
6 changes: 5 additions & 1 deletion src/validators/publisher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@

import {
type AreaStub,
validateAliases, validateIdentifiers, validateNameSection,
validateAliases,
validateAnnotationSection,
validateIdentifiers,
validateNameSection,
validateSubmissionSection
} from './common';
import {ValidationError, dateIsBefore, get, validateDate, validatePositiveInteger} from './base';
Expand Down Expand Up @@ -86,6 +89,7 @@ export function validatePublisher(
validateIdentifiers(get(formData, 'identifierEditor', {}), identifierTypes);
validateNameSection(get(formData, 'nameSection', {}));
validatePublisherSection(get(formData, 'publisherSection', {}));
validateAnnotationSection(get(formData, 'annotationSection', {}));
validateSubmissionSection(get(formData, 'submissionSection', {}));
}

Expand Down
2 changes: 2 additions & 0 deletions src/validators/series.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import {ValidationError, get, validatePositiveInteger} from './base';
import {
validateAliases,
validateAnnotationSection,
validateIdentifiers,
validateNameSection,
validateSubmissionSection
Expand Down Expand Up @@ -58,6 +59,7 @@ export function validateSeries(
validateIdentifiers(get(formData, 'identifierEditor', {}), identifierTypes);
validateNameSection(get(formData, 'nameSection', {}));
validateSeriesSection(get(formData, 'seriesSection', {}));
validateAnnotationSection(get(formData, 'annotationSection', {}));
validateSubmissionSection(get(formData, 'submissionSection', {}));
}

Expand Down
3 changes: 2 additions & 1 deletion src/validators/work.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

import {
LanguageStub,
validateAliases, validateIdentifiers, validateNameSection,
validateAliases, validateAnnotationSection, validateIdentifiers, validateNameSection,
validateSubmissionSection
} from './common';
import {get, validatePositiveInteger} from './base';
Expand Down Expand Up @@ -51,6 +51,7 @@ export function validateWork(
validateIdentifiers(get(formData, 'identifierEditor', {}), identifierTypes);
validateNameSection(get(formData, 'nameSection', {}));
validateWorkSection(get(formData, 'workSection', {}));
validateAnnotationSection(get(formData, 'annotationSection', {}));
validateSubmissionSection(get(formData, 'submissionSection', {}));
}

Expand Down
8 changes: 8 additions & 0 deletions test/validators/data.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,14 @@ export const VALID_NAME_SECTION = {

export const INVALID_NAME_SECTION = {...VALID_NAME_SECTION, language: null};

export const VALID_ANNOTATION_SECTION = {
content: 'blah blah'
};

export const EMPTY_ANNOTATION_SECTION = {
content: null
};

export const VALID_SUBMISSION_SECTION = {
note: 'blah'
};
Expand Down
85 changes: 85 additions & 0 deletions test/validators/test-annotation-section.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
* Copyright (C) 2024 David Kellner
*
* This program 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 2 of the License, or
* (at your option) any later version.
*
* This program 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 this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

import * as Immutable from 'immutable';

import {EMPTY_ANNOTATION_SECTION, VALID_ANNOTATION_SECTION} from './data';
import {
validateAnnotationSection,
validateAnnotationSectionContent
} from '../../lib/validators/common';

import {ValidationError} from '../../lib/validators/base';
import chai from 'chai';
import chaiAsPromised from 'chai-as-promised';
import {testValidateStringFunc} from './helpers';


chai.use(chaiAsPromised);
const {expect} = chai;


function describeValidateAnnotationSectionContent() {
testValidateStringFunc(validateAnnotationSectionContent, false);
}


function describeValidateAnnotationSection() {
it('should pass a valid Object', () => {
expect(() => validateAnnotationSection(VALID_ANNOTATION_SECTION)).to.not.throw();
});

it('should pass a valid Immutable.Map', () => {
expect(() => validateAnnotationSection(
Immutable.fromJS(VALID_ANNOTATION_SECTION)
)).to.not.throw();
});

it('should pass an Object with an empty content', () => {
expect(() => validateAnnotationSection(
{...VALID_ANNOTATION_SECTION, content: null}
)).to.not.throw();
});

it('should pass an empty content Immutable.Map', () => {
expect(() => validateAnnotationSection(
Immutable.fromJS(EMPTY_ANNOTATION_SECTION)
)).to.not.throw();
});

it('should reject any other non-null data type', () => {
expect(() => validateAnnotationSection(
{...VALID_ANNOTATION_SECTION, content: 1}
)).to.throw(ValidationError);
});

it('should pass a null value', () => {
expect(() => validateAnnotationSection(null)).to.not.throw();
});
}


function tests() {
describe(
'validateAnnotationSectionContent',
describeValidateAnnotationSectionContent
);
describe('validateAnnotationSection', describeValidateAnnotationSection);
}

describe('validateAnnotationSection* functions', tests);

0 comments on commit ab54ec8

Please sign in to comment.