diff --git a/src/routes/user/user.controller.js b/src/routes/user/user.controller.js index f86eaa4..4e22ff7 100755 --- a/src/routes/user/user.controller.js +++ b/src/routes/user/user.controller.js @@ -3,6 +3,7 @@ const jwt = require('jsonwebtoken'); const config = require('../../common/config'); const userService = require('./user.service'); +const wordService = require('../word/word.service'); const sendResponse = require('../../common/utils/response-handler'); const ErrorMessage = require('../../common/constants/error-message'); const SuccessMessage = require('../../common/constants/success-message'); @@ -319,6 +320,7 @@ exports.updateRequestState = async (req, res) => { exports.deleteUser = async (req, res) => { try { const { _id } = req.user; + await wordService.deleteWordContributor(_id); await userService.deleteUser(_id); sendResponse.ok(res, { message: SuccessMessage.DELETE_USER_SUCCESS, diff --git a/src/routes/word/word.repository.js b/src/routes/word/word.repository.js index c5cb8dd..eecfc5d 100644 --- a/src/routes/word/word.repository.js +++ b/src/routes/word/word.repository.js @@ -73,7 +73,7 @@ exports.getAllWords = async (isSorted, page, limit) => { exports.addWord = async (requestId, formData) => { try { - // requestId에 해당하는 request를 찾습니다. + // requestId에 해당하는 request를 찾습니다. const user = await User.findOne({ 'requests._id': requestId }); if (!user) { console.log('User with the given request not found'); @@ -141,14 +141,31 @@ exports.updateWord = async (requestId, formData) => { } }; +exports.deleteWordContributor = async (_id) => { + // _id가 일치하는 유저의 닉네임 값을 가져오고, 해당 유저가 suggestedBy로 있는 모든 단어의 suggestedBy를 null로 변경 + try { + const user = await User.findById(_id); + + if (!user) { + console.log('User not found'); + return null; + } + const nickname = user.nickname; + + const words = await Word.updateMany({ suggestedBy: nickname }, { suggestedBy: null }); + return words; + } catch (error) { + console.log('Error while deleting word contributor:', error); + return null; + } +}; exports.checkDuplicateWord = async (word) => { try { const wordExists = await Word.findOne({ word: { $regex: new RegExp(`^${word}$`, 'i') } }); console.log('wordExists:', wordExists); return wordExists; - } - catch (error) { + } catch (error) { console.log('Error while checking duplicate word:', error); return null; } -} \ No newline at end of file +}; diff --git a/src/routes/word/word.service.js b/src/routes/word/word.service.js index c4d10e3..bd48baa 100644 --- a/src/routes/word/word.service.js +++ b/src/routes/word/word.service.js @@ -24,9 +24,13 @@ exports.getAllWords = async (sort, page, limit) => { return words; }; +exports.deleteWordContributor = async (_id) => { + return await wordRepository.deleteWordContributor(_id); +}; + // 등록 단어 중복 검사 exports.checkDuplicateWord = async (word) => { const isDuplicate = await wordRepository.checkDuplicateWord(word); - console.log("isDuplicate: ", isDuplicate) + console.log('isDuplicate: ', isDuplicate); return isDuplicate; };