Skip to content

Commit

Permalink
Merge pull request #321 from kellnerd/import-annotation
Browse files Browse the repository at this point in the history
Preserve imported annotation on approval & Add SeriesImport model
  • Loading branch information
MonkeyDo authored Aug 6, 2024
2 parents ab54ec8 + aaa2a92 commit d555ccf
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 14 deletions.
25 changes: 14 additions & 11 deletions src/func/imports/approve-import.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,26 +38,30 @@ export async function approveImport(
{orm, transacting, importEntity, editorId}: approveEntityPropsType
): Promise<Record<string, unknown>> {
const {source, importId, type: entityType, disambiguationId, aliasSet,
identifierSetId} = importEntity;
identifierSetId, annotationId} = importEntity;
const {id: aliasSetId} = aliasSet;

const {Entity, Revision} = orm;
const {Annotation, Entity, Revision} = orm;

// Increase user edit count
const editorUpdatePromise =
incrementEditorEditCountById(orm, editorId, transacting);

// Create a new revision record
const revisionPromise = new Revision({
const revision = await new Revision({
authorId: editorId
}).save(null, {transacting});
const revisionId = revision.get('id');

if (annotationId) {
// Set revision of our annotation which is NULL for imports
await new Annotation({id: annotationId})
.save({lastRevisionId: revisionId}, {transacting});
}

const note = `Approved from automatically imported record of ${source}`;
// Create a new note promise
const notePromise = revisionPromise
.then((revision) => createNote(
orm, note, editorId, revision, transacting
));
const notePromise = createNote(orm, note, editorId, revision, transacting);

// Get additional props
const additionalProps = getAdditionalEntityProps(importEntity, entityType);
Expand All @@ -70,19 +74,18 @@ export async function approveImport(
, {}
);

const [revisionRecord] = await Promise.all([
revisionPromise, notePromise, editorUpdatePromise
]);
await Promise.all([notePromise, editorUpdatePromise]);

const newEntity = await new Entity({type: entityType})
.save(null, {transacting});
const bbid = newEntity.get('bbid');
const propsToSet = _.extend({
aliasSetId,
annotationId,
bbid,
disambiguationId,
identifierSetId,
revisionId: revisionRecord && revisionRecord.get('id')
revisionId
}, entitySets, additionalProps);

const Model = getEntityModelByType(orm, entityType);
Expand Down
3 changes: 1 addition & 2 deletions src/func/imports/delete-import.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ export async function deleteImport(
// Get the type of the import
const [typeObj] = await transacting.select('type')
.from('bookbrainz.import').where('id', importId);
const {type} = typeObj;
const importType = type.toLowerCase();
const {type: importType} = typeObj;

// Get the dataId of the import
const [dataIdObj] =
Expand Down
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ import revision from './models/revision';
import series from './models/entities/series';
import seriesData from './models/data/seriesData';
import seriesHeader from './models/headers/seriesHeader';
import seriesImport from './models/imports/seriesImport';
import seriesOrderingType from './models/seriesOrderingType';
import seriesRevision from './models/revisions/seriesRevision';
import titleType from './models/titleType';
Expand Down Expand Up @@ -180,6 +181,7 @@ export default function init(config: Knex.Config) {
Series: series(bookshelf),
SeriesData,
SeriesHeader: seriesHeader(bookshelf),
SeriesImport: seriesImport(bookshelf),
SeriesOrderingType: seriesOrderingType(bookshelf),
SeriesRevision: seriesRevision(bookshelf),
TitleType: titleType(bookshelf),
Expand Down
34 changes: 34 additions & 0 deletions src/models/imports/seriesImport.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* 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 type Bookshelf from '@metabrainz/bookshelf';


export default function series(bookshelf: Bookshelf) {
const SeriesData = bookshelf.model('SeriesData');

const SeriesImport = SeriesData.extend({
defaultAlias() {
return this.belongsTo('Alias', 'default_alias_id');
},
idAttribute: 'import_id',
tableName: 'bookbrainz.series_import'
});

return bookshelf.model('SeriesImport', SeriesImport);
}
3 changes: 2 additions & 1 deletion src/types/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ export type ParsedAuthor = ParsedBaseEntity & {
beginDate?: string;
endDate?: string;
ended: boolean;
// TODO: `type` is currently ignored, only `typeId` is actually used!
type?: string;
typeId?: number;
genderId?: number;
Expand Down Expand Up @@ -79,7 +80,7 @@ export type ParsedPublisher = ParsedBaseEntity & {
export type ParsedSeries = ParsedBaseEntity & {

/** Type of the items in the series. */
entityType: EntityTypeString;
entityType: Exclude<EntityTypeString, 'Series'>;
orderingTypeId: number;
};

Expand Down

0 comments on commit d555ccf

Please sign in to comment.