Sindbad~EG File Manager

Current Path : /home/infinitibizsol/.trash/controllers.8/common/
Upload File :
Current File : /home/infinitibizsol/.trash/controllers.8/common/personalinfo.js

import db from "../../model/index";
import AppError from "../../utils/apiError";
import catchAsync from "../../utils/catchAsync";
import { successResponse } from "../../utils/responseFormat";

const index = catchAsync(async (req, res, next) => {
  const query = req.query;
  let allData = await db.PersonalInfo.find(query, {
    __v: 0,
    createdOn: 0,
    modifiedOn: 0,
  })
    .populate("contact_id", ["_id", "first_name"])
    .populate("user_id", ["_id", "first_name"])
    .exec();
  return successResponse(res, allData);
});

const add = catchAsync(async (req, res, next) => {
  const address = new db.PersonalInfo(req.body);
  await address.save();
  return successResponse(res, address, "PersonalInfo saved successfully");
});

const view = catchAsync(async (req, res, next) => {
  let notes = await db.PersonalInfo.findOne({ user_id: req.user._id });
  if (!notes) {
    return next(new AppError("No data found.", 404));
  }
  return successResponse(res, notes);
});

const edit = catchAsync(async (req, res, next) => {
  let result = await db.PersonalInfo.findByIdAndUpdate(
    { _id: req.params.id },
    { $set: req.body },
    { new: true }
  );
  if (!result) {
    return next(new AppError("No data found.", 404));
  }
  return successResponse(res, result, "PersonalInfo updated successfully");
});

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

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

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