Skip to content
Snippets Groups Projects
Commit ed7f0b53 authored by 조대희's avatar 조대희
Browse files

refactor: 업데이트 병렬 처리 및 벌크 처리 (#23)

parent 7d66ec50
Branches
No related tags found
2 merge requests!42[#25] 배포코드 master브랜치로 이동,!36[#23] 스케줄 로직 리팩토링
...@@ -67,29 +67,43 @@ class ScheduleService { ...@@ -67,29 +67,43 @@ class ScheduleService {
async updateSchedules(userId, updates, transaction = null) { async updateSchedules(userId, updates, transaction = null) {
const { originalTitle, title, is_fixed, time_indices } = updates; const { originalTitle, title, is_fixed, time_indices } = updates;
const t = transaction || await sequelize.transaction();
try {
// 기존 스케줄 조회 // 기존 스케줄 조회
const existingSchedules = await Schedule.findAll({ const [existingSchedule, existingSchedules] = await Promise.all([
Schedule.findOne({
where: { where: {
user_id: userId, user_id: userId,
title: originalTitle title: originalTitle
}, },
transaction transaction: t
}); }),
Schedule.findAll({
attributes: ['time_idx'],
where: {
user_id: userId,
title: originalTitle
},
transaction: t
})
]);
if (existingSchedules.length === 0) { if (!existingSchedule) {
throw new Error('Schedule not found'); throw new Error('Schedule not found');
} }
const existingTimeIndices = existingSchedules.map(s => s.time_idx); // 기존 시간대 const existingTimeIndices = existingSchedules.map(s => s.time_idx);
const toDelete = existingTimeIndices.filter(idx => !time_indices.includes(idx)); // 삭제할 시간대 const toDelete = existingTimeIndices.filter(idx => !time_indices.includes(idx));
const toAdd = time_indices.filter(idx => !existingTimeIndices.includes(idx)); // 추가할 시간대 const toAdd = time_indices.filter(idx => !existingTimeIndices.includes(idx));
const t = transaction || await sequelize.transaction();
try { // 벌크 연산
// 삭제 const operations = [];
// 삭제 연산
if (toDelete.length > 0) { if (toDelete.length > 0) {
await Schedule.destroy({ operations.push(
Schedule.destroy({
where: { where: {
user_id: userId, user_id: userId,
title: originalTitle, title: originalTitle,
...@@ -98,15 +112,14 @@ class ScheduleService { ...@@ -98,15 +112,14 @@ class ScheduleService {
} }
}, },
transaction: t transaction: t
}); })
);
} }
// 제목, 고정/유동 업데이트 // 업데이트 연산
await Schedule.update( operations.push(
{ Schedule.update(
title, { title, is_fixed },
is_fixed
},
{ {
where: { where: {
user_id: userId, user_id: userId,
...@@ -114,33 +127,40 @@ class ScheduleService { ...@@ -114,33 +127,40 @@ class ScheduleService {
}, },
transaction: t transaction: t
} }
)
); );
// 새로운 time_indices 추가 // 생성 연산
if (toAdd.length > 0) { if (toAdd.length > 0) {
await Promise.all( operations.push(
toAdd.map(time_idx => Schedule.bulkCreate(
Schedule.create({ toAdd.map(time_idx => ({
user_id: userId, user_id: userId,
title, title,
time_idx, time_idx,
is_fixed is_fixed
}, { transaction: t }) })),
{
transaction: t,
validate: true
}
) )
); );
} }
await Promise.all(operations); // 병렬 처리
if (!transaction) { if (!transaction) {
await t.commit(); await t.commit();
} }
return { return {
id: existingSchedules[0].id, id: existingSchedule.id,
user_id: userId, user_id: userId,
title, title,
is_fixed, is_fixed,
time_indices, time_indices,
createdAt: existingSchedules[0].createdAt, createdAt: existingSchedule.createdAt,
updatedAt: new Date() updatedAt: new Date()
}; };
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment