Compare commits

..

36 Commits

Author SHA1 Message Date
Marco Lanzara
591687b000 🚀 Release v1.1.1
- Tipo: patch
- Database backup: database-backups/vigilanzaturni_v1.1.1_20251115_101122.sql.gz
- Data: 2025-11-15 10:11:44
2025-11-15 10:11:44 +00:00
marco370
ee5d1aaa24 Show all guards in planning, marking those already busy
Update the general planning view to display all guards, regardless of their current availability or contractual compatibility. Guards who are already assigned to a shift are now visually indicated in red but remain selectable for alternative shift assignments. This change involves modifying the guard filtering logic and adding visual cues for busy guards in the selection interface.

Replit-Commit-Author: Agent
Replit-Commit-Session-Id: e0b5b11c-5b75-4389-8ea9-5f3cd9332f88
Replit-Commit-Checkpoint-Type: full_checkpoint
Replit-Commit-Screenshot-Url: https://storage.googleapis.com/screenshot-production-us-central1/6d543d2c-20b9-4ea6-93fe-70fe9b1d9f80/e0b5b11c-5b75-4389-8ea9-5f3cd9332f88/HjUaTbs
2025-10-29 09:56:33 +00:00
marco370
fc6f5a39f8 Fix issue preventing patrol route saving due to conflicting shifts
Modify shift conflict checking logic to properly handle date comparisons by including 'less than' operator.

Replit-Commit-Author: Agent
Replit-Commit-Session-Id: e0b5b11c-5b75-4389-8ea9-5f3cd9332f88
Replit-Commit-Checkpoint-Type: full_checkpoint
Replit-Commit-Screenshot-Url: https://storage.googleapis.com/screenshot-production-us-central1/6d543d2c-20b9-4ea6-93fe-70fe9b1d9f80/e0b5b11c-5b75-4389-8ea9-5f3cd9332f88/p38S9Gi
2025-10-29 09:31:44 +00:00
marco370
758a697447 Add detailed validation and conflict resolution for patrol shifts
Implement new API endpoint `/api/patrol-routes/check-overlaps` for shift conflict detection, including fixed posts, other mobile routes, and weekly hour compliance. Introduce a new "force-save" dialog for manual conflict overrides and enhance the patrol route duplication feature to support multi-day operations with overlap validation.

Replit-Commit-Author: Agent
Replit-Commit-Session-Id: e0b5b11c-5b75-4389-8ea9-5f3cd9332f88
Replit-Commit-Checkpoint-Type: full_checkpoint
Replit-Commit-Screenshot-Url: https://storage.googleapis.com/screenshot-production-us-central1/6d543d2c-20b9-4ea6-93fe-70fe9b1d9f80/e0b5b11c-5b75-4389-8ea9-5f3cd9332f88/p38S9Gi
2025-10-29 09:24:38 +00:00
marco370
40cde1634b Improve link testing by normalizing URL slugs in sidebar
Update `data-testid` attributes in `AppSidebar` component to use a normalized slug representation of menu item titles, ensuring more robust end-to-end testing by replacing spaces with hyphens. Also, add a new section describing the "Guardie Settimanale" feature in the `replit.md` documentation.

Replit-Commit-Author: Agent
Replit-Commit-Session-Id: e0b5b11c-5b75-4389-8ea9-5f3cd9332f88
Replit-Commit-Checkpoint-Type: full_checkpoint
Replit-Commit-Screenshot-Url: https://storage.googleapis.com/screenshot-production-us-central1/6d543d2c-20b9-4ea6-93fe-70fe9b1d9f80/e0b5b11c-5b75-4389-8ea9-5f3cd9332f88/EoraZGH
2025-10-29 08:52:43 +00:00
marco370
7b05c8cbce Add a weekly guard schedule overview page
Add a new page to display a summarized weekly schedule for guards, divided by location, with detailed shift information accessible on click. Includes error handling for data fetching.

Replit-Commit-Author: Agent
Replit-Commit-Session-Id: e0b5b11c-5b75-4389-8ea9-5f3cd9332f88
Replit-Commit-Checkpoint-Type: intermediate_checkpoint
Replit-Commit-Screenshot-Url: https://storage.googleapis.com/screenshot-production-us-central1/6d543d2c-20b9-4ea6-93fe-70fe9b1d9f80/e0b5b11c-5b75-4389-8ea9-5f3cd9332f88/EPTvOHB
2025-10-29 08:30:30 +00:00
marco370
d868c6ee31 Add weekly guard schedule view with shift details
Implements the "Guardie Settimanale" page, displaying a summarized weekly schedule per guard and location. Includes a dialog for detailed shift information (fixed or mobile) upon clicking a cell, integrating new components and logic for interactive schedule viewing.

Replit-Commit-Author: Agent
Replit-Commit-Session-Id: e0b5b11c-5b75-4389-8ea9-5f3cd9332f88
Replit-Commit-Checkpoint-Type: intermediate_checkpoint
Replit-Commit-Screenshot-Url: https://storage.googleapis.com/screenshot-production-us-central1/6d543d2c-20b9-4ea6-93fe-70fe9b1d9f80/e0b5b11c-5b75-4389-8ea9-5f3cd9332f88/EPTvOHB
2025-10-29 08:26:54 +00:00
marco370
a10b50e7e9 Add weekly guard schedule overview page
Introduce a new page for viewing weekly guard schedules, displaying guard assignments, absences, and site information per day.

Replit-Commit-Author: Agent
Replit-Commit-Session-Id: e0b5b11c-5b75-4389-8ea9-5f3cd9332f88
Replit-Commit-Checkpoint-Type: intermediate_checkpoint
Replit-Commit-Screenshot-Url: https://storage.googleapis.com/screenshot-production-us-central1/6d543d2c-20b9-4ea6-93fe-70fe9b1d9f80/e0b5b11c-5b75-4389-8ea9-5f3cd9332f88/EPTvOHB
2025-10-29 08:25:05 +00:00
marco370
c8825a9b4c Add a page to view the weekly guard schedule by location
Implement a new API endpoint `/api/weekly-guards-schedule` to retrieve and display guard assignments, including fixed shifts, mobile shifts, and absences, for a specified location and week.

Replit-Commit-Author: Agent
Replit-Commit-Session-Id: e0b5b11c-5b75-4389-8ea9-5f3cd9332f88
Replit-Commit-Checkpoint-Type: intermediate_checkpoint
Replit-Commit-Screenshot-Url: https://storage.googleapis.com/screenshot-production-us-central1/6d543d2c-20b9-4ea6-93fe-70fe9b1d9f80/e0b5b11c-5b75-4389-8ea9-5f3cd9332f88/EPTvOHB
2025-10-29 08:23:21 +00:00
marco370
7961971ad0 Organize the main navigation menu with new sub-menus and updated labels
Refactor the `app-sidebar.tsx` component to restructure the navigation menu, introducing collapsible sub-menus for planning and master data, and renaming several menu items.

Replit-Commit-Author: Agent
Replit-Commit-Session-Id: e0b5b11c-5b75-4389-8ea9-5f3cd9332f88
Replit-Commit-Checkpoint-Type: full_checkpoint
Replit-Commit-Screenshot-Url: https://storage.googleapis.com/screenshot-production-us-central1/6d543d2c-20b9-4ea6-93fe-70fe9b1d9f80/e0b5b11c-5b75-4389-8ea9-5f3cd9332f88/EPTvOHB
2025-10-29 08:08:37 +00:00
Marco Lanzara
3e3b9851a8 🚀 Release v1.1.0
- Tipo: minor
- Database backup: database-backups/vigilanzaturni_v1.1.0_20251025_090838.sql.gz
- Data: 2025-10-25 09:08:56
2025-10-25 09:08:56 +00:00
Marco Lanzara
3fc0c55fd2 🚀 Release v1.0.58
- Tipo: patch
- Database backup: database-backups/vigilanzaturni_v1.0.58_20251025_090445.sql.gz
- Data: 2025-10-25 09:05:02
2025-10-25 09:05:02 +00:00
marco370
2eb53bb1b2 Improve security by updating user authentication protocols
Update authentication logic and dependencies to enhance system security and compliance.

Replit-Commit-Author: Agent
Replit-Commit-Session-Id: e0b5b11c-5b75-4389-8ea9-5f3cd9332f88
Replit-Commit-Checkpoint-Type: full_checkpoint
Replit-Commit-Screenshot-Url: https://storage.googleapis.com/screenshot-production-us-central1/6d543d2c-20b9-4ea6-93fe-70fe9b1d9f80/e0b5b11c-5b75-4389-8ea9-5f3cd9332f88/N0pfy8w
2025-10-25 09:01:45 +00:00
marco370
a9f3453755 Improve mobile patrol planning with smart assignments and route optimization
Enhance Planning Mobile functionality with smart site assignment indicators, drag-and-drop reordering using @dnd-kit, and OSRM API integration for route optimization.

Replit-Commit-Author: Agent
Replit-Commit-Session-Id: e0b5b11c-5b75-4389-8ea9-5f3cd9332f88
Replit-Commit-Checkpoint-Type: intermediate_checkpoint
Replit-Commit-Screenshot-Url: https://storage.googleapis.com/screenshot-production-us-central1/6d543d2c-20b9-4ea6-93fe-70fe9b1d9f80/e0b5b11c-5b75-4389-8ea9-5f3cd9332f88/N0pfy8w
2025-10-25 09:01:11 +00:00
marco370
dd84ddb35b Add route optimization and display results in mobile planning
Integrate a new API endpoint for route optimization, update the UI to include Sparkles icon, and implement state management for optimization results including total distance and estimated time.

Replit-Commit-Author: Agent
Replit-Commit-Session-Id: e0b5b11c-5b75-4389-8ea9-5f3cd9332f88
Replit-Commit-Checkpoint-Type: intermediate_checkpoint
Replit-Commit-Screenshot-Url: https://storage.googleapis.com/screenshot-production-us-central1/6d543d2c-20b9-4ea6-93fe-70fe9b1d9f80/e0b5b11c-5b75-4389-8ea9-5f3cd9332f88/N0pfy8w
2025-10-25 08:59:53 +00:00
marco370
5c22ec14f1 Add route optimization to improve patrol efficiency
Implement a new API endpoint that uses OSRM and the Nearest Neighbor algorithm to optimize delivery routes, calculating distances, durations, and providing an ordered list of stops.

Replit-Commit-Author: Agent
Replit-Commit-Session-Id: e0b5b11c-5b75-4389-8ea9-5f3cd9332f88
Replit-Commit-Checkpoint-Type: intermediate_checkpoint
Replit-Commit-Screenshot-Url: https://storage.googleapis.com/screenshot-production-us-central1/6d543d2c-20b9-4ea6-93fe-70fe9b1d9f80/e0b5b11c-5b75-4389-8ea9-5f3cd9332f88/N0pfy8w
2025-10-25 08:53:50 +00:00
marco370
efa056dd98 Allow users to reorder planned stops using drag-and-drop
Integrate @dnd-kit for implementing drag-and-drop functionality to reorder stops within the mobile planning view.

Replit-Commit-Author: Agent
Replit-Commit-Session-Id: e0b5b11c-5b75-4389-8ea9-5f3cd9332f88
Replit-Commit-Checkpoint-Type: intermediate_checkpoint
Replit-Commit-Screenshot-Url: https://storage.googleapis.com/screenshot-production-us-central1/6d543d2c-20b9-4ea6-93fe-70fe9b1d9f80/e0b5b11c-5b75-4389-8ea9-5f3cd9332f88/N0pfy8w
2025-10-25 08:52:36 +00:00
marco370
b132082ffc Improve patrol planning by showing assigned guards and scrolling to sequences
Add functionality to display assigned guards for patrol routes and scroll to patrol sequences section.

