mirror of
https://git.joinsharkey.org/Sharkey/Sharkey.git
synced 2024-11-13 17:33:09 +02:00
a1b490afa7
Closes #1411
48 lines
907 B
TypeScript
48 lines
907 B
TypeScript
/**
|
|
* Module dependencies
|
|
*/
|
|
import $ from 'cafy';
|
|
import Favorite from '../../../../../models/favorite';
|
|
import Note from '../../../../../models/note';
|
|
|
|
/**
|
|
* Favorite a note
|
|
*
|
|
* @param {any} params
|
|
* @param {any} user
|
|
* @return {Promise<any>}
|
|
*/
|
|
module.exports = (params, user) => new Promise(async (res, rej) => {
|
|
// Get 'noteId' parameter
|
|
const [noteId, noteIdErr] = $(params.noteId).id().$;
|
|
if (noteIdErr) return rej('invalid noteId param');
|
|
|
|
// Get favoritee
|
|
const note = await Note.findOne({
|
|
_id: noteId
|
|
});
|
|
|
|
if (note === null) {
|
|
return rej('note not found');
|
|
}
|
|
|
|
// if already favorited
|
|
const exist = await Favorite.findOne({
|
|
noteId: note._id,
|
|
userId: user._id
|
|
});
|
|
|
|
if (exist !== null) {
|
|
return rej('already favorited');
|
|
}
|
|
|
|
// Create favorite
|
|
await Favorite.insert({
|
|
createdAt: new Date(),
|
|
noteId: note._id,
|
|
userId: user._id
|
|
});
|
|
|
|
// Send response
|
|
res();
|
|
});
|