From 440c8f627ced3fc8937d0cd1d8346c05e1d8e6df Mon Sep 17 00:00:00 2001 From: youngkwon02 Date: Sat, 1 Oct 2022 18:04:09 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20=EB=B6=84=EA=B8=B0=EB=B3=84=20=EC=83=9D?= =?UTF-8?q?=EB=AA=85=20=EC=B6=A9=EC=A0=84=20=EA=B8=B0=EB=8A=A5=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- functions/api/routes/room/roomTimelineGET.js | 43 +++++++++++++------- functions/constants/termList.js | 5 +++ functions/scheduler/funcs.js | 40 +++++++++++++----- 3 files changed, 63 insertions(+), 25 deletions(-) create mode 100644 functions/constants/termList.js diff --git a/functions/api/routes/room/roomTimelineGET.js b/functions/api/routes/room/roomTimelineGET.js index 4eb1cb0..a15b9f3 100644 --- a/functions/api/routes/room/roomTimelineGET.js +++ b/functions/api/routes/room/roomTimelineGET.js @@ -54,20 +54,35 @@ module.exports = async (req, res) => { let timelines = await lifeTimelineDB.getLifeTimeline(client, roomId, user.userId); timelines = timelines.map((t) => { - const timeline = {}; - const { title, content } = timelineMessage.LIFE_DECREASE(t.decreaseCount); - - let createdAt = dayjs(t.createdAt); - const passedDay = now.diff(createdAt, 'd'); - const profiles = [t.profile_1, t.profile_2].filter((profile) => profile !== 'null'); - - timeline['title'] = title; - timeline['content'] = content; - timeline['profiles'] = profiles; - timeline['day'] = passedDayToStr(passedDay); - timeline['isNew'] = !t.isRead; - - return timeline; + if (t.isDecrease) { + const timeline = {}; + const { title, content } = timelineMessage.LIFE_DECREASE(t.decreaseCount); + + let createdAt = dayjs(t.createdAt); + const passedDay = now.diff(createdAt, 'd'); + const profiles = [t.profile_1, t.profile_2].filter((profile) => profile !== 'null'); + + timeline['title'] = title; + timeline['content'] = content; + timeline['profiles'] = profiles; + timeline['day'] = passedDayToStr(passedDay); + timeline['isNew'] = !t.isRead; + + return timeline; + } else { + const timeline = {}; + const { title, content } = timelineMessage.LIFE_FILL(t.termDay); + + let createdAt = dayjs(t.createdAt); + const passedDay = now.diff(createdAt, 'd'); + + timeline['title'] = title; + timeline['content'] = content; + timeline['day'] = passedDayToStr(passedDay); + timeline['isNew'] = !t.isRead; + + return timeline; + } }); await lifeTimelineDB.readLifeTimeline(client, roomId, user.userId); diff --git a/functions/constants/termList.js b/functions/constants/termList.js new file mode 100644 index 0000000..08f84f5 --- /dev/null +++ b/functions/constants/termList.js @@ -0,0 +1,5 @@ +const termList = [1, 4, 8, 34, 60]; + +module.exports = { + termList, +}; diff --git a/functions/scheduler/funcs.js b/functions/scheduler/funcs.js index 6c56b5e..c2c9b9e 100644 --- a/functions/scheduler/funcs.js +++ b/functions/scheduler/funcs.js @@ -5,6 +5,7 @@ const dayjs = require('dayjs'); const slackAPI = require('../middlewares/slackAPI'); const alarmMessage = require('../constants/alarmMessage'); const pushAlarm = require('../lib/pushAlarm'); +const { termList } = require('../constants/termList'); const checkLife = async () => { let client; @@ -103,7 +104,7 @@ const checkLife = async () => { // 생명 감소시 Time Line Insert if (decreaseMessage.length) { - await lifeTimelineDB.addLifeTimeline(client, decreaseMessage); + await lifeTimelineDB.addDecreaseTimelines(client, decreaseMessage); } // 살아남은 방 없으면 return @@ -114,24 +115,41 @@ const checkLife = async () => { const survivedRoomIds = _.difference(successRoomIds, completeRoomIds); const ongoingEntries = await roomDB.getEntriesByRoomIds(client, survivedRoomIds); // 성공한 방들의 entry 불러오기 - // 추가해줄 record들의 속성들 빚어주기 - const insertEntries = ongoingEntries.map((o) => { - const startDate = dayjs(o.startAt); - const day = dayjs(today).diff(startDate, 'day') + 1; - const queryParameter = '(' + o.entryId + ",'" + now.format('YYYY-MM-DD') + "'," + day + ')'; + // 추가해줄 records + let insertEntries = []; - return queryParameter; - }); + // 추가해줄 lifeTimelines + let insertTimelines = []; + + // 생명 충전해줄 RoomIds + let fillLifeRoomIds = new Set(); + + for (let i = 0; i < ongoingEntries.length; i++) { + // insert할 record 값들 생성 + const entry = ongoingEntries[i]; + const day = dayjs(today).diff(dayjs(entry.startAt), 'day') + 1; + const queryParameter = '(' + entry.entryId + ",'" + now.format('YYYY-MM-DD') + "'," + day + ')'; + insertEntries.push(queryParameter); + + if (termList.includes(day)) { + fillLifeRoomIds.add(entry.roomId); + insertTimelines.push(`('${entry.userId}', '${entry.roomId}', false, ${day})`); + } + } + + fillLifeRoomIds = Array.from(fillLifeRoomIds); + + await recordDB.insertRecords(client, insertEntries); // record 추가! + await lifeTimelineDB.addFillTimelines(client, insertTimelines); // lifeTimeline 추가! + await roomDB.fillLifeByRoomIds(client, fillLifeRoomIds); // 생명 충전 - const resultRecords = await recordDB.insertRecords(client, insertEntries); // record 추가! - const slackMessage = `폭파된 방 목록: ${failRoomIds} \n 살아남은 방 목록: ${survivedRoomIds}`; + const slackMessage = `폭파된 방 목록: ${failRoomIds} \n 생명 충전 방 목록: ${fillLifeRoomIds} \n 살아남은 방 목록: ${survivedRoomIds}`; slackAPI.sendMessageToSlack(slackMessage, slackAPI.DEV_WEB_HOOK_ERROR_MONITORING); } catch (error) { console.log(error); const slackMessage = `[ERROR] ${error} ${JSON.stringify(error)}`; slackAPI.sendMessageToSlack(slackMessage, slackAPI.DEV_WEB_HOOK_ERROR_MONITORING); } finally { - console.log('relase'); client.release(); } };