From 56e564af633940d83868187956aae296cfdfb1ee Mon Sep 17 00:00:00 2001 From: Naveen Kumar Date: Fri, 21 Mar 2025 00:59:45 +0530 Subject: [PATCH] timing issue fixed --- nginx.conf | 2 +- server/auth.js | 1 + server/db.js | 2 +- server/middlewares/rateLimit.js | 2 +- server/scripts/test-api.js | 12 ++++++-- server/services/adminService.js | 13 +++++++++ server/services/resultService.js | 18 ++++++++---- server/utils/dateHelpers.js | 50 ++++++++++++++++++++++++++++++++ 8 files changed, 88 insertions(+), 12 deletions(-) create mode 100644 server/utils/dateHelpers.js diff --git a/nginx.conf b/nginx.conf index 343db5a..0cf17bd 100644 --- a/nginx.conf +++ b/nginx.conf @@ -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"; diff --git a/server/auth.js b/server/auth.js index 0d7a8c1..6b59719 100644 --- a/server/auth.js +++ b/server/auth.js @@ -1,5 +1,6 @@ const argon2 = require('argon2'); const crypto = require('crypto'); +const db = require('./db'); // Generate Admin Credentials const createAdmin = async (password) => { diff --git a/server/db.js b/server/db.js index a8e86e7..19840fd 100644 --- a/server/db.js +++ b/server/db.js @@ -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 = { diff --git a/server/middlewares/rateLimit.js b/server/middlewares/rateLimit.js index 193ed9e..8c495de 100644 --- a/server/middlewares/rateLimit.js +++ b/server/middlewares/rateLimit.js @@ -1,7 +1,7 @@ const { RateLimiterMemory } = require('rate-limiter-flexible'); const publicLimiter = new RateLimiterMemory({ - points: 100, + points: 1000, duration: 60 }); diff --git a/server/scripts/test-api.js b/server/scripts/test-api.js index 099481e..a38cb79 100644 --- a/server/scripts/test-api.js +++ b/server/scripts/test-api.js @@ -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}` } diff --git a/server/services/adminService.js b/server/services/adminService.js index 4334795..ddc9aae 100644 --- a/server/services/adminService.js +++ b/server/services/adminService.js @@ -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' }; diff --git a/server/services/resultService.js b/server/services/resultService.js index 379e5ff..ffb3000 100644 --- a/server/services/resultService.js +++ b/server/services/resultService.js @@ -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); diff --git a/server/utils/dateHelpers.js b/server/utils/dateHelpers.js new file mode 100644 index 0000000..9ea8b00 --- /dev/null +++ b/server/utils/dateHelpers.js @@ -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 +};