Select Git revision
friendrelation.test.js
scheduleService.js 6.39 KiB
const { Op } = require('sequelize');
const Schedule = require('../models/Schedule');
class scheduleService {
/**
* 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');
}
}
/**
* 유동 스케줄 만료일 구하기
*/
getNextMonday() {
const date = new Date();
const day = date.getDay();
const daysUntilNextMonday = (8 - day) % 7;
date.setDate(date.getDate() + daysUntilNextMonday);
date.setHours(0, 0, 0, 0);
return date;
}
/**
* 사용자 스케줄 생성
*/
async createSchedule({ userId, title, start_time, end_time, is_fixed }) {
return this.withTransaction(async (transaction) => {