Sindbad~EG File Manager

Current Path : /home/infinitibizsol/.trash/controllers.8/user/
Upload File :
Current File : /home/infinitibizsol/.trash/controllers.8/user/accountInformation.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;
  query.created_by = req.user._id;
  let allData = await db.AccountInfo.find(query)
    .populate("created_by", "_id first_name last_name")
    .populate("contact_id", "_id first_name last_name");
  return successResponse(res, allData);
});

const add = catchAsync(async (req, res, next) => {
  req.body.created_by = req.user._id;
  const accountInformation = new db.AccountInfo(req.body);
  await accountInformation.save();
  return successResponse(
    res,
    accountInformation,
    "AccountInformation saved successfully"
  );
});

const edit = catchAsync(async (req, res, next) => {
  let result = await db.AccountInfo.findByIdAndUpdate(
    { _id: req.params.id },
    { $set: req.body },
    { new: true }
  );

  result = await db.AccountInfo.findOne({ _id: result._id })
    .populate("agent_id", ["first_name", "last_name"])
    .exec();

  if (!result) {
    return next(new AppError("No data found.", 404));
  }
  return successResponse(
    res,
    result,
    "AccountInformation updated successfully"
  );
});

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

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

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