mirror of
https://github.com/itsnaveenk/bazar3.git
synced 2025-12-19 21:17:06 +00:00
"updated"
This commit is contained in:
parent
71bb8623ec
commit
70b8e57602
@ -1,34 +1,25 @@
|
||||
const argon2 = require('argon2');
|
||||
const speakeasy = require('speakeasy');
|
||||
const crypto = require('crypto');
|
||||
|
||||
// Generate Admin Credentials
|
||||
const createAdmin = async (password) => {
|
||||
const accessKey = crypto.randomBytes(16).toString('hex');
|
||||
const hash = await argon2.hash(password);
|
||||
const totpSecret = speakeasy.generateSecret({ length: 20 });
|
||||
|
||||
return {
|
||||
accessKey,
|
||||
hash,
|
||||
totpSecret: totpSecret.base32
|
||||
};
|
||||
return { accessKey, hash };
|
||||
};
|
||||
|
||||
// Verify Admin Login
|
||||
const verifyAdmin = async (accessKey, password, token) => {
|
||||
const { rows: [admin] } = await db.query(
|
||||
'SELECT * FROM admins WHERE access_key = $1',
|
||||
const verifyAdmin = async (accessKey, password) => {
|
||||
const admins = await db.query(
|
||||
'SELECT * FROM admins WHERE access_key = ?',
|
||||
[accessKey]
|
||||
);
|
||||
const admin = admins[0];
|
||||
if (!admin || !await argon2.verify(admin.argon2_hash, password)) {
|
||||
return false;
|
||||
}
|
||||
return speakeasy.totp.verify({
|
||||
secret: admin.totp_secret,
|
||||
encoding: 'base32',
|
||||
token,
|
||||
window: 1
|
||||
});
|
||||
return true;
|
||||
};
|
||||
|
||||
module.exports = { createAdmin, verifyAdmin };
|
||||
@ -35,7 +35,7 @@ exports.publishResult = async (data, authorization) => {
|
||||
// publish result using team id
|
||||
await db.query(`
|
||||
INSERT INTO results (team_id, result_date, result, announcement_time)
|
||||
VALUES (?, ?, ?, COALESCE(?, '00:00:00'))
|
||||
VALUES (?, ?, ?, ?)
|
||||
ON DUPLICATE KEY UPDATE
|
||||
result = VALUES(result),
|
||||
announcement_time = VALUES(announcement_time)
|
||||
|
||||
@ -11,6 +11,8 @@ exports.getMonthlyResults = async (req, res) => {
|
||||
FROM results r
|
||||
JOIN teams t ON r.team_id = t.id
|
||||
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]);
|
||||
if (results.length === 0) {
|
||||
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
|
||||
JOIN teams t ON r.team_id = t.id
|
||||
WHERE r.result_date = ?
|
||||
AND (r.result_date < CURDATE()
|
||||
OR (r.result_date = CURDATE() AND r.announcement_time <= CURTIME()))
|
||||
`, [date]);
|
||||
if (results.length === 0) {
|
||||
return res.status(404).json({ message: 'No results found for the specified date.' });
|
||||
|
||||
@ -18,6 +18,8 @@ router.get('/results', async (req, res) => {
|
||||
FROM results r
|
||||
JOIN teams t ON r.team_id = t.id
|
||||
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]);
|
||||
|
||||
if (!result) return res.status(404).json({ error: 'Result not found' });
|
||||
@ -43,6 +45,8 @@ router.get('/today', async (req, res) => {
|
||||
FROM results r
|
||||
JOIN teams t ON r.team_id = t.id
|
||||
WHERE r.result_date = ?
|
||||
AND (r.result_date < CURDATE()
|
||||
OR (r.result_date = CURDATE() AND r.announcement_time <= CURTIME()))
|
||||
`, [today]);
|
||||
|
||||
cache.set(cacheKey, results);
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user