yumechi-no-kuni/src/api/endpoints/username/available.ts
syuilo 31f36fa618 ✌️
2017-03-09 03:50:09 +09:00

31 lines
650 B
TypeScript

/**
* Module dependencies
*/
import $ from 'cafy';
import User from '../../models/user';
import { validateUsername } from '../../models/user';
/**
* Check available username
*
* @param {any} params
* @return {Promise<any>}
*/
module.exports = async (params) => new Promise(async (res, rej) => {
// Get 'username' parameter
const [username, usernameError] = $(params.username).string().pipe(validateUsername).$;
if (usernameError) return rej('invalid username param');
// Get exist
const exist = await User
.count({
username_lower: username.toLowerCase()
}, {
limit: 1
});
// Reply
res({
available: exist === 0
});
});