Skip to content
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: 1 addition & 1 deletion src/controllers/friend.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ export const handleGetOutgoingFriendRequests = async (req, res, next) => {
export const handleAcceptFriendRequest = async (req, res, next) => {
// 친구 신청 수락 로직 구현
const receiverUserId = req.user.id;
const { requesterUserId } = req.params;
const requesterUserId = Number(req.params.requesterUserId);
userIsNull(receiverUserId, requesterUserId);
try {
const result = await acceptFriendRequest(receiverUserId, requesterUserId);
Expand Down
13 changes: 13 additions & 0 deletions src/repositories/session.repository.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,19 @@ export const findMatchingSessionBySessionId = async (sessionId) => {
});
};

export const findMatchingSessionByParticipantUserId = async (userId, otherUserId) => {
const session = await prisma.matchingSession.findFirst({
where: {
participants: {
every: {
userId: { in: [userId, otherUserId] },
},
},
},
});
return session;
};

export const countMatchingSessionByUserId = async (userId) => {
const participants = await prisma.sessionParticipant.findMany({
where: { userId },
Expand Down
18 changes: 13 additions & 5 deletions src/services/friend.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import {
ConflictError,
InternalServerError,
} from "../errors/base.error.js";
import { findMatchingSessionByParticipantUserId, updateMatchingSessionToFriends, updateMatchingSessionToDiscard } from "../repositories/session.repository.js";
import { SessionInternalError, SessionNotFoundError } from "../errors/session.error.js";

async function userExistsOrThrow(userId) {
const userById = await findUserById(userId);
Expand All @@ -42,12 +44,12 @@ async function assertUsersExistOrThrow(userId, targetUserId) {

if (!userById)
throw new InvalidUserError(undefined, "잘못된 유저 정보 입력입니다.", {
userId,
userId
});

if (!targetUserById)
throw new InvalidUserError(undefined, "잘못된 유저 정보 입력입니다.", {
targetUserId,
targetUserId
});
}

Expand Down Expand Up @@ -178,10 +180,17 @@ export const getOutgoingFriendRequests = async (userId) => {
}
};

// 5) 친구 신청 수락
// 5) 친구 신청 수락 (receiverUserId: userId, requesterUserId: targetUserId)
export const acceptFriendRequest = async (receiverUserId, requesterUserId) => {
await assertUsersExistOrThrow(receiverUserId, requesterUserId);

const session = await findMatchingSessionByParticipantUserId(receiverUserId, requesterUserId);
if (!session) {
throw new SessionNotFoundError(undefined, undefined, {receiverUserId, requesterUserId});
}
const friendsSession = await updateMatchingSessionToFriends(session.id);
if(friendsSession.status != "FRIENDS") {
throw new SessionInternalError(undefined, "세션 상태 변경에 실패했습니다.", {sessionId: session.id});
}
const req = await userExistsFriendRequest(receiverUserId, requesterUserId);
if (!req)
throw new FriendRequestNotFoundError(undefined, undefined, {
Expand Down Expand Up @@ -221,7 +230,6 @@ export const acceptFriendRequest = async (receiverUserId, requesterUserId) => {
// 6) 친구 신청 거절
export const rejectFriendRequest = async (requesterUserId, receiverUserId) => {
await assertUsersExistOrThrow(requesterUserId, receiverUserId);

try {
const rejectedFriendRequest = await updateFriendRequestRejectTx(
requesterUserId, receiverUserId
Expand Down