Correctly format & return shelf values
This commit is contained in:
parent
f5a4db6dc9
commit
383607c7cf
|
@ -1,5 +1,4 @@
|
||||||
const Inventaire = require('./bookData/Inventaire');
|
const Inventaire = require('./bookData/Inventaire');
|
||||||
const SearchController = require('./search');
|
|
||||||
|
|
||||||
class BookReferenceController {
|
class BookReferenceController {
|
||||||
constructor(sequelizeModels, language) {
|
constructor(sequelizeModels, language) {
|
||||||
|
@ -32,9 +31,10 @@ class BookReferenceController {
|
||||||
}
|
}
|
||||||
|
|
||||||
async createOrUpdateReference(source, sourceId, skipSearchBySourceCodes = false) {
|
async createOrUpdateReference(source, sourceId, skipSearchBySourceCodes = false) {
|
||||||
const searchController = new SearchController(this.models);
|
const SearchController = require('./search');
|
||||||
|
const search = new SearchController(this.models);
|
||||||
if (!skipSearchBySourceCodes) {
|
if (!skipSearchBySourceCodes) {
|
||||||
const existingReferences = await searchController.searchReferencesBySourceCodes(source, [sourceId]);
|
const existingReferences = await search.searchReferencesBySourceCodes(source, [sourceId]);
|
||||||
|
|
||||||
if (existingReferences.length > 0) {
|
if (existingReferences.length > 0) {
|
||||||
return existingReferences[0];
|
return existingReferences[0];
|
||||||
|
@ -58,7 +58,7 @@ class BookReferenceController {
|
||||||
|
|
||||||
if (typeof bookData.sources[0].uri !== 'undefined') {
|
if (typeof bookData.sources[0].uri !== 'undefined') {
|
||||||
// Check for references by exact name and author from source
|
// Check for references by exact name and author from source
|
||||||
const matchingReferences = await searchController.searchReferencesForExactMatch(bookData.name, bookData.description);
|
const matchingReferences = await search.searchReferencesForExactMatch(bookData.name, bookData.description);
|
||||||
|
|
||||||
if (matchingReferences.length > 0) {
|
if (matchingReferences.length > 0) {
|
||||||
// If a match is found, update the sources of reference in the database and return it.
|
// If a match is found, update the sources of reference in the database and return it.
|
||||||
|
|
|
@ -1,4 +1,6 @@
|
||||||
const fetch = require('node-fetch');
|
const fetch = require('node-fetch');
|
||||||
|
const { Op, fn, col } = require('sequelize');
|
||||||
|
|
||||||
const BookReferenceController = require('./bookReference');
|
const BookReferenceController = require('./bookReference');
|
||||||
|
|
||||||
class ShelfController {
|
class ShelfController {
|
||||||
|
@ -126,9 +128,60 @@ class ShelfController {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
return this.getFakeShelf();
|
// REMINDER: Factor in privacy levels
|
||||||
|
|
||||||
const shelf = await this.shelfModel.findByPk(shelfId);
|
const shelf = await this.models.Shelf.findByPk(shelfId, {
|
||||||
|
include: [
|
||||||
|
{
|
||||||
|
as: 'User',
|
||||||
|
model: this.models.User,
|
||||||
|
attributes: ['id', 'username', 'displayName'],
|
||||||
|
required: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
as: 'ShelfItems',
|
||||||
|
model: this.models.ShelfItem,
|
||||||
|
attributes: [
|
||||||
|
'id',
|
||||||
|
'bookEdition',
|
||||||
|
'order'
|
||||||
|
],
|
||||||
|
required: false,
|
||||||
|
include: [
|
||||||
|
{
|
||||||
|
as: 'Statuses',
|
||||||
|
model: this.models.Status,
|
||||||
|
attributes: ['id', 'text', 'progress', 'createdAt', 'updatedAt'],
|
||||||
|
required: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
as: 'BookReference',
|
||||||
|
model: this.models.BookReference,
|
||||||
|
attributes: ['id', 'name', 'description', 'sources', 'covers'],
|
||||||
|
required: true,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
orderBy: [[col('ShelfItems.order'), 'ASC']],
|
||||||
|
},
|
||||||
|
],
|
||||||
|
attributes: [
|
||||||
|
[col('Shelf.id'), 'id'],
|
||||||
|
'name',
|
||||||
|
'userId',
|
||||||
|
'permissionLevel',
|
||||||
|
'isDeletable',
|
||||||
|
'createdAt',
|
||||||
|
'updatedAt',
|
||||||
|
[fn('COUNT', col('ShelfItems.id')), 'numShelfItems'],
|
||||||
|
],
|
||||||
|
group: [
|
||||||
|
col('Shelf.id'),
|
||||||
|
col('User.id'),
|
||||||
|
col('ShelfItems.id'),
|
||||||
|
col('ShelfItems->Statuses.id'),
|
||||||
|
col('ShelfItems->BookReference.id'),
|
||||||
|
],
|
||||||
|
});
|
||||||
if (shelf === null) {
|
if (shelf === null) {
|
||||||
return {
|
return {
|
||||||
error: `Shelf with ID ${shelfId} not found.`,
|
error: `Shelf with ID ${shelfId} not found.`,
|
||||||
|
@ -136,6 +189,8 @@ class ShelfController {
|
||||||
}
|
}
|
||||||
|
|
||||||
shelf.updatedAt = this.getLastUpdatedTimestamp(shelf);
|
shelf.updatedAt = this.getLastUpdatedTimestamp(shelf);
|
||||||
|
shelf.isPublic = shelf.permissionLevel === 0;
|
||||||
|
|
||||||
return shelf;
|
return shelf;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -198,7 +253,7 @@ class ShelfController {
|
||||||
|
|
||||||
async scrubShelfData (shelf, currentUser) {
|
async scrubShelfData (shelf, currentUser) {
|
||||||
const userOwnsShelf = currentUser.id === shelf.userId;
|
const userOwnsShelf = currentUser.id === shelf.userId;
|
||||||
const shelfUser = userOwnsShelf ? null : await shelf.getUser({ attributes: ['username, displayName'] });
|
const shelfUser = userOwnsShelf ? null : shelf.User;
|
||||||
let user = {};
|
let user = {};
|
||||||
if (shelfUser !== null) {
|
if (shelfUser !== null) {
|
||||||
user.name = shelfUser.displayName;
|
user.name = shelfUser.displayName;
|
||||||
|
@ -206,40 +261,25 @@ class ShelfController {
|
||||||
} else {
|
} else {
|
||||||
user = null;
|
user = null;
|
||||||
}
|
}
|
||||||
const rawShelfItems = await shelf.getShelfItems({
|
|
||||||
attributes: ['bookId', 'createdAt', 'updatedAt'],
|
// Untested
|
||||||
order: [['order', 'ASC']],
|
const shelfItems = await Promise.all(shelf.ShelfItems.map(async (shelfItem) => {
|
||||||
})
|
const bookReference = BookReferenceController.formatReferenceSources(shelfItem.BookReference);
|
||||||
const bookReferenceMap = await Promise.all(shelfItems.map(shelfItem => {
|
const reviews = await bookReference.getInteractions({
|
||||||
return shelfItem.getBookReference({
|
|
||||||
attributes: ['name', 'description', 'sources', 'covers'],
|
|
||||||
});
|
|
||||||
}));
|
|
||||||
const bookReferenceStatuses = await Promise.all(bookReferenceMap.map(bookReference => {
|
|
||||||
return bookReference.getStatuses({
|
|
||||||
where: {
|
where: {
|
||||||
userId: shelf.userId,
|
userId: shelf.userId,
|
||||||
},
|
}, // Return all reviews for any bookEdition so they can be filtered on frontend.
|
||||||
order: [['updatedAt', 'DESC']],
|
attributes: ['text', 'rating', 'bookEdition'],
|
||||||
});
|
});
|
||||||
|
return {
|
||||||
|
title: bookReference.name,
|
||||||
|
author: bookReference.description,
|
||||||
|
sources: bookReference.sources,
|
||||||
|
covers: bookReference.covers,
|
||||||
|
updates: shelfItem.Statuses,
|
||||||
|
reviews,
|
||||||
|
};
|
||||||
}));
|
}));
|
||||||
const bookReferences = bookReferenceMap.map(bookReference => {
|
|
||||||
bookReference.updates = bookReferenceStatuses.findAll(status => status.type === 1);
|
|
||||||
bookReference.rating = bookReferenceStatuses.find(status => status.type === 3);
|
|
||||||
bookReference.review = bookReferenceStatuses.find(status => status.type === 4);
|
|
||||||
return bookReference;
|
|
||||||
});
|
|
||||||
const shelfItems = rawShelfItems.map(shelfItem => {
|
|
||||||
const bookReference = bookReferences.find(bookData => bookData.id === shelfItem.bookId);
|
|
||||||
shelfItem.title = bookReference.name;
|
|
||||||
shelfItem.author = bookReference.description;
|
|
||||||
shelfItem.sources = bookReference.sources;
|
|
||||||
shelfItem.covers = bookReference.covers;
|
|
||||||
shelfItem.updates = bookReference.updates;
|
|
||||||
shelfItem.rating = bookReference.rating.data;
|
|
||||||
shelfItem.review = bookReference.review.data;
|
|
||||||
return shelfItem;
|
|
||||||
})
|
|
||||||
|
|
||||||
const shelfData = {
|
const shelfData = {
|
||||||
name: shelf.name,
|
name: shelf.name,
|
||||||
|
|
|
@ -62,9 +62,8 @@ async function routes(fastify, options) {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// const shelfData = await shelfController.scrubShelfData(shelf, request.user);
|
const shelfData = await shelfController.scrubShelfData(shelf, request.user);
|
||||||
// return reply.send(shelfData);
|
return reply.send(shelfData);
|
||||||
return reply.send(shelf);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
fastify.post('/api/shelf/create', async (request, reply) => {
|
fastify.post('/api/shelf/create', async (request, reply) => {
|
||||||
|
|
|
@ -16,11 +16,6 @@ module.exports = models => {
|
||||||
onDelete: 'SET NULL',
|
onDelete: 'SET NULL',
|
||||||
});
|
});
|
||||||
|
|
||||||
Shelf.belongsTo(User, {
|
|
||||||
foreignKey: 'permissionLevel',
|
|
||||||
onDelete: 'CASCADE',
|
|
||||||
});
|
|
||||||
|
|
||||||
Shelf.hasMany(ShelfItem, {
|
Shelf.hasMany(ShelfItem, {
|
||||||
foreignKey: 'shelfId',
|
foreignKey: 'shelfId',
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in New Issue