Sindbad~EG File Manager

Current Path : /home/infinitibizsol/.trash/controllers.8/user/
Upload File :
Current File : /home/infinitibizsol/.trash/controllers.8/user/employeeInfo.js

import db from "../../model/index";
import AppError from "../../utils/apiError";
import catchAsync from "../../utils/catchAsync";
import { successResponse, failedResponse } from "../../utils/responseFormat";
const index = catchAsync(async (req, res, next) => {
  const query = req.query;
  let allData = await db.EmployeeInfo.findOne(
    { user_id: req.user._id },
    {
      __v: 0,
      createdOn: 0,
      modifiedOn: 0,
    }
  )
    .populate("user_id", ["_id", "first_name"])
    .exec();

  return successResponse(res, allData);
});

const add = catchAsync(async (req, res, next) => {
  req.body.user_id = req.user._id;
  const address = new db.EmployeeInfo(req.body);
  await address.save();
  return successResponse(res, address, "employee-info saved successfully");
});

const edit = catchAsync(async (req, res, next) => {
  if (req.body.created_by || req.body.user_id) {
    return next(
      new AppError("CreatedBy OR UserId is restricted to change.", 404)
    );
  }
  let result = await db.EmployeeInfo.findByIdAndUpdate(
    { _id: req.params.id },
    { $set: req.body },
    { new: true }
  );
  if (!result) {
    return next(new AppError("No data found.", 404));
  }
  return successResponse(res, result, "employee-info updated successfully");
});

const deleteData = catchAsync(async (req, res, next) => {
  let lead = await db.EmployeeInfo.findByIdAndDelete({
    _id: req.params.id,
  });
  if (!lead) {
    return next(new AppError("No data found.", 404));
  }
  return successResponse(res, lead, "employee-info deleted successfully");
});

export default { index, add, edit, deleteData };

Sindbad File Manager Version 1.0, Coded By Sindbad EG ~ The Terrorists