{"size": 2973, "ext": "js", "max_stars_repo_path": "app/routes/user.route.js", "max_stars_repo_name": "RobertSomodi/RAW_SERVER_2", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "app/routes/user.route.js", "max_issues_repo_name": "RobertSomodi/RAW_SERVER_2", "max_issues_repo_issues_event_min_datetime": "2020-07-17T02:33:51.000Z", "max_issues_repo_issues_event_max_datetime": "2021-05-08T22:03:55.000Z", "max_forks_repo_path": "app/routes/user.route.js", "max_forks_repo_name": "RobertSomodi/RAW_SERVER_2", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": 2, "max_forks_count": null, "avg_line_length": 23.784, "max_line_length": 102, "alphanum_fraction": 0.6394214598}
const userService = require('../services/user.service');
var schema = require('../schema/userValidationSchema.json')
var iValidator = require('../../common/iValidator');
var errorCode = require('../../common/error-code');
var errorMessage = require('../../common/error-methods');
var mail = require('./../../common/mailer.js');


function init(router) {
    router.route('/user')
        .get(getAllUsers)
        .post(addUser);
    router.route('/user/:id')
        .get(getUserById)
        .delete(deleteUser)
        .put(updateUser);
    router.route('/user/:id/:auth')
        .get(getUserByAuthId);
    router.route('/user/all/:storeId/:departmentId')
        .get(getAllUsersByStoreIdDepartmentId);   
}

function getAllUsers(req,res) {
  userService.getAllUser().then((data) => {
      res.send(data);
    }).catch((err) => {
      mail.mail(err);
      res.send(err);
    });
}

function getAllUsersByStoreIdDepartmentId(req,res) {
  let reqData = req.params;
  var json_format = iValidator.json_schema(schema.getByStoreDepartmentSchema,reqData,"user");
  if (json_format.valid == false) {
    return res.status(422).send(json_format.errorMessage);
  }

  userService.getAllUsersByStoreIdDepartmentId(reqData.storeId, reqData.departmentId).then((data) => {
      res.send(data);
    }).catch((err) => {
      mail.mail(err);
      res.send(err);
    });
}


function getUserById(req,res) {

  let reqData = req.params;

  var json_format = iValidator.json_schema(schema.getSchema,reqData,"user");
  if (json_format.valid == false) {
    return res.status(422).send(json_format.errorMessage);
  }

  userService.getUserById(reqData.id).then((data) => {
      res.send(data);
    }).catch((err) => {
      res.send(err);
    });
}

function getUserByAuthId(req,res) {

  let reqData = req.params;
  
  var json_format = iValidator.json_schema(schema.getSchema,reqData,"user");
  if (json_format.valid == false) {
    return res.status(422).send(json_format.errorMessage);
  }

  userService.getUserByAuthId(reqData.auth).then((data) => {
      res.send(data);
    }).catch((err) => {
      res.send(err);
    });
}

function addUser(req,res) {
  var userData=req.body;
  
  //Validating the input entity
   var json_format = iValidator.json_schema(schema.postSchema, userData, "user");
   if (json_format.valid == false) {
     return res.status(422).send(json_format.errorMessage);
   }

  userService.addUser(userData).then((data) => {
    res.json(data);
  }).catch((err) => {
    mail.mail(err);
    res.json(err);
  });

}


function updateUser(req,res) {
   var userData=req.body;
   userService.updateUser(userData).then((data)=>{
      res.json(data);
  }).catch((err)=>{
      mail.mail(err);
      res.json(err);
   });
}


function deleteUser(req,res) {
  var delId = req.params.id;
  userService.deleteUser(delId).then((data)=>{
    res.json(data);
  }).catch((err)=>{
     mail.mail(err);
      res.json(err);
  });
}


module.exports.init = init;



