summaryrefslogtreecommitdiff
path: root/src/server/api/endpoints/posts/categorize.ts
blob: 0436c8e6978aa31162f513d267c4e9e803d73857 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
/**
 * Module dependencies
 */
import $ from 'cafy';
import Post from '../../models/post';

/**
 * Categorize a post
 *
 * @param {any} params
 * @param {any} user
 * @return {Promise<any>}
 */
module.exports = (params, user) => new Promise(async (res, rej) => {
	if (!user.account.isPro) {
		return rej('This endpoint is available only from a Pro account');
	}

	// Get 'postId' parameter
	const [postId, postIdErr] = $(params.postId).id().$;
	if (postIdErr) return rej('invalid postId param');

	// Get categorizee
	const post = await Post.findOne({
		_id: postId
	});

	if (post === null) {
		return rej('post not found');
	}

	if (post.is_category_verified) {
		return rej('This post already has the verified category');
	}

	// Get 'category' parameter
	const [category, categoryErr] = $(params.category).string().or([
		'music', 'game', 'anime', 'it', 'gadgets', 'photography'
	]).$;
	if (categoryErr) return rej('invalid category param');

	// Set category
	Post.update({ _id: post._id }, {
		$set: {
			category: category,
			is_category_verified: true
		}
	});

	// Send response
	res();
});