Replit-Commit-Author: Agent
Replit-Commit-Session-Id: e0b5b11c-5b75-4389-8ea9-5f3cd9332f88
Replit-Commit-Checkpoint-Type: intermediate_checkpoint
Replit-Commit-Screenshot-Url: https://storage.googleapis.com/screenshot-production-us-central1/6d543d2c-20b9-4ea6-93fe-70fe9b1d9f80/e0b5b11c-5b75-4389-8ea9-5f3cd9332f88/Z8fg4as
2025-10-25 08:50:05 +00:00
Marco Lanzara
e5ce415aeb 🚀 Release v1.0.57
- Tipo: patch
- Database backup: database-backups/vigilanzaturni_v1.0.57_20251025_080211.sql.gz
- Data: 2025-10-25 08:02:29
2025-10-25 08:02:29 +00:00
marco370
34bdb99599 Update shift status when duplicating patrol routes
Correctly set the shift status to "planned" instead of "scheduled" when duplicating a patrol route to resolve an enum type error.

Replit-Commit-Author: Agent
Replit-Commit-Session-Id: e0b5b11c-5b75-4389-8ea9-5f3cd9332f88
Replit-Commit-Checkpoint-Type: full_checkpoint
Replit-Commit-Screenshot-Url: https://storage.googleapis.com/screenshot-production-us-central1/6d543d2c-20b9-4ea6-93fe-70fe9b1d9f80/e0b5b11c-5b75-4389-8ea9-5f3cd9332f88/Z8fg4as
2025-10-25 08:00:37 +00:00
marco370
3cc1739015 Fix error that occurs when viewing user profile information
Resolve null reference exception in UserProfileComponent.cs by adding null checks.

Replit-Commit-Author: Agent
Replit-Commit-Session-Id: e0b5b11c-5b75-4389-8ea9-5f3cd9332f88
Replit-Commit-Checkpoint-Type: full_checkpoint
Replit-Commit-Screenshot-Url: https://storage.googleapis.com/screenshot-production-us-central1/6d543d2c-20b9-4ea6-93fe-70fe9b1d9f80/e0b5b11c-5b75-4389-8ea9-5f3cd9332f88/Z8fg4as
2025-10-25 07:57:02 +00:00
marco370
20f24ba25e Fix error when copying patrol routes to a different date
Update date field name from `scheduledDate` to `shiftDate` in patrol route copying logic to resolve a data inconsistency issue.

Replit-Commit-Author: Agent
Replit-Commit-Session-Id: e0b5b11c-5b75-4389-8ea9-5f3cd9332f88
Replit-Commit-Checkpoint-Type: intermediate_checkpoint
Replit-Commit-Screenshot-Url: https://storage.googleapis.com/screenshot-production-us-central1/6d543d2c-20b9-4ea6-93fe-70fe9b1d9f80/e0b5b11c-5b75-4389-8ea9-5f3cd9332f88/Z8fg4as
2025-10-25 07:56:17 +00:00
Marco Lanzara
bd55070abc 🚀 Release v1.0.56
- Tipo: patch
- Database backup: database-backups/vigilanzaturni_v1.0.56_20251025_074951.sql.gz
- Data: 2025-10-25 07:50:09
2025-10-25 07:50:09 +00:00
Marco Lanzara
7a6fd3245b 🚀 Release v1.0.55
- Tipo: patch
- Database backup: database-backups/vigilanzaturni_v1.0.55_20251025_072838.sql.gz
- Data: 2025-10-25 07:28:56
2025-10-25 07:28:56 +00:00
marco370
bafc34065e Fix invalid date error when duplicating shifts on mobile
Corrected date validation logic in the shift duplication dialog on the mobile planning interface to resolve "invalid date" errors and ensure successful duplication.

Replit-Commit-Author: Agent
Replit-Commit-Session-Id: e0b5b11c-5b75-4389-8ea9-5f3cd9332f88
Replit-Commit-Checkpoint-Type: full_checkpoint
Replit-Commit-Screenshot-Url: https://storage.googleapis.com/screenshot-production-us-central1/6d543d2c-20b9-4ea6-93fe-70fe9b1d9f80/e0b5b11c-5b75-4389-8ea9-5f3cd9332f88/tgJ9Kmd
2025-10-25 06:39:36 +00:00
marco370
b4c6400360 Fix date validation for duplicating shifts in the mobile planning view
Update date parsing and validation logic in PlanningMobile.tsx to correctly handle `shiftDate` instead of `scheduledDate` for duplicating shifts, resolving "invalid date" errors and duplicate confirmation issues.

Replit-Commit-Author: Agent
Replit-Commit-Session-Id: e0b5b11c-5b75-4389-8ea9-5f3cd9332f88
Replit-Commit-Checkpoint-Type: intermediate_checkpoint
Replit-Commit-Screenshot-Url: https://storage.googleapis.com/screenshot-production-us-central1/6d543d2c-20b9-4ea6-93fe-70fe9b1d9f80/e0b5b11c-5b75-4389-8ea9-5f3cd9332f88/tgJ9Kmd
2025-10-25 06:38:55 +00:00
Marco Lanzara
753e01d612 🚀 Release v1.0.54
- Tipo: patch
- Database backup: database-backups/vigilanzaturni_v1.0.54_20251024_172526.sql.gz
- Data: 2025-10-24 17:25:44
2025-10-24 17:25:44 +00:00
marco370
3b3056f6b8 Improve planning duplication functionality with error handling and validation
Add validation for selected date and error handling for opening the duplicate dialog in the mobile planning view, also improve date display for scheduled routes.

Replit-Commit-Author: Agent
Replit-Commit-Session-Id: e0b5b11c-5b75-4389-8ea9-5f3cd9332f88
Replit-Commit-Checkpoint-Type: full_checkpoint
Replit-Commit-Screenshot-Url: https://storage.googleapis.com/screenshot-production-us-central1/6d543d2c-20b9-4ea6-93fe-70fe9b1d9f80/e0b5b11c-5b75-4389-8ea9-5f3cd9332f88/JYtd6x2
2025-10-24 17:08:57 +00:00
Marco Lanzara
2b62d8ff4e 🚀 Release v1.0.53
- Tipo: patch
- Database backup: database-backups/vigilanzaturni_v1.0.53_20251024_163455.sql.gz
- Data: 2025-10-24 16:35:13
2025-10-24 16:35:13 +00:00
marco370
1639244169 Add system to manage guard shifts and client portal functionality
Implement a new API endpoint for guard shift management and a client portal.

Replit-Commit-Author: Agent
Replit-Commit-Session-Id: e0b5b11c-5b75-4389-8ea9-5f3cd9332f88
Replit-Commit-Checkpoint-Type: full_checkpoint
Replit-Commit-Screenshot-Url: https://storage.googleapis.com/screenshot-production-us-central1/6d543d2c-20b9-4ea6-93fe-70fe9b1d9f80/e0b5b11c-5b75-4389-8ea9-5f3cd9332f88/EDxr1e6
2025-10-24 15:47:54 +00:00
marco370
2cd6c32ad9 Add ability to duplicate shifts and patrol routes to streamline planning
Introduces POST /api/shift-assignments/copy-week and POST /api/patrol-routes/duplicate endpoints for duplicating weekly fixed shifts and mobile patrol routes, respectively, with corresponding frontend dialogs.

Replit-Commit-Author: Agent
Replit-Commit-Session-Id: e0b5b11c-5b75-4389-8ea9-5f3cd9332f88
Replit-Commit-Checkpoint-Type: intermediate_checkpoint
Replit-Commit-Screenshot-Url: https://storage.googleapis.com/screenshot-production-us-central1/6d543d2c-20b9-4ea6-93fe-70fe9b1d9f80/e0b5b11c-5b75-4389-8ea9-5f3cd9332f88/EDxr1e6
2025-10-24 15:47:25 +00:00
marco370
0a72b413fa Add functionality to duplicate weekly schedules and patrol routes
Introduces a dialog to copy weekly schedules to the next week and duplicates patrol routes with specified guards and dates, updating the client-side UI and API interactions.

Replit-Commit-Author: Agent
Replit-Commit-Session-Id: e0b5b11c-5b75-4389-8ea9-5f3cd9332f88
Replit-Commit-Checkpoint-Type: intermediate_checkpoint
Replit-Commit-Screenshot-Url: https://storage.googleapis.com/screenshot-production-us-central1/6d543d2c-20b9-4ea6-93fe-70fe9b1d9f80/e0b5b11c-5b75-4389-8ea9-5f3cd9332f88/EDxr1e6
2025-10-24 15:46:11 +00:00
marco370
1bad21cf9e Add functionality to duplicate and modify patrol routes
Adds a new POST endpoint `/api/patrol-routes/duplicate` to duplicate existing patrol routes to a new date, optionally assigning a different guard. If the target date is the same as the source date, it updates the guard for the existing route.

Replit-Commit-Author: Agent
Replit-Commit-Session-Id: e0b5b11c-5b75-4389-8ea9-5f3cd9332f88
Replit-Commit-Checkpoint-Type: intermediate_checkpoint
Replit-Commit-Screenshot-Url: https://storage.googleapis.com/screenshot-production-us-central1/6d543d2c-20b9-4ea6-93fe-70fe9b1d9f80/e0b5b11c-5b75-4389-8ea9-5f3cd9332f88/EDxr1e6
2025-10-24 15:40:27 +00:00
marco370
6366382753 Add functionality to copy weekly shifts to the next week
Introduce a new feature allowing users to copy weekly shift assignments to the subsequent week via a dedicated button, including a confirmation dialog and error handling for the copy operation. The UI also includes an update to the navigation bar for better responsiveness.

Replit-Commit-Author: Agent
Replit-Commit-Session-Id: e0b5b11c-5b75-4389-8ea9-5f3cd9332f88
Replit-Commit-Checkpoint-Type: intermediate_checkpoint
Replit-Commit-Screenshot-Url: https://storage.googleapis.com/screenshot-production-us-central1/6d543d2c-20b9-4ea6-93fe-70fe9b1d9f80/e0b5b11c-5b75-4389-8ea9-5f3cd9332f88/EDxr1e6
2025-10-24 15:38:51 +00:00
marco370
0b64fd2f08 Add functionality to copy weekly shift assignments to the following week
Introduce a new POST API endpoint `/api/shift-assignments/copy-week` to duplicate existing shift assignments and their associated shifts for a specified location and week, automatically adjusting dates by adding 7 days.

Replit-Commit-Author: Agent
Replit-Commit-Session-Id: e0b5b11c-5b75-4389-8ea9-5f3cd9332f88
Replit-Commit-Checkpoint-Type: intermediate_checkpoint
Replit-Commit-Screenshot-Url: https://storage.googleapis.com/screenshot-production-us-central1/6d543d2c-20b9-4ea6-93fe-70fe9b1d9f80/e0b5b11c-5b75-4389-8ea9-5f3cd9332f88/EDxr1e6
2025-10-24 15:36:54 +00:00
marco370
36bfad3815 Add a new system for managing security guard shift schedules
No changes to review.

Replit-Commit-Author: Agent
Replit-Commit-Session-Id: e0b5b11c-5b75-4389-8ea9-5f3cd9332f88
Replit-Commit-Checkpoint-Type: intermediate_checkpoint
Replit-Commit-Screenshot-Url: https://storage.googleapis.com/screenshot-production-us-central1/6d543d2c-20b9-4ea6-93fe-70fe9b1d9f80/e0b5b11c-5b75-4389-8ea9-5f3cd9332f88/EDxr1e6
2025-10-24 15:35:33 +00:00
27 changed files with 2479 additions and 201 deletions

View File

