text: const response = require('../../../utils/response'); 
  const responseHandler = require('../../../utils/response/responseHandler'); 
  const getSelectObject = require('../../../utils/getSelectObject'); 
  
  const addRouteRole = (addRouteRoleUsecase) => async (req,res) => {
    try {
      let dataToCreate = { ...req.body || {} };
      dataToCreate.addedBy = req.user.id;
      let result = await addRouteRoleUsecase(dataToCreate,req,res);
      return responseHandler(res,result);
    } catch (error){
      return responseHandler(res,response.internalServerError({ message:error.message }));
    }
  };
  
  const bulkInsertRouteRole = (bulkInsertRouteRoleUsecase)=> async (req,res) => {
    try {
      let dataToCreate = req.body.data;
      for (let i = 0;i < dataToCreate.length;i++){
        dataToCreate[i] = {
          ...dataToCreate[i],
          addedBy:req.user.id,
        };
      }
      let result = await bulkInsertRouteRoleUsecase(dataToCreate,req,res);
      return responseHandler(res,result);
    } catch (error){
      return responseHandler(res,response.internalServerError({ message:error.message }));
    }
  };
  
  const findAllRouteRole = (findAllRouteRoleUsecase) => async (req,res) => {
    try {
      let query = { ...req.body.query || {} };
      let options = { ...req.body.options || {} };
      let result = await findAllRouteRoleUsecase({
        query,
        options,
        isCountOnly:req.body.isCountOnly || false
      },req,res);
      return responseHandler(res,result);
    } catch (error){
      return responseHandler(res,response.internalServerError({ message:error.message }));
    }
  };
  
  const getRouteRoleCount = (getRouteRoleCountUsecase) => async (req,res) => {
    try {
      let where = { ...req.body.where || {} };
      let result = await getRouteRoleCountUsecase({ where },req,res);  
      return responseHandler(res,result);
    } catch (error){
      return responseHandler(res,response.internalServerError({ message:error.message }));
    }
  };
  
  const bulkUpdateRouteRole = (bulkUpdateRouteRoleUsecase) => async (req,res) => {
    try {
      let dataToUpdate = { ...req.body.data || {} };
      let query = { ...req.body.filter || {} };
      delete dataToUpdate.addedBy;
      delete dataToUpdate.updatedBy;
      dataToUpdate.updatedBy = req.user.id;
      let result = await bulkUpdateRouteRoleUsecase({
        dataToUpdate,
        query
      },req,res);
      return responseHandler(res,result);
    } catch (error){
      return responseHandler(res,response.internalServerError({ message:error.message }));
    }
  };
  
  const softDeleteManyRouteRole = (softDeleteManyRouteRoleUsecase) => async (req,res) => {
    try {
      if (!req.body || !req.body.ids){
        return responseHandler(res,response.badRequest());
      }
      let ids = req.body.ids;
      let query = { id : { $in:ids } };
      const dataToUpdate = {
        isDeleted: true,
        updatedBy: req.user.id
      };
      let result = await softDeleteManyRouteRoleUsecase({
        dataToUpdate,
        query
      },req,res);
      return responseHandler(res,result);
    } catch (error){
      return responseHandler(res,response.internalServerError({ message:error.message }));
    }
  };
  
  const deleteManyRouteRole = (deleteManyRouteRoleUsecase) => async (req,res) => {
    try {
      if (!req.body || !req.body.ids){
        return responseHandler(res,response.badRequest());
      }
      let ids = req.body.ids;
      let query = { id : { $in:ids } };
      let result = await deleteManyRouteRoleUsecase(query,req,res);
      return responseHandler(res,result);
    } catch (error){
      return responseHandler(res,response.internalServerError({ message:error.message }));
    }
  };
  
  const softDeleteRouteRole = (softDeleteRouteRoleUsecase) => async (req,res)=>{
    try {
      if (!req.params.id){
        return responseHandler(res,response.badRequest());
      }
      let query = { id: req.params.id };
      const dataToUpdate = {
        isDeleted: true,
        updatedBy: req.user.id,
      };
      let result = await softDeleteRouteRoleUsecase({
        dataToUpdate,
        query
      },req,res);
      return responseHandler(res,result);
    } catch (error){
      return responseHandler(res,response.internalServerError({ message:error.message }));
    }
  };
  
  const partialUpdateRouteRole = (partialUpdateRouteRoleUsecase) => async (req,res) => {
    try {
      if (!req.params.id){
        return responseHandler(res,response.badRequest());
      }
      let query = { id: req.params.id };
      let dataToUpdate = req.body;
      delete dataToUpdate.updatedBy;
      dataToUpdate.updatedBy = req.user.id;
      let result = await partialUpdateRouteRoleUsecase({
        dataToUpdate,
        query
      },req,res);
      return responseHandler(res,result);
    } catch (error){
      return responseHandler(res,response.internalServerError({ message:error.message }));
    }
  };
  
  const updateRouteRole = (updateRouteRoleUsecase) => async (req,res) =>{
    try {
      if (!req.params.id){
        return responseHandler(res,response.badRequest());
      }
      let dataToUpdate = { ...req.body || {} };
      let query = { id: req.params.id };
      delete dataToUpdate.addedBy;
      delete dataToUpdate.updatedBy;
      dataToUpdate.updatedBy = req.user.id;
      let result = await updateRouteRoleUsecase({
        dataToUpdate,
        query
      },req,res);
      return responseHandler(res,result);
    } catch (error){
      return responseHandler(res,response.internalServerError({ message:error.message }));
    }
  };
  
  const getRouteRole = (getRouteRoleUsecase) => async (req,res) =>{
    try {
      if (!req.params.id){
        return responseHandler(res,response.badRequest());
      }
      let options = {};
      let query = { id: req.params.id };
      let result = await getRouteRoleUsecase({
        query,
        options
      },req,res);
      return responseHandler(res,result);
    } catch (error) {
      return responseHandler(res,response.internalServerError({ message:error.message }));
    }
  };
  
  const deleteRouteRole = (deleteRouteRoleUsecase) => async (req,res) => {
    try {
      if (!req.params.id){
        return responseHandler(res,response.badRequest());
      }
      let query = { id: req.params.id };
      let result = await deleteRouteRoleUsecase(query,req,res);
      return responseHandler(res,result);
    } catch (error){
      return responseHandler(res,response.internalServerError({ message:error.message }));
    }
  };
  
  module.exports = {
    addRouteRole,
    bulkInsertRouteRole,
    findAllRouteRole,
    getRouteRoleCount,
    bulkUpdateRouteRole,
    softDeleteManyRouteRole,
    deleteManyRouteRole,
    softDeleteRouteRole,
    partialUpdateRouteRole,
    updateRouteRole,
    getRouteRole,
    deleteRouteRole
  };
  