
const approveCtr = (req, res) => {
    let _resp_json = {
        result: "ok",
        reason: "",
        data: {},
        req_body: {},
        req_param: {}
    }
    if (req.body) {
        let { id, isApprove } = req.body;
        // approve 업데이트 전달
        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;
        }

        isApprove = _GTP.TO_BOOLEAN(isApprove);
        let _cookieInfo = _SPM.uac.get_cookie_info(req);
        if (_cookieInfo) {
            if (_cookieInfo.cls == "admin") {
                _SPM.db.pgSql.Query_AUTH_APPROVE(id, isApprove)
                .then((resRow) => {
                    if (resRow.rowCount) {
                        _resp_json.result = "ok";
                        _resp_json.data = resRow.rows
                        _resp_json.reason = 'update your accounts';
                        res.status(200).json(_resp_json);
                    }
                    else {
                        _resp_json.result = "fail";
                        _resp_json.reason = 'not applied';
                        res.status(500).json(_resp_json);
                    }
                })
                .catch((error) => {
                    _resp_json.result = "fail";
                    _resp_json.reason = error.message;
                    res.status(500).json(_resp_json);
                })
            }
            else {
                _resp_json.result = "fail";
                _resp_json.reason = 'please login admin';
                res.status(401).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);
    }
}

const pendingsCtr = (req, res) => {
    let _resp_json = {
        result: "ok",
        reason: "",
        data: {},
        req_body: {},
        req_param: {}
    }
    _SPM.db.pgSql.Query_AUTH_PENDING(
    ).then((resRow) => {
        if (!resRow.rows.length) throw new Error("pending account is Empty")

        _resp_json.result = "ok";
        _resp_json.data = resRow.rows
        _resp_json.reason = 'get pending accounts';
        res.status(200).json(_resp_json);
    }).catch((error) => {
        console.log(error);
        _resp_json.result = "fail";
        _resp_json.reason = error.message;
        res.status(200).json(_resp_json);
    })

}

module.exports = { approveCtr, pendingsCtr };