diff --git a/repositorys/followRepository.js b/repositorys/followRepository.js index 60958e5..12ac2bd 100644 --- a/repositorys/followRepository.js +++ b/repositorys/followRepository.js @@ -185,6 +185,17 @@ const createFollow = async (followerId, followingId) => { }; const deleteFollow = async (userId, targetUserId) => { + const followCheck = await prisma.follow.findUnique({ + where: { + follower_id_following_id: { + follower_id: userId, + following_id: targetUserId, + }, + }, + }); + if (!followCheck) { + throw new Error('팔로우 관계가 존재하지 않습니다.'); + } const follow = await prisma.follow.delete({ where: { follower_id_following_id: { @@ -197,6 +208,17 @@ const deleteFollow = async (userId, targetUserId) => { }; const deleteFollower = async (userId, targetUserId) => { + const followCheck = await prisma.follow.findUnique({ + where: { + follower_id_following_id: { + follower_id: targetUserId, + following_id: userId, + }, + }, + }); + if (!followCheck) { + throw new Error('팔로우 관계가 존재하지 않습니다.'); + } const follow = await prisma.follow.delete({ where: { follower_id_following_id: { diff --git a/repositorys/userRepository.js b/repositorys/userRepository.js index 0341448..99a163a 100644 --- a/repositorys/userRepository.js +++ b/repositorys/userRepository.js @@ -303,21 +303,35 @@ const checkEmail = async (email) => { }; const resetPassword = async (email) => { - const user = await prisma.user.findUnique({ - where: { email }, - }); - if (!user) { - throw new Error('존재하지 않는 이메일입니다.'); - } - const newPassword = Math.random().toString(36).substring(2, 15); - const hashedPassword = await bcrypt.hash(newPassword, 10); - await sendEmailAuth(email, newPassword); - await prisma.user.update({ - where: { email }, - data: { password: hashedPassword }, - }); + try { + const result = await prisma.$transaction(async (tx) => { + const user = await tx.user.findUnique({ + where: { email }, + }); + if (!user) { + throw new Error('존재하지 않는 이메일입니다.'); + } - return; + const newPassword = Math.random().toString(36).substring(2, 15); + const hashedPassword = await bcrypt.hash(newPassword, 10); + + await sendEmailAuth(email, newPassword); + + await tx.user.update({ + where: { email }, + data: { password: hashedPassword }, + }); + + return newPassword; + }); + + return result; + } catch (error) { + if (error.message === '존재하지 않는 이메일입니다.') { + throw error; + } + throw new Error('비밀번호 재설정 중 오류가 발생했습니다.'); + } }; const passwordChange = async (userId, newPassword, confirmPassword) => {