2019-07-08 12:06:13 +02:00
|
|
|
import Collection from '../../models/collection';
|
|
|
|
|
|
|
|
export default function addProjectToCollection(req, res) {
|
|
|
|
const owner = req.user._id;
|
|
|
|
const { id: collectionId, projectId } = req.params;
|
|
|
|
|
|
|
|
function sendFailure({ code = 500, message = 'Something went wrong' }) {
|
|
|
|
res.status(code).json({ success: false, message });
|
|
|
|
}
|
|
|
|
|
|
|
|
function sendSuccess(collection) {
|
|
|
|
res.status(200).json(collection);
|
|
|
|
}
|
|
|
|
|
|
|
|
function updateCollection(collection) {
|
|
|
|
if (collection == null) {
|
|
|
|
sendFailure({ code: 404, message: 'Collection not found' });
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!collection.owner.equals(owner)) {
|
|
|
|
sendFailure({ code: 403, message: 'User does not own this collection' });
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2020-06-29 11:39:35 +02:00
|
|
|
const project = collection.items.find(p => p.projectId === projectId);
|
2019-07-08 12:06:13 +02:00
|
|
|
|
|
|
|
if (project != null) {
|
|
|
|
project.remove();
|
|
|
|
return collection.save();
|
|
|
|
}
|
|
|
|
|
|
|
|
const error = new Error('not found');
|
|
|
|
error.code = 404;
|
|
|
|
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
|
|
|
|
function populateReferences(collection) {
|
|
|
|
return Collection.populate(
|
|
|
|
collection,
|
|
|
|
[
|
|
|
|
{ path: 'owner', select: ['id', 'username'] },
|
|
|
|
{
|
|
|
|
path: 'items.project',
|
|
|
|
select: ['id', 'name', 'slug'],
|
|
|
|
populate: {
|
|
|
|
path: 'user', select: ['username']
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return Collection.findById(collectionId)
|
|
|
|
.populate('items.project', '_id')
|
|
|
|
.then(updateCollection)
|
|
|
|
.then(populateReferences)
|
|
|
|
.then(sendSuccess)
|
|
|
|
.catch(sendFailure);
|
|
|
|
}
|