Skip to content

Commit

Permalink
Add: add timelock to submit and cancel
Browse files Browse the repository at this point in the history
  • Loading branch information
thxx132 committed Feb 18, 2025
1 parent c998dd1 commit 411e2e7
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 7 deletions.
12 changes: 6 additions & 6 deletions src/lottery/schedules/dailyQuiz.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,23 @@ import { completeAnswerCorrectlyQuest } from "../modules/contracts";

const determineQuizResult = async () => {
try {
const yesterdayMidnight = new Date();
yesterdayMidnight.setHours(0, 0, 0, 0);
yesterdayMidnight.setDate(yesterdayMidnight.getDate() + 1);
const tomorrowMidnight = new Date();
tomorrowMidnight.setHours(0, 0, 0, 0);
tomorrowMidnight.setDate(tomorrowMidnight.getDate() + 1);

const todayMidnight = new Date();
todayMidnight.setHours(0, 0, 0, 0);

// 어제의 퀴즈 조회
// 오늘의 퀴즈 조회
const quiz = await quizModel.findOne({
quizDate: {
$lt: yesterdayMidnight,
$gte: todayMidnight,
$lt: tomorrowMidnight,
},
});

if (!quiz) {
logger.info("No quiz found for yesterday.");
logger.info("No quiz found for today.");
return;
}

Expand Down
2 changes: 1 addition & 1 deletion src/lottery/schedules/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const cron = require("node-cron");

const registerSchedules = () => {
cron.schedule("0 4 * * *", require("./detectAbusingUsers"));
cron.schedule("5 0 * * *", require("./dailyQuiz").default);
cron.schedule("56 23 * * *", require("./dailyQuiz").default);
};

module.exports = registerSchedules;
16 changes: 16 additions & 0 deletions src/lottery/services/quizzes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,14 @@ export const submitAnswerHandler: RequestHandler = async (req, res) => {
const userId = req.userOid;
const { answer } = req.body;

// 11시 55분 이전인지 검사하기
const timestamp = new Date();
const settingtime = new Date();
settingtime.setHours(23, 55, 0, 0);
if (timestamp > settingtime) {
return res.status(404).json({ message: "You can't submit answer now." });
}

const startOfToday = new Date();
startOfToday.setHours(0, 0, 0, 0);

Expand Down Expand Up @@ -288,6 +296,14 @@ export const cancelAnswerHandler: RequestHandler = async (req, res) => {
// 로그인된 사용자 정보 가져오기
const userId = req.userOid;

// 11시 55분 이전인지 검사하기
const timestamp = new Date();
const settingtime = new Date();
settingtime.setHours(23, 55, 0, 0);
if (timestamp > settingtime) {
return res.status(404).json({ message: "You can't submit answer now." });
}

const startOfToday = new Date();
startOfToday.setHours(0, 0, 0, 0);

Expand Down

0 comments on commit 411e2e7

Please sign in to comment.