diff --git a/controllers/schedule.js b/controllers/schedule.js index 2bea511af3362c75482078aaa41d8a1c6272cde7..5f1886cd98d9f7c1d2593ff49bff6a4b6744c844 100644 --- a/controllers/schedule.js +++ b/controllers/schedule.js @@ -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); + } +}; diff --git a/routes/mySchedule.js b/routes/mySchedule.js index 6e6291dbea08f6a70a51f27ad42dbe448f75e29f..4b3aa52788dfa009c2e7f1a7885711be1e616bc0 100644 --- a/routes/mySchedule.js +++ b/routes/mySchedule.js @@ -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;