2017-08-30 08:31:39 +00:00
|
|
|
/**
|
|
|
|
* Module dependencies
|
|
|
|
*/
|
|
|
|
import $ from 'cafy';
|
|
|
|
import User from '../../models/user';
|
|
|
|
import Post from '../../models/post';
|
2018-02-01 23:21:30 +00:00
|
|
|
import { pack } from '../../models/user';
|
2017-08-30 08:31:39 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Pin post
|
|
|
|
*
|
|
|
|
* @param {any} params
|
|
|
|
* @param {any} user
|
|
|
|
* @return {Promise<any>}
|
|
|
|
*/
|
|
|
|
module.exports = async (params, user) => new Promise(async (res, rej) => {
|
2018-03-29 05:48:47 +00:00
|
|
|
// Get 'postId' parameter
|
|
|
|
const [postId, postIdErr] = $(params.postId).id().$;
|
|
|
|
if (postIdErr) return rej('invalid postId param');
|
2017-08-30 08:31:39 +00:00
|
|
|
|
|
|
|
// Fetch pinee
|
|
|
|
const post = await Post.findOne({
|
|
|
|
_id: postId,
|
2018-03-29 05:48:47 +00:00
|
|
|
userId: user._id
|
2017-08-30 08:31:39 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
if (post === null) {
|
|
|
|
return rej('post not found');
|
|
|
|
}
|
|
|
|
|
|
|
|
await User.update(user._id, {
|
|
|
|
$set: {
|
2018-03-29 05:48:47 +00:00
|
|
|
pinnedPostId: post._id
|
2017-08-30 08:31:39 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// Serialize
|
2018-02-01 23:21:30 +00:00
|
|
|
const iObj = await pack(user, user, {
|
2017-08-30 08:31:39 +00:00
|
|
|
detail: true
|
|
|
|
});
|
|
|
|
|
|
|
|
// Send response
|
|
|
|
res(iObj);
|
|
|
|
});
|