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

feat: 참가자 존재 여부 확인하기 API 구현

parent 3a5e03ab
No related branches found
No related tags found
No related merge requests found
const bcrypt = require('bcrypt'); const bcrypt = require('bcrypt');
const { Participant } = require('../models');
const { createPasswordIsNullError } = require('../errors/meetingErrors'); const { createPasswordIsNullError } = require('../errors/meetingErrors');
const {
createParticipantIsAlreadyExistError,
} = require('../errors/participantErrors');
const ParticipantResponse = require('../dto/response/participantResponse'); const ParticipantResponse = require('../dto/response/participantResponse');
const { Participant } = require('../models');
const HASHING_ROUND = 12; const HASHING_ROUND = 12;
async function createParticipant(name, password, email, meetingId) {
return Participant.create({
name,
password,
email,
MeetingId: meetingId,
});
}
async function findParticipantByMeetingIdAndName(meetingId, name) {
return Participant.findOne({
where: {
MeetingId: meetingId,
name,
},
});
}
async function encryptPassword(password, next) { async function encryptPassword(password, next) {
if (!password) { if (!password) {
return next(createPasswordIsNullError()); return next(createPasswordIsNullError());
...@@ -18,24 +39,17 @@ async function encryptPassword(password, next) { ...@@ -18,24 +39,17 @@ async function encryptPassword(password, next) {
} }
exports.createParticipant = async (req, res, next) => { exports.createParticipant = async (req, res, next) => {
console.log(req.params);
const { meetingId } = req.params; const { meetingId } = req.params;
const reqName = req.body.name; const reqName = req.body.name;
const reqPassword = req.body.password || null; const reqPassword = req.body.password || null;
const reqEmail = req.body.email || null; const reqEmail = req.body.email || null;
console.log(meetingId);
console.log(reqName);
console.log(reqPassword);
console.log(reqEmail);
try { try {
const existingParticipant = await Participant.findOne({ const existingParticipant = await findParticipantByMeetingIdAndName(
where: { meetingId,
MeetingId: meetingId, reqName,
name: reqName, );
},
});
if (existingParticipant) { if (existingParticipant) {
throw new Error(); throw createParticipantIsAlreadyExistError;
} }
let passwordEncrypted = null; let passwordEncrypted = null;
...@@ -43,14 +57,28 @@ exports.createParticipant = async (req, res, next) => { ...@@ -43,14 +57,28 @@ exports.createParticipant = async (req, res, next) => {
passwordEncrypted = await encryptPassword(reqPassword, next); passwordEncrypted = await encryptPassword(reqPassword, next);
} }
const participantCreated = await Participant.create({ const participantCreated = await createParticipant(
name: reqName, reqName,
password: passwordEncrypted, passwordEncrypted,
email: reqEmail, reqEmail,
MeetingId: meetingId, meetingId,
}); );
return res.status(201).json(ParticipantResponse.from(participantCreated)); return res.status(201).json(ParticipantResponse.from(participantCreated));
} catch (error) { } catch (error) {
return next(error); return next(error);
} }
}; };
exports.getParticipantExistence = async (req, res, next) => {
try {
const participant = await findParticipantByMeetingIdAndName(
req.params.meetingId,
req.query.name,
);
return res.json({
exist: !!participant,
});
} catch (error) {
return next(error);
}
};
exports.createParticipantIsAlreadyExistError = () => {
const error = new Error(
'이미 존재하는 참가자입니다. 같은 이름의 참가자를 중복 생성할 수 없습니다.',
);
error.status = 409;
return error;
};
const express = require('express'); const express = require('express');
const { createParticipant } = require('../controllers/participant'); const {
createParticipant,
getParticipantExistence,
} = require('../controllers/participant');
const router = express.Router(); const router = express.Router();
router.post('/:meetingId/participants', createParticipant); router.post('/:meetingId/participants', createParticipant);
router.get('/:meetingId/participants/existence', getParticipantExistence);
module.exports = router; module.exports = router;
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment