fix: Corregir filtro de fechas en auditoría por zona horaria
- El filtro usaba hora local que se convertía incorrectamente a UTC - Los registros de 'hoy' aparecían en 'ayer' por desfase horario - Ahora trabaja directamente en UTC para consistencia - Afecta todos los filtros: today, yesterday, last7days, last30days - Custom dates también parseados correctamente como UTC
This commit is contained in:
@@ -43,51 +43,49 @@
|
|||||||
* Obtener fechas según el período seleccionado
|
* Obtener fechas según el período seleccionado
|
||||||
*/
|
*/
|
||||||
function getDateRangeForPeriod(): { from: string; to: string } {
|
function getDateRangeForPeriod(): { from: string; to: string } {
|
||||||
const today = new Date();
|
// Trabajar en UTC para evitar problemas de zona horaria
|
||||||
today.setHours(0, 0, 0, 0);
|
const now = new Date();
|
||||||
|
|
||||||
let from: Date;
|
let from: Date;
|
||||||
let to: Date = new Date();
|
let to: Date;
|
||||||
|
|
||||||
switch (periodFilter) {
|
switch (periodFilter) {
|
||||||
case 'today':
|
case 'today':
|
||||||
from = new Date(today);
|
// Hoy desde las 00:00:00 hasta ahora en UTC
|
||||||
to = new Date();
|
from = new Date(Date.UTC(now.getUTCFullYear(), now.getUTCMonth(), now.getUTCDate(), 0, 0, 0));
|
||||||
to.setHours(23, 59, 59, 999);
|
to = new Date(); // Ahora en UTC
|
||||||
break;
|
break;
|
||||||
case 'yesterday':
|
case 'yesterday':
|
||||||
from = new Date(today);
|
// Ayer completo en UTC
|
||||||
from.setDate(from.getDate() - 1);
|
from = new Date(Date.UTC(now.getUTCFullYear(), now.getUTCMonth(), now.getUTCDate() - 1, 0, 0, 0));
|
||||||
to = new Date(today);
|
to = new Date(Date.UTC(now.getUTCFullYear(), now.getUTCMonth(), now.getUTCDate(), 0, 0, 0));
|
||||||
to.setSeconds(to.getSeconds() - 1);
|
|
||||||
break;
|
break;
|
||||||
case 'last7days':
|
case 'last7days':
|
||||||
from = new Date(today);
|
// Últimos 7 días en UTC
|
||||||
from.setDate(from.getDate() - 7);
|
from = new Date(Date.UTC(now.getUTCFullYear(), now.getUTCMonth(), now.getUTCDate() - 7, 0, 0, 0));
|
||||||
to = new Date();
|
to = new Date();
|
||||||
to.setHours(23, 59, 59, 999);
|
|
||||||
break;
|
break;
|
||||||
case 'last30days':
|
case 'last30days':
|
||||||
from = new Date(today);
|
// Últimos 30 días en UTC
|
||||||
from.setDate(from.getDate() - 30);
|
from = new Date(Date.UTC(now.getUTCFullYear(), now.getUTCMonth(), now.getUTCDate() - 30, 0, 0, 0));
|
||||||
to = new Date();
|
to = new Date();
|
||||||
to.setHours(23, 59, 59, 999);
|
|
||||||
break;
|
break;
|
||||||
case 'custom':
|
case 'custom':
|
||||||
// Para fechas custom, parsear las fechas string y agregar hora
|
// Para fechas custom, parsear como UTC
|
||||||
if (!customDateFrom || !customDateTo) {
|
if (!customDateFrom || !customDateTo) {
|
||||||
return { from: '', to: '' };
|
return { from: '', to: '' };
|
||||||
}
|
}
|
||||||
const fromDate = new Date(customDateFrom);
|
const fromParts = customDateFrom.split('-').map(Number);
|
||||||
fromDate.setHours(0, 0, 0, 0);
|
const toParts = customDateTo.split('-').map(Number);
|
||||||
const toDate = new Date(customDateTo);
|
from = new Date(Date.UTC(fromParts[0], fromParts[1] - 1, fromParts[2], 0, 0, 0));
|
||||||
toDate.setHours(23, 59, 59, 999);
|
to = new Date(Date.UTC(toParts[0], toParts[1] - 1, toParts[2], 23, 59, 59));
|
||||||
return {
|
return {
|
||||||
from: fromDate.toISOString(),
|
from: from.toISOString(),
|
||||||
to: toDate.toISOString()
|
to: to.toISOString()
|
||||||
};
|
};
|
||||||
default:
|
default:
|
||||||
from = new Date(today);
|
from = new Date(Date.UTC(now.getUTCFullYear(), now.getUTCMonth(), now.getUTCDate(), 0, 0, 0));
|
||||||
|
to = new Date();
|
||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
|||||||
Reference in New Issue
Block a user