From cbd630dd562cfa8412af236f03f46372e69207d6 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: Thu, 14 Nov 2024 23:31:28 +0900
Subject: [PATCH] =?UTF-8?q?feat:=20=EC=8A=A4=EC=BC=80=EC=A4=84=20=EC=8B=9C?=
 =?UTF-8?q?=EA=B0=84=20=EC=9C=A0=ED=9A=A8=EC=84=B1=20=EA=B2=80=EC=82=AC=20?=
 =?UTF-8?q?(#5)?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 services/scheduleService.js | 61 ++++++++++++++++++++++++++++++++++++-
 1 file changed, 60 insertions(+), 1 deletion(-)

diff --git a/services/scheduleService.js b/services/scheduleService.js
index b202e70..aeb075d 100644
--- a/services/scheduleService.js
+++ b/services/scheduleService.js
@@ -20,6 +20,18 @@ class schedulService {
      */
     async createSchedule({ userId, title, start_time, end_time, is_fixed }) {
         try {
+
+            // 일정 시작 시간 - 종료 시간 유효성 검사
+            if (new Date(start_time) >= new Date(end_time)) {
+                throw new Error('Start time must be before end time');
+            }
+
+            // 중복 검사
+            const overlap = await this.checkScheduleOverlap(userId, start_time, end_time);
+            if (overlap) {
+                throw new Error('Schedule overlaps with existing schedule');
+            }
+
             const scehduleData = {
                 user_id: userId,
                 title,
@@ -49,6 +61,17 @@ class schedulService {
                 throw new Error('schedule not found');
             }
 
+            // 일정 시작 시간 - 종료 시간 유효성 검사
+            if (new Date(updateData.start_time) >= new Date(updateData.end_time)) {
+                throw new Error('Start time must be before end time');
+            }
+
+            // 중복 검사
+            const overlap = await this.checkScheduleOverlap(userId, updateData.start_time, updateData.end_time);
+            if (overlap) {
+                throw new Error('Schedule overlaps with existing schedule');
+            }
+
             // 스케줄 타입 변경하지 못하도록 update값 삭제 -> 기존값 유지
             delete updateData.is_fixed;
             
@@ -153,7 +176,43 @@ class schedulService {
             throw new Error(`Failed to clean expired schedules: ${error.message}`);
         }
     }
+
+
+    /**
+     * 스케줄 중복 검사 -> 기존 스케줄 시간대에 추가 못하도록
+     */
+    async checkScheduleOverlap(userId, start_time, end_time, excludeId = null) {
+        try {
+            const where = {
+                user_id: userId,
+                [Op.or]: [
+                    {
+                        // 새로운 스케줄이 기존 스케줄 내 존재
+                        [Op.and]: [
+                            { start_time: { [Op.lte]: start_time } },
+                            { end_time: { [Op.gte]: start_time } }
+                        ]
+                    },
+                    {
+                        // 새로운 스케줄이 기존 스케줄을 포함
+                        [Op.and]: [
+                            { start_time: { [Op.gte]: start_time } },
+                            { start_time: { [Op.lte]: end_time } }
+                        ]
+                    }
+                ]
+            };
+    
+            if (excludeId) {
+                where.id = { [Op.ne]: excludeId };
+            }
+    
+            const overlappingSchedule = await Schedule.findOne({ where });
+            return overlappingSchedule;
+        } catch (error) {
+            throw new Error(`Failed to check schedule overlap: ${error.message}`);
+        }
+    }
 }
 
 module.exports = new scheduleService();
-
-- 
GitLab