@ -19,6 +19,10 @@ externalPort = 80
localPort = 33035 localPort = 33035
externalPort = 3001 externalPort = 3001
[[ports]]
localPort = 40417
externalPort = 8000
[[ports]] [[ports]]
localPort = 41295 localPort = 41295
externalPort = 5173 externalPort = 5173

View File

@ -30,6 +30,7 @@ import PlanningMobile from "@/pages/planning-mobile";
import MyShiftsFixed from "@/pages/my-shifts-fixed"; import MyShiftsFixed from "@/pages/my-shifts-fixed";
import MyShiftsMobile from "@/pages/my-shifts-mobile"; import MyShiftsMobile from "@/pages/my-shifts-mobile";
import SitePlanningView from "@/pages/site-planning-view"; import SitePlanningView from "@/pages/site-planning-view";
import WeeklyGuards from "@/pages/weekly-guards";
function Router() { function Router() {
const { isAuthenticated, isLoading } = useAuth(); const { isAuthenticated, isLoading } = useAuth();
@ -57,6 +58,7 @@ function Router() {
<Route path="/my-shifts-fixed" component={MyShiftsFixed} /> <Route path="/my-shifts-fixed" component={MyShiftsFixed} />
<Route path="/my-shifts-mobile" component={MyShiftsMobile} /> <Route path="/my-shifts-mobile" component={MyShiftsMobile} />
<Route path="/site-planning-view" component={SitePlanningView} /> <Route path="/site-planning-view" component={SitePlanningView} />
<Route path="/weekly-guards" component={WeeklyGuards} />
<Route path="/reports" component={Reports} /> <Route path="/reports" component={Reports} />
<Route path="/notifications" component={Notifications} /> <Route path="/notifications" component={Notifications} />
<Route path="/users" component={Users} /> <Route path="/users" component={Users} />

View File

@ -12,6 +12,11 @@ import {
Car, Car,
Briefcase, Briefcase,
Navigation, Navigation,
ChevronDown,
FileText,
FolderKanban,
Building2,
Wrench,
} from "lucide-react"; } from "lucide-react";
import { Link, useLocation } from "wouter"; import { Link, useLocation } from "wouter";
import { import {
@ -23,15 +28,31 @@ import {
SidebarMenu, SidebarMenu,
SidebarMenuButton, SidebarMenuButton,
SidebarMenuItem, SidebarMenuItem,
SidebarMenuSub,
SidebarMenuSubItem,
SidebarMenuSubButton,
SidebarHeader, SidebarHeader,
SidebarFooter, SidebarFooter,
} from "@/components/ui/sidebar"; } from "@/components/ui/sidebar";
import {
Collapsible,
CollapsibleContent,
CollapsibleTrigger,
} from "@/components/ui/collapsible";
import { useAuth } from "@/hooks/useAuth"; import { useAuth } from "@/hooks/useAuth";
import { Avatar, AvatarFallback, AvatarImage } from "@/components/ui/avatar"; import { Avatar, AvatarFallback, AvatarImage } from "@/components/ui/avatar";
import { Button } from "@/components/ui/button"; import { Button } from "@/components/ui/button";
import { ThemeToggle } from "@/components/theme-toggle"; import { ThemeToggle } from "@/components/theme-toggle";
const menuItems = [ interface MenuItem {
title: string;
url?: string;
icon: any;
roles: string[];
items?: MenuItem[];
}
const menuItems: MenuItem[] = [
{ {
title: "Dashboard", title: "Dashboard",
url: "/", url: "/",
@ -39,100 +60,123 @@ const menuItems = [
roles: ["admin", "coordinator", "guard", "client"], roles: ["admin", "coordinator", "guard", "client"],
}, },
{ {
title: "Turni", title: "Planning",
url: "/shifts", icon: FolderKanban,
icon: Calendar,
roles: ["admin", "coordinator", "guard"],
},
{
title: "Pianificazione",
url: "/planning",
icon: ClipboardList,
roles: ["admin", "coordinator"], roles: ["admin", "coordinator"],
items: [
{
title: "Fissi",
url: "/general-planning",
icon: Calendar,
roles: ["admin", "coordinator"],
},
{
title: "Mobili",
url: "/planning-mobile",
icon: Navigation,
roles: ["admin", "coordinator"],
},
{
title: "Vista",
url: "/service-planning",
icon: ClipboardList,
roles: ["admin", "coordinator"],
},
{
title: "Guardie Settimanale",
url: "/weekly-guards",
icon: Users,
roles: ["admin", "coordinator"],
},
],
}, },
{ {
title: "Pianificazione Operativa", title: "Scadenziario",
url: "/operational-planning",
icon: Calendar,
roles: ["admin", "coordinator"],
},
{
title: "Planning Fissi",
url: "/general-planning",
icon: BarChart3,
roles: ["admin", "coordinator"],
},
{
title: "Planning Mobile",
url: "/planning-mobile",
icon: Navigation,
roles: ["admin", "coordinator"],
},
{
title: "Planning di Servizio",
url: "/service-planning",
icon: ClipboardList,
roles: ["admin", "coordinator"],
},
{
title: "Gestione Pianificazioni",
url: "/advanced-planning", url: "/advanced-planning",
icon: ClipboardList, icon: Calendar,
roles: ["admin", "coordinator"], roles: ["admin", "coordinator"],
}, },
{ {
title: "Guardie", title: "Anagrafiche",
url: "/guards", icon: Building2,
icon: Users,
roles: ["admin", "coordinator"], roles: ["admin", "coordinator"],
items: [
{
title: "Guardie",
url: "/guards",
icon: Users,
roles: ["admin", "coordinator"],
},
{
title: "Siti",
url: "/sites",
icon: MapPin,
roles: ["admin", "coordinator", "client"],
},
{
title: "Clienti",
url: "/customers",
icon: Briefcase,
roles: ["admin", "coordinator"],
},
{
title: "Automezzi",
url: "/vehicles",
icon: Car,
roles: ["admin", "coordinator"],
},
],
}, },
{ {
title: "Siti", title: "Tipologia",
url: "/sites", icon: Wrench,
icon: MapPin,
roles: ["admin", "coordinator", "client"],
},
{
title: "Clienti",
url: "/customers",
icon: Briefcase,
roles: ["admin", "coordinator"],
},
{
title: "Servizi",
url: "/services",
icon: Briefcase,
roles: ["admin", "coordinator"],
},
{
title: "Parco Automezzi",
url: "/vehicles",
icon: Car,
roles: ["admin", "coordinator"], roles: ["admin", "coordinator"],
items: [
{
title: "Servizi",
url: "/services",
icon: Briefcase,
roles: ["admin", "coordinator"],
},
{
title: "Contratti",
url: "/parameters",
icon: Settings,
roles: ["admin", "coordinator"],
},
],
}, },
{ {
title: "Report", title: "Report",
url: "/reports",
icon: BarChart3, icon: BarChart3,
roles: ["admin", "coordinator", "client"], roles: ["admin", "coordinator", "client"],
items: [
{
title: "Report Amministrativo",
url: "/reports",
icon: FileText,
roles: ["admin", "coordinator", "client"],
},
],
}, },
{ {
title: "Notifiche", title: "Utilità",
url: "/notifications",
icon: Bell,
roles: ["admin", "coordinator", "guard"],
},
{
title: "Utenti",
url: "/users",
icon: UserCog,
roles: ["admin"],
},
{
title: "Parametri",
url: "/parameters",
icon: Settings, icon: Settings,
roles: ["admin", "coordinator"], roles: ["admin", "coordinator", "guard"],
items: [
{
title: "Utenti",
url: "/users",
icon: UserCog,
roles: ["admin"],
},
{
title: "Notifiche",
url: "/notifications",
icon: Bell,
roles: ["admin", "coordinator", "guard"],
},
],
}, },
]; ];
@ -140,9 +184,78 @@ export function AppSidebar() {
const { user } = useAuth(); const { user } = useAuth();
const [location] = useLocation(); const [location] = useLocation();
const filteredItems = menuItems.filter( const filterMenuItems = (items: MenuItem[]): MenuItem[] => {
(item) => user && item.roles.includes(user.role) if (!user) return [];
);
return items.filter((item) => {
const hasRole = item.roles.includes(user.role);
if (!hasRole) return false;
if (item.items) {
item.items = filterMenuItems(item.items);
return item.items.length > 0;
}
return true;
});
};
const filteredItems = filterMenuItems(menuItems);
const renderMenuItem = (item: MenuItem) => {
// Menu item con sottomenu
if (item.items && item.items.length > 0) {
const isAnySubItemActive = item.items.some((subItem) => location === subItem.url);
return (
<Collapsible key={item.title} defaultOpen={isAnySubItemActive} className="group/collapsible">
<SidebarMenuItem>
<CollapsibleTrigger asChild>
<SidebarMenuButton data-testid={`menu-${item.title.toLowerCase()}`}>
<item.icon className="h-4 w-4" />
<span>{item.title}</span>
<ChevronDown className="ml-auto h-4 w-4 transition-transform group-data-[state=open]/collapsible:rotate-180" />
</SidebarMenuButton>
</CollapsibleTrigger>
<CollapsibleContent>
<SidebarMenuSub>
{item.items.map((subItem) => (
<SidebarMenuSubItem key={subItem.title}>
<SidebarMenuSubButton
asChild
isActive={location === subItem.url}
data-testid={`link-${subItem.title.toLowerCase().replace(/\s+/g, '-')}`}
>
<Link href={subItem.url!}>
<subItem.icon className="h-4 w-4" />
<span>{subItem.title}</span>
</Link>
</SidebarMenuSubButton>
</SidebarMenuSubItem>
))}
</SidebarMenuSub>
</CollapsibleContent>
</SidebarMenuItem>
</Collapsible>
);
}
// Menu item semplice
return (
<SidebarMenuItem key={item.title}>
<SidebarMenuButton
asChild
isActive={location === item.url}
data-testid={`link-${item.title.toLowerCase().replace(/\s+/g, '-')}`}
>
<Link href={item.url!}>
<item.icon className="h-4 w-4" />
<span>{item.title}</span>
</Link>
</SidebarMenuButton>
</SidebarMenuItem>
);
};
return ( return (
<Sidebar> <Sidebar>
@ -161,20 +274,7 @@ export function AppSidebar() {
<SidebarGroupLabel>Menu Principale</SidebarGroupLabel> <SidebarGroupLabel>Menu Principale</SidebarGroupLabel>
<SidebarGroupContent> <SidebarGroupContent>
<SidebarMenu> <SidebarMenu>
{filteredItems.map((item) => ( {filteredItems.map(renderMenuItem)}
<SidebarMenuItem key={item.title}>
<SidebarMenuButton
asChild
isActive={location === item.url}
data-testid={`link-${item.title.toLowerCase()}`}
>
<Link href={item.url}>
<item.icon className="h-4 w-4" />
<span>{item.title}</span>
</Link>
</SidebarMenuButton>
</SidebarMenuItem>
))}
</SidebarMenu> </SidebarMenu>
</SidebarGroupContent> </SidebarGroupContent>
</SidebarGroup> </SidebarGroup>

View File

@ -8,7 +8,7 @@ import { Button } from "@/components/ui/button";
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select"; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select";
import { Input } from "@/components/ui/input"; import { Input } from "@/components/ui/input";
import { Label } from "@/components/ui/label"; import { Label } from "@/components/ui/label";
import { ChevronLeft, ChevronRight, Calendar, MapPin, Users, AlertTriangle, Car, Edit, CheckCircle2, Plus, Trash2, Clock } from "lucide-react"; import { ChevronLeft, ChevronRight, Calendar, MapPin, Users, AlertTriangle, Car, Edit, CheckCircle2, Plus, Trash2, Clock, Copy, Circle } from "lucide-react";
import { Badge } from "@/components/ui/badge"; import { Badge } from "@/components/ui/badge";
import { Skeleton } from "@/components/ui/skeleton"; import { Skeleton } from "@/components/ui/skeleton";
import { import {
@ -117,6 +117,7 @@ export default function GeneralPlanning() {
const [consecutiveDays, setConsecutiveDays] = useState<number>(1); const [consecutiveDays, setConsecutiveDays] = useState<number>(1);
const [showOvertimeGuards, setShowOvertimeGuards] = useState<boolean>(false); const [showOvertimeGuards, setShowOvertimeGuards] = useState<boolean>(false);
const [ccnlConfirmation, setCcnlConfirmation] = useState<{ message: string; data: any } | null>(null); const [ccnlConfirmation, setCcnlConfirmation] = useState<{ message: string; data: any } | null>(null);
const [showCopyWeekConfirmation, setShowCopyWeekConfirmation] = useState<boolean>(false);
// Query per dati planning settimanale // Query per dati planning settimanale
const { data: planningData, isLoading } = useQuery<GeneralPlanningResponse>({ const { data: planningData, isLoading } = useQuery<GeneralPlanningResponse>({
@ -275,6 +276,54 @@ export default function GeneralPlanning() {
}, },
}); });
// Mutation per copiare turni settimanali
const copyWeekMutation = useMutation({
mutationFn: async () => {
return apiRequest("POST", "/api/shift-assignments/copy-week", {
weekStart: format(weekStart, "yyyy-MM-dd"),
location: selectedLocation,
});
},
onSuccess: async (response: any) => {
const data = await response.json();
toast({
title: "Settimana copiata!",
description: `${data.copiedShifts} turni e ${data.copiedAssignments} assegnazioni copiate nella settimana successiva`,
});
// Invalida cache e naviga alla settimana successiva
await queryClient.invalidateQueries({ queryKey: ["/api/general-planning"] });
setWeekStart(addWeeks(weekStart, 1)); // Naviga alla settimana copiata
setShowCopyWeekConfirmation(false);
},
onError: (error: any) => {
let errorMessage = "Impossibile copiare la settimana";
if (error.message) {
const match = error.message.match(/^(\d+):\s*(.+)$/);
if (match) {
try {
const parsed = JSON.parse(match[2]);
errorMessage = parsed.message || errorMessage;
} catch {
errorMessage = match[2];
}
} else {
errorMessage = error.message;
}
}
toast({
title: "Errore Copia Settimana",
description: errorMessage,
variant: "destructive",
});
setShowCopyWeekConfirmation(false);
},
});
// Handler per submit form assegnazione guardia // Handler per submit form assegnazione guardia
const handleAssignGuard = () => { const handleAssignGuard = () => {
if (!selectedCell || !selectedGuardId) return; if (!selectedCell || !selectedGuardId) return;
@ -358,7 +407,7 @@ export default function GeneralPlanning() {
</div> </div>
{/* Navigazione settimana */} {/* Navigazione settimana */}
<div className="flex items-center gap-2"> <div className="flex items-center gap-2 flex-wrap">
<Button <Button
variant="outline" variant="outline"
size="icon" size="icon"
@ -385,6 +434,16 @@ export default function GeneralPlanning() {
> >
<ChevronRight className="h-4 w-4" /> <ChevronRight className="h-4 w-4" />
</Button> </Button>
<Button
variant="default"
onClick={() => setShowCopyWeekConfirmation(true)}
disabled={isLoading || !planningData || copyWeekMutation.isPending}
data-testid="button-copy-week"
>
<Copy className="h-4 w-4 mr-2" />
{copyWeekMutation.isPending ? "Copia in corso..." : "Copia Turno Settimanale"}
</Button>
</div> </div>
{/* Info settimana */} {/* Info settimana */}
@ -658,19 +717,19 @@ export default function GeneralPlanning() {
})()} })()}
</div> </div>
{/* Select guardia disponibile */} {/* Select guardia (tutte, evidenziate in rosso se impegnate) */}
{(() => { {(() => {
// Filtra guardie: mostra solo con ore ordinarie se toggle è off // Mostra TUTTE le guardie, ma filtra solo per ore ordinarie/straordinario
const filteredGuards = availableGuards?.filter(g => const filteredGuards = availableGuards?.filter(g =>
g.isAvailable && (showOvertimeGuards || !g.requiresOvertime) showOvertimeGuards || !g.requiresOvertime
) || []; ) || [];
const hasOvertimeGuards = availableGuards?.some(g => g.requiresOvertime && g.isAvailable) || false; const hasOvertimeGuards = availableGuards?.some(g => g.requiresOvertime) || false;
return ( return (
<div className="space-y-2"> <div className="space-y-2">
<div className="flex items-center justify-between"> <div className="flex items-center justify-between">
<Label htmlFor="guard-select">Guardia Disponibile</Label> <Label htmlFor="guard-select">Guardia</Label>
{!isLoadingGuards && hasOvertimeGuards && ( {!isLoadingGuards && hasOvertimeGuards && (
<Button <Button
variant="outline" variant="outline"
@ -699,15 +758,20 @@ export default function GeneralPlanning() {
{filteredGuards.length > 0 ? ( {filteredGuards.length > 0 ? (
filteredGuards.map((guard) => ( filteredGuards.map((guard) => (
<SelectItem key={guard.guardId} value={guard.guardId}> <SelectItem key={guard.guardId} value={guard.guardId}>
{guard.guardName} ({guard.badgeNumber}) - {guard.ordinaryHoursRemaining}h ord. <div className={`flex items-center gap-1.5 ${guard.isAvailable ? "" : "text-destructive font-medium"}`}>
{guard.requiresOvertime && ` + ${guard.overtimeHoursRemaining}h strao.`} {!guard.isAvailable && <Circle className="h-3 w-3 fill-current" />}
{guard.requiresOvertime && " 🔸"} <span>
{guard.guardName} ({guard.badgeNumber}) - {guard.ordinaryHoursRemaining}h ord.
{guard.requiresOvertime && ` + ${guard.overtimeHoursRemaining}h strao.`}
{guard.requiresOvertime && " 🔸"}
</span>
</div>
</SelectItem> </SelectItem>
)) ))
) : ( ) : (
<SelectItem value="no-guards" disabled> <SelectItem value="no-guards" disabled>
{showOvertimeGuards {showOvertimeGuards
? "Nessuna guardia disponibile" ? "Nessuna guardia"
: "Nessuna guardia con ore ordinarie (prova 'Mostra Straordinario')"} : "Nessuna guardia con ore ordinarie (prova 'Mostra Straordinario')"}
</SelectItem> </SelectItem>
)} )}
@ -715,7 +779,7 @@ export default function GeneralPlanning() {
</Select> </Select>
{filteredGuards.length === 0 && !showOvertimeGuards && hasOvertimeGuards && ( {filteredGuards.length === 0 && !showOvertimeGuards && hasOvertimeGuards && (
<p className="text-xs text-muted-foreground"> <p className="text-xs text-muted-foreground">
Alcune guardie disponibili richiedono straordinario. Clicca "Mostra Straordinario" per vederle. Alcune guardie richiedono straordinario. Clicca "Mostra Straordinario" per vederle.
</p> </p>
)} )}
{filteredGuards.length > 0 && selectedGuardId && ( {filteredGuards.length > 0 && selectedGuardId && (
@ -988,6 +1052,60 @@ export default function GeneralPlanning() {
</AlertDialogFooter> </AlertDialogFooter>
</AlertDialogContent> </AlertDialogContent>
</AlertDialog> </AlertDialog>
{/* Dialog conferma copia settimana */}
<AlertDialog open={showCopyWeekConfirmation} onOpenChange={setShowCopyWeekConfirmation}>
<AlertDialogContent>
<AlertDialogHeader>
<AlertDialogTitle className="flex items-center gap-2">
<Copy className="h-5 w-5" />
Copia Turno Settimanale
</AlertDialogTitle>
<AlertDialogDescription asChild>
<div className="space-y-3">
<p className="text-foreground font-medium">
Vuoi copiare tutti i turni della settimana corrente nella settimana successiva?
</p>
{planningData && (
<div className="space-y-2 bg-muted/30 p-3 rounded-md">
<div className="flex items-center justify-between text-sm">
<span className="text-muted-foreground">Settimana corrente:</span>
<span className="font-medium">
{format(new Date(planningData.weekStart), "dd MMM", { locale: it })} -{" "}
{format(new Date(planningData.weekEnd), "dd MMM yyyy", { locale: it })}
</span>
</div>
<div className="flex items-center justify-between text-sm">
<span className="text-muted-foreground">Verrà copiata in:</span>
<span className="font-medium">
{format(addWeeks(new Date(planningData.weekStart), 1), "dd MMM", { locale: it })} -{" "}
{format(addWeeks(new Date(planningData.weekEnd), 1), "dd MMM yyyy", { locale: it })}
</span>
</div>
<div className="flex items-center justify-between text-sm">
<span className="text-muted-foreground">Sede:</span>
<span className="font-medium">{formatLocation(selectedLocation)}</span>
</div>
</div>
)}
<p className="text-sm text-muted-foreground">
Tutti i turni e le assegnazioni guardie verranno duplicati con le stesse caratteristiche (orari, dotazioni, veicoli).
</p>
</div>
</AlertDialogDescription>
</AlertDialogHeader>
<AlertDialogFooter>
<AlertDialogCancel data-testid="button-cancel-copy-week">Annulla</AlertDialogCancel>
<AlertDialogAction
onClick={() => copyWeekMutation.mutate()}
data-testid="button-confirm-copy-week"
disabled={copyWeekMutation.isPending}
>
{copyWeekMutation.isPending ? "Copia in corso..." : "Conferma Copia"}
</AlertDialogAction>
</AlertDialogFooter>
</AlertDialogContent>
</AlertDialog>
</div> </div>
); );
} }

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,448 @@
import { useState } from "react";
import { useQuery } from "@tanstack/react-query";
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card";
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select";
import { Button } from "@/components/ui/button";
import { Dialog, DialogContent, DialogDescription, DialogHeader, DialogTitle, DialogFooter } from "@/components/ui/dialog";
import { Badge } from "@/components/ui/badge";
import { Calendar as CalendarIcon, ChevronLeft, ChevronRight, Users, Clock, MapPin, Navigation, ExternalLink } from "lucide-react";
import { format, parseISO, addDays, startOfWeek, addWeeks } from "date-fns";
import { it } from "date-fns/locale";
import { Link } from "wouter";
type AbsenceType = "sick_leave" | "vacation" | "personal_leave" | "injury";
interface GuardScheduleData {
guard: {
id: string;
firstName: string;
lastName: string;
badgeNumber: string;
};
fixedShifts: Array<{
assignmentId: string;
shiftId: string;
plannedStartTime: Date;
plannedEndTime: Date;
siteName: string;
siteId: string;
}>;
mobileShifts: Array<{
routeId: string;
shiftDate: string;
startTime: string;
endTime: string;
}>;
absences: Array<{
id: string;
type: AbsenceType;
startDate: string;
endDate: string;
}>;
}
interface WeeklyScheduleResponse {
weekStart: string;
weekEnd: string;
location: string;
guards: GuardScheduleData[];
}
const ABSENCE_LABELS: Record<AbsenceType, string> = {
sick_leave: "Malattia",
vacation: "Ferie",
personal_leave: "Permesso",
injury: "Infortunio",
};
type DialogData = {
type: "fixed" | "mobile";
guardName: string;
date: string;
data: any;
} | null;
export default function WeeklyGuards() {
const [selectedLocation, setSelectedLocation] = useState<string>("roccapiemonte");
const [currentWeekStart, setCurrentWeekStart] = useState<Date>(() =>
startOfWeek(new Date(), { weekStartsOn: 1 }) // Inizia lunedì
);
const [dialogData, setDialogData] = useState<DialogData>(null);
const { data: scheduleData, isLoading, error } = useQuery<WeeklyScheduleResponse>({
queryKey: ["/api/weekly-guards-schedule", selectedLocation, format(currentWeekStart, "yyyy-MM-dd")],
queryFn: async () => {
const startDate = format(currentWeekStart, "yyyy-MM-dd");
const response = await fetch(
`/api/weekly-guards-schedule?location=${selectedLocation}&startDate=${startDate}`
);
if (!response.ok) {
throw new Error("Failed to fetch weekly schedule");
}
return response.json();
},
enabled: !!selectedLocation,
});
// Helper per ottenere i giorni della settimana
const getWeekDays = () => {
const days = [];
for (let i = 0; i < 7; i++) {
days.push(addDays(currentWeekStart, i));
}
return days;
};
const weekDays = getWeekDays();
// Helper per trovare l'attività di una guardia in un giorno specifico
const getDayActivity = (guardData: GuardScheduleData, date: Date) => {
const dateStr = format(date, "yyyy-MM-dd");
// Controlla assenze
const absence = guardData.absences.find(abs => {
const startDate = abs.startDate;
const endDate = abs.endDate;
return dateStr >= startDate && dateStr <= endDate;
});
if (absence) {
return {
type: "absence" as const,
label: ABSENCE_LABELS[absence.type],
data: absence,
};
}
// Controlla turni fissi
const fixedShift = guardData.fixedShifts.find(shift => {
const shiftDate = format(new Date(shift.plannedStartTime), "yyyy-MM-dd");
return shiftDate === dateStr;
});
if (fixedShift) {
const startTime = format(new Date(fixedShift.plannedStartTime), "HH:mm");
const endTime = format(new Date(fixedShift.plannedEndTime), "HH:mm");
return {
type: "fixed" as const,
label: `${fixedShift.siteName} ${startTime}-${endTime}`,
data: fixedShift,
};
}
// Controlla turni mobili
const mobileShift = guardData.mobileShifts.find(shift => shift.shiftDate === dateStr);
if (mobileShift) {
return {
type: "mobile" as const,
label: `Pattuglia ${mobileShift.startTime}-${mobileShift.endTime}`,
data: mobileShift,
};
}
return null;
};
const handlePreviousWeek = () => {
setCurrentWeekStart(prev => addWeeks(prev, -1));
};
const handleNextWeek = () => {
setCurrentWeekStart(prev => addWeeks(prev, 1));
};
const handleCellClick = (guardData: GuardScheduleData, activity: ReturnType<typeof getDayActivity>, date: Date) => {
if (!activity || activity.type === "absence") return;
const guardName = `${guardData.guard.lastName} ${guardData.guard.firstName}`;
const dateStr = format(date, "EEEE dd MMMM yyyy", { locale: it });
setDialogData({
type: activity.type,
guardName,
date: dateStr,
data: activity.data,
});
};
return (
<div className="container mx-auto p-6 space-y-6">
<div className="flex items-center justify-between">
<div>
<h1 className="text-3xl font-bold flex items-center gap-2">
<Users className="h-8 w-8 text-primary" />
Guardie Settimanale
</h1>
<p className="text-muted-foreground mt-1">
Vista riepilogativa delle assegnazioni settimanali per sede
</p>
</div>
</div>
{/* Filtri */}
<Card>
<CardHeader>
<CardTitle className="flex items-center gap-2">
<CalendarIcon className="h-5 w-5" />
Filtri Visualizzazione
</CardTitle>
<CardDescription>
Seleziona sede e settimana per visualizzare le assegnazioni
</CardDescription>
</CardHeader>
<CardContent>
<div className="flex items-end gap-4">
<div className="flex-1">
<label className="text-sm font-medium mb-2 block">Sede</label>
<Select
value={selectedLocation}
onValueChange={setSelectedLocation}
data-testid="select-location"
>
<SelectTrigger>
<SelectValue placeholder="Seleziona sede" />
</SelectTrigger>
<SelectContent>
<SelectItem value="roccapiemonte">Roccapiemonte</SelectItem>
<SelectItem value="milano">Milano</SelectItem>
<SelectItem value="roma">Roma</SelectItem>
</SelectContent>
</Select>
</div>
<div className="flex items-center gap-2">
<Button
variant="outline"
size="icon"
onClick={handlePreviousWeek}
data-testid="button-previous-week"
>
<ChevronLeft className="h-4 w-4" />
</Button>
<div className="px-4 py-2 border rounded-md bg-muted min-w-[280px] text-center">
<span className="font-medium">
{format(currentWeekStart, "d MMM", { locale: it })} - {format(addDays(currentWeekStart, 6), "d MMM yyyy", { locale: it })}
</span>
</div>
<Button
variant="outline"
size="icon"
onClick={handleNextWeek}
data-testid="button-next-week"
>
<ChevronRight className="h-4 w-4" />
</Button>
</div>
</div>
</CardContent>
</Card>
{/* Griglia Settimanale */}
{isLoading ? (
<Card>
<CardContent className="p-6">
<p className="text-center text-muted-foreground">Caricamento...</p>
</CardContent>
</Card>
) : error ? (
<Card>
<CardContent className="p-6">
<p className="text-center text-destructive">
Errore nel caricamento della pianificazione. Riprova più tardi.
</p>
</CardContent>
</Card>
) : scheduleData && scheduleData.guards.length > 0 ? (
<Card>
<CardContent className="p-0">
<div className="overflow-x-auto">
<table className="w-full border-collapse">
<thead>
<tr className="border-b bg-muted/50">
<th className="sticky left-0 z-10 bg-muted/50 text-left p-3 font-medium min-w-[180px]">
Guardia
</th>
{weekDays.map((day, index) => (
<th key={index} className="text-center p-3 font-medium min-w-[200px]">
<div>{format(day, "EEE", { locale: it })}</div>
<div className="text-xs text-muted-foreground font-normal">
{format(day, "dd/MM")}
</div>
</th>
))}
</tr>
</thead>
<tbody>
{scheduleData.guards.map((guardData) => (
<tr
key={guardData.guard.id}
className="border-b hover:bg-muted/30"
data-testid={`row-guard-${guardData.guard.id}`}
>
<td className="sticky left-0 z-10 bg-background p-3 font-medium border-r">
<div className="flex flex-col">
<span className="text-sm">
{guardData.guard.lastName} {guardData.guard.firstName}
</span>
<span className="text-xs text-muted-foreground">
#{guardData.guard.badgeNumber}
</span>
</div>
</td>
{weekDays.map((day, dayIndex) => {
const activity = getDayActivity(guardData, day);
return (
<td
key={dayIndex}
className="p-2 text-center align-middle"
>
{activity ? (
activity.type === "absence" ? (
<div
className="text-xs px-2 py-1.5 rounded bg-amber-100 dark:bg-amber-900/30 text-amber-900 dark:text-amber-300"
data-testid={`cell-absence-${guardData.guard.id}-${dayIndex}`}
>
{activity.label}
</div>
) : (
<Button
variant="outline"
size="sm"
className="w-full h-auto text-xs px-2 py-1.5 whitespace-normal hover-elevate"
onClick={() => handleCellClick(guardData, activity, day)}
data-testid={`button-shift-${guardData.guard.id}-${dayIndex}`}
>
{activity.label}
</Button>
)
) : (
<span className="text-xs text-muted-foreground">-</span>
)}
</td>
);
})}
</tr>
))}
</tbody>
</table>
</div>
</CardContent>
</Card>
) : (
<Card>
<CardContent className="p-6">
<p className="text-center text-muted-foreground">
Nessuna guardia trovata per la sede selezionata
</p>
</CardContent>
</Card>
)}
{/* Dialog Dettaglio Turno */}
<Dialog open={!!dialogData} onOpenChange={() => setDialogData(null)}>
<DialogContent className="max-w-2xl">
<DialogHeader>
<DialogTitle className="flex items-center gap-2">
{dialogData?.type === "fixed" ? (
<>
<MapPin className="h-5 w-5" />
Turno Fisso - {dialogData?.guardName}
</>
) : (
<>
<Navigation className="h-5 w-5" />
Turno Mobile - {dialogData?.guardName}
</>
)}
</DialogTitle>
<DialogDescription>
{dialogData?.date}
</DialogDescription>
</DialogHeader>
{dialogData && (
<div className="space-y-4">
{dialogData.type === "fixed" ? (
// Dettagli turno fisso
<div className="space-y-3">
<div className="bg-muted/30 p-3 rounded-md space-y-2">
<div className="flex items-center justify-between">
<span className="text-sm text-muted-foreground">Sito</span>
<span className="text-sm font-medium">{dialogData.data.siteName}</span>
</div>
<div className="flex items-center justify-between">
<span className="text-sm text-muted-foreground">Orario</span>
<div className="flex items-center gap-1 text-sm font-medium">
<Clock className="h-3 w-3" />
{format(new Date(dialogData.data.plannedStartTime), "HH:mm")} - {format(new Date(dialogData.data.plannedEndTime), "HH:mm")}
</div>
</div>
<div className="flex items-center justify-between">
<span className="text-sm text-muted-foreground">Durata</span>
<span className="text-sm font-bold">
{Math.round((new Date(dialogData.data.plannedEndTime).getTime() - new Date(dialogData.data.plannedStartTime).getTime()) / (1000 * 60 * 60))}h
</span>
</div>
</div>
<div className="p-3 bg-blue-500/10 border border-blue-500/20 rounded-md">
<p className="text-sm text-muted-foreground">
Per modificare questo turno, vai alla pagina <Link href="/general-planning" className="text-primary font-medium hover:underline">Planning Fissi</Link>
</p>
</div>
</div>
) : (
// Dettagli turno mobile
<div className="space-y-3">
<div className="bg-muted/30 p-3 rounded-md space-y-2">
<div className="flex items-center justify-between">
<span className="text-sm text-muted-foreground">Tipo</span>
<Badge variant="outline">Pattuglia</Badge>
</div>
<div className="flex items-center justify-between">
<span className="text-sm text-muted-foreground">Orario</span>
<div className="flex items-center gap-1 text-sm font-medium">
<Clock className="h-3 w-3" />
{dialogData.data.startTime} - {dialogData.data.endTime}
</div>
</div>
</div>
<div className="p-3 bg-blue-500/10 border border-blue-500/20 rounded-md">
<p className="text-sm text-muted-foreground">
Per visualizzare il percorso completo e modificare il turno, vai alla pagina <Link href="/planning-mobile" className="text-primary font-medium hover:underline">Planning Mobile</Link>
</p>
</div>
</div>
)}
</div>
)}
<DialogFooter>
<Button variant="outline" onClick={() => setDialogData(null)}>
Chiudi
</Button>
{dialogData?.type === "fixed" ? (
<Link href="/general-planning">
<Button data-testid="button-goto-planning-fissi">
<ExternalLink className="h-4 w-4 mr-2" />
Vai a Planning Fissi
</Button>
</Link>
) : (
<Link href="/planning-mobile">
<Button data-testid="button-goto-planning-mobile">
<ExternalLink className="h-4 w-4 mr-2" />
Vai a Planning Mobile
</Button>
</Link>
)}
</DialogFooter>
</DialogContent>
</Dialog>
</div>
);
}

56
package-lock.json generated
View File

@ -9,6 +9,9 @@
"version": "1.0.0", "version": "1.0.0",
"license": "MIT", "license": "MIT",
"dependencies": { "dependencies": {
"@dnd-kit/core": "^6.3.1",
"@dnd-kit/sortable": "^10.0.0",
"@dnd-kit/utilities": "^3.2.2",
"@hookform/resolvers": "^3.10.0", "@hookform/resolvers": "^3.10.0",
"@jridgewell/trace-mapping": "^0.3.25", "@jridgewell/trace-mapping": "^0.3.25",
"@neondatabase/serverless": "^0.10.4", "@neondatabase/serverless": "^0.10.4",
@ -417,6 +420,59 @@
"node": ">=6.9.0" "node": ">=6.9.0"
} }
}, },
"node_modules/@dnd-kit/accessibility": {
"version": "3.1.1",
"resolved": "https://registry.npmjs.org/@dnd-kit/accessibility/-/accessibility-3.1.1.tgz",
"integrity": "sha512-2P+YgaXF+gRsIihwwY1gCsQSYnu9Zyj2py8kY5fFvUM1qm2WA2u639R6YNVfU4GWr+ZM5mqEsfHZZLoRONbemw==",
"license": "MIT",
"dependencies": {
"tslib": "^2.0.0"
},
"peerDependencies": {
"react": ">=16.8.0"
}
},
"node_modules/@dnd-kit/core": {
"version": "6.3.1",
"resolved": "https://registry.npmjs.org/@dnd-kit/core/-/core-6.3.1.tgz",
"integrity": "sha512-xkGBRQQab4RLwgXxoqETICr6S5JlogafbhNsidmrkVv2YRs5MLwpjoF2qpiGjQt8S9AoxtIV603s0GIUpY5eYQ==",
"license": "MIT",
"dependencies": {
"@dnd-kit/accessibility": "^3.1.1",
"@dnd-kit/utilities": "^3.2.2",
"tslib": "^2.0.0"
},
"peerDependencies": {
"react": ">=16.8.0",
"react-dom": ">=16.8.0"
}
},
"node_modules/@dnd-kit/sortable": {
"version": "10.0.0",
"resolved": "https://registry.npmjs.org/@dnd-kit/sortable/-/sortable-10.0.0.tgz",
"integrity": "sha512-+xqhmIIzvAYMGfBYYnbKuNicfSsk4RksY2XdmJhT+HAC01nix6fHCztU68jooFiMUB01Ky3F0FyOvhG/BZrWkg==",
"license": "MIT",
"dependencies": {
"@dnd-kit/utilities": "^3.2.2",
"tslib": "^2.0.0"
},
"peerDependencies": {
"@dnd-kit/core": "^6.3.0",
"react": ">=16.8.0"
}
},
"node_modules/@dnd-kit/utilities": {
"version": "3.2.2",
"resolved": "https://registry.npmjs.org/@dnd-kit/utilities/-/utilities-3.2.2.tgz",
"integrity": "sha512-+MKAJEOfaBe5SmV6t34p80MMKhjvUz0vRrvVJbPT0WElzaOJ/1xs+D+KDv+tD/NE5ujfrChEcshd4fLn0wpiqg==",
"license": "MIT",
"dependencies": {
"tslib": "^2.0.0"
},
"peerDependencies": {
"react": ">=16.8.0"
}
},
"node_modules/@drizzle-team/brocli": { "node_modules/@drizzle-team/brocli": {
"version": "0.10.2", "version": "0.10.2",
"resolved": "https://registry.npmjs.org/@drizzle-team/brocli/-/brocli-0.10.2.tgz", "resolved": "https://registry.npmjs.org/@drizzle-team/brocli/-/brocli-0.10.2.tgz",

View File

@ -11,6 +11,9 @@
"db:push": "drizzle-kit push" "db:push": "drizzle-kit push"
}, },
"dependencies": { "dependencies": {
"@dnd-kit/core": "^6.3.1",
"@dnd-kit/sortable": "^10.0.0",
"@dnd-kit/utilities": "^3.2.2",
"@hookform/resolvers": "^3.10.0", "@hookform/resolvers": "^3.10.0",
"@jridgewell/trace-mapping": "^0.3.25", "@jridgewell/trace-mapping": "^0.3.25",
"@neondatabase/serverless": "^0.10.4", "@neondatabase/serverless": "^0.10.4",

View File

@ -34,8 +34,19 @@ The database supports managing users, guards, certifications, sites, shifts, shi
### Core Features ### Core Features
- **Multi-Sede Operational Planning**: Location-first approach for shift planning, filtering resources by selected branch. - **Multi-Sede Operational Planning**: Location-first approach for shift planning, filtering resources by selected branch.
- **Service Type Classification**: Classifies services as "fisso" (fixed posts) or "mobile" (patrols, inspections) to route sites to appropriate planning modules. - **Service Type Classification**: Classifies services as "fisso" (fixed posts) or "mobile" (patrols, inspections) to route sites to appropriate planning modules.
- **Planning Fissi**: Weekly planning grid for fixed posts, enabling shift creation with guard availability checks. - **Planning Fissi**: Weekly planning grid for fixed posts, enabling shift creation with guard availability checks. Includes weekly shift duplication feature with confirmation dialog and automatic navigation.
- **Planning Mobile**: Guard-centric interface for mobile services, displaying guard availability and hours, with an interactive Leaflet map showing sites. - **Planning Mobile**: Guard-centric interface for mobile services, displaying guard availability and hours, with an interactive Leaflet map showing sites. Features include:
- **Smart Site Assignment Indicators**: Sites already in patrol routes display "Assegnato a [Guard Name]" button with scroll-to functionality; unassigned sites show "Non assegnato" text
- **Drag-and-Drop Reordering**: Interactive drag-and-drop using @dnd-kit library for patrol route stops with visual feedback and automatic sequenceOrder persistence
- **Route Optimization**: OSRM API integration with TSP (Traveling Salesman Problem) nearest neighbor algorithm; displays total distance (km) and estimated travel time in dedicated dialog
- **Patrol Sequence List View**: Daily view of planned patrol routes with stops visualization
- **Custom Shift Timing**: Configurable start time and duration for each patrol route (replaces hardcoded 08:00-20:00)
- **Shift Overlap Validation**: POST /api/patrol-routes/check-overlaps endpoint verifies:
- No conflicts with existing fixed post shifts (shift_assignments)
- No conflicts with other mobile patrol routes
- Weekly hours compliance with contract parameters (maxHoursPerWeek + maxOvertimePerWeek)
- **Force-Save Dialog**: Interactive conflict resolution when saving patrol routes with overlaps or contractual limit violations; shows detailed conflict information and allows coordinator override
- **Multi-Day Duplication**: Duplication dialog supports "numero giorni consecutivi" field to create patrol sequences across N consecutive days; includes overlap validation (conservative approach: blocks entire operation if any day has conflicts)
- **Customer Management**: Full CRUD operations for customer details and customer-centric reporting with CSV export. - **Customer Management**: Full CRUD operations for customer details and customer-centric reporting with CSV export.
- **Dashboard Operativa**: Live KPIs and real-time shift status. - **Dashboard Operativa**: Live KPIs and real-time shift status.
- **Gestione Guardie**: Complete profiles with skill matrix, certification management, and badge numbers. - **Gestione Guardie**: Complete profiles with skill matrix, certification management, and badge numbers.
@ -44,6 +55,15 @@ The database supports managing users, guards, certifications, sites, shifts, shi
- **Advanced Planning**: Management of guard constraints, site preferences, contract parameters, training courses, holidays, and absences. Includes patrol route persistence and exclusivity constraints between fixed and mobile shifts. - **Advanced Planning**: Management of guard constraints, site preferences, contract parameters, training courses, holidays, and absences. Includes patrol route persistence and exclusivity constraints between fixed and mobile shifts.
- **Guard Planning Views**: Dedicated views for guards to see their fixed post shifts and mobile patrol routes. - **Guard Planning Views**: Dedicated views for guards to see their fixed post shifts and mobile patrol routes.
- **Site Planning View**: Coordinators can view all guards assigned to a specific site over a week. - **Site Planning View**: Coordinators can view all guards assigned to a specific site over a week.
- **Shift Duplication Features**:
- **Weekly Copy (Planning Fissi)**: POST /api/shift-assignments/copy-week endpoint duplicates all shifts and assignments from selected week to next week (+7 days) with atomic transaction. Frontend includes confirmation dialog with week details and success feedback.
- **Patrol Sequence Duplication (Planning Mobili)**: POST /api/patrol-routes/duplicate endpoint with dual behavior: UPDATE when target date = source date (modifies guard), CREATE when different date (duplicates route with all stops). Frontend shows daily sequence list with duplication dialog (date picker defaulting to next day, guard selector pre-filled but changeable).
- **Guardie Settimanale**: Compact weekly schedule view showing all guards' assignments across the week in a grid format. Features include:
- **Weekly Grid View**: Guard names in first column, 7 daily columns (Mon-Sun) with compact cell display
- **Multi-Source Aggregation**: GET /api/weekly-guards-schedule endpoint aggregates fixed shifts, patrol routes, and absences by location and week
- **Compact Cell Format**: Fixed posts show "Site Name HH:mm-HH:mm", mobile patrols show "Pattuglia HH:mm-HH:mm", absences show status (Ferie/Malattia/Permesso/Riposo)
- **Read-Only Dialogs**: Clicking cells opens appropriate dialog (fixed shift details or mobile patrol info) with navigation links to Planning Fissi/Mobile for edits
- **Location and Week Filters**: Dropdown for branch selection, week navigation with prev/next buttons displaying "Settimana dal DD MMM al DD MMM YYYY"
### User Roles ### User Roles
- **Admin**: Full access. - **Admin**: Full access.
@ -65,3 +85,5 @@ The system handles timezone conversions for shift times, converting Italy local
- **date-fns**: For date manipulation and formatting. - **date-fns**: For date manipulation and formatting.
- **Leaflet**: Interactive map library with react-leaflet bindings and OpenStreetMap tiles. - **Leaflet**: Interactive map library with react-leaflet bindings and OpenStreetMap tiles.
- **Nominatim**: OpenStreetMap geocoding API for automatic address-to-coordinates conversion. - **Nominatim**: OpenStreetMap geocoding API for automatic address-to-coordinates conversion.
- **OSRM (Open Source Routing Machine)**: Public API (router.project-osrm.org) for distance matrix calculation and route optimization in Planning Mobile. No authentication required.
- **@dnd-kit**: Drag-and-drop library (@dnd-kit/core, @dnd-kit/sortable, @dnd-kit/utilities) for interactive patrol route reordering.

View File

@ -4,8 +4,8 @@ import { storage } from "./storage";
import { setupAuth as setupReplitAuth, isAuthenticated as isAuthenticatedReplit } from "./replitAuth"; import { setupAuth as setupReplitAuth, isAuthenticated as isAuthenticatedReplit } from "./replitAuth";
import { setupLocalAuth, isAuthenticated as isAuthenticatedLocal } from "./localAuth"; import { setupLocalAuth, isAuthenticated as isAuthenticatedLocal } from "./localAuth";
import { db } from "./db"; import { db } from "./db";
import { guards, certifications, sites, shifts, shiftAssignments, users, insertShiftSchema, contractParameters, vehicles, serviceTypes, createMultiDayShiftSchema, insertCustomerSchema, customers, patrolRoutes, patrolRouteStops, insertPatrolRouteSchema } from "@shared/schema"; import { guards, certifications, sites, shifts, shiftAssignments, users, insertShiftSchema, contractParameters, vehicles, serviceTypes, createMultiDayShiftSchema, insertCustomerSchema, customers, patrolRoutes, patrolRouteStops, insertPatrolRouteSchema, absences } from "@shared/schema";
import { eq, and, gte, lte, desc, asc, ne, sql } from "drizzle-orm"; import { eq, and, gte, lte, lt, desc, asc, ne, sql } from "drizzle-orm";
import { differenceInDays, differenceInHours, differenceInMinutes, startOfWeek, endOfWeek, startOfMonth, endOfMonth, isWithinInterval, startOfDay, isSameDay, parseISO, format, isValid, addDays } from "date-fns"; import { differenceInDays, differenceInHours, differenceInMinutes, startOfWeek, endOfWeek, startOfMonth, endOfMonth, isWithinInterval, startOfDay, isSameDay, parseISO, format, isValid, addDays } from "date-fns";
import { z } from "zod"; import { z } from "zod";
import { fromZodError } from "zod-validation-error"; import { fromZodError } from "zod-validation-error";
@ -1337,6 +1337,129 @@ export async function registerRoutes(app: Express): Promise<Server> {
} }
}); });
// Copy weekly shift assignments to next week
app.post("/api/shift-assignments/copy-week", isAuthenticated, async (req, res) => {
try {
const { weekStart, location } = req.body;
if (!weekStart || !location) {
return res.status(400).json({ message: "Missing required fields: weekStart, location" });
}
// Parse week start date
const [year, month, day] = weekStart.split("-").map(Number);
if (!year || !month || !day) {
return res.status(400).json({ message: "Invalid weekStart format. Expected YYYY-MM-DD" });
}
// Calculate week boundaries (Monday to Sunday)
const weekStartDate = new Date(year, month - 1, day, 0, 0, 0, 0);
const weekEndDate = new Date(year, month - 1, day + 6, 23, 59, 59, 999);
console.log("📋 Copying weekly shifts:", {
weekStart: weekStartDate.toISOString(),
weekEnd: weekEndDate.toISOString(),
location
});
// Transaction: copy all shifts and assignments
const result = await db.transaction(async (tx) => {
// 1. Find all shifts in the source week filtered by location
const sourceShifts = await tx
.select({
shift: shifts,
site: sites
})
.from(shifts)
.innerJoin(sites, eq(shifts.siteId, sites.id))
.where(
and(
gte(shifts.startTime, weekStartDate),
lte(shifts.startTime, weekEndDate),
eq(sites.location, location)
)
);
if (sourceShifts.length === 0) {
throw new Error("Nessun turno trovato nella settimana selezionata");
}
console.log(`📋 Found ${sourceShifts.length} shifts to copy`);
let copiedShiftsCount = 0;
let copiedAssignmentsCount = 0;
// 2. For each shift, copy to next week (+7 days)
for (const { shift: sourceShift, site } of sourceShifts) {
// Calculate new dates (+7 days)
const newStartTime = new Date(sourceShift.startTime);
newStartTime.setDate(newStartTime.getDate() + 7);
const newEndTime = new Date(sourceShift.endTime);
newEndTime.setDate(newEndTime.getDate() + 7);
// Create new shift
const [newShift] = await tx
.insert(shifts)
.values({
siteId: sourceShift.siteId,
startTime: newStartTime,
endTime: newEndTime,
status: "planned",
vehicleId: sourceShift.vehicleId,
notes: sourceShift.notes,
})
.returning();
copiedShiftsCount++;
// 3. Copy all assignments for this shift
const sourceAssignments = await tx
.select()
.from(shiftAssignments)
.where(eq(shiftAssignments.shiftId, sourceShift.id));
for (const sourceAssignment of sourceAssignments) {
// Calculate new planned times (+7 days)
const newPlannedStart = new Date(sourceAssignment.plannedStartTime);
newPlannedStart.setDate(newPlannedStart.getDate() + 7);
const newPlannedEnd = new Date(sourceAssignment.plannedEndTime);
newPlannedEnd.setDate(newPlannedEnd.getDate() + 7);
// Create new assignment
await tx
.insert(shiftAssignments)
.values({
shiftId: newShift.id,
guardId: sourceAssignment.guardId,
plannedStartTime: newPlannedStart,
plannedEndTime: newPlannedEnd,
isArmedOnDuty: sourceAssignment.isArmedOnDuty,
assignedVehicleId: sourceAssignment.assignedVehicleId,
});
copiedAssignmentsCount++;
}
}
return { copiedShiftsCount, copiedAssignmentsCount };
});
res.json({
message: `Settimana copiata con successo: ${result.copiedShiftsCount} turni, ${result.copiedAssignmentsCount} assegnazioni`,
copiedShifts: result.copiedShiftsCount,
copiedAssignments: result.copiedAssignmentsCount,
});
} catch (error: any) {
console.error("❌ Error copying weekly shifts:", error);
res.status(500).json({
message: error.message || "Errore durante la copia dei turni settimanali",
error: String(error)
});
}
});
// Assign guard to site/date with specific time slot (supports multi-day assignments) // Assign guard to site/date with specific time slot (supports multi-day assignments)
app.post("/api/general-planning/assign-guard", isAuthenticated, async (req, res) => { app.post("/api/general-planning/assign-guard", isAuthenticated, async (req, res) => {
try { try {
@ -4133,6 +4256,282 @@ export async function registerRoutes(app: Express): Promise<Server> {
} }
}); });
// POST - Duplica o modifica patrol route
app.post("/api/patrol-routes/duplicate", isAuthenticated, async (req: any, res) => {
try {
const { sourceRouteId, targetDate, guardId } = req.body;
if (!sourceRouteId || !targetDate) {
return res.status(400).json({
message: "sourceRouteId e targetDate sono obbligatori"
});
}
// Carica patrol route sorgente con tutti gli stops
const sourceRoute = await db.query.patrolRoutes.findFirst({
where: eq(patrolRoutes.id, sourceRouteId),
with: {
stops: {
orderBy: (stops, { asc }) => [asc(stops.sequenceOrder)],
},
},
});
if (!sourceRoute) {
return res.status(404).json({ message: "Sequenza pattuglia sorgente non trovata" });
}
// Controlla se targetDate è uguale a sourceRoute.shiftDate
const sourceDate = new Date(sourceRoute.shiftDate).toISOString().split('T')[0];
const targetDateNormalized = new Date(targetDate).toISOString().split('T')[0];
if (sourceDate === targetDateNormalized) {
// UPDATE: stessa data, modifica solo guardia se fornita
if (guardId && guardId !== sourceRoute.guardId) {
const updated = await db
.update(patrolRoutes)
.set({ guardId })
.where(eq(patrolRoutes.id, sourceRouteId))
.returning();
return res.json({
action: "updated",
route: updated[0],
message: "Guardia assegnata alla sequenza esistente",
});
} else {
return res.status(400).json({
message: "Nessuna modifica da applicare (stessa data e stessa guardia)"
});
}
} else {
// CREATE: data diversa, duplica sequenza con stops
// Crea nuova patrol route
const newRoute = await db
.insert(patrolRoutes)
.values({
guardId: guardId || sourceRoute.guardId, // Usa nuova guardia o mantieni originale
shiftDate: targetDate,
startTime: sourceRoute.startTime,
endTime: sourceRoute.endTime,
status: "planned", // Nuova sequenza sempre in stato planned
location: sourceRoute.location,
notes: sourceRoute.notes,
})
.returning();
const newRouteId = newRoute[0].id;
// Duplica tutti gli stops
if (sourceRoute.stops && sourceRoute.stops.length > 0) {
const stopsData = sourceRoute.stops.map((stop) => ({
patrolRouteId: newRouteId,
siteId: stop.siteId,
sequenceOrder: stop.sequenceOrder,
estimatedArrivalTime: stop.estimatedArrivalTime,
}));
await db.insert(patrolRouteStops).values(stopsData);
}
return res.json({
action: "created",
route: newRoute[0],
copiedStops: sourceRoute.stops?.length || 0,
message: "Sequenza pattuglia duplicata con successo",
});
}
} catch (error) {
console.error("Error duplicating patrol route:", error);
res.status(500).json({ message: "Errore durante duplicazione sequenza pattuglia" });
}
});
// POST - Verifica sovrapposizioni turni e calcola ore settimanali
app.post("/api/patrol-routes/check-overlaps", isAuthenticated, async (req: any, res) => {
try {
const { guardId, shiftDate, startTime, endTime, excludeRouteId } = req.body;
if (!guardId || !shiftDate || !startTime || !endTime) {
return res.status(400).json({
message: "guardId, shiftDate, startTime e endTime sono obbligatori"
});
}
// Converte orari in timestamp per confronto
const shiftDateObj = new Date(shiftDate);
const [startHour, startMin] = startTime.split(':').map(Number);
const [endHour, endMin] = endTime.split(':').map(Number);
const startTimestamp = new Date(shiftDateObj);
startTimestamp.setHours(startHour, startMin, 0, 0);
const endTimestamp = new Date(shiftDateObj);
endTimestamp.setHours(endHour, endMin, 0, 0);
// Se endTime è minore di startTime, il turno attraversa la mezzanotte
if (endTimestamp <= startTimestamp) {
endTimestamp.setDate(endTimestamp.getDate() + 1);
}
const conflicts = [];
// 1. Controlla sovrapposizioni con shift_assignments (turni fissi)
const fixedShifts = await db
.select({
id: shiftAssignments.id,
siteName: sites.name,
plannedStartTime: shiftAssignments.plannedStartTime,
plannedEndTime: shiftAssignments.plannedEndTime,
})
.from(shiftAssignments)
.leftJoin(shifts, eq(shiftAssignments.shiftId, shifts.id))
.leftJoin(sites, eq(shifts.siteId, sites.id))
.where(eq(shiftAssignments.guardId, guardId));
for (const shift of fixedShifts) {
const shiftStart = new Date(shift.plannedStartTime);
const shiftEnd = new Date(shift.plannedEndTime);
// Controlla sovrapposizione
if (startTimestamp < shiftEnd && endTimestamp > shiftStart) {
conflicts.push({
type: 'fisso',
siteName: shift.siteName,
startTime: shift.plannedStartTime,
endTime: shift.plannedEndTime,
});
}
}
// 2. Controlla sovrapposizioni con patrol_routes (turni mobili)
const mobileShifts = await db
.select()
.from(patrolRoutes)
.where(
and(
eq(patrolRoutes.guardId, guardId),
excludeRouteId ? ne(patrolRoutes.id, excludeRouteId) : undefined
)
);
for (const route of mobileShifts) {
const routeDateObj = new Date(route.shiftDate);
const [rStartHour, rStartMin] = route.startTime.split(':').map(Number);
const [rEndHour, rEndMin] = route.endTime.split(':').map(Number);
const routeStart = new Date(routeDateObj);
routeStart.setHours(rStartHour, rStartMin, 0, 0);
const routeEnd = new Date(routeDateObj);
routeEnd.setHours(rEndHour, rEndMin, 0, 0);
if (routeEnd <= routeStart) {
routeEnd.setDate(routeEnd.getDate() + 1);
}
// Controlla sovrapposizione
if (startTimestamp < routeEnd && endTimestamp > routeStart) {
conflicts.push({
type: 'mobile',
shiftDate: route.shiftDate,
startTime: route.startTime,
endTime: route.endTime,
});
}
}
// 3. Calcola ore settimanali
const weekStart = new Date(shiftDateObj);
weekStart.setDate(weekStart.getDate() - weekStart.getDay() + (weekStart.getDay() === 0 ? -6 : 1));
weekStart.setHours(0, 0, 0, 0);
const weekEnd = new Date(weekStart);
weekEnd.setDate(weekEnd.getDate() + 7);
let totalWeeklyHours = 0;
// Ore da turni fissi
const weeklyFixedShifts = await db
.select({
plannedStartTime: shiftAssignments.plannedStartTime,
plannedEndTime: shiftAssignments.plannedEndTime,
})
.from(shiftAssignments)
.where(
and(
eq(shiftAssignments.guardId, guardId),
gte(shiftAssignments.plannedStartTime, weekStart),
lt(shiftAssignments.plannedStartTime, weekEnd)
)
);
for (const shift of weeklyFixedShifts) {
const start = new Date(shift.plannedStartTime);
const end = new Date(shift.plannedEndTime);
const hours = (end.getTime() - start.getTime()) / (1000 * 60 * 60);
totalWeeklyHours += hours;
}
// Ore da turni mobili
const weeklyMobileShifts = await db
.select()
.from(patrolRoutes)
.where(
and(
eq(patrolRoutes.guardId, guardId),
gte(patrolRoutes.shiftDate, weekStart.toISOString().split('T')[0]),
lt(patrolRoutes.shiftDate, weekEnd.toISOString().split('T')[0]),
excludeRouteId ? ne(patrolRoutes.id, excludeRouteId) : undefined
)
);
for (const route of weeklyMobileShifts) {
const [rStartHour, rStartMin] = route.startTime.split(':').map(Number);
const [rEndHour, rEndMin] = route.endTime.split(':').map(Number);
let hours = rEndHour - rStartHour + (rEndMin - rStartMin) / 60;
if (hours < 0) hours += 24; // Turno attraversa mezzanotte
totalWeeklyHours += hours;
}
// Aggiungi ore del nuovo turno
const newShiftHours = (endTimestamp.getTime() - startTimestamp.getTime()) / (1000 * 60 * 60);
const totalHoursWithNew = totalWeeklyHours + newShiftHours;
// 4. Recupera limiti contrattuali
const contractParams = await db
.select()
.from(contractParameters)
.limit(1);
const maxHours = contractParams[0]?.maxHoursPerWeek || 40;
const maxOvertime = contractParams[0]?.maxOvertimePerWeek || 8;
const totalMaxHours = maxHours + maxOvertime;
const exceedsContractLimit = totalHoursWithNew > totalMaxHours;
res.json({
hasConflicts: conflicts.length > 0,
conflicts,
weeklyHours: {
current: Math.round(totalWeeklyHours * 100) / 100,
withNewShift: Math.round(totalHoursWithNew * 100) / 100,
newShiftHours: Math.round(newShiftHours * 100) / 100,
maxRegular: maxHours,
maxOvertime: maxOvertime,
maxTotal: totalMaxHours,
exceedsLimit: exceedsContractLimit,
},
});
} catch (error) {
console.error("Error checking overlaps:", error);
res.status(500).json({ message: "Errore verifica sovrapposizioni" });
}
});
// ============= GEOCODING API (Nominatim/OSM) ============= // ============= GEOCODING API (Nominatim/OSM) =============
// Rate limiter semplice per rispettare 1 req/sec di Nominatim // Rate limiter semplice per rispettare 1 req/sec di Nominatim
@ -4197,6 +4596,217 @@ export async function registerRoutes(app: Express): Promise<Server> {
} }
}); });
// ============= ROUTE OPTIMIZATION API (OSRM + TSP) =============
app.post("/api/optimize-route", isAuthenticated, async (req: any, res) => {
try {
const { coordinates } = req.body;
// Validazione: array di coordinate [{lat, lon, id}]
if (!Array.isArray(coordinates) || coordinates.length < 2) {
return res.status(400).json({
message: "Almeno 2 coordinate richieste per l'ottimizzazione"
});
}
// Verifica formato coordinate
for (const coord of coordinates) {
if (!coord.lat || !coord.lon || !coord.id) {
return res.status(400).json({
message: "Ogni coordinata deve avere lat, lon e id"
});
}
}
// STEP 1: Calcola matrice distanze usando OSRM Table API
const coordsString = coordinates.map(c => `${c.lon},${c.lat}`).join(';');
const osrmTableUrl = `https://router.project-osrm.org/table/v1/driving/${coordsString}?annotations=distance,duration`;
const osrmResponse = await fetch(osrmTableUrl);
if (!osrmResponse.ok) {
throw new Error(`OSRM API error: ${osrmResponse.status}`);
}
const osrmData = await osrmResponse.json();
if (osrmData.code !== 'Ok' || !osrmData.distances || !osrmData.durations) {
throw new Error("OSRM non ha restituito dati validi");
}
const distances = osrmData.distances; // Matrice NxN in metri
const durations = osrmData.durations; // Matrice NxN in secondi
// STEP 2: Applica algoritmo TSP Nearest Neighbor
// Inizia dalla prima tappa (indice 0)
const n = coordinates.length;
const visited = new Set<number>();
const route: number[] = [];
let current = 0;
let totalDistance = 0;
let totalDuration = 0;
visited.add(current);
route.push(current);
// Trova sempre il vicino più vicino non visitato
for (let i = 1; i < n; i++) {
let nearest = -1;
let minDistance = Infinity;
for (let j = 0; j < n; j++) {
if (!visited.has(j) && distances[current][j] < minDistance) {
minDistance = distances[current][j];
nearest = j;
}
}
if (nearest !== -1) {
totalDistance += distances[current][nearest];
totalDuration += durations[current][nearest];
visited.add(nearest);
route.push(nearest);
current = nearest;
}
}
// Ritorna al punto di partenza (circuito chiuso)
totalDistance += distances[current][0];
totalDuration += durations[current][0];
// STEP 3: Prepara risposta
const optimizedRoute = route.map(index => ({
...coordinates[index],
order: route.indexOf(index) + 1,
}));
res.json({
optimizedRoute,
totalDistanceMeters: Math.round(totalDistance),
totalDistanceKm: (totalDistance / 1000).toFixed(2),
totalDurationSeconds: Math.round(totalDuration),
totalDurationMinutes: Math.round(totalDuration / 60),
estimatedTimeFormatted: formatDuration(totalDuration),
});
} catch (error) {
console.error("Error optimizing route:", error);
res.status(500).json({ message: "Errore durante l'ottimizzazione del percorso" });
}
});
// Helper per formattare durata in ore e minuti
function formatDuration(seconds: number): string {
const hours = Math.floor(seconds / 3600);
const minutes = Math.floor((seconds % 3600) / 60);
if (hours > 0) {
return `${hours}h ${minutes}m`;
}
return `${minutes}m`;
}
// ============= WEEKLY GUARDS SCHEDULE =============
app.get("/api/weekly-guards-schedule", isAuthenticated, async (req: any, res) => {
try {
const location = req.query.location as string;
const startDate = req.query.startDate as string;
if (!location || !startDate) {
return res.status(400).json({ message: "Location e startDate richiesti" });
}
// Calcola l'intervallo della settimana (7 giorni da startDate)
const weekStart = parseISO(startDate);
const weekEnd = addDays(weekStart, 6);
// Recupera tutte le guardie della sede
const guardsInLocation = await db
.select()
.from(guards)
.where(eq(guards.location, location))
.orderBy(guards.lastName, guards.firstName);
// Prepara la struttura dati
const guardsSchedule = [];
for (const guard of guardsInLocation) {
// Recupera turni fissi della settimana (shift_assignments)
const fixedShifts = await db
.select({
assignmentId: shiftAssignments.id,
shiftId: shifts.id,
plannedStartTime: shiftAssignments.plannedStartTime,
plannedEndTime: shiftAssignments.plannedEndTime,
siteName: sites.name,
siteId: sites.id,
})
.from(shiftAssignments)
.innerJoin(shifts, eq(shiftAssignments.shiftId, shifts.id))
.innerJoin(sites, eq(shifts.siteId, sites.id))
.where(
and(
eq(shiftAssignments.guardId, guard.id),
gte(shiftAssignments.plannedStartTime, weekStart),
lte(shiftAssignments.plannedStartTime, weekEnd)
)
);
// Recupera turni mobili della settimana (patrol_routes)
const mobileShifts = await db
.select({
routeId: patrolRoutes.id,
shiftDate: patrolRoutes.shiftDate,
startTime: patrolRoutes.startTime,
endTime: patrolRoutes.endTime,
})
.from(patrolRoutes)
.where(
and(
eq(patrolRoutes.guardId, guard.id),
gte(sql`${patrolRoutes.shiftDate}`, startDate),
lte(sql`${patrolRoutes.shiftDate}`, format(weekEnd, 'yyyy-MM-dd'))
)
);
// Recupera assenze della settimana
const absencesData = await db
.select()
.from(absences)
.where(
and(
eq(absences.guardId, guard.id),
lte(sql`${absences.startDate}`, format(weekEnd, 'yyyy-MM-dd')),
gte(sql`${absences.endDate}`, startDate)
)
);
guardsSchedule.push({
guard: {
id: guard.id,
firstName: guard.firstName,
lastName: guard.lastName,
badgeNumber: guard.badgeNumber,
},
fixedShifts,
mobileShifts,
absences: absencesData,
});
}
res.json({
weekStart: format(weekStart, 'yyyy-MM-dd'),
weekEnd: format(weekEnd, 'yyyy-MM-dd'),
location,
guards: guardsSchedule,
});
} catch (error) {
console.error("Error fetching weekly guards schedule:", error);
res.status(500).json({ message: "Errore nel recupero della pianificazione settimanale" });
}
});
const httpServer = createServer(app); const httpServer = createServer(app);
return httpServer; return httpServer;
} }

View File

@ -1,7 +1,55 @@
{ {
"version": "1.0.52", "version": "1.1.1",
"lastUpdate": "2025-10-24T14:53:47.910Z", "lastUpdate": "2025-11-15T10:11:44.404Z",
"changelog": [ "changelog": [
{
"version": "1.1.1",
"date": "2025-11-15",
"type": "patch",
"description": "Deployment automatico v1.1.1"
},
{
"version": "1.1.0",
"date": "2025-10-25",
"type": "minor",
"description": "Deployment automatico v1.1.0"
},
{
"version": "1.0.58",
"date": "2025-10-25",
"type": "patch",
"description": "Deployment automatico v1.0.58"
},
{
"version": "1.0.57",
"date": "2025-10-25",
"type": "patch",
"description": "Deployment automatico v1.0.57"
},
{
"version": "1.0.56",
"date": "2025-10-25",
"type": "patch",
"description": "Deployment automatico v1.0.56"
},
{
"version": "1.0.55",
"date": "2025-10-25",
"type": "patch",
"description": "Deployment automatico v1.0.55"
},
{
"version": "1.0.54",
"date": "2025-10-24",
"type": "patch",
"description": "Deployment automatico v1.0.54"
},
{
"version": "1.0.53",
"date": "2025-10-24",
"type": "patch",
"description": "Deployment automatico v1.0.53"
},
{ {
"version": "1.0.52", "version": "1.0.52",
"date": "2025-10-24", "date": "2025-10-24",
@ -253,54 +301,6 @@
"date": "2025-10-17", "date": "2025-10-17",
"type": "patch", "type": "patch",
"description": "Deployment automatico v1.0.11" "description": "Deployment automatico v1.0.11"
},
{
"version": "1.0.10",
"date": "2025-10-17",
"type": "patch",
"description": "Deployment automatico v1.0.10"
},
{
"version": "1.0.9",
"date": "2025-10-17",
"type": "patch",
"description": "Deployment automatico v1.0.9"
},
{
"version": "1.0.8",
"date": "2025-10-17",
"type": "patch",
"description": "Deployment automatico v1.0.8"
},
{
"version": "1.0.7",
"date": "2025-10-17",
"type": "patch",
"description": "Deployment automatico v1.0.7"
},
{
"version": "1.0.6",
"date": "2025-10-17",
"type": "patch",
"description": "Deployment automatico v1.0.6"
},
{
"version": "1.0.5",
"date": "2025-10-17",
"type": "patch",
"description": "Deployment automatico v1.0.5"
},
{
"version": "1.0.4",
"date": "2025-10-17",
"type": "patch",
"description": "Deployment automatico v1.0.4"
},
{
"version": "1.0.3",
"date": "2025-10-17",
"type": "patch",
"description": "Deployment automatico v1.0.3"
} }
] ]
} }