Filter sites by contract dates and calculate weekly guard summary

Update `registerRoutes` to filter active sites by contract validity dates within the specified week and calculate total guards needed, assigned, and missing for the week.

Replit-Commit-Author: Agent
Replit-Commit-Session-Id: e5565357-90e1-419f-b9a8-6ee8394636df
Replit-Commit-Checkpoint-Type: intermediate_checkpoint
Replit-Commit-Screenshot-Url: https://storage.googleapis.com/screenshot-production-us-central1/6d543d2c-20b9-4ea6-93fe-70fe9b1d9f80/e5565357-90e1-419f-b9a8-6ee8394636df/uZXH8P1
This commit is contained in:
marco370 2025-10-18 07:38:01 +00:00
parent edbd1f1aae
commit c07441cd72

View File

@ -830,7 +830,11 @@ export async function registerRoutes(app: Express): Promise<Server> {
// Calcola fine settimana (weekStart + 6 giorni) // Calcola fine settimana (weekStart + 6 giorni)
const weekEndDate = format(addDays(parsedWeekStart, 6), "yyyy-MM-dd"); const weekEndDate = format(addDays(parsedWeekStart, 6), "yyyy-MM-dd");
// Ottieni tutti i siti attivi della sede // Timestamp per filtro contratti
const weekStartTimestampForContract = new Date(weekStartDate);
const weekEndTimestampForContract = new Date(weekEndDate);
// Ottieni tutti i siti attivi della sede con contratto valido nelle date della settimana
const activeSites = await db const activeSites = await db
.select() .select()
.from(sites) .from(sites)
@ -838,7 +842,11 @@ export async function registerRoutes(app: Express): Promise<Server> {
.where( .where(
and( and(
eq(sites.isActive, true), eq(sites.isActive, true),
eq(sites.location, location as any) eq(sites.location, location as any),
// Contratto deve essere valido in almeno un giorno della settimana
// contractStartDate <= weekEnd AND contractEndDate >= weekStart
lte(sites.contractStartDate, weekEndTimestampForContract),
gte(sites.contractEndDate, weekStartTimestampForContract)
) )
); );
@ -992,11 +1000,38 @@ export async function registerRoutes(app: Express): Promise<Server> {
}); });
} }
// Calcola guardie totali necessarie per l'intera settimana
let totalGuardsNeededForWeek = 0;
let totalGuardsAssignedForWeek = 0;
// Set per tracciare guardie uniche assegnate nella settimana
const uniqueGuardsInWeek = new Set<string>();
for (const day of weekData) {
for (const siteData of day.sites) {
// Somma guardie necessarie (già calcolate per sito/giorno)
totalGuardsNeededForWeek += (siteData.guardsAssigned + siteData.missingGuards);
// Traccia guardie uniche
for (const guard of siteData.guards) {
uniqueGuardsInWeek.add(guard.guardId);
}
}
}
totalGuardsAssignedForWeek = uniqueGuardsInWeek.size;
const totalGuardsMissingForWeek = Math.max(0, totalGuardsNeededForWeek - totalGuardsAssignedForWeek);
res.json({ res.json({
weekStart: weekStartDate, weekStart: weekStartDate,
weekEnd: weekEndDate, weekEnd: weekEndDate,
location, location,
days: weekData, days: weekData,
summary: {
totalGuardsNeeded: totalGuardsNeededForWeek,
totalGuardsAssigned: totalGuardsAssignedForWeek,
totalGuardsMissing: totalGuardsMissingForWeek,
}
}); });
} catch (error) { } catch (error) {
console.error("Error fetching general planning:", error); console.error("Error fetching general planning:", error);