Files
LogiFlow/client/src/lib/dateUtils.ts
T
Claude 4220131432 fix(types): bring real code to zero TypeScript errors, fix three latent bugs
Continues the cleanup from 164 errors down to 88, all of which now sit in
MemStorage — the in-memory dev mock — documented as known debt. Production
server code and the entire client are at zero errors.

Three genuine defects surfaced by the types and fixed:
- deleteAnnouncement returned void while routes check the result and answer
  404 "not found" on falsy: deleting an announcement succeeded in DB but the
  API reported failure on the fallback paths; it now returns a real boolean
- AnnouncementMemoryStorage declared getAnnouncement twice; the first
  implementation was dead at runtime (second definition wins) and is removed
- one route called storage.getDeliveryById(), a method that does not exist,
  crashing with a TypeError whenever hit; it now calls getDelivery()

Everything else is type-level only (annotations, null-to-undefined for
inline styles, honest signatures for markClientCalled's comment parameter
and the invoice verification result's invoiceAmountTTC field), verified
behavior-neutral: client build, production server bundle and the SMTP
end-to-end test all pass unchanged.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01FHdE9nEh8zHhQNGpCgrtYe
2026-08-14 16:42:24 +00:00

71 lines
1.8 KiB
TypeScript

import { format, type Locale } from "date-fns";
import { fr } from "date-fns/locale";
/**
* Formate une date de façon sécurisée pour éviter "Invalid time value"
* Retourne une chaîne par défaut si la date est invalide
*/
export function safeFormat(
date: string | Date | null | undefined,
formatStr: string = "dd/MM/yyyy",
options: { locale?: Locale; defaultValue?: string } = {}
): string {
const { locale = fr, defaultValue = "Date non disponible" } = options;
if (!date) {
return defaultValue;
}
try {
const dateObj = typeof date === 'string' ? new Date(date) : date;
// Vérifier si la date est valide
if (isNaN(dateObj.getTime())) {
return defaultValue;
}
return format(dateObj, formatStr, { locale });
} catch (error) {
console.warn('Date formatting error:', error, 'for date:', date);
return defaultValue;
}
}
/**
* Crée un objet Date de façon sécurisée
* Retourne null si la date est invalide
*/
export function safeDate(date: string | Date | null | undefined): Date | null {
if (!date) {
return null;
}
try {
const dateObj = typeof date === 'string' ? new Date(date) : date;
// Vérifier si la date est valide
if (isNaN(dateObj.getTime())) {
return null;
}
return dateObj;
} catch (error) {
console.warn('Date creation error:', error, 'for date:', date);
return null;
}
}
/**
* Compare deux dates de façon sécurisée
* Retourne 0 si l'une des dates est invalide
*/
export function safeCompareDate(a: string | Date | null | undefined, b: string | Date | null | undefined): number {
const dateA = safeDate(a);
const dateB = safeDate(b);
if (!dateA || !dateB) {
return 0;
}
return dateA.getTime() - dateB.getTime();
}