mirror of
https://github.com/itsnaveenk/bazar3.git
synced 2025-12-19 22:57:06 +00:00
timing issue fixed
This commit is contained in:
parent
a01d1d9549
commit
56e564af63
@ -17,7 +17,7 @@ http {
|
|||||||
server {
|
server {
|
||||||
server_name backend.kings.com;
|
server_name backend.kings.com;
|
||||||
location / {
|
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_http_version 1.1;
|
||||||
proxy_set_header Upgrade $http_upgrade;
|
proxy_set_header Upgrade $http_upgrade;
|
||||||
proxy_set_header Connection "upgrade";
|
proxy_set_header Connection "upgrade";
|
||||||
|
|||||||
@ -1,5 +1,6 @@
|
|||||||
const argon2 = require('argon2');
|
const argon2 = require('argon2');
|
||||||
const crypto = require('crypto');
|
const crypto = require('crypto');
|
||||||
|
const db = require('./db');
|
||||||
|
|
||||||
// Generate Admin Credentials
|
// Generate Admin Credentials
|
||||||
const createAdmin = async (password) => {
|
const createAdmin = async (password) => {
|
||||||
|
|||||||
@ -14,7 +14,7 @@ const pool = mysql.createPool({
|
|||||||
waitForConnections: true,
|
waitForConnections: true,
|
||||||
connectionLimit: 20,
|
connectionLimit: 20,
|
||||||
queueLimit: 0,
|
queueLimit: 0,
|
||||||
timezone: '+00:00'
|
timezone: '+05:30' // Updated to Indian Standard Time (IST)
|
||||||
});
|
});
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
|
|||||||
@ -1,7 +1,7 @@
|
|||||||
const { RateLimiterMemory } = require('rate-limiter-flexible');
|
const { RateLimiterMemory } = require('rate-limiter-flexible');
|
||||||
|
|
||||||
const publicLimiter = new RateLimiterMemory({
|
const publicLimiter = new RateLimiterMemory({
|
||||||
points: 100,
|
points: 1000,
|
||||||
duration: 60
|
duration: 60
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@ -1,6 +1,7 @@
|
|||||||
const axios = require('axios');
|
const axios = require('axios');
|
||||||
|
const { formatMySQLDateTime } = require('../utils/dateHelpers');
|
||||||
|
|
||||||
const BASE_URL = 'http://localhost:3000';
|
const BASE_URL = 'http://localhost:5500';
|
||||||
|
|
||||||
(async () => {
|
(async () => {
|
||||||
try {
|
try {
|
||||||
@ -48,13 +49,18 @@ const BASE_URL = 'http://localhost:3000';
|
|||||||
});
|
});
|
||||||
console.log('Team deleted:', deleteTeamResponse.data);
|
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...');
|
console.log('Publishing a result...');
|
||||||
const publishResultResponse = await axios.post(
|
const publishResultResponse = await axios.post(
|
||||||
`${BASE_URL}/admin/results`,
|
`${BASE_URL}/admin/results`,
|
||||||
{
|
{
|
||||||
team: 'NEW TEAM',
|
team: 'NEW TEAM',
|
||||||
date: '2025-03-12',
|
result: '45',
|
||||||
result: '45'
|
result_time: formattedDateTime
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
headers: { Authorization: `Bearer ${sessionToken}` }
|
headers: { Authorization: `Bearer ${sessionToken}` }
|
||||||
|
|||||||
@ -37,6 +37,12 @@ exports.login = async (accessKey, password) => {
|
|||||||
|
|
||||||
exports.publishResult = async (data) => {
|
exports.publishResult = async (data) => {
|
||||||
const { team, result, result_time } = 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()]);
|
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.' };
|
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
|
FROM results r
|
||||||
JOIN teams t ON r.team_id = t.id
|
JOIN teams t ON r.team_id = t.id
|
||||||
WHERE t.name = ?
|
WHERE t.name = ?
|
||||||
|
ORDER BY r.result_time DESC
|
||||||
`, [teamName.toUpperCase()]);
|
`, [teamName.toUpperCase()]);
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -81,6 +88,12 @@ exports.deleteTeam = async (id) => {
|
|||||||
|
|
||||||
exports.updateResultById = async (id, data) => {
|
exports.updateResultById = async (id, data) => {
|
||||||
const { team, result, result_time } = 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()]);
|
const teams = await db.query('SELECT id FROM teams WHERE name = ?', [team.toUpperCase()]);
|
||||||
if (!teams.length) throw { status: 400, message: 'Team does not exist' };
|
if (!teams.length) throw { status: 400, message: 'Team does not exist' };
|
||||||
|
|
||||||
|
|||||||
@ -1,5 +1,6 @@
|
|||||||
const db = require('../db');
|
const db = require('../db');
|
||||||
const cache = require('../cache');
|
const cache = require('../cache');
|
||||||
|
const { formatDate, getCurrentIndianDate } = require('../utils/dateHelpers');
|
||||||
|
|
||||||
exports.getResultsByTeamAndDate = async (team, date) => {
|
exports.getResultsByTeamAndDate = async (team, date) => {
|
||||||
console.log(`Fetching results for team: ${team}, date: ${date}`);
|
console.log(`Fetching results for team: ${team}, date: ${date}`);
|
||||||
@ -11,7 +12,7 @@ exports.getResultsByTeamAndDate = async (team, date) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const results = await db.query(`
|
const results = await db.query(`
|
||||||
SELECT r.result_time,
|
SELECT r.id, r.result_time,
|
||||||
CASE
|
CASE
|
||||||
WHEN NOW() < r.result_time THEN '-1'
|
WHEN NOW() < r.result_time THEN '-1'
|
||||||
ELSE r.result
|
ELSE r.result
|
||||||
@ -20,6 +21,7 @@ exports.getResultsByTeamAndDate = async (team, date) => {
|
|||||||
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(r.result_time) = ?
|
WHERE t.name = ? AND DATE(r.result_time) = ?
|
||||||
|
ORDER BY r.result_time DESC
|
||||||
`, [team.toUpperCase(), date]);
|
`, [team.toUpperCase(), date]);
|
||||||
|
|
||||||
if (!results.length) {
|
if (!results.length) {
|
||||||
@ -36,7 +38,7 @@ exports.getResultsByTeamAndDate = async (team, date) => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
exports.getTodayResults = async () => {
|
exports.getTodayResults = async () => {
|
||||||
const today = new Date().toISOString().split('T')[0];
|
const today = getCurrentIndianDate();
|
||||||
const cacheKey = `today:${today}`;
|
const cacheKey = `today:${today}`;
|
||||||
|
|
||||||
console.log(`Cache key: ${cacheKey}`);
|
console.log(`Cache key: ${cacheKey}`);
|
||||||
@ -47,15 +49,16 @@ exports.getTodayResults = async () => {
|
|||||||
|
|
||||||
console.log('Cache miss. Fetching results from the database...');
|
console.log('Cache miss. Fetching results from the database...');
|
||||||
const results = await db.query(`
|
const results = await db.query(`
|
||||||
SELECT t.name AS team, r.result_time,
|
SELECT r.id, t.name AS team, r.result_time,
|
||||||
CASE
|
CASE
|
||||||
WHEN NOW() < r.result_time THEN '-1'
|
WHEN NOW() < r.result_time THEN '-1'
|
||||||
ELSE r.result
|
ELSE r.result
|
||||||
END AS visible_result
|
END AS visible_result
|
||||||
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 DATE(r.result_time) = ?
|
WHERE DATE(r.result_time) = CURDATE()
|
||||||
`, [today]);
|
ORDER BY r.result_time DESC
|
||||||
|
`);
|
||||||
|
|
||||||
console.log('Caching today\'s results...');
|
console.log('Caching today\'s results...');
|
||||||
cache.set(cacheKey, results);
|
cache.set(cacheKey, results);
|
||||||
@ -88,6 +91,7 @@ exports.getMonthlyResults = async (team, month) => {
|
|||||||
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_time, '%Y-%m') = ?
|
WHERE t.name = ? AND DATE_FORMAT(r.result_time, '%Y-%m') = ?
|
||||||
|
ORDER BY r.result_time DESC
|
||||||
`, [team.toUpperCase(), month]);
|
`, [team.toUpperCase(), month]);
|
||||||
|
|
||||||
return results;
|
return results;
|
||||||
@ -99,7 +103,7 @@ exports.getDailyResults = async (date) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const results = await db.query(`
|
const results = await db.query(`
|
||||||
SELECT t.name AS team, r.result_time,
|
SELECT r.id, t.name AS team, r.result_time,
|
||||||
CASE
|
CASE
|
||||||
WHEN NOW() < r.result_time THEN '-1'
|
WHEN NOW() < r.result_time THEN '-1'
|
||||||
ELSE r.result
|
ELSE r.result
|
||||||
@ -107,6 +111,7 @@ exports.getDailyResults = async (date) => {
|
|||||||
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 DATE(r.result_time) = ?
|
WHERE DATE(r.result_time) = ?
|
||||||
|
ORDER BY r.result_time DESC
|
||||||
`, [date]);
|
`, [date]);
|
||||||
|
|
||||||
return results;
|
return results;
|
||||||
@ -132,6 +137,7 @@ exports.getResultsByTeam = async (team) => {
|
|||||||
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 = ?
|
WHERE t.name = ?
|
||||||
|
ORDER BY r.result_time DESC
|
||||||
`, [team.toUpperCase()]);
|
`, [team.toUpperCase()]);
|
||||||
|
|
||||||
cache.set(cacheKey, results);
|
cache.set(cacheKey, results);
|
||||||
|
|||||||
50
server/utils/dateHelpers.js
Normal file
50
server/utils/dateHelpers.js
Normal 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
|
||||||
|
};
|
||||||
Loading…
x
Reference in New Issue
Block a user