feat: enhance ata management by adding dataProrrogacao field and updating related logic for effective date handling, improving data integrity and user experience in pedidos

This commit is contained in:
2025-12-17 10:39:33 -03:00
parent fbf00c824e
commit 9072619e26
8 changed files with 390 additions and 115 deletions

View File

@@ -7,10 +7,10 @@
* Converte uma string de data no formato YYYY-MM-DD para um objeto Date local
* No ambiente Convex, as datas são tratadas como UTC, então precisamos garantir
* que a data seja interpretada corretamente.
*
*
* @param dateString - String no formato YYYY-MM-DD
* @returns Date objeto representando a data
*
*
* @example
* parseLocalDate('2024-01-15') // Retorna Date para 15/01/2024
*/
@@ -42,13 +42,13 @@ export function parseLocalDate(dateString: string): Date {
/**
* Formata uma data para o formato brasileiro (DD/MM/YYYY)
*
*
* @param date - Date objeto ou string no formato YYYY-MM-DD
* @returns String formatada no formato DD/MM/YYYY
*/
export function formatarDataBR(date: Date | string): string {
let dateObj: Date;
if (typeof date === 'string') {
dateObj = parseLocalDate(date);
} else {
@@ -63,3 +63,69 @@ export function formatarDataBR(date: Date | string): string {
return `${day}/${month}/${year}`;
}
function pad2(n: number): string {
return String(n).padStart(2, '0');
}
/**
* Retorna a data atual (UTC) no formato YYYY-MM-DD.
* Útil para comparações lexicográficas com strings YYYY-MM-DD persistidas no banco.
*/
export function getTodayYMD(): string {
const now = new Date();
const y = now.getUTCFullYear();
const m = now.getUTCMonth() + 1;
const d = now.getUTCDate();
return `${y}-${pad2(m)}-${pad2(d)}`;
}
/**
* Soma meses (calendário) a uma data YYYY-MM-DD, mantendo o dia quando possível,
* e fazendo clamp para o último dia do mês quando necessário.
*
* Ex.: 2025-03-31 + (-1) mês => 2025-02-28 (ou 29 em ano bissexto)
*/
export function addMonthsClampedYMD(dateString: string, deltaMonths: number): string {
const base = parseLocalDate(dateString); // UTC midnight
const year = base.getUTCFullYear();
const monthIndex = base.getUTCMonth(); // 0..11
const day = base.getUTCDate();
const totalMonths = monthIndex + deltaMonths;
const newYear = year + Math.floor(totalMonths / 12);
let newMonthIndex = totalMonths % 12;
if (newMonthIndex < 0) {
newMonthIndex += 12;
}
// Último dia do mês alvo
const lastDay = new Date(Date.UTC(newYear, newMonthIndex + 1, 0)).getUTCDate();
const newDay = Math.min(day, lastDay);
return `${newYear}-${pad2(newMonthIndex + 1)}-${pad2(newDay)}`;
}
/**
* Retorna o maior (mais recente) entre duas datas YYYY-MM-DD (lexicograficamente).
* Se uma delas for null/undefined, retorna a outra.
*/
export function maxYMD(a?: string | null, b?: string | null): string | null {
if (!a && !b) return null;
if (!a) return b ?? null;
if (!b) return a;
return a >= b ? a : b;
}
/**
* Checa se `date` está dentro do intervalo [inicio..fim], onde
* `inicio` e `fim` são YYYY-MM-DD (ou null para aberto).
*/
export function isWithinRangeYMD(
date: string,
inicio?: string | null,
fim?: string | null
): boolean {
const start = inicio ?? '0000-01-01';
const end = fim ?? '9999-12-31';
return start <= date && date <= end;
}