Please analyze the code and figure out how the messages are getting saved in the database(historymodel), {mongoose db is being used here}

code-
Chatgpt.listmessages = async (req) => {
  let data=req.body;
  let categorykey = data.categorykey;
  let currentpage = data.currentpage-1;
  let numberofdocs = data.numberofdocs;

  if (!categorykey) {
    reject("categorykey required");
    return;
  }

  if (!currentpage) {
    currentpage = 0
  }

  if (!numberofdocs) {
    numberofdocs = 10
  }

  let userId = req.authUser ? req.authUser.userId : 0;
  if (!userId) {
    reject("login required");
    return;
  }
  let usrdata = await UserModel.findOne({ _id: userId }).select({
    email:1,
    name:1,
    _id:1,
    dob:1,
    gender:1,
    mobileNo:1,
    profilePicture:1,
    userToken:1,
    status:1,
    feedback:1,
    signuptype:1,
  }).exec();
  if(usrdata.status != "active"){
    reject("User is Inactive");
  }
  
  let totalcount = await HistoryModel.find({ user_id: userId,categorykey:categorykey,status:1 }).select({
    _id:1,
  }).sort({createdAt:-1}).lean().exec();

return new Promise((resolve, reject) => {
  HistoryModel.find({ user_id: userId,categorykey:categorykey,status:1 }).select({
    _id:1,
    message:1,
    message_from:1,
    categorykey:1,
    categoryname:1,
    isLike:1,
    isDisLike:1,
    translator:1,
    isSave:1,
    status:1,
    createdAt:1
  }).sort({createdAt:-1}).skip(numberofdocs * currentpage)
  .limit(numberofdocs).lean().exec((err, res) => {
    if (err) {
      reject(err);
    } else {
      let responsedata = {
        data:res,
        totalCount:totalcount.length
      }
      resolve(responsedata);
    }
  });
});
};

Chatgpt.savedmessage = (req) => {
  let data=req.body;
  let messageid = data.messageid;

return new Promise(async (resolve, reject) => {
  let userId = req.authUser ? req.authUser.userId : 0;
  if (!userId) {
    reject("login required");
    return;
  }
  let usrdata = await UserModel.findOne({ _id: userId }).select({
    email:1,
    name:1,
    _id:1,
    dob:1,
    gender:1,
    mobileNo:1,
    profilePicture:1,
    userToken:1,
    status:1,
    feedback:1,
    signuptype:1,
  }).exec();
  if(usrdata.status != "active"){
    reject("User is Inactive");
  }
  
  if (!messageid) {
    reject("messageid required");
    return;
  }
if(ObjectId.isValid(messageid)){
  HistoryModel.find({ user_id: userId,_id:new ObjectId(messageid) }).select({
    _id:1,
    message:1,
    message_from:1,
    categoryname:1,
    user_id:1,
    categorykey:1,
    isLike:1,
    isDisLike:1,
    translator:1,
    isSave:1,
    status:1,
    createdAt:1
  }).lean().exec((err, msgres) => {
    if (err) {
      reject(err);
    } else {
      if (msgres.length > 0) {
        SavedmessageModel.find({ message_id:messageid }).select({
          _id:1,
        }).lean().exec((err, res) => { 
          if (err) {
            reject(err);
          } else {
            if (res.length == 0) {
              let savedmsgobject={
                text: msgres[0].message,
                categorykey:msgres[0].categorykey,
                categoryname:msgres[0].categoryname,
                user_id:msgres[0].user_id,
                message_id:messageid,
                isLike:msgres[0].isLike,
                isDisLike:msgres[0].isDisLike,
                isSave:true,
                translator:msgres[0].translator,
                createdAt: new Date()
              }
              SavedmessageModel.collection.insertOne(savedmsgobject,(err, data) => {
                if (err) {
                  reject(err);
                } else {
                  if (data) {
                    HistoryModel.findOneAndUpdate(
                      { _id:  new ObjectId(messageid)  },
                      {
                        $set: {
                          isSave:true,
                          updatedAt: new Date()
                        }
                      },
                      { new: true },
                      function (err, data) {
                        if (err) {
                          reject(err);
                        } else {
                          if (data) {
                            resolve("Message Saved Successfully");
                          } else {
                            reject("Message not found");
                          }
                        }
                      }
                    );
                  } else {
                    reject("Kindly try again, some error occured.");
                  }
                }
              });
            }else{
            reject("message already saved");
            }
          }
        });
      }else{
        reject("message not found");
      }
    }
  });
}else{
    reject("message id is invalid");
  }
});
};

Chatgpt.savedmessagelist = async (req) => {
  let data=req.body;
  let currentpage = data.currentpage-1;
  let numberofdocs = data.numberofdocs;

  if (!currentpage) {
    currentpage = 0
  }
  if (!numberofdocs) {
    numberofdocs = 10
  }
  let userId = req.authUser ? req.authUser.userId : 0;
  let totalcount =0
if(userId){
  totalcount = await SavedmessageModel.find({ user_id: userId }).select({
    _id:1,
  }).sort({createdAt:-1}).lean().exec();
}
return new Promise(async (resolve, reject) => {
  if (!userId) {
    reject("login required");
    return;
  }
  let usrdata = await UserModel.findOne({ _id: userId }).select({
    email:1,
    name:1,
    _id:1,
    dob:1,
    gender:1,
    mobileNo:1,
    profilePicture:1,
    userToken:1,
    status:1,
    feedback:1,
    signuptype:1,
  }).exec();
  if(usrdata.status != "active"){
    reject("User is Inactive");
  }
  
  SavedmessageModel.find({ user_id: userId }).select({
    _id :1,
    text:1,
    categorykey:1,
    categoryname:1,
    user_id:1,
    message_id:1,
    isLike:1,
    isDisLike:1,
    isSave:1,
    translator:1,
    createdAt:1,
    updatedAt:1,
  }).sort({createdAt:-1}).skip(numberofdocs * currentpage)
  .limit(numberofdocs).lean().exec((err, res) => {
    if (err) {
      reject(err);
    } else {
      let responsedata = {
        data:res,
        totalCount:totalcount.length
      }
      resolve(responsedata);
    }
  });
});
};