Compare commits
36 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
591687b000 | ||
|
|
ee5d1aaa24 | ||
|
|
fc6f5a39f8 | ||
|
|
758a697447 | ||
|
|
40cde1634b | ||
|
|
7b05c8cbce | ||
|
|
d868c6ee31 | ||
|
|
a10b50e7e9 | ||
|
|
c8825a9b4c | ||
|
|
7961971ad0 | ||
|
|
3e3b9851a8 | ||
|
|
3fc0c55fd2 | ||
|
|
2eb53bb1b2 | ||
|
|
a9f3453755 | ||
|
|
dd84ddb35b | ||
|
|
5c22ec14f1 | ||
|
|
efa056dd98 | ||
|
|
b132082ffc | ||
|
|
e5ce415aeb | ||
|
|
34bdb99599 | ||
|
|
3cc1739015 | ||
|
|
20f24ba25e | ||
|
|
bd55070abc | ||
|
|
7a6fd3245b | ||
|
|
bafc34065e | ||
|
|
b4c6400360 | ||
|
|
753e01d612 | ||
|
|
3b3056f6b8 | ||
|
|
2b62d8ff4e | ||
|
|
1639244169 | ||
|
|
2cd6c32ad9 | ||
|
|
0a72b413fa | ||
|
|
1bad21cf9e | ||
|
|
6366382753 | ||
|
|
0b64fd2f08 | ||
|
|
36bfad3815 |
4
.replit
4
.replit
@ -19,6 +19,10 @@ externalPort = 80
|
||||
localPort = 33035
|
||||
externalPort = 3001
|
||||
|
||||
[[ports]]
|
||||
localPort = 40417
|
||||
externalPort = 8000
|
||||
|
||||
[[ports]]
|
||||
localPort = 41295
|
||||
externalPort = 5173
|
||||
|
||||
@ -30,6 +30,7 @@ import PlanningMobile from "@/pages/planning-mobile";
|
||||
import MyShiftsFixed from "@/pages/my-shifts-fixed";
|
||||
import MyShiftsMobile from "@/pages/my-shifts-mobile";
|
||||
import SitePlanningView from "@/pages/site-planning-view";
|
||||
import WeeklyGuards from "@/pages/weekly-guards";
|
||||
|
||||
function Router() {
|
||||
const { isAuthenticated, isLoading } = useAuth();
|
||||
@ -57,6 +58,7 @@ function Router() {
|
||||
<Route path="/my-shifts-fixed" component={MyShiftsFixed} />
|
||||
<Route path="/my-shifts-mobile" component={MyShiftsMobile} />
|
||||
<Route path="/site-planning-view" component={SitePlanningView} />
|
||||
<Route path="/weekly-guards" component={WeeklyGuards} />
|
||||
<Route path="/reports" component={Reports} />
|
||||
<Route path="/notifications" component={Notifications} />
|
||||
<Route path="/users" component={Users} />
|
||||
|
||||
@ -12,6 +12,11 @@ import {
|
||||
Car,
|
||||
Briefcase,
|
||||
Navigation,
|
||||
ChevronDown,
|
||||
FileText,
|
||||
FolderKanban,
|
||||
Building2,
|
||||
Wrench,
|
||||
} from "lucide-react";
|
||||
import { Link, useLocation } from "wouter";
|
||||
import {
|
||||
@ -23,15 +28,31 @@ import {
|
||||
SidebarMenu,
|
||||
SidebarMenuButton,
|
||||
SidebarMenuItem,
|
||||
SidebarMenuSub,
|
||||
SidebarMenuSubItem,
|
||||
SidebarMenuSubButton,
|
||||
SidebarHeader,
|
||||
SidebarFooter,
|
||||
} from "@/components/ui/sidebar";
|
||||
import {
|
||||
Collapsible,
|
||||
CollapsibleContent,
|
||||
CollapsibleTrigger,
|
||||
} from "@/components/ui/collapsible";
|
||||
import { useAuth } from "@/hooks/useAuth";
|
||||
import { Avatar, AvatarFallback, AvatarImage } from "@/components/ui/avatar";
|
||||
import { Button } from "@/components/ui/button";
|
||||
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",
|
||||
url: "/",
|
||||
@ -39,47 +60,47 @@ const menuItems = [
|
||||
roles: ["admin", "coordinator", "guard", "client"],
|
||||
},
|
||||
{
|
||||
title: "Turni",
|
||||
url: "/shifts",
|
||||
icon: Calendar,
|
||||
roles: ["admin", "coordinator", "guard"],
|
||||
},
|
||||
{
|
||||
title: "Pianificazione",
|
||||
url: "/planning",
|
||||
icon: ClipboardList,
|
||||
title: "Planning",
|
||||
icon: FolderKanban,
|
||||
roles: ["admin", "coordinator"],
|
||||
},
|
||||
items: [
|
||||
{
|
||||
title: "Pianificazione Operativa",
|
||||
url: "/operational-planning",
|
||||
icon: Calendar,
|
||||
roles: ["admin", "coordinator"],
|
||||
},
|
||||
{
|
||||
title: "Planning Fissi",
|
||||
title: "Fissi",
|
||||
url: "/general-planning",
|
||||
icon: BarChart3,
|
||||
icon: Calendar,
|
||||
roles: ["admin", "coordinator"],
|
||||
},
|
||||
{
|
||||
title: "Planning Mobile",
|
||||
title: "Mobili",
|
||||
url: "/planning-mobile",
|
||||
icon: Navigation,
|
||||
roles: ["admin", "coordinator"],
|
||||
},
|
||||
{
|
||||
title: "Planning di Servizio",
|
||||
title: "Vista",
|
||||
url: "/service-planning",
|
||||
icon: ClipboardList,
|
||||
roles: ["admin", "coordinator"],
|
||||
},
|
||||
{
|
||||
title: "Gestione Pianificazioni",
|
||||
url: "/advanced-planning",
|
||||
icon: ClipboardList,
|
||||
title: "Guardie Settimanale",
|
||||
url: "/weekly-guards",
|
||||
icon: Users,
|
||||
roles: ["admin", "coordinator"],
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
title: "Scadenziario",
|
||||
url: "/advanced-planning",
|
||||
icon: Calendar,
|
||||
roles: ["admin", "coordinator"],
|
||||
},
|
||||
{
|
||||
title: "Anagrafiche",
|
||||
icon: Building2,
|
||||
roles: ["admin", "coordinator"],
|
||||
items: [
|
||||
{
|
||||
title: "Guardie",
|
||||
url: "/guards",
|
||||
@ -98,6 +119,19 @@ const menuItems = [
|
||||
icon: Briefcase,
|
||||
roles: ["admin", "coordinator"],
|
||||
},
|
||||
{
|
||||
title: "Automezzi",
|
||||
url: "/vehicles",
|
||||
icon: Car,
|
||||
roles: ["admin", "coordinator"],
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
title: "Tipologia",
|
||||
icon: Wrench,
|
||||
roles: ["admin", "coordinator"],
|
||||
items: [
|
||||
{
|
||||
title: "Servizi",
|
||||
url: "/services",
|
||||
@ -105,23 +139,31 @@ const menuItems = [
|
||||
roles: ["admin", "coordinator"],
|
||||
},
|
||||
{
|
||||
title: "Parco Automezzi",
|
||||
url: "/vehicles",
|
||||
icon: Car,
|
||||
title: "Contratti",
|
||||
url: "/parameters",
|
||||
icon: Settings,
|
||||
roles: ["admin", "coordinator"],
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
title: "Report",
|
||||
url: "/reports",
|
||||
icon: BarChart3,
|
||||
roles: ["admin", "coordinator", "client"],
|
||||
items: [
|
||||
{
|
||||
title: "Report Amministrativo",
|
||||
url: "/reports",
|
||||
icon: FileText,
|
||||
roles: ["admin", "coordinator", "client"],
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
title: "Notifiche",
|
||||
url: "/notifications",
|
||||
icon: Bell,
|
||||
title: "Utilità",
|
||||
icon: Settings,
|
||||
roles: ["admin", "coordinator", "guard"],
|
||||
},
|
||||
items: [
|
||||
{
|
||||
title: "Utenti",
|
||||
url: "/users",
|
||||
@ -129,10 +171,12 @@ const menuItems = [
|
||||
roles: ["admin"],
|
||||
},
|
||||
{
|
||||
title: "Parametri",
|
||||
url: "/parameters",
|
||||
icon: Settings,
|
||||
roles: ["admin", "coordinator"],
|
||||
title: "Notifiche",
|
||||
url: "/notifications",
|
||||
icon: Bell,
|
||||
roles: ["admin", "coordinator", "guard"],
|
||||
},
|
||||
],
|
||||
},
|
||||
];
|
||||
|
||||
@ -140,9 +184,78 @@ export function AppSidebar() {
|
||||
const { user } = useAuth();
|
||||
const [location] = useLocation();
|
||||
|
||||
const filteredItems = menuItems.filter(
|
||||
(item) => user && item.roles.includes(user.role)
|
||||
const filterMenuItems = (items: MenuItem[]): MenuItem[] => {
|
||||
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 (
|
||||
<Sidebar>
|
||||
@ -161,20 +274,7 @@ export function AppSidebar() {
|
||||
<SidebarGroupLabel>Menu Principale</SidebarGroupLabel>
|
||||
<SidebarGroupContent>
|
||||
<SidebarMenu>
|
||||
{filteredItems.map((item) => (
|
||||
<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>
|
||||
))}
|
||||
{filteredItems.map(renderMenuItem)}
|
||||
</SidebarMenu>
|
||||
</SidebarGroupContent>
|
||||
</SidebarGroup>
|
||||
|
||||
@ -8,7 +8,7 @@ import { Button } from "@/components/ui/button";
|
||||
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select";
|
||||
import { Input } from "@/components/ui/input";
|
||||
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 { Skeleton } from "@/components/ui/skeleton";
|
||||
import {
|
||||
@ -117,6 +117,7 @@ export default function GeneralPlanning() {
|
||||
const [consecutiveDays, setConsecutiveDays] = useState<number>(1);
|
||||
const [showOvertimeGuards, setShowOvertimeGuards] = useState<boolean>(false);
|
||||
const [ccnlConfirmation, setCcnlConfirmation] = useState<{ message: string; data: any } | null>(null);
|
||||
const [showCopyWeekConfirmation, setShowCopyWeekConfirmation] = useState<boolean>(false);
|
||||
|
||||
// Query per dati planning settimanale
|
||||
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
|
||||
const handleAssignGuard = () => {
|
||||
if (!selectedCell || !selectedGuardId) return;
|
||||
@ -358,7 +407,7 @@ export default function GeneralPlanning() {
|
||||
</div>
|
||||
|
||||
{/* Navigazione settimana */}
|
||||
<div className="flex items-center gap-2">
|
||||
<div className="flex items-center gap-2 flex-wrap">
|
||||
<Button
|
||||
variant="outline"
|
||||
size="icon"
|
||||
@ -385,6 +434,16 @@ export default function GeneralPlanning() {
|
||||
>
|
||||
<ChevronRight className="h-4 w-4" />
|
||||
</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>
|
||||
|
||||
{/* Info settimana */}
|
||||
@ -658,19 +717,19 @@ export default function GeneralPlanning() {
|
||||
})()}
|
||||
</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 =>
|
||||
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 (
|
||||
<div className="space-y-2">
|
||||
<div className="flex items-center justify-between">
|
||||
<Label htmlFor="guard-select">Guardia Disponibile</Label>
|
||||
<Label htmlFor="guard-select">Guardia</Label>
|
||||
{!isLoadingGuards && hasOvertimeGuards && (
|
||||
<Button
|
||||
variant="outline"
|
||||
@ -699,15 +758,20 @@ export default function GeneralPlanning() {
|
||||
{filteredGuards.length > 0 ? (
|
||||
filteredGuards.map((guard) => (
|
||||
<SelectItem key={guard.guardId} value={guard.guardId}>
|
||||
<div className={`flex items-center gap-1.5 ${guard.isAvailable ? "" : "text-destructive font-medium"}`}>
|
||||
{!guard.isAvailable && <Circle className="h-3 w-3 fill-current" />}
|
||||
<span>
|
||||
{guard.guardName} ({guard.badgeNumber}) - {guard.ordinaryHoursRemaining}h ord.
|
||||
{guard.requiresOvertime && ` + ${guard.overtimeHoursRemaining}h strao.`}
|
||||
{guard.requiresOvertime && " 🔸"}
|
||||
</span>
|
||||
</div>
|
||||
</SelectItem>
|
||||
))
|
||||
) : (
|
||||
<SelectItem value="no-guards" disabled>
|
||||
{showOvertimeGuards
|
||||
? "Nessuna guardia disponibile"
|
||||
? "Nessuna guardia"
|
||||
: "Nessuna guardia con ore ordinarie (prova 'Mostra Straordinario')"}
|
||||
</SelectItem>
|
||||
)}
|
||||
@ -715,7 +779,7 @@ export default function GeneralPlanning() {
|
||||
</Select>
|
||||
{filteredGuards.length === 0 && !showOvertimeGuards && hasOvertimeGuards && (
|
||||
<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>
|
||||
)}
|
||||
{filteredGuards.length > 0 && selectedGuardId && (
|
||||
@ -988,6 +1052,60 @@ export default function GeneralPlanning() {
|
||||
</AlertDialogFooter>
|
||||
</AlertDialogContent>
|
||||
</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>
|
||||
);
|
||||
}
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
448
client/src/pages/weekly-guards.tsx
Normal file
448
client/src/pages/weekly-guards.tsx
Normal 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>
|
||||
);
|
||||
}
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
BIN
database-backups/vigilanzaturni_v1.0.53_20251024_163455.sql.gz
Normal file
BIN
database-backups/vigilanzaturni_v1.0.53_20251024_163455.sql.gz
Normal file
Binary file not shown.
BIN
database-backups/vigilanzaturni_v1.0.54_20251024_172526.sql.gz
Normal file
BIN
database-backups/vigilanzaturni_v1.0.54_20251024_172526.sql.gz
Normal file
Binary file not shown.
BIN
database-backups/vigilanzaturni_v1.0.55_20251025_072838.sql.gz
Normal file
BIN
database-backups/vigilanzaturni_v1.0.55_20251025_072838.sql.gz
Normal file
Binary file not shown.
BIN
database-backups/vigilanzaturni_v1.0.56_20251025_074951.sql.gz
Normal file
BIN
database-backups/vigilanzaturni_v1.0.56_20251025_074951.sql.gz
Normal file
Binary file not shown.
BIN
database-backups/vigilanzaturni_v1.0.57_20251025_080211.sql.gz
Normal file
BIN
database-backups/vigilanzaturni_v1.0.57_20251025_080211.sql.gz
Normal file
Binary file not shown.
BIN
database-backups/vigilanzaturni_v1.0.58_20251025_090445.sql.gz
Normal file
BIN
database-backups/vigilanzaturni_v1.0.58_20251025_090445.sql.gz
Normal file
Binary file not shown.
BIN
database-backups/vigilanzaturni_v1.1.0_20251025_090838.sql.gz
Normal file
BIN
database-backups/vigilanzaturni_v1.1.0_20251025_090838.sql.gz
Normal file
Binary file not shown.
BIN
database-backups/vigilanzaturni_v1.1.1_20251115_101122.sql.gz
Normal file
BIN
database-backups/vigilanzaturni_v1.1.1_20251115_101122.sql.gz
Normal file
Binary file not shown.
56
package-lock.json
generated
56
package-lock.json
generated
@ -9,6 +9,9 @@
|
||||
"version": "1.0.0",
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"@dnd-kit/core": "^6.3.1",
|
||||
"@dnd-kit/sortable": "^10.0.0",
|
||||
"@dnd-kit/utilities": "^3.2.2",
|
||||
"@hookform/resolvers": "^3.10.0",
|
||||
"@jridgewell/trace-mapping": "^0.3.25",
|
||||
"@neondatabase/serverless": "^0.10.4",
|
||||
@ -417,6 +420,59 @@
|
||||
"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": {
|
||||
"version": "0.10.2",
|
||||
"resolved": "https://registry.npmjs.org/@drizzle-team/brocli/-/brocli-0.10.2.tgz",
|
||||
|
||||
@ -11,6 +11,9 @@
|
||||
"db:push": "drizzle-kit push"
|
||||
},
|
||||
"dependencies": {
|
||||
"@dnd-kit/core": "^6.3.1",
|
||||
"@dnd-kit/sortable": "^10.0.0",
|
||||
"@dnd-kit/utilities": "^3.2.2",
|
||||
"@hookform/resolvers": "^3.10.0",
|
||||
"@jridgewell/trace-mapping": "^0.3.25",
|
||||
"@neondatabase/serverless": "^0.10.4",
|
||||
|
||||
26
replit.md
26
replit.md
@ -34,8 +34,19 @@ The database supports managing users, guards, certifications, sites, shifts, shi
|
||||
### Core Features
|
||||
- **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.
|
||||
- **Planning Fissi**: Weekly planning grid for fixed posts, enabling shift creation with guard availability checks.
|
||||
- **Planning Mobile**: Guard-centric interface for mobile services, displaying guard availability and hours, with an interactive Leaflet map showing sites.
|
||||
- **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. 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.
|
||||
- **Dashboard Operativa**: Live KPIs and real-time shift status.
|
||||
- **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.
|
||||
- **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.
|
||||
- **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
|
||||
- **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.
|
||||
- **Leaflet**: Interactive map library with react-leaflet bindings and OpenStreetMap tiles.
|
||||
- **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.
|
||||
614
server/routes.ts
614
server/routes.ts
@ -4,8 +4,8 @@ import { storage } from "./storage";
|
||||
import { setupAuth as setupReplitAuth, isAuthenticated as isAuthenticatedReplit } from "./replitAuth";
|
||||
import { setupLocalAuth, isAuthenticated as isAuthenticatedLocal } from "./localAuth";
|
||||
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 { eq, and, gte, lte, desc, asc, ne, sql } from "drizzle-orm";
|
||||
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, 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 { z } from "zod";
|
||||
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)
|
||||
app.post("/api/general-planning/assign-guard", isAuthenticated, async (req, res) => {
|
||||
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) =============
|
||||
|
||||
// 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);
|
||||
return httpServer;
|
||||
}
|
||||
|
||||
100
version.json
100
version.json
@ -1,7 +1,55 @@
|
||||
{
|
||||
"version": "1.0.52",
|
||||
"lastUpdate": "2025-10-24T14:53:47.910Z",
|
||||
"version": "1.1.1",
|
||||
"lastUpdate": "2025-11-15T10:11:44.404Z",
|
||||
"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",
|
||||
"date": "2025-10-24",
|
||||
@ -253,54 +301,6 @@
|
||||
"date": "2025-10-17",
|
||||
"type": "patch",
|
||||
"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"
|
||||
}
|
||||
]
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user