timing issue fixed

This commit is contained in:
Naveen Kumar 2025-03-21 00:59:45 +05:30
parent a01d1d9549
commit 56e564af63
8 changed files with 88 additions and 12 deletions

View File

@ -17,7 +17,7 @@ http {
server {
server_name backend.kings.com;
location / {
proxy_pass http://localhost:3000; // Forward to your Node.js backend
proxy_pass http://localhost:5500; # Forward to your Node.js backend
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";

View File

@ -1,5 +1,6 @@
const argon2 = require('argon2');
const crypto = require('crypto');
const db = require('./db');
// Generate Admin Credentials
const createAdmin = async (password) => {

View File

@ -14,7 +14,7 @@ const pool = mysql.createPool({
waitForConnections: true,
connectionLimit: 20,
queueLimit: 0,
timezone: '+00:00'
timezone: '+05:30' // Updated to Indian Standard Time (IST)
});
module.exports = {

View File

@ -1,7 +1,7 @@
const { RateLimiterMemory } = require('rate-limiter-flexible');
const publicLimiter = new RateLimiterMemory({
points: 100,
points: 1000,
duration: 60
});

View File

@ -1,6 +1,7 @@
const axios = require('axios');
const { formatMySQLDateTime } = require('../utils/dateHelpers');
const BASE_URL = 'http://localhost:3000';
const BASE_URL = 'http://localhost:5500';
(async () => {
try {
@ -48,13 +49,18 @@ const BASE_URL = 'http://localhost:3000';
});
console.log('Team deleted:', deleteTeamResponse.data);
// Get tomorrow's date in IST for result_time
const tomorrowInIST = new Date();
tomorrowInIST.setDate(tomorrowInIST.getDate() + 1);
const formattedDateTime = formatMySQLDateTime(tomorrowInIST);
console.log('Publishing a result...');
const publishResultResponse = await axios.post(
`${BASE_URL}/admin/results`,
{
team: 'NEW TEAM',
date: '2025-03-12',
result: '45'
result: '45',
result_time: formattedDateTime
},
{
headers: { Authorization: `Bearer ${sessionToken}` }

View File

@ -37,6 +37,12 @@ exports.login = async (accessKey, password) => {
exports.publishResult = async (data) => {
const { team, result, result_time } = data;
// Validate date format
if (!result_time || !/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/.test(result_time)) {
throw { status: 400, message: 'Result time must be in YYYY-MM-DD HH:MM:SS format.' };
}
const teams = await db.query('SELECT id FROM teams WHERE name = ?', [team.toUpperCase()]);
if (!teams.length) throw { status: 400, message: 'Team does not exist. Create team first.' };
@ -57,6 +63,7 @@ exports.getResultsByTeam = async (teamName) => {
FROM results r
JOIN teams t ON r.team_id = t.id
WHERE t.name = ?
ORDER BY r.result_time DESC
`, [teamName.toUpperCase()]);
};
@ -81,6 +88,12 @@ exports.deleteTeam = async (id) => {
exports.updateResultById = async (id, data) => {
const { team, result, result_time } = data;
// Validate date format
if (!result_time || !/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/.test(result_time)) {
throw { status: 400, message: 'Result time must be in YYYY-MM-DD HH:MM:SS format.' };
}
const teams = await db.query('SELECT id FROM teams WHERE name = ?', [team.toUpperCase()]);
if (!teams.length) throw { status: 400, message: 'Team does not exist' };

View File

@ -1,5 +1,6 @@
const db = require('../db');
const cache = require('../cache');
const { formatDate, getCurrentIndianDate } = require('../utils/dateHelpers');
exports.getResultsByTeamAndDate = async (team, date) => {
console.log(`Fetching results for team: ${team}, date: ${date}`);
@ -11,7 +12,7 @@ exports.getResultsByTeamAndDate = async (team, date) => {
}
const results = await db.query(`
SELECT r.result_time,
SELECT r.id, r.result_time,
CASE
WHEN NOW() < r.result_time THEN '-1'
ELSE r.result
@ -20,6 +21,7 @@ exports.getResultsByTeamAndDate = async (team, date) => {
FROM results r
JOIN teams t ON r.team_id = t.id
WHERE t.name = ? AND DATE(r.result_time) = ?
ORDER BY r.result_time DESC
`, [team.toUpperCase(), date]);
if (!results.length) {
@ -36,7 +38,7 @@ exports.getResultsByTeamAndDate = async (team, date) => {
};
exports.getTodayResults = async () => {
const today = new Date().toISOString().split('T')[0];
const today = getCurrentIndianDate();
const cacheKey = `today:${today}`;
console.log(`Cache key: ${cacheKey}`);
@ -47,15 +49,16 @@ exports.getTodayResults = async () => {
console.log('Cache miss. Fetching results from the database...');
const results = await db.query(`
SELECT t.name AS team, r.result_time,
SELECT r.id, t.name AS team, r.result_time,
CASE
WHEN NOW() < r.result_time THEN '-1'
ELSE r.result
END AS visible_result
FROM results r
JOIN teams t ON r.team_id = t.id
WHERE DATE(r.result_time) = ?
`, [today]);
WHERE DATE(r.result_time) = CURDATE()
ORDER BY r.result_time DESC
`);
console.log('Caching today\'s results...');
cache.set(cacheKey, results);
@ -88,6 +91,7 @@ exports.getMonthlyResults = async (team, month) => {
FROM results r
JOIN teams t ON r.team_id = t.id
WHERE t.name = ? AND DATE_FORMAT(r.result_time, '%Y-%m') = ?
ORDER BY r.result_time DESC
`, [team.toUpperCase(), month]);
return results;
@ -99,7 +103,7 @@ exports.getDailyResults = async (date) => {
}
const results = await db.query(`
SELECT t.name AS team, r.result_time,
SELECT r.id, t.name AS team, r.result_time,
CASE
WHEN NOW() < r.result_time THEN '-1'
ELSE r.result
@ -107,6 +111,7 @@ exports.getDailyResults = async (date) => {
FROM results r
JOIN teams t ON r.team_id = t.id
WHERE DATE(r.result_time) = ?
ORDER BY r.result_time DESC
`, [date]);
return results;
@ -132,6 +137,7 @@ exports.getResultsByTeam = async (team) => {
FROM results r
JOIN teams t ON r.team_id = t.id
WHERE t.name = ?
ORDER BY r.result_time DESC
`, [team.toUpperCase()]);
cache.set(cacheKey, results);

View File

@ -0,0 +1,50 @@
/**
* Helper functions for date-time handling in Indian Standard Time (IST)
*/
// Format a date to YYYY-MM-DD format in Indian timezone
const formatDate = (date) => {
const options = { timeZone: 'Asia/Kolkata' };
const dateObj = date ? new Date(date) : new Date();
const year = dateObj.toLocaleString('en-US', { year: 'numeric', ...options });
const month = dateObj.toLocaleString('en-US', { month: '2-digit', ...options });
const day = dateObj.toLocaleString('en-US', { day: '2-digit', ...options });
return `${year}-${month}-${day}`;
};
// Get current date in YYYY-MM-DD format in Indian timezone
const getCurrentIndianDate = () => {
return formatDate(new Date());
};
// Format datetime to MySQL datetime format in Indian timezone
const formatMySQLDateTime = (date) => {
const options = { timeZone: 'Asia/Kolkata' };
const dateObj = date ? new Date(date) : new Date();
const year = dateObj.toLocaleString('en-US', { year: 'numeric', ...options });
const month = dateObj.toLocaleString('en-US', { month: '2-digit', ...options });
const day = dateObj.toLocaleString('en-US', { day: '2-digit', ...options });
const hours = dateObj.toLocaleString('en-US', { hour: '2-digit', hour12: false, ...options });
const minutes = dateObj.toLocaleString('en-US', { minute: '2-digit', ...options });
const seconds = dateObj.toLocaleString('en-US', { second: '2-digit', ...options });
return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
};
// Check if a given time is in the future (Indian timezone)
const isTimeInFuture = (dateTime) => {
const now = new Date();
const options = { timeZone: 'Asia/Kolkata' };
const nowInIST = new Date(now.toLocaleString('en-US', options));
const checkTime = new Date(dateTime);
return checkTime > nowInIST;
};
module.exports = {
formatDate,
getCurrentIndianDate,
formatMySQLDateTime,
isTimeInFuture
};