monkeeShark/src/server/api/endpoints/following/requests/cancel.ts

27 lines
715 B
TypeScript
Raw Normal View History

2018-06-01 15:51:20 +00:00
import $ from 'cafy'; import ID from '../../../../../cafy-id';
import cancelFollowRequest from '../../../../../services/following/requests/cancel';
2018-06-02 03:58:56 +00:00
import User, { pack } from '../../../../../models/user';
2018-06-01 15:51:20 +00:00
/**
* Cancel a follow request
*/
module.exports = (params, user) => new Promise(async (res, rej) => {
2018-06-02 03:58:56 +00:00
// Get 'userId' parameter
const [followeeId, followeeIdErr] = $.type(ID).get(params.userId);
if (followeeIdErr) return rej('invalid userId param');
2018-06-01 15:51:20 +00:00
2018-06-02 03:58:56 +00:00
// Fetch followee
const followee = await User.findOne({
_id: followeeId
2018-06-01 15:51:20 +00:00
});
2018-06-02 03:58:56 +00:00
if (followee === null) {
return rej('followee not found');
2018-06-01 15:51:20 +00:00
}
2018-06-02 03:58:56 +00:00
await cancelFollowRequest(followee, user);
2018-06-01 15:51:20 +00:00
// Send response
2018-06-02 03:58:56 +00:00
res(await pack(followee._id, user));
2018-06-01 15:51:20 +00:00
});