add dispute functionality for contract duty and implement collection dates

This commit is contained in:
Marshal
2026-07-26 17:58:37 +00:00
parent bfea9de660
commit 225d2c8229
5 changed files with 233 additions and 27 deletions

View File

@@ -563,6 +563,11 @@ export function canCreateSchedule(user: AuthUser | null | undefined): boolean {
return hasPermission(user, FREIGHT_PERMS.trainScheduling.create);
}
/** Dispatching a train and opening/closing its booking window are both updates. */
export function canUpdateSchedule(user: AuthUser | null | undefined): boolean {
return hasPermission(user, FREIGHT_PERMS.trainScheduling.update);
}
export function canViewFleet(user: AuthUser | null | undefined): boolean {
return hasPermission(user, FREIGHT_PERMS.fleet.view);
}

View File

@@ -24,10 +24,13 @@ import {
CalendarClock,
Clock,
Eye,
Lock,
MoreHorizontal,
Navigation,
Pencil,
Play,
Send,
Unlock,
Train,
Weight,
} from "lucide-react";
@@ -56,7 +59,7 @@ import { api } from "@/services/api";
import { formatRouteLabel } from "@/services/routes.service";
import { useToast } from "@/hooks/use-toast";
import { useAuth } from "@/auth/useAuth";
import { canCreateSchedule } from "@/lib/permissions";
import { canCreateSchedule, canUpdateSchedule } from "@/lib/permissions";
import type {
FreightType,
TrainScheduleListFilters,
@@ -103,6 +106,7 @@ export default function TrainScheduleV2ListPage() {
const { toast } = useToast();
const { user } = useAuth();
const canCreate = canCreateSchedule(user);
const canUpdate = canUpdateSchedule(user);
const { viewMode, setViewMode } = useFleetViewMode("train-scheduling-v2");
const { pagination, setPagination } = usePagination({ pageSize: 10 });
const [search, setSearch] = useState("");
@@ -121,6 +125,12 @@ export default function TrainScheduleV2ListPage() {
const [sortDir, setSortDir] = useState<"desc" | "asc">("desc");
const [createOpen, setCreateOpen] = useState(false);
const [windowSettingsId, setWindowSettingsId] = useState<string | null>(null);
// Dispatching a train and closing its booking window are both irreversible
// from this screen, so each goes through an explicit confirmation.
const [dispatchTarget, setDispatchTarget] =
useState<TrainScheduleListItem | null>(null);
const [windowTarget, setWindowTarget] =
useState<TrainScheduleListItem | null>(null);
const [editDateSchedule, setEditDateSchedule] =
useState<TrainScheduleListItem | null>(null);
const [routeId, setRouteId] = useState("");
@@ -200,6 +210,12 @@ export default function TrainScheduleV2ListPage() {
}),
);
const create = useMutation(api.trainScheduling.createSchedule.mutationOptions());
const dispatchSchedule = useMutation(
api.trainScheduling.dispatchSchedule.mutationOptions(),
);
const setWindow = useMutation(
api.trainScheduling.setBookingWindow.mutationOptions(),
);
// const cancel = useMutation(api.trainScheduling.cancelSchedule.mutationOptions());
// Intercity (same-country / DOMESTIC) routes cannot be scheduled yet — the
@@ -468,6 +484,38 @@ export default function TrainScheduleV2ListPage() {
Booking window settings
</Menu.Item>
) : null}
{/* Stop / resume taking bookings on this departure without
opening it. FULL is a capacity state the engine owns, so
only an explicitly OPEN/CLOSED window is toggled here. */}
{canUpdate &&
["DRAFT", "SCHEDULED"].includes(schedule.status) &&
schedule.bookingWindowStatus !== "FULL" ? (
<Menu.Item
leftSection={
schedule.bookingWindowStatus === "CLOSED" ? (
<Unlock size={15} />
) : (
<Lock size={15} />
)
}
onClick={() => setWindowTarget(schedule)}
>
{schedule.bookingWindowStatus === "CLOSED"
? "Open booking window"
: "Close booking window"}
</Menu.Item>
) : null}
{/* Start the run. Same transition as the detail page's
Dispatch button — that page also shows unassigned-wagon
and not-loaded warnings, so it stays the fuller surface. */}
{canUpdate && schedule.status === "SCHEDULED" ? (
<Menu.Item
leftSection={<Play size={15} />}
onClick={() => setDispatchTarget(schedule)}
>
Start (dispatch) train
</Menu.Item>
) : null}
{/* Cancel schedule — hidden for now (frontend only; the
cancelSchedule mutation is untouched). Restore by
uncommenting.
@@ -853,6 +901,125 @@ export default function TrainScheduleV2ListPage() {
onClose={() => setEditDateSchedule(null)}
onSaved={() => void schedulesQuery.refetch()}
/>
<Modal
opened={windowTarget != null}
onClose={() => setWindowTarget(null)}
title={
windowTarget?.bookingWindowStatus === "CLOSED"
? "Open booking window?"
: "Close booking window?"
}
centered
radius="md"
>
<Stack gap="md">
<Text size="sm" c="dimmed">
{windowTarget?.bookingWindowStatus === "CLOSED" ? (
<>
Customers will be able to book onto{" "}
<Text span fw={600} c="dark">
{windowTarget?.trainNumber ?? windowTarget?.reference ?? "this departure"}
</Text>{" "}
again, up to its remaining capacity.
</>
) : (
<>
No further bookings will be accepted onto{" "}
<Text span fw={600} c="dark">
{windowTarget?.trainNumber ?? windowTarget?.reference ?? "this departure"}
</Text>
. Bookings already aboard are unaffected, and you can reopen the
window from this menu.
</>
)}
</Text>
<Group justify="flex-end" gap="sm">
<Button variant="default" onClick={() => setWindowTarget(null)}>
Cancel
</Button>
<Button
color={windowTarget?.bookingWindowStatus === "CLOSED" ? "edr-green" : "orange"}
loading={setWindow.isPending}
onClick={async () => {
if (!windowTarget) return;
const next =
windowTarget.bookingWindowStatus === "CLOSED" ? "OPEN" : "CLOSED";
try {
await setWindow.mutateAsync({ id: windowTarget.id, status: next });
toast({
title:
next === "OPEN" ? "Booking window opened" : "Booking window closed",
});
setWindowTarget(null);
void schedulesQuery.refetch();
} catch (err) {
toast({
title: "Could not update the booking window",
description: parseError(err, "Update failed"),
variant: "destructive",
});
}
}}
>
{windowTarget?.bookingWindowStatus === "CLOSED"
? "Open window"
: "Close window"}
</Button>
</Group>
</Stack>
</Modal>
<Modal
opened={dispatchTarget != null}
onClose={() => setDispatchTarget(null)}
title="Dispatch this train?"
centered
radius="md"
>
<Stack gap="md">
<Text size="sm" c="dimmed">
<Text span fw={600} c="dark">
{dispatchTarget?.trainNumber ?? dispatchTarget?.reference ?? "This train"}
</Text>{" "}
departs {dispatchTarget?.origin ?? "its origin"} for{" "}
{dispatchTarget?.destination ?? "its destination"} and its booking
window closes. This cannot be undone.
</Text>
<Text size="xs" c="dimmed">
Open the schedule detail first if you want to check for unassigned
wagons or cargo not yet marked loaded those warnings are shown
there, not here.
</Text>
<Group justify="flex-end" gap="sm">
<Button variant="default" onClick={() => setDispatchTarget(null)}>
Cancel
</Button>
<Button
color="edr-green"
leftSection={<Play size={16} />}
loading={dispatchSchedule.isPending}
onClick={async () => {
if (!dispatchTarget) return;
try {
await dispatchSchedule.mutateAsync(dispatchTarget.id);
toast({ title: "Train dispatched" });
setDispatchTarget(null);
void schedulesQuery.refetch();
} catch (err) {
toast({
title: "Dispatch failed",
description: parseError(err, "Could not dispatch"),
variant: "destructive",
});
}
}}
>
Dispatch train
</Button>
</Group>
</Stack>
</Modal>
</PageContainer>
);
}

View File

@@ -203,6 +203,8 @@ export interface TrainScheduleListItem {
totalLengthMeters: number;
bookingsCount: number;
status: TrainScheduleStatus | string;
/** Whether the departure is still taking bookings (orthogonal to `status`). */
bookingWindowStatus?: "OPEN" | "FULL" | "CLOSED" | string;
}
export type TrainScheduleSortField =