diff --git a/.replit b/.replit index 90b1d94..b6a9521 100644 --- a/.replit +++ b/.replit @@ -39,6 +39,10 @@ externalPort = 5000 localPort = 43267 externalPort = 3003 +[[ports]] +localPort = 44575 +externalPort = 5173 + [env] PORT = "5000" diff --git a/server/routes.ts b/server/routes.ts index bcc19b1..48c0ea6 100644 --- a/server/routes.ts +++ b/server/routes.ts @@ -1326,6 +1326,7 @@ export async function registerRoutes(app: Express): Promise { .from(shiftAssignments) .where(eq(shiftAssignments.guardId, guard.id)); + // Check for time overlaps for (const existing of existingAssignments) { const hasOverlap = plannedStart < existing.plannedEndTime && @@ -1338,6 +1339,34 @@ export async function registerRoutes(app: Express): Promise { } } + // CCNL: Check daily hour limit (max 9h/day) + const maxDailyHours = 9; + let dailyHoursAlreadyAssigned = 0; + + for (const existing of existingAssignments) { + // Check if assignment is on the same day + const existingDate = new Date(existing.plannedStartTime); + if ( + existingDate.getUTCFullYear() === actualYear && + existingDate.getUTCMonth() === actualMonth && + existingDate.getUTCDate() === actualDay + ) { + const assignmentHours = differenceInHours( + existing.plannedEndTime, + existing.plannedStartTime + ); + dailyHoursAlreadyAssigned += assignmentHours; + } + } + + // Check if new assignment would exceed daily limit + if (dailyHoursAlreadyAssigned + durationHours > maxDailyHours) { + throw new Error( + `Limite giornaliero superato: la guardia ha giĆ  ${dailyHoursAlreadyAssigned}h assegnate il ${shiftDate.toLocaleDateString('it-IT')}. ` + + `Aggiungendo ${durationHours}h si supererebbero le ${maxDailyHours}h massime giornaliere (CCNL).` + ); + } + // Create assignment for this day const [assignment] = await tx.insert(shiftAssignments).values({ shiftId: shift.id,