2019-07-09 10:35:24 +02:00
|
|
|
import Collection from '../../models/collection';
|
|
|
|
import User from '../../models/user';
|
|
|
|
|
|
|
|
export default function collectionForUserExists(username, collectionId, callback) {
|
|
|
|
function sendFailure() {
|
|
|
|
callback(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
function sendSuccess(collection) {
|
|
|
|
callback(collection != null);
|
|
|
|
}
|
|
|
|
|
|
|
|
function findUser() {
|
2020-07-15 23:33:11 +02:00
|
|
|
return User.findByUsername(username);
|
2019-07-09 10:35:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function findCollection(owner) {
|
|
|
|
if (owner == null) {
|
|
|
|
throw new Error('User not found');
|
|
|
|
}
|
|
|
|
|
|
|
|
return Collection.findOne({ _id: collectionId, owner });
|
|
|
|
}
|
|
|
|
|
|
|
|
return findUser()
|
|
|
|
.then(findCollection)
|
|
|
|
.then(sendSuccess)
|
|
|
|
.catch(sendFailure);
|
|
|
|
}
|