From a7f956dfcf9cea482f59733a697ba037a1029c25 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=A1=B0=EB=8C=80=ED=9D=AC?= <joedaehui@ajou.ac.kr> Date: Sun, 8 Dec 2024 19:58:29 +0900 Subject: [PATCH] =?UTF-8?q?refactor:=20=EB=82=B4=20=EB=AF=B8=ED=8C=85/=20?= =?UTF-8?q?=EC=A0=84=EC=B2=B4=20=EB=AF=B8=ED=8C=85=20=EC=A1=B0=ED=9A=8C=20?= =?UTF-8?q?=EC=84=9C=EB=B9=84=EC=8A=A4=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- services/meetingService.js | 225 +++++++++++++++++++------------------ 1 file changed, 117 insertions(+), 108 deletions(-) diff --git a/services/meetingService.js b/services/meetingService.js index 746bcf1..dd1eaca 100644 --- a/services/meetingService.js +++ b/services/meetingService.js @@ -314,130 +314,139 @@ class MeetingService { async getMeetings(userId, pagination) { const { limit = 20, offset = 0 } = pagination; - - const meetings = await Meeting.findAll({ - attributes: [ - 'id', - 'title', - 'description', - 'time_idx_start', - 'time_idx_end', - 'location', - 'time_idx_deadline', - 'type', - 'max_num', - 'cur_num', - ], - include: [ - { - model: MeetingParticipant, - as: 'participants', - required: false, - attributes: [], - }, - { - model: User, - as: 'creator', - attributes: ['name'], - } - ], - order: [['createdAt', 'DESC']], - offset - }); - const hasNext = meetings.length > limit; - const content = await Promise.all( - meetings.slice(0, limit).map(async (meeting) => { - const isParticipant = await MeetingParticipant.findOne({ - where: { - meeting_id: meeting.id, - user_id: userId + try { + const meetings = await Meeting.findAll({ + attributes: [ + 'id', 'title', 'description', + 'time_idx_start', 'time_idx_end', + 'location', 'time_idx_deadline', + 'type', 'max_num', 'cur_num', + 'created_at' + ], + include: [ + { + model: MeetingParticipant, + as: 'participants', + required: false, + where: { user_id: userId }, + attributes: [] + }, + { + model: User, + as: 'creator', + attributes: ['name'], + required: false } - }); + ], + where: { + [Op.or]: [ + { created_by: userId }, + { '$participants.meeting_id$': { [Op.col]: 'Meeting.id' } }, + { '$participants.user_id$': userId } + ] + }, + subQuery: false, + order: [['created_at', 'DESC']], + limit: limit + 1, + offset, + distinct: true + }); - const hasConflict = await ScheduleService.checkScheduleOverlapByTime( - userId, - meeting.time_idx_start, - meeting.time_idx_end - ); + const hasNext = meetings.length > limit; + const content = await Promise.all( + meetings.slice(0, limit).map(async (meeting) => { + const isParticipant = true; + const hasConflict = await ScheduleService.checkScheduleOverlapByTime( + userId, + meeting.time_idx_start, + meeting.time_idx_end + ); - const creatorName = meeting.creator ? meeting.creator.name : 'Unknown'; - return new MeetingResponseDTO(meeting, !!isParticipant, hasConflict, creatorName); - }) - ); + return new MeetingResponseDTO( + meeting, + isParticipant, + hasConflict, + meeting.creator?.name || 'Unknown' + ); + }) + ); - return { - content, - hasNext - }; + return { content, hasNext }; + } catch (error) { + console.error('getMeetings error:', error); + throw new Error('Failed to fetch meetings'); + } } async getMyMeetings(userId, pagination) { const { limit = 20, offset = 0 } = pagination; - const meetings = await Meeting.findAll({ - attributes: [ - 'id', - 'title', - 'description', - 'time_idx_start', - 'time_idx_end', - 'location', - 'time_idx_deadline', - 'type', - 'max_num', - 'cur_num', - ], - include: [ - { - model: MeetingParticipant, - as: 'participants', - where: { user_id: userId }, - attributes: [], + try { + const meetings = await Meeting.findAll({ + attributes: [ + 'id', 'title', 'description', 'time_idx_start', + 'time_idx_end', 'location', 'time_idx_deadline', + 'type', 'max_num', 'cur_num', + ], + include: [ + { + model: MeetingParticipant, + as: 'participants', + required: false, + attributes: [] + }, + { + model: User, + as: 'creator', + attributes: ['name'] + } + ], + where: { + [Op.or]: [ + { created_by: userId }, + { '$participants.meeting_id$': { [Op.col]: 'Meeting.id' } }, + { '$participants.user_id$': userId } + ] }, - { - model: User, - as: 'creator', - attributes: ['name'], - } - ], - where: { - [Op.or]: [ - { created_by: userId }, - { '$participants.user_id$': userId } - ] - }, - order: [['createdAt', 'DESC']], - offset - }); + subQuery: false, + order: [['created_at', 'DESC']], + limit: limit + 1, + offset, + distinct: true + }); - const hasNext = meetings.length > limit; - const content = await Promise.all( - meetings.slice(0, limit).map(async (meeting) => { - const isParticipant = await MeetingParticipant.findOne({ - where: { - meeting_id: meeting.id, - user_id: userId - } - }); + const hasNext = meetings.length > limit; + const content = await Promise.all( + meetings.slice(0, limit).map(async (meeting) => { + const isParticipant = await MeetingParticipant.findOne({ + where: { + meeting_id: meeting.id, + user_id: userId + } + }); - const hasConflict = await ScheduleService.checkScheduleOverlapByTime( - userId, - meeting.time_idx_start, - meeting.time_idx_end - ); + const hasConflict = await ScheduleService.checkScheduleOverlapByTime( + userId, + meeting.time_idx_start, + meeting.time_idx_end + ); - const creatorName = meeting.creator ? meeting.creator.name : 'Unknown'; - return new MeetingResponseDTO(meeting, !!isParticipant, hasConflict, creatorName); - }) - ); + return { + ...meeting.toJSON(), + isParticipant: !!isParticipant, + hasConflict, + creatorName: meeting.creator?.name || 'Unknown' + }; + }) + ); - return { - content, - hasNext - }; + return { content, hasNext }; + } catch (error) { + console.error('getMyMeetings error:', error); + throw new Error('Failed to fetch my meetings'); + } } - async getMeetingDetail(meetingId, userId) { const meeting = await Meeting.findByPk(meetingId, { include: [ -- GitLab