Skip to content

Commit

Permalink
Merge pull request #43 from TeamSparker/feature/#42
Browse files Browse the repository at this point in the history
[GET] /room/:roomId/waiting/member 대기방 인원 조회
  • Loading branch information
youngkwon02 authored Jan 15, 2022
2 parents 33ab526 + 8064dcc commit 773b0a2
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 10 deletions.
1 change: 1 addition & 0 deletions functions/api/routes/room/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ 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;
15 changes: 8 additions & 7 deletions functions/api/routes/room/roomWaitingGET.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,16 @@ module.exports = async (req, res) => {
// 요청을 보낸 사용자를 제외한 member list
const friendsEntries = await roomDB.getFriendsByIds(client, roomId, userId);
const friendsIds = friendsEntries.map((f) => f.userId);
const users = await userDB.getUsersByIds(client, friendsIds);
let members = [];
for (let i = 0; i < friendsIds.length; i++) {
const userRow = await userDB.getUserById(client, friendsIds[i]);

users.map((u) =>
members.push({
userId: userRow.userId,
nickname: userRow.nickname,
profileImg: userRow.profileImg,
});
}
userId: u.userId,
nickname: u.nickname,
profileImg: u.profileImg,
}),
);

const data = {
roomId,
Expand Down
62 changes: 62 additions & 0 deletions functions/api/routes/room/roomWaitingMemberGET.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
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);
const users = await userDB.getUsersByIds(client, friendsIds);
let members = [];

users.map((u) =>
members.push({
userId: u.userId,
nickname: u.nickname,
profileImg: u.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();
}
};
16 changes: 13 additions & 3 deletions functions/db/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,18 @@ const getUserById = async (client, userId) => {
[userId],
);
return convertSnakeToCamel.keysToCamel(rows[0]);
}
};

const getUsersByIds = async (client, userIds) => {
const { rows } = await client.query(
`
SELECT * FROM spark.user
WHERE user_id in (${userIds.join()})
AND is_deleted = FALSE
`,
);
return convertSnakeToCamel.keysToCamel(rows);
};

const getUserBySocialId = async (client, socialId) => {
const { rows } = await client.query(
Expand Down Expand Up @@ -49,5 +60,4 @@ const addUser = async (client, socialId, nickname, profileImg) => {
return convertSnakeToCamel.keysToCamel(rows[0]);
};

module.exports = { getAllUsers, getUserById, getUserBySocialId, addUser };

module.exports = { getAllUsers, getUserById, getUsersByIds, getUserBySocialId, addUser };

0 comments on commit 773b0a2

Please sign in to comment.