From e4b29cdf4aeef9c4c63bc8b17e99beedfce1fd71 Mon Sep 17 00:00:00 2001 From: youngkwon02 Date: Fri, 21 Jan 2022 15:15:04 +0900 Subject: [PATCH 1/3] feat: rest related query --- functions/db/room.js | 80 ++++++++++++++++++++++++++++++-------------- 1 file changed, 55 insertions(+), 25 deletions(-) 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, }; From e7a6b30711c070bfe5f68b87dd9f1c767ed9d28a Mon Sep 17 00:00:00 2001 From: youngkwon02 Date: Fri, 21 Jan 2022 15:15:26 +0900 Subject: [PATCH 2/3] style: REST_COUNT_ZERO res message --- functions/constants/responseMessage.js | 1 + 1 file changed, 1 insertion(+) 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인 사용자입니다', }; From 24985b5106faac6e5007faebffd4a00f817e3379 Mon Sep 17 00:00:00 2001 From: youngkwon02 Date: Fri, 21 Jan 2022 15:18:00 +0900 Subject: [PATCH 3/3] =?UTF-8?q?feat:=20roomStatusPOST=20=EC=89=B4=EB=9E=98?= =?UTF-8?q?=EC=9A=94=20=EA=B4=80=EB=A0=A8=20=EC=B2=98=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- functions/api/routes/room/roomStatusPOST.js | 11 +++++++++++ 1 file changed, 11 insertions(+) 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 전송