diff --git a/controllers/participant.js b/controllers/participant.js index 55929a2b4305e592e9a6e732eb8211da9166f122..a60d093e554e233fb9575c21142831fdb4b04d3d 100644 --- a/controllers/participant.js +++ b/controllers/participant.js @@ -1,10 +1,31 @@ const bcrypt = require('bcrypt'); -const { Participant } = require('../models'); const { createPasswordIsNullError } = require('../errors/meetingErrors'); +const { + createParticipantIsAlreadyExistError, +} = require('../errors/participantErrors'); const ParticipantResponse = require('../dto/response/participantResponse'); +const { Participant } = require('../models'); 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) { if (!password) { return next(createPasswordIsNullError()); @@ -18,24 +39,17 @@ async function encryptPassword(password, next) { } exports.createParticipant = async (req, res, next) => { - console.log(req.params); const { meetingId } = req.params; const reqName = req.body.name; const reqPassword = req.body.password || null; const reqEmail = req.body.email || null; - console.log(meetingId); - console.log(reqName); - console.log(reqPassword); - console.log(reqEmail); try { - const existingParticipant = await Participant.findOne({ - where: { - MeetingId: meetingId, - name: reqName, - }, - }); + const existingParticipant = await findParticipantByMeetingIdAndName( + meetingId, + reqName, + ); if (existingParticipant) { - throw new Error(); + throw createParticipantIsAlreadyExistError; } let passwordEncrypted = null; @@ -43,14 +57,28 @@ exports.createParticipant = async (req, res, next) => { passwordEncrypted = await encryptPassword(reqPassword, next); } - const participantCreated = await Participant.create({ - name: reqName, - password: passwordEncrypted, - email: reqEmail, - MeetingId: meetingId, - }); + const participantCreated = await createParticipant( + reqName, + passwordEncrypted, + reqEmail, + meetingId, + ); return res.status(201).json(ParticipantResponse.from(participantCreated)); } catch (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); + } +}; diff --git a/errors/participantErrors.js b/errors/participantErrors.js new file mode 100644 index 0000000000000000000000000000000000000000..f02070d5003b6411e9cc136d0840d92a85d81763 --- /dev/null +++ b/errors/participantErrors.js @@ -0,0 +1,7 @@ +exports.createParticipantIsAlreadyExistError = () => { + const error = new Error( + '이미 존재하는 참가자입니다. 같은 이름의 참가자를 중복 생성할 수 없습니다.', + ); + error.status = 409; + return error; +}; diff --git a/routes/participant.js b/routes/participant.js index 6d8787e41849c6858104eb5bc63f2a2984f8171f..9f68d11bb2e542aac4bf52e75410285aa3abda8a 100644 --- a/routes/participant.js +++ b/routes/participant.js @@ -1,8 +1,13 @@ const express = require('express'); -const { createParticipant } = require('../controllers/participant'); +const { + createParticipant, + getParticipantExistence, +} = require('../controllers/participant'); const router = express.Router(); router.post('/:meetingId/participants', createParticipant); +router.get('/:meetingId/participants/existence', getParticipantExistence); + module.exports = router;