Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[GET] /room/:roomId/waiting/member 대기방 인원 조회 #43

Merged
merged 9 commits into from
Jan 15, 2022
2 changes: 2 additions & 0 deletions functions/api/routes/room/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ router.get('/code/:code', checkUser, require('./roomCodeGET'));
router.post('', checkUser, require('./roomPOST'));
router.post('/:roomId/enter', checkUser, require('./roomEnterPOST'));
router.patch('/:roomId/purpose', checkUser, require('./roomPurposePATCH'));
router.get('/:roomId/waiting', checkUser, require('./roomWaitingGET'));
router.get('/:roomId', checkUser, require('./roomDetailGET'));
router.get('/:roomId/waiting/member', checkUser, require('./roomWaitingMemberGET'));

module.exports = router;
82 changes: 82 additions & 0 deletions functions/api/routes/room/roomWaitingGET.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
const functions = require('firebase-functions');
const util = require('../../../lib/util');
const statusCode = require('../../../constants/statusCode');
const responseMessage = require('../../../constants/responseMessage');
const db = require('../../../db/db');
const { userDB, roomDB } = require('../../../db');

/**
* @대기_방_조회
* @route GET /room/:roomId/waiting
* @error
* 1. 유효하지 않은 roomId
* 2. 권한이 없는 사용자로부터의 요청
*/

module.exports = async (req, res) => {
const { roomId } = req.params;
const user = req.user;
console.log(user);
const userId = user.userId;

let client;

try {
client = await db.connect(req);

const room = await roomDB.getRoomById(client, roomId);

// @error 1. 유효하지 않은 roomId
if (!room) {
return res.status(statusCode.BAD_REQUEST).send(util.fail(statusCode.BAD_REQUEST, responseMessage.ROOM_ID_NOT_FOUND));
}

const selfEntry = await roomDB.getEntryByIds(client, roomId, userId);

// error 2. 권한이 없는 사용자로부터의 요청
if (!selfEntry) {
return res.status(statusCode.BAD_REQUEST).send(util.fail(statusCode.BAD_REQUEST, responseMessage.PRIV_NOT_FOUND));
}

// 요청을 보낸 사용자 본인 data
const reqUser = {
userId,
nickname: user.nickname,
profileImg: user.profileImg,
isPurposeSet: selfEntry.moment !== null && selfEntry.purpose !== null,
moment: selfEntry.moment,
purpose: selfEntry.purpose,
isHost: room.creator === userId,
};

// 요청을 보낸 사용자를 제외한 member list
const friendsEntries = await roomDB.getFriendsByIds(client, roomId, userId);
const friendsIds = friendsEntries.map((f) => f.userId);
let members = [];
for (let i = 0; i < friendsIds.length; i++) {
const userRow = await userDB.getUserById(client, friendsIds[i]);
members.push({
userId: userRow.userId,
nickname: userRow.nickname,
profileImg: userRow.profileImg,
});
}

const data = {
roomId,
roomName: room.roomName,
roomCode: room.code,
fromStart: room.fromStart,
reqUser,
members,
};

res.status(statusCode.OK).send(util.success(statusCode.OK, responseMessage.GET_WAITROOM_DATA_SUCCESS, data));
} catch (error) {
functions.logger.error(`[ERROR] [${req.method.toUpperCase()}] ${req.originalUrl}`, `[CONTENT] ${error}`);
console.log(error);
res.status(statusCode.INTERNAL_SERVER_ERROR).send(util.fail(statusCode.INTERNAL_SERVER_ERROR, responseMessage.INTERNAL_SERVER_ERROR));
} finally {
client.release();
}
};
61 changes: 61 additions & 0 deletions functions/api/routes/room/roomWaitingMemberGET.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
const functions = require('firebase-functions');
const util = require('../../../lib/util');
const statusCode = require('../../../constants/statusCode');
const responseMessage = require('../../../constants/responseMessage');
const db = require('../../../db/db');
const { userDB, roomDB } = require('../../../db');

