Skip to content

Commit

Permalink
refactor: word 도메인 리팩토링에 따른 메서드 추가 및 수정 Murakano#89
Browse files Browse the repository at this point in the history
  • Loading branch information
jjikky committed Oct 23, 2024
1 parent d5e48e6 commit e56a2c3
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 8 deletions.
9 changes: 8 additions & 1 deletion src/routes/user/user.repository.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ exports.createUser = async (userData) => {
}
};

exports.findUserById = async (_id) => {
return await User.findOne({ _id });
};

exports.findUserByNickname = async (nickname) => {
const userExists = await User.exists({ nickname });
return userExists ? true : false;
Expand Down Expand Up @@ -199,13 +203,16 @@ exports.getRole = async (userId) => {
}
};

exports.findUserByRequestId = async (requestId) => {
return await User.findOne({ 'requests._id': requestId }).exec();
};

exports.updateRequest = async (requestId, formData) => {
try {
const user = await User.findOne({ 'requests._id': requestId }).select('requests').exec();
if (!user) {
throw new Error('User not found');
}

const request = user.requests.find((req) => req._id.toString() === requestId && req.deletedAt === null);
if (request) {
if (formData.addInfo !== undefined) {
Expand Down
36 changes: 29 additions & 7 deletions src/routes/user/user.service.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
const wordService = require('../word/word.service');
const userRepository = require('./user.repository');
const wordRepository = require('../word/word.repository');
const redisClient = require('../../common/modules/redis');
const { generateAccessToken, generateRefreshToken } = require('../../common/utils/auth');
const config = require('../../common/config');

exports.register = async (userData) => {
const newUser = {
Expand All @@ -14,6 +17,16 @@ exports.kakaoRegister = async (newUser) => {
return await userRepository.createUser(newUser);
};

exports.handleLogin = async (user) => {
const accessToken = generateAccessToken(user);
const refreshToken = generateRefreshToken(user);

await redisClient.set(user.email, refreshToken);
await redisClient.expire(user.email, config.cookieInRefreshTokenOptions.maxAge / 1000);

return { accessToken, refreshToken };
};

exports.isNicknameExist = async (nickname) => {
const isUserExist = await userRepository.findUserByNickname(nickname);
return isUserExist;
Expand All @@ -29,6 +42,14 @@ exports.isKaKaoUserExist = async (snsId) => {
return user;
};

exports.findUserById = async (userId) => {
return await userRepository.findUserById(userId);
};

exports.isUserExist = async (userId) => {
return await userRepository.findUserById(userId);
};

exports.getRecentSearches = async (userId) => {
const recentSearches = await userRepository.getRecentSearches(userId);
return recentSearches;
Expand All @@ -38,14 +59,12 @@ exports.delRecentSearch = async (userId, searchTerm) => {
return await userRepository.delRecentSearch(userId, searchTerm);
};

// 최근 검색어 저장
exports.updateRecentSearch = async (userID, searchTerm) => {
if (userID) {
await userRepository.updateRecentSearch(userID, searchTerm);
}
};

// 단어 추가 및 수정
exports.postWords = async (userId, formData, nickname, type) => {
const word = await userRepository.postWords(userId, formData, nickname, type);
return word;
Expand All @@ -70,6 +89,10 @@ exports.getRole = async (userId) => {
return role;
};

exports.findUserByRequestId = async (requestId) => {
return await userRepository.findUserByRequestId(requestId);
};

exports.updateRequest = async (requestId, formData) => {
if (requestId) {
await userRepository.updateRequest(requestId, formData);
Expand All @@ -78,13 +101,12 @@ exports.updateRequest = async (requestId, formData) => {

exports.updateRequestState = async (userId, requestId, status, formData, requestType) => {
if (userId) {
await userRepository.updateRequestState(userId, requestId, status, formData);
await userRepository.updateRequestState(requestId, status);
if (requestType === 'add') {
//TODO: add 일 때, 단어 중복검사 로직 우선적 검증 추가
await wordRepository.addWord(requestId, formData);
await wordService.addWord(requestId, formData);
await userRepository.updateRequest(requestId, formData);
} else if (requestType === 'mod') {
await wordRepository.updateWord(requestId, formData);
await wordService.updateWord(requestId, formData);
await userRepository.updateRequest(requestId, formData);
} else {
console.log('requestType 오류');
Expand Down

0 comments on commit e56a2c3

Please sign in to comment.