-
Notifications
You must be signed in to change notification settings - Fork 0
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 특정 습관방 조회 #31
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
7a7fabe
Merge branch 'develop' of github.com:TeamSparker/Spark-Server into fe…
xxeol2 a76570d
Merge branch 'develop' of github.com:TeamSparker/Spark-Server into fe…
xxeol2 0baab28
Merge branch 'develop' of github.com:TeamSparker/Spark-Server into fe…
xxeol2 c55b3ed
Merge branch 'develop' of github.com:TeamSparker/Spark-Server into fe…
xxeol2 3c92bb8
feat: 습관방 detail GET api
xxeol2 15e8b68
fix: 불필요한 코드 삭제 및 response Message 추가
xxeol2 86e67fe
Merge branch 'develop' into feature/#13
xxeol2 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
const functions = require('firebase-functions'); | ||
const admin = require('firebase-admin'); | ||
const util = require('../../../lib/util'); | ||
const statusCode = require('../../../constants/statusCode'); | ||
const responseMessage = require('../../../constants/responseMessage'); | ||
const db = require('../../../db/db'); | ||
const { userDB, roomDB, sparkDB } = require('../../../db'); | ||
const jwtHandlers = require('../../../lib/jwtHandlers'); | ||
const slackAPI = require('../../../middlewares/slackAPI'); | ||
const dayjs = require('dayjs'); | ||
|
||
/** | ||
* @습관방_상세_조회 | ||
* @route GET /room/:roomId | ||
* @error | ||
* 1. 존재하지 않는 습관방인 경우 | ||
* 2. 진행중인 습관방이 아닌 경우 | ||
* 3. 접근 권한이 없는 유저인 경우 | ||
*/ | ||
|
||
module.exports = async (req, res) => { | ||
const { roomId } = req.params; | ||
const user = req.user; | ||
|
||
let client; | ||
|
||
try { | ||
client = await db.connect(req); | ||
|
||
const room = await roomDB.getRoomById(client, roomId); | ||
|
||
const startDate = dayjs(room.startAt); | ||
const endDate = dayjs(room.endAt); | ||
const now = dayjs().add(9, "hour"); | ||
const today = dayjs(now.format("YYYY-MM-DD")); | ||
const leftDay = endDate.diff(today, "day"); | ||
const day = today.diff(startDate,"day") + 1; | ||
|
||
// @error 1. 존재하지 않는 습관방인 경우 | ||
if (!room) { | ||
res.status(statusCode.NO_CONTENT).send(util.fail(statusCode.NO_CONTENT, responseMessage.GET_ROOM_DATA_FAIL)); | ||
} | ||
// @error 2. 진행중인 습관방이 아닌 경우 | ||
if (!room.isStarted || leftDay < 0) { | ||
res.status(statusCode.BAD_REQUEST).send(util.fail(statusCode.BAD_REQUEST, responseMessage.NOT_ONGOING_ROOM)); | ||
} | ||
const entries = await roomDB.getEntriesByRoomId(client, roomId); | ||
|
||
// @error 3. 접근 권한이 없는 유저인 경우 | ||
const userEntry = entries.filter((entry) => entry.userId === user.userId); | ||
|
||
if (!userEntry.length) { | ||
res.status(statusCode.UNAUTHORIZED).send(util.fail(statusCode.UNAUTHORIZED, responseMessage.NOT_MEMBER)); | ||
} | ||
|
||
const records = await roomDB.getRecordsByDay(client, roomId, day); | ||
|
||
let myRecord = null; | ||
|
||
let otherRecords = []; | ||
records.map((record) => { | ||
if (record.userId === user.userId) { | ||
myRecord = { | ||
recordId: record.recordId, | ||
userId: record.userId, | ||
profileImg: record.profileImg, | ||
nickname: record.nickname, | ||
status: record.status, | ||
rest: record.rest | ||
} | ||
} | ||
else{ | ||
otherRecords.push({ | ||
recordId: record.recordId, | ||
userId: record.userId, | ||
profileImg: record.profileImg, | ||
nickname: record.nickname, | ||
status: record.status | ||
}); | ||
} | ||
}); | ||
|
||
const recievedSpark = await sparkDB.countSparkByRecordId(client, myRecord.recordId); | ||
myRecord.recievedSpark = parseInt(recievedSpark[0].count); | ||
|
||
console.log("myRecrod", myRecord); | ||
console.log("otherRecords", otherRecords); | ||
|
||
const data = { | ||
roomId, | ||
roomName: room.roomName, | ||
startDate: startDate.format('YYYY.MM.DD.'), | ||
endDate: endDate.format('YYYY.MM.DD.'), | ||
moment: userEntry.moment, | ||
purpose: userEntry.purpose, | ||
leftDay, | ||
life: room.life, | ||
fromStart: room.fromStart, | ||
myRecord, | ||
otherRecords | ||
} | ||
|
||
res.status(statusCode.OK).send(util.success(statusCode.OK, responseMessage.GET_ROOM_DETAIL_SUCCESS, data)); | ||
|
||
} catch (error) { | ||
console.log(error); | ||
functions.logger.error(`[ERROR] [${req.method.toUpperCase()}] ${req.originalUrl}`, `[CONTENT] ${error}`); | ||
const slackMessage = `[ERROR] [${req.method.toUpperCase()}] ${req.originalUrl} ${error} ${JSON.stringify(error)}`; | ||
slackAPI.sendMessageToSlack(slackMessage, slackAPI.DEV_WEB_HOOK_ERROR_MONITORING); | ||
res.status(statusCode.INTERNAL_SERVER_ERROR).send(util.fail(statusCode.INTERNAL_SERVER_ERROR, responseMessage.INTERNAL_SERVER_ERROR)); | ||
} finally { | ||
client.release(); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
module.exports = { | ||
userDB: require('./user'), | ||
roomDB: require('./room'), | ||
sparkDB: require('./spark'), | ||
noticeDB: require('./notice'), | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
const _ = require('lodash'); | ||
const convertSnakeToCamel = require('../lib/convertSnakeToCamel'); | ||
|
||
const countSparkByRecordId = async (client, recordId) => { | ||
const { rows } = await client.query( | ||
` | ||
SELECT COUNT(*) | ||
FROM spark.spark | ||
WHERE record_id = $1 | ||
`, | ||
[recordId], | ||
); | ||
return convertSnakeToCamel.keysToCamel(rows); | ||
}; | ||
|
||
module.exports = { countSparkByRecordId }; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"GET_ROOM_DETAIL_SUCCESS"
이 ResponseMessage 등록 안해주셨어요~~
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
앗! pull받으면서 삭제됐네요 추가할게요!!😀