feat: add workday utility functions
- Introduced utility functions for workday calculations in `workday-utils.ts`, including `getPreviousWorkday`, `getNextWorkday`, `isWorkday`, and `getDayName`. - Updated `DailyService` and `DailyPageClient` to utilize `getPreviousWorkday` for accurate date handling instead of simple date subtraction.
This commit is contained in:
82
lib/workday-utils.ts
Normal file
82
lib/workday-utils.ts
Normal file
@@ -0,0 +1,82 @@
|
||||
/**
|
||||
* Utilitaires pour la gestion des jours de travail
|
||||
* Logique : Lundi-Vendredi sont les jours de travail
|
||||
*/
|
||||
|
||||
/**
|
||||
* Calcule le jour de travail précédent selon la logique métier :
|
||||
* - Lundi → Vendredi (au lieu de Dimanche)
|
||||
* - Mardi-Vendredi → jour précédent
|
||||
* - Samedi → Vendredi
|
||||
* - Dimanche → Vendredi
|
||||
*/
|
||||
export function getPreviousWorkday(date: Date): Date {
|
||||
const result = new Date(date);
|
||||
result.setHours(0, 0, 0, 0);
|
||||
|
||||
const dayOfWeek = result.getDay(); // 0 = Dimanche, 1 = Lundi, ..., 6 = Samedi
|
||||
|
||||
switch (dayOfWeek) {
|
||||
case 1: // Lundi → Vendredi précédent
|
||||
result.setDate(result.getDate() - 3);
|
||||
break;
|
||||
case 0: // Dimanche → Vendredi précédent
|
||||
result.setDate(result.getDate() - 2);
|
||||
break;
|
||||
case 6: // Samedi → Vendredi précédent
|
||||
result.setDate(result.getDate() - 1);
|
||||
break;
|
||||
default: // Mardi-Vendredi → jour précédent
|
||||
result.setDate(result.getDate() - 1);
|
||||
break;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Calcule le jour de travail suivant selon la logique métier :
|
||||
* - Vendredi → Lundi suivant
|
||||
* - Samedi → Lundi suivant
|
||||
* - Dimanche → Lundi suivant
|
||||
* - Lundi-Jeudi → jour suivant
|
||||
*/
|
||||
export function getNextWorkday(date: Date): Date {
|
||||
const result = new Date(date);
|
||||
result.setHours(0, 0, 0, 0);
|
||||
|
||||
const dayOfWeek = result.getDay(); // 0 = Dimanche, 1 = Lundi, ..., 6 = Samedi
|
||||
|
||||
switch (dayOfWeek) {
|
||||
case 5: // Vendredi → Lundi suivant
|
||||
result.setDate(result.getDate() + 3);
|
||||
break;
|
||||
case 6: // Samedi → Lundi suivant
|
||||
result.setDate(result.getDate() + 2);
|
||||
break;
|
||||
case 0: // Dimanche → Lundi suivant
|
||||
result.setDate(result.getDate() + 1);
|
||||
break;
|
||||
default: // Lundi-Jeudi → jour suivant
|
||||
result.setDate(result.getDate() + 1);
|
||||
break;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Vérifie si une date est un jour de travail (Lundi-Vendredi)
|
||||
*/
|
||||
export function isWorkday(date: Date): boolean {
|
||||
const dayOfWeek = date.getDay();
|
||||
return dayOfWeek >= 1 && dayOfWeek <= 5; // Lundi (1) à Vendredi (5)
|
||||
}
|
||||
|
||||
/**
|
||||
* Retourne le nom du jour en français
|
||||
*/
|
||||
export function getDayName(date: Date): string {
|
||||
const days = ['Dimanche', 'Lundi', 'Mardi', 'Mercredi', 'Jeudi', 'Vendredi', 'Samedi'];
|
||||
return days[date.getDay()];
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
import { prisma } from './database';
|
||||
import { Prisma } from '@prisma/client';
|
||||
import { DailyCheckbox, DailyView, CreateDailyCheckboxData, UpdateDailyCheckboxData, BusinessError, DailyCheckboxType, TaskStatus, TaskPriority, TaskSource } from '@/lib/types';
|
||||
import { getPreviousWorkday } from '@/lib/workday-utils';
|
||||
|
||||
/**
|
||||
* Service pour la gestion des checkboxes daily
|
||||
@@ -15,8 +16,8 @@ export class DailyService {
|
||||
const today = new Date(date);
|
||||
today.setHours(0, 0, 0, 0);
|
||||
|
||||
const yesterday = new Date(today);
|
||||
yesterday.setDate(yesterday.getDate() - 1);
|
||||
// Utiliser la logique de jour de travail précédent au lieu de jour-1
|
||||
const yesterday = getPreviousWorkday(today);
|
||||
|
||||
// Récupérer les checkboxes des deux jours
|
||||
const [yesterdayCheckboxes, todayCheckboxes] = await Promise.all([
|
||||
|
||||
@@ -10,6 +10,7 @@ import { DailyCalendar } from '@/components/daily/DailyCalendar';
|
||||
import { DailySection } from '@/components/daily/DailySection';
|
||||
import { dailyClient } from '@/clients/daily-client';
|
||||
import { Header } from '@/components/ui/Header';
|
||||
import { getPreviousWorkday } from '@/lib/workday-utils';
|
||||
|
||||
interface DailyPageClientProps {
|
||||
initialDailyView?: DailyView;
|
||||
@@ -99,9 +100,7 @@ export function DailyPageClient({
|
||||
};
|
||||
|
||||
const getYesterdayDate = () => {
|
||||
const yesterday = new Date(currentDate);
|
||||
yesterday.setDate(yesterday.getDate() - 1);
|
||||
return yesterday;
|
||||
return getPreviousWorkday(currentDate);
|
||||
};
|
||||
|
||||
const getTodayDate = () => {
|
||||
|
||||
Reference in New Issue
Block a user