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

feat: 내 스케줄 저장하기 API 구현

parent 79cbafa6
No related branches found
No related tags found
No related merge requests found
...@@ -10,6 +10,7 @@ dotenv.config(); ...@@ -10,6 +10,7 @@ dotenv.config();
const meetingRouter = require('./routes/meeting'); const meetingRouter = require('./routes/meeting');
const participantRouter = require('./routes/participant'); const participantRouter = require('./routes/participant');
const myScheduleRouter = require('./routes/mySchedule');
const { sequelize } = require('./models'); const { sequelize } = require('./models');
const app = express(); const app = express();
...@@ -50,6 +51,7 @@ app.use( ...@@ -50,6 +51,7 @@ app.use(
app.use('/meetings', meetingRouter); app.use('/meetings', meetingRouter);
app.use('/meetings/:meetingId/participants', participantRouter); app.use('/meetings/:meetingId/participants', participantRouter);
app.use('/meetings/:meetingId/my/schedules', myScheduleRouter);
app.use((req, res, next) => { app.use((req, res, next) => {
const error = new Error(`There is no router. ${req.method} ${req.url}`); const error = new Error(`There is no router. ${req.method} ${req.url}`);
......
const { Schedule, sequelize } = require('../models');
const { getLoggedInParticipantId } = require('../middlewares/auth');
const SchedulesResponse = require('../dto/response/schedulesResponse');
const { createScheduleAlreadyExistError } = require('../errors/scheduleErrors');
async function validateScheduleNotExist(participantId) {
const numOfSchedules = await Schedule.count({
where: {
ParticipantId: participantId,
},
});
if (numOfSchedules > 0) {
throw createScheduleAlreadyExistError();
}
}
exports.createMySchedules = async (req, res, next) => {
const participantId = getLoggedInParticipantId(req, res, next);
const { availableSchedules } = 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));
} catch (error) {
return next(error);
}
};
const ScheduleResponse = require('./scheduleResponse');
class SchedulesResponse {
constructor(schedules) {
this.schedules = schedules;
}
static from(schedules) {
return new SchedulesResponse(
schedules.map((schedule) => ScheduleResponse.from(schedule)),
);
}
}
module.exports = SchedulesResponse;
exports.createScheduleAlreadyExistError = () => {
const error = new Error(
'기존에 저장한 스케줄이 존재합니다. 기존 스케줄 데이터를 삭제한 후 다시 시도하거나, 수정하기 API를 사용해주세요.',
);
error.status = 409;
return error;
};
exports.isAuthenticated = (req, res, next) => { function parseParticipantData(req, res, next) {
let participantData = null; let participantData = null;
if (req.signedCookies.participantData) { if (req.signedCookies.participantData) {
participantData = JSON.parse(req.signedCookies.participantData); participantData = JSON.parse(req.signedCookies.participantData);
} }
if (!participantData || participantData.meetingId !== req.params.meetingId) { if (!participantData) {
const error = new Error('인증 권한이 없습니다.');
error.status = 401;
return next(error);
}
return participantData;
}
exports.isAuthenticated = (req, res, next) => {
const participantData = parseParticipantData(req);
if (participantData.meetingId !== req.params.meetingId) {
const error = new Error('접근 권한이 없습니다.'); const error = new Error('접근 권한이 없습니다.');
error.status = 401; error.status = 401;
next(error); next(error);
...@@ -11,3 +21,8 @@ exports.isAuthenticated = (req, res, next) => { ...@@ -11,3 +21,8 @@ exports.isAuthenticated = (req, res, next) => {
} }
next(); next();
}; };
exports.getLoggedInParticipantId = (req, res, next) => {
const participantData = parseParticipantData(req, res, next);
return participantData?.participantId;
};
const express = require('express'); const express = require('express');
const { isAuthenticated } = require('../middlewares/index'); const { isAuthenticated } = require('../middlewares/auth');
const { const {
createMeeting, createMeeting,
entry, entry,
......
const express = require('express');
const { isAuthenticated } = require('../middlewares/auth');
const { createMySchedules } = require('../controllers/schedule');
const router = express.Router({ mergeParams: true });
router.post('/bulk', isAuthenticated, createMySchedules);
module.exports = router;
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment