From 7356196eb1559451c53ab36362cec47a25025db6 Mon Sep 17 00:00:00 2001
From: Wo-ogie <siwall0105@gmail.com>
Date: Sun, 10 Dec 2023 00:28:26 +0900
Subject: [PATCH] =?UTF-8?q?feat:=20=EC=B0=B8=EA=B0=80=EC=9E=90=20=EC=83=9D?=
 =?UTF-8?q?=EC=84=B1=20=EC=8B=9C=20=EC=95=BD=EC=86=8D=20=EC=B5=9C=EB=8C=80?=
 =?UTF-8?q?=20=EC=B0=B8=EA=B0=80=EC=9E=90=20=EC=88=98=EB=A5=BC=20=EB=84=98?=
 =?UTF-8?q?=EC=96=B4=EC=84=A0=EB=8B=A4=EB=A9=B4=20=EC=97=90=EB=9F=AC?=
 =?UTF-8?q?=EA=B0=80=20=EB=B0=9C=EC=83=9D=ED=95=98=EB=8F=84=EB=A1=9D=20?=
 =?UTF-8?q?=EC=98=88=EC=99=B8=EC=B2=98=EB=A6=AC=20=EC=B6=94=EA=B0=80?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 controllers/meeting.js     |  9 +--------
 controllers/participant.js | 16 +++++++++++++++-
 errors/meetingErrors.js    |  8 ++++++++
 services/meeting.js        |  8 ++++++++
 4 files changed, 32 insertions(+), 9 deletions(-)

diff --git a/controllers/meeting.js b/controllers/meeting.js
index 99d1f50..d897d04 100644
--- a/controllers/meeting.js
+++ b/controllers/meeting.js
@@ -5,6 +5,7 @@ const {
   getMeetingById,
   getMeetingWithParticipantsById,
   getMeetingWithParticipantsAndSchedulesById,
+  getNumOfParticipantsByMeetingId,
   closeMeetingById,
 } = require('../services/meeting');
 const {
@@ -44,14 +45,6 @@ async function getParticipantByNameAndMeetingId(name, meetingId) {
   return participant;
 }
 
-async function getNumOfParticipantsByMeetingId(meetingId) {
-  return Participant.count({
-    where: {
-      MeetingId: meetingId,
-    },
-  });
-}
-
 async function validatePasswordIsMatched(requestPassword, exPassword) {
   if (!requestPassword) {
     throw createPasswordIsNullError();
diff --git a/controllers/participant.js b/controllers/participant.js
index 581e3e3..902bfb9 100644
--- a/controllers/participant.js
+++ b/controllers/participant.js
@@ -1,5 +1,12 @@
 const bcrypt = require('bcrypt');
-const { createPasswordIsNullError } = require('../errors/meetingErrors');
+const {
+  getMeetingById,
+  getNumOfParticipantsByMeetingId,
+} = require('../services/meeting');
+const {
+  createPasswordIsNullError,
+  createMaxParticipantsError,
+} = require('../errors/meetingErrors');
 const {
   createParticipantIsAlreadyExistError,
   createParticipantNotFoundError,
@@ -65,6 +72,13 @@ exports.createParticipant = async (req, res, next) => {
   const reqPassword = req.body.password || null;
   const reqEmail = req.body.email || null;
   try {
+    const meeting = await getMeetingById(meetingId);
+    const currentParticipants =
+      await getNumOfParticipantsByMeetingId(meetingId);
+    if (currentParticipants >= meeting.maxParticipants) {
+      throw createMaxParticipantsError();
+    }
+
     const existingParticipant = await findParticipantByMeetingIdAndName(
       meetingId,
       reqName,
diff --git a/errors/meetingErrors.js b/errors/meetingErrors.js
index b52602f..5e6882e 100644
--- a/errors/meetingErrors.js
+++ b/errors/meetingErrors.js
@@ -29,3 +29,11 @@ exports.createMostConfirmedTimeNotFoundError = () => {
   error.status = 404;
   return error;
 };
+
+exports.createMaxParticipantsError = () => {
+  const error = new Error(
+    '설정된 최대 참가자가 모두 참여했습니다. 더 이상 새로 참가하실 수 없습니다.',
+  );
+  error.status = 409;
+  return error;
+};
diff --git a/services/meeting.js b/services/meeting.js
index 6bfea25..cb76ec2 100644
--- a/services/meeting.js
+++ b/services/meeting.js
@@ -50,6 +50,13 @@ const getMeetingWithParticipantsAndSchedulesById = async (meetingId) => {
   return meeting;
 };
 
+const getNumOfParticipantsByMeetingId = async (meetingId) =>
+  Participant.count({
+    where: {
+      MeetingId: meetingId,
+    },
+  });
+
 function validateMeetingIsNotClosed(meeting) {
   if (meeting.isClosed === true) {
     throw createMeetingIsAlreadyClosedError();
@@ -75,5 +82,6 @@ module.exports = {
   getMeetingById,
   getMeetingWithParticipantsById,
   getMeetingWithParticipantsAndSchedulesById,
+  getNumOfParticipantsByMeetingId,
   closeMeetingById,
 };
-- 
GitLab