{"size": 1398, "ext": "js", "max_stars_repo_path": "server/api/user/register.js", "max_stars_repo_name": "JxJayden/weightChallenge", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "server/api/user/register.js", "max_issues_repo_name": "JxJayden/weightChallenge", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "server/api/user/register.js", "max_forks_repo_name": "JxJayden/weightChallenge", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 28.5306122449, "max_line_length": 64, "alphanum_fraction": 0.5057224607}
const db = require('../../models')
const logger = require('../../lib/log')('routes-user:register')
const { RES_CODE, DEFAULT_LIMIT, } = require('../../config')
const BodyFactory = require('../../lib/body-factory')

module.exports = async function (ctx) {
    const { username, password, } = ctx.request.body

    try {
        if (!username || !password) {
            ctx.body = new BodyFactory({
                msg: 'username & password is required',
                code: RES_CODE.ERR_PARAM,
            })
            return
        }

        let isUserExit = await db.UserModel.isUserExit(username)

        if (isUserExit) {
            ctx.body = new BodyFactory({
                msg: `用户名: ${username} 已存在！`,
                code: RES_CODE.ERR_USER_EXIT,
            })
            return
        }

        let newUser = await new db.UserModel({
            password,
            username,
            limit_code: DEFAULT_LIMIT,
        }).save()

        ctx.body = new BodyFactory({
            code: RES_CODE.SUCCESS,
            msg: 'register success',
            data: {
                username: newUser.username,
                limit_code: newUser.limit_code,
                created_time: newUser.created_at,
                email: newUser.email || '',
            },
        })
    } catch (error) {
        logger.error(error)
        ctx.throw(400, error.message)
    }
}
