refactor: unify date handling with utility functions

- Replaced direct date manipulations with utility functions like `getToday`, `parseDate`, and `createDateFromParts` across various components and services for consistency.
- Updated date initialization in `JiraAnalyticsService`, `BackupService`, and `DailyClient` to improve clarity and maintainability.
- Enhanced date parsing in forms and API routes to ensure proper handling of date strings.
This commit is contained in:
Julien Froidefond
2025-09-21 13:04:34 +02:00
parent c3c1d24fa2
commit 4ba6ba2c0b
23 changed files with 117 additions and 68 deletions

View File

@@ -1,5 +1,6 @@
import { Task, TaskStatus, TaskPriority, TaskSource } from '@/lib/types';
import { prisma } from './database';
import { getToday, parseDate, subtractDays, addDays } from '@/lib/date-utils';
export interface ProductivityMetrics {
completionTrend: Array<{
@@ -42,8 +43,8 @@ export class AnalyticsService {
*/
static async getProductivityMetrics(timeRange?: TimeRange): Promise<ProductivityMetrics> {
try {
const now = new Date();
const defaultStart = new Date(now.getTime() - 30 * 24 * 60 * 60 * 1000); // 30 jours
const now = getToday();
const defaultStart = subtractDays(now, 30); // 30 jours
const start = timeRange?.start || defaultStart;
const end = timeRange?.end || now;
@@ -99,7 +100,7 @@ export class AnalyticsService {
const trend: Array<{ date: string; completed: number; created: number; total: number }> = [];
// Générer les dates pour la période
const currentDate = new Date(start);
const currentDate = new Date(start.getTime());
while (currentDate <= end) {
const dateStr = currentDate.toISOString().split('T')[0];
@@ -116,7 +117,7 @@ export class AnalyticsService {
// Total cumulé jusqu'à ce jour
const totalUntilThisDay = tasks.filter(task =>
new Date(task.createdAt) <= currentDate
task.createdAt <= currentDate
).length;
trend.push({
@@ -156,7 +157,7 @@ export class AnalyticsService {
// Convertir en format pour le graphique
weekGroups.forEach((count, weekKey) => {
const weekDate = new Date(weekKey);
const weekDate = parseDate(weekKey);
weeklyData.push({
week: `Sem. ${this.getWeekNumber(weekDate)}`,
completed: count,
@@ -209,10 +210,10 @@ export class AnalyticsService {
* Calcule les statistiques hebdomadaires
*/
private static calculateWeeklyStats(tasks: Task[]) {
const now = new Date();
const now = getToday();
const thisWeekStart = this.getWeekStart(now);
const lastWeekStart = new Date(thisWeekStart.getTime() - 7 * 24 * 60 * 60 * 1000);
const lastWeekEnd = new Date(thisWeekStart.getTime() - 1);
const lastWeekStart = subtractDays(thisWeekStart, 7);
const lastWeekEnd = subtractDays(thisWeekStart, 1);
const thisWeekCompleted = tasks.filter(task =>
task.completedAt &&
@@ -243,7 +244,7 @@ export class AnalyticsService {
* Obtient le début de la semaine pour une date
*/
private static getWeekStart(date: Date): Date {
const d = new Date(date);
const d = new Date(date.getTime());
const day = d.getDay();
const diff = d.getDate() - day + (day === 0 ? -6 : 1); // Lundi = début de semaine
return new Date(d.setDate(diff));