This commit is contained in:
shivam 2025-03-18 21:45:54 +05:30
commit d445fca239
4 changed files with 16 additions and 17 deletions

View File

@ -1,34 +1,25 @@
const argon2 = require('argon2'); const argon2 = require('argon2');
const speakeasy = require('speakeasy'); const crypto = require('crypto');
// Generate Admin Credentials // Generate Admin Credentials
const createAdmin = async (password) => { const createAdmin = async (password) => {
const accessKey = crypto.randomBytes(16).toString('hex'); const accessKey = crypto.randomBytes(16).toString('hex');
const hash = await argon2.hash(password); const hash = await argon2.hash(password);
const totpSecret = speakeasy.generateSecret({ length: 20 });
return { return { accessKey, hash };
accessKey,
hash,
totpSecret: totpSecret.base32
};
}; };
// Verify Admin Login // Verify Admin Login
const verifyAdmin = async (accessKey, password, token) => { const verifyAdmin = async (accessKey, password) => {
const { rows: [admin] } = await db.query( const admins = await db.query(
'SELECT * FROM admins WHERE access_key = $1', 'SELECT * FROM admins WHERE access_key = ?',
[accessKey] [accessKey]
); );
const admin = admins[0];
if (!admin || !await argon2.verify(admin.argon2_hash, password)) { if (!admin || !await argon2.verify(admin.argon2_hash, password)) {
return false; return false;
} }
return speakeasy.totp.verify({ return true;
secret: admin.totp_secret,
encoding: 'base32',
token,
window: 1
});
}; };
module.exports = { createAdmin, verifyAdmin }; module.exports = { createAdmin, verifyAdmin };

View File

@ -35,7 +35,7 @@ exports.publishResult = async (data, authorization) => {
// publish result using team id // publish result using team id
await db.query(` await db.query(`
INSERT INTO results (team_id, result_date, result, announcement_time) INSERT INTO results (team_id, result_date, result, announcement_time)
VALUES (?, ?, ?, COALESCE(?, '00:00:00')) VALUES (?, ?, ?, ?)
ON DUPLICATE KEY UPDATE ON DUPLICATE KEY UPDATE
result = VALUES(result), result = VALUES(result),
announcement_time = VALUES(announcement_time) announcement_time = VALUES(announcement_time)

View File

@ -11,6 +11,8 @@ exports.getMonthlyResults = async (req, res) => {
FROM results r FROM results r
JOIN teams t ON r.team_id = t.id JOIN teams t ON r.team_id = t.id
WHERE t.name = ? AND DATE_FORMAT(r.result_date, '%Y-%m') = ? WHERE t.name = ? AND DATE_FORMAT(r.result_date, '%Y-%m') = ?
AND (r.result_date < CURDATE()
OR (r.result_date = CURDATE() AND r.announcement_time <= CURTIME()))
`, [team.toUpperCase(), month]); `, [team.toUpperCase(), month]);
if (results.length === 0) { if (results.length === 0) {
return res.status(404).json({ message: 'No results found for this team in the specified month.' }); return res.status(404).json({ message: 'No results found for this team in the specified month.' });
@ -32,6 +34,8 @@ exports.getDailyResults = async (req, res) => {
FROM results r FROM results r
JOIN teams t ON r.team_id = t.id JOIN teams t ON r.team_id = t.id
WHERE r.result_date = ? WHERE r.result_date = ?
AND (r.result_date < CURDATE()
OR (r.result_date = CURDATE() AND r.announcement_time <= CURTIME()))
`, [date]); `, [date]);
if (results.length === 0) { if (results.length === 0) {
return res.status(404).json({ message: 'No results found for the specified date.' }); return res.status(404).json({ message: 'No results found for the specified date.' });

View File

@ -18,6 +18,8 @@ router.get('/results', async (req, res) => {
FROM results r FROM results r
JOIN teams t ON r.team_id = t.id JOIN teams t ON r.team_id = t.id
WHERE t.name = ? AND r.result_date = ? WHERE t.name = ? AND r.result_date = ?
AND (r.result_date < CURDATE()
OR (r.result_date = CURDATE() AND r.announcement_time <= CURTIME()))
`, [team.toUpperCase(), date]); `, [team.toUpperCase(), date]);
if (!result) return res.status(404).json({ error: 'Result not found' }); if (!result) return res.status(404).json({ error: 'Result not found' });
@ -43,6 +45,8 @@ router.get('/today', async (req, res) => {
FROM results r FROM results r
JOIN teams t ON r.team_id = t.id JOIN teams t ON r.team_id = t.id
WHERE r.result_date = ? WHERE r.result_date = ?
AND (r.result_date < CURDATE()
OR (r.result_date = CURDATE() AND r.announcement_time <= CURTIME()))
`, [today]); `, [today]);
cache.set(cacheKey, results); cache.set(cacheKey, results);