{"size": 4453, "ext": "js", "max_stars_repo_path": "model/admin/addNewMaterial.js", "max_stars_repo_name": "mczal/widji-server", "max_stars_repo_stars_event_min_datetime": "2021-04-27T02:37:17.000Z", "max_stars_repo_stars_event_max_datetime": "2021-04-27T02:37:17.000Z", "max_issues_repo_path": "model/admin/addNewMaterial.js", "max_issues_repo_name": "mczal/widji-server", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "model/admin/addNewMaterial.js", "max_forks_repo_name": "mczal/widji-server", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": 1, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 45.9072164948, "max_line_length": 273, "alphanum_fraction": 0.4262295082}
var mysql = require('mysql');

function addNewType(router,connection){
  var self=this;
  self.handleRoutes(router,connection);
}

var self=this;

addNewType.prototype.handleRoutes = function(router,connection){
  router.post('/addNewMaterial',function(req,res){
    var session = req.body.session;
    var kodeBahan = req.body.kodeBahan;
    var namaBahan = req.body.namaBahan;
    var unit = req.body.unit;
    var ukuranPerUnit = req.body.ukuranPerUnit;
    var jumlahStok = req.body.jumlahStok;
    var satuan = req.body.satuan;
    if(kodeBahan == null || kodeBahan==undefined || kodeBahan==""){
      res.json({"message":"err.. no params kodeBahan received"});
    }else{
      if(namaBahan == null || namaBahan==undefined || namaBahan==""){
        res.json({"message":"err.. no params namaBahan received"});
      }else{
        if(unit == null || unit==undefined || unit==""){
          res.json({"message":"err.. no params unit received"});
        }else{
          if(ukuranPerUnit == null || ukuranPerUnit==undefined || ukuranPerUnit==""){
            res.json({"message":"err.. no params ukuranPerUnit received"});
          }else{
            if(jumlahStok == null || jumlahStok==undefined || jumlahStok==""){
              res.json({"message":"err.. no params jumlahStok received"});
            }else{
              if(satuan == null || satuan==undefined || satuan==""){
                res.json({"message":"err.. no params satuan received"});
              }else{
                if(session==null || session==undefined || session==""){
                  res.json({"message":"err.. no params rec"});
                }else{
                  connection.query("select id_user from `session` where session_code='"+session+"'",function(err,rows){
                    if(err){
                      res.json({"message":"err.. error on selecting session"});
                    }else{
                      if(rows.length>0){
                        var idUser = rows[0].id_user;
                        //
                        connection.query("select id_role from `user` where id_user="+idUser,function(err,rows){
                          if(err){
                            res.json({"message":"err.. error on selecting user"});
                          }else{
                            if(rows.length>0){
                              var idRole = rows[0].id_role;
                              //
                              connection.query("select role_name from `role` where id_role="+idRole,function(err,rows){
                                if(err){
                                  res.json({"message":"err.. error on selecting role"});
                                }else{
                                  if(rows.length>0){
                                    var roleName = rows[0].role_name;
                                    if(roleName == 'admin'){
                                      connection.query("insert into `material`(material_code,material_name,smallest_unit,stock_per_unit,unit_name,quantity) values('"+kodeBahan+"','"+namaBahan+"','"+satuan+"',"+ukuranPerUnit+",'"+unit+"',"+jumlahStok+")",function(err,rows){
                                        if(err){
                                          res.json({"message":"err.. error on inserting new material"});
                                        }else{
                                          res.json({"message":"success cuy congrats"});
                                        }
                                      });
                                    }else{
                                      res.json({"message":"err.. do not authorize"});
                                    }
                                  }else{
                                    res.json({"message":"err.. error no role registered"});
                                  }
                                }
                              });
                            }else{
                              res.json({"message":"err.. error no user registered"});
                            }
                          }
                        });
                      }else{
                        res.json({"message":"err.. error no session registered"});
                      }
                    }
                  });
                }
              }
            }
          }
        }
      }
    }
  });
}

module.exports = addNewType;
