diff --git a/functions/api/routes/room/roomStatusPOST.js b/functions/api/routes/room/roomStatusPOST.js index cc8f029..3d1d00c 100644 --- a/functions/api/routes/room/roomStatusPOST.js +++ b/functions/api/routes/room/roomStatusPOST.js @@ -68,6 +68,17 @@ module.exports = async (req, res) => { return res.status(statusCode.BAD_REQUEST).send(util.fail(statusCode.BAD_REQUEST, responseMessage.REST_ALREADY_DONE)); } + if (statusType === 'REST') { + // @error 7. 쉴래요 가능 횟수가 0인 사용쟈 + const restCount = await roomDB.getRestCountByIds(client, roomId, userId); + if (restCount < 1) { + return res.status(statusCode.BAD_REQUEST).send(util.fail(statusCode.BAD_REQUEST, responseMessage.REST_COUNT_ZERO)); + } + + const newRestCount = restCount - 1; + await roomDB.updateRestByIds(client, roomId, userId, newRestCount); + } + await recordDB.updateStatusByRecordId(client, recentRecord.recordId, statusType); // 고민중을 눌렀으면 Notification에 추가, PushAlarm 전송 diff --git a/functions/constants/responseMessage.js b/functions/constants/responseMessage.js index 01cb1fe..d0f4e6f 100644 --- a/functions/constants/responseMessage.js +++ b/functions/constants/responseMessage.js @@ -81,4 +81,5 @@ module.exports = { CERTIFICATION_ALREADY_DONE: '이미 인증을 완료하였습니다', REST_ALREADY_DONE: '이미 쉴래요를 사용한 사용자입니다', UPDATE_STATUS_SUCCESS: '상태 변경 완료', + REST_COUNT_ZERO: '쉴래요 사용 가능 횟수가 0인 사용자입니다', }; diff --git a/functions/db/room.js b/functions/db/room.js index 9390a31..bb8e1b5 100644 --- a/functions/db/room.js +++ b/functions/db/room.js @@ -371,16 +371,16 @@ const getFailRecords = async (client) => { AND r.date = $1 GROUP BY e.room_id `, - [yesterday] + [yesterday], ); return convertSnakeToCamel.keysToCamel(rows); -} +}; -const updateLife = async(client, failCount, roomIds) => { +const updateLife = async (client, failCount, roomIds) => { const now = dayjs().add(9, 'hour'); const yesterday = dayjs(now.subtract(1, 'day').format('YYYY-MM-DD')); - const { rows } = await client.query( - ` + const { rows } = await client.query( + ` UPDATE spark.room SET end_at = CASE @@ -400,40 +400,68 @@ const updateLife = async(client, failCount, roomIds) => { WHERE room_id IN (${roomIds.join()}) RETURNING room_id, life `, - [failCount, yesterday] - ); - return convertSnakeToCamel.keysToCamel(rows); -} + [failCount, yesterday], + ); + return convertSnakeToCamel.keysToCamel(rows); +}; -const updateThumbnail = async(client, entryId, img) => { - const { rows } = await client.query( - ` +const updateThumbnail = async (client, entryId, img) => { + const now = dayjs().add(9, 'hour'); + const { rows } = await client.query( + ` UPDATE spark.entry e - SET thumbnail = $2 + SET thumbnail = $2, updated_at = $3 WHERE entry_id = $1 `, - [entryId, img] - ); - return convertSnakeToCamel.keysToCamel(rows[0]); -} - + [entryId, img, now], + ); + return convertSnakeToCamel.keysToCamel(rows[0]); +}; -const getAllRoomIds = async(client) => { +const getAllRoomIds = async (client) => { const { rows } = await client.query( ` SELECT r.room_id FROM spark.room r - WHERE is_deleted=FALSE + WHERE is_deleted = FALSE AND status='ONGOING' `, ); return convertSnakeToCamel.keysToCamel(rows); -} +}; + +const getRestCountByIds = async (client, roomId, userId) => { + const { rows } = await client.query( + ` + SELECT rest + FROM spark.room + WHERE room_id = $1 + AND user_id = $2 + `, + [roomId, userId], + ); + return convertSnakeToCamel.keysToCamel(rows[0]); +}; + +const updateRestByIds = async (client, roomId, userId, newRestCount) => { + const now = dayjs().add(9, 'hour'); + const { rows } = await client.query( + ` + UPDATE spark.entry e + SET rest = $3, updated_at = $4 + WHERE room_id = $1 + AND user_id = $2 + RETURNING * + `, + [roomId, userId, newRestCount, now], + ); + return convertSnakeToCamel.keysToCamel(rows[0]); +}; -module.exports = { - addRoom, - isCodeUnique, - getRoomById, +module.exports = { + addRoom, + isCodeUnique, + getRoomById, getRoomsByIds, getRoomByCode, getEntriesByRoomId, @@ -455,4 +483,6 @@ module.exports = { getEntriesByRoomIds, updateThumbnail, getAllRoomIds, + getRestCountByIds, + updateRestByIds, };