

const updateCtr = async (req, res) => {
    let _resp_json = {
        result: "ok",
        reason: {
            content: "",
            body: "",
        },
        req_body: {},
        req_param: {}
    }

    //-- guest 계정
    //-- id, pw, name, pos
    //-- admin 계정
    //-- id, pw, name, pos, cls, isApprove

    try {
        if (req.body) {
            let { id, pw, name, pos, cls, isApprove } = req.body;
            let _cookieInfo = _SPM.uac.get_cookie_info(req);
            if (_cookieInfo) {
                let ret_id = _GTP.STR_VALIDER(id);
                if (!ret_id) {
                    _resp_json.result = "fail";
                    _resp_json.reason = "Body 'id' Error";
                    res.status(400).json(_resp_json);
                    return;
                }
    
                // _cookieInfo.id;
                // _cookieInfo.cls;
                if (_cookieInfo.cls == "guest") {
                    //변경할 pw, name, pos만 변경 가능(요청시 id, pw 쿠키가 동일해야함)
                    if (_cookieInfo.id != id) {
                        _resp_json.result = "fail";
                        _resp_json.reason = 'Bad Request, is not your id';
                        res.status(400).json(_resp_json);
                    }
                }
                else if (_cookieInfo.cls == "admin") {
                    //변경할 pw, name, pos, cls, isApprove(요청시 id, pw 쿠키가 동일해야함)
                }
                else {
                    _resp_json.result = "fail";
                    _resp_json.reason = 'Bad Request, check login authorization';
                    res.status(400).json(_resp_json);
                }
    
                _SPM.db.pgSql.Query_AUTH_ROLES();
                // let checkToken = await _SPM.uac.jwtTokenAuthentication(req, res);
                let dbAccProm = _SPM.db.pgSql.Query_AUTH_GETID_ALL(id);
                let _checkAcc = await Promise.race([dbAccProm]);
                let _userPw = _checkAcc.rows[0].pw
                let rolesProm = _SPM.db.pgSql.Query_AUTH_ROLES();
                let _preRoles = await Promise.race([rolesProm]);
                var isChanged = {
                    pw: false,
                    name: false,
                    pos: false,
                    cls: false,
                    isApprove: false
                };
                var setContext = "";
                if (pw != "") {
                    let compare_prom = _GTP.BCRYPT_COMPARE(pw, _userPw)
                    let _pre_pwCh = await Promise.race([compare_prom]);
                    if (typeof _pre_pwCh === "boolean") {
                        isChanged.pw = _pre_pwCh; // false면, 바뀔게 있으니 pw 변경 진행
                        if (!isChanged.pw) {
                            // hashed
                            let hashedProm = _GTP.BCRYPT_HASH(pw)
                            const _hashed_ChPw = await Promise.race([hashedProm]);
                            if (_hashed_ChPw != undefined) {
                                setContext = `SET pw='${_hashed_ChPw}'`;
                            }
                        }
                    }
                    else {
                        console.log(_pre_pwCh);
                    }
                }
    
                if (name != "") {
                    isChanged.name = true;
                    if (setContext.length) setContext += `, name='${name}'`
                    else setContext = `SET name='${name}'`
                }
                if (pos != "") {
                    isChanged.pos = true;
                    if (setContext.length) setContext += `, pos='${pos}'`
                    else setContext = `SET pos='${pos}'`
                }
                if (_cookieInfo.cls == "admin") {
                    let rolesProm = _SPM.db.pgSql.Query_AUTH_ROLES();
                    let _preRoles = await Promise.race([rolesProm]);
                    if (_preRoles.rowCount) {
                        if (cls != undefined) {
                            let cls_type_check = false;
                            for (let _row of _preRoles.rows) {
                                let _cls_name = _row.name;
                                if (_cls_name == cls) {
                                    cls_type_check = true;
                                    break;
                                }
                            }
                            if (cls_type_check) {
                                if (setContext.length) setContext += `, cls='${cls}'`
                                else setContext = `SET cls='${cls}'`
                            }
                        }
                    }
    
                    if (isApprove != undefined && isApprove != "") {
                        isApprove = _GTP.TO_BOOLEAN(isApprove)
                        if (setContext.length) setContext += `, "isApprove"=${isApprove}`
                        else setContext = `SET "isApprove"=${isApprove}`
                    }
                }
    
                if (setContext.length) {
                    setContext += `, "mdfDate"=CURRENT_TIMESTAMP`;
                    let _updateProm = _SPM.db.pgSql.Query_AUTH_UPDATE(id, setContext);
                    let _updateRes = await Promise.race([_updateProm]);
                    if (_updateRes.rowCount) {
                        _resp_json.result = "ok";
                        _resp_json.reason = `'${id}' Info Updated`;
                        res.status(200).json(_resp_json);
                    }
                    else {
                        _resp_json.result = "fail";
                        _resp_json.reason = 'Internal Update Query Error';
                        res.status(500).json(_resp_json);
                    }
                }
                else {
                    _resp_json.result = "fail";
                    _resp_json.reason = 'Bad Request';
                    res.status(400).json(_resp_json);
                }
            }
            else {
                _resp_json.result = "fail";
                _resp_json.reason = 'please relogin';
                res.status(401).json(_resp_json);
            }
        }
        else {
            _resp_json.result = "fail";
            _resp_json.reason = 'Bad Request';
            res.status(400).json(_resp_json);
        }
    }
    catch(error) {
        _resp_json.result = "fail";
        _resp_json.reason = error.message;
        res.status(500).json(_resp_json);
    }
}

module.exports = updateCtr;