Merge branch 'contrat-backup2' of github.com:Tria-plc/edr-platform into contrat-backup2

This commit is contained in:
marshal
2026-07-03 06:39:02 +03:00
41 changed files with 2480 additions and 124 deletions

View File

@@ -419,10 +419,10 @@ const buildSidebarSections = (demoItems: SidebarItem[]): SidebarSection[] => [
label: "Contract validity",
href: "/dashboard/configuration/contract-validity-periods",
},
// {
// label: "Train scheduling rules",
// href: "/dashboard/configuration/train-scheduling-rules",
// },
{
label: "Train scheduling rules",
href: "/dashboard/configuration/train-scheduling-rules",
},
],
},
{

View File

@@ -48,6 +48,7 @@ import type {
} from "@/types/trainScheduling";
import { ContainerPlacementGrid } from "./ContainerPlacementGrid";
import { locomotiveOption, showScheduleWarnings } from "./locomotiveOptions";
import {
autoFillPlacements,
mergePlacementsWithSaved,
@@ -274,6 +275,7 @@ export function AllocateBookingWizard({
const created = await create.mutateAsync({
payload: { routeId, scheduleDate, locomotiveIds },
});
showScheduleWarnings(created.warnings);
setSelectedScheduleId(created.id);
return created.id;
};
@@ -536,10 +538,9 @@ export function AllocateBookingWizard({
placeholder={
routeId ? "Select at least two locomotives" : "Select a route first"
}
data={(locomotivesQuery.data ?? []).map((l) => ({
value: l.id,
label: `${l.code}${l.name ? ` · ${l.name}` : ""}`,
}))}
data={(locomotivesQuery.data ?? []).map((l) =>
locomotiveOption(l, " · "),
)}
value={locomotiveIds}
onChange={setLocomotiveIds}
searchable

View File

@@ -1,6 +1,8 @@
import type { ReactNode } from "react";
import { Box, Group, Progress, Text, Tooltip } from "@mantine/core";
import type { BookingWindowPhase } from "@/types/trainScheduling";
import "./batchVisuals.css";
/**
@@ -213,3 +215,69 @@ export function HeroChip({
</Group>
);
}
const PHASE_META: Record<
BookingWindowPhase,
{ color: string; label: string; pulse: boolean }
> = {
PRE_WINDOW: { color: "gray", label: "Pre-window", pulse: false },
OPEN: { color: "edr-green", label: "Booking open", pulse: true },
DOC_REVIEW: { color: "yellow", label: "Doc review", pulse: true },
PAYMENT: { color: "blue", label: "Payment", pulse: true },
CLOSED_FOR_DAY: { color: "dark", label: "Closed for day", pulse: false },
DONE: { color: "dark", label: "Done", pulse: false },
};
/**
* Import booking-cycle phase pill (OPEN → DOC_REVIEW → PAYMENT → …) with an
* optional cycle number. Same visual language as `WindowStatusPill`.
*/
export function WindowPhasePill({
phase,
cycleNo,
size = "md",
}: {
phase: BookingWindowPhase;
cycleNo?: number;
size?: "sm" | "md";
}) {
const meta = PHASE_META[phase] ?? {
color: "gray",
label: phase,
pulse: false,
};
const compact = size === "sm";
return (
<Group
gap={6}
wrap="nowrap"
style={{
display: "inline-flex",
padding: compact ? "2px 8px" : "4px 11px",
borderRadius: 999,
background: `var(--mantine-color-${meta.color}-0)`,
border: `1px solid var(--mantine-color-${meta.color}-2)`,
}}
>
<Box
w={compact ? 6 : 7}
h={compact ? 6 : 7}
className={meta.pulse ? "bb-pulse-dot" : undefined}
style={{
borderRadius: 999,
flexShrink: 0,
background: `var(--mantine-color-${meta.color}-6)`,
}}
/>
<Text
size="xs"
fw={700}
c={`${meta.color}.8`}
style={{ letterSpacing: 0.3, lineHeight: 1, whiteSpace: "nowrap" }}
>
{meta.label}
{cycleNo && cycleNo > 1 ? ` · cycle ${cycleNo}` : ""}
</Text>
</Group>
);
}

View File

@@ -0,0 +1,50 @@
import hotToast from "react-hot-toast";
import type { LocomotiveRecord } from "@/types/trainScheduling";
/**
* Locomotives can now be scheduled in advance: not-at-origin-yard or
* already-on-future-schedules is allowed with a warning (only OUT_OF_SERVICE
* is blocked server-side). This returns the hint to surface in the picker,
* or null when the locomotive is ready at the origin yard.
*/
export function locomotiveWarning(loco: LocomotiveRecord): string | null {
const hints: string[] = [];
if (loco.atOriginYard === false) hints.push("not at origin yard");
const futureCount = loco.futureScheduleCount ?? 0;
if (futureCount > 0) {
hints.push(`on ${futureCount} future schedule${futureCount === 1 ? "" : "s"}`);
}
return hints.length ? hints.join(" · ") : null;
}
/** MultiSelect option for the schedule-creation locomotive picker. */
export function locomotiveOption(
loco: LocomotiveRecord,
nameSeparator = " — ",
): { value: string; label: string } {
const base = `${loco.code}${loco.name ? `${nameSeparator}${loco.name}` : ""}`;
const warning = locomotiveWarning(loco);
return {
value: loco.id,
label: warning ? `${base} · ⚠ ${warning}` : base,
};
}
/**
* Yellow toast listing create-schedule warnings (e.g. locomotive not at the
* origin yard yet). The shared `useToast` hook only knows success/error, so
* this styles a react-hot-toast directly.
*/
export function showScheduleWarnings(warnings?: string[] | null): void {
if (!warnings?.length) return;
hotToast(warnings.join("\n"), {
icon: "⚠️",
duration: 8000,
style: {
background: "var(--mantine-color-yellow-0)",
color: "var(--mantine-color-yellow-9)",
border: "1px solid var(--mantine-color-yellow-4)",
},
});
}

View File

@@ -261,6 +261,8 @@ export const URL_CONSTANTS = {
BATCH_BOARD_DETAIL: (scheduleId: string) =>
`/train-scheduling/batch-board/${scheduleId}`,
RUN_BATCH: (id: string) => `/train-scheduling/schedules/${id}/run-batch`,
DOC_REVIEW_COMPLETE: (id: string) =>
`/train-scheduling/schedules/${id}/doc-review-complete`,
RUN_ALLOCATION: (id: string) => `/train-scheduling/schedules/${id}/run-allocation`,
ASSIGN_UNASSIGNED_BOOKING: (id: string) =>
`/train-scheduling/schedules/${id}/assign-unassigned-booking`,

View File

@@ -41,6 +41,7 @@ import {
BookingPipeline,
HeroChip,
totalBookingCount,
WindowPhasePill,
WindowStatusPill,
} from "@/components/trainScheduling/batchVisuals";
import { RouteCorridor } from "@/components/trainScheduling/scheduleVisuals";
@@ -233,7 +234,16 @@ function ScheduleCard({ schedule }: { schedule: BatchBoardSchedule }) {
</Text>
</Box>
</Group>
<WindowStatusPill status={schedule.bookingWindowStatus} />
<Stack gap={4} align="flex-end">
<WindowStatusPill status={schedule.bookingWindowStatus} />
{schedule.windowPhase ? (
<WindowPhasePill
phase={schedule.windowPhase}
cycleNo={schedule.bookingCycleNo}
size="sm"
/>
) : null}
</Stack>
</Group>
<RouteCorridor

View File

@@ -24,6 +24,7 @@ import {
CalendarDays,
CheckCircle2,
ChevronLeft,
ClipboardCheck,
ChevronRight,
Clock,
FileSignature,
@@ -52,6 +53,7 @@ import {
BookingPipeline,
HeroChip,
totalBookingCount,
WindowPhasePill,
WindowStatusPill,
} from "@/components/trainScheduling/batchVisuals";
import { RouteCorridor } from "@/components/trainScheduling/scheduleVisuals";
@@ -62,6 +64,7 @@ import { useToast } from "@/hooks/use-toast";
import type {
BatchBoardBookingDetail,
BatchBoardBookingState,
BatchBoardScheduleDetail,
BatchWindowGroup,
BookingAllocationStatus,
} from "@/types/trainScheduling";
@@ -114,6 +117,61 @@ const fmtDateTime = (iso: string | null) =>
}).format(new Date(iso))
: "—";
const eatDayFmt = new Intl.DateTimeFormat("en-CA", {
timeZone: "Africa/Addis_Ababa",
year: "numeric",
month: "2-digit",
day: "2-digit",
});
/** "11:00 EAT" if the timestamp falls on today (EAT), else "05 Jun, 11:00 EAT". */
const fmtPhaseTime = (iso: string) => {
const date = new Date(iso);
const time = new Intl.DateTimeFormat("en-GB", {
hour: "2-digit",
minute: "2-digit",
hour12: false,
timeZone: "Africa/Addis_Ababa",
}).format(date);
if (eatDayFmt.format(date) === eatDayFmt.format(new Date())) {
return `${time} EAT`;
}
const day = new Intl.DateTimeFormat("en-GB", {
day: "2-digit",
month: "short",
timeZone: "Africa/Addis_Ababa",
}).format(date);
return `${day}, ${time} EAT`;
};
/** Countdown label for the current booking-cycle phase, e.g. "Closes 11:00 EAT". */
function phaseCountdown(data: BatchBoardScheduleDetail): string | null {
switch (data.windowPhase) {
case "PRE_WINDOW":
return data.windowOpensAt
? `Opens ${fmtPhaseTime(data.windowOpensAt)}`
: null;
case "OPEN":
return data.windowClosesAt
? `Closes ${fmtPhaseTime(data.windowClosesAt)}`
: null;
case "DOC_REVIEW":
return data.docReviewEndsAt
? `Doc review ends ${fmtPhaseTime(data.docReviewEndsAt)}`
: null;
case "PAYMENT":
return data.paymentPhaseEndsAt
? `Payment ends ${fmtPhaseTime(data.paymentPhaseEndsAt)}`
: null;
case "CLOSED_FOR_DAY":
return data.windowOpensAt
? `Reopens ${fmtPhaseTime(data.windowOpensAt)}`
: null;
default:
return null;
}
}
const initials = (name: string) =>
name
.split(/\s+/)
@@ -462,6 +520,9 @@ export default function BatchScheduleDetailPage() {
const runAllocation = useMutation(
api.trainScheduling.runAllocation.mutationOptions(),
);
const completeDocReview = useMutation(
api.trainScheduling.completeDocReview.mutationOptions(),
);
const hasAssignedWagons = useMemo(
() =>
@@ -607,6 +668,24 @@ export default function BatchScheduleDetailPage() {
);
const selectedDay = dayGroups[selectedIndex];
const handleCompleteDocReview = () => {
completeDocReview
.mutateAsync(scheduleId ?? "")
.then(() => {
toast({
title: "Document review complete",
description: "Batch is running for this route-day group",
});
void refetch();
})
.catch(() => {
toast({
title: "Could not complete document review",
variant: "destructive",
});
});
};
const handleRunAllocation = () => {
runAllocation
.mutateAsync({ scheduleId: scheduleId ?? "" })
@@ -641,6 +720,7 @@ export default function BatchScheduleDetailPage() {
}
const totalBookings = totalBookingCount(data.counts);
const countdown = phaseCountdown(data);
return (
<PageContainer fluid>
@@ -689,6 +769,12 @@ export default function BatchScheduleDetailPage() {
{data.trainNumber ?? data.routeName ?? "Schedule"}
</Title>
<WindowStatusPill status={data.bookingWindowStatus} />
{data.windowPhase ? (
<WindowPhasePill
phase={data.windowPhase}
cycleNo={data.bookingCycleNo}
/>
) : null}
<HeroChip>{data.status}</HeroChip>
</Group>
<RouteCorridor
@@ -717,6 +803,12 @@ export default function BatchScheduleDetailPage() {
{data.locomotive.maxTrainLengthMeters} m
</HeroChip>
) : null}
{data.windowPhase ? (
<HeroChip icon={<Clock size={12} />}>
Cycle {data.bookingCycleNo}
{countdown ? ` · ${countdown}` : ""}
</HeroChip>
) : null}
</Group>
</Stack>
@@ -730,6 +822,17 @@ export default function BatchScheduleDetailPage() {
>
Refresh
</Button>
{data.windowPhase === "DOC_REVIEW" ? (
<Button
color="yellow"
radius="md"
leftSection={<ClipboardCheck size={16} />}
loading={completeDocReview.isPending}
onClick={handleCompleteDocReview}
>
Doc review complete run batch
</Button>
) : null}
<Button
color="edr-green"
radius="md"

View File

@@ -36,6 +36,10 @@ import { KpiStrip, PageContainer, PageHeader } from "@/components/page";
import RuleEngineListFooter from "@/components/ruleEngine/RuleEngineListFooter";
import { ruleEngineTable } from "@/components/ruleEngine/ruleEngineStyles";
import { FreightTypeBadge } from "@/components/trainScheduling/ScheduleStatusBadge";
import {
locomotiveOption,
showScheduleWarnings,
} from "@/components/trainScheduling/locomotiveOptions";
import {
RouteCorridor,
StatusPill,
@@ -110,7 +114,7 @@ export default function TrainScheduleV2ListPage() {
selectedRoute.originYard?.label ??
selectedRoute.originYard?.code ??
"the route origin yard";
return `Only locomotives currently at ${originLabel} are shown`;
return `All in-service locomotives are shown — those not yet at ${originLabel} or already on future schedules are flagged`;
}, [selectedRoute]);
useEffect(() => {
@@ -356,6 +360,7 @@ export default function TrainScheduleV2ListPage() {
payload: { routeId, scheduleDate, locomotiveIds },
});
toast({ title: "Train schedule created" });
showScheduleWarnings(created.warnings);
setCreateOpen(false);
navigate(`/dashboard/operations/train-scheduling-v2/${created.id}`);
} catch (err) {
@@ -554,10 +559,7 @@ export default function TrainScheduleV2ListPage() {
placeholder={
routeId ? "Select at least two locomotives" : "Select a route first"
}
data={(locomotivesQuery.data ?? []).map((l) => ({
value: l.id,
label: `${l.code}${l.name ? `${l.name}` : ""}`,
}))}
data={(locomotivesQuery.data ?? []).map((l) => locomotiveOption(l))}
value={locomotiveIds}
onChange={setLocomotiveIds}
searchable

View File

@@ -34,6 +34,13 @@ export default function TrainSchedulingGlobalRulesPage() {
maxWagonsPerTrain: Number(form.maxWagonsPerTrain),
max20ftContainerWeightTons: Number(form.max20ftContainerWeightTons),
max20ftPairWeightDiffTons: Number(form.max20ftPairWeightDiffTons),
importWindowLeadDays: Number(form.importWindowLeadDays),
exportBookingLeadHours: Number(form.exportBookingLeadHours),
windowOpenHour: Number(form.windowOpenHour),
windowDurationHours: Number(form.windowDurationHours),
docReviewMinutes: Number(form.docReviewMinutes),
paymentWindowMinutes: Number(form.paymentWindowMinutes),
reopenDelayMinutes: Number(form.reopenDelayMinutes),
});
setForm(updated);
toast({ title: "Train scheduling rules saved" });
@@ -108,6 +115,87 @@ export default function TrainSchedulingGlobalRulesPage() {
min={0}
disabled={loading}
/>
</Stack>
</Card>
<Card maw={720} mt="md">
<Stack gap="md">
<PageHeader
title="Booking windows"
subtitle="Import booking-day cycle and export lead time. All times in Addis Ababa (EAT)."
/>
<NumberInput
label="Import window lead (days)"
description="The single booking day opens this many days before departure"
value={form.importWindowLeadDays ?? ""}
onChange={(value) =>
setForm((current) => ({ ...current, importWindowLeadDays: Number(value) }))
}
min={0}
disabled={loading}
/>
<NumberInput
label="Export booking lead (hours)"
description="Export bookings are accepted first-come-first-serve starting this many hours before departure"
value={form.exportBookingLeadHours ?? ""}
onChange={(value) =>
setForm((current) => ({ ...current, exportBookingLeadHours: Number(value) }))
}
min={1}
disabled={loading}
/>
<NumberInput
label="Window open hour (EAT)"
description="Local hour the import window opens on its booking day (e.g. 8 = 08:00)"
value={form.windowOpenHour ?? ""}
onChange={(value) =>
setForm((current) => ({ ...current, windowOpenHour: Number(value) }))
}
min={0}
max={23}
disabled={loading}
/>
<NumberInput
label="Window duration (hours)"
value={form.windowDurationHours ?? ""}
onChange={(value) =>
setForm((current) => ({ ...current, windowDurationHours: Number(value) }))
}
min={0.25}
max={12}
step={0.25}
disabled={loading}
/>
<NumberInput
label="Document review (minutes)"
description="Max staff time to accept booking documents after the window closes"
value={form.docReviewMinutes ?? ""}
onChange={(value) =>
setForm((current) => ({ ...current, docReviewMinutes: Number(value) }))
}
min={0}
disabled={loading}
/>
<NumberInput
label="Payment window (minutes)"
description="Time a selected customer has to pay before the slot expires"
value={form.paymentWindowMinutes ?? ""}
onChange={(value) =>
setForm((current) => ({ ...current, paymentWindowMinutes: Number(value) }))
}
min={1}
disabled={loading}
/>
<NumberInput
label="Reopen delay (minutes)"
description="Delay after window close before reopening when the train is not full (90 = 11:00 close → 12:30 reopen)"
value={form.reopenDelayMinutes ?? ""}
onChange={(value) =>
setForm((current) => ({ ...current, reopenDelayMinutes: Number(value) }))
}
min={1}
disabled={loading}
/>
<Group justify="flex-end">
<Button loading={saving} disabled={loading} onClick={() => void handleSave()}>
Save rules

View File

@@ -360,6 +360,14 @@ export const api = {
() => TRAIN_SCHEDULING_INVALIDATIONS,
),
completeDocReview: endpoint<string, BatchBoardScheduleDetail>(
"train-scheduling",
"doc-review-complete",
(id) => trainSchedulingService.completeDocReview(id),
undefined,
() => TRAIN_SCHEDULING_INVALIDATIONS,
),
setBookingWindow: endpoint<
{ id: string; status: "OPEN" | "CLOSED" },
TrainScheduleDetail

View File

@@ -158,6 +158,20 @@ export const trainSchedulingService = {
return unwrap(response.data);
},
/**
* Staff finished reviewing documents early — runs the batch immediately
* for the schedule's whole route-day group.
*/
completeDocReview: async (
scheduleId: string,
): Promise<BatchBoardScheduleDetail> => {
const response = await client.post<BatchBoardScheduleDetail>(
URL_CONSTANTS.TRAIN_SCHEDULING.DOC_REVIEW_COMPLETE(scheduleId),
{},
);
return unwrap(response.data);
},
runAllocation: async (
scheduleId: string,
): Promise<WagonAllocationAttemptResult> => {
@@ -494,16 +508,7 @@ export const trainSchedulingService = {
},
updateGlobalRules: async (
payload: Partial<
Pick<
TrainSchedulingGlobalRules,
| "maxTrainLengthMeters"
| "maxTrainWeightTons"
| "maxWagonsPerTrain"
| "max20ftContainerWeightTons"
| "max20ftPairWeightDiffTons"
>
>,
payload: Partial<Omit<TrainSchedulingGlobalRules, "id">>,
): Promise<TrainSchedulingGlobalRules> => {
const response = await client.patch<TrainSchedulingGlobalRules>(
URL_CONSTANTS.TRAIN_SCHEDULING.GLOBAL_RULES,

View File

@@ -105,6 +105,13 @@ export interface TrainSchedulingGlobalRules {
maxWagonsPerTrain: number;
max20ftContainerWeightTons: number;
max20ftPairWeightDiffTons: number;
importWindowLeadDays: number;
exportBookingLeadHours: number;
windowOpenHour: number;
windowDurationHours: number;
docReviewMinutes: number;
paymentWindowMinutes: number;
reopenDelayMinutes: number;
}
export interface TrainSchedulePreviewResponse {
@@ -136,6 +143,10 @@ export interface LocomotiveRecord {
status: "AVAILABLE" | "ASSIGNED" | "MAINTENANCE" | "OUT_OF_SERVICE";
currentYardId?: string | null;
locomotiveType?: "DIESEL" | "ELECTRIC";
/** Whether the locomotive is currently at the route's origin yard. */
atOriginYard?: boolean;
/** Number of upcoming schedules this locomotive is already assigned to. */
futureScheduleCount?: number;
}
export interface TrainScheduleListItem {
@@ -183,6 +194,18 @@ export interface BookableSchedule {
locomotive: { id: string; code: string; name?: string | null } | null;
}
/**
* Import booking-cycle phase for a schedule's booking window (null for
* legacy/DOMESTIC schedules that don't run the one-day cycle).
*/
export type BookingWindowPhase =
| "PRE_WINDOW"
| "OPEN"
| "DOC_REVIEW"
| "PAYMENT"
| "CLOSED_FOR_DAY"
| "DONE";
export type BatchBoardBookingState =
| "ALLOCATED"
| "SELECTED_FOR_BATCH"
@@ -212,6 +235,13 @@ export interface BatchBoardSchedule {
scheduleDate: string | null;
status: string;
bookingWindowStatus: string;
direction: string | null;
windowPhase: BookingWindowPhase | null;
windowOpensAt: string | null;
windowClosesAt: string | null;
docReviewEndsAt: string | null;
paymentPhaseEndsAt: string | null;
bookingCycleNo: number;
locomotive: {
code: string;
name: string | null;
@@ -278,6 +308,13 @@ export interface BatchBoardScheduleDetail {
scheduleDate: string | null;
status: string;
bookingWindowStatus: string;
direction: string | null;
windowPhase: BookingWindowPhase | null;
windowOpensAt: string | null;
windowClosesAt: string | null;
docReviewEndsAt: string | null;
paymentPhaseEndsAt: string | null;
bookingCycleNo: number;
locomotive: BatchBoardSchedule["locomotive"];
capacity: BatchBoardSchedule["capacity"];
counts: BatchBoardSchedule["counts"];