2017-03-08 12:50:09 -06:00
|
|
|
import $ from 'cafy';
|
2018-03-29 06:32:18 -05:00
|
|
|
import User from '../../../../models/user';
|
|
|
|
import { validateUsername } from '../../../../models/user';
|
2016-12-28 16:49:51 -06:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Check available username
|
|
|
|
*/
|
2018-06-17 19:54:53 -05:00
|
|
|
module.exports = async (params: any) => new Promise(async (res, rej) => {
|
2016-12-28 16:49:51 -06:00
|
|
|
// Get 'username' parameter
|
2018-05-02 04:06:16 -05:00
|
|
|
const [username, usernameError] = $.str.pipe(validateUsername).get(params.username);
|
2017-03-02 17:00:10 -06:00
|
|
|
if (usernameError) return rej('invalid username param');
|
2016-12-28 16:49:51 -06:00
|
|
|
|
|
|
|
// Get exist
|
|
|
|
const exist = await User
|
|
|
|
.count({
|
2018-03-27 02:51:12 -05:00
|
|
|
host: null,
|
2018-03-29 00:48:47 -05:00
|
|
|
usernameLower: username.toLowerCase()
|
2016-12-28 16:49:51 -06:00
|
|
|
}, {
|
|
|
|
limit: 1
|
|
|
|
});
|
|
|
|
|
|
|
|
// Reply
|
|
|
|
res({
|
|
|
|
available: exist === 0
|
|
|
|
});
|
|
|
|
});
|