From b923a078212c5a4c25d1263120c2e3f5022f2498 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=8B=AC=EC=9E=AC=EC=97=BD?= <jysim0326@ajou.ac.kr> Date: Sun, 8 Dec 2024 15:55:29 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20=EC=B1=84=ED=8C=85=EB=B0=A9=20=EA=B3=B5?= =?UTF-8?q?=EC=A7=80=EC=82=AC=ED=95=AD=20=EA=B8=B0=EB=8A=A5=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- controllers/chatController.js | 63 +++++++++++++++++++++++++++ routes/chatRoute.js | 4 ++ schemas/ChatRooms.js | 7 ++- services/chatService.js | 81 +++++++++++++++++++++++++++++++++++ 4 files changed, 153 insertions(+), 2 deletions(-) diff --git a/controllers/chatController.js b/controllers/chatController.js index 41a2d3c..bfcca38 100644 --- a/controllers/chatController.js +++ b/controllers/chatController.js @@ -91,4 +91,67 @@ exports.updateStatusAndLogId = async (req, res) => { console.error('Error updating user status and lastReadLogId:', err); res.status(500).json({ error: 'Failed to update user status and lastReadLogId' }); } +}; + +// 공지 등록 +exports.addNotice = async (req, res) => { + const { chatRoomId } = req.params; + const { sender, message } = req.body; + + try { + const notice = await chatService.addNotice(chatRoomId, sender, message); + res.status(200).json(notice); + } catch (error) { + console.error('Error adding notice:', error.message); + res.status(500).json({ error: 'Failed to add notice' }); + } +}; + +// 최신 공지 조회 +exports.getLatestNotice = async (req, res) => { + const { chatRoomId } = req.params; + + try { + const latestNotice = await chatService.getLatestNotice(chatRoomId); + if (latestNotice) { + res.status(200).json(latestNotice); + } else { + res.status(404).json({ message: 'No notices found' }); + } + } catch (error) { + console.error('Error fetching latest notice:', error.message); + res.status(500).json({ error: 'Failed to fetch latest notice' }); + } +}; + +// 공지 전체 조회 +exports.getAllNotices = async (req, res) => { + const { chatRoomId } = req.params; + + try { + const notices = await chatService.getAllNotices(chatRoomId); + console.log(`[getAllNotices] Notices for chatRoomId ${chatRoomId}:`, notices); // 로그 추가 + res.status(200).json(notices); + } catch (error) { + console.error('Error fetching all notices:', error.message); + res.status(500).json({ error: 'Failed to fetch all notices' }); + } +}; + +// 공지사항 상세 조회 +exports.getNoticeById = async (req, res) => { + const { chatRoomId, noticeId } = req.params; + + try { + const notice = await chatService.getNoticeById(chatRoomId, noticeId); + + if (!notice) { + return res.status(404).json({ error: 'Notice not found' }); + } + + res.status(200).json(notice); + } catch (error) { + console.error('Error fetching notice by ID:', error.message); + res.status(500).json({ error: 'Failed to fetch notice by ID' }); + } }; \ No newline at end of file diff --git a/routes/chatRoute.js b/routes/chatRoute.js index 6ae03f1..e7c2769 100644 --- a/routes/chatRoute.js +++ b/routes/chatRoute.js @@ -10,5 +10,9 @@ router.get('/unread-messages/:nickname', chatController.getUnreadMessages); router.get('/unread-count/:chatRoomId', chatController.getUnreadCount); router.post('/update-status-and-logid', chatController.updateStatusAndLogId); router.post('/update-read-log-id', chatController.updateReadLogId); +router.post('/:chatRoomId/notices', chatController.addNotice); +router.get('/:chatRoomId/notices/latest', chatController.getLatestNotice); +router.get('/:chatRoomId/notices', chatController.getAllNotices); +router.get('/:chatRoomId/notices/:noticeId', chatController.getNoticeById); module.exports = router; diff --git a/schemas/ChatRooms.js b/schemas/ChatRooms.js index 323bb94..beaba10 100644 --- a/schemas/ChatRooms.js +++ b/schemas/ChatRooms.js @@ -1,7 +1,5 @@ -//schemas/chatRooms.js const mongoose = require('mongoose'); -// MongoDB 채팅방 스키마 수정 (FCM 토큰을 배열로 관리) const chatRoomsSchema = new mongoose.Schema({ chatRoomId: { type: String, required: true, unique: true }, chatRoomName: { type: String, required: true }, @@ -18,6 +16,11 @@ const chatRoomsSchema = new mongoose.Schema({ lastReadAt: { type: Map, of: Date }, lastReadLogId: { type: Map, of: String }, isOnline: { type: Map, of: Boolean }, + notices: [{ + sender: { type: String }, + message: { type: String }, + timestamp: { type: Date, default: Date.now }, + }] }, { collection: 'chatrooms' }); const ChatRooms = mongoose.models.ChatRooms || mongoose.model('ChatRooms', chatRoomsSchema); diff --git a/services/chatService.js b/services/chatService.js index 0d462a5..f67ee42 100644 --- a/services/chatService.js +++ b/services/chatService.js @@ -209,6 +209,87 @@ class ChatService { } } + // 공지사항 추가 + async addNotice(chatRoomId, sender, message) { + try { + const newNotice = { + sender, + message, + timestamp: new Date(), + }; + + const updatedChatRoom = await ChatRooms.findOneAndUpdate( + { chatRoomId }, + { $push: { notices: newNotice } }, // 공지사항 배열에 추가 + { new: true } + ); + + if (!updatedChatRoom) { + throw new Error('Chat room not found'); + } + + return newNotice; + } catch (error) { + console.error('Error adding notice:', error.message); + throw new Error('Failed to add notice'); + } + } + + // 최신 공지사항 조회 + async getLatestNotice(chatRoomId) { + try { + const chatRoom = await ChatRooms.findOne( + { chatRoomId }, + { notices: { $slice: -1 } } // 최신 공지 1개만 가져오기 + ); + + if (!chatRoom || chatRoom.notices.length === 0) { + return null; + } + + return chatRoom.notices[0]; + } catch (error) { + console.error('Error fetching latest notice:', error.message); + throw new Error('Failed to fetch latest notice'); + } + } + + // 공지사항 전체 조회 + async getAllNotices(chatRoomId) { + try { + const chatRoom = await ChatRooms.findOne({ chatRoomId }, { notices: 1 }); + + if (!chatRoom) { + throw new Error('Chat room not found'); + } + + return chatRoom.notices; + } catch (error) { + console.error('Error fetching all notices:', error.message); + throw new Error('Failed to fetch all notices'); + } + } + + // 공지사항 상세 조회 + async getNoticeById(chatRoomId, noticeId) { + try { + const chatRoom = await ChatRooms.findOne({ chatRoomId }); + if (!chatRoom) { + throw new Error('Chat room not found'); + } + + const notice = chatRoom.notices.find(notice => notice._id.toString() === noticeId); + if (!notice) { + throw new Error('Notice not found'); + } + + return notice; + } catch (error) { + console.error('Error in getNoticeById:', error.message); + throw error; + } + } + } module.exports = new ChatService(); \ No newline at end of file -- GitLab