2019-12-01 23:31:34 +01:00
|
|
|
const fs = require('fs');
|
|
|
|
const path = require('path');
|
|
|
|
const ShelfController = require('../controllers/shelf');
|
|
|
|
|
|
|
|
async function routes(fastify, options) {
|
|
|
|
fastify.get('/api/shelf/test', async (request, reply) => {
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
|
2020-02-04 23:00:45 +01:00
|
|
|
fastify.get('/api/shelf/getAll', async (request, reply) => {
|
2019-12-22 04:16:52 +01:00
|
|
|
if (!request.isLoggedInUser) {
|
|
|
|
return reply.code(400).send({
|
|
|
|
error: true,
|
|
|
|
message: 'api.not_logged_in',
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-02-05 00:05:08 +01:00
|
|
|
const shelfController = new ShelfController(fastify.models);
|
2019-12-24 00:51:26 +01:00
|
|
|
|
|
|
|
const shelves = await request.user.getShelves({
|
2020-02-24 19:13:12 +01:00
|
|
|
attributes: ['id', 'name', 'isDeletable', 'permissionLevel', 'updatedAt'],
|
2019-12-24 00:51:26 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
return shelves.map(shelf => {
|
|
|
|
shelf.updatedAt = shelfController.getLastUpdatedTimestamp(shelf);
|
|
|
|
return shelf;
|
2019-12-22 04:16:52 +01:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-01-08 06:50:58 +01:00
|
|
|
fastify.get('/api/shelf/get/:shelfId/:domain', async (request, reply) => {
|
|
|
|
if (typeof request.params.shelfId === 'undefined') {
|
2019-12-01 23:31:34 +01:00
|
|
|
return reply.code(400).send({
|
|
|
|
error: true,
|
2020-01-05 03:34:26 +01:00
|
|
|
message: 'api.shelf.get.missing_id',
|
2019-12-01 23:31:34 +01:00
|
|
|
});
|
|
|
|
}
|
2020-01-12 04:35:35 +01:00
|
|
|
if (isNaN(parseInt(request.params.shelfId))) {
|
|
|
|
return reply.code(400).send({
|
|
|
|
error: true,
|
|
|
|
message: 'api.shelf.get.invalid_id',
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (request.params.domain.trim() !== '') {
|
|
|
|
return ShelfController.CheckExternalDomainForShelf(request.params.domain.trim(), request.params.shelfId);
|
|
|
|
}
|
2019-12-01 23:31:34 +01:00
|
|
|
|
2020-02-05 00:05:08 +01:00
|
|
|
const shelfController = new ShelfController(fastify.models);
|
2020-01-08 06:50:58 +01:00
|
|
|
|
|
|
|
const shelf = await shelfController.getShelfById(request.params.shelfId);
|
2020-01-05 03:34:26 +01:00
|
|
|
if (typeof shelf.error !== 'undefined') {
|
|
|
|
shelf.message = 'api.shelf.get.nonexistent_shelf';
|
|
|
|
return reply.code(400).send(shelf);
|
|
|
|
}
|
|
|
|
|
2020-01-08 06:50:58 +01:00
|
|
|
const userCanViewShelf = await shelfController.userCanViewShelf(request.user, shelf);
|
|
|
|
console.log('can view?', userCanViewShelf);
|
2020-01-05 03:34:26 +01:00
|
|
|
if (userCanViewShelf !== true) {
|
|
|
|
return reply.code(400).send({
|
|
|
|
error: true,
|
|
|
|
message: 'api.shelf.get.access_denied', // Should potentially be nonexistent shelf message instead?
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-01-08 06:50:58 +01:00
|
|
|
// const shelfData = await shelfController.scrubShelfData(shelf, request.user);
|
|
|
|
// return reply.send(shelfData);
|
2020-01-05 03:34:26 +01:00
|
|
|
return reply.send(shelf);
|
|
|
|
});
|
|
|
|
|
|
|
|
fastify.post('/api/shelf/create', async (request, reply) => {
|
|
|
|
if (!request.isLoggedInUser) {
|
2019-12-01 23:31:34 +01:00
|
|
|
return reply.code(400).send({
|
|
|
|
error: true,
|
2020-01-05 03:34:26 +01:00
|
|
|
message: 'api.not_logged_in',
|
2019-12-01 23:31:34 +01:00
|
|
|
});
|
|
|
|
}
|
2020-01-05 03:34:26 +01:00
|
|
|
|
2019-12-01 23:31:34 +01:00
|
|
|
request.body.shelfName = request.body.shelfName.trim();
|
|
|
|
|
|
|
|
const userShelves = await request.user.getShelves({
|
|
|
|
attributes: ['name'],
|
|
|
|
});
|
|
|
|
const shelfNameIsValid = ShelfController.newShelfNameIsValid(
|
|
|
|
request.body.shelfName,
|
|
|
|
userShelves.map(shelf => shelf.name)
|
|
|
|
);
|
|
|
|
if (shelfNameIsValid !== true) {
|
|
|
|
return reply.code(400).send(shelfNameIsValid);
|
|
|
|
}
|
|
|
|
|
2020-02-05 00:05:08 +01:00
|
|
|
const shelfController = new ShelfController(fastify.models);
|
2019-12-01 23:31:34 +01:00
|
|
|
|
2020-01-05 03:34:26 +01:00
|
|
|
const newShelf = shelfController.createShelf(request.user, request.body.shelfName);
|
2019-12-01 23:31:34 +01:00
|
|
|
if (typeof newShelf.error !== 'undefined' && newShelf.error !== false) {
|
|
|
|
newShelf.message = 'api.shelf.create.fail';
|
|
|
|
return reply.code(400).send(newShelf);
|
|
|
|
}
|
|
|
|
|
|
|
|
return reply.send({
|
|
|
|
error: false,
|
|
|
|
message: 'api.shelf.create.success',
|
|
|
|
});
|
|
|
|
});
|
2019-12-11 21:15:55 +01:00
|
|
|
|
|
|
|
fastify.post('/api/shelf/rename', async (request, reply) => {
|
|
|
|
if (!request.isLoggedInUser) {
|
2020-02-08 20:39:10 +01:00
|
|
|
return reply.code(401).send({
|
2019-12-11 21:15:55 +01:00
|
|
|
error: true,
|
|
|
|
message: 'api.not_logged_in',
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof request.body.shelfId === 'undefined') {
|
|
|
|
return reply.code(400).send({
|
|
|
|
error: true,
|
|
|
|
message: 'api.shelf.rename.missing_id',
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof request.body.shelfName === 'undefined') {
|
|
|
|
return reply.code(400).send({
|
|
|
|
error: true,
|
|
|
|
message: 'api.shelf.rename.missing_name',
|
|
|
|
});
|
|
|
|
}
|
|
|
|
request.body.shelfName = request.body.shelfName.trim();
|
|
|
|
|
|
|
|
const userShelves = await request.user.getShelves({
|
|
|
|
attributes: ['name'],
|
|
|
|
});
|
|
|
|
const shelfNameIsValid = ShelfController.newShelfNameIsValid(
|
|
|
|
request.body.shelfName,
|
|
|
|
userShelves.map(shelf => shelf.name)
|
|
|
|
);
|
|
|
|
if (shelfNameIsValid !== true) {
|
|
|
|
return reply.code(400).send(shelfNameIsValid);
|
|
|
|
}
|
|
|
|
|
2020-02-08 20:39:10 +01:00
|
|
|
const shelf = await fastify.models.Shelf.findByPk(request.body.shelfId);
|
|
|
|
|
|
|
|
if (!ShelfController.userOwnsShelf(request.user, shelf)) {
|
|
|
|
return reply.code(403).send({
|
|
|
|
error: true,
|
|
|
|
message: 'api.shelf.not_owner',
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!ShelfController.shelfCanBeModified(shelf)) {
|
|
|
|
return reply.code(403).send({
|
|
|
|
error: true,
|
|
|
|
message: 'api.shelf.not_editable',
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-02-05 00:05:08 +01:00
|
|
|
const shelfController = new ShelfController(fastify.models);
|
2019-12-11 21:15:55 +01:00
|
|
|
|
2020-02-08 20:39:10 +01:00
|
|
|
const newShelf = shelfController.renameShelf(request.user, shelf, request.body.shelfName);
|
2019-12-11 21:15:55 +01:00
|
|
|
if (typeof newShelf.error !== 'undefined' && newShelf.error !== false) {
|
|
|
|
newShelf.message = 'api.shelf.rename.fail';
|
|
|
|
return reply.code(400).send(newShelf);
|
|
|
|
}
|
|
|
|
|
|
|
|
return reply.send({
|
|
|
|
error: false,
|
|
|
|
message: 'api.shelf.rename.success',
|
|
|
|
});
|
|
|
|
});
|
2020-02-08 20:39:43 +01:00
|
|
|
|
|
|
|
fastify.post('/api/shelf/addItem', async (request, reply) => {
|
|
|
|
if (!request.isLoggedInUser) {
|
|
|
|
return reply.code(401).send({
|
|
|
|
error: true,
|
|
|
|
message: 'api.not_logged_in',
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof request.body.shelfId === 'undefined') {
|
|
|
|
return reply.code(400).send({
|
|
|
|
error: true,
|
|
|
|
message: 'api.shelf.addItem.missing_id',
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof request.body.bookId === 'undefined') {
|
|
|
|
return reply.code(400).send({
|
|
|
|
error: true,
|
|
|
|
message: 'api.shelf.addItem.missing_id',
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
const shelf = await request.user.getShelf({
|
|
|
|
where: { id: request.body.shelfId },
|
|
|
|
include: [ fastify.models.ShelfItem ],
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!ShelfController.userOwnsShelf(request.user, shelf)) {
|
|
|
|
return reply.code(403).send({
|
|
|
|
error: true,
|
|
|
|
message: 'api.shelf.not_owner',
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
const shelfController = new ShelfController(fastify.models, request.language);
|
|
|
|
|
|
|
|
const shelfItem = await shelfController.addShelfItem(shelf, request.body.bookId, request.body.source);
|
|
|
|
|
|
|
|
if (typeof shelfItem.error !== 'undefined') {
|
|
|
|
return reply.code(400).send({
|
|
|
|
error: shelfItem.error,
|
|
|
|
message: 'api.shelf.addItem.could_not_add',
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return reply.send({
|
|
|
|
error: false,
|
|
|
|
message: 'api.shelf.addItem.success',
|
|
|
|
});
|
|
|
|
});
|
2020-02-08 21:01:15 +01:00
|
|
|
|
|
|
|
fastify.post('/api/shelf/moveItem', async (request, reply) => {
|
|
|
|
if (!request.isLoggedInUser) {
|
|
|
|
return reply.code(401).send({
|
|
|
|
error: true,
|
|
|
|
message: 'api.not_logged_in',
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof request.body.itemId === 'undefined') {
|
|
|
|
return reply.code(400).send({
|
|
|
|
error: true,
|
|
|
|
message: 'api.shelf.moveItem.missing_item_id',
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof request.body.shelfId === 'undefined') {
|
|
|
|
return reply.code(400).send({
|
|
|
|
error: true,
|
|
|
|
message: 'api.shelf.moveItem.missing_shelf_id',
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
const shelfItem = await fastify.models.ShelfItem.findByPk(request.body.itemId, {
|
|
|
|
include: [ fastify.models.Shelf ],
|
|
|
|
});
|
|
|
|
|
|
|
|
const toShelf = await request.user.getShelf({
|
|
|
|
where: { id: request.body.shelfId },
|
|
|
|
include: [ fastify.models.ShelfItem ],
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!ShelfController.userOwnsShelf(request.user, shelfItem.Shelf)
|
|
|
|
|| !ShelfController.userOwnsShelf(request.user, toShelf)) {
|
|
|
|
return reply.code(403).send({
|
|
|
|
error: true,
|
|
|
|
message: 'api.shelf.not_owner',
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
const shelfController = new ShelfController(fastify.models, request.language);
|
|
|
|
|
2020-02-24 17:46:18 +01:00
|
|
|
const moveSuccess = await shelfController.moveShelfItem(shelfItem, toShelf);
|
2020-02-08 21:01:15 +01:00
|
|
|
|
2020-02-24 17:46:18 +01:00
|
|
|
if (typeof moveSuccess.error !== 'undefined') {
|
2020-02-08 21:01:15 +01:00
|
|
|
return reply.code(400).send({
|
2020-02-24 17:46:18 +01:00
|
|
|
error: moveSuccess.error,
|
2020-02-08 21:01:15 +01:00
|
|
|
message: 'api.shelf.moveItem.could_not_move',
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return reply.send({
|
|
|
|
error: false,
|
|
|
|
message: 'api.shelf.moveItem.success',
|
|
|
|
});
|
|
|
|
});
|
2020-02-24 17:46:18 +01:00
|
|
|
|
|
|
|
fastify.post('/api/shelf/deleteItem', async (request, reply) => {
|
|
|
|
if (!request.isLoggedInUser) {
|
|
|
|
return reply.code(401).send({
|
|
|
|
error: true,
|
|
|
|
message: 'api.not_logged_in',
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof request.body.itemId === 'undefined') {
|
|
|
|
return reply.code(400).send({
|
|
|
|
error: true,
|
|
|
|
message: 'api.shelf.deleteItem.missing_item_id',
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
const shelfItem = await fastify.models.ShelfItem.findByPk(request.body.itemId, {
|
|
|
|
include: [ fastify.models.Shelf ],
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!ShelfController.userOwnsShelf(request.user, shelfItem.Shelf)) {
|
|
|
|
return reply.code(403).send({
|
|
|
|
error: true,
|
|
|
|
message: 'api.shelf.not_owner',
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
const shelfController = new ShelfController(fastify.models, request.language);
|
|
|
|
|
|
|
|
const deleteSuccess = await shelfController.deleteShelfItem(shelfItem);
|
|
|
|
|
|
|
|
if (typeof deleteSuccess.error !== 'undefined') {
|
|
|
|
return reply.code(400).send({
|
|
|
|
error: deleteSuccess.error,
|
|
|
|
message: 'api.shelf.deleteItem.could_not_delete',
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return reply.send({
|
|
|
|
error: false,
|
|
|
|
message: 'api.shelf.deleteItem.success',
|
|
|
|
});
|
|
|
|
});
|
2019-12-01 23:31:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = routes;
|