Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
W
WebBack
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
websystem
WebBack
Commits
ed7f0b53
Commit
ed7f0b53
authored
4 months ago
by
조대희
Browse files
Options
Downloads
Patches
Plain Diff
refactor: 업데이트 병렬 처리 및 벌크 처리 (
#23
)
parent
7d66ec50
No related branches found
No related tags found
2 merge requests
!42
[#25] 배포코드 master브랜치로 이동
,
!36
[#23] 스케줄 로직 리팩토링
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
services/scheduleService.js
+62
-42
62 additions, 42 deletions
services/scheduleService.js
with
62 additions
and
42 deletions
services/scheduleService.js
+
62
−
42
View file @
ed7f0b53
...
...
@@ -67,80 +67,100 @@ class ScheduleService {
async
updateSchedules
(
userId
,
updates
,
transaction
=
null
)
{
const
{
originalTitle
,
title
,
is_fixed
,
time_indices
}
=
updates
;
// 기존 스케줄 조회
const
existingSchedules
=
await
Schedule
.
findAll
({
where
:
{
user_id
:
userId
,
title
:
originalTitle
},
transaction
});
if
(
existingSchedules
.
length
===
0
)
{
throw
new
Error
(
'
Schedule not found
'
);
}
const
existingTimeIndices
=
existingSchedules
.
map
(
s
=>
s
.
time_idx
);
// 기존 시간대
const
toDelete
=
existingTimeIndices
.
filter
(
idx
=>
!
time_indices
.
includes
(
idx
));
// 삭제할 시간대
const
toAdd
=
time_indices
.
filter
(
idx
=>
!
existingTimeIndices
.
includes
(
idx
));
// 추가할 시간대
const
t
=
transaction
||
await
sequelize
.
transaction
();
try
{
//
삭제
if
(
toDelete
.
length
>
0
)
{
await
Schedule
.
destroy
({
//
기존 스케줄 조회
const
[
existingSchedule
,
existingSchedules
]
=
await
Promise
.
all
([
Schedule
.
findOne
({
where
:
{
user_id
:
userId
,
title
:
originalTitle
,
time_idx
:
{
[
Op
.
in
]:
toDelete
}
title
:
originalTitle
},
transaction
:
t
});
}
// 제목, 고정/유동 업데이트
await
Schedule
.
update
(
{
title
,
is_fixed
},
{
}),
Schedule
.
findAll
({
attributes
:
[
'
time_idx
'
],
where
:
{
user_id
:
userId
,
title
:
originalTitle
},
transaction
:
t
}
})
]);
if
(
!
existingSchedule
)
{
throw
new
Error
(
'
Schedule not found
'
);
}
const
existingTimeIndices
=
existingSchedules
.
map
(
s
=>
s
.
time_idx
);
const
toDelete
=
existingTimeIndices
.
filter
(
idx
=>
!
time_indices
.
includes
(
idx
));
const
toAdd
=
time_indices
.
filter
(
idx
=>
!
existingTimeIndices
.
includes
(
idx
));
// 벌크 연산
const
operations
=
[];
// 삭제 연산
if
(
toDelete
.
length
>
0
)
{
operations
.
push
(
Schedule
.
destroy
({
where
:
{
user_id
:
userId
,
title
:
originalTitle
,
time_idx
:
{
[
Op
.
in
]:
toDelete
}
},
transaction
:
t
})
);
}
// 업데이트 연산
operations
.
push
(
Schedule
.
update
(
{
title
,
is_fixed
},
{
where
:
{
user_id
:
userId
,
title
:
originalTitle
},
transaction
:
t
}
)
);
//
새로운 time_indices 추가
//
생성 연산
if
(
toAdd
.
length
>
0
)
{
await
Promise
.
all
(
toAdd
.
map
(
time_idx
=>
Schedule
.
create
({
operations
.
push
(
Schedule
.
bulkCreate
(
toAdd
.
map
(
time_idx
=>
({
user_id
:
userId
,
title
,
time_idx
,
is_fixed
},
{
transaction
:
t
})
})),
{
transaction
:
t
,
validate
:
true
}
)
);
}
await
Promise
.
all
(
operations
);
// 병렬 처리
if
(
!
transaction
)
{
await
t
.
commit
();
}
return
{
id
:
existingSchedule
s
[
0
]
.
id
,
id
:
existingSchedule
.
id
,
user_id
:
userId
,
title
,
is_fixed
,
time_indices
,
createdAt
:
existingSchedule
s
[
0
]
.
createdAt
,
createdAt
:
existingSchedule
.
createdAt
,
updatedAt
:
new
Date
()
};
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment