Skip to content

Commit

Permalink
[feat]: 채팅방에 메시지 입력하지 않을 시 채팅방 삭제
Browse files Browse the repository at this point in the history
  • Loading branch information
Gseungmin committed Aug 19, 2023
1 parent 9d4c44c commit e7bb190
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
2 changes: 2 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,13 @@ const finAllRoom = require('./routes/finAllRoom');
const createRoom = require('./routes/createRoom');
const findRoom = require('./routes/findRoom');
const leaveRoom = require('./routes/leaveRoom');
const checkRoom = require('./routes/checkRoom');

app.use('/chat/room/all', finAllRoom);
app.use('/chat/room/create', createRoom);
app.use('/chat/room', findRoom);
app.use('/chat/room', leaveRoom);
app.use('/chat/room', checkRoom);

server.listen(port, () => {
console.log(`App listening at http://localhost:${port}`)
Expand Down
46 changes: 46 additions & 0 deletions routes/checkRoom.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
const express = require('express');
const axios = require('axios');
const { chat, chatroom } = require('../schemas');

const router = express.Router();

const resource_url = process.env.OAUTH_URL;

//메시지가 존재하는지 체크하는 메서드
router.get('/:roomId/check/message', async (req, res) => {

const token = req.headers.authorization; // 헤더에서 액세스 토큰 추출
const roomId = req.params.roomId; // route parameter로부터 roomId를 추출

//엑세스 토큰이 존재하지 않을 경우
if (!token) {
res.status(400).json({ message: "JWT Token이 존재하지 않습니다.", code: 10100 });
return;
}

//리소스에 접급
try {
const response = await axios.get(resource_url, {
headers: {
'Authorization': `${token}`
}
});
} catch (error) {
res.status(400).json({ message: "유효하지 않은 JWT Token 입니다.", code: 10101 });
}

try {
const chatCount = await chat.countDocuments({ roomId: roomId });

if(chatCount === 0) {
await chatroom.deleteOne({ roomId: roomId });
res.json({ message: "채팅방이 삭제되었습니다.", code: 200 });
} else {
res.json({ message: "채팅방에 메시지가 존재합니다.", code: 70002 });
}
} catch (error) {
res.status(500).json({ message: "서버 내부 오류가 발생하였습니다.", code: 50002 });
}
});

module.exports = router;

0 comments on commit e7bb190

Please sign in to comment.