/**
* @대기_방_인원_조회
* @route GET /room/:roomId/waiting/member
* @error
* 1. 유효하지 않은 roomId
* 2. 권한이 없는 사용자로부터의 요청
*/

module.exports = async (req, res) => {
const { roomId } = req.params;
const user = req.user;
const userId = user.userId;

let client;

try {
client = await db.connect(req);

const room = await roomDB.getRoomById(client, roomId);

// @error 1. 유효하지 않은 roomId
if (!room) {
return res.status(statusCode.BAD_REQUEST).send(util.fail(statusCode.BAD_REQUEST, responseMessage.ROOM_ID_NOT_FOUND));
}

const selfEntry = await roomDB.getEntryByIds(client, roomId, userId);

// error 2. 권한이 없는 사용자로부터의 요청
if (!selfEntry) {
return res.status(statusCode.BAD_REQUEST).send(util.fail(statusCode.BAD_REQUEST, responseMessage.PRIV_NOT_FOUND));
}

// 요청을 보낸 사용자를 제외한 member list
const friendsEntries = await roomDB.getFriendsByIds(client, roomId, userId);
const friendsIds = friendsEntries.map((f) => f.userId);
let members = [];
for (let i = 0; i < friendsIds.length; i++) {
youngkwon02 marked this conversation as resolved.
Show resolved Hide resolved
const userRow = await userDB.getUserById(client, friendsIds[i]);
members.push({
userId: userRow.userId,
nickname: userRow.nickname,
profileImg: userRow.profileImg,
});
}

res.status(statusCode.OK).send(util.success(statusCode.OK, responseMessage.GET_WAITROOM_DATA_SUCCESS, { members }));
} catch (error) {
functions.logger.error(`[ERROR] [${req.method.toUpperCase()}] ${req.originalUrl}`, `[CONTENT] ${error}`);
console.log(error);
res.status(statusCode.INTERNAL_SERVER_ERROR).send(util.fail(statusCode.INTERNAL_SERVER_ERROR, responseMessage.INTERNAL_SERVER_ERROR));
} finally {
client.release();
}
};
1 change: 1 addition & 0 deletions functions/constants/responseMessage.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ module.exports = {
ENTER_ROOM_ALREADY: '이미 참여중인 습관 방입니다',
ROOM_ID_INVALID: '올바르지 않은 roomId입니다',
PURPOSE_SET_SUCCESS: '목표 설정 성공',
ROOM_ID_NOT_FOUND: '유효하지 않은 roomId입니다',
GET_ROOM_DATA_FAIL: '존재하지 않는 습관방입니다',
NOT_ONGOING_ROOM: '현재 진행중인 습관방이 아닙니다',
NOT_MEMBER: '참여중인 습관방이 아닙니다',
Expand Down
19 changes: 18 additions & 1 deletion functions/db/room.js
Original file line number Diff line number Diff line change
Expand Up @@ -197,4 +197,21 @@ const enterById = async (client, roomId, userId) => {
}
};

module.exports = { addRoom, isCodeUnique, getRoomById, getRoomByCode, getEntriesByRoomId, kickedHistoryByIds, getEntryByIds, updatePurposeByEntryId, getRecordsByDay, checkEnteredById, enterById };
const getFriendsByIds = async (client, roomId, userId) => {
const { rows } = await client.query(
`
SELECT * FROM spark.entry
WHERE room_id = $1
AND user_id != $2
AND is_deleted = FALSE
AND is_out = FALSE
AND is_kicked = FALSE
ORDER BY created_at
`,
[roomId, userId],
);
return convertSnakeToCamel.keysToCamel(rows);
};


module.exports = { addRoom, isCodeUnique, getRoomById, getRoomByCode, getEntriesByRoomId, kickedHistoryByIds, getEntryByIds, updatePurposeByEntryId, getRecordsByDay, checkEnteredById, enterById, getFriendsByIds };