From 0fc2b8ee3bf3aa00c2662951349e121e74c43f71 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=EC=A1=B0=EB=8C=80=ED=9D=AC?= <joedaehui@ajou.ac.kr>
Date: Fri, 15 Nov 2024 23:00:13 +0900
Subject: [PATCH] =?UTF-8?q?feat:=20=EC=B9=9C=EA=B5=AC=20=EC=9A=94=EC=B2=AD?=
 =?UTF-8?q?=20=EB=B3=B4=EB=82=B4=EA=B8=B0=20(#7)?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 services/friendService.js | 38 ++++++++++++++++++++++++++++++++++++++
 1 file changed, 38 insertions(+)
 create mode 100644 services/friendService.js

diff --git a/services/friendService.js b/services/friendService.js
new file mode 100644
index 0000000..8b93749
--- /dev/null
+++ b/services/friendService.js
@@ -0,0 +1,38 @@
+const { Op } = require('sequelize');
+const Friend = require('../models/Friend');
+const User = require('../models/user');
+
+class friendService {
+    
+    /**
+     * 친구 요청 보내기
+     * 나 자신에게 보내기 or 이미 존재하는 친구 -> X
+     * 이후, PENDING 상태로 변환 -> 수락/거절에 따라 변화
+     */
+    async sendFriendRequest(userId, friendId) {
+        if (userId === friendId) {
+            throw new Error('Cannot send friend request to yourself');
+        }
+
+        const existingFriend = await Friend.findOne({
+            where: {
+                [Op.or]: [
+                    { user_id: userId, friend_id: friendId },
+                    { user_id: friendId, friend_id: userId }
+                ]
+            }
+        });
+
+        if (existingFriend) {
+            throw new Error('Friend request already exists');
+        }
+
+        return Friend.create({
+            user_id: userId,
+            friend_id: friendId,
+            status: 'PENDING'
+        });
+    }
+}
+
+module.exports = new FriendService();
\ No newline at end of file
-- 
GitLab