summaryrefslogtreecommitdiff
path: root/src/server/api/endpoints/app/name_id/available.ts
blob: 2cd56e92d6bde9a995587c0956f9e8dc7a2394e9 (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
/**
 * Module dependencies
 */
import $ from 'cafy';
import App from '../../../../../models/app';
import { isValidNameId } from '../../../../../models/app';

/**
 * Check available nameId of app
 *
 * @param {any} params
 * @return {Promise<any>}
 */
export default async (params: any) => new Promise(async (res, rej) => {
	// Get 'nameId' parameter
	const [nameId, nameIdErr] = $.str.pipe(isValidNameId).get(params.nameId);
	if (nameIdErr) return rej('invalid nameId param');

	// Get exist
	const exist = await App
		.count({
			nameIdLower: nameId.toLowerCase()
		}, {
			limit: 1
		});

	// Reply
	res({
		available: exist === 0
	});
});