From e56a2c3a61620327200621122d08c640e8140fb9 Mon Sep 17 00:00:00 2001 From: jjikky Date: Thu, 24 Oct 2024 01:46:32 +0900 Subject: [PATCH] =?UTF-8?q?refactor:=20word=20=EB=8F=84=EB=A9=94=EC=9D=B8?= =?UTF-8?q?=20=EB=A6=AC=ED=8C=A9=ED=86=A0=EB=A7=81=EC=97=90=20=EB=94=B0?= =?UTF-8?q?=EB=A5=B8=20=EB=A9=94=EC=84=9C=EB=93=9C=20=EC=B6=94=EA=B0=80=20?= =?UTF-8?q?=EB=B0=8F=20=EC=88=98=EC=A0=95=20#89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/routes/user/user.repository.js | 9 +++++++- src/routes/user/user.service.js | 36 ++++++++++++++++++++++++------ 2 files changed, 37 insertions(+), 8 deletions(-) diff --git a/src/routes/user/user.repository.js b/src/routes/user/user.repository.js index 89f8f43..8229166 100644 --- a/src/routes/user/user.repository.js +++ b/src/routes/user/user.repository.js @@ -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; @@ -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) { diff --git a/src/routes/user/user.service.js b/src/routes/user/user.service.js index 027cb0b..3ecb73a 100644 --- a/src/routes/user/user.service.js +++ b/src/routes/user/user.service.js @@ -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 = { @@ -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; @@ -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; @@ -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; @@ -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); @@ -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 오류');