diff --git a/controllers/Temp.js b/controllers/Temp.js index 9d4894ce..c99e4e71 100644 --- a/controllers/Temp.js +++ b/controllers/Temp.js @@ -3526,16 +3526,12 @@ class TempController { }) } - static #threadpostcommentreply = (userId, comment, userName, postId, commentId) => { + static #threadpostcommentreply = (userId, comment, postId, commentId) => { return new Promise(resolve => { if (typeof comment !== 'string') { return resolve(HTTPWTHandler.badInput(`comment must be a string. Provided type: ${typeof comment}`)) } - if (typeof userName !== 'string') { - return resolve(HTTPWTHandler.badInput(`userName must be a string. Provided type: ${typeof userName}`)) - } - if (typeof postId !== 'string') { return resolve(HTTPWTHandler.badInput(`postId must be a string. Provided type: ${typeof postId}`)) } @@ -3561,38 +3557,34 @@ class TempController { //Find User User.findOne({_id: {$eq: userId}}).lean().then(result => { if (result) { - if (result.name == userName) { - Thread.findOne({_id: {$eq: postId}}).lean().then(data => { - if (data) { - const comments = data.comments; - - const commentIndex = comments.findIndex(item => String(item.commentId) === commentId) + Thread.findOne({_id: {$eq: postId}}).lean().then(data => { + if (data) { + const comments = data.comments; - if (commentIndex === -1) { - return resolve(HTTPWTHandler.badInput("Couldn't find comment")) - } + const commentIndex = comments.findIndex(item => String(item.commentId) === commentId) - const objectId = new mongoose.Types.ObjectId() - console.log(objectId) - var commentForPost = {commentId: objectId, commenterId: userId, commentsText: comment, commentUpVotes: [], commentDownVotes: [], datePosted: Date.now()} - Thread.findOneAndUpdate({_id: {$eq: postId}}, { $push: { [`comments.${sentIndex}.commentReplies`]: commentForPost } }).then(function(){ - console.log("SUCCESS1") - return resolve(HTTPWTHandler.OK('Comment upload successful')) - }) - .catch(err => { - console.error('An error occurred while pushing:', commentForPost, 'to:', `comments.${sentIndex}.commentReplies`, ' for thread with id:', postId, '. The error was:', err) - return resolve(HTTPWTHandler.serverError('An error occurred while adding comment. Please try again.')) - }); - } else { - return resolve(HTTPWTHandler.notFound('Could not find thread')) + if (commentIndex === -1) { + return resolve(HTTPWTHandler.badInput("Couldn't find comment")) } - }).catch(error => { - console.error('An error occurred while finding thread with id:', postId, '. The error was:', error) - return resolve(HTTPWTHandler.serverError('An error occurred while finding thread. Please try again.')) - }) - } else { - return resolve(HTTPWTHandler.badInput('name in database does not match up with userName provided')) - } + + const objectId = new mongoose.Types.ObjectId() + console.log(objectId) + var commentForPost = {commentId: objectId, commenterId: userId, commentsText: comment, commentUpVotes: [], commentDownVotes: [], datePosted: Date.now()} + Thread.findOneAndUpdate({_id: {$eq: postId}}, { $push: { [`comments.${sentIndex}.commentReplies`]: commentForPost } }).then(function(){ + console.log("SUCCESS1") + return resolve(HTTPWTHandler.OK('Comment upload successful')) + }) + .catch(err => { + console.error('An error occurred while pushing:', commentForPost, 'to:', `comments.${sentIndex}.commentReplies`, ' for thread with id:', postId, '. The error was:', err) + return resolve(HTTPWTHandler.serverError('An error occurred while adding comment. Please try again.')) + }); + } else { + return resolve(HTTPWTHandler.notFound('Could not find thread')) + } + }).catch(error => { + console.error('An error occurred while finding thread with id:', postId, '. The error was:', error) + return resolve(HTTPWTHandler.serverError('An error occurred while finding thread. Please try again.')) + }) } else { return resolve(HTTPWTHandler.notFound('Could not find user with provided userId')) } @@ -6565,8 +6557,8 @@ class TempController { return await this.#threadpostcomment(userId, comment, postId) } - static threadpostcommentreply = async (userId, comment, userName, postId, commentId) => { - return await this.#threadpostcommentreply(userId, comment, userName, postId, commentId) + static threadpostcommentreply = async (userId, comment, postId, commentId) => { + return await this.#threadpostcommentreply(userId, comment, postId, commentId) } static searchforthreadcomments = async (userId, threadId) => { diff --git a/routes/Temp.js b/routes/Temp.js index bdf1a0fb..bb2f471b 100644 --- a/routes/Temp.js +++ b/routes/Temp.js @@ -1737,7 +1737,7 @@ router.post('/threadpostcommentreply', rateLimiters['/threadpostcommentreply'], const worker = new Worker(workerPath, { workerData: { functionName: 'threadpostcommentreply', - functionArgs: [req.tokenData, req.body.comment, req.body.userName, req.body.postId, req.body.commentId] + functionArgs: [req.tokenData, req.body.comment, req.body.postId, req.body.commentId] } })