Skip to content

Commit 3ac7a75

Browse files
authored
Refactor friend validation logic in DM conversation creation (#77)
2 parents 18eb9fd + c6ab49a commit 3ac7a75

1 file changed

Lines changed: 4 additions & 5 deletions

File tree

backend/src/friends-service/src/services/chatService.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,16 +49,15 @@ async function findDmBetweenTx(tx, userId, otherUserId) {
4949
}
5050

5151
async function getOrCreateDmConversation(userId, otherUserId) {
52-
const friendId = await getOtherUserIdInFriendship(userId, otherUserId);
53-
if (!friendId || friendId !== otherUserId) {
54-
return { error: { status: 403, code: 'NOT_FRIEND', message: 'You can only chat with friends' } };
55-
}
56-
5752
return prisma.$transaction(async (tx) => {
5853
const existing = await findDmBetweenTx(tx, userId, otherUserId);
5954
if (existing) {
6055
return { conversation: existing };
6156
}
57+
const friendId = await getOtherUserIdInFriendship(userId, otherUserId);
58+
if (!friendId || friendId !== otherUserId) {
59+
return { error: { status: 403, code: 'NOT_FRIEND', message: 'You can only chat with friends' } };
60+
}
6261
const conversation = await tx.conversation.create({
6362
data: {
6463
participants: {

0 commit comments

Comments
 (0)