Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/user #54

Merged
merged 2 commits into from
Jul 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/routes/user/user.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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,
Expand Down
25 changes: 21 additions & 4 deletions src/routes/word/word.repository.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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;
}
}
};
6 changes: 5 additions & 1 deletion src/routes/word/word.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};