'use strict';

/**
 * Module dependencies
 */
import User from '../../models/user';

/**
 * Check available username
 *
 * @param {Object} params
 * @param {Object} res
 * @return {Promise<object>}
 */
module.exports = async (params, res) =>
	new Promise(async (res, rej) =>
{
	// Init 'username' parameter
	const username = params.username;
	if (username === undefined || username === null || username === '') {
		return res(400, 'username-is-required');
	}

	if (!/^[a-zA-Z0-9\-]{3,20}$/.test(username)) {
		return res(400, 'invalid-username');
	}

	const exist = await User
		.count({
			username_lower: username.toLowerCase()
		}, {
			limit: 1
		});

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

@@ -143,31 +143,8 @@
 ams%0A
- * @param %7BObject%7D res%0A
  * @
@@ -206,13 +206,8 @@
 rams
-, res
 ) =%3E
@@ -250,11 +250,10 @@
 %09// 
-Ini
+Ge
 t 'u
@@ -326,35 +326,8 @@
 e ==
-= undefined %7C%7C username ===
  nul
@@ -342,17 +342,16 @@
 rname ==
-=
  '') %7B%0A%09
@@ -360,23 +360,18 @@
 eturn re
-s(400, 
+j(
 'usernam
@@ -391,16 +391,38 @@
 ');%0A%09%7D%0A%0A
+%09// Validate username%0A
 %09if (!/%5E
@@ -476,15 +476,10 @@
 n re
-s(400, 
+j(
 'inv
@@ -499,16 +499,30 @@
 ');%0A%09%7D%0A%0A
+%09// Get exist%0A
 %09const e
@@ -618,16 +618,26 @@
 %0A%09%09%7D);%0A%0A
+%09// Reply%0A
 %09res(%7B%0A%09

Fix bug