2020-02-07 23:31:32 +01:00
|
|
|
const Inventaire = require('./bookData/Inventaire');
|
|
|
|
const SearchController = require('./search');
|
|
|
|
|
|
|
|
class BookReferenceController {
|
|
|
|
constructor(sequelizeModels, language) {
|
|
|
|
this.models = sequelizeModels;
|
|
|
|
this.lang = language;
|
|
|
|
}
|
|
|
|
|
2020-08-26 07:49:05 +02:00
|
|
|
static formatReferenceSources(reference) {
|
|
|
|
const referenceSources = Object.keys(reference.sources);
|
|
|
|
const reformattedSources = referenceSources.map(source => {
|
|
|
|
const uri = reference.sources[source];
|
|
|
|
let link;
|
|
|
|
switch (source) {
|
|
|
|
default:
|
|
|
|
case 'inventaire': {
|
|
|
|
link = `${Inventaire.url}/entity/${uri}`
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
source,
|
|
|
|
uri,
|
|
|
|
link,
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
reference.sources = reformattedSources;
|
|
|
|
|
|
|
|
return reference;
|
|
|
|
}
|
|
|
|
|
2020-08-25 01:14:08 +02:00
|
|
|
async createOrUpdateReference(source, sourceId, skipSearchBySourceCodes = false) {
|
2020-02-07 23:31:32 +01:00
|
|
|
const searchController = new SearchController(this.models);
|
2020-08-25 01:14:08 +02:00
|
|
|
if (!skipSearchBySourceCodes) {
|
|
|
|
const existingReferences = await searchController.searchReferencesBySourceCodes(source, [sourceId]);
|
2020-02-07 23:31:32 +01:00
|
|
|
|
2020-08-25 01:14:08 +02:00
|
|
|
if (existingReferences.length > 0) {
|
|
|
|
return existingReferences[0];
|
|
|
|
}
|
2020-02-07 23:31:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
let dataClass;
|
|
|
|
switch (source) {
|
|
|
|
case 'openlibrary': {
|
|
|
|
// break;
|
|
|
|
}
|
|
|
|
case 'inventaire':
|
|
|
|
default: {
|
|
|
|
dataClass = new Inventaire(this.lang);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get formatted book data from source
|
2020-08-24 23:16:14 +02:00
|
|
|
const bookData = await dataClass.getBookData(sourceId);
|
2020-02-07 23:31:32 +01:00
|
|
|
|
2020-08-25 01:14:08 +02:00
|
|
|
if (typeof bookData.sources[0].uri !== 'undefined') {
|
2020-02-07 23:31:32 +01:00
|
|
|
// Check for references by exact name and author from source
|
2020-08-25 01:14:08 +02:00
|
|
|
const matchingReferences = await searchController.searchReferencesForExactMatch(bookData.name, bookData.description);
|
2020-02-07 23:31:32 +01:00
|
|
|
|
2020-08-25 01:14:08 +02:00
|
|
|
if (matchingReferences.length > 0) {
|
2020-02-07 23:31:32 +01:00
|
|
|
// If a match is found, update the sources of reference in the database and return it.
|
2020-08-25 01:14:08 +02:00
|
|
|
return await this.addSourceToReference(matchingReferences[0], source, sourceId);
|
2020-02-07 23:31:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return await this.createReference(bookData, source, sourceId);
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
error: true,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async createReference(bookData, source, sourceId) {
|
|
|
|
const newReference = await this.models.BookReference.create({
|
|
|
|
name: bookData.name,
|
|
|
|
description: bookData.description,
|
|
|
|
sources: {
|
|
|
|
[source]: sourceId,
|
|
|
|
},
|
|
|
|
covers: bookData.covers,
|
|
|
|
locale: this.lang,
|
|
|
|
});
|
2020-08-25 01:14:08 +02:00
|
|
|
// newReference.totalInteractions = 0;
|
|
|
|
// newReference.numReviews = 0;
|
|
|
|
// newReference.averageRating = null;
|
|
|
|
// newReference.Interactions = [];
|
|
|
|
// newReference.Reviews = [];
|
|
|
|
// newReference.Ratings = [];
|
2020-02-07 23:31:32 +01:00
|
|
|
return newReference;
|
|
|
|
}
|
|
|
|
|
|
|
|
async addSourceToReference(reference, source, sourceId) {
|
|
|
|
const updatedSources = Object.assign({ [source]: sourceId }, reference.sources);
|
|
|
|
return await reference.update({
|
|
|
|
sources: updatedSources,
|
|
|
|
}).then(() => {
|
|
|
|
reference.sources = updatedSources;
|
|
|
|
return reference;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = BookReferenceController;
|