This commit is contained in:
2025-05-09 00:54:10 +09:00
parent 744a0db3bd
commit 486ae6de6b
15 changed files with 295 additions and 248 deletions
+65 -42
View File
@@ -1260,14 +1260,30 @@ exports.getEventParticipants = async (req, res) => {
}
};
// 이벤트 참가자 등록
// 참가자 정보 핵심 update 함수 (중복 제거)
async function updateEventParticipantCore({ id, eventId, memberId, clubId, status, paymentStatus, comment }) {
const [updatedCount] = await EventParticipant.update(
{ status, paymentStatus, comment },
{
where: {
id,
eventId,
memberId
}
}
);
return updatedCount;
}
// 이벤트 참가자 등록 (이미 있으면 update, 없으면 create)
exports.addEventParticipant = async (req, res) => {
try {
const {
clubId,
memberId,
status,
paymentStatus
paymentStatus,
comment
} = req.body;
const eventId = req.params.id;
if (!eventId || !clubId || !memberId) {
@@ -1311,51 +1327,58 @@ exports.addEventParticipant = async (req, res) => {
}
});
let participant;
let isUpdate = false;
if (existingParticipant) {
return res.status(400).json({ message: '이미 참가 신청한 사용자입니다.' });
}
// 참가자 수 제한 확인
if (event.maxParticipants > 0) {
const currentParticipants = await EventParticipant.count({
where: {
eventId,
status: { [Op.ne]: '취소' }
}
// 이미 있으면 update (핵심 update 함수 활용)
await updateEventParticipantCore({
id: existingParticipant.id,
eventId,
memberId,
clubId,
status: status || existingParticipant.status,
paymentStatus: paymentStatus || existingParticipant.paymentStatus,
comment: comment || existingParticipant.comment
});
participant = await EventParticipant.findOne({
where: { id: existingParticipant.id },
include: [
{
model: Member,
attributes: ['id', 'name']
}
]
});
isUpdate = true;
} else {
// 없으면 create
participant = await EventParticipant.create({
eventId,
memberId,
status: status || '참가예정',
paymentStatus: paymentStatus || '미납',
comment: comment || null,
createdAt: new Date(),
updatedAt: new Date()
});
participant = await EventParticipant.findOne({
where: { id: participant.id },
include: [
{
model: Member,
attributes: ['id', 'name']
}
]
});
if (currentParticipants >= event.maxParticipants) {
return res.status(400).json({ message: '참가자 수가 초과되었습니다.' });
}
}
const participant = await EventParticipant.create({
eventId,
memberId,
status: status || '참가예정',
paymentStatus: paymentStatus || '미납',
createdAt: new Date(),
updatedAt: new Date()
});
// 참가자 정보 조회
const participantWithDetails = await EventParticipant.findOne({
where: { id: participant.id },
include: [
{
model: Member,
attributes: ['id', 'name']
}
]
});
res.status(201).json({
message: '참가자가 등록되었습니다.',
participant: participantWithDetails
message: isUpdate ? '기존 참가자 정보를 수정했습니다.' : '참가자가 등록되었습니다.',
participant
});
} catch (error) {
console.error('참가자 등록 오류:', error);
res.status(500).json({ message: '참가자 등록 중 오류가 발생했습니다.' });
console.error('참가자 등록/수정 오류:', error);
res.status(500).json({ message: '참가자 등록/수정 중 오류가 발생했습니다.' });
}
};
@@ -1363,7 +1386,7 @@ exports.addEventParticipant = async (req, res) => {
exports.updateEventParticipant = async (req, res) => {
try {
const eventId = req.params.id;
const { id, clubId, memberId, status, paymentStatus } = req.body;
const { id, clubId, memberId, status, paymentStatus, comment } = req.body;
if (!eventId || !clubId || !memberId || !id) {
return res.status(400).json({ message: '이벤트 ID, 클럽 ID, 사용자 ID가 필요합니다.' });
@@ -1384,7 +1407,7 @@ exports.updateEventParticipant = async (req, res) => {
// 참가자 정보 수정
const [updatedCount] = await EventParticipant.update(
{ status, paymentStatus },
{ status, paymentStatus, comment },
{
where: {
id,