From 387f65ca3d9dd3f0251a3da582c50bfc94b8d782 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:42:12 +0900
Subject: [PATCH] =?UTF-8?q?refactor:=20=EC=8A=A4=EC=BC=80=EC=A4=84=20?=
 =?UTF-8?q?=EC=83=9D=EC=84=B1/=EC=88=98=EC=A0=95/=EC=82=AD=EC=A0=9C=20?=
 =?UTF-8?q?=EC=8B=9C=20transaction=20=EC=A0=81=EC=9A=A9=20(#5)?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 services/scheduleService.js | 19 ++++++++++++++++---
 1 file changed, 16 insertions(+), 3 deletions(-)

diff --git a/services/scheduleService.js b/services/scheduleService.js
index 9b6f6b8..7fd1707 100644
--- a/services/scheduleService.js
+++ b/services/scheduleService.js
@@ -19,6 +19,8 @@ class schedulService {
      * �ъ슜�� �ㅼ�以� �앹꽦
      */
     async createSchedule({ userId, title, start_time, end_time, is_fixed }) {
+        const transaction = await Schedule.sequelize.transaction();
+
         try {
 
             // �쇱젙 �쒖옉 �쒓컙 - 醫낅즺 �쒓컙 �좏슚�� 寃���
@@ -41,9 +43,11 @@ class schedulService {
                 expiry_date: is_fixed ? null : this.getNextMonday()
             };
 
-            const schedule = await Schedule.create(scheduleData);
+            const schedule = await Schedule.create(scheduleData, { transaction });
+            await transaction.commit();
             return schedule;
         } catch (error) {
+            await transaction.rollback();
             throw new Error(`Failed to create schedule: ${error.message}`);
         }
     }
@@ -52,6 +56,8 @@ class schedulService {
      * �ъ슜�� �ㅼ�以� �섏젙
      */
     async updateSchedule(id, userId, updateData) {
+        const transaction = await Schedule.sequelize.transaction();
+
         try {
             const schedule = await Schedule.findOne({
                 where: { id, user_id: userId }
@@ -75,9 +81,11 @@ class schedulService {
             // �ㅼ�以� ���� 蹂�寃쏀븯吏� 紐삵븯�꾨줉 update媛� ��젣 -> 湲곗〈媛� �좎�
             delete updateData.is_fixed;
             
-            await schedule.update(updateData);
+            await schedule.update(updateData, { transaction });
+            await transaction.commit();
             return schedule;
         } catch (error) {
+            await transaction.rollback();
             throw new Error(`Failed to update schedule: ${error.message}`);
         }
     }
@@ -86,17 +94,22 @@ class schedulService {
      * �ъ슜�� �ㅼ�以� ��젣
      */
     async deleteSchedule(id, userId) {
+        const transaction = await Schedule.sequelize.transaction();
+
         try {
             const schedule = await Schedule.destroy({
-                where: { id, user_id: userId }
+                where: { id, user_id: userId },
+                transaction
             });
 
             if (!schedule) {
                 throw new Error('Schedule not found');
             }
 
+            await transaction.commit();
             return true;
         } catch (error) {
+            await transaction.rollback();
             throw new Error(`Failed to delete schedule: ${error.message}`);
         }
     }
-- 
GitLab