Skip to content

Commit

Permalink
feat: 분기별 생명 충전 기능 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
youngkwon02 committed Oct 1, 2022
1 parent 1d76407 commit 440c8f6
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 25 deletions.
43 changes: 29 additions & 14 deletions functions/api/routes/room/roomTimelineGET.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
5 changes: 5 additions & 0 deletions functions/constants/termList.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const termList = [1, 4, 8, 34, 60];

module.exports = {
termList,
};
40 changes: 29 additions & 11 deletions functions/scheduler/funcs.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -103,7 +104,7 @@ const checkLife = async () => {

// 생명 감소시 Time Line Insert
if (decreaseMessage.length) {
await lifeTimelineDB.addLifeTimeline(client, decreaseMessage);
await lifeTimelineDB.addDecreaseTimelines(client, decreaseMessage);
}

// 살아남은 방 없으면 return
Expand All @@ -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();
}
};
Expand Down

0 comments on commit 440c8f6

Please sign in to comment.