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

[FIX] 쉴래요 누르면 카운트 차감 #153

Merged
merged 3 commits into from
Jan 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions functions/api/routes/room/roomStatusPOST.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 전송
Expand Down
1 change: 1 addition & 0 deletions functions/constants/responseMessage.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,5 @@ module.exports = {
CERTIFICATION_ALREADY_DONE: '이미 인증을 완료하였습니다',
REST_ALREADY_DONE: '이미 쉴래요를 사용한 사용자입니다',
UPDATE_STATUS_SUCCESS: '상태 변경 완료',
REST_COUNT_ZERO: '쉴래요 사용 가능 횟수가 0인 사용자입니다',
};
80 changes: 55 additions & 25 deletions functions/db/room.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand All @@ -455,4 +483,6 @@ module.exports = {
getEntriesByRoomIds,
updateThumbnail,
getAllRoomIds,
getRestCountByIds,
updateRestByIds,
};