# VigilanzaTurni - Sistema Gestione Turni per Istituti di Vigilanza ## Overview VigilanzaTurni is a professional 24/7 shift management system designed for security companies. It offers multi-role authentication (Admin, Coordinator, Guard, Client), comprehensive guard and site management, 24/7 shift planning, a live operational dashboard with KPIs, reporting for worked hours, and a notification system. The system supports multi-location operations (Roccapiemonte, Milano, Roma) managing 250+ security personnel across different branches. The project aims to streamline operations and enhance efficiency for security institutes. ## User Preferences - Interfaccia in italiano - Dark mode di default - Design operativo e funzionale (non decorativo) - Focus su efficienza e densità informativa - **Testing**: Tutti i test vengono eseguiti ESCLUSIVAMENTE sul server esterno (vt.alfacom.it) con autenticazione locale (non Replit Auth) ## System Architecture ### Stack Tecnologico - **Frontend**: React + TypeScript + Tailwind CSS + Shadcn UI - **Backend**: Express.js + TypeScript - **Database**: PostgreSQL (Neon) with Drizzle ORM - **Autenticazione**: Replit Auth (OIDC) - **State Management**: TanStack Query v5 - **Routing**: Wouter ### Design System - **Font Principale**: Inter (sans-serif) - **Font Monospace**: JetBrains Mono - **Colori**: Primary Blue, Status Green, Orange, Red, Gray for various operational states. - **Tema**: Dark mode by default, with light mode support. - **Componenti**: Shadcn UI with an operational design. ### Database Schema The database includes core tables for `users`, `guards`, `certifications`, `sites`, `shifts`, `shift_assignments`, and `notifications`. Advanced scheduling and constraints are managed via `guard_constraints`, `site_preferences`, `contract_parameters`, `training_courses`, `holidays`, `holiday_assignments`, `absences`, and `absence_affected_shifts`. All tables include appropriate foreign keys and unique constraints to maintain data integrity. **Recent Schema Updates (October 2025)**: - Service types now include specialized parameters: `fixedPostHours` (ore presidio fisso), `patrolPassages` (numero passaggi pattuglia), `inspectionFrequency` (frequenza ispezioni), `responseTimeMinutes` (tempo risposta pronto intervento) - Sites include service schedule fields: `serviceStartTime` and `serviceEndTime` (formato HH:MM) - **Contract Management**: Sites now include contract fields: `contractReference` (codice contratto), `contractStartDate`, `contractEndDate` (date validità contratto in formato YYYY-MM-DD) - Sites now reference service types via `serviceTypeId` foreign key; `shiftType` is optional and can be derived from service type - **Multi-Location Support**: Added `location` field (enum: roccapiemonte, milano, roma) to `sites`, `guards`, and `vehicles` tables for complete multi-sede resource isolation **Recent Features (October 17-18, 2025)**: - **Multi-Sede Operational Planning**: Redesigned operational planning workflow with location-first approach: 1. Select sede (Roccapiemonte/Milano/Roma) - first step with default value 2. Select date 3. View uncovered sites filtered by selected sede 4. Select site → view available resources (guards and vehicles) filtered by sede 5. Assign resources and create shift - **Location-Based Filtering**: Backend endpoints use INNER JOIN with sites table to ensure complete resource isolation between locations - guards/vehicles in one sede remain available even when assigned to shifts in other sedi - **Site Management**: Added sede selection in site creation/editing forms with visual badges showing location in site listings - **Planning Generale (October 18, 2025)**: New weekly planning overview feature showing all sites × 7 days in table format: - **Contract filtering**: Shows only sites with active contracts in the week dates (`contractStartDate <= weekEnd AND contractEndDate >= weekStart`) - Backend endpoint `/api/general-planning?weekStart=YYYY-MM-DD&location=sede` with complex joins and location filtering - Automatic missing guards calculation: `ceil(totalShiftHours / maxHoursPerGuard) × minGuards - assignedGuards` (e.g., 24h shift, 2 guards min, 9h max = 6 total needed) - **Weekly summary**: Shows total guards needed, guards assigned (counting slots, not unique people), and guards missing for the entire week - Table cells display: assigned guards with hours, vehicles, missing guards badge (if any), shift count, total hours - Interactive cells with click handler opening detail dialog - Dialog shows: shift count, total hours, guard list with hours and badge numbers, vehicle list, missing guards warning with explanation - **Direct Shift Creation from Dialog**: Users can now create multi-day shifts directly from the Planning Generale dialog: - Select guard from dropdown showing name + weekly available hours (max 45h - assigned hours) - Specify number of consecutive days (1-7) - Backend endpoint `POST /api/general-planning/shifts` with atomic transaction using `db.transaction()` - all shifts created or none (rollback on error) - Validates contract dates, site and guard existence before transaction - Automatically creates shifts spanning multiple days with correct time ranges from site service schedule - TanStack Query mutation with cache invalidation for real-time planning grid updates - "Modifica in Pianificazione Operativa" button in dialog navigates to operational planning page with pre-filled date/location parameters - Week navigation (previous/next week) with location selector - Operational planning page now supports query parameters (`?date=YYYY-MM-DD&location=sede`) for seamless integration **Recent Bug Fixes (October 17-18, 2025)**: - **Operational Planning Date Handling**: Fixed date sanitization in `/api/operational-planning/uncovered-sites` and `/api/operational-planning/availability` endpoints to handle malformed date inputs (e.g., "2025-10-17/2025-10-17"). Both endpoints now validate dates using `parseISO`/`isValid` and return 400 for invalid formats. - **Checkbox Event Propagation**: Fixed double-toggle bug in operational planning resource selection by wrapping vehicle and guard checkboxes in `