2016-12-28 23:49:51 +01:00
|
|
|
/**
|
|
|
|
* Module dependencies
|
|
|
|
*/
|
2017-03-05 04:00:39 +01:00
|
|
|
import it from 'cafy';
|
2016-12-28 23:49:51 +01:00
|
|
|
import User from '../../models/user';
|
|
|
|
import { validateUsername } from '../../models/user';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check available username
|
|
|
|
*
|
2017-03-01 09:37:01 +01:00
|
|
|
* @param {any} params
|
|
|
|
* @return {Promise<any>}
|
2016-12-28 23:49:51 +01:00
|
|
|
*/
|
2017-03-03 20:28:38 +01:00
|
|
|
module.exports = async (params) => new Promise(async (res, rej) => {
|
2016-12-28 23:49:51 +01:00
|
|
|
// Get 'username' parameter
|
2017-03-05 04:00:39 +01:00
|
|
|
const [username, usernameError] = it(params.username).expect.string().required().trim().validate(validateUsername).get();
|
2017-03-03 00:00:10 +01:00
|
|
|
if (usernameError) return rej('invalid username param');
|
2016-12-28 23:49:51 +01:00
|
|
|
|
|
|
|
// Get exist
|
|
|
|
const exist = await User
|
|
|
|
.count({
|
|
|
|
username_lower: username.toLowerCase()
|
|
|
|
}, {
|
|
|
|
limit: 1
|
|
|
|
});
|
|
|
|
|
|
|
|
// Reply
|
|
|
|
res({
|
|
|
|
available: exist === 0
|
|
|
|
});
|
|
|
|
});
|