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
58234d3c
Commit
58234d3c
authored
4 months ago
by
조대희
Browse files
Options
Downloads
Patches
Plain Diff
refactor: 스케줄 서비스 중복 코드 함수화 (
#5
)
parent
387f65ca
No related branches found
Branches containing commit
No related tags found
2 merge requests
!31
Develop
,
!6
[#5] Schedule 서비스 로직 개발
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
services/scheduleService.js
+71
-72
71 additions, 72 deletions
services/scheduleService.js
with
71 additions
and
72 deletions
services/scheduleService.js
+
71
−
72
View file @
58234d3c
...
...
@@ -3,6 +3,54 @@ const Schedule = require('../models/Schedule');
class
schedulService
{
/**
* transactin wrapper 함수
*/
async
withTransaction
(
callback
)
{
const
transaction
=
await
Schedule
.
sequelize
.
transaction
();
try
{
const
result
=
await
callback
(
transaction
);
await
transaction
.
commit
();
return
result
;
}
catch
(
error
)
{
await
transaction
.
rollback
();
throw
error
;
}
}
/**
* 공통 where 절 생성
*/
getScheduleWhereClause
(
userId
,
id
=
null
)
{
const
where
=
{
user_id
:
userId
,
[
Op
.
or
]:
[
{
is_fixed
:
true
},
{
is_fixed
:
false
,
expiry_date
:
{
[
Op
.
gt
]:
new
Date
()
}
}
]
};
if
(
id
)
{
where
.
id
=
id
;
}
return
where
;
}
/**
* 스케줄 유효성 검사
*/
validateScheduleTime
(
start_time
,
end_time
)
{
if
(
new
Date
(
start_time
)
>=
new
Date
(
end_time
))
{
throw
new
Error
(
'
Start time must be before end time
'
);
}
}
/**
* 유동 스케줄 만료일 구하기
*/
...
...
@@ -19,16 +67,9 @@ class schedulService {
* 사용자 스케줄 생성
*/
async
createSchedule
({
userId
,
title
,
start_time
,
end_time
,
is_fixed
})
{
const
transaction
=
await
Schedule
.
sequelize
.
transaction
();
return
this
.
withTransaction
(
async
(
transaction
)
=>
{
this
.
validateScheduleTime
(
start_time
,
end_time
);
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
'
);
...
...
@@ -43,75 +84,57 @@ class schedulService {
expiry_date
:
is_fixed
?
null
:
this
.
getNextMonday
()
};
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
}
`
);
}
return
Schedule
.
create
(
scheduleData
,
{
transaction
});
});
}
/**
* 사용자 스케줄 수정
*/
async
updateSchedule
(
id
,
userId
,
updateData
)
{
const
transaction
=
await
Schedule
.
sequelize
.
transaction
();
try
{
return
this
.
withTransaction
(
async
(
transaction
)
=>
{
const
schedule
=
await
Schedule
.
findOne
({
where
:
{
id
,
user_id
:
userId
}
where
:
{
id
,
user_id
:
userId
},
transaction
});
if
(
!
schedule
)
{
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
'
);
}
this
.
validateScheduleTime
(
updateData
.
start_time
,
updateData
.
end_time
);
// 중복 검사
const
overlap
=
await
this
.
checkScheduleOverlap
(
userId
,
updateData
.
start_time
,
updateData
.
end_time
);
const
overlap
=
await
this
.
checkScheduleOverlap
(
userId
,
updateData
.
start_time
,
updateData
.
end_time
,
id
);
if
(
overlap
)
{
throw
new
Error
(
'
Schedule overlaps with existing schedule
'
);
}
// 스케줄 타입 변경하지 못하도록 update값 삭제 -> 기존값 유지
delete
updateData
.
is_fixed
;
await
schedule
.
update
(
updateData
,
{
transaction
});
await
transaction
.
commit
();
return
schedule
;
}
catch
(
error
)
{
await
transaction
.
rollback
();
throw
new
Error
(
`Failed to update schedule:
${
error
.
message
}
`
);
}
return
schedule
.
update
(
updateData
,
{
transaction
});
});
}
/**
* 사용자 스케줄 삭제
*/
async
deleteSchedule
(
id
,
userId
)
{
const
transaction
=
await
Schedule
.
sequelize
.
transaction
();
try
{
const
schedule
=
await
Schedule
.
destroy
({
return
this
.
withTransaction
(
async
(
transaction
)
=>
{
const
result
=
await
Schedule
.
destroy
({
where
:
{
id
,
user_id
:
userId
},
transaction
});
if
(
!
sched
ul
e
)
{
if
(
!
res
ul
t
)
{
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
}
`
);
}
});
}
/**
...
...
@@ -119,22 +142,10 @@ class schedulService {
*/
async
getAllSchedules
(
userId
)
{
try
{
const
schedules
=
await
Schedule
.
findAll
({
where
:
{
user_id
:
userId
,
[
Op
.
or
]:
[
{
is_fixed
:
true
},
{
is_fixed
:
false
,
expiry_date
:
{
[
Op
.
gt
]:
new
Date
()
}
}
]
},
return
Schedule
.
findAll
({
where
:
this
.
getScheduleWhereClause
(
userId
),
order
:
[[
'
start_time
'
,
'
ASC
'
]]
});
return
schedules
;
}
catch
(
error
)
{
throw
new
Error
(
`Failed to fetch schedules:
${
error
.
message
}
`
);
}
...
...
@@ -146,19 +157,7 @@ class schedulService {
async
getScheduleById
(
id
,
userId
)
{
try
{
const
schedule
=
await
Schedule
.
findOne
({
where
:
{
id
,
user_id
:
userId
,
[
Op
.
or
]:
[
{
is_fixed
:
true
},
{
is_fixed
:
false
,
expiry_date
:
{
[
Op
.
gt
]:
new
Date
()
}
}
]
}
where
:
this
.
getScheduleWhereClause
(
userId
,
id
)
});
if
(
!
schedule
)
{
...
...
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