Skip to content
Snippets Groups Projects
Commit 96c3b22c authored by Wo-ogie's avatar Wo-ogie
Browse files

feat: 내 스케줄 수정하기 API 구현

parent 6639ee45
No related branches found
No related tags found
No related merge requests found
......@@ -3,6 +3,25 @@ const { getLoggedInParticipantId } = require('../middlewares/auth');
const SchedulesResponse = require('../dto/response/schedulesResponse');
const { createScheduleAlreadyExistError } = require('../errors/scheduleErrors');
async function createSchedules(participantId, schedules) {
console.log('participantId', participantId);
console.log('schedules', schedules);
return sequelize.transaction(async (transaction) =>
Promise.all(
schedules.map(async (availableSchedule) =>
Schedule.create(
{
availableDate: availableSchedule.availableDate,
availableTimes: availableSchedule.availableTimes,
ParticipantId: participantId,
},
{ transaction },
),
),
),
);
}
async function validateScheduleNotExist(participantId) {
const numOfSchedules = await Schedule.count({
where: {
......@@ -16,25 +35,11 @@ async function validateScheduleNotExist(participantId) {
exports.createMySchedules = async (req, res, next) => {
const participantId = getLoggedInParticipantId(req, res, next);
const { availableSchedules } = req.body;
const { schedules } = req.body;
try {
await validateScheduleNotExist(participantId);
const schedules = await sequelize.transaction(async (transaction) =>
Promise.all(
availableSchedules.map((availableSchedule) =>
Schedule.create(
{
availableDate: availableSchedule.availableDate,
availableTimes: availableSchedule.availableTimes,
ParticipantId: participantId,
},
{ transaction },
),
),
),
);
return res.json(SchedulesResponse.from(schedules));
const createdSchedules = createSchedules(participantId, schedules);
return res.json(SchedulesResponse.from(createdSchedules));
} catch (error) {
return next(error);
}
......@@ -53,3 +58,23 @@ exports.getMySchedules = async (req, res, next) => {
return next(error);
}
};
async function deleteAllByParticipantId(participantId) {
await Schedule.destroy({
where: {
ParticipantId: participantId,
},
});
}
exports.updateMySchedules = async (req, res, next) => {
const participantId = getLoggedInParticipantId(req, res, next);
const { schedules } = req.body;
try {
await deleteAllByParticipantId(participantId);
const createdSchedules = await createSchedules(participantId, schedules);
return res.json(SchedulesResponse.from(createdSchedules));
} catch (error) {
return next(error);
}
};
......@@ -3,6 +3,7 @@ const { isAuthenticated } = require('../middlewares/auth');
const {
createMySchedules,
getMySchedules,
updateMySchedules,
} = require('../controllers/schedule');
const router = express.Router({ mergeParams: true });
......@@ -11,4 +12,6 @@ router.post('/bulk', isAuthenticated, createMySchedules);
router.get('/', isAuthenticated, getMySchedules);
router.put('/bulk', isAuthenticated, updateMySchedules);
module.exports = router;
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment