From 6958b3d4989e43895d887377097ea2cca8c8b9da 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: Wed, 4 Dec 2024 18:46:26 +0900
Subject: [PATCH] =?UTF-8?q?refactor:=20=EC=8A=A4=EC=BC=80=EC=A4=84=20?=
 =?UTF-8?q?=EC=84=9C=EB=B9=84=EC=8A=A4=20=EC=88=98=EC=A0=95=EC=97=90=20?=
 =?UTF-8?q?=EB=94=B0=EB=A5=B8=20=EC=BB=A8=ED=8A=B8=EB=A1=A4=EB=9F=AC,=20?=
 =?UTF-8?q?=EB=AF=B8=ED=8C=85=20=EC=84=9C=EB=B9=84=EC=8A=A4=20=EC=88=98?=
 =?UTF-8?q?=EC=A0=95?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 controllers/scheduleController.js | 60 +++++++++++++++----------------
 services/meetingService.js        | 51 +++++++++++---------------
 2 files changed, 49 insertions(+), 62 deletions(-)

diff --git a/controllers/scheduleController.js b/controllers/scheduleController.js
index 4d43ee1..e2c7122 100644
--- a/controllers/scheduleController.js
+++ b/controllers/scheduleController.js
@@ -22,22 +22,19 @@ class scheduleController {
     async createSchedule(req, res) {
         try {
             const userId = req.user.id;
-            const scheduleRequestDTO = new ScheduleRequestDTO(req.body);
-            const validatedData = scheduleRequestDTO.validate('create'); // 'create' 타입 검증
+            const { title, is_fixed, time_indices } = req.body;
 
-            const { title, is_fixed, events } = validatedData;
-
-            const schedules = await ScheduleService.createSchedules({
+            const schedule = await ScheduleService.createSchedules({
                 userId,
                 title,
                 is_fixed,
-                events
+                time_indices
             });
 
             return res.status(201).json({
                 success: true,
                 data: {
-                    schedules
+                    schedule
                 }
             });
         } catch (error) {
@@ -49,7 +46,7 @@ class scheduleController {
                 }
             });
         }
-    }
+    }   
 
     /**
      * 스케줄 수정
@@ -57,31 +54,32 @@ class scheduleController {
      * Bulk update 지원
      * 요청 본문 예시:
      * {
-     *   updates: [
-     *     { time_idx: 36, title: 'New Title', is_fixed: true },
-     *     { time_idx: 44, title: 'Another Title' },
-     *     // ...
-     *   ]
+     *  "originalTitle": "알고리즘 스터디", // 기존 스케줄의 제목
+     *  "title": "알고리즘 스터디 2.0",     // 변경할 제목 (제목 변경 안할거면 기존 제목을 넣어야함 * -> title로 동일 스케줄을 찾아서)
+     *   "is_fixed": true,  
+     *   "time_indices": [36, 37, 38, 40]   // 변경할 time_indices 배열
      * }
      */
     async updateSchedules(req, res) {
         try {
             const userId = req.user.id;
-            const scheduleRequestDTO = new ScheduleRequestDTO(req.body);
-            const validatedData = scheduleRequestDTO.validate('bulk_update'); // 'bulk_update' 타입 검증
-
-            const { updates } = validatedData;
+            const { originalTitle, title, is_fixed, time_indices } = req.body;
 
-            const updatedSchedules = await ScheduleService.updateSchedules(userId, updates);
+            const updatedSchedule = await ScheduleService.updateSchedules(userId, {
+                originalTitle, 
+                title,         
+                is_fixed,
+                time_indices
+            });
 
             return res.status(200).json({
                 success: true,
                 data: {
-                    schedules: updatedSchedules
+                    schedule: updatedSchedule
                 }
             });
         } catch (error) {
-            if (error.code === 'SCHEDULE_NOT_FOUND') {
+            if (error.message === 'Schedule not found') {
                 return res.status(404).json({
                     success: false,
                     error: {
@@ -106,24 +104,21 @@ class scheduleController {
      * Bulk delete 지원
      * 요청 본문 예시:
      * {
-     *   time_idxs: [36, 44, ...]
+     *  "title": "알고리즘 스터디"
      * }
      */
     async deleteSchedules(req, res) {
         try {
             const userId = req.user.id;
-            const scheduleRequestDTO = new ScheduleRequestDTO(req.body);
-            const validatedData = scheduleRequestDTO.validate('bulk_delete'); // 'bulk_delete' 타입 검증
-
-            const { time_idxs } = validatedData;
+            const { title } = req.body;
 
-            const result = await ScheduleService.deleteSchedules(userId, time_idxs);
+            const result = await ScheduleService.deleteSchedules(userId, title);
 
             return res.status(200).json({
                 success: true,
                 data: {
-                    message: 'Schedules successfully deleted',
-                    deleted_time_idxs: result.deleted_time_idxs
+                    message: 'Schedule successfully deleted',
+                    deletedCount: result.deletedCount
                 }
             });
         } catch (error) {
@@ -136,7 +131,6 @@ class scheduleController {
             });
         }
     }
-
     /**
      * 해당 사용자 전체 스케줄 조회
      * GET /api/schedule/all
@@ -148,7 +142,9 @@ class scheduleController {
 
             return res.status(200).json({
                 success: true,
-                data: schedules
+                data: {
+                    schedules
+                }
             });
         } catch (error) {
             return res.status(500).json({
@@ -175,7 +171,9 @@ class scheduleController {
 
             return res.status(200).json({
                 success: true,
-                data: schedule
+                data: {
+                    schedule
+                }
             });
         } catch (error) {
             if (error.message === 'Schedule not found') {
diff --git a/services/meetingService.js b/services/meetingService.js
index 24b9e67..746597d 100644
--- a/services/meetingService.js
+++ b/services/meetingService.js
@@ -118,20 +118,16 @@ class MeetingService {
                 { transaction }
             );
 
-            // 스케줄 생성 (모임 시간 범위 내 모든 time_idx에 대해 생성)
-            const events = [];
-            for (let idx = time_idx_start; idx <= time_idx_end; idx++) {
-                events.push({ time_idx: idx });
-            }
-            await ScheduleService.createSchedules(
-                {
-                    userId: created_by,
-                    title: `번개 모임: ${title}`,
-                    is_fixed: false,
-                    events: events,
-                },
-                transaction
+            const time_indices = Array.from(
+                { length: time_idx_end - time_idx_start + 1 },
+                (_, i) => time_idx_start + i
             );
+            await ScheduleService.createSchedules({
+                userId: created_by,
+                title: `번개 모임: ${title}`,
+                is_fixed: false,
+                time_indices: time_indices,
+            }, transaction);
 
             // 친구 초대 로직 호출
             const invitedFriendIds = await this.sendInvites({
@@ -264,24 +260,17 @@ class MeetingService {
             { transaction }
           );
 
-          // 스케줄 생성 (모임 시간 범위 내 모든 time_idx에 대해 생성)
-          const events = [];
-          for (
-            let idx = meeting.time_idx_start;
-            idx <= meeting.time_idx_end;
-            idx++
-          ) {
-            events.push({ time_idx: idx });
-          }
-          await ScheduleService.createSchedules(
-            {
-              userId: userId,
-              title: `번개 모임: ${meeting.title}`,
-              is_fixed: false,
-              events: events,
-            },
-            transaction
-          );
+        const time_indices = Array.from(
+            { length: meeting.time_idx_end - meeting.time_idx_start + 1 },
+            (_, i) => meeting.time_idx_start + i
+        );
+        
+        await ScheduleService.createSchedules({
+            userId: userId,
+            title: `번개 모임: ${meeting.title}`,
+            is_fixed: false,
+            time_indices: time_indices,
+        }, transaction);
 
           // 채팅방 참가 (MongoDB)
           const user = await User.findOne({
-- 
GitLab