var replyProc = function (doc, req, res, callback) {


  for(var i = 0; i < doc.length; i++) {

    if (doc[i].author) {

      doc[i].writer = doc[i].author.displayName;

      if (req.user) {

        if ((req.user._id+'') === (doc[i].author._id+'')) {

          doc[i].status = 'mine';

        } else {

          doc[i].status = 'users';

        }

      } else {

        doc[i].status = 'guests';

      }

    }

    doc[i].date = moment(doc[i].date).format('YYYY년 MMMM Do a h:mm:ss');

  }


  callback(null, doc);


};


var getReply = function (postId, req, res, callback) {


  Reply.find({ postId : postId }, { password: 0 }).populate('author',{ _id:'_id', displayName:'displayName' }).exec(function (err,doc){

    if(err) {

      return res.status(400).send({

        message: errorHandler.getErrorMessage(err)

      });

    } else {

      async.waterfall([

        function(callback){

          replyProc(doc, req, res, callback);

        }

      ],

      function(err,result){

        if (result) {

          callback(null, doc);

        }

      }); 

    }

  });


};


var removeReply = function(req, res, callback) {

  Reply.remove({ _id: req.body._id }).exec(function (err,doc){

    if(err) {

      return res.status(400).send({

        message: errorHandler.getErrorMessage(err)

      });

    } else {

      callback(null);

    }

  });

};


exports.deleteReply = function (req, res) {


  var isUser = (req.user) ? true : false;


  Reply.findById({ _id: req.body._id }).populate('author','_id').exec(function (err,reply){

    if(err) {

      return res.status(400).send({

        message: errorHandler.getErrorMessage(err)

      });

    } else {

      Post.findById({ _id: reply.postId }).populate('author','_id').exec(function (err,post){

        if(err) {

          return res.status(400).send({

            message: errorHandler.getErrorMessage(err)

          });

        } else {

          if (post.author && isUser) {

            if ((post.author._id+'') === (req.user._id+'') || (reply.author._id+'') === (req.user._id+'')) {

              async.waterfall([

                function(callback){

                  removeReply(req, res, callback);

                },

                function(callback){

                  getReply(req.body.postId, req, res, callback);

                }

              ],

              function(err,result){

                res.json(result);

              });

            }

          } else {

            if (reply.author) {

              if (isUser) {

                if ((reply.author._id+'') === (req.user._id+'')) {

                  async.waterfall([

                    function(callback){

                      removeReply(req, res, callback);

                    },

                    function(callback){

                      getReply(req.body.postId, req, res, callback);

                    }

                  ],

                  function(err,result){

                    res.json(result);

                  });

                }

              }

            } else {

              if (reply.password === req.body.password) {

                async.waterfall([

                  function(callback){

                    removeReply(req, res, callback);

                  },

                  function(callback){

                    getReply(req.body.postId, req, res, callback);

                  }

                ],

                function(err,result){

                  res.json(result);

                });

              } else {

                return res.status(400).send({

                  message: '사용자 인증에 실패하였습니다.'

                });

              }

            }

          }

        }

      });

    }

  });

 

};


살려주세요~~