if 회원이 쓴 게시물이고 댓글삭제하는 사람이 게시물작성자이고, 지우려는 댓글의작성자가 회원또는 비회원일경우

if 회원이 쓴 게시물이고 댓글삭제하는 사람이 회원이고 ,지우려는 댓글의작성자가 회원일경우

if 회원이 쓴 게시물이고 댓글삭제하는 사람이 회원이고 ,지우려는 댓글의작성자가 비회원일경우

if 비회원이 쓴 게시물이고, 댓글삭제하는 사람이 회원이고, 지우려는 댓글의작성자가 댓글 삭제하는 사람과 같은 사람일경우

if 비회원이 쓴 게시물이고 , 댓글 삭제하는 사람이 비회원이고,지우려는 댓글의 작성자가 비회원일경우


뭐 대충 이정도 유즈케이스를 이따구 로직으로밖에 못품. ㅋㅋㅋ 


 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 && reply.author) {

            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 (post.author && isUser && !reply.author) {

            if ((post.author._id+'') === (req.user._id+'') || 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 {

            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: '사용자 인증에 실패하였습니다.'

                });

              }

            }

          }

        }

      });

    }

  });


};