const name = _req.getString("name");
const username = _req.getString("username");
const email = _req.getString("email");
const pass = _req.getString("password");

const emailExists = _user.firstByMail(email);
const usernameExists = _user.firstByUser(username);

if (emailExists != null) {
    _header.status(409);
    _out.json(
        _val.map()
            .set("error", true)
            .set("message", "email-exists")
    );
} else if (usernameExists != null) {
    _header.status(409);
    _out.json(
        _val.map()
            .set("error", true)
            .set("message", "user-exists")
    );
} else {
    const group = _group.firstByCode("user");
    const user_id = _user.create(
        _val.map()
            .set("name", name)
            .set("mail", email)
            .set("user", username)
            .set("pass", password)
            .set("group_id", group.getInt("id"))
    );
    _db.insert(
        'user',
        _val.map()
            .set("name", name)
            .set("user", username)
            .set("email", email)
            .set("client_user_id", user_id)
            .set("role_id", 1)
    );
    _log.info("New user created with id: "+ user_id);
    _out.json(_val.map().set("result", true));
}