Sharkey/src/api/endpoints/username/available.ts

36 lines
705 B
TypeScript
Raw Normal View History

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