Compare commits
20 Commits
main
...
sandbox-ma
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ac1a7900c8 | ||
|
|
511d80b629 | ||
|
|
c183dda383 | ||
|
|
0f88207f3f | ||
|
|
9f1af71d15 | ||
|
|
f231d87ae3 | ||
|
|
d0ab254dd7 | ||
|
|
7790361a0a | ||
|
|
05a30b2a21 | ||
|
|
ea92b32618 | ||
|
|
538b06bd4b | ||
|
|
b6eed8b6db | ||
|
|
a0ed517047 | ||
|
|
0ad2451dd4 | ||
|
|
d172eaf629 | ||
|
|
363c9fbf4f | ||
|
|
a369a69978 | ||
|
|
1fe0b4dbf9 | ||
|
|
945ff2dc09 | ||
|
|
ffc39a5c90 |
55
README.md
55
README.md
@@ -20,6 +20,61 @@ You can start editing the page by modifying `app/page.tsx`. The page auto-update
|
||||
|
||||
This project uses [`next/font`](https://nextjs.org/docs/app/building-your-application/optimizing/fonts) to automatically optimize and load [Geist](https://vercel.com/font), a new font family for Vercel.
|
||||
|
||||
## Downtime Action Reminders
|
||||
|
||||
Reminders are sent by calling `POST /api/downtime/actions/reminders`. This endpoint does not run automatically, so you need to schedule it with cron or systemd. It sends at most one reminder per threshold (1w/1d/1h/overdue) and resets if the due date changes.
|
||||
The secret can be any random string; it just needs to match what your scheduler sends in the Authorization header.
|
||||
|
||||
1) Set a secret in your env file (example: `/etc/mis-control-tower.env`):
|
||||
|
||||
```
|
||||
DOWNTIME_ACTION_REMINDER_SECRET=your-secret-here
|
||||
APP_BASE_URL=https://your-domain
|
||||
```
|
||||
|
||||
2) Cron example (runs hourly for 1w/1d/1h/overdue thresholds):
|
||||
|
||||
```
|
||||
0 * * * * . /etc/mis-control-tower.env && curl -s -X POST "$APP_BASE_URL/api/downtime/actions/reminders?dueInDays=7" -H "Authorization: Bearer $DOWNTIME_ACTION_REMINDER_SECRET"
|
||||
```
|
||||
|
||||
If you prefer systemd instead of cron, you can create a small service + timer that runs the same curl command.
|
||||
|
||||
Example systemd units:
|
||||
|
||||
`/etc/systemd/system/mis-control-tower-reminders.service`
|
||||
|
||||
```
|
||||
[Unit]
|
||||
Description=MIS Control Tower downtime action reminders
|
||||
|
||||
[Service]
|
||||
Type=oneshot
|
||||
EnvironmentFile=/etc/mis-control-tower.env
|
||||
ExecStart=/usr/bin/curl -s -X POST "$APP_BASE_URL/api/downtime/actions/reminders?dueInDays=7" -H "Authorization: Bearer $DOWNTIME_ACTION_REMINDER_SECRET"
|
||||
```
|
||||
|
||||
`/etc/systemd/system/mis-control-tower-reminders.timer`
|
||||
|
||||
```
|
||||
[Unit]
|
||||
Description=Run MIS Control Tower reminders hourly
|
||||
|
||||
[Timer]
|
||||
OnCalendar=hourly
|
||||
Persistent=true
|
||||
|
||||
[Install]
|
||||
WantedBy=timers.target
|
||||
```
|
||||
|
||||
Enable with:
|
||||
|
||||
```
|
||||
sudo systemctl daemon-reload
|
||||
sudo systemctl enable --now mis-control-tower-reminders.timer
|
||||
```
|
||||
|
||||
## Learn More
|
||||
|
||||
To learn more about Next.js, take a look at the following resources:
|
||||
|
||||
499
app/(app)/alerts/AlertsClient.tsx
Normal file
499
app/(app)/alerts/AlertsClient.tsx
Normal file
@@ -0,0 +1,499 @@
|
||||
"use client";
|
||||
|
||||
import { useEffect, useMemo, useRef, useState } from "react";
|
||||
import { useI18n } from "@/lib/i18n/useI18n";
|
||||
|
||||
type MachineRow = {
|
||||
id: string;
|
||||
name: string;
|
||||
location?: string | null;
|
||||
};
|
||||
|
||||
type ShiftRow = {
|
||||
name: string;
|
||||
enabled: boolean;
|
||||
};
|
||||
|
||||
type AlertEvent = {
|
||||
id: string;
|
||||
ts: string;
|
||||
eventType: string;
|
||||
severity: string;
|
||||
title: string;
|
||||
description?: string | null;
|
||||
machineId: string;
|
||||
machineName?: string | null;
|
||||
location?: string | null;
|
||||
workOrderId?: string | null;
|
||||
sku?: string | null;
|
||||
durationSec?: number | null;
|
||||
status?: string | null;
|
||||
shift?: string | null;
|
||||
alertId?: string | null;
|
||||
isUpdate?: boolean;
|
||||
isAutoAck?: boolean;
|
||||
};
|
||||
|
||||
const RANGE_OPTIONS = [
|
||||
{ value: "24h", labelKey: "alerts.inbox.range.24h" },
|
||||
{ value: "7d", labelKey: "alerts.inbox.range.7d" },
|
||||
{ value: "30d", labelKey: "alerts.inbox.range.30d" },
|
||||
{ value: "custom", labelKey: "alerts.inbox.range.custom" },
|
||||
] as const;
|
||||
|
||||
function formatDuration(seconds: number | null | undefined, t: (key: string) => string) {
|
||||
if (seconds == null || !Number.isFinite(seconds)) return t("alerts.inbox.duration.na");
|
||||
if (seconds < 60) return `${Math.round(seconds)}${t("alerts.inbox.duration.sec")}`;
|
||||
if (seconds < 3600) return `${Math.round(seconds / 60)}${t("alerts.inbox.duration.min")}`;
|
||||
return `${(seconds / 3600).toFixed(1)}${t("alerts.inbox.duration.hr")}`;
|
||||
}
|
||||
|
||||
function normalizeLabel(value?: string | null) {
|
||||
if (!value) return "";
|
||||
return String(value).trim();
|
||||
}
|
||||
|
||||
export default function AlertsClient({
|
||||
initialMachines = [],
|
||||
initialShifts = [],
|
||||
initialEvents = [],
|
||||
}: {
|
||||
initialMachines?: MachineRow[];
|
||||
initialShifts?: ShiftRow[];
|
||||
initialEvents?: AlertEvent[];
|
||||
}) {
|
||||
const { t, locale } = useI18n();
|
||||
const [events, setEvents] = useState<AlertEvent[]>(() => initialEvents);
|
||||
const [machines, setMachines] = useState<MachineRow[]>(() => initialMachines);
|
||||
const [shifts, setShifts] = useState<ShiftRow[]>(() => initialShifts);
|
||||
const [loading, setLoading] = useState(() => initialMachines.length === 0 || initialShifts.length === 0);
|
||||
const [loadingEvents, setLoadingEvents] = useState(false);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
const [range, setRange] = useState<string>("24h");
|
||||
const [start, setStart] = useState<string>("");
|
||||
const [end, setEnd] = useState<string>("");
|
||||
const [machineId, setMachineId] = useState<string>("");
|
||||
const [location, setLocation] = useState<string>("");
|
||||
const [shift, setShift] = useState<string>("");
|
||||
const [eventType, setEventType] = useState<string>("");
|
||||
const [severity, setSeverity] = useState<string>("");
|
||||
const [status, setStatus] = useState<string>("");
|
||||
const [includeUpdates, setIncludeUpdates] = useState(false);
|
||||
const [search, setSearch] = useState("");
|
||||
const skipInitialEventsRef = useRef(true);
|
||||
|
||||
const locations = useMemo(() => {
|
||||
const seen = new Set<string>();
|
||||
for (const machine of machines) {
|
||||
if (!machine.location) continue;
|
||||
seen.add(machine.location);
|
||||
}
|
||||
return Array.from(seen).sort();
|
||||
}, [machines]);
|
||||
|
||||
useEffect(() => {
|
||||
if (initialMachines.length && initialShifts.length) {
|
||||
setLoading(false);
|
||||
return;
|
||||
}
|
||||
let alive = true;
|
||||
|
||||
async function loadFilters() {
|
||||
setLoading(true);
|
||||
try {
|
||||
const [machinesRes, settingsRes] = await Promise.all([
|
||||
fetch("/api/machines", { cache: "no-store" }),
|
||||
fetch("/api/settings", { cache: "no-store" }),
|
||||
]);
|
||||
const machinesJson = await machinesRes.json().catch(() => ({}));
|
||||
const settingsJson = await settingsRes.json().catch(() => ({}));
|
||||
if (!alive) return;
|
||||
setMachines(machinesJson.machines ?? []);
|
||||
const shiftRows = settingsJson?.settings?.shiftSchedule?.shifts ?? [];
|
||||
setShifts(
|
||||
Array.isArray(shiftRows)
|
||||
? shiftRows
|
||||
.map((row: unknown) => {
|
||||
const data = row && typeof row === "object" ? (row as Record<string, unknown>) : {};
|
||||
const name = typeof data.name === "string" ? data.name : "";
|
||||
const enabled = data.enabled !== false;
|
||||
return { name, enabled };
|
||||
})
|
||||
.filter((row) => row.name)
|
||||
: []
|
||||
);
|
||||
} catch {
|
||||
if (!alive) return;
|
||||
} finally {
|
||||
if (alive) setLoading(false);
|
||||
}
|
||||
}
|
||||
|
||||
loadFilters();
|
||||
return () => {
|
||||
alive = false;
|
||||
};
|
||||
}, [initialMachines, initialShifts]);
|
||||
|
||||
useEffect(() => {
|
||||
let alive = true;
|
||||
const controller = new AbortController();
|
||||
|
||||
async function loadEvents() {
|
||||
const isDefault =
|
||||
range === "24h" &&
|
||||
!start &&
|
||||
!end &&
|
||||
!machineId &&
|
||||
!location &&
|
||||
!shift &&
|
||||
!eventType &&
|
||||
!severity &&
|
||||
!status &&
|
||||
!includeUpdates;
|
||||
if (skipInitialEventsRef.current) {
|
||||
skipInitialEventsRef.current = false;
|
||||
if (initialEvents.length && isDefault) return;
|
||||
}
|
||||
|
||||
setLoadingEvents(true);
|
||||
setError(null);
|
||||
const params = new URLSearchParams();
|
||||
params.set("range", range);
|
||||
if (range === "custom") {
|
||||
if (start) params.set("start", start);
|
||||
if (end) params.set("end", end);
|
||||
}
|
||||
if (machineId) params.set("machineId", machineId);
|
||||
if (location) params.set("location", location);
|
||||
if (shift) params.set("shift", shift);
|
||||
if (eventType) params.set("eventType", eventType);
|
||||
if (severity) params.set("severity", severity);
|
||||
if (status) params.set("status", status);
|
||||
if (includeUpdates) params.set("includeUpdates", "1");
|
||||
params.set("limit", "250");
|
||||
|
||||
try {
|
||||
const res = await fetch(`/api/alerts/inbox?${params.toString()}`, {
|
||||
cache: "no-store",
|
||||
signal: controller.signal,
|
||||
});
|
||||
const json = await res.json().catch(() => ({}));
|
||||
if (!alive) return;
|
||||
if (!res.ok || !json?.ok) {
|
||||
setError(json?.error || t("alerts.inbox.error"));
|
||||
setEvents([]);
|
||||
} else {
|
||||
setEvents(json.events ?? []);
|
||||
}
|
||||
} catch {
|
||||
if (alive) {
|
||||
setError(t("alerts.inbox.error"));
|
||||
setEvents([]);
|
||||
}
|
||||
} finally {
|
||||
if (alive) setLoadingEvents(false);
|
||||
}
|
||||
}
|
||||
|
||||
loadEvents();
|
||||
return () => {
|
||||
alive = false;
|
||||
controller.abort();
|
||||
};
|
||||
}, [
|
||||
range,
|
||||
start,
|
||||
end,
|
||||
machineId,
|
||||
location,
|
||||
shift,
|
||||
eventType,
|
||||
severity,
|
||||
status,
|
||||
includeUpdates,
|
||||
t,
|
||||
initialEvents.length,
|
||||
]);
|
||||
|
||||
const eventTypes = useMemo(() => {
|
||||
const seen = new Set<string>();
|
||||
for (const ev of events) {
|
||||
if (ev.eventType) seen.add(ev.eventType);
|
||||
}
|
||||
return Array.from(seen).sort();
|
||||
}, [events]);
|
||||
|
||||
const severities = useMemo(() => {
|
||||
const seen = new Set<string>();
|
||||
for (const ev of events) {
|
||||
if (ev.severity) seen.add(ev.severity);
|
||||
}
|
||||
return Array.from(seen).sort();
|
||||
}, [events]);
|
||||
|
||||
const statuses = useMemo(() => {
|
||||
const seen = new Set<string>();
|
||||
for (const ev of events) {
|
||||
if (ev.status) seen.add(ev.status);
|
||||
}
|
||||
return Array.from(seen).sort();
|
||||
}, [events]);
|
||||
|
||||
const filteredEvents = useMemo(() => {
|
||||
if (!search.trim()) return events;
|
||||
const needle = search.trim().toLowerCase();
|
||||
return events.filter((ev) => {
|
||||
return (
|
||||
normalizeLabel(ev.title).toLowerCase().includes(needle) ||
|
||||
normalizeLabel(ev.description).toLowerCase().includes(needle) ||
|
||||
normalizeLabel(ev.machineName).toLowerCase().includes(needle) ||
|
||||
normalizeLabel(ev.location).toLowerCase().includes(needle) ||
|
||||
normalizeLabel(ev.eventType).toLowerCase().includes(needle)
|
||||
);
|
||||
});
|
||||
}, [events, search]);
|
||||
|
||||
function formatEventTypeLabel(value: string) {
|
||||
const key = `alerts.event.${value}`;
|
||||
const label = t(key);
|
||||
return label === key ? value : label;
|
||||
}
|
||||
|
||||
function formatStatusLabel(value?: string | null) {
|
||||
if (!value) return t("alerts.inbox.table.unknown");
|
||||
const key = `alerts.inbox.status.${value}`;
|
||||
const label = t(key);
|
||||
return label === key ? value : label;
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="mx-auto flex w-full max-w-7xl flex-col gap-6 px-4 py-6 sm:px-6 sm:py-8">
|
||||
<div>
|
||||
<h1 className="text-2xl font-semibold text-white">{t("alerts.title")}</h1>
|
||||
<p className="mt-2 text-sm text-zinc-400">{t("alerts.subtitle")}</p>
|
||||
</div>
|
||||
|
||||
<div className="rounded-2xl border border-white/10 bg-white/5 p-5">
|
||||
<div className="mb-4 flex flex-col gap-2 sm:flex-row sm:items-center sm:justify-between">
|
||||
<div className="text-sm font-semibold text-white">{t("alerts.inbox.filters.title")}</div>
|
||||
{loading && <div className="text-xs text-zinc-500">{t("alerts.inbox.loadingFilters")}</div>}
|
||||
</div>
|
||||
<div className="grid grid-cols-1 gap-3 md:grid-cols-2 xl:grid-cols-4">
|
||||
<label className="text-xs text-zinc-400">
|
||||
{t("alerts.inbox.filters.range")}
|
||||
<select
|
||||
className="mt-2 w-full rounded-lg border border-white/10 bg-black/30 px-3 py-2 text-sm text-white"
|
||||
value={range}
|
||||
onChange={(event) => setRange(event.target.value)}
|
||||
>
|
||||
{RANGE_OPTIONS.map((option) => (
|
||||
<option key={option.value} value={option.value}>
|
||||
{t(option.labelKey)}
|
||||
</option>
|
||||
))}
|
||||
</select>
|
||||
</label>
|
||||
{range === "custom" && (
|
||||
<>
|
||||
<label className="text-xs text-zinc-400">
|
||||
{t("alerts.inbox.filters.start")}
|
||||
<input
|
||||
type="date"
|
||||
className="mt-2 w-full rounded-lg border border-white/10 bg-black/30 px-3 py-2 text-sm text-white"
|
||||
value={start}
|
||||
onChange={(event) => setStart(event.target.value)}
|
||||
/>
|
||||
</label>
|
||||
<label className="text-xs text-zinc-400">
|
||||
{t("alerts.inbox.filters.end")}
|
||||
<input
|
||||
type="date"
|
||||
className="mt-2 w-full rounded-lg border border-white/10 bg-black/30 px-3 py-2 text-sm text-white"
|
||||
value={end}
|
||||
onChange={(event) => setEnd(event.target.value)}
|
||||
/>
|
||||
</label>
|
||||
</>
|
||||
)}
|
||||
<label className="text-xs text-zinc-400">
|
||||
{t("alerts.inbox.filters.machine")}
|
||||
<select
|
||||
className="mt-2 w-full rounded-lg border border-white/10 bg-black/30 px-3 py-2 text-sm text-white"
|
||||
value={machineId}
|
||||
onChange={(event) => setMachineId(event.target.value)}
|
||||
>
|
||||
<option value="">{t("alerts.inbox.filters.allMachines")}</option>
|
||||
{machines.map((machine) => (
|
||||
<option key={machine.id} value={machine.id}>
|
||||
{machine.name}
|
||||
</option>
|
||||
))}
|
||||
</select>
|
||||
</label>
|
||||
<label className="text-xs text-zinc-400">
|
||||
{t("alerts.inbox.filters.site")}
|
||||
<select
|
||||
className="mt-2 w-full rounded-lg border border-white/10 bg-black/30 px-3 py-2 text-sm text-white"
|
||||
value={location}
|
||||
onChange={(event) => setLocation(event.target.value)}
|
||||
>
|
||||
<option value="">{t("alerts.inbox.filters.allSites")}</option>
|
||||
{locations.map((loc) => (
|
||||
<option key={loc} value={loc}>
|
||||
{loc}
|
||||
</option>
|
||||
))}
|
||||
</select>
|
||||
</label>
|
||||
<label className="text-xs text-zinc-400">
|
||||
{t("alerts.inbox.filters.shift")}
|
||||
<select
|
||||
className="mt-2 w-full rounded-lg border border-white/10 bg-black/30 px-3 py-2 text-sm text-white"
|
||||
value={shift}
|
||||
onChange={(event) => setShift(event.target.value)}
|
||||
>
|
||||
<option value="">{t("alerts.inbox.filters.allShifts")}</option>
|
||||
{shifts
|
||||
.filter((row) => row.enabled)
|
||||
.map((row) => (
|
||||
<option key={row.name} value={row.name}>
|
||||
{row.name}
|
||||
</option>
|
||||
))}
|
||||
</select>
|
||||
</label>
|
||||
<label className="text-xs text-zinc-400">
|
||||
{t("alerts.inbox.filters.type")}
|
||||
<select
|
||||
className="mt-2 w-full rounded-lg border border-white/10 bg-black/30 px-3 py-2 text-sm text-white"
|
||||
value={eventType}
|
||||
onChange={(event) => setEventType(event.target.value)}
|
||||
>
|
||||
<option value="">{t("alerts.inbox.filters.allTypes")}</option>
|
||||
{eventTypes.map((value) => (
|
||||
<option key={value} value={value}>
|
||||
{formatEventTypeLabel(value)}
|
||||
</option>
|
||||
))}
|
||||
</select>
|
||||
</label>
|
||||
<label className="text-xs text-zinc-400">
|
||||
{t("alerts.inbox.filters.severity")}
|
||||
<select
|
||||
className="mt-2 w-full rounded-lg border border-white/10 bg-black/30 px-3 py-2 text-sm text-white"
|
||||
value={severity}
|
||||
onChange={(event) => setSeverity(event.target.value)}
|
||||
>
|
||||
<option value="">{t("alerts.inbox.filters.allSeverities")}</option>
|
||||
{severities.map((value) => (
|
||||
<option key={value} value={value}>
|
||||
{value}
|
||||
</option>
|
||||
))}
|
||||
</select>
|
||||
</label>
|
||||
<label className="text-xs text-zinc-400">
|
||||
{t("alerts.inbox.filters.status")}
|
||||
<select
|
||||
className="mt-2 w-full rounded-lg border border-white/10 bg-black/30 px-3 py-2 text-sm text-white"
|
||||
value={status}
|
||||
onChange={(event) => setStatus(event.target.value)}
|
||||
>
|
||||
<option value="">{t("alerts.inbox.filters.allStatuses")}</option>
|
||||
{statuses.map((value) => (
|
||||
<option key={value} value={value}>
|
||||
{formatStatusLabel(value)}
|
||||
</option>
|
||||
))}
|
||||
</select>
|
||||
</label>
|
||||
<label className="text-xs text-zinc-400">
|
||||
{t("alerts.inbox.filters.search")}
|
||||
<input
|
||||
className="mt-2 w-full rounded-lg border border-white/10 bg-black/30 px-3 py-2 text-sm text-white"
|
||||
value={search}
|
||||
onChange={(event) => setSearch(event.target.value)}
|
||||
placeholder={t("alerts.inbox.filters.searchPlaceholder")}
|
||||
/>
|
||||
</label>
|
||||
<label className="flex items-center gap-2 text-xs text-zinc-400">
|
||||
<input
|
||||
type="checkbox"
|
||||
checked={includeUpdates}
|
||||
onChange={(event) => setIncludeUpdates(event.target.checked)}
|
||||
className="h-4 w-4 rounded border border-white/20 bg-black/20"
|
||||
/>
|
||||
{t("alerts.inbox.filters.includeUpdates")}
|
||||
</label>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="rounded-2xl border border-white/10 bg-white/5 p-5">
|
||||
<div className="mb-4 flex flex-col gap-2 sm:flex-row sm:items-center sm:justify-between">
|
||||
<div className="text-sm font-semibold text-white">{t("alerts.inbox.title")}</div>
|
||||
{loadingEvents && <div className="text-xs text-zinc-500">{t("alerts.inbox.loading")}</div>}
|
||||
</div>
|
||||
|
||||
{error && (
|
||||
<div className="mb-4 rounded-xl border border-red-500/20 bg-red-500/10 p-3 text-sm text-red-200">
|
||||
{error}
|
||||
</div>
|
||||
)}
|
||||
|
||||
{!loadingEvents && !filteredEvents.length && (
|
||||
<div className="text-sm text-zinc-400">{t("alerts.inbox.empty")}</div>
|
||||
)}
|
||||
|
||||
{!!filteredEvents.length && (
|
||||
<div className="overflow-x-auto">
|
||||
<table className="w-full border-collapse text-sm text-zinc-300">
|
||||
<thead>
|
||||
<tr className="text-xs uppercase text-zinc-500">
|
||||
<th className="border-b border-white/10 px-3 py-2 text-left">{t("alerts.inbox.table.time")}</th>
|
||||
<th className="border-b border-white/10 px-3 py-2 text-left">{t("alerts.inbox.table.machine")}</th>
|
||||
<th className="border-b border-white/10 px-3 py-2 text-left">{t("alerts.inbox.table.site")}</th>
|
||||
<th className="border-b border-white/10 px-3 py-2 text-left">{t("alerts.inbox.table.shift")}</th>
|
||||
<th className="border-b border-white/10 px-3 py-2 text-left">{t("alerts.inbox.table.type")}</th>
|
||||
<th className="border-b border-white/10 px-3 py-2 text-left">{t("alerts.inbox.table.severity")}</th>
|
||||
<th className="border-b border-white/10 px-3 py-2 text-left">{t("alerts.inbox.table.status")}</th>
|
||||
<th className="border-b border-white/10 px-3 py-2 text-left">{t("alerts.inbox.table.duration")}</th>
|
||||
<th className="border-b border-white/10 px-3 py-2 text-left">{t("alerts.inbox.table.title")}</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{filteredEvents.map((ev) => (
|
||||
<tr key={ev.id} className="border-b border-white/5">
|
||||
<td className="px-3 py-3 text-xs text-zinc-400">
|
||||
{new Date(ev.ts).toLocaleString(locale)}
|
||||
</td>
|
||||
<td className="px-3 py-3">{ev.machineName || t("alerts.inbox.table.unknown")}</td>
|
||||
<td className="px-3 py-3">{ev.location || t("alerts.inbox.table.unknown")}</td>
|
||||
<td className="px-3 py-3">{ev.shift || t("alerts.inbox.table.unknown")}</td>
|
||||
<td className="px-3 py-3">{formatEventTypeLabel(ev.eventType)}</td>
|
||||
<td className="px-3 py-3">{ev.severity || t("alerts.inbox.table.unknown")}</td>
|
||||
<td className="px-3 py-3">{formatStatusLabel(ev.status)}</td>
|
||||
<td className="px-3 py-3">{formatDuration(ev.durationSec, t)}</td>
|
||||
<td className="px-3 py-3">
|
||||
<div className="text-sm text-white">{ev.title}</div>
|
||||
{ev.description && (
|
||||
<div className="mt-1 text-xs text-zinc-400">{ev.description}</div>
|
||||
)}
|
||||
{(ev.workOrderId || ev.sku) && (
|
||||
<div className="mt-1 text-[11px] text-zinc-500">
|
||||
{ev.workOrderId ? `${t("alerts.inbox.meta.workOrder")}: ${ev.workOrderId}` : null}
|
||||
{ev.workOrderId && ev.sku ? " • " : null}
|
||||
{ev.sku ? `${t("alerts.inbox.meta.sku")}: ${ev.sku}` : null}
|
||||
</div>
|
||||
)}
|
||||
</td>
|
||||
</tr>
|
||||
))}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
46
app/(app)/alerts/page.tsx
Normal file
46
app/(app)/alerts/page.tsx
Normal file
@@ -0,0 +1,46 @@
|
||||
import { redirect } from "next/navigation";
|
||||
import { prisma } from "@/lib/prisma";
|
||||
import { requireSession } from "@/lib/auth/requireSession";
|
||||
import { getAlertsInboxData } from "@/lib/alerts/getAlertsInboxData";
|
||||
import AlertsClient from "./AlertsClient";
|
||||
|
||||
export default async function AlertsPage() {
|
||||
const session = await requireSession();
|
||||
if (!session) redirect("/login?next=/alerts");
|
||||
|
||||
const [machines, shiftRows, inbox] = await Promise.all([
|
||||
prisma.machine.findMany({
|
||||
where: { orgId: session.orgId },
|
||||
orderBy: { createdAt: "desc" },
|
||||
select: { id: true, name: true, location: true },
|
||||
}),
|
||||
prisma.orgShift.findMany({
|
||||
where: { orgId: session.orgId },
|
||||
orderBy: { sortOrder: "asc" },
|
||||
select: { name: true, enabled: true },
|
||||
}),
|
||||
getAlertsInboxData({
|
||||
orgId: session.orgId,
|
||||
range: "24h",
|
||||
limit: 250,
|
||||
}),
|
||||
]);
|
||||
|
||||
const initialEvents = inbox.events.map((event) => ({
|
||||
...event,
|
||||
ts: event.ts ? event.ts.toISOString() : "",
|
||||
}));
|
||||
|
||||
const initialShifts = shiftRows.map((shift) => ({
|
||||
name: shift.name,
|
||||
enabled: shift.enabled !== false,
|
||||
}));
|
||||
|
||||
return (
|
||||
<AlertsClient
|
||||
initialMachines={machines}
|
||||
initialShifts={initialShifts}
|
||||
initialEvents={initialEvents}
|
||||
/>
|
||||
);
|
||||
}
|
||||
29
app/(app)/downtime/layout.tsx
Normal file
29
app/(app)/downtime/layout.tsx
Normal file
@@ -0,0 +1,29 @@
|
||||
import { redirect } from "next/navigation";
|
||||
import { prisma } from "@/lib/prisma";
|
||||
import { requireSession } from "@/lib/auth/requireSession";
|
||||
|
||||
function isPlainObject(value: unknown): value is Record<string, unknown> {
|
||||
return !!value && typeof value === "object" && !Array.isArray(value);
|
||||
}
|
||||
|
||||
function getScreenlessMode(defaultsJson: unknown) {
|
||||
const defaults = isPlainObject(defaultsJson) ? defaultsJson : {};
|
||||
const modules = isPlainObject(defaults.modules) ? defaults.modules : {};
|
||||
return modules.screenlessMode === true;
|
||||
}
|
||||
|
||||
export default async function DowntimeLayout({ children }: { children: React.ReactNode }) {
|
||||
const session = await requireSession();
|
||||
if (!session) redirect("/login?next=/downtime");
|
||||
|
||||
const settings = await prisma.orgSettings.findUnique({
|
||||
where: { orgId: session.orgId },
|
||||
select: { defaultsJson: true },
|
||||
});
|
||||
|
||||
if (getScreenlessMode(settings?.defaultsJson)) {
|
||||
redirect("/overview");
|
||||
}
|
||||
|
||||
return <>{children}</>;
|
||||
}
|
||||
5
app/(app)/downtime/page.tsx
Normal file
5
app/(app)/downtime/page.tsx
Normal file
@@ -0,0 +1,5 @@
|
||||
import DowntimePageClient from "@/components/downtime/DowntimePageClient";
|
||||
|
||||
export default function DowntimePage() {
|
||||
return <DowntimePageClient />;
|
||||
}
|
||||
388
app/(app)/financial/FinancialClient.tsx
Normal file
388
app/(app)/financial/FinancialClient.tsx
Normal file
@@ -0,0 +1,388 @@
|
||||
"use client";
|
||||
|
||||
import { useEffect, useMemo, useRef, useState } from "react";
|
||||
import Link from "next/link";
|
||||
import {
|
||||
Area,
|
||||
AreaChart,
|
||||
CartesianGrid,
|
||||
ResponsiveContainer,
|
||||
Tooltip,
|
||||
XAxis,
|
||||
YAxis,
|
||||
} from "recharts";
|
||||
import { useI18n } from "@/lib/i18n/useI18n";
|
||||
|
||||
type MachineRow = {
|
||||
id: string;
|
||||
name: string;
|
||||
location?: string | null;
|
||||
};
|
||||
|
||||
type ImpactSummary = {
|
||||
currency: string;
|
||||
totals: {
|
||||
total: number;
|
||||
slowCycle: number;
|
||||
microstop: number;
|
||||
macrostop: number;
|
||||
scrap: number;
|
||||
};
|
||||
byDay: Array<{
|
||||
day: string;
|
||||
total: number;
|
||||
slowCycle: number;
|
||||
microstop: number;
|
||||
macrostop: number;
|
||||
scrap: number;
|
||||
}>;
|
||||
};
|
||||
|
||||
type ImpactResponse = {
|
||||
ok: boolean;
|
||||
currencySummaries: ImpactSummary[];
|
||||
};
|
||||
|
||||
function formatMoney(value: number, currency: string, locale: string) {
|
||||
if (!Number.isFinite(value)) return "--";
|
||||
try {
|
||||
return new Intl.NumberFormat(locale, {
|
||||
style: "currency",
|
||||
currency,
|
||||
maximumFractionDigits: 0,
|
||||
}).format(value);
|
||||
} catch {
|
||||
return `${value.toFixed(0)} ${currency}`;
|
||||
}
|
||||
}
|
||||
|
||||
export default function FinancialClient({
|
||||
initialRole = null,
|
||||
initialMachines = [],
|
||||
initialImpact = null,
|
||||
}: {
|
||||
initialRole?: string | null;
|
||||
initialMachines?: MachineRow[];
|
||||
initialImpact?: ImpactResponse | null;
|
||||
}) {
|
||||
const { locale, t } = useI18n();
|
||||
const [role, setRole] = useState<string | null>(initialRole);
|
||||
const [machines, setMachines] = useState<MachineRow[]>(() => initialMachines);
|
||||
const [impact, setImpact] = useState<ImpactResponse | null>(initialImpact);
|
||||
const [range, setRange] = useState("7d");
|
||||
const [machineFilter, setMachineFilter] = useState("");
|
||||
const [locationFilter, setLocationFilter] = useState("");
|
||||
const [skuFilter, setSkuFilter] = useState("");
|
||||
const [currencyFilter, setCurrencyFilter] = useState("");
|
||||
const [loading, setLoading] = useState(() => initialMachines.length === 0);
|
||||
const skipInitialImpactRef = useRef(true);
|
||||
|
||||
const locations = useMemo(() => {
|
||||
const seen = new Set<string>();
|
||||
for (const m of machines) {
|
||||
if (!m.location) continue;
|
||||
seen.add(m.location);
|
||||
}
|
||||
return Array.from(seen).sort();
|
||||
}, [machines]);
|
||||
|
||||
useEffect(() => {
|
||||
if (initialRole != null) return;
|
||||
let alive = true;
|
||||
|
||||
async function loadMe() {
|
||||
try {
|
||||
const res = await fetch("/api/me", { cache: "no-store" });
|
||||
const data = await res.json().catch(() => ({}));
|
||||
if (!alive) return;
|
||||
setRole(data?.membership?.role ?? null);
|
||||
} catch {
|
||||
if (alive) setRole(null);
|
||||
}
|
||||
}
|
||||
|
||||
loadMe();
|
||||
return () => {
|
||||
alive = false;
|
||||
};
|
||||
}, [initialRole]);
|
||||
|
||||
useEffect(() => {
|
||||
if (initialMachines.length) {
|
||||
setLoading(false);
|
||||
return;
|
||||
}
|
||||
let alive = true;
|
||||
|
||||
async function loadMachines() {
|
||||
try {
|
||||
const res = await fetch("/api/machines", { cache: "no-store" });
|
||||
const json = await res.json().catch(() => ({}));
|
||||
if (!alive) return;
|
||||
setMachines(json.machines ?? []);
|
||||
} catch {
|
||||
if (!alive) return;
|
||||
} finally {
|
||||
if (alive) setLoading(false);
|
||||
}
|
||||
}
|
||||
|
||||
loadMachines();
|
||||
return () => {
|
||||
alive = false;
|
||||
};
|
||||
}, [initialMachines]);
|
||||
|
||||
useEffect(() => {
|
||||
let alive = true;
|
||||
const controller = new AbortController();
|
||||
|
||||
async function loadImpact() {
|
||||
if (role == null) return;
|
||||
if (role !== "OWNER") return;
|
||||
|
||||
const isDefault =
|
||||
range === "7d" &&
|
||||
!machineFilter &&
|
||||
!locationFilter &&
|
||||
!skuFilter &&
|
||||
!currencyFilter;
|
||||
if (skipInitialImpactRef.current) {
|
||||
skipInitialImpactRef.current = false;
|
||||
if (initialImpact && isDefault) return;
|
||||
}
|
||||
|
||||
const params = new URLSearchParams();
|
||||
params.set("range", range);
|
||||
if (machineFilter) params.set("machineId", machineFilter);
|
||||
if (locationFilter) params.set("location", locationFilter);
|
||||
if (skuFilter) params.set("sku", skuFilter);
|
||||
if (currencyFilter) params.set("currency", currencyFilter);
|
||||
|
||||
try {
|
||||
const res = await fetch(`/api/financial/impact?${params.toString()}`, {
|
||||
cache: "no-store",
|
||||
signal: controller.signal,
|
||||
});
|
||||
const json = await res.json().catch(() => ({}));
|
||||
if (!alive) return;
|
||||
setImpact(json);
|
||||
} catch {
|
||||
if (alive) setImpact(null);
|
||||
}
|
||||
}
|
||||
|
||||
loadImpact();
|
||||
return () => {
|
||||
alive = false;
|
||||
controller.abort();
|
||||
};
|
||||
}, [currencyFilter, initialImpact, locationFilter, machineFilter, range, role, skuFilter]);
|
||||
|
||||
const selectedSummary = impact?.currencySummaries?.[0] ?? null;
|
||||
const chartData = selectedSummary?.byDay ?? [];
|
||||
const exportQuery = useMemo(() => {
|
||||
const params = new URLSearchParams();
|
||||
params.set("range", range);
|
||||
if (machineFilter) params.set("machineId", machineFilter);
|
||||
if (locationFilter) params.set("location", locationFilter);
|
||||
if (skuFilter) params.set("sku", skuFilter);
|
||||
if (currencyFilter) params.set("currency", currencyFilter);
|
||||
return params.toString();
|
||||
}, [range, machineFilter, locationFilter, skuFilter, currencyFilter]);
|
||||
|
||||
const htmlHref = `/api/financial/export/pdf?${exportQuery}`;
|
||||
const csvHref = `/api/financial/export/excel?${exportQuery}`;
|
||||
|
||||
if (role && role !== "OWNER") {
|
||||
return (
|
||||
<div className="p-4 sm:p-6">
|
||||
<div className="rounded-2xl border border-white/10 bg-black/40 p-6 text-zinc-300">
|
||||
{t("financial.ownerOnly")}
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="p-4 sm:p-6 space-y-6">
|
||||
<div className="flex flex-col gap-4 sm:flex-row sm:items-start sm:justify-between">
|
||||
<div>
|
||||
<h1 className="text-2xl font-semibold text-white">{t("financial.title")}</h1>
|
||||
<p className="text-sm text-zinc-400">{t("financial.subtitle")}</p>
|
||||
</div>
|
||||
<div className="flex w-full flex-col gap-3 sm:w-auto sm:flex-row">
|
||||
<a
|
||||
className="w-full rounded-xl border border-white/10 bg-white/5 px-4 py-2 text-center text-sm text-zinc-200 hover:bg-white/10 sm:w-auto"
|
||||
href={htmlHref}
|
||||
target="_blank"
|
||||
rel="noreferrer"
|
||||
>
|
||||
{t("financial.export.html")}
|
||||
</a>
|
||||
<a
|
||||
className="w-full rounded-xl border border-white/10 bg-white/5 px-4 py-2 text-center text-sm text-zinc-200 hover:bg-white/10 sm:w-auto"
|
||||
href={csvHref}
|
||||
target="_blank"
|
||||
rel="noreferrer"
|
||||
>
|
||||
{t("financial.export.csv")}
|
||||
</a>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="rounded-2xl border border-white/10 bg-black/40 p-4 text-sm text-zinc-300">
|
||||
{t("financial.costsMoved")}{" "}
|
||||
<Link className="text-emerald-200 hover:text-emerald-100" href="/settings">
|
||||
{t("financial.costsMovedLink")}
|
||||
</Link>
|
||||
.
|
||||
</div>
|
||||
|
||||
<div className="grid gap-4 lg:grid-cols-4">
|
||||
{(impact?.currencySummaries ?? []).slice(0, 4).map((summary) => (
|
||||
<div key={summary.currency} className="rounded-2xl border border-white/10 bg-black/40 p-4">
|
||||
<div className="text-xs uppercase tracking-wide text-zinc-500">{t("financial.totalLoss")}</div>
|
||||
<div className="mt-2 text-2xl font-semibold text-white">
|
||||
{formatMoney(summary.totals.total, summary.currency, locale)}
|
||||
</div>
|
||||
<div className="mt-3 text-xs text-zinc-400">
|
||||
{t("financial.currencyLabel", { currency: summary.currency })}
|
||||
</div>
|
||||
</div>
|
||||
))}
|
||||
{!impact?.currencySummaries?.length && (
|
||||
<div className="rounded-2xl border border-white/10 bg-black/40 p-4 text-sm text-zinc-400">
|
||||
{t("financial.noImpact")}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
|
||||
<div className="grid gap-4 lg:grid-cols-[2fr_1fr]">
|
||||
<div className="rounded-2xl border border-white/10 bg-black/40 p-4">
|
||||
<div className="flex items-center justify-between">
|
||||
<div>
|
||||
<h2 className="text-lg font-semibold text-white">{t("financial.chart.title")}</h2>
|
||||
<p className="text-xs text-zinc-500">{t("financial.chart.subtitle")}</p>
|
||||
</div>
|
||||
<div className="flex gap-2">
|
||||
{["24h", "7d", "30d"].map((value) => (
|
||||
<button
|
||||
key={value}
|
||||
type="button"
|
||||
onClick={() => setRange(value)}
|
||||
className={
|
||||
value === range
|
||||
? "rounded-full bg-emerald-500/20 px-3 py-1 text-xs text-emerald-200"
|
||||
: "rounded-full border border-white/10 px-3 py-1 text-xs text-zinc-300"
|
||||
}
|
||||
>
|
||||
{value === "24h"
|
||||
? t("financial.range.day")
|
||||
: value === "7d"
|
||||
? t("financial.range.week")
|
||||
: t("financial.range.month")}
|
||||
</button>
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="mt-4 h-64">
|
||||
<ResponsiveContainer width="100%" height="100%">
|
||||
<AreaChart data={chartData}>
|
||||
<defs>
|
||||
<linearGradient id="slowFill" x1="0" y1="0" x2="0" y2="1">
|
||||
<stop offset="5%" stopColor="#facc15" stopOpacity={0.5} />
|
||||
<stop offset="95%" stopColor="#facc15" stopOpacity={0.05} />
|
||||
</linearGradient>
|
||||
<linearGradient id="microFill" x1="0" y1="0" x2="0" y2="1">
|
||||
<stop offset="5%" stopColor="#fb7185" stopOpacity={0.5} />
|
||||
<stop offset="95%" stopColor="#fb7185" stopOpacity={0.05} />
|
||||
</linearGradient>
|
||||
<linearGradient id="macroFill" x1="0" y1="0" x2="0" y2="1">
|
||||
<stop offset="5%" stopColor="#f97316" stopOpacity={0.5} />
|
||||
<stop offset="95%" stopColor="#f97316" stopOpacity={0.05} />
|
||||
</linearGradient>
|
||||
<linearGradient id="scrapFill" x1="0" y1="0" x2="0" y2="1">
|
||||
<stop offset="5%" stopColor="#38bdf8" stopOpacity={0.5} />
|
||||
<stop offset="95%" stopColor="#38bdf8" stopOpacity={0.05} />
|
||||
</linearGradient>
|
||||
</defs>
|
||||
<CartesianGrid strokeDasharray="3 3" stroke="var(--app-chart-grid)" />
|
||||
<XAxis dataKey="day" tick={{ fill: "var(--app-chart-tick)", fontSize: 10 }} />
|
||||
<YAxis tick={{ fill: "var(--app-chart-tick)", fontSize: 10 }} />
|
||||
<Tooltip
|
||||
contentStyle={{
|
||||
background: "var(--app-chart-tooltip-bg)",
|
||||
border: "1px solid var(--app-chart-tooltip-border)",
|
||||
}}
|
||||
labelStyle={{ color: "var(--app-chart-label)" }}
|
||||
/>
|
||||
<Area type="monotone" dataKey="slowCycle" stackId="1" stroke="#facc15" fill="url(#slowFill)" />
|
||||
<Area type="monotone" dataKey="microstop" stackId="1" stroke="#fb7185" fill="url(#microFill)" />
|
||||
<Area type="monotone" dataKey="macrostop" stackId="1" stroke="#f97316" fill="url(#macroFill)" />
|
||||
<Area type="monotone" dataKey="scrap" stackId="1" stroke="#38bdf8" fill="url(#scrapFill)" />
|
||||
</AreaChart>
|
||||
</ResponsiveContainer>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="rounded-2xl border border-white/10 bg-black/40 p-4 space-y-4">
|
||||
<h2 className="text-lg font-semibold text-white">{t("financial.filters.title")}</h2>
|
||||
<div className="space-y-3 text-sm text-zinc-300">
|
||||
<div>
|
||||
<label className="text-xs uppercase text-zinc-500">{t("financial.filters.machine")}</label>
|
||||
<select
|
||||
className="mt-2 w-full rounded-lg border border-white/10 bg-black/30 px-3 py-2"
|
||||
value={machineFilter}
|
||||
onChange={(event) => setMachineFilter(event.target.value)}
|
||||
>
|
||||
<option value="">{t("financial.filters.allMachines")}</option>
|
||||
{machines.map((machine) => (
|
||||
<option key={machine.id} value={machine.id}>
|
||||
{machine.name}
|
||||
</option>
|
||||
))}
|
||||
</select>
|
||||
</div>
|
||||
<div>
|
||||
<label className="text-xs uppercase text-zinc-500">{t("financial.filters.location")}</label>
|
||||
<select
|
||||
className="mt-2 w-full rounded-lg border border-white/10 bg-black/30 px-3 py-2"
|
||||
value={locationFilter}
|
||||
onChange={(event) => setLocationFilter(event.target.value)}
|
||||
>
|
||||
<option value="">{t("financial.filters.allLocations")}</option>
|
||||
{locations.map((loc) => (
|
||||
<option key={loc} value={loc}>
|
||||
{loc}
|
||||
</option>
|
||||
))}
|
||||
</select>
|
||||
</div>
|
||||
<div>
|
||||
<label className="text-xs uppercase text-zinc-500">{t("financial.filters.sku")}</label>
|
||||
<input
|
||||
className="mt-2 w-full rounded-lg border border-white/10 bg-black/30 px-3 py-2"
|
||||
value={skuFilter}
|
||||
onChange={(event) => setSkuFilter(event.target.value)}
|
||||
placeholder={t("financial.filters.skuPlaceholder")}
|
||||
/>
|
||||
</div>
|
||||
<div>
|
||||
<label className="text-xs uppercase text-zinc-500">{t("financial.filters.currency")}</label>
|
||||
<input
|
||||
className="mt-2 w-full rounded-lg border border-white/10 bg-black/30 px-3 py-2"
|
||||
value={currencyFilter}
|
||||
onChange={(event) => setCurrencyFilter(event.target.value.toUpperCase())}
|
||||
placeholder={t("financial.filters.currencyPlaceholder")}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{loading && <div className="text-xs text-zinc-500">{t("financial.loadingMachines")}</div>}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
45
app/(app)/financial/page.tsx
Normal file
45
app/(app)/financial/page.tsx
Normal file
@@ -0,0 +1,45 @@
|
||||
import { redirect } from "next/navigation";
|
||||
import { prisma } from "@/lib/prisma";
|
||||
import { requireSession } from "@/lib/auth/requireSession";
|
||||
import { computeFinancialImpact } from "@/lib/financial/impact";
|
||||
import FinancialClient from "./FinancialClient";
|
||||
|
||||
const RANGE_MS = 7 * 24 * 60 * 60 * 1000;
|
||||
|
||||
export default async function FinancialPage() {
|
||||
const session = await requireSession();
|
||||
if (!session) redirect("/login?next=/financial");
|
||||
|
||||
const membership = await prisma.orgUser.findUnique({
|
||||
where: { orgId_userId: { orgId: session.orgId, userId: session.userId } },
|
||||
select: { role: true },
|
||||
});
|
||||
|
||||
const role = membership?.role ?? null;
|
||||
if (role !== "OWNER") {
|
||||
return <FinancialClient initialRole={role ?? "GUEST"} />;
|
||||
}
|
||||
|
||||
const machines = await prisma.machine.findMany({
|
||||
where: { orgId: session.orgId },
|
||||
orderBy: { createdAt: "desc" },
|
||||
select: { id: true, name: true, location: true },
|
||||
});
|
||||
|
||||
const end = new Date();
|
||||
const start = new Date(end.getTime() - RANGE_MS);
|
||||
const impact = await computeFinancialImpact({
|
||||
orgId: session.orgId,
|
||||
start,
|
||||
end,
|
||||
includeEvents: false,
|
||||
});
|
||||
|
||||
return (
|
||||
<FinancialClient
|
||||
initialRole={role}
|
||||
initialMachines={machines}
|
||||
initialImpact={{ ok: true, currencySummaries: impact.currencySummaries }}
|
||||
/>
|
||||
);
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
import { Sidebar } from "@/components/layout/Sidebar";
|
||||
import { AppShell } from "@/components/layout/AppShell";
|
||||
import { cookies } from "next/headers";
|
||||
import { redirect } from "next/navigation";
|
||||
import { prisma } from "@/lib/prisma";
|
||||
@@ -6,7 +6,10 @@ import { prisma } from "@/lib/prisma";
|
||||
const COOKIE_NAME = "mis_session";
|
||||
|
||||
export default async function AppLayout({ children }: { children: React.ReactNode }) {
|
||||
const sessionId = (await cookies()).get(COOKIE_NAME)?.value;
|
||||
const cookieJar = await cookies();
|
||||
const sessionId = cookieJar.get(COOKIE_NAME)?.value;
|
||||
const themeCookie = cookieJar.get("mis_theme")?.value;
|
||||
const initialTheme = themeCookie === "light" ? "light" : "dark";
|
||||
|
||||
if (!sessionId) redirect("/login?next=/machines");
|
||||
|
||||
@@ -20,14 +23,9 @@ export default async function AppLayout({ children }: { children: React.ReactNod
|
||||
include: { user: true, org: true },
|
||||
});
|
||||
|
||||
if (!session) redirect("/login?next=/machines");
|
||||
|
||||
return (
|
||||
<div className="min-h-screen bg-black text-white">
|
||||
<div className="flex">
|
||||
<Sidebar />
|
||||
<main className="flex-1 min-h-screen">{children}</main>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
if (!session || !session.user?.isActive || !session.user?.emailVerifiedAt) {
|
||||
redirect("/login?next=/machines");
|
||||
}
|
||||
|
||||
return <AppShell initialTheme={initialTheme}>{children}</AppShell>;
|
||||
}
|
||||
|
||||
338
app/(app)/machines/MachinesClient.tsx
Normal file
338
app/(app)/machines/MachinesClient.tsx
Normal file
@@ -0,0 +1,338 @@
|
||||
"use client";
|
||||
|
||||
import Link from "next/link";
|
||||
import { useRouter } from "next/navigation";
|
||||
import { useEffect, useState, type KeyboardEvent } from "react";
|
||||
import { useI18n } from "@/lib/i18n/useI18n";
|
||||
|
||||
type MachineRow = {
|
||||
id: string;
|
||||
name: string;
|
||||
code?: string | null;
|
||||
location?: string | null;
|
||||
latestHeartbeat: null | {
|
||||
ts: string;
|
||||
tsServer?: string | null;
|
||||
status: string;
|
||||
message?: string | null;
|
||||
ip?: string | null;
|
||||
fwVersion?: string | null;
|
||||
};
|
||||
};
|
||||
|
||||
function secondsAgo(ts: string | undefined, locale: string, fallback: string) {
|
||||
if (!ts) return fallback;
|
||||
const diff = Math.floor((Date.now() - new Date(ts).getTime()) / 1000);
|
||||
const rtf = new Intl.RelativeTimeFormat(locale, { numeric: "auto" });
|
||||
if (diff < 60) return rtf.format(-diff, "second");
|
||||
return rtf.format(-Math.floor(diff / 60), "minute");
|
||||
}
|
||||
|
||||
function isOffline(ts?: string) {
|
||||
if (!ts) return true;
|
||||
return Date.now() - new Date(ts).getTime() > 30000; // 30s threshold
|
||||
}
|
||||
|
||||
function normalizeStatus(status?: string) {
|
||||
const s = (status ?? "").toUpperCase();
|
||||
if (s === "ONLINE") return "RUN";
|
||||
return s;
|
||||
}
|
||||
|
||||
function badgeClass(status?: string, offline?: boolean) {
|
||||
if (offline) return "bg-white/10 text-zinc-300";
|
||||
const s = (status ?? "").toUpperCase();
|
||||
if (s === "RUN") return "bg-emerald-500/15 text-emerald-300";
|
||||
if (s === "IDLE") return "bg-yellow-500/15 text-yellow-300";
|
||||
if (s === "STOP" || s === "DOWN") return "bg-red-500/15 text-red-300";
|
||||
return "bg-white/10 text-white";
|
||||
}
|
||||
|
||||
export default function MachinesClient({ initialMachines = [] }: { initialMachines?: MachineRow[] }) {
|
||||
const { t, locale } = useI18n();
|
||||
const router = useRouter();
|
||||
const [machines, setMachines] = useState<MachineRow[]>(() => initialMachines);
|
||||
const [loading, setLoading] = useState(false);
|
||||
const [showCreate, setShowCreate] = useState(false);
|
||||
const [createName, setCreateName] = useState("");
|
||||
const [createCode, setCreateCode] = useState("");
|
||||
const [createLocation, setCreateLocation] = useState("");
|
||||
const [creating, setCreating] = useState(false);
|
||||
const [createError, setCreateError] = useState<string | null>(null);
|
||||
const [createdMachine, setCreatedMachine] = useState<{
|
||||
id: string;
|
||||
name: string;
|
||||
pairingCode: string;
|
||||
pairingExpiresAt: string;
|
||||
} | null>(null);
|
||||
const [copyStatus, setCopyStatus] = useState<string | null>(null);
|
||||
|
||||
useEffect(() => {
|
||||
let alive = true;
|
||||
|
||||
async function load() {
|
||||
try {
|
||||
const res = await fetch("/api/machines", { cache: "no-store" });
|
||||
const json = await res.json();
|
||||
if (alive) {
|
||||
setMachines(json.machines ?? []);
|
||||
setLoading(false);
|
||||
}
|
||||
} catch {
|
||||
if (alive) setLoading(false);
|
||||
}
|
||||
}
|
||||
|
||||
load();
|
||||
const t = setInterval(load, 15000);
|
||||
|
||||
return () => {
|
||||
alive = false;
|
||||
clearInterval(t);
|
||||
};
|
||||
}, []);
|
||||
|
||||
async function createMachine() {
|
||||
if (!createName.trim()) {
|
||||
setCreateError(t("machines.create.error.nameRequired"));
|
||||
return;
|
||||
}
|
||||
|
||||
setCreating(true);
|
||||
setCreateError(null);
|
||||
|
||||
try {
|
||||
const res = await fetch("/api/machines", {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({
|
||||
name: createName,
|
||||
code: createCode,
|
||||
location: createLocation,
|
||||
}),
|
||||
});
|
||||
const data = await res.json().catch(() => ({}));
|
||||
if (!res.ok || !data.ok) {
|
||||
throw new Error(data.error || t("machines.create.error.failed"));
|
||||
}
|
||||
|
||||
const nextMachine = {
|
||||
...data.machine,
|
||||
latestHeartbeat: null,
|
||||
};
|
||||
setMachines((prev) => [nextMachine, ...prev]);
|
||||
setCreatedMachine({
|
||||
id: data.machine.id,
|
||||
name: data.machine.name,
|
||||
pairingCode: data.machine.pairingCode,
|
||||
pairingExpiresAt: data.machine.pairingCodeExpiresAt,
|
||||
});
|
||||
setCreateName("");
|
||||
setCreateCode("");
|
||||
setCreateLocation("");
|
||||
setShowCreate(false);
|
||||
} catch (err: unknown) {
|
||||
const message = err instanceof Error ? err.message : null;
|
||||
setCreateError(message || t("machines.create.error.failed"));
|
||||
} finally {
|
||||
setCreating(false);
|
||||
}
|
||||
}
|
||||
|
||||
async function copyText(text: string) {
|
||||
try {
|
||||
if (navigator.clipboard?.writeText) {
|
||||
await navigator.clipboard.writeText(text);
|
||||
setCopyStatus(t("machines.pairing.copied"));
|
||||
} else {
|
||||
setCopyStatus(t("machines.pairing.copyUnsupported"));
|
||||
}
|
||||
} catch {
|
||||
setCopyStatus(t("machines.pairing.copyFailed"));
|
||||
}
|
||||
setTimeout(() => setCopyStatus(null), 2000);
|
||||
}
|
||||
|
||||
function handleCardKeyDown(event: KeyboardEvent<HTMLDivElement>, machineId: string) {
|
||||
if (event.key === "Enter" || event.key === " ") {
|
||||
event.preventDefault();
|
||||
router.push(`/machines/${machineId}`);
|
||||
}
|
||||
}
|
||||
|
||||
const showCreateCard = showCreate || (!loading && machines.length === 0);
|
||||
|
||||
return (
|
||||
<div className="p-4 sm:p-6">
|
||||
<div className="mb-6 flex flex-col gap-4 sm:flex-row sm:items-center sm:justify-between">
|
||||
<div>
|
||||
<h1 className="text-2xl font-semibold text-white">{t("machines.title")}</h1>
|
||||
<p className="text-sm text-zinc-400">{t("machines.subtitle")}</p>
|
||||
</div>
|
||||
|
||||
<div className="flex w-full flex-wrap items-center gap-2 sm:w-auto">
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => setShowCreate((prev) => !prev)}
|
||||
className="w-full rounded-xl border border-emerald-400/40 bg-emerald-500/20 px-4 py-2 text-sm text-emerald-100 hover:bg-emerald-500/30 sm:w-auto"
|
||||
>
|
||||
{showCreate ? t("machines.cancel") : t("machines.addMachine")}
|
||||
</button>
|
||||
<Link
|
||||
href="/overview"
|
||||
className="w-full rounded-xl border border-white/10 bg-white/5 px-4 py-2 text-center text-sm text-white hover:bg-white/10 sm:w-auto"
|
||||
>
|
||||
{t("machines.backOverview")}
|
||||
</Link>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{showCreateCard && (
|
||||
<div className="mb-6 rounded-2xl border border-white/10 bg-white/5 p-5">
|
||||
<div className="flex flex-col gap-2 sm:flex-row sm:items-center sm:justify-between">
|
||||
<div>
|
||||
<div className="text-sm font-semibold text-white">{t("machines.addCardTitle")}</div>
|
||||
<div className="text-xs text-zinc-400">{t("machines.addCardSubtitle")}</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="mt-4 grid grid-cols-1 gap-3 md:grid-cols-3">
|
||||
<label className="rounded-xl border border-white/10 bg-black/20 p-3 text-xs text-zinc-400">
|
||||
{t("machines.field.name")}
|
||||
<input
|
||||
value={createName}
|
||||
onChange={(event) => setCreateName(event.target.value)}
|
||||
className="mt-2 w-full rounded-lg border border-white/10 bg-black/30 px-3 py-2 text-sm text-white"
|
||||
/>
|
||||
</label>
|
||||
<label className="rounded-xl border border-white/10 bg-black/20 p-3 text-xs text-zinc-400">
|
||||
{t("machines.field.code")}
|
||||
<input
|
||||
value={createCode}
|
||||
onChange={(event) => setCreateCode(event.target.value)}
|
||||
className="mt-2 w-full rounded-lg border border-white/10 bg-black/30 px-3 py-2 text-sm text-white"
|
||||
/>
|
||||
</label>
|
||||
<label className="rounded-xl border border-white/10 bg-black/20 p-3 text-xs text-zinc-400">
|
||||
{t("machines.field.location")}
|
||||
<input
|
||||
value={createLocation}
|
||||
onChange={(event) => setCreateLocation(event.target.value)}
|
||||
className="mt-2 w-full rounded-lg border border-white/10 bg-black/30 px-3 py-2 text-sm text-white"
|
||||
/>
|
||||
</label>
|
||||
</div>
|
||||
|
||||
<div className="mt-4 flex flex-wrap items-center gap-3">
|
||||
<button
|
||||
type="button"
|
||||
onClick={createMachine}
|
||||
disabled={creating}
|
||||
className="rounded-xl border border-emerald-400/40 bg-emerald-500/20 px-4 py-2 text-sm text-emerald-100 hover:bg-emerald-500/30 disabled:opacity-60"
|
||||
>
|
||||
{creating ? t("machines.create.loading") : t("machines.create.default")}
|
||||
</button>
|
||||
{createError && <div className="text-xs text-red-200">{createError}</div>}
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{createdMachine && (
|
||||
<div className="mb-6 rounded-2xl border border-emerald-500/20 bg-emerald-500/10 p-5">
|
||||
<div className="text-sm font-semibold text-white">{t("machines.pairing.title")}</div>
|
||||
<div className="mt-2 text-xs text-zinc-300">
|
||||
{t("machines.pairing.machine")} <span className="text-white">{createdMachine.name}</span>
|
||||
</div>
|
||||
<div className="mt-3 rounded-xl border border-white/10 bg-black/30 p-4">
|
||||
<div className="text-xs uppercase tracking-wide text-zinc-400">{t("machines.pairing.codeLabel")}</div>
|
||||
<div className="mt-2 text-3xl font-semibold text-white">{createdMachine.pairingCode}</div>
|
||||
<div className="mt-2 text-xs text-zinc-400">
|
||||
{t("machines.pairing.expires")}{" "}
|
||||
{createdMachine.pairingExpiresAt
|
||||
? new Date(createdMachine.pairingExpiresAt).toLocaleString(locale)
|
||||
: t("machines.pairing.soon")}
|
||||
</div>
|
||||
</div>
|
||||
<div className="mt-3 text-xs text-zinc-300">
|
||||
{t("machines.pairing.instructions")}
|
||||
</div>
|
||||
<div className="mt-3 flex flex-wrap items-center gap-3">
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => copyText(createdMachine.pairingCode)}
|
||||
className="rounded-xl border border-white/10 bg-white/5 px-3 py-2 text-sm text-white hover:bg-white/10"
|
||||
>
|
||||
{t("machines.pairing.copy")}
|
||||
</button>
|
||||
{copyStatus && <div className="text-xs text-zinc-300">{copyStatus}</div>}
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{loading && <div className="mb-4 text-sm text-zinc-400">{t("machines.loading")}</div>}
|
||||
|
||||
{!loading && machines.length === 0 && (
|
||||
<div className="mb-4 text-sm text-zinc-400">{t("machines.empty")}</div>
|
||||
)}
|
||||
|
||||
<div className="grid grid-cols-1 gap-4 md:grid-cols-2 xl:grid-cols-3">
|
||||
{(!loading ? machines : []).map((m) => {
|
||||
const hb = m.latestHeartbeat;
|
||||
const hbTs = hb?.tsServer ?? hb?.ts;
|
||||
const offline = isOffline(hbTs);
|
||||
const normalizedStatus = normalizeStatus(hb?.status);
|
||||
const statusLabel = offline ? t("machines.status.offline") : (normalizedStatus || t("machines.status.unknown"));
|
||||
const lastSeen = secondsAgo(hbTs, locale, t("common.never"));
|
||||
|
||||
return (
|
||||
<div
|
||||
key={m.id}
|
||||
role="link"
|
||||
tabIndex={0}
|
||||
onClick={() => router.push(`/machines/${m.id}`)}
|
||||
onKeyDown={(event) => handleCardKeyDown(event, m.id)}
|
||||
className="cursor-pointer rounded-2xl border border-white/10 bg-white/5 p-5 hover:bg-white/10"
|
||||
>
|
||||
<div className="flex items-center justify-between gap-3">
|
||||
<div className="min-w-0">
|
||||
<div className="truncate text-lg font-semibold text-white">{m.name}</div>
|
||||
<div className="mt-1 text-xs text-zinc-400">
|
||||
{m.code ? m.code : t("common.na")} - {t("machines.lastSeen", { time: lastSeen })}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<span
|
||||
className={`shrink-0 rounded-full px-3 py-1 text-xs ${badgeClass(
|
||||
normalizedStatus,
|
||||
offline
|
||||
)}`}
|
||||
>
|
||||
{statusLabel}
|
||||
</span>
|
||||
</div>
|
||||
|
||||
<div className="mt-4 text-sm text-zinc-400">{t("machines.status")}</div>
|
||||
<div className="mt-1 flex items-center gap-2 text-sm font-semibold text-white">
|
||||
{offline ? (
|
||||
<>
|
||||
<span className="inline-flex h-2.5 w-2.5 rounded-full bg-zinc-500" aria-hidden="true" />
|
||||
<span>{t("machines.status.noHeartbeat")}</span>
|
||||
</>
|
||||
) : (
|
||||
<>
|
||||
<span className="relative flex h-2.5 w-2.5" aria-hidden="true">
|
||||
<span className="animate-ping absolute inline-flex h-full w-full rounded-full bg-emerald-400 opacity-75" />
|
||||
<span className="relative inline-flex h-2.5 w-2.5 rounded-full bg-emerald-400" />
|
||||
</span>
|
||||
<span>{t("machines.status.ok")}</span>
|
||||
</>
|
||||
)}
|
||||
</div>
|
||||
|
||||
</div>
|
||||
);
|
||||
})}
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
@@ -1,133 +1,45 @@
|
||||
"use client";
|
||||
import { redirect } from "next/navigation";
|
||||
import { prisma } from "@/lib/prisma";
|
||||
import { requireSession } from "@/lib/auth/requireSession";
|
||||
import MachinesClient from "./MachinesClient";
|
||||
|
||||
import Link from "next/link";
|
||||
import { useEffect, useState } from "react";
|
||||
|
||||
type MachineRow = {
|
||||
id: string;
|
||||
name: string;
|
||||
code?: string | null;
|
||||
location?: string | null;
|
||||
latestHeartbeat: null | {
|
||||
ts: string;
|
||||
status: string;
|
||||
message?: string | null;
|
||||
ip?: string | null;
|
||||
fwVersion?: string | null;
|
||||
};
|
||||
};
|
||||
|
||||
function secondsAgo(ts?: string) {
|
||||
if (!ts) return "never";
|
||||
const diff = Math.floor((Date.now() - new Date(ts).getTime()) / 1000);
|
||||
if (diff < 60) return `${diff}s ago`;
|
||||
return `${Math.floor(diff / 60)}m ago`;
|
||||
function toIso(value?: Date | null) {
|
||||
return value ? value.toISOString() : null;
|
||||
}
|
||||
|
||||
function isOffline(ts?: string) {
|
||||
if (!ts) return true;
|
||||
return Date.now() - new Date(ts).getTime() > 15000; // 15s threshold
|
||||
export default async function MachinesPage() {
|
||||
const session = await requireSession();
|
||||
if (!session) redirect("/login?next=/machines");
|
||||
|
||||
const machines = await prisma.machine.findMany({
|
||||
where: { orgId: session.orgId },
|
||||
orderBy: { createdAt: "desc" },
|
||||
select: {
|
||||
id: true,
|
||||
name: true,
|
||||
code: true,
|
||||
location: true,
|
||||
createdAt: true,
|
||||
updatedAt: true,
|
||||
heartbeats: {
|
||||
orderBy: { tsServer: "desc" },
|
||||
take: 1,
|
||||
select: { ts: true, tsServer: true, status: true, message: true, ip: true, fwVersion: true },
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
const initialMachines = machines.map((machine) => ({
|
||||
...machine,
|
||||
latestHeartbeat: machine.heartbeats[0]
|
||||
? {
|
||||
...machine.heartbeats[0],
|
||||
ts: toIso(machine.heartbeats[0].ts) ?? "",
|
||||
tsServer: toIso(machine.heartbeats[0].tsServer),
|
||||
}
|
||||
: null,
|
||||
heartbeats: undefined,
|
||||
}));
|
||||
|
||||
function badgeClass(status?: string, offline?: boolean) {
|
||||
if (offline) return "bg-white/10 text-zinc-300";
|
||||
const s = (status ?? "").toUpperCase();
|
||||
if (s === "RUN") return "bg-emerald-500/15 text-emerald-300";
|
||||
if (s === "IDLE") return "bg-yellow-500/15 text-yellow-300";
|
||||
if (s === "STOP" || s === "DOWN") return "bg-red-500/15 text-red-300";
|
||||
return "bg-white/10 text-white";
|
||||
}
|
||||
|
||||
export default function MachinesPage() {
|
||||
const [machines, setMachines] = useState<MachineRow[]>([]);
|
||||
const [loading, setLoading] = useState(true);
|
||||
|
||||
useEffect(() => {
|
||||
let alive = true;
|
||||
|
||||
async function load() {
|
||||
try {
|
||||
const res = await fetch("/api/machines", { cache: "no-store" });
|
||||
const json = await res.json();
|
||||
if (alive) {
|
||||
setMachines(json.machines ?? []);
|
||||
setLoading(false);
|
||||
}
|
||||
} catch {
|
||||
if (alive) setLoading(false);
|
||||
}
|
||||
}
|
||||
|
||||
load();
|
||||
const t = setInterval(load, 5000);
|
||||
|
||||
return () => {
|
||||
alive = false;
|
||||
clearInterval(t);
|
||||
};
|
||||
}, []);
|
||||
|
||||
return (
|
||||
<div className="p-6">
|
||||
<div className="mb-6 flex items-center justify-between">
|
||||
<div>
|
||||
<h1 className="text-2xl font-semibold text-white">Machines</h1>
|
||||
<p className="text-sm text-zinc-400">Select a machine to view live KPIs.</p>
|
||||
</div>
|
||||
|
||||
<Link
|
||||
href="/overview"
|
||||
className="rounded-xl border border-white/10 bg-white/5 px-4 py-2 text-sm text-white hover:bg-white/10"
|
||||
>
|
||||
Back to Overview
|
||||
</Link>
|
||||
</div>
|
||||
|
||||
{loading && <div className="mb-4 text-sm text-zinc-400">Loading machines…</div>}
|
||||
|
||||
{!loading && machines.length === 0 && (
|
||||
<div className="mb-4 text-sm text-zinc-400">No machines found for this org.</div>
|
||||
)}
|
||||
|
||||
<div className="grid grid-cols-1 gap-4 md:grid-cols-2 xl:grid-cols-3">
|
||||
{(!loading ? machines : []).map((m) => {
|
||||
const hb = m.latestHeartbeat;
|
||||
const offline = isOffline(hb?.ts);
|
||||
const statusLabel = offline ? "OFFLINE" : hb?.status ?? "UNKNOWN";
|
||||
const lastSeen = secondsAgo(hb?.ts);
|
||||
|
||||
return (
|
||||
<Link
|
||||
key={m.id}
|
||||
href={`/machines/${m.id}`}
|
||||
className="rounded-2xl border border-white/10 bg-white/5 p-5 hover:bg-white/10"
|
||||
>
|
||||
<div className="flex items-center justify-between gap-3">
|
||||
<div className="min-w-0">
|
||||
<div className="truncate text-lg font-semibold text-white">{m.name}</div>
|
||||
<div className="mt-1 text-xs text-zinc-400">
|
||||
{m.code ? m.code : "—"} • Last seen {lastSeen}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<span
|
||||
className={`shrink-0 rounded-full px-3 py-1 text-xs ${badgeClass(
|
||||
hb?.status,
|
||||
offline
|
||||
)}`}
|
||||
>
|
||||
{statusLabel}
|
||||
</span>
|
||||
</div>
|
||||
|
||||
<div className="mt-4 text-sm text-zinc-400">Status</div>
|
||||
<div className="text-xl font-semibold text-white">
|
||||
{offline ? "No heartbeat" : hb?.message ?? "OK"}
|
||||
</div>
|
||||
</Link>
|
||||
);
|
||||
})}
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
return <MachinesClient initialMachines={initialMachines} />;
|
||||
}
|
||||
|
||||
465
app/(app)/overview/OverviewClient.tsx
Normal file
465
app/(app)/overview/OverviewClient.tsx
Normal file
@@ -0,0 +1,465 @@
|
||||
"use client";
|
||||
|
||||
import Link from "next/link";
|
||||
import { useEffect, useMemo, useState } from "react";
|
||||
import { useI18n } from "@/lib/i18n/useI18n";
|
||||
|
||||
type Heartbeat = {
|
||||
ts: string;
|
||||
tsServer?: string | null;
|
||||
status: string;
|
||||
message?: string | null;
|
||||
ip?: string | null;
|
||||
fwVersion?: string | null;
|
||||
};
|
||||
|
||||
type Kpi = {
|
||||
ts: string;
|
||||
oee?: number | null;
|
||||
availability?: number | null;
|
||||
performance?: number | null;
|
||||
quality?: number | null;
|
||||
workOrderId?: string | null;
|
||||
sku?: string | null;
|
||||
good?: number | null;
|
||||
scrap?: number | null;
|
||||
target?: number | null;
|
||||
cycleTime?: number | null;
|
||||
};
|
||||
|
||||
type MachineRow = {
|
||||
id: string;
|
||||
name: string;
|
||||
code?: string | null;
|
||||
location?: string | null;
|
||||
latestHeartbeat: Heartbeat | null;
|
||||
latestKpi?: Kpi | null;
|
||||
};
|
||||
|
||||
type EventRow = {
|
||||
id: string;
|
||||
ts: string;
|
||||
topic?: string;
|
||||
eventType: string;
|
||||
severity: string;
|
||||
title: string;
|
||||
description?: string | null;
|
||||
requiresAck: boolean;
|
||||
machineId?: string;
|
||||
machineName?: string;
|
||||
source: "ingested";
|
||||
};
|
||||
|
||||
const OFFLINE_MS = 30000;
|
||||
const MAX_EVENT_MACHINES = 6;
|
||||
|
||||
function secondsAgo(ts: string | undefined, locale: string, fallback: string) {
|
||||
if (!ts) return fallback;
|
||||
const diff = Math.floor((Date.now() - new Date(ts).getTime()) / 1000);
|
||||
const rtf = new Intl.RelativeTimeFormat(locale, { numeric: "auto" });
|
||||
if (diff < 60) return rtf.format(-diff, "second");
|
||||
if (diff < 3600) return rtf.format(-Math.floor(diff / 60), "minute");
|
||||
return rtf.format(-Math.floor(diff / 3600), "hour");
|
||||
}
|
||||
|
||||
function isOffline(ts?: string) {
|
||||
if (!ts) return true;
|
||||
return Date.now() - new Date(ts).getTime() > OFFLINE_MS;
|
||||
}
|
||||
|
||||
function normalizeStatus(status?: string) {
|
||||
const s = (status ?? "").toUpperCase();
|
||||
if (s === "ONLINE") return "RUN";
|
||||
return s;
|
||||
}
|
||||
|
||||
function heartbeatTime(hb?: Heartbeat | null) {
|
||||
return hb?.tsServer ?? hb?.ts;
|
||||
}
|
||||
|
||||
function fmtPct(v?: number | null) {
|
||||
if (v === null || v === undefined || Number.isNaN(v)) return "--";
|
||||
return `${v.toFixed(1)}%`;
|
||||
}
|
||||
|
||||
function fmtNum(v?: number | null) {
|
||||
if (v === null || v === undefined || Number.isNaN(v)) return "--";
|
||||
return `${Math.round(v)}`;
|
||||
}
|
||||
|
||||
function severityClass(sev?: string) {
|
||||
const s = (sev ?? "").toLowerCase();
|
||||
if (s === "critical") return "bg-red-500/15 text-red-300";
|
||||
if (s === "warning") return "bg-yellow-500/15 text-yellow-300";
|
||||
if (s === "info") return "bg-blue-500/15 text-blue-300";
|
||||
return "bg-white/10 text-zinc-200";
|
||||
}
|
||||
|
||||
function sourceClass(src: EventRow["source"]) {
|
||||
if (src === "ingested") return "bg-white/10 text-zinc-200";
|
||||
return "bg-white/10 text-zinc-200";
|
||||
}
|
||||
|
||||
export default function OverviewClient({
|
||||
initialMachines = [],
|
||||
initialEvents = [],
|
||||
}: {
|
||||
initialMachines?: MachineRow[];
|
||||
initialEvents?: EventRow[];
|
||||
}) {
|
||||
const { t, locale } = useI18n();
|
||||
const [machines, setMachines] = useState<MachineRow[]>(() => initialMachines);
|
||||
const [events, setEvents] = useState<EventRow[]>(() => initialEvents);
|
||||
const [loading, setLoading] = useState(false);
|
||||
const [eventsLoading, setEventsLoading] = useState(false);
|
||||
|
||||
useEffect(() => {
|
||||
let alive = true;
|
||||
|
||||
async function load() {
|
||||
try {
|
||||
setEventsLoading(true);
|
||||
const res = await fetch(`/api/overview?events=critical&eventMachines=${MAX_EVENT_MACHINES}`, {
|
||||
cache: "no-cache",
|
||||
});
|
||||
if (res.status === 304) {
|
||||
if (alive) setLoading(false);
|
||||
return;
|
||||
}
|
||||
const json = await res.json().catch(() => ({}));
|
||||
if (!alive) return;
|
||||
setMachines(json.machines ?? []);
|
||||
setEvents(json.events ?? []);
|
||||
setLoading(false);
|
||||
} catch {
|
||||
if (!alive) return;
|
||||
setMachines([]);
|
||||
setEvents([]);
|
||||
setLoading(false);
|
||||
} finally {
|
||||
if (alive) setEventsLoading(false);
|
||||
}
|
||||
}
|
||||
|
||||
load();
|
||||
const t = setInterval(load, 30000);
|
||||
return () => {
|
||||
alive = false;
|
||||
clearInterval(t);
|
||||
};
|
||||
}, []);
|
||||
|
||||
const stats = useMemo(() => {
|
||||
const total = machines.length;
|
||||
let online = 0;
|
||||
let running = 0;
|
||||
let idle = 0;
|
||||
let stopped = 0;
|
||||
let oeeSum = 0;
|
||||
let oeeCount = 0;
|
||||
let availSum = 0;
|
||||
let availCount = 0;
|
||||
let perfSum = 0;
|
||||
let perfCount = 0;
|
||||
let qualSum = 0;
|
||||
let qualCount = 0;
|
||||
let goodSum = 0;
|
||||
let scrapSum = 0;
|
||||
let targetSum = 0;
|
||||
|
||||
for (const m of machines) {
|
||||
const hb = m.latestHeartbeat;
|
||||
const offline = isOffline(heartbeatTime(hb));
|
||||
if (!offline) online += 1;
|
||||
|
||||
const status = normalizeStatus(hb?.status);
|
||||
if (!offline) {
|
||||
if (status === "RUN") running += 1;
|
||||
else if (status === "IDLE") idle += 1;
|
||||
else if (status === "STOP" || status === "DOWN") stopped += 1;
|
||||
}
|
||||
|
||||
const k = m.latestKpi;
|
||||
if (k?.oee != null) {
|
||||
oeeSum += Number(k.oee);
|
||||
oeeCount += 1;
|
||||
}
|
||||
if (k?.availability != null) {
|
||||
availSum += Number(k.availability);
|
||||
availCount += 1;
|
||||
}
|
||||
if (k?.performance != null) {
|
||||
perfSum += Number(k.performance);
|
||||
perfCount += 1;
|
||||
}
|
||||
if (k?.quality != null) {
|
||||
qualSum += Number(k.quality);
|
||||
qualCount += 1;
|
||||
}
|
||||
if (k?.good != null) goodSum += Number(k.good);
|
||||
if (k?.scrap != null) scrapSum += Number(k.scrap);
|
||||
if (k?.target != null) targetSum += Number(k.target);
|
||||
}
|
||||
|
||||
return {
|
||||
total,
|
||||
online,
|
||||
offline: total - online,
|
||||
running,
|
||||
idle,
|
||||
stopped,
|
||||
oee: oeeCount ? oeeSum / oeeCount : null,
|
||||
availability: availCount ? availSum / availCount : null,
|
||||
performance: perfCount ? perfSum / perfCount : null,
|
||||
quality: qualCount ? qualSum / qualCount : null,
|
||||
goodSum,
|
||||
scrapSum,
|
||||
targetSum,
|
||||
};
|
||||
}, [machines]);
|
||||
|
||||
const attention = useMemo(() => {
|
||||
const list = machines
|
||||
.map((m) => {
|
||||
const hb = m.latestHeartbeat;
|
||||
const offline = isOffline(heartbeatTime(hb));
|
||||
const k = m.latestKpi;
|
||||
const oee = k?.oee ?? null;
|
||||
let score = 0;
|
||||
if (offline) score += 100;
|
||||
if (oee != null && oee < 75) score += 50;
|
||||
if (oee != null && oee < 85) score += 25;
|
||||
return { machine: m, offline, oee, score };
|
||||
})
|
||||
.filter((x) => x.score > 0)
|
||||
.sort((a, b) => b.score - a.score)
|
||||
.slice(0, 6);
|
||||
|
||||
return list;
|
||||
}, [machines]);
|
||||
|
||||
const formatEventType = (eventType?: string) => {
|
||||
if (!eventType) return "";
|
||||
const key = `overview.event.${eventType}`;
|
||||
const label = t(key);
|
||||
return label === key ? eventType : label;
|
||||
};
|
||||
|
||||
const formatSource = (source?: string) => {
|
||||
if (!source) return "";
|
||||
const key = `overview.source.${source}`;
|
||||
const label = t(key);
|
||||
return label === key ? source : label;
|
||||
};
|
||||
|
||||
const formatSeverity = (severity?: string) => {
|
||||
if (!severity) return "";
|
||||
const key = `overview.severity.${severity}`;
|
||||
const label = t(key);
|
||||
return label === key ? severity.toUpperCase() : label;
|
||||
};
|
||||
|
||||
return (
|
||||
<div className="p-4 sm:p-6">
|
||||
<div className="mb-6 flex flex-col gap-4 sm:flex-row sm:items-start sm:justify-between">
|
||||
<div>
|
||||
<h1 className="text-2xl font-semibold text-white">{t("overview.title")}</h1>
|
||||
<p className="text-sm text-zinc-400">{t("overview.subtitle")}</p>
|
||||
</div>
|
||||
|
||||
<Link
|
||||
href="/machines"
|
||||
className="w-full rounded-xl border border-white/10 bg-white/5 px-4 py-2 text-center text-sm text-white hover:bg-white/10 sm:w-auto"
|
||||
>
|
||||
{t("overview.viewMachines")}
|
||||
</Link>
|
||||
</div>
|
||||
|
||||
{loading && <div className="mb-4 text-sm text-zinc-400">{t("overview.loading")}</div>}
|
||||
|
||||
<div className="grid grid-cols-1 gap-4 xl:grid-cols-3">
|
||||
<div className="rounded-2xl border border-white/10 bg-white/5 p-5">
|
||||
<div className="text-xs text-zinc-400">{t("overview.fleetHealth")}</div>
|
||||
<div className="mt-2 text-3xl font-semibold text-white">{stats.total}</div>
|
||||
<div className="mt-2 text-xs text-zinc-400">{t("overview.machinesTotal")}</div>
|
||||
<div className="mt-4 flex flex-wrap gap-2 text-xs">
|
||||
<span className="rounded-full bg-emerald-500/15 px-2 py-0.5 text-emerald-300">
|
||||
{t("overview.online")} {stats.online}
|
||||
</span>
|
||||
<span className="rounded-full bg-white/10 px-2 py-0.5 text-zinc-300">
|
||||
{t("overview.offline")} {stats.offline}
|
||||
</span>
|
||||
<span className="rounded-full bg-emerald-500/10 px-2 py-0.5 text-emerald-200">
|
||||
{t("overview.run")} {stats.running}
|
||||
</span>
|
||||
<span className="rounded-full bg-yellow-500/15 px-2 py-0.5 text-yellow-300">
|
||||
{t("overview.idle")} {stats.idle}
|
||||
</span>
|
||||
<span className="rounded-full bg-red-500/15 px-2 py-0.5 text-red-300">
|
||||
{t("overview.stop")} {stats.stopped}
|
||||
</span>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="rounded-2xl border border-white/10 bg-white/5 p-5">
|
||||
<div className="text-xs text-zinc-400">{t("overview.productionTotals")}</div>
|
||||
<div className="mt-2 grid grid-cols-1 gap-3 sm:grid-cols-3">
|
||||
<div className="rounded-xl border border-white/10 bg-black/20 p-3">
|
||||
<div className="text-[11px] text-zinc-400">{t("overview.good")}</div>
|
||||
<div className="mt-1 text-sm font-semibold text-white">{fmtNum(stats.goodSum)}</div>
|
||||
</div>
|
||||
<div className="rounded-xl border border-white/10 bg-black/20 p-3">
|
||||
<div className="text-[11px] text-zinc-400">{t("overview.scrap")}</div>
|
||||
<div className="mt-1 text-sm font-semibold text-white">{fmtNum(stats.scrapSum)}</div>
|
||||
</div>
|
||||
<div className="rounded-xl border border-white/10 bg-black/20 p-3">
|
||||
<div className="text-[11px] text-zinc-400">{t("overview.target")}</div>
|
||||
<div className="mt-1 text-sm font-semibold text-white">{fmtNum(stats.targetSum)}</div>
|
||||
</div>
|
||||
</div>
|
||||
<div className="mt-3 text-xs text-zinc-400">{t("overview.kpiSumNote")}</div>
|
||||
</div>
|
||||
|
||||
<div className="rounded-2xl border border-white/10 bg-white/5 p-5">
|
||||
<div className="text-xs text-zinc-400">{t("overview.activityFeed")}</div>
|
||||
<div className="mt-2 text-3xl font-semibold text-white">{events.length}</div>
|
||||
<div className="mt-2 text-xs text-zinc-400">
|
||||
{eventsLoading ? t("overview.eventsRefreshing") : t("overview.eventsLast30")}
|
||||
</div>
|
||||
<div className="mt-4 space-y-2">
|
||||
{events.slice(0, 3).map((e) => (
|
||||
<div key={e.id} className="flex items-center justify-between text-xs text-zinc-300">
|
||||
<div className="truncate">
|
||||
{e.machineName ? `${e.machineName}: ` : ""}
|
||||
{e.title}
|
||||
</div>
|
||||
<div className="shrink-0 text-zinc-500">
|
||||
{secondsAgo(e.ts, locale, t("common.never"))}
|
||||
</div>
|
||||
</div>
|
||||
))}
|
||||
{events.length === 0 && !eventsLoading ? (
|
||||
<div className="text-xs text-zinc-500">{t("overview.eventsNone")}</div>
|
||||
) : null}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="mt-6 grid grid-cols-1 gap-4 md:grid-cols-2 xl:grid-cols-4">
|
||||
<div className="rounded-2xl border border-white/10 bg-white/5 p-5">
|
||||
<div className="text-xs text-zinc-400">{t("overview.oeeAvg")}</div>
|
||||
<div className="mt-2 text-3xl font-semibold text-emerald-300">{fmtPct(stats.oee)}</div>
|
||||
</div>
|
||||
<div className="rounded-2xl border border-white/10 bg-white/5 p-5">
|
||||
<div className="text-xs text-zinc-400">{t("overview.availabilityAvg")}</div>
|
||||
<div className="mt-2 text-2xl font-semibold text-white">{fmtPct(stats.availability)}</div>
|
||||
</div>
|
||||
<div className="rounded-2xl border border-white/10 bg-white/5 p-5">
|
||||
<div className="text-xs text-zinc-400">{t("overview.performanceAvg")}</div>
|
||||
<div className="mt-2 text-2xl font-semibold text-white">{fmtPct(stats.performance)}</div>
|
||||
</div>
|
||||
<div className="rounded-2xl border border-white/10 bg-white/5 p-5">
|
||||
<div className="text-xs text-zinc-400">{t("overview.qualityAvg")}</div>
|
||||
<div className="mt-2 text-2xl font-semibold text-white">{fmtPct(stats.quality)}</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="mt-6 grid grid-cols-1 gap-4 xl:grid-cols-3">
|
||||
<div className="rounded-2xl border border-white/10 bg-white/5 p-5 xl:col-span-1">
|
||||
<div className="mb-3 flex items-center justify-between">
|
||||
<div className="text-sm font-semibold text-white">{t("overview.attentionList")}</div>
|
||||
<div className="text-xs text-zinc-400">
|
||||
{attention.length} {t("overview.shown")}
|
||||
</div>
|
||||
</div>
|
||||
{attention.length === 0 ? (
|
||||
<div className="text-sm text-zinc-400">{t("overview.noUrgent")}</div>
|
||||
) : (
|
||||
<div className="space-y-3">
|
||||
{attention.map(({ machine, offline, oee }) => (
|
||||
<div key={machine.id} className="rounded-xl border border-white/10 bg-black/20 p-3">
|
||||
<div className="flex items-center justify-between gap-3">
|
||||
<div className="min-w-0">
|
||||
<div className="truncate text-sm font-semibold text-white">{machine.name}</div>
|
||||
<div className="mt-1 text-xs text-zinc-400">
|
||||
{machine.code ?? ""} {machine.location ? `- ${machine.location}` : ""}
|
||||
</div>
|
||||
</div>
|
||||
<div className="text-xs text-zinc-400">
|
||||
{secondsAgo(heartbeatTime(machine.latestHeartbeat), locale, t("common.never"))}
|
||||
</div>
|
||||
</div>
|
||||
<div className="mt-2 flex items-center gap-2 text-xs">
|
||||
<span
|
||||
className={`rounded-full px-2 py-0.5 ${
|
||||
offline ? "bg-white/10 text-zinc-300" : "bg-emerald-500/15 text-emerald-300"
|
||||
}`}
|
||||
>
|
||||
{offline ? t("overview.status.offline") : t("overview.status.online")}
|
||||
</span>
|
||||
{oee != null && (
|
||||
<span className="rounded-full bg-yellow-500/15 px-2 py-0.5 text-yellow-300">
|
||||
OEE {fmtPct(oee)}
|
||||
</span>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
|
||||
<div className="rounded-2xl border border-white/10 bg-white/5 p-5 xl:col-span-2">
|
||||
<div className="mb-3 flex items-center justify-between">
|
||||
<div className="text-sm font-semibold text-white">{t("overview.timeline")}</div>
|
||||
<div className="text-xs text-zinc-400">
|
||||
{events.length} {t("overview.items")}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{events.length === 0 && !eventsLoading ? (
|
||||
<div className="text-sm text-zinc-400">{t("overview.noEvents")}</div>
|
||||
) : (
|
||||
<div className="h-[360px] space-y-3 overflow-y-auto no-scrollbar">
|
||||
{events.map((e) => (
|
||||
<div key={`${e.id}-${e.source}`} className="rounded-xl border border-white/10 bg-black/20 p-4">
|
||||
<div className="flex items-start justify-between gap-3">
|
||||
<div className="min-w-0">
|
||||
<div className="flex flex-wrap items-center gap-2">
|
||||
<span className={`rounded-full px-2 py-0.5 text-xs ${severityClass(e.severity)}`}>
|
||||
{formatSeverity(e.severity)}
|
||||
</span>
|
||||
<span className="rounded-full bg-white/10 px-2 py-0.5 text-xs text-zinc-200">
|
||||
{formatEventType(e.eventType)}
|
||||
</span>
|
||||
<span className={`rounded-full px-2 py-0.5 text-xs ${sourceClass(e.source)}`}>
|
||||
{formatSource(e.source)}
|
||||
</span>
|
||||
{e.requiresAck ? (
|
||||
<span className="rounded-full bg-white/10 px-2 py-0.5 text-xs text-white">
|
||||
{t("overview.ack")}
|
||||
</span>
|
||||
) : null}
|
||||
</div>
|
||||
|
||||
<div className="mt-2 truncate text-sm font-semibold text-white">
|
||||
{e.machineName ? `${e.machineName}: ` : ""}
|
||||
{e.title}
|
||||
</div>
|
||||
{e.description ? (
|
||||
<div className="mt-1 text-sm text-zinc-300">{e.description}</div>
|
||||
) : null}
|
||||
</div>
|
||||
<div className="shrink-0 text-xs text-zinc-400">
|
||||
{secondsAgo(e.ts, locale, t("common.never"))}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
47
app/(app)/overview/page.tsx
Normal file
47
app/(app)/overview/page.tsx
Normal file
@@ -0,0 +1,47 @@
|
||||
import { redirect } from "next/navigation";
|
||||
import { requireSession } from "@/lib/auth/requireSession";
|
||||
import { getOverviewData } from "@/lib/overview/getOverviewData";
|
||||
import OverviewClient from "./OverviewClient";
|
||||
|
||||
function toIso(value?: Date | null) {
|
||||
return value ? value.toISOString() : null;
|
||||
}
|
||||
|
||||
export default async function OverviewPage() {
|
||||
const session = await requireSession();
|
||||
if (!session) redirect("/login?next=/overview");
|
||||
|
||||
const { machines, events } = await getOverviewData({
|
||||
orgId: session.orgId,
|
||||
eventsMode: "critical",
|
||||
eventsWindowSec: 21600,
|
||||
eventMachines: 6,
|
||||
});
|
||||
|
||||
const initialMachines = machines.map((machine) => ({
|
||||
...machine,
|
||||
createdAt: toIso(machine.createdAt),
|
||||
updatedAt: toIso(machine.updatedAt),
|
||||
latestHeartbeat: machine.latestHeartbeat
|
||||
? {
|
||||
...machine.latestHeartbeat,
|
||||
ts: toIso(machine.latestHeartbeat.ts) ?? "",
|
||||
tsServer: toIso(machine.latestHeartbeat.tsServer),
|
||||
}
|
||||
: null,
|
||||
latestKpi: machine.latestKpi
|
||||
? {
|
||||
...machine.latestKpi,
|
||||
ts: toIso(machine.latestKpi.ts) ?? "",
|
||||
}
|
||||
: null,
|
||||
}));
|
||||
|
||||
const initialEvents = events.map((event) => ({
|
||||
...event,
|
||||
ts: event.ts ? event.ts.toISOString() : "",
|
||||
machineName: event.machineName ?? undefined,
|
||||
}));
|
||||
|
||||
return <OverviewClient initialMachines={initialMachines} initialEvents={initialEvents} />;
|
||||
}
|
||||
15
app/(app)/reports/downtime-pareto/page.tsx
Normal file
15
app/(app)/reports/downtime-pareto/page.tsx
Normal file
@@ -0,0 +1,15 @@
|
||||
import { redirect } from "next/navigation";
|
||||
|
||||
export default function LegacyDowntimeParetoPage({
|
||||
searchParams,
|
||||
}: {
|
||||
searchParams: Record<string, string | string[] | undefined>;
|
||||
}) {
|
||||
const qs = new URLSearchParams();
|
||||
for (const [k, v] of Object.entries(searchParams)) {
|
||||
if (typeof v === "string") qs.set(k, v);
|
||||
else if (Array.isArray(v)) v.forEach((vv) => qs.append(k, vv));
|
||||
}
|
||||
const q = qs.toString();
|
||||
redirect(q ? `/downtime?${q}` : "/downtime");
|
||||
}
|
||||
21
app/(app)/reports/loading.tsx
Normal file
21
app/(app)/reports/loading.tsx
Normal file
@@ -0,0 +1,21 @@
|
||||
export default function ReportsLoading() {
|
||||
return (
|
||||
<div className="p-4 sm:p-6 space-y-6">
|
||||
<div className="h-8 w-56 rounded-lg bg-white/5" />
|
||||
<div className="grid gap-4 lg:grid-cols-4">
|
||||
{Array.from({ length: 4 }).map((_, idx) => (
|
||||
<div key={idx} className="h-24 rounded-2xl border border-white/10 bg-white/5" />
|
||||
))}
|
||||
</div>
|
||||
<div className="grid gap-4 lg:grid-cols-[2fr_1fr]">
|
||||
<div className="h-80 rounded-2xl border border-white/10 bg-white/5" />
|
||||
<div className="h-80 rounded-2xl border border-white/10 bg-white/5" />
|
||||
</div>
|
||||
<div className="grid gap-4 lg:grid-cols-3">
|
||||
{Array.from({ length: 3 }).map((_, idx) => (
|
||||
<div key={idx} className="h-24 rounded-2xl border border-white/10 bg-white/5" />
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
880
app/(app)/reports/page.tsx
Normal file
880
app/(app)/reports/page.tsx
Normal file
@@ -0,0 +1,880 @@
|
||||
"use client";
|
||||
|
||||
import { useEffect, useMemo, useState } from "react";
|
||||
import { useI18n } from "@/lib/i18n/useI18n";
|
||||
import {
|
||||
Bar,
|
||||
BarChart,
|
||||
CartesianGrid,
|
||||
Cell,
|
||||
Line,
|
||||
LineChart,
|
||||
ResponsiveContainer,
|
||||
Tooltip,
|
||||
XAxis,
|
||||
YAxis,
|
||||
} from "recharts";
|
||||
|
||||
type RangeKey = "24h" | "7d" | "30d" | "custom";
|
||||
|
||||
type ReportSummary = {
|
||||
oeeAvg: number | null;
|
||||
availabilityAvg: number | null;
|
||||
performanceAvg: number | null;
|
||||
qualityAvg: number | null;
|
||||
goodTotal: number | null;
|
||||
scrapTotal: number | null;
|
||||
targetTotal: number | null;
|
||||
scrapRate: number | null;
|
||||
topScrapSku?: string | null;
|
||||
topScrapWorkOrder?: string | null;
|
||||
};
|
||||
|
||||
type ReportDowntime = {
|
||||
macrostopSec: number;
|
||||
microstopSec: number;
|
||||
slowCycleCount: number;
|
||||
qualitySpikeCount: number;
|
||||
performanceDegradationCount: number;
|
||||
oeeDropCount: number;
|
||||
};
|
||||
|
||||
type ReportTrendPoint = { t: string; v: number };
|
||||
|
||||
type ReportPayload = {
|
||||
summary: ReportSummary;
|
||||
downtime: ReportDowntime;
|
||||
trend: {
|
||||
oee: ReportTrendPoint[];
|
||||
availability: ReportTrendPoint[];
|
||||
performance: ReportTrendPoint[];
|
||||
quality: ReportTrendPoint[];
|
||||
scrapRate: ReportTrendPoint[];
|
||||
};
|
||||
distribution: {
|
||||
cycleTime: {
|
||||
label: string;
|
||||
count: number;
|
||||
rangeStart?: number;
|
||||
rangeEnd?: number;
|
||||
overflow?: "low" | "high";
|
||||
minValue?: number;
|
||||
maxValue?: number;
|
||||
}[];
|
||||
};
|
||||
insights?: string[];
|
||||
};
|
||||
|
||||
type MachineOption = { id: string; name: string };
|
||||
type FilterOptions = { workOrders: string[]; skus: string[] };
|
||||
type Translator = (key: string, vars?: Record<string, string | number>) => string;
|
||||
type TooltipPayload<T> = { payload?: T; name?: string; value?: number | string };
|
||||
type SimpleTooltipProps<T> = {
|
||||
active?: boolean;
|
||||
payload?: Array<TooltipPayload<T>>;
|
||||
label?: string | number;
|
||||
};
|
||||
type CycleHistogramRow = ReportPayload["distribution"]["cycleTime"][number];
|
||||
|
||||
function fmtPct(v?: number | null) {
|
||||
if (v === null || v === undefined || Number.isNaN(v)) return "--";
|
||||
return `${v.toFixed(1)}%`;
|
||||
}
|
||||
|
||||
function fmtDuration(sec?: number | null) {
|
||||
if (!sec) return "--";
|
||||
const h = Math.floor(sec / 3600);
|
||||
const m = Math.floor((sec % 3600) / 60);
|
||||
if (h > 0) return `${h}h ${m}m`;
|
||||
return `${m}m`;
|
||||
}
|
||||
|
||||
function downsample<T>(rows: T[], max: number) {
|
||||
if (rows.length <= max) return rows;
|
||||
const step = Math.ceil(rows.length / max);
|
||||
return rows.filter((_, idx) => idx % step === 0);
|
||||
}
|
||||
|
||||
function formatTickLabel(ts: string, range: RangeKey) {
|
||||
const d = new Date(ts);
|
||||
if (Number.isNaN(d.getTime())) return ts;
|
||||
const hh = String(d.getHours()).padStart(2, "0");
|
||||
const mm = String(d.getMinutes()).padStart(2, "0");
|
||||
const month = String(d.getMonth() + 1).padStart(2, "0");
|
||||
const day = String(d.getDate()).padStart(2, "0");
|
||||
if (range === "24h") return `${hh}:${mm}`;
|
||||
return `${month}-${day}`;
|
||||
}
|
||||
|
||||
function CycleTooltip({ active, payload, t }: SimpleTooltipProps<CycleHistogramRow> & { t: Translator }) {
|
||||
if (!active || !payload?.length) return null;
|
||||
const p = payload[0]?.payload;
|
||||
if (!p) return null;
|
||||
|
||||
let detail = "";
|
||||
if (p.overflow === "low") {
|
||||
detail = `${t("reports.tooltip.below")} ${p.rangeEnd?.toFixed(1)}s`;
|
||||
} else if (p.overflow === "high") {
|
||||
detail = `${t("reports.tooltip.above")} ${p.rangeStart?.toFixed(1)}s`;
|
||||
} else if (p.rangeStart != null && p.rangeEnd != null) {
|
||||
detail = `${p.rangeStart.toFixed(1)}s - ${p.rangeEnd.toFixed(1)}s`;
|
||||
}
|
||||
|
||||
const extreme =
|
||||
p.overflow && (p.minValue != null || p.maxValue != null)
|
||||
? `${t("reports.tooltip.extremes")}: ${p.minValue?.toFixed(1) ?? "--"}s - ${p.maxValue?.toFixed(1) ?? "--"}s`
|
||||
: "";
|
||||
|
||||
return (
|
||||
<div className="rounded-xl border border-white/10 bg-zinc-950/95 px-4 py-3 shadow-lg">
|
||||
<div className="text-sm font-semibold text-white">{p.label}</div>
|
||||
<div className="mt-2 space-y-1 text-xs text-zinc-300">
|
||||
<div>
|
||||
{t("reports.tooltip.cycles")}: <span className="text-white">{p.count}</span>
|
||||
</div>
|
||||
{detail ? (
|
||||
<div>
|
||||
{t("reports.tooltip.range")}: <span className="text-white">{detail}</span>
|
||||
</div>
|
||||
) : null}
|
||||
{extreme ? <div className="text-zinc-400">{extreme}</div> : null}
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
function DowntimeTooltip({ active, payload, t }: SimpleTooltipProps<{ name?: string; value?: number }> & { t: Translator }) {
|
||||
if (!active || !payload?.length) return null;
|
||||
const row = payload[0]?.payload ?? {};
|
||||
const label = row.name ?? payload[0]?.name ?? "";
|
||||
const value = row.value ?? payload[0]?.value ?? 0;
|
||||
|
||||
return (
|
||||
<div className="rounded-xl border border-white/10 bg-zinc-950/95 px-4 py-3 shadow-lg">
|
||||
<div className="text-sm font-semibold text-white">{label}</div>
|
||||
<div className="mt-2 text-xs text-zinc-300">
|
||||
{t("reports.tooltip.downtime")}: <span className="text-white">{Number(value)} min</span>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
function toMachineOption(value: unknown): MachineOption | null {
|
||||
if (!value || typeof value !== "object") return null;
|
||||
const record = value as Record<string, unknown>;
|
||||
const id = typeof record.id === "string" ? record.id : "";
|
||||
const name = typeof record.name === "string" ? record.name : "";
|
||||
if (!id || !name) return null;
|
||||
return { id, name };
|
||||
}
|
||||
|
||||
function buildCsv(report: ReportPayload, t: Translator) {
|
||||
const rows = new Map<string, Record<string, string | number>>();
|
||||
const addSeries = (series: ReportTrendPoint[], key: string) => {
|
||||
for (const p of series) {
|
||||
const row = rows.get(p.t) ?? { timestamp: p.t };
|
||||
row[key] = p.v;
|
||||
rows.set(p.t, row);
|
||||
}
|
||||
};
|
||||
|
||||
addSeries(report.trend.oee, "oee");
|
||||
addSeries(report.trend.availability, "availability");
|
||||
addSeries(report.trend.performance, "performance");
|
||||
addSeries(report.trend.quality, "quality");
|
||||
addSeries(report.trend.scrapRate, "scrapRate");
|
||||
|
||||
const ordered = [...rows.values()].sort((a, b) => {
|
||||
const at = new Date(String(a.timestamp)).getTime();
|
||||
const bt = new Date(String(b.timestamp)).getTime();
|
||||
return at - bt;
|
||||
});
|
||||
|
||||
const header = ["timestamp", "oee", "availability", "performance", "quality", "scrapRate"].join(",");
|
||||
const lines = ordered.map((row) =>
|
||||
[
|
||||
row.timestamp,
|
||||
row.oee ?? "",
|
||||
row.availability ?? "",
|
||||
row.performance ?? "",
|
||||
row.quality ?? "",
|
||||
row.scrapRate ?? "",
|
||||
]
|
||||
.map((v) => (v == null ? "" : String(v)))
|
||||
.join(",")
|
||||
);
|
||||
|
||||
const summary = report.summary;
|
||||
const downtime = report.downtime;
|
||||
|
||||
const sectionLines: string[] = [];
|
||||
sectionLines.push(
|
||||
[t("reports.csv.section"), t("reports.csv.key"), t("reports.csv.value")].join(",")
|
||||
);
|
||||
const addRow = (section: string, key: string, value: string | number | null | undefined) => {
|
||||
sectionLines.push(
|
||||
[section, key, value == null ? "" : String(value)]
|
||||
.map((v) => (v.includes(",") ? `"${v.replace(/\"/g, '""')}"` : v))
|
||||
.join(",")
|
||||
);
|
||||
};
|
||||
|
||||
addRow("summary", "oeeAvg", summary.oeeAvg);
|
||||
addRow("summary", "availabilityAvg", summary.availabilityAvg);
|
||||
addRow("summary", "performanceAvg", summary.performanceAvg);
|
||||
addRow("summary", "qualityAvg", summary.qualityAvg);
|
||||
addRow("summary", "goodTotal", summary.goodTotal);
|
||||
addRow("summary", "scrapTotal", summary.scrapTotal);
|
||||
addRow("summary", "targetTotal", summary.targetTotal);
|
||||
addRow("summary", "scrapRate", summary.scrapRate);
|
||||
addRow("summary", "topScrapSku", summary.topScrapSku ?? "");
|
||||
addRow("summary", "topScrapWorkOrder", summary.topScrapWorkOrder ?? "");
|
||||
|
||||
addRow("loss_drivers", "macrostopSec", downtime.macrostopSec);
|
||||
addRow("loss_drivers", "microstopSec", downtime.microstopSec);
|
||||
addRow("loss_drivers", "slowCycleCount", downtime.slowCycleCount);
|
||||
addRow("loss_drivers", "qualitySpikeCount", downtime.qualitySpikeCount);
|
||||
addRow("loss_drivers", "performanceDegradationCount", downtime.performanceDegradationCount);
|
||||
addRow("loss_drivers", "oeeDropCount", downtime.oeeDropCount);
|
||||
|
||||
for (const bin of report.distribution.cycleTime) {
|
||||
addRow("cycle_distribution", bin.label, bin.count);
|
||||
}
|
||||
|
||||
if (report.insights?.length) {
|
||||
report.insights.forEach((note, idx) => addRow("insights", String(idx + 1), note));
|
||||
}
|
||||
|
||||
return [header, ...lines, "", ...sectionLines].join("\n");
|
||||
}
|
||||
|
||||
function downloadText(filename: string, content: string) {
|
||||
const blob = new Blob([content], { type: "text/csv;charset=utf-8;" });
|
||||
const url = URL.createObjectURL(blob);
|
||||
const link = document.createElement("a");
|
||||
link.href = url;
|
||||
link.setAttribute("download", filename);
|
||||
document.body.appendChild(link);
|
||||
link.click();
|
||||
document.body.removeChild(link);
|
||||
URL.revokeObjectURL(url);
|
||||
}
|
||||
|
||||
function buildPdfHtml(
|
||||
report: ReportPayload,
|
||||
rangeLabel: string,
|
||||
filters: { machine: string; workOrder: string; sku: string },
|
||||
t: Translator
|
||||
) {
|
||||
const summary = report.summary;
|
||||
const downtime = report.downtime;
|
||||
const cycleBins = report.distribution.cycleTime;
|
||||
const insights = report.insights ?? [];
|
||||
|
||||
return `
|
||||
<!doctype html>
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="utf-8" />
|
||||
<title>${t("reports.pdf.title")}</title>
|
||||
<style>
|
||||
body { font-family: Arial, sans-serif; color: #111; margin: 24px; }
|
||||
h1 { margin: 0 0 6px; }
|
||||
.meta { margin-bottom: 16px; color: #555; }
|
||||
.grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 16px; }
|
||||
.card { border: 1px solid #ddd; border-radius: 8px; padding: 12px; }
|
||||
.label { color: #666; font-size: 12px; text-transform: uppercase; letter-spacing: .03em; }
|
||||
.value { font-size: 18px; font-weight: 600; margin-top: 6px; }
|
||||
table { width: 100%; border-collapse: collapse; margin-top: 12px; }
|
||||
th, td { border: 1px solid #ddd; padding: 6px 8px; font-size: 12px; }
|
||||
th { background: #f5f5f5; text-align: left; }
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<h1>${t("reports.title")}</h1>
|
||||
<div class="meta">${t("reports.pdf.range")}: ${rangeLabel} | ${t("reports.pdf.machine")}: ${filters.machine} | ${t("reports.pdf.workOrder")}: ${filters.workOrder} | ${t("reports.pdf.sku")}: ${filters.sku}</div>
|
||||
|
||||
<div class="grid">
|
||||
<div class="card">
|
||||
<div class="label">OEE (avg)</div>
|
||||
<div class="value">${summary.oeeAvg != null ? summary.oeeAvg.toFixed(1) + "%" : "--"}</div>
|
||||
</div>
|
||||
<div class="card">
|
||||
<div class="label">Availability (avg)</div>
|
||||
<div class="value">${summary.availabilityAvg != null ? summary.availabilityAvg.toFixed(1) + "%" : "--"}</div>
|
||||
</div>
|
||||
<div class="card">
|
||||
<div class="label">Performance (avg)</div>
|
||||
<div class="value">${summary.performanceAvg != null ? summary.performanceAvg.toFixed(1) + "%" : "--"}</div>
|
||||
</div>
|
||||
<div class="card">
|
||||
<div class="label">Quality (avg)</div>
|
||||
<div class="value">${summary.qualityAvg != null ? summary.qualityAvg.toFixed(1) + "%" : "--"}</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="card" style="margin-top: 16px;">
|
||||
<div class="label">${t("reports.pdf.topLoss")}</div>
|
||||
<table>
|
||||
<thead>
|
||||
<tr><th>${t("reports.pdf.metric")}</th><th>${t("reports.pdf.value")}</th></tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<tr><td>${t("reports.loss.macrostop")} (sec)</td><td>${downtime.macrostopSec}</td></tr>
|
||||
<tr><td>${t("reports.loss.microstop")} (sec)</td><td>${downtime.microstopSec}</td></tr>
|
||||
<tr><td>${t("reports.loss.slowCycle")}</td><td>${downtime.slowCycleCount}</td></tr>
|
||||
<tr><td>${t("reports.loss.qualitySpike")}</td><td>${downtime.qualitySpikeCount}</td></tr>
|
||||
<tr><td>${t("reports.loss.perfDegradation")}</td><td>${downtime.performanceDegradationCount}</td></tr>
|
||||
<tr><td>${t("reports.loss.oeeDrop")}</td><td>${downtime.oeeDropCount}</td></tr>
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
|
||||
<div class="card" style="margin-top: 16px;">
|
||||
<div class="label">${t("reports.pdf.qualitySummary")}</div>
|
||||
<table>
|
||||
<thead>
|
||||
<tr><th>${t("reports.pdf.metric")}</th><th>${t("reports.pdf.value")}</th></tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<tr><td>${t("reports.scrapRate")}</td><td>${summary.scrapRate != null ? summary.scrapRate.toFixed(1) + "%" : "--"}</td></tr>
|
||||
<tr><td>${t("overview.good")}</td><td>${summary.goodTotal ?? "--"}</td></tr>
|
||||
<tr><td>${t("overview.scrap")}</td><td>${summary.scrapTotal ?? "--"}</td></tr>
|
||||
<tr><td>${t("overview.target")}</td><td>${summary.targetTotal ?? "--"}</td></tr>
|
||||
<tr><td>${t("reports.topScrapSku")}</td><td>${summary.topScrapSku ?? "--"}</td></tr>
|
||||
<tr><td>${t("reports.topScrapWorkOrder")}</td><td>${summary.topScrapWorkOrder ?? "--"}</td></tr>
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
|
||||
<div class="card" style="margin-top: 16px;">
|
||||
<div class="label">${t("reports.pdf.cycleDistribution")}</div>
|
||||
<table>
|
||||
<thead>
|
||||
<tr><th>${t("reports.tooltip.range")}</th><th>${t("reports.tooltip.cycles")}</th></tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
${cycleBins
|
||||
.map((bin) => `<tr><td>${bin.label}</td><td>${bin.count}</td></tr>`)
|
||||
.join("")}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
|
||||
<div class="card" style="margin-top: 16px;">
|
||||
<div class="label">${t("reports.pdf.notes")}</div>
|
||||
${insights.length ? `<ul>${insights.map((n) => `<li>${n}</li>`).join("")}</ul>` : `<div>${t("reports.pdf.none")}</div>`}
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
`.trim();
|
||||
}
|
||||
|
||||
export default function ReportsPage() {
|
||||
const { t, locale } = useI18n();
|
||||
const [range, setRange] = useState<RangeKey>("24h");
|
||||
const [report, setReport] = useState<ReportPayload | null>(null);
|
||||
const [loading, setLoading] = useState(true);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
const [machines, setMachines] = useState<MachineOption[]>([]);
|
||||
const [filterOptions, setFilterOptions] = useState<FilterOptions>({ workOrders: [], skus: [] });
|
||||
const [machineId, setMachineId] = useState("");
|
||||
const [workOrderId, setWorkOrderId] = useState("");
|
||||
const [sku, setSku] = useState("");
|
||||
|
||||
const rangeLabel = useMemo(() => {
|
||||
if (range === "24h") return t("reports.rangeLabel.last24");
|
||||
if (range === "7d") return t("reports.rangeLabel.last7");
|
||||
if (range === "30d") return t("reports.rangeLabel.last30");
|
||||
return t("reports.rangeLabel.custom");
|
||||
}, [range, t]);
|
||||
|
||||
useEffect(() => {
|
||||
let alive = true;
|
||||
|
||||
async function loadMachines() {
|
||||
try {
|
||||
const res = await fetch("/api/machines", { cache: "no-store" });
|
||||
const json = await res.json();
|
||||
if (!alive) return;
|
||||
const rows: unknown[] = Array.isArray(json?.machines) ? json.machines : [];
|
||||
const options: MachineOption[] = [];
|
||||
rows.forEach((row) => {
|
||||
const option = toMachineOption(row);
|
||||
if (option) options.push(option);
|
||||
});
|
||||
setMachines(options);
|
||||
} catch {
|
||||
if (!alive) return;
|
||||
setMachines([]);
|
||||
}
|
||||
}
|
||||
|
||||
loadMachines();
|
||||
return () => {
|
||||
alive = false;
|
||||
};
|
||||
}, []);
|
||||
|
||||
useEffect(() => {
|
||||
let alive = true;
|
||||
const controller = new AbortController();
|
||||
|
||||
async function load() {
|
||||
setLoading(true);
|
||||
setError(null);
|
||||
try {
|
||||
const params = new URLSearchParams({ range });
|
||||
if (machineId) params.set("machineId", machineId);
|
||||
if (workOrderId) params.set("workOrderId", workOrderId);
|
||||
if (sku) params.set("sku", sku);
|
||||
|
||||
const res = await fetch(`/api/reports?${params.toString()}`, {
|
||||
cache: "no-store",
|
||||
signal: controller.signal,
|
||||
});
|
||||
const json = await res.json();
|
||||
if (!alive) return;
|
||||
if (!res.ok || json?.ok === false) {
|
||||
setError(json?.error ?? t("reports.error.failed"));
|
||||
setReport(null);
|
||||
} else {
|
||||
setReport(json);
|
||||
}
|
||||
} catch {
|
||||
if (!alive) return;
|
||||
setError(t("reports.error.network"));
|
||||
setReport(null);
|
||||
} finally {
|
||||
if (alive) setLoading(false);
|
||||
}
|
||||
}
|
||||
|
||||
load();
|
||||
return () => {
|
||||
alive = false;
|
||||
controller.abort();
|
||||
};
|
||||
}, [range, machineId, workOrderId, sku, t]);
|
||||
|
||||
useEffect(() => {
|
||||
let alive = true;
|
||||
const controller = new AbortController();
|
||||
|
||||
async function loadFilters() {
|
||||
try {
|
||||
const params = new URLSearchParams({ range });
|
||||
if (machineId) params.set("machineId", machineId);
|
||||
const res = await fetch(`/api/reports/filters?${params.toString()}`, {
|
||||
cache: "no-store",
|
||||
signal: controller.signal,
|
||||
});
|
||||
const json = await res.json();
|
||||
if (!alive) return;
|
||||
if (!res.ok || json?.ok === false) {
|
||||
setFilterOptions({ workOrders: [], skus: [] });
|
||||
} else {
|
||||
setFilterOptions({
|
||||
workOrders: json.workOrders ?? [],
|
||||
skus: json.skus ?? [],
|
||||
});
|
||||
}
|
||||
} catch {
|
||||
if (!alive) return;
|
||||
setFilterOptions({ workOrders: [], skus: [] });
|
||||
}
|
||||
}
|
||||
|
||||
loadFilters();
|
||||
return () => {
|
||||
alive = false;
|
||||
controller.abort();
|
||||
};
|
||||
}, [range, machineId]);
|
||||
|
||||
const summary = report?.summary;
|
||||
const downtime = report?.downtime;
|
||||
const trend = report?.trend;
|
||||
const distribution = report?.distribution;
|
||||
|
||||
const oeeSeries = useMemo(() => {
|
||||
const rows = trend?.oee ?? [];
|
||||
const trimmed = downsample(rows, 600);
|
||||
return trimmed.map((p) => ({
|
||||
ts: p.t,
|
||||
label: formatTickLabel(p.t, range),
|
||||
value: p.v,
|
||||
}));
|
||||
}, [trend?.oee, range]);
|
||||
|
||||
const scrapSeries = useMemo(() => {
|
||||
const rows = trend?.scrapRate ?? [];
|
||||
const trimmed = downsample(rows, 600);
|
||||
return trimmed.map((p) => ({
|
||||
ts: p.t,
|
||||
label: formatTickLabel(p.t, range),
|
||||
value: p.v,
|
||||
}));
|
||||
}, [trend?.scrapRate, range]);
|
||||
|
||||
const cycleHistogram = useMemo(() => {
|
||||
return distribution?.cycleTime ?? [];
|
||||
}, [distribution?.cycleTime]);
|
||||
|
||||
const downtimeSeries = useMemo(() => {
|
||||
if (!downtime) return [];
|
||||
return [
|
||||
{ name: "Macrostop", value: Math.round(downtime.macrostopSec / 60) },
|
||||
{ name: "Microstop", value: Math.round(downtime.microstopSec / 60) },
|
||||
];
|
||||
}, [downtime]);
|
||||
|
||||
const downtimeColors: Record<string, string> = {
|
||||
Macrostop: "#FF3B5C",
|
||||
Microstop: "#FF7A00",
|
||||
};
|
||||
|
||||
const machineLabel = useMemo(() => {
|
||||
if (!machineId) return t("reports.filter.allMachines");
|
||||
return machines.find((m) => m.id === machineId)?.name ?? machineId;
|
||||
}, [machineId, machines, t]);
|
||||
|
||||
const workOrderLabel = workOrderId || t("reports.filter.allWorkOrders");
|
||||
const skuLabel = sku || t("reports.filter.allSkus");
|
||||
|
||||
const handleExportCsv = () => {
|
||||
if (!report) return;
|
||||
const csv = buildCsv(report, t);
|
||||
downloadText("reports.csv", csv);
|
||||
};
|
||||
|
||||
const handleExportPdf = () => {
|
||||
if (!report) return;
|
||||
const html = buildPdfHtml(
|
||||
report,
|
||||
rangeLabel,
|
||||
{
|
||||
machine: machineLabel,
|
||||
workOrder: workOrderLabel,
|
||||
sku: skuLabel,
|
||||
},
|
||||
t
|
||||
);
|
||||
|
||||
const win = window.open("", "_blank", "width=900,height=650");
|
||||
if (!win) return;
|
||||
win.document.open();
|
||||
win.document.write(html);
|
||||
win.document.close();
|
||||
win.focus();
|
||||
setTimeout(() => win.print(), 300);
|
||||
};
|
||||
|
||||
return (
|
||||
<div className="p-4 sm:p-6">
|
||||
<div className="mb-6 flex flex-col gap-4 sm:flex-row sm:items-start sm:justify-between">
|
||||
<div>
|
||||
<h1 className="text-2xl font-semibold text-white">{t("reports.title")}</h1>
|
||||
<p className="text-sm text-zinc-400">{t("reports.subtitle")}</p>
|
||||
</div>
|
||||
|
||||
<div className="flex w-full flex-wrap items-center gap-2 sm:w-auto">
|
||||
<button
|
||||
onClick={handleExportCsv}
|
||||
className="w-full rounded-xl border border-white/10 bg-white/5 px-4 py-2 text-sm text-white hover:bg-white/10 sm:w-auto"
|
||||
>
|
||||
{t("reports.exportCsv")}
|
||||
</button>
|
||||
<button
|
||||
onClick={handleExportPdf}
|
||||
className="w-full rounded-xl border border-white/10 bg-white/5 px-4 py-2 text-sm text-white hover:bg-white/10 sm:w-auto"
|
||||
>
|
||||
{t("reports.exportPdf")}
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="rounded-2xl border border-white/10 bg-white/5 p-5">
|
||||
<div className="flex flex-wrap items-center justify-between gap-4">
|
||||
<div className="text-sm font-semibold text-white">{t("reports.filters")}</div>
|
||||
<div className="text-xs text-zinc-400">{rangeLabel}</div>
|
||||
</div>
|
||||
|
||||
<div className="mt-4 grid grid-cols-1 gap-3 md:grid-cols-2 xl:grid-cols-4">
|
||||
<div className="rounded-xl border border-white/10 bg-black/20 p-3">
|
||||
<div className="text-[11px] text-zinc-400">{t("reports.filter.range")}</div>
|
||||
<div className="mt-2 flex flex-wrap gap-2">
|
||||
{(["24h", "7d", "30d", "custom"] as RangeKey[]).map((k) => (
|
||||
<button
|
||||
key={k}
|
||||
onClick={() => setRange(k)}
|
||||
className={`rounded-full border px-3 py-1 text-xs ${
|
||||
range === k
|
||||
? "border-emerald-500/30 bg-emerald-500/15 text-emerald-200"
|
||||
: "border-white/10 bg-white/5 text-zinc-300 hover:bg-white/10"
|
||||
}`}
|
||||
>
|
||||
{k.toUpperCase()}
|
||||
</button>
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="rounded-xl border border-white/10 bg-black/20 p-3">
|
||||
<div className="text-[11px] text-zinc-400">{t("reports.filter.machine")}</div>
|
||||
<select
|
||||
value={machineId}
|
||||
onChange={(e) => setMachineId(e.target.value)}
|
||||
className="mt-2 w-full rounded-lg border border-white/10 bg-white/5 px-3 py-2 text-sm text-zinc-300"
|
||||
>
|
||||
<option value="">{t("reports.filter.allMachines")}</option>
|
||||
{machines.map((m) => (
|
||||
<option key={m.id} value={m.id}>
|
||||
{m.name}
|
||||
</option>
|
||||
))}
|
||||
</select>
|
||||
</div>
|
||||
|
||||
<div className="rounded-xl border border-white/10 bg-black/20 p-3">
|
||||
<div className="text-[11px] text-zinc-400">{t("reports.filter.workOrder")}</div>
|
||||
<input
|
||||
list="work-order-list"
|
||||
value={workOrderId}
|
||||
onChange={(e) => setWorkOrderId(e.target.value)}
|
||||
placeholder={t("reports.filter.allWorkOrders")}
|
||||
className="mt-2 w-full rounded-lg border border-white/10 bg-white/5 px-3 py-2 text-sm text-zinc-300 placeholder:text-zinc-500"
|
||||
/>
|
||||
<datalist id="work-order-list">
|
||||
{filterOptions.workOrders.map((wo) => (
|
||||
<option key={wo} value={wo} />
|
||||
))}
|
||||
</datalist>
|
||||
</div>
|
||||
|
||||
<div className="rounded-xl border border-white/10 bg-black/20 p-3">
|
||||
<div className="text-[11px] text-zinc-400">{t("reports.filter.sku")}</div>
|
||||
<input
|
||||
list="sku-list"
|
||||
value={sku}
|
||||
onChange={(e) => setSku(e.target.value)}
|
||||
placeholder={t("reports.filter.allSkus")}
|
||||
className="mt-2 w-full rounded-lg border border-white/10 bg-white/5 px-3 py-2 text-sm text-zinc-300 placeholder:text-zinc-500"
|
||||
/>
|
||||
<datalist id="sku-list">
|
||||
{filterOptions.skus.map((s) => (
|
||||
<option key={s} value={s} />
|
||||
))}
|
||||
</datalist>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="mt-4">
|
||||
{loading && <div className="text-sm text-zinc-400">{t("reports.loading")}</div>}
|
||||
{error && !loading && (
|
||||
<div className="rounded-2xl border border-red-500/20 bg-red-500/10 p-4 text-sm text-red-200">
|
||||
{error}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
|
||||
<div className="mt-4 grid grid-cols-1 gap-4 md:grid-cols-2 xl:grid-cols-4">
|
||||
{[
|
||||
{ label: "OEE", value: fmtPct(summary?.oeeAvg), tone: "text-emerald-300" },
|
||||
{ label: "Availability", value: fmtPct(summary?.availabilityAvg), tone: "text-white" },
|
||||
{ label: "Performance", value: fmtPct(summary?.performanceAvg), tone: "text-white" },
|
||||
{ label: "Quality", value: fmtPct(summary?.qualityAvg), tone: "text-white" },
|
||||
].map((kpi) => (
|
||||
<div key={kpi.label} className="rounded-2xl border border-white/10 bg-white/5 p-5">
|
||||
<div className="text-xs text-zinc-400">{kpi.label} (avg)</div>
|
||||
<div className={`mt-2 text-3xl font-semibold ${kpi.tone}`}>{kpi.value}</div>
|
||||
<div className="mt-2 text-xs text-zinc-500">
|
||||
{summary ? t("reports.kpi.note.withData") : t("reports.kpi.note.noData")}
|
||||
</div>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
|
||||
<div className="mt-6 grid grid-cols-1 gap-4 xl:grid-cols-2">
|
||||
<div className="rounded-2xl border border-white/10 bg-white/5 p-5">
|
||||
<div className="mb-2 text-sm font-semibold text-white">{t("reports.oeeTrend")}</div>
|
||||
<div className="h-[260px] rounded-2xl border border-white/10 bg-black/25 p-4">
|
||||
{oeeSeries.length ? (
|
||||
<ResponsiveContainer width="100%" height="100%">
|
||||
<LineChart data={oeeSeries}>
|
||||
<CartesianGrid strokeDasharray="3 3" stroke="var(--app-chart-grid)" />
|
||||
<XAxis dataKey="label" tick={{ fill: "var(--app-chart-tick)" }} />
|
||||
<YAxis domain={[0, 100]} tick={{ fill: "var(--app-chart-tick)" }} />
|
||||
<Tooltip
|
||||
contentStyle={{
|
||||
background: "var(--app-chart-tooltip-bg)",
|
||||
border: "1px solid var(--app-chart-tooltip-border)",
|
||||
}}
|
||||
labelStyle={{ color: "var(--app-chart-label)" }}
|
||||
labelFormatter={(_, payload) => {
|
||||
const row = payload?.[0]?.payload;
|
||||
return row?.ts ? new Date(row.ts).toLocaleString(locale) : "";
|
||||
}}
|
||||
formatter={(val: number | string | undefined) => [
|
||||
val == null ? "--" : `${Number(val).toFixed(1)}%`,
|
||||
"OEE",
|
||||
]}
|
||||
/>
|
||||
<Line type="monotone" dataKey="value" stroke="#34d399" dot={false} strokeWidth={2} />
|
||||
</LineChart>
|
||||
</ResponsiveContainer>
|
||||
) : (
|
||||
<div className="flex h-full items-center justify-center text-sm text-zinc-500">
|
||||
{t("reports.noTrend")}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="rounded-2xl border border-white/10 bg-white/5 p-5">
|
||||
<div className="mb-2 text-sm font-semibold text-white">{t("reports.downtimePareto")}</div>
|
||||
<div className="h-[260px] rounded-2xl border border-white/10 bg-black/25 p-4">
|
||||
{downtimeSeries.length ? (
|
||||
<ResponsiveContainer width="100%" height="100%">
|
||||
<BarChart data={downtimeSeries}>
|
||||
<CartesianGrid strokeDasharray="3 3" stroke="var(--app-chart-grid)" />
|
||||
<XAxis dataKey="name" tick={{ fill: "var(--app-chart-tick)" }} />
|
||||
<YAxis tick={{ fill: "var(--app-chart-tick)" }} />
|
||||
<Tooltip content={<DowntimeTooltip t={t} />} />
|
||||
<Bar dataKey="value" radius={[10, 10, 0, 0]} isAnimationActive={false}>
|
||||
{downtimeSeries.map((row, idx) => (
|
||||
<Cell key={`${row.name}-${idx}`} fill={downtimeColors[row.name] ?? "#94a3b8"} />
|
||||
))}
|
||||
</Bar>
|
||||
</BarChart>
|
||||
</ResponsiveContainer>
|
||||
) : (
|
||||
<div className="flex h-full items-center justify-center text-sm text-zinc-500">
|
||||
{t("reports.noTrend")}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="mt-6 grid grid-cols-1 gap-4 xl:grid-cols-3">
|
||||
<div className="rounded-2xl border border-white/10 bg-white/5 p-5">
|
||||
<div className="mb-2 text-sm font-semibold text-white">{t("reports.cycleDistribution")}</div>
|
||||
<div className="h-[220px] rounded-2xl border border-white/10 bg-black/25 p-4">
|
||||
{cycleHistogram.length ? (
|
||||
<ResponsiveContainer width="100%" height="100%">
|
||||
<BarChart data={cycleHistogram}>
|
||||
<CartesianGrid strokeDasharray="3 3" stroke="var(--app-chart-grid)" />
|
||||
<XAxis dataKey="label" tick={{ fill: "var(--app-chart-tick)", fontSize: 10 }} />
|
||||
<YAxis tick={{ fill: "var(--app-chart-tick)" }} />
|
||||
<Tooltip content={<CycleTooltip t={t} />} />
|
||||
<Bar dataKey="count" radius={[8, 8, 0, 0]} fill="#60a5fa" isAnimationActive={false} />
|
||||
</BarChart>
|
||||
</ResponsiveContainer>
|
||||
) : (
|
||||
<div className="flex h-full items-center justify-center text-sm text-zinc-500">
|
||||
{t("reports.noCycle")}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="rounded-2xl border border-white/10 bg-white/5 p-5">
|
||||
<div className="mb-2 text-sm font-semibold text-white">{t("reports.scrapTrend")}</div>
|
||||
<div className="h-[220px] rounded-2xl border border-white/10 bg-black/25 p-4">
|
||||
{scrapSeries.length ? (
|
||||
<ResponsiveContainer width="100%" height="100%">
|
||||
<LineChart data={scrapSeries}>
|
||||
<CartesianGrid strokeDasharray="3 3" stroke="var(--app-chart-grid)" />
|
||||
<XAxis dataKey="label" tick={{ fill: "var(--app-chart-tick)" }} />
|
||||
<YAxis domain={[0, 100]} tick={{ fill: "var(--app-chart-tick)" }} />
|
||||
<Tooltip
|
||||
contentStyle={{
|
||||
background: "var(--app-chart-tooltip-bg)",
|
||||
border: "1px solid var(--app-chart-tooltip-border)",
|
||||
}}
|
||||
labelStyle={{ color: "var(--app-chart-label)" }}
|
||||
labelFormatter={(_, payload) => {
|
||||
const row = payload?.[0]?.payload;
|
||||
return row?.ts ? new Date(row.ts).toLocaleString(locale) : "";
|
||||
}}
|
||||
formatter={(val: number | string | undefined) => [
|
||||
val == null ? "--" : `${Number(val).toFixed(1)}%`,
|
||||
t("reports.scrapRate"),
|
||||
]}
|
||||
/>
|
||||
<Line type="monotone" dataKey="value" stroke="#f97316" dot={false} strokeWidth={2} />
|
||||
</LineChart>
|
||||
</ResponsiveContainer>
|
||||
) : (
|
||||
<div className="flex h-full items-center justify-center text-sm text-zinc-500">
|
||||
{t("reports.noDowntime")}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="rounded-2xl border border-white/10 bg-white/5 p-5">
|
||||
<div className="mb-2 text-sm font-semibold text-white">{t("reports.topLossDrivers")}</div>
|
||||
<div className="space-y-3 text-sm text-zinc-300">
|
||||
{[
|
||||
{ label: t("reports.loss.macrostop"), value: fmtDuration(downtime?.macrostopSec) },
|
||||
{ label: t("reports.loss.microstop"), value: fmtDuration(downtime?.microstopSec) },
|
||||
{ label: t("reports.loss.slowCycle"), value: downtime ? `${downtime.slowCycleCount}` : "--" },
|
||||
{ label: t("reports.loss.qualitySpike"), value: downtime ? `${downtime.qualitySpikeCount}` : "--" },
|
||||
{ label: t("reports.loss.oeeDrop"), value: downtime ? `${downtime.oeeDropCount}` : "--" },
|
||||
{
|
||||
label: t("reports.loss.perfDegradation"),
|
||||
value: downtime ? `${downtime.performanceDegradationCount}` : "--",
|
||||
},
|
||||
].map((row) => (
|
||||
<div key={row.label} className="flex items-center justify-between rounded-xl border border-white/10 bg-black/20 p-3">
|
||||
<span>{row.label}</span>
|
||||
<span className="text-xs text-zinc-400">{row.value}</span>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="mt-6 grid grid-cols-1 gap-4 xl:grid-cols-2">
|
||||
<div className="rounded-2xl border border-white/10 bg-white/5 p-5">
|
||||
<div className="mb-3 text-sm font-semibold text-white">{t("reports.qualitySummary")}</div>
|
||||
<div className="space-y-3 text-sm text-zinc-300">
|
||||
<div className="rounded-xl border border-white/10 bg-black/20 p-3">
|
||||
<div className="text-xs text-zinc-400">{t("reports.scrapRate")}</div>
|
||||
<div className="mt-1 text-lg font-semibold text-white">
|
||||
{summary?.scrapRate != null ? fmtPct(summary.scrapRate) : "--"}
|
||||
</div>
|
||||
</div>
|
||||
<div className="rounded-xl border border-white/10 bg-black/20 p-3">
|
||||
<div className="text-xs text-zinc-400">{t("reports.topScrapSku")}</div>
|
||||
<div className="mt-1 text-sm text-zinc-300">{summary?.topScrapSku ?? "--"}</div>
|
||||
</div>
|
||||
<div className="rounded-xl border border-white/10 bg-black/20 p-3">
|
||||
<div className="text-xs text-zinc-400">{t("reports.topScrapWorkOrder")}</div>
|
||||
<div className="mt-1 text-sm text-zinc-300">{summary?.topScrapWorkOrder ?? "--"}</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="rounded-2xl border border-white/10 bg-white/5 p-5">
|
||||
<div className="mb-3 text-sm font-semibold text-white">{t("reports.notes")}</div>
|
||||
<div className="rounded-xl border border-white/10 bg-black/20 p-4 text-sm text-zinc-300">
|
||||
<div className="mb-2 text-xs text-zinc-400">{t("reports.notes.suggested")}</div>
|
||||
{report?.insights && report.insights.length > 0 ? (
|
||||
<div className="space-y-2">
|
||||
{report.insights.map((note, idx) => (
|
||||
<div key={idx}>{note}</div>
|
||||
))}
|
||||
</div>
|
||||
) : (
|
||||
<div>{t("reports.notes.none")}</div>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
1178
app/(app)/settings/page.tsx
Normal file
1178
app/(app)/settings/page.tsx
Normal file
File diff suppressed because it is too large
Load Diff
89
app/api/alerts/contacts/[id]/route.ts
Normal file
89
app/api/alerts/contacts/[id]/route.ts
Normal file
@@ -0,0 +1,89 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import { z } from "zod";
|
||||
import { Prisma } from "@prisma/client";
|
||||
import { prisma } from "@/lib/prisma";
|
||||
import { requireSession } from "@/lib/auth/requireSession";
|
||||
|
||||
const roleScopeSchema = z.preprocess(
|
||||
(value) => (typeof value === "string" ? value.trim().toUpperCase() : value),
|
||||
z.enum(["MEMBER", "ADMIN", "OWNER", "CUSTOM"])
|
||||
);
|
||||
|
||||
const contactPatchSchema = z.object({
|
||||
name: z.string().trim().min(1).max(120).optional(),
|
||||
roleScope: roleScopeSchema.optional(),
|
||||
email: z.string().trim().email().optional().nullable(),
|
||||
phone: z.string().trim().min(6).max(40).optional().nullable(),
|
||||
userId: z.string().uuid().optional().nullable(),
|
||||
eventTypes: z.array(z.string().trim().min(1)).optional().nullable(),
|
||||
isActive: z.boolean().optional(),
|
||||
});
|
||||
|
||||
function canManageAlerts(role?: string | null) {
|
||||
return role === "OWNER";
|
||||
}
|
||||
|
||||
export async function PATCH(req: Request, { params }: { params: Promise<{ id: string }> }) {
|
||||
const session = await requireSession();
|
||||
if (!session) return NextResponse.json({ ok: false, error: "Unauthorized" }, { status: 401 });
|
||||
|
||||
const membership = await prisma.orgUser.findUnique({
|
||||
where: { orgId_userId: { orgId: session.orgId, userId: session.userId } },
|
||||
select: { role: true },
|
||||
});
|
||||
if (!canManageAlerts(membership?.role)) {
|
||||
return NextResponse.json({ ok: false, error: "Forbidden" }, { status: 403 });
|
||||
}
|
||||
|
||||
const body = await req.json().catch(() => ({}));
|
||||
const parsed = contactPatchSchema.safeParse(body);
|
||||
if (!parsed.success) {
|
||||
return NextResponse.json({ ok: false, error: "Invalid contact payload" }, { status: 400 });
|
||||
}
|
||||
|
||||
const { id } = await params;
|
||||
const existing = await prisma.alertContact.findFirst({
|
||||
where: { id, orgId: session.orgId },
|
||||
});
|
||||
if (!existing) {
|
||||
return NextResponse.json({ ok: false, error: "Not found" }, { status: 404 });
|
||||
}
|
||||
|
||||
const { userId: _userId, eventTypes, ...updateData } = parsed.data;
|
||||
void _userId;
|
||||
const normalizedEventTypes =
|
||||
eventTypes === null ? Prisma.DbNull : eventTypes ?? undefined;
|
||||
const data = normalizedEventTypes === undefined
|
||||
? updateData
|
||||
: { ...updateData, eventTypes: normalizedEventTypes };
|
||||
const updated = await prisma.alertContact.update({
|
||||
where: { id },
|
||||
data,
|
||||
});
|
||||
|
||||
return NextResponse.json({ ok: true, contact: updated });
|
||||
}
|
||||
|
||||
export async function DELETE(_req: Request, { params }: { params: Promise<{ id: string }> }) {
|
||||
const session = await requireSession();
|
||||
if (!session) return NextResponse.json({ ok: false, error: "Unauthorized" }, { status: 401 });
|
||||
|
||||
const membership = await prisma.orgUser.findUnique({
|
||||
where: { orgId_userId: { orgId: session.orgId, userId: session.userId } },
|
||||
select: { role: true },
|
||||
});
|
||||
if (!canManageAlerts(membership?.role)) {
|
||||
return NextResponse.json({ ok: false, error: "Forbidden" }, { status: 403 });
|
||||
}
|
||||
|
||||
const { id } = await params;
|
||||
const existing = await prisma.alertContact.findFirst({
|
||||
where: { id, orgId: session.orgId },
|
||||
});
|
||||
if (!existing) {
|
||||
return NextResponse.json({ ok: false, error: "Not found" }, { status: 404 });
|
||||
}
|
||||
|
||||
await prisma.alertContact.delete({ where: { id } });
|
||||
return NextResponse.json({ ok: true });
|
||||
}
|
||||
77
app/api/alerts/contacts/route.ts
Normal file
77
app/api/alerts/contacts/route.ts
Normal file
@@ -0,0 +1,77 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import { z } from "zod";
|
||||
import { Prisma } from "@prisma/client";
|
||||
import { prisma } from "@/lib/prisma";
|
||||
import { requireSession } from "@/lib/auth/requireSession";
|
||||
|
||||
const roleScopeSchema = z.preprocess(
|
||||
(value) => (typeof value === "string" ? value.trim().toUpperCase() : value),
|
||||
z.enum(["MEMBER", "ADMIN", "OWNER", "CUSTOM"])
|
||||
);
|
||||
|
||||
const contactSchema = z.object({
|
||||
name: z.string().trim().min(1).max(120),
|
||||
roleScope: roleScopeSchema,
|
||||
email: z.string().trim().email().optional().nullable(),
|
||||
phone: z.string().trim().min(6).max(40).optional().nullable(),
|
||||
userId: z.string().uuid().optional().nullable(),
|
||||
eventTypes: z.array(z.string().trim().min(1)).optional().nullable(),
|
||||
});
|
||||
|
||||
function canManageAlerts(role?: string | null) {
|
||||
return role === "OWNER";
|
||||
}
|
||||
|
||||
export async function GET() {
|
||||
const session = await requireSession();
|
||||
if (!session) return NextResponse.json({ ok: false, error: "Unauthorized" }, { status: 401 });
|
||||
|
||||
const contacts = await prisma.alertContact.findMany({
|
||||
where: { orgId: session.orgId },
|
||||
orderBy: { createdAt: "asc" },
|
||||
});
|
||||
|
||||
return NextResponse.json({ ok: true, contacts });
|
||||
}
|
||||
|
||||
export async function POST(req: Request) {
|
||||
const session = await requireSession();
|
||||
if (!session) return NextResponse.json({ ok: false, error: "Unauthorized" }, { status: 401 });
|
||||
|
||||
const membership = await prisma.orgUser.findUnique({
|
||||
where: { orgId_userId: { orgId: session.orgId, userId: session.userId } },
|
||||
select: { role: true },
|
||||
});
|
||||
if (!canManageAlerts(membership?.role)) {
|
||||
return NextResponse.json({ ok: false, error: "Forbidden" }, { status: 403 });
|
||||
}
|
||||
|
||||
const body = await req.json().catch(() => ({}));
|
||||
const parsed = contactSchema.safeParse(body);
|
||||
if (!parsed.success) {
|
||||
return NextResponse.json({ ok: false, error: "Invalid contact payload" }, { status: 400 });
|
||||
}
|
||||
|
||||
const data = parsed.data;
|
||||
const hasChannel = !!(data.email || data.phone);
|
||||
if (!data.userId && !hasChannel) {
|
||||
return NextResponse.json({ ok: false, error: "email or phone required for external contact" }, { status: 400 });
|
||||
}
|
||||
|
||||
const eventTypes =
|
||||
data.eventTypes === null ? Prisma.DbNull : data.eventTypes ?? undefined;
|
||||
|
||||
const contact = await prisma.alertContact.create({
|
||||
data: {
|
||||
orgId: session.orgId,
|
||||
userId: data.userId ?? null,
|
||||
name: data.name,
|
||||
roleScope: data.roleScope,
|
||||
email: data.email ?? null,
|
||||
phone: data.phone ?? null,
|
||||
eventTypes,
|
||||
},
|
||||
});
|
||||
|
||||
return NextResponse.json({ ok: true, contact });
|
||||
}
|
||||
48
app/api/alerts/inbox/route.ts
Normal file
48
app/api/alerts/inbox/route.ts
Normal file
@@ -0,0 +1,48 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import type { NextRequest } from "next/server";
|
||||
import { requireSession } from "@/lib/auth/requireSession";
|
||||
import { getAlertsInboxData } from "@/lib/alerts/getAlertsInboxData";
|
||||
|
||||
function parseDate(input?: string | null) {
|
||||
if (!input) return null;
|
||||
const n = Number(input);
|
||||
if (!Number.isNaN(n)) return new Date(n);
|
||||
const d = new Date(input);
|
||||
return Number.isNaN(d.getTime()) ? null : d;
|
||||
}
|
||||
|
||||
export async function GET(req: NextRequest) {
|
||||
const session = await requireSession();
|
||||
if (!session) return NextResponse.json({ ok: false, error: "Unauthorized" }, { status: 401 });
|
||||
|
||||
const url = new URL(req.url);
|
||||
const range = url.searchParams.get("range") ?? "24h";
|
||||
const machineId = url.searchParams.get("machineId") ?? undefined;
|
||||
const location = url.searchParams.get("location") ?? undefined;
|
||||
const eventType = url.searchParams.get("eventType") ?? undefined;
|
||||
const severity = url.searchParams.get("severity") ?? undefined;
|
||||
const status = url.searchParams.get("status") ?? undefined;
|
||||
const shift = url.searchParams.get("shift") ?? undefined;
|
||||
const includeUpdates = url.searchParams.get("includeUpdates") === "1";
|
||||
const limitRaw = Number(url.searchParams.get("limit") ?? "200");
|
||||
const limit = Number.isFinite(limitRaw) ? Math.min(Math.max(limitRaw, 1), 500) : 200;
|
||||
const start = parseDate(url.searchParams.get("start"));
|
||||
const end = parseDate(url.searchParams.get("end"));
|
||||
|
||||
const result = await getAlertsInboxData({
|
||||
orgId: session.orgId,
|
||||
range,
|
||||
start,
|
||||
end,
|
||||
machineId,
|
||||
location,
|
||||
eventType,
|
||||
severity,
|
||||
status,
|
||||
shift,
|
||||
includeUpdates,
|
||||
limit,
|
||||
});
|
||||
|
||||
return NextResponse.json({ ok: true, range: result.range, events: result.events });
|
||||
}
|
||||
23
app/api/alerts/notifications/route.ts
Normal file
23
app/api/alerts/notifications/route.ts
Normal file
@@ -0,0 +1,23 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import { prisma } from "@/lib/prisma";
|
||||
import { requireSession } from "@/lib/auth/requireSession";
|
||||
|
||||
export async function GET(req: Request) {
|
||||
const session = await requireSession();
|
||||
if (!session) return NextResponse.json({ ok: false, error: "Unauthorized" }, { status: 401 });
|
||||
|
||||
const url = new URL(req.url);
|
||||
const machineId = url.searchParams.get("machineId") || undefined;
|
||||
const limit = Math.min(Number(url.searchParams.get("limit") ?? "50"), 200);
|
||||
|
||||
const notifications = await prisma.alertNotification.findMany({
|
||||
where: {
|
||||
orgId: session.orgId,
|
||||
...(machineId ? { machineId } : {}),
|
||||
},
|
||||
orderBy: { sentAt: "desc" },
|
||||
take: Number.isFinite(limit) ? limit : 50,
|
||||
});
|
||||
|
||||
return NextResponse.json({ ok: true, notifications });
|
||||
}
|
||||
55
app/api/alerts/policy/route.ts
Normal file
55
app/api/alerts/policy/route.ts
Normal file
@@ -0,0 +1,55 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import { prisma } from "@/lib/prisma";
|
||||
import { requireSession } from "@/lib/auth/requireSession";
|
||||
import { AlertPolicySchema, DEFAULT_POLICY } from "@/lib/alerts/policy";
|
||||
|
||||
function canManageAlerts(role?: string | null) {
|
||||
return role === "OWNER";
|
||||
}
|
||||
|
||||
export async function GET() {
|
||||
const session = await requireSession();
|
||||
if (!session) return NextResponse.json({ ok: false, error: "Unauthorized" }, { status: 401 });
|
||||
|
||||
let policy = await prisma.alertPolicy.findUnique({
|
||||
where: { orgId: session.orgId },
|
||||
select: { policyJson: true },
|
||||
});
|
||||
|
||||
if (!policy) {
|
||||
await prisma.alertPolicy.create({
|
||||
data: { orgId: session.orgId, policyJson: DEFAULT_POLICY },
|
||||
});
|
||||
policy = { policyJson: DEFAULT_POLICY };
|
||||
}
|
||||
|
||||
const parsed = AlertPolicySchema.safeParse(policy.policyJson);
|
||||
return NextResponse.json({ ok: true, policy: parsed.success ? parsed.data : DEFAULT_POLICY });
|
||||
}
|
||||
|
||||
export async function PUT(req: Request) {
|
||||
const session = await requireSession();
|
||||
if (!session) return NextResponse.json({ ok: false, error: "Unauthorized" }, { status: 401 });
|
||||
|
||||
const membership = await prisma.orgUser.findUnique({
|
||||
where: { orgId_userId: { orgId: session.orgId, userId: session.userId } },
|
||||
select: { role: true },
|
||||
});
|
||||
if (!canManageAlerts(membership?.role)) {
|
||||
return NextResponse.json({ ok: false, error: "Forbidden" }, { status: 403 });
|
||||
}
|
||||
|
||||
const body = await req.json().catch(() => ({}));
|
||||
const parsed = AlertPolicySchema.safeParse(body?.policy ?? body);
|
||||
if (!parsed.success) {
|
||||
return NextResponse.json({ ok: false, error: "Invalid policy payload" }, { status: 400 });
|
||||
}
|
||||
|
||||
await prisma.alertPolicy.upsert({
|
||||
where: { orgId: session.orgId },
|
||||
create: { orgId: session.orgId, policyJson: parsed.data, updatedBy: session.userId },
|
||||
update: { policyJson: parsed.data, updatedBy: session.userId },
|
||||
});
|
||||
|
||||
return NextResponse.json({ ok: true });
|
||||
}
|
||||
64
app/api/analytics/coverage/route.ts
Normal file
64
app/api/analytics/coverage/route.ts
Normal file
@@ -0,0 +1,64 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import { prisma } from "@/lib/prisma";
|
||||
import { requireSession } from "@/lib/auth/requireSession";
|
||||
import { coerceDowntimeRange, rangeToStart } from "@/lib/analytics/downtimeRange";
|
||||
|
||||
const bad = (status: number, error: string) =>
|
||||
NextResponse.json({ ok: false, error }, { status });
|
||||
|
||||
export async function GET(req: Request) {
|
||||
const session = await requireSession();
|
||||
if (!session) return bad(401, "Unauthorized");
|
||||
const orgId = session.orgId;
|
||||
|
||||
const url = new URL(req.url);
|
||||
|
||||
// ✅ Parse params INSIDE handler
|
||||
const range = coerceDowntimeRange(url.searchParams.get("range"));
|
||||
const start = rangeToStart(range);
|
||||
|
||||
const machineId = url.searchParams.get("machineId"); // optional
|
||||
const kind = (url.searchParams.get("kind") || "downtime").toLowerCase();
|
||||
|
||||
// coverage is only meaningful for downtime
|
||||
if (kind !== "downtime") return bad(400, "Invalid kind (downtime only)");
|
||||
|
||||
let resolvedMachineId: string | null = null;
|
||||
|
||||
// If machineId provided, validate ownership
|
||||
if (machineId) {
|
||||
const m = await prisma.machine.findFirst({
|
||||
where: { id: machineId, orgId },
|
||||
select: { id: true },
|
||||
});
|
||||
if (!m) return bad(404, "Machine not found");
|
||||
resolvedMachineId = m.id;
|
||||
}
|
||||
|
||||
const rows = await prisma.reasonEntry.findMany({
|
||||
where: {
|
||||
orgId,
|
||||
...(resolvedMachineId ? { machineId: resolvedMachineId } : {}),
|
||||
kind: "downtime",
|
||||
capturedAt: { gte: start },
|
||||
},
|
||||
select: { durationSeconds: true, episodeId: true },
|
||||
});
|
||||
|
||||
const receivedEpisodes = new Set(rows.map((r) => r.episodeId).filter(Boolean)).size;
|
||||
|
||||
const receivedMinutes =
|
||||
Math.round((rows.reduce((acc, r) => acc + (r.durationSeconds ?? 0), 0) / 60) * 10) / 10;
|
||||
|
||||
return NextResponse.json({
|
||||
ok: true,
|
||||
orgId,
|
||||
machineId: resolvedMachineId, // null => org-wide
|
||||
range,
|
||||
start,
|
||||
receivedEpisodes,
|
||||
receivedMinutes,
|
||||
note:
|
||||
"Control Tower received coverage (sync health). True coverage vs total downtime minutes can be added once CT has total downtime minutes per window.",
|
||||
});
|
||||
}
|
||||
130
app/api/analytics/downtime-events/route.ts
Normal file
130
app/api/analytics/downtime-events/route.ts
Normal file
@@ -0,0 +1,130 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import { prisma } from "@/lib/prisma";
|
||||
import { requireSession } from "@/lib/auth/requireSession";
|
||||
import { coerceDowntimeRange, rangeToStart } from "@/lib/analytics/downtimeRange";
|
||||
|
||||
const bad = (status: number, error: string) =>
|
||||
NextResponse.json({ ok: false, error }, { status });
|
||||
|
||||
function toISO(d: Date | null | undefined) {
|
||||
return d ? d.toISOString() : null;
|
||||
}
|
||||
|
||||
export async function GET(req: Request) {
|
||||
// ✅ Session auth (cookie)
|
||||
const session = await requireSession();
|
||||
if (!session) return bad(401, "Unauthorized");
|
||||
const orgId = session.orgId;
|
||||
|
||||
const url = new URL(req.url);
|
||||
|
||||
// ✅ Params
|
||||
const range = coerceDowntimeRange(url.searchParams.get("range"));
|
||||
const start = rangeToStart(range);
|
||||
|
||||
const machineId = url.searchParams.get("machineId"); // optional
|
||||
const reasonCode = url.searchParams.get("reasonCode"); // optional
|
||||
|
||||
const limitRaw = url.searchParams.get("limit");
|
||||
const limit = Math.min(Math.max(Number(limitRaw || 200), 1), 500);
|
||||
|
||||
// Optional pagination: return events before this timestamp (capturedAt)
|
||||
const before = url.searchParams.get("before"); // ISO string
|
||||
const beforeDate = before ? new Date(before) : null;
|
||||
if (before && isNaN(beforeDate!.getTime())) return bad(400, "Invalid before timestamp");
|
||||
|
||||
// ✅ If machineId provided, verify it belongs to this org
|
||||
if (machineId) {
|
||||
const m = await prisma.machine.findFirst({
|
||||
where: { id: machineId, orgId },
|
||||
select: { id: true },
|
||||
});
|
||||
if (!m) return bad(404, "Machine not found");
|
||||
}
|
||||
|
||||
// ✅ Query ReasonEntry as the "episode" table for downtime
|
||||
// We only return rows that have an episodeId (true downtime episodes)
|
||||
const where: any = {
|
||||
orgId,
|
||||
kind: "downtime",
|
||||
episodeId: { not: null },
|
||||
capturedAt: {
|
||||
gte: start,
|
||||
...(beforeDate ? { lt: beforeDate } : {}),
|
||||
},
|
||||
...(machineId ? { machineId } : {}),
|
||||
...(reasonCode ? { reasonCode } : {}),
|
||||
};
|
||||
|
||||
const rows = await prisma.reasonEntry.findMany({
|
||||
where,
|
||||
orderBy: { capturedAt: "desc" },
|
||||
take: limit,
|
||||
select: {
|
||||
id: true,
|
||||
episodeId: true,
|
||||
machineId: true,
|
||||
reasonCode: true,
|
||||
reasonLabel: true,
|
||||
reasonText: true,
|
||||
durationSeconds: true,
|
||||
capturedAt: true,
|
||||
episodeEndTs: true,
|
||||
workOrderId: true,
|
||||
meta: true,
|
||||
createdAt: true,
|
||||
machine: { select: { name: true } },
|
||||
},
|
||||
});
|
||||
|
||||
const events = rows.map((r) => {
|
||||
const startAt = r.capturedAt;
|
||||
const endAt =
|
||||
r.episodeEndTs ??
|
||||
(r.durationSeconds != null
|
||||
? new Date(startAt.getTime() + r.durationSeconds * 1000)
|
||||
: null);
|
||||
|
||||
const durationSeconds = r.durationSeconds ?? null;
|
||||
const durationMinutes =
|
||||
durationSeconds != null ? Math.round((durationSeconds / 60) * 10) / 10 : null;
|
||||
|
||||
return {
|
||||
id: r.id,
|
||||
episodeId: r.episodeId,
|
||||
machineId: r.machineId,
|
||||
machineName: r.machine?.name ?? null,
|
||||
|
||||
reasonCode: r.reasonCode,
|
||||
reasonLabel: r.reasonLabel ?? r.reasonCode,
|
||||
reasonText: r.reasonText ?? null,
|
||||
|
||||
durationSeconds,
|
||||
durationMinutes,
|
||||
|
||||
startAt: toISO(startAt),
|
||||
endAt: toISO(endAt),
|
||||
capturedAt: toISO(r.capturedAt),
|
||||
|
||||
workOrderId: r.workOrderId ?? null,
|
||||
meta: r.meta ?? null,
|
||||
createdAt: toISO(r.createdAt),
|
||||
};
|
||||
});
|
||||
|
||||
const nextBefore =
|
||||
events.length > 0 ? events[events.length - 1]?.capturedAt ?? null : null;
|
||||
|
||||
return NextResponse.json({
|
||||
ok: true,
|
||||
orgId,
|
||||
range,
|
||||
start,
|
||||
machineId: machineId ?? null,
|
||||
reasonCode: reasonCode ?? null,
|
||||
limit,
|
||||
before: before ?? null,
|
||||
nextBefore, // pass this back for pagination
|
||||
events,
|
||||
});
|
||||
}
|
||||
123
app/api/analytics/pareto/route.ts
Normal file
123
app/api/analytics/pareto/route.ts
Normal file
@@ -0,0 +1,123 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import { prisma } from "@/lib/prisma";
|
||||
import { requireSession } from "@/lib/auth/requireSession";
|
||||
import { coerceDowntimeRange, rangeToStart } from "@/lib/analytics/downtimeRange";
|
||||
|
||||
const bad = (status: number, error: string) =>
|
||||
NextResponse.json({ ok: false, error }, { status });
|
||||
|
||||
export async function GET(req: Request) {
|
||||
// ✅ Session auth (cookie)
|
||||
const session = await requireSession();
|
||||
if (!session) return bad(401, "Unauthorized");
|
||||
const orgId = session.orgId;
|
||||
|
||||
const url = new URL(req.url);
|
||||
|
||||
// ✅ Parse params INSIDE handler
|
||||
const range = coerceDowntimeRange(url.searchParams.get("range"));
|
||||
const start = rangeToStart(range);
|
||||
|
||||
const machineId = url.searchParams.get("machineId"); // optional
|
||||
const kind = (url.searchParams.get("kind") || "downtime").toLowerCase();
|
||||
|
||||
if (kind !== "downtime" && kind !== "scrap") {
|
||||
return bad(400, "Invalid kind (downtime|scrap)");
|
||||
}
|
||||
|
||||
// ✅ If machineId provided, verify it belongs to this org
|
||||
if (machineId) {
|
||||
const m = await prisma.machine.findFirst({
|
||||
where: { id: machineId, orgId },
|
||||
select: { id: true },
|
||||
});
|
||||
if (!m) return bad(404, "Machine not found");
|
||||
}
|
||||
|
||||
// ✅ Scope by orgId (+ machineId if provided)
|
||||
const grouped = await prisma.reasonEntry.groupBy({
|
||||
by: ["reasonCode", "reasonLabel"],
|
||||
where: {
|
||||
orgId,
|
||||
...(machineId ? { machineId } : {}),
|
||||
kind,
|
||||
capturedAt: { gte: start },
|
||||
},
|
||||
_sum: {
|
||||
durationSeconds: true,
|
||||
scrapQty: true,
|
||||
},
|
||||
_count: { _all: true },
|
||||
});
|
||||
|
||||
const itemsRaw = grouped
|
||||
.map((g) => {
|
||||
const value =
|
||||
kind === "downtime"
|
||||
? Math.round(((g._sum.durationSeconds ?? 0) / 60) * 10) / 10 // minutes, 1 decimal
|
||||
: g._sum.scrapQty ?? 0;
|
||||
|
||||
return {
|
||||
reasonCode: g.reasonCode,
|
||||
reasonLabel: g.reasonLabel ?? g.reasonCode,
|
||||
value,
|
||||
count: g._count._all,
|
||||
};
|
||||
})
|
||||
.filter((x) => x.value > 0);
|
||||
|
||||
itemsRaw.sort((a, b) => b.value - a.value);
|
||||
|
||||
const total = itemsRaw.reduce((acc, x) => acc + x.value, 0);
|
||||
|
||||
let cum = 0;
|
||||
let threshold80Index: number | null = null;
|
||||
|
||||
const rows = itemsRaw.map((x, idx) => {
|
||||
const pctOfTotal = total > 0 ? (x.value / total) * 100 : 0;
|
||||
cum += x.value;
|
||||
const cumulativePct = total > 0 ? (cum / total) * 100 : 0;
|
||||
|
||||
if (threshold80Index === null && cumulativePct >= 80) threshold80Index = idx;
|
||||
|
||||
return {
|
||||
reasonCode: x.reasonCode,
|
||||
reasonLabel: x.reasonLabel,
|
||||
minutesLost: kind === "downtime" ? x.value : undefined,
|
||||
scrapQty: kind === "scrap" ? x.value : undefined,
|
||||
pctOfTotal,
|
||||
cumulativePct,
|
||||
count: x.count,
|
||||
};
|
||||
});
|
||||
|
||||
const top3 = rows.slice(0, 3);
|
||||
const threshold80 =
|
||||
threshold80Index === null
|
||||
? null
|
||||
: {
|
||||
index: threshold80Index,
|
||||
reasonCode: rows[threshold80Index].reasonCode,
|
||||
reasonLabel: rows[threshold80Index].reasonLabel,
|
||||
};
|
||||
|
||||
return NextResponse.json({
|
||||
ok: true,
|
||||
orgId,
|
||||
machineId: machineId ?? null,
|
||||
kind,
|
||||
range, // ✅ now defined correctly
|
||||
start, // ✅ now defined correctly
|
||||
totalMinutesLost: kind === "downtime" ? total : undefined,
|
||||
totalScrap: kind === "scrap" ? total : undefined,
|
||||
rows,
|
||||
top3,
|
||||
threshold80,
|
||||
// (optional) keep old shape if anything else uses it:
|
||||
items: itemsRaw.map((x, i) => ({
|
||||
...x,
|
||||
cumPct: rows[i]?.cumulativePct ?? 0,
|
||||
})),
|
||||
total,
|
||||
});
|
||||
}
|
||||
226
app/api/downtime/actions/[id]/route.ts
Normal file
226
app/api/downtime/actions/[id]/route.ts
Normal file
@@ -0,0 +1,226 @@
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
import { z } from "zod";
|
||||
import { Prisma } from "@prisma/client";
|
||||
import { prisma } from "@/lib/prisma";
|
||||
import { requireSession } from "@/lib/auth/requireSession";
|
||||
import { buildDowntimeActionAssignedEmail, sendEmail } from "@/lib/email";
|
||||
import { getBaseUrl } from "@/lib/appUrl";
|
||||
|
||||
const STATUS = ["open", "in_progress", "blocked", "done"] as const;
|
||||
const PRIORITY = ["low", "medium", "high"] as const;
|
||||
const DATE_RE = /^\d{4}-\d{2}-\d{2}$/;
|
||||
|
||||
const updateSchema = z.object({
|
||||
machineId: z.string().trim().min(1).optional().nullable(),
|
||||
reasonCode: z.string().trim().min(1).max(64).optional().nullable(),
|
||||
hmDay: z.number().int().min(0).max(6).optional().nullable(),
|
||||
hmHour: z.number().int().min(0).max(23).optional().nullable(),
|
||||
title: z.string().trim().min(1).max(160).optional(),
|
||||
notes: z.string().trim().max(4000).optional().nullable(),
|
||||
ownerUserId: z.string().trim().min(1).optional().nullable(),
|
||||
dueDate: z.string().trim().regex(DATE_RE).optional().nullable(),
|
||||
status: z.enum(STATUS).optional(),
|
||||
priority: z.enum(PRIORITY).optional(),
|
||||
});
|
||||
|
||||
function parseDueDate(value?: string | null) {
|
||||
if (value === undefined) return undefined;
|
||||
if (!value) return null;
|
||||
return new Date(`${value}T00:00:00.000Z`);
|
||||
}
|
||||
|
||||
function formatDueDate(value?: Date | null) {
|
||||
if (!value) return null;
|
||||
return value.toISOString().slice(0, 10);
|
||||
}
|
||||
|
||||
function buildActionUrl(baseUrl: string, action: { machineId: string | null; reasonCode: string | null; hmDay: number | null; hmHour: number | null }) {
|
||||
const params = new URLSearchParams();
|
||||
if (action.machineId) params.set("machineId", action.machineId);
|
||||
if (action.reasonCode) params.set("reasonCode", action.reasonCode);
|
||||
if (action.hmDay != null && action.hmHour != null) {
|
||||
params.set("hmDay", String(action.hmDay));
|
||||
params.set("hmHour", String(action.hmHour));
|
||||
}
|
||||
const qs = params.toString();
|
||||
return qs ? `${baseUrl}/downtime?${qs}` : `${baseUrl}/downtime`;
|
||||
}
|
||||
|
||||
function serializeAction(action: {
|
||||
id: string;
|
||||
createdAt: Date;
|
||||
updatedAt: Date;
|
||||
machineId: string | null;
|
||||
reasonCode: string | null;
|
||||
hmDay: number | null;
|
||||
hmHour: number | null;
|
||||
title: string;
|
||||
notes: string | null;
|
||||
ownerUserId: string | null;
|
||||
dueDate: Date | null;
|
||||
status: string;
|
||||
priority: string;
|
||||
ownerUser?: { name: string | null; email: string } | null;
|
||||
}) {
|
||||
return {
|
||||
id: action.id,
|
||||
createdAt: action.createdAt.toISOString(),
|
||||
updatedAt: action.updatedAt.toISOString(),
|
||||
machineId: action.machineId,
|
||||
reasonCode: action.reasonCode,
|
||||
hmDay: action.hmDay,
|
||||
hmHour: action.hmHour,
|
||||
title: action.title,
|
||||
notes: action.notes ?? "",
|
||||
ownerUserId: action.ownerUserId,
|
||||
ownerName: action.ownerUser?.name ?? null,
|
||||
ownerEmail: action.ownerUser?.email ?? null,
|
||||
dueDate: formatDueDate(action.dueDate),
|
||||
status: action.status,
|
||||
priority: action.priority,
|
||||
};
|
||||
}
|
||||
|
||||
export async function PATCH(req: NextRequest, context: { params: Promise<{ id: string }> }) {
|
||||
const session = await requireSession();
|
||||
if (!session) return NextResponse.json({ ok: false, error: "Unauthorized" }, { status: 401 });
|
||||
|
||||
const { id } = await context.params;
|
||||
const body = await req.json().catch(() => ({}));
|
||||
const parsed = updateSchema.safeParse(body);
|
||||
if (!parsed.success) {
|
||||
return NextResponse.json({ ok: false, error: "Invalid action payload" }, { status: 400 });
|
||||
}
|
||||
|
||||
const data = parsed.data;
|
||||
if (("hmDay" in data || "hmHour" in data) && (data.hmDay == null) !== (data.hmHour == null)) {
|
||||
return NextResponse.json({ ok: false, error: "Heatmap requires hmDay and hmHour" }, { status: 400 });
|
||||
}
|
||||
|
||||
const existing = await prisma.downtimeAction.findFirst({
|
||||
where: { id, orgId: session.orgId },
|
||||
include: { ownerUser: { select: { name: true, email: true } } },
|
||||
});
|
||||
if (!existing) {
|
||||
return NextResponse.json({ ok: false, error: "Action not found" }, { status: 404 });
|
||||
}
|
||||
|
||||
if (data.machineId) {
|
||||
const machine = await prisma.machine.findFirst({
|
||||
where: { id: data.machineId, orgId: session.orgId },
|
||||
select: { id: true },
|
||||
});
|
||||
if (!machine) {
|
||||
return NextResponse.json({ ok: false, error: "Invalid machineId" }, { status: 400 });
|
||||
}
|
||||
}
|
||||
|
||||
let ownerMembership: { user: { name: string | null; email: string } } | null = null;
|
||||
if (data.ownerUserId) {
|
||||
ownerMembership = await prisma.orgUser.findUnique({
|
||||
where: { orgId_userId: { orgId: session.orgId, userId: data.ownerUserId } },
|
||||
include: { user: { select: { name: true, email: true } } },
|
||||
});
|
||||
if (!ownerMembership) {
|
||||
return NextResponse.json({ ok: false, error: "Invalid ownerUserId" }, { status: 400 });
|
||||
}
|
||||
}
|
||||
|
||||
let completedAt: Date | null | undefined = undefined;
|
||||
if ("status" in data) {
|
||||
completedAt = data.status === "done" ? existing.completedAt ?? new Date() : null;
|
||||
}
|
||||
|
||||
const updateData: Prisma.DowntimeActionUncheckedUpdateInput = {};
|
||||
let shouldResetReminder = false;
|
||||
if ("machineId" in data) updateData.machineId = data.machineId;
|
||||
if ("reasonCode" in data) updateData.reasonCode = data.reasonCode;
|
||||
if ("hmDay" in data) updateData.hmDay = data.hmDay;
|
||||
if ("hmHour" in data) updateData.hmHour = data.hmHour;
|
||||
if ("title" in data) updateData.title = data.title?.trim();
|
||||
if ("notes" in data) updateData.notes = data.notes == null ? null : data.notes.trim() || null;
|
||||
if ("ownerUserId" in data) updateData.ownerUserId = data.ownerUserId;
|
||||
if ("dueDate" in data) {
|
||||
const nextDue = parseDueDate(data.dueDate);
|
||||
const prev = formatDueDate(existing.dueDate);
|
||||
const next = formatDueDate(nextDue ?? null);
|
||||
updateData.dueDate = nextDue;
|
||||
if (prev !== next) {
|
||||
shouldResetReminder = true;
|
||||
}
|
||||
}
|
||||
if ("status" in data) updateData.status = data.status;
|
||||
if ("priority" in data) updateData.priority = data.priority;
|
||||
if (completedAt !== undefined) updateData.completedAt = completedAt;
|
||||
if (shouldResetReminder) {
|
||||
updateData.reminderStage = null;
|
||||
updateData.lastReminderAt = null;
|
||||
}
|
||||
|
||||
const updated = await prisma.downtimeAction.update({
|
||||
where: { id: existing.id },
|
||||
data: updateData,
|
||||
include: { ownerUser: { select: { name: true, email: true } } },
|
||||
});
|
||||
|
||||
const ownerChanged = "ownerUserId" in data && data.ownerUserId !== existing.ownerUserId;
|
||||
const dueChanged =
|
||||
"dueDate" in data && formatDueDate(existing.dueDate) !== formatDueDate(updated.dueDate);
|
||||
|
||||
let emailSent = false;
|
||||
let emailError: string | null = null;
|
||||
if ((ownerChanged || dueChanged) && updated.ownerUser?.email) {
|
||||
try {
|
||||
const org = await prisma.org.findUnique({
|
||||
where: { id: session.orgId },
|
||||
select: { name: true },
|
||||
});
|
||||
const baseUrl = getBaseUrl(req);
|
||||
const actionUrl = buildActionUrl(baseUrl, updated);
|
||||
const content = buildDowntimeActionAssignedEmail({
|
||||
appName: "MIS Control Tower",
|
||||
orgName: org?.name || "your organization",
|
||||
actionTitle: updated.title,
|
||||
assigneeName: updated.ownerUser.name ?? updated.ownerUser.email,
|
||||
dueDate: formatDueDate(updated.dueDate),
|
||||
actionUrl,
|
||||
priority: updated.priority,
|
||||
status: updated.status,
|
||||
});
|
||||
await sendEmail({
|
||||
to: updated.ownerUser.email,
|
||||
subject: content.subject,
|
||||
text: content.text,
|
||||
html: content.html,
|
||||
});
|
||||
emailSent = true;
|
||||
} catch (err: unknown) {
|
||||
emailError = err instanceof Error ? err.message : "Failed to send assignment email";
|
||||
}
|
||||
}
|
||||
|
||||
return NextResponse.json({
|
||||
ok: true,
|
||||
action: serializeAction(updated),
|
||||
emailSent,
|
||||
emailError,
|
||||
});
|
||||
}
|
||||
|
||||
export async function DELETE(req: NextRequest, context: { params: Promise<{ id: string }> }) {
|
||||
const session = await requireSession();
|
||||
if (!session) return NextResponse.json({ ok: false, error: "Unauthorized" }, { status: 401 });
|
||||
|
||||
const { id } = await context.params;
|
||||
const existing = await prisma.downtimeAction.findFirst({
|
||||
where: { id, orgId: session.orgId },
|
||||
select: { id: true },
|
||||
});
|
||||
if (!existing) {
|
||||
return NextResponse.json({ ok: false, error: "Action not found" }, { status: 404 });
|
||||
}
|
||||
|
||||
await prisma.downtimeAction.delete({ where: { id: existing.id } });
|
||||
|
||||
return NextResponse.json({ ok: true });
|
||||
}
|
||||
123
app/api/downtime/actions/reminders/route.ts
Normal file
123
app/api/downtime/actions/reminders/route.ts
Normal file
@@ -0,0 +1,123 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import { prisma } from "@/lib/prisma";
|
||||
import { requireSession } from "@/lib/auth/requireSession";
|
||||
import { buildDowntimeActionReminderEmail, sendEmail } from "@/lib/email";
|
||||
import { getBaseUrl } from "@/lib/appUrl";
|
||||
|
||||
const DEFAULT_DUE_DAYS = 7;
|
||||
const DEFAULT_LIMIT = 100;
|
||||
const MS_PER_HOUR = 60 * 60 * 1000;
|
||||
|
||||
type ReminderStage = "week" | "day" | "hour" | "overdue";
|
||||
|
||||
function formatDueDate(value?: Date | null) {
|
||||
if (!value) return null;
|
||||
return value.toISOString().slice(0, 10);
|
||||
}
|
||||
|
||||
function getReminderStage(dueDate: Date, now: Date): ReminderStage | null {
|
||||
const diffMs = dueDate.getTime() - now.getTime();
|
||||
if (diffMs <= 0) return "overdue";
|
||||
if (diffMs <= MS_PER_HOUR) return "hour";
|
||||
if (diffMs <= 24 * MS_PER_HOUR) return "day";
|
||||
if (diffMs <= 7 * 24 * MS_PER_HOUR) return "week";
|
||||
return null;
|
||||
}
|
||||
|
||||
function buildActionUrl(baseUrl: string, action: { machineId: string | null; reasonCode: string | null; hmDay: number | null; hmHour: number | null }) {
|
||||
const params = new URLSearchParams();
|
||||
if (action.machineId) params.set("machineId", action.machineId);
|
||||
if (action.reasonCode) params.set("reasonCode", action.reasonCode);
|
||||
if (action.hmDay != null && action.hmHour != null) {
|
||||
params.set("hmDay", String(action.hmDay));
|
||||
params.set("hmHour", String(action.hmHour));
|
||||
}
|
||||
const qs = params.toString();
|
||||
return qs ? `${baseUrl}/downtime?${qs}` : `${baseUrl}/downtime`;
|
||||
}
|
||||
|
||||
async function authorizeRequest(req: Request) {
|
||||
const secret = process.env.DOWNTIME_ACTION_REMINDER_SECRET;
|
||||
if (!secret) {
|
||||
const session = await requireSession();
|
||||
return { ok: !!session };
|
||||
}
|
||||
const authHeader = req.headers.get("authorization") || "";
|
||||
const token = authHeader.startsWith("Bearer ") ? authHeader.slice(7).trim() : null;
|
||||
const urlToken = new URL(req.url).searchParams.get("token");
|
||||
return { ok: token === secret || urlToken === secret };
|
||||
}
|
||||
|
||||
export async function POST(req: Request) {
|
||||
const auth = await authorizeRequest(req);
|
||||
if (!auth.ok) return NextResponse.json({ ok: false, error: "Unauthorized" }, { status: 401 });
|
||||
|
||||
const sp = new URL(req.url).searchParams;
|
||||
const dueInDays = Number(sp.get("dueInDays") || DEFAULT_DUE_DAYS);
|
||||
const limit = Number(sp.get("limit") || DEFAULT_LIMIT);
|
||||
|
||||
const now = new Date();
|
||||
const dueBy = new Date(now.getTime() + dueInDays * 24 * 60 * 60 * 1000);
|
||||
|
||||
const actions = await prisma.downtimeAction.findMany({
|
||||
where: {
|
||||
status: { not: "done" },
|
||||
ownerUserId: { not: null },
|
||||
dueDate: { not: null, lte: dueBy },
|
||||
},
|
||||
include: {
|
||||
ownerUser: { select: { name: true, email: true } },
|
||||
org: { select: { name: true } },
|
||||
},
|
||||
orderBy: { dueDate: "asc" },
|
||||
take: Number.isFinite(limit) ? Math.max(1, Math.min(500, limit)) : DEFAULT_LIMIT,
|
||||
});
|
||||
|
||||
const baseUrl = getBaseUrl(req);
|
||||
const sentIds: string[] = [];
|
||||
const failures: Array<{ id: string; error: string }> = [];
|
||||
|
||||
for (const action of actions) {
|
||||
const email = action.ownerUser?.email;
|
||||
if (!email) continue;
|
||||
if (!action.dueDate) continue;
|
||||
const stage = getReminderStage(action.dueDate, now);
|
||||
if (!stage) continue;
|
||||
if (action.reminderStage === stage) continue;
|
||||
try {
|
||||
const content = buildDowntimeActionReminderEmail({
|
||||
appName: "MIS Control Tower",
|
||||
orgName: action.org.name,
|
||||
actionTitle: action.title,
|
||||
assigneeName: action.ownerUser?.name ?? email,
|
||||
dueDate: formatDueDate(action.dueDate),
|
||||
actionUrl: buildActionUrl(baseUrl, action),
|
||||
priority: action.priority,
|
||||
status: action.status,
|
||||
});
|
||||
await sendEmail({
|
||||
to: email,
|
||||
subject: content.subject,
|
||||
text: content.text,
|
||||
html: content.html,
|
||||
});
|
||||
sentIds.push(action.id);
|
||||
await prisma.downtimeAction.update({
|
||||
where: { id: action.id },
|
||||
data: { reminderStage: stage, lastReminderAt: now },
|
||||
});
|
||||
} catch (err: unknown) {
|
||||
failures.push({
|
||||
id: action.id,
|
||||
error: err instanceof Error ? err.message : "Failed to send reminder email",
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
return NextResponse.json({
|
||||
ok: true,
|
||||
sent: sentIds.length,
|
||||
failed: failures.length,
|
||||
failures,
|
||||
});
|
||||
}
|
||||
226
app/api/downtime/actions/route.ts
Normal file
226
app/api/downtime/actions/route.ts
Normal file
@@ -0,0 +1,226 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import { z } from "zod";
|
||||
import { prisma } from "@/lib/prisma";
|
||||
import { requireSession } from "@/lib/auth/requireSession";
|
||||
import { buildDowntimeActionAssignedEmail, sendEmail } from "@/lib/email";
|
||||
import { getBaseUrl } from "@/lib/appUrl";
|
||||
|
||||
const STATUS = ["open", "in_progress", "blocked", "done"] as const;
|
||||
const PRIORITY = ["low", "medium", "high"] as const;
|
||||
const DATE_RE = /^\d{4}-\d{2}-\d{2}$/;
|
||||
|
||||
const createSchema = z.object({
|
||||
machineId: z.string().trim().min(1).optional().nullable(),
|
||||
reasonCode: z.string().trim().min(1).max(64).optional().nullable(),
|
||||
hmDay: z.number().int().min(0).max(6).optional().nullable(),
|
||||
hmHour: z.number().int().min(0).max(23).optional().nullable(),
|
||||
title: z.string().trim().min(1).max(160),
|
||||
notes: z.string().trim().max(4000).optional().nullable(),
|
||||
ownerUserId: z.string().trim().min(1).optional().nullable(),
|
||||
dueDate: z.string().trim().regex(DATE_RE).optional().nullable(),
|
||||
status: z.enum(STATUS).optional(),
|
||||
priority: z.enum(PRIORITY).optional(),
|
||||
});
|
||||
|
||||
function parseDueDate(value?: string | null) {
|
||||
if (!value) return null;
|
||||
return new Date(`${value}T00:00:00.000Z`);
|
||||
}
|
||||
|
||||
function formatDueDate(value?: Date | null) {
|
||||
if (!value) return null;
|
||||
return value.toISOString().slice(0, 10);
|
||||
}
|
||||
|
||||
function buildActionUrl(baseUrl: string, action: { machineId: string | null; reasonCode: string | null; hmDay: number | null; hmHour: number | null }) {
|
||||
const params = new URLSearchParams();
|
||||
if (action.machineId) params.set("machineId", action.machineId);
|
||||
if (action.reasonCode) params.set("reasonCode", action.reasonCode);
|
||||
if (action.hmDay != null && action.hmHour != null) {
|
||||
params.set("hmDay", String(action.hmDay));
|
||||
params.set("hmHour", String(action.hmHour));
|
||||
}
|
||||
const qs = params.toString();
|
||||
return qs ? `${baseUrl}/downtime?${qs}` : `${baseUrl}/downtime`;
|
||||
}
|
||||
|
||||
function serializeAction(action: {
|
||||
id: string;
|
||||
createdAt: Date;
|
||||
updatedAt: Date;
|
||||
machineId: string | null;
|
||||
reasonCode: string | null;
|
||||
hmDay: number | null;
|
||||
hmHour: number | null;
|
||||
title: string;
|
||||
notes: string | null;
|
||||
ownerUserId: string | null;
|
||||
dueDate: Date | null;
|
||||
status: string;
|
||||
priority: string;
|
||||
ownerUser?: { name: string | null; email: string } | null;
|
||||
}) {
|
||||
return {
|
||||
id: action.id,
|
||||
createdAt: action.createdAt.toISOString(),
|
||||
updatedAt: action.updatedAt.toISOString(),
|
||||
machineId: action.machineId,
|
||||
reasonCode: action.reasonCode,
|
||||
hmDay: action.hmDay,
|
||||
hmHour: action.hmHour,
|
||||
title: action.title,
|
||||
notes: action.notes ?? "",
|
||||
ownerUserId: action.ownerUserId,
|
||||
ownerName: action.ownerUser?.name ?? null,
|
||||
ownerEmail: action.ownerUser?.email ?? null,
|
||||
dueDate: formatDueDate(action.dueDate),
|
||||
status: action.status,
|
||||
priority: action.priority,
|
||||
};
|
||||
}
|
||||
|
||||
export async function GET(req: Request) {
|
||||
const session = await requireSession();
|
||||
if (!session) return NextResponse.json({ ok: false, error: "Unauthorized" }, { status: 401 });
|
||||
|
||||
const sp = new URL(req.url).searchParams;
|
||||
const machineId = sp.get("machineId");
|
||||
const reasonCode = sp.get("reasonCode");
|
||||
const hmDayStr = sp.get("hmDay");
|
||||
const hmHourStr = sp.get("hmHour");
|
||||
|
||||
const hmDay = hmDayStr != null ? Number(hmDayStr) : null;
|
||||
const hmHour = hmHourStr != null ? Number(hmHourStr) : null;
|
||||
if ((hmDayStr != null || hmHourStr != null) && (!Number.isFinite(hmDay) || !Number.isFinite(hmHour))) {
|
||||
return NextResponse.json({ ok: false, error: "Invalid heatmap selection" }, { status: 400 });
|
||||
}
|
||||
if ((hmDayStr != null || hmHourStr != null) && (hmDay == null || hmHour == null)) {
|
||||
return NextResponse.json({ ok: false, error: "Heatmap requires hmDay and hmHour" }, { status: 400 });
|
||||
}
|
||||
|
||||
const where: {
|
||||
orgId: string;
|
||||
AND?: Array<Record<string, unknown>>;
|
||||
} = { orgId: session.orgId };
|
||||
|
||||
if (machineId) {
|
||||
where.AND = [...(where.AND ?? []), { OR: [{ machineId }, { machineId: null }] }];
|
||||
}
|
||||
|
||||
if (reasonCode) {
|
||||
where.AND = [...(where.AND ?? []), { OR: [{ reasonCode }, { reasonCode: null }] }];
|
||||
}
|
||||
|
||||
if (hmDay != null && hmHour != null) {
|
||||
where.AND = [
|
||||
...(where.AND ?? []),
|
||||
{ OR: [{ hmDay, hmHour }, { hmDay: null, hmHour: null }] },
|
||||
];
|
||||
}
|
||||
|
||||
const actions = await prisma.downtimeAction.findMany({
|
||||
where,
|
||||
orderBy: { updatedAt: "desc" },
|
||||
include: { ownerUser: { select: { name: true, email: true } } },
|
||||
});
|
||||
|
||||
return NextResponse.json({
|
||||
ok: true,
|
||||
actions: actions.map(serializeAction),
|
||||
});
|
||||
}
|
||||
|
||||
export async function POST(req: Request) {
|
||||
const session = await requireSession();
|
||||
if (!session) return NextResponse.json({ ok: false, error: "Unauthorized" }, { status: 401 });
|
||||
|
||||
const body = await req.json().catch(() => ({}));
|
||||
const parsed = createSchema.safeParse(body);
|
||||
if (!parsed.success) {
|
||||
return NextResponse.json({ ok: false, error: "Invalid action payload" }, { status: 400 });
|
||||
}
|
||||
|
||||
const data = parsed.data;
|
||||
if ((data.hmDay == null) !== (data.hmHour == null)) {
|
||||
return NextResponse.json({ ok: false, error: "Heatmap requires hmDay and hmHour" }, { status: 400 });
|
||||
}
|
||||
|
||||
if (data.machineId) {
|
||||
const machine = await prisma.machine.findFirst({
|
||||
where: { id: data.machineId, orgId: session.orgId },
|
||||
select: { id: true },
|
||||
});
|
||||
if (!machine) {
|
||||
return NextResponse.json({ ok: false, error: "Invalid machineId" }, { status: 400 });
|
||||
}
|
||||
}
|
||||
|
||||
let ownerMembership: { user: { name: string | null; email: string } } | null = null;
|
||||
if (data.ownerUserId) {
|
||||
ownerMembership = await prisma.orgUser.findUnique({
|
||||
where: { orgId_userId: { orgId: session.orgId, userId: data.ownerUserId } },
|
||||
include: { user: { select: { name: true, email: true } } },
|
||||
});
|
||||
if (!ownerMembership) {
|
||||
return NextResponse.json({ ok: false, error: "Invalid ownerUserId" }, { status: 400 });
|
||||
}
|
||||
}
|
||||
|
||||
const created = await prisma.downtimeAction.create({
|
||||
data: {
|
||||
orgId: session.orgId,
|
||||
machineId: data.machineId ?? null,
|
||||
reasonCode: data.reasonCode ?? null,
|
||||
hmDay: data.hmDay ?? null,
|
||||
hmHour: data.hmHour ?? null,
|
||||
title: data.title.trim(),
|
||||
notes: data.notes?.trim() || null,
|
||||
ownerUserId: data.ownerUserId ?? null,
|
||||
dueDate: parseDueDate(data.dueDate),
|
||||
status: data.status ?? "open",
|
||||
priority: data.priority ?? "medium",
|
||||
completedAt: data.status === "done" ? new Date() : null,
|
||||
createdBy: session.userId,
|
||||
},
|
||||
include: { ownerUser: { select: { name: true, email: true } } },
|
||||
});
|
||||
|
||||
let emailSent = false;
|
||||
let emailError: string | null = null;
|
||||
if (ownerMembership?.user?.email) {
|
||||
try {
|
||||
const org = await prisma.org.findUnique({
|
||||
where: { id: session.orgId },
|
||||
select: { name: true },
|
||||
});
|
||||
const baseUrl = getBaseUrl(req);
|
||||
const actionUrl = buildActionUrl(baseUrl, created);
|
||||
const content = buildDowntimeActionAssignedEmail({
|
||||
appName: "MIS Control Tower",
|
||||
orgName: org?.name || "your organization",
|
||||
actionTitle: created.title,
|
||||
assigneeName: ownerMembership.user.name ?? ownerMembership.user.email,
|
||||
dueDate: formatDueDate(created.dueDate),
|
||||
actionUrl,
|
||||
priority: created.priority,
|
||||
status: created.status,
|
||||
});
|
||||
await sendEmail({
|
||||
to: ownerMembership.user.email,
|
||||
subject: content.subject,
|
||||
text: content.text,
|
||||
html: content.html,
|
||||
});
|
||||
emailSent = true;
|
||||
} catch (err: unknown) {
|
||||
emailError = err instanceof Error ? err.message : "Failed to send assignment email";
|
||||
}
|
||||
}
|
||||
|
||||
return NextResponse.json({
|
||||
ok: true,
|
||||
action: serializeAction(created),
|
||||
emailSent,
|
||||
emailError,
|
||||
});
|
||||
}
|
||||
262
app/api/financial/costs/route.ts
Normal file
262
app/api/financial/costs/route.ts
Normal file
@@ -0,0 +1,262 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import { prisma } from "@/lib/prisma";
|
||||
import { requireSession } from "@/lib/auth/requireSession";
|
||||
import { Prisma } from "@prisma/client";
|
||||
import { z } from "zod";
|
||||
|
||||
function canManageFinancials(role?: string | null) {
|
||||
return role === "OWNER";
|
||||
}
|
||||
|
||||
function stripUndefined<T extends Record<string, unknown>>(input: T) {
|
||||
const out: Record<string, unknown> = {};
|
||||
for (const [key, value] of Object.entries(input)) {
|
||||
if (value !== undefined) out[key] = value;
|
||||
}
|
||||
return out as T;
|
||||
}
|
||||
|
||||
function normalizeCurrency(value?: string | null) {
|
||||
if (!value) return null;
|
||||
const trimmed = value.trim();
|
||||
if (!trimmed) return null;
|
||||
return trimmed.toUpperCase();
|
||||
}
|
||||
|
||||
const numberField = z.preprocess(
|
||||
(value) => {
|
||||
if (value === "" || value === null || value === undefined) return null;
|
||||
const n = Number(value);
|
||||
return Number.isFinite(n) ? n : value;
|
||||
},
|
||||
z.number().finite().nullable()
|
||||
);
|
||||
|
||||
const numericFields = {
|
||||
machineCostPerMin: numberField.optional(),
|
||||
operatorCostPerMin: numberField.optional(),
|
||||
ratedRunningKw: numberField.optional(),
|
||||
idleKw: numberField.optional(),
|
||||
kwhRate: numberField.optional(),
|
||||
energyMultiplier: numberField.optional(),
|
||||
energyCostPerMin: numberField.optional(),
|
||||
scrapCostPerUnit: numberField.optional(),
|
||||
rawMaterialCostPerUnit: numberField.optional(),
|
||||
};
|
||||
|
||||
const orgSchema = z
|
||||
.object({
|
||||
defaultCurrency: z.string().trim().min(1).max(8).optional(),
|
||||
...numericFields,
|
||||
})
|
||||
.strict();
|
||||
|
||||
const locationSchema = z
|
||||
.object({
|
||||
location: z.string().trim().min(1).max(80),
|
||||
currency: z.string().trim().min(1).max(8).optional().nullable(),
|
||||
...numericFields,
|
||||
})
|
||||
.strict();
|
||||
|
||||
const machineSchema = z
|
||||
.object({
|
||||
machineId: z.string().uuid(),
|
||||
currency: z.string().trim().min(1).max(8).optional().nullable(),
|
||||
...numericFields,
|
||||
})
|
||||
.strict();
|
||||
|
||||
const productSchema = z
|
||||
.object({
|
||||
sku: z.string().trim().min(1).max(64),
|
||||
currency: z.string().trim().min(1).max(8).optional().nullable(),
|
||||
rawMaterialCostPerUnit: numberField.optional(),
|
||||
})
|
||||
.strict();
|
||||
|
||||
const payloadSchema = z
|
||||
.object({
|
||||
org: orgSchema.optional(),
|
||||
locations: z.array(locationSchema).optional(),
|
||||
machines: z.array(machineSchema).optional(),
|
||||
products: z.array(productSchema).optional(),
|
||||
})
|
||||
.strict();
|
||||
|
||||
async function ensureOrgFinancialProfile(
|
||||
tx: Prisma.TransactionClient,
|
||||
orgId: string,
|
||||
userId: string
|
||||
) {
|
||||
const existing = await tx.orgFinancialProfile.findUnique({ where: { orgId } });
|
||||
if (existing) return existing;
|
||||
return tx.orgFinancialProfile.create({
|
||||
data: {
|
||||
orgId,
|
||||
defaultCurrency: "USD",
|
||||
energyMultiplier: 1.0,
|
||||
updatedBy: userId,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
async function loadFinancialConfig(orgId: string) {
|
||||
const [org, locations, machines, products] = await Promise.all([
|
||||
prisma.orgFinancialProfile.findUnique({ where: { orgId } }),
|
||||
prisma.locationFinancialOverride.findMany({ where: { orgId }, orderBy: { location: "asc" } }),
|
||||
prisma.machineFinancialOverride.findMany({ where: { orgId }, orderBy: { createdAt: "desc" } }),
|
||||
prisma.productCostOverride.findMany({ where: { orgId }, orderBy: { sku: "asc" } }),
|
||||
]);
|
||||
|
||||
return { org, locations, machines, products };
|
||||
}
|
||||
|
||||
export async function GET() {
|
||||
const session = await requireSession();
|
||||
if (!session) return NextResponse.json({ ok: false, error: "Unauthorized" }, { status: 401 });
|
||||
|
||||
const membership = await prisma.orgUser.findUnique({
|
||||
where: { orgId_userId: { orgId: session.orgId, userId: session.userId } },
|
||||
select: { role: true },
|
||||
});
|
||||
if (!canManageFinancials(membership?.role)) {
|
||||
return NextResponse.json({ ok: false, error: "Forbidden" }, { status: 403 });
|
||||
}
|
||||
|
||||
await prisma.$transaction((tx) => ensureOrgFinancialProfile(tx, session.orgId, session.userId));
|
||||
const payload = await loadFinancialConfig(session.orgId);
|
||||
return NextResponse.json({ ok: true, ...payload });
|
||||
}
|
||||
|
||||
export async function POST(req: Request) {
|
||||
const session = await requireSession();
|
||||
if (!session) return NextResponse.json({ ok: false, error: "Unauthorized" }, { status: 401 });
|
||||
|
||||
const membership = await prisma.orgUser.findUnique({
|
||||
where: { orgId_userId: { orgId: session.orgId, userId: session.userId } },
|
||||
select: { role: true },
|
||||
});
|
||||
if (!canManageFinancials(membership?.role)) {
|
||||
return NextResponse.json({ ok: false, error: "Forbidden" }, { status: 403 });
|
||||
}
|
||||
|
||||
const body = await req.json().catch(() => ({}));
|
||||
const parsed = payloadSchema.safeParse(body);
|
||||
if (!parsed.success) {
|
||||
return NextResponse.json({ ok: false, error: "Invalid payload" }, { status: 400 });
|
||||
}
|
||||
|
||||
const data = parsed.data;
|
||||
|
||||
await prisma.$transaction(async (tx) => {
|
||||
await ensureOrgFinancialProfile(tx, session.orgId, session.userId);
|
||||
|
||||
if (data.org) {
|
||||
const updateData = stripUndefined({
|
||||
defaultCurrency: data.org.defaultCurrency?.trim().toUpperCase(),
|
||||
machineCostPerMin: data.org.machineCostPerMin,
|
||||
operatorCostPerMin: data.org.operatorCostPerMin,
|
||||
ratedRunningKw: data.org.ratedRunningKw,
|
||||
idleKw: data.org.idleKw,
|
||||
kwhRate: data.org.kwhRate,
|
||||
energyMultiplier: data.org.energyMultiplier == null ? undefined : data.org.energyMultiplier,
|
||||
energyCostPerMin: data.org.energyCostPerMin,
|
||||
scrapCostPerUnit: data.org.scrapCostPerUnit,
|
||||
rawMaterialCostPerUnit: data.org.rawMaterialCostPerUnit,
|
||||
updatedBy: session.userId,
|
||||
});
|
||||
|
||||
if (Object.keys(updateData).length > 0) {
|
||||
await tx.orgFinancialProfile.update({
|
||||
where: { orgId: session.orgId },
|
||||
data: updateData,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
const machineIds = new Set((data.machines ?? []).map((m) => m.machineId));
|
||||
const validMachineIds = new Set<string>();
|
||||
if (machineIds.size > 0) {
|
||||
const rows = await tx.machine.findMany({
|
||||
where: { orgId: session.orgId, id: { in: Array.from(machineIds) } },
|
||||
select: { id: true },
|
||||
});
|
||||
rows.forEach((m) => validMachineIds.add(m.id));
|
||||
}
|
||||
|
||||
for (const loc of data.locations ?? []) {
|
||||
const updateData = stripUndefined({
|
||||
currency: normalizeCurrency(loc.currency),
|
||||
machineCostPerMin: loc.machineCostPerMin,
|
||||
operatorCostPerMin: loc.operatorCostPerMin,
|
||||
ratedRunningKw: loc.ratedRunningKw,
|
||||
idleKw: loc.idleKw,
|
||||
kwhRate: loc.kwhRate,
|
||||
energyMultiplier: loc.energyMultiplier,
|
||||
energyCostPerMin: loc.energyCostPerMin,
|
||||
scrapCostPerUnit: loc.scrapCostPerUnit,
|
||||
rawMaterialCostPerUnit: loc.rawMaterialCostPerUnit,
|
||||
updatedBy: session.userId,
|
||||
});
|
||||
|
||||
await tx.locationFinancialOverride.upsert({
|
||||
where: { orgId_location: { orgId: session.orgId, location: loc.location } },
|
||||
update: updateData,
|
||||
create: {
|
||||
orgId: session.orgId,
|
||||
location: loc.location,
|
||||
...updateData,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
for (const machine of data.machines ?? []) {
|
||||
if (!validMachineIds.has(machine.machineId)) continue;
|
||||
const updateData = stripUndefined({
|
||||
currency: normalizeCurrency(machine.currency),
|
||||
machineCostPerMin: machine.machineCostPerMin,
|
||||
operatorCostPerMin: machine.operatorCostPerMin,
|
||||
ratedRunningKw: machine.ratedRunningKw,
|
||||
idleKw: machine.idleKw,
|
||||
kwhRate: machine.kwhRate,
|
||||
energyMultiplier: machine.energyMultiplier,
|
||||
energyCostPerMin: machine.energyCostPerMin,
|
||||
scrapCostPerUnit: machine.scrapCostPerUnit,
|
||||
rawMaterialCostPerUnit: machine.rawMaterialCostPerUnit,
|
||||
updatedBy: session.userId,
|
||||
});
|
||||
|
||||
await tx.machineFinancialOverride.upsert({
|
||||
where: { orgId_machineId: { orgId: session.orgId, machineId: machine.machineId } },
|
||||
update: updateData,
|
||||
create: {
|
||||
orgId: session.orgId,
|
||||
machineId: machine.machineId,
|
||||
...updateData,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
for (const product of data.products ?? []) {
|
||||
const updateData = stripUndefined({
|
||||
currency: normalizeCurrency(product.currency),
|
||||
rawMaterialCostPerUnit: product.rawMaterialCostPerUnit,
|
||||
updatedBy: session.userId,
|
||||
});
|
||||
|
||||
await tx.productCostOverride.upsert({
|
||||
where: { orgId_sku: { orgId: session.orgId, sku: product.sku } },
|
||||
update: updateData,
|
||||
create: {
|
||||
orgId: session.orgId,
|
||||
sku: product.sku,
|
||||
...updateData,
|
||||
},
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
const payload = await loadFinancialConfig(session.orgId);
|
||||
return NextResponse.json({ ok: true, ...payload });
|
||||
}
|
||||
156
app/api/financial/export/excel/route.ts
Normal file
156
app/api/financial/export/excel/route.ts
Normal file
@@ -0,0 +1,156 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import type { NextRequest } from "next/server";
|
||||
import { requireSession } from "@/lib/auth/requireSession";
|
||||
import { prisma } from "@/lib/prisma";
|
||||
import { computeFinancialImpact } from "@/lib/financial/impact";
|
||||
|
||||
const RANGE_MS: Record<string, number> = {
|
||||
"24h": 24 * 60 * 60 * 1000,
|
||||
"7d": 7 * 24 * 60 * 60 * 1000,
|
||||
"30d": 30 * 24 * 60 * 60 * 1000,
|
||||
};
|
||||
|
||||
function canManageFinancials(role?: string | null) {
|
||||
return role === "OWNER";
|
||||
}
|
||||
|
||||
function parseDate(input?: string | null) {
|
||||
if (!input) return null;
|
||||
const n = Number(input);
|
||||
if (!Number.isNaN(n)) return new Date(n);
|
||||
const d = new Date(input);
|
||||
return Number.isNaN(d.getTime()) ? null : d;
|
||||
}
|
||||
|
||||
function pickRange(req: NextRequest) {
|
||||
const url = new URL(req.url);
|
||||
const range = url.searchParams.get("range") ?? "7d";
|
||||
const now = new Date();
|
||||
|
||||
if (range === "custom") {
|
||||
const start = parseDate(url.searchParams.get("start")) ?? new Date(now.getTime() - RANGE_MS["24h"]);
|
||||
const end = parseDate(url.searchParams.get("end")) ?? now;
|
||||
return { start, end };
|
||||
}
|
||||
|
||||
const ms = RANGE_MS[range] ?? RANGE_MS["24h"];
|
||||
return { start: new Date(now.getTime() - ms), end: now };
|
||||
}
|
||||
|
||||
function csvValue(value: string | number | null | undefined) {
|
||||
if (value === null || value === undefined) return "";
|
||||
const text = String(value);
|
||||
if (/[",\n]/.test(text)) {
|
||||
return `"${text.replace(/"/g, "\"\"")}"`;
|
||||
}
|
||||
return text;
|
||||
}
|
||||
|
||||
function formatNumber(value: number | null) {
|
||||
if (value == null || !Number.isFinite(value)) return "";
|
||||
return value.toFixed(4);
|
||||
}
|
||||
|
||||
function slugify(value: string) {
|
||||
return value
|
||||
.toLowerCase()
|
||||
.replace(/[^a-z0-9]+/g, "-")
|
||||
.replace(/^-+|-+$/g, "")
|
||||
.slice(0, 60) || "report";
|
||||
}
|
||||
|
||||
export async function GET(req: NextRequest) {
|
||||
const session = await requireSession();
|
||||
if (!session) return NextResponse.json({ ok: false, error: "Unauthorized" }, { status: 401 });
|
||||
|
||||
const membership = await prisma.orgUser.findUnique({
|
||||
where: { orgId_userId: { orgId: session.orgId, userId: session.userId } },
|
||||
select: { role: true },
|
||||
});
|
||||
if (!canManageFinancials(membership?.role)) {
|
||||
return NextResponse.json({ ok: false, error: "Forbidden" }, { status: 403 });
|
||||
}
|
||||
|
||||
const url = new URL(req.url);
|
||||
const { start, end } = pickRange(req);
|
||||
const machineId = url.searchParams.get("machineId") ?? undefined;
|
||||
const location = url.searchParams.get("location") ?? undefined;
|
||||
const sku = url.searchParams.get("sku") ?? undefined;
|
||||
const currency = url.searchParams.get("currency") ?? undefined;
|
||||
|
||||
const [org, impact] = await Promise.all([
|
||||
prisma.org.findUnique({ where: { id: session.orgId }, select: { name: true } }),
|
||||
computeFinancialImpact({
|
||||
orgId: session.orgId,
|
||||
start,
|
||||
end,
|
||||
machineId,
|
||||
location,
|
||||
sku,
|
||||
currency,
|
||||
includeEvents: true,
|
||||
}),
|
||||
]);
|
||||
|
||||
const orgName = org?.name ?? "Organization";
|
||||
const header = [
|
||||
"org_name",
|
||||
"range_start",
|
||||
"range_end",
|
||||
"event_id",
|
||||
"event_ts",
|
||||
"event_type",
|
||||
"status",
|
||||
"severity",
|
||||
"category",
|
||||
"machine_id",
|
||||
"machine_name",
|
||||
"location",
|
||||
"work_order_id",
|
||||
"sku",
|
||||
"duration_sec",
|
||||
"cost_machine",
|
||||
"cost_operator",
|
||||
"cost_energy",
|
||||
"cost_scrap",
|
||||
"cost_raw_material",
|
||||
"cost_total",
|
||||
"currency",
|
||||
];
|
||||
|
||||
const rows = impact.events.map((event) => [
|
||||
orgName,
|
||||
start.toISOString(),
|
||||
end.toISOString(),
|
||||
event.id,
|
||||
event.ts.toISOString(),
|
||||
event.eventType,
|
||||
event.status,
|
||||
event.severity,
|
||||
event.category,
|
||||
event.machineId,
|
||||
event.machineName ?? "",
|
||||
event.location ?? "",
|
||||
event.workOrderId ?? "",
|
||||
event.sku ?? "",
|
||||
formatNumber(event.durationSec),
|
||||
formatNumber(event.costMachine),
|
||||
formatNumber(event.costOperator),
|
||||
formatNumber(event.costEnergy),
|
||||
formatNumber(event.costScrap),
|
||||
formatNumber(event.costRawMaterial),
|
||||
formatNumber(event.costTotal),
|
||||
event.currency,
|
||||
]);
|
||||
|
||||
const lines = [header, ...rows].map((row) => row.map(csvValue).join(","));
|
||||
const csv = lines.join("\n");
|
||||
|
||||
const fileName = `financial_events_${slugify(orgName)}.csv`;
|
||||
return new NextResponse(csv, {
|
||||
headers: {
|
||||
"Content-Type": "text/csv; charset=utf-8",
|
||||
"Content-Disposition": `attachment; filename=\"${fileName}\"`,
|
||||
},
|
||||
});
|
||||
}
|
||||
246
app/api/financial/export/pdf/route.ts
Normal file
246
app/api/financial/export/pdf/route.ts
Normal file
@@ -0,0 +1,246 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import type { NextRequest } from "next/server";
|
||||
import { requireSession } from "@/lib/auth/requireSession";
|
||||
import { prisma } from "@/lib/prisma";
|
||||
import { computeFinancialImpact } from "@/lib/financial/impact";
|
||||
|
||||
const RANGE_MS: Record<string, number> = {
|
||||
"24h": 24 * 60 * 60 * 1000,
|
||||
"7d": 7 * 24 * 60 * 60 * 1000,
|
||||
"30d": 30 * 24 * 60 * 60 * 1000,
|
||||
};
|
||||
|
||||
function canManageFinancials(role?: string | null) {
|
||||
return role === "OWNER";
|
||||
}
|
||||
|
||||
function parseDate(input?: string | null) {
|
||||
if (!input) return null;
|
||||
const n = Number(input);
|
||||
if (!Number.isNaN(n)) return new Date(n);
|
||||
const d = new Date(input);
|
||||
return Number.isNaN(d.getTime()) ? null : d;
|
||||
}
|
||||
|
||||
function pickRange(req: NextRequest) {
|
||||
const url = new URL(req.url);
|
||||
const range = url.searchParams.get("range") ?? "7d";
|
||||
const now = new Date();
|
||||
|
||||
if (range === "custom") {
|
||||
const start = parseDate(url.searchParams.get("start")) ?? new Date(now.getTime() - RANGE_MS["24h"]);
|
||||
const end = parseDate(url.searchParams.get("end")) ?? now;
|
||||
return { start, end };
|
||||
}
|
||||
|
||||
const ms = RANGE_MS[range] ?? RANGE_MS["24h"];
|
||||
return { start: new Date(now.getTime() - ms), end: now };
|
||||
}
|
||||
|
||||
function escapeHtml(value: string) {
|
||||
return value
|
||||
.replace(/&/g, "&")
|
||||
.replace(/</g, "<")
|
||||
.replace(/>/g, ">")
|
||||
.replace(/\"/g, """)
|
||||
.replace(/'/g, "'");
|
||||
}
|
||||
|
||||
function formatMoney(value: number, currency: string) {
|
||||
if (!Number.isFinite(value)) return "--";
|
||||
try {
|
||||
return new Intl.NumberFormat("en-US", { style: "currency", currency, maximumFractionDigits: 2 }).format(value);
|
||||
} catch {
|
||||
return `${value.toFixed(2)} ${currency}`;
|
||||
}
|
||||
}
|
||||
|
||||
function formatNumber(value: number | null, digits = 2) {
|
||||
if (value == null || !Number.isFinite(value)) return "--";
|
||||
return value.toFixed(digits);
|
||||
}
|
||||
|
||||
function slugify(value: string) {
|
||||
return value
|
||||
.toLowerCase()
|
||||
.replace(/[^a-z0-9]+/g, "-")
|
||||
.replace(/^-+|-+$/g, "")
|
||||
.slice(0, 60) || "report";
|
||||
}
|
||||
|
||||
export async function GET(req: NextRequest) {
|
||||
const session = await requireSession();
|
||||
if (!session) return NextResponse.json({ ok: false, error: "Unauthorized" }, { status: 401 });
|
||||
|
||||
const membership = await prisma.orgUser.findUnique({
|
||||
where: { orgId_userId: { orgId: session.orgId, userId: session.userId } },
|
||||
select: { role: true },
|
||||
});
|
||||
if (!canManageFinancials(membership?.role)) {
|
||||
return NextResponse.json({ ok: false, error: "Forbidden" }, { status: 403 });
|
||||
}
|
||||
|
||||
const url = new URL(req.url);
|
||||
const { start, end } = pickRange(req);
|
||||
const machineId = url.searchParams.get("machineId") ?? undefined;
|
||||
const location = url.searchParams.get("location") ?? undefined;
|
||||
const sku = url.searchParams.get("sku") ?? undefined;
|
||||
const currency = url.searchParams.get("currency") ?? undefined;
|
||||
|
||||
const [org, impact] = await Promise.all([
|
||||
prisma.org.findUnique({ where: { id: session.orgId }, select: { name: true } }),
|
||||
computeFinancialImpact({
|
||||
orgId: session.orgId,
|
||||
start,
|
||||
end,
|
||||
machineId,
|
||||
location,
|
||||
sku,
|
||||
currency,
|
||||
includeEvents: true,
|
||||
}),
|
||||
]);
|
||||
|
||||
const orgName = org?.name ?? "Organization";
|
||||
const summaryBlocks = impact.currencySummaries
|
||||
.map(
|
||||
(summary) => `
|
||||
<div class="card">
|
||||
<div class="card-title">${escapeHtml(summary.currency)}</div>
|
||||
<div class="card-value">${escapeHtml(formatMoney(summary.totals.total, summary.currency))}</div>
|
||||
<div class="card-sub">Slow: ${escapeHtml(formatMoney(summary.totals.slowCycle, summary.currency))}</div>
|
||||
<div class="card-sub">Micro: ${escapeHtml(formatMoney(summary.totals.microstop, summary.currency))}</div>
|
||||
<div class="card-sub">Macro: ${escapeHtml(formatMoney(summary.totals.macrostop, summary.currency))}</div>
|
||||
<div class="card-sub">Scrap: ${escapeHtml(formatMoney(summary.totals.scrap, summary.currency))}</div>
|
||||
</div>
|
||||
`
|
||||
)
|
||||
.join("");
|
||||
|
||||
const dailyTables = impact.currencySummaries
|
||||
.map((summary) => {
|
||||
const rows = summary.byDay
|
||||
.map(
|
||||
(row) => `
|
||||
<tr>
|
||||
<td>${escapeHtml(row.day)}</td>
|
||||
<td>${escapeHtml(formatMoney(row.total, summary.currency))}</td>
|
||||
<td>${escapeHtml(formatMoney(row.slowCycle, summary.currency))}</td>
|
||||
<td>${escapeHtml(formatMoney(row.microstop, summary.currency))}</td>
|
||||
<td>${escapeHtml(formatMoney(row.macrostop, summary.currency))}</td>
|
||||
<td>${escapeHtml(formatMoney(row.scrap, summary.currency))}</td>
|
||||
</tr>
|
||||
`
|
||||
)
|
||||
.join("");
|
||||
|
||||
return `
|
||||
<section class="section">
|
||||
<h3>${escapeHtml(summary.currency)} Daily Breakdown</h3>
|
||||
<table>
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Day</th>
|
||||
<th>Total</th>
|
||||
<th>Slow</th>
|
||||
<th>Micro</th>
|
||||
<th>Macro</th>
|
||||
<th>Scrap</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
${rows || "<tr><td colspan=\"6\">No data</td></tr>"}
|
||||
</tbody>
|
||||
</table>
|
||||
</section>
|
||||
`;
|
||||
})
|
||||
.join("");
|
||||
|
||||
const eventRows = impact.events
|
||||
.map(
|
||||
(e) => `
|
||||
<tr>
|
||||
<td>${escapeHtml(e.ts.toISOString())}</td>
|
||||
<td>${escapeHtml(e.eventType)}</td>
|
||||
<td>${escapeHtml(e.category)}</td>
|
||||
<td>${escapeHtml(e.machineName ?? "-")}</td>
|
||||
<td>${escapeHtml(e.location ?? "-")}</td>
|
||||
<td>${escapeHtml(e.sku ?? "-")}</td>
|
||||
<td>${escapeHtml(e.workOrderId ?? "-")}</td>
|
||||
<td>${escapeHtml(formatNumber(e.durationSec))}</td>
|
||||
<td>${escapeHtml(formatMoney(e.costTotal, e.currency))}</td>
|
||||
<td>${escapeHtml(e.currency)}</td>
|
||||
</tr>
|
||||
`
|
||||
)
|
||||
.join("");
|
||||
|
||||
const html = `<!doctype html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="utf-8" />
|
||||
<title>Financial Impact Report</title>
|
||||
<style>
|
||||
body { font-family: Arial, sans-serif; color: #0f172a; margin: 32px; }
|
||||
h1 { margin: 0 0 6px; }
|
||||
.muted { color: #64748b; font-size: 12px; }
|
||||
.cards { display: grid; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); gap: 12px; margin: 20px 0; }
|
||||
.card { border: 1px solid #e2e8f0; border-radius: 12px; padding: 12px; }
|
||||
.card-title { font-size: 12px; text-transform: uppercase; color: #64748b; }
|
||||
.card-value { font-size: 20px; font-weight: 700; margin: 8px 0; }
|
||||
.card-sub { font-size: 12px; color: #475569; }
|
||||
.section { margin-top: 24px; }
|
||||
table { width: 100%; border-collapse: collapse; margin-top: 8px; }
|
||||
th, td { border: 1px solid #e2e8f0; padding: 8px; font-size: 12px; text-align: left; }
|
||||
th { background: #f8fafc; }
|
||||
footer { margin-top: 32px; text-align: right; font-size: 11px; color: #94a3b8; }
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<header>
|
||||
<h1>Financial Impact Report</h1>
|
||||
<div class="muted">${escapeHtml(orgName)} | ${escapeHtml(start.toISOString())} - ${escapeHtml(end.toISOString())}</div>
|
||||
</header>
|
||||
|
||||
<section class="cards">
|
||||
${summaryBlocks || "<div class=\"muted\">No totals yet.</div>"}
|
||||
</section>
|
||||
|
||||
${dailyTables}
|
||||
|
||||
<section class="section">
|
||||
<h3>Event Details</h3>
|
||||
<table>
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Timestamp</th>
|
||||
<th>Event</th>
|
||||
<th>Category</th>
|
||||
<th>Machine</th>
|
||||
<th>Location</th>
|
||||
<th>SKU</th>
|
||||
<th>Work Order</th>
|
||||
<th>Duration (sec)</th>
|
||||
<th>Cost</th>
|
||||
<th>Currency</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
${eventRows || "<tr><td colspan=\"10\">No events</td></tr>"}
|
||||
</tbody>
|
||||
</table>
|
||||
</section>
|
||||
|
||||
<footer>Power by MaliounTech</footer>
|
||||
</body>
|
||||
</html>`;
|
||||
|
||||
const fileName = `financial_report_${slugify(orgName)}.html`;
|
||||
return new NextResponse(html, {
|
||||
headers: {
|
||||
"Content-Type": "text/html; charset=utf-8",
|
||||
"Content-Disposition": `attachment; filename=\"${fileName}\"`,
|
||||
},
|
||||
});
|
||||
}
|
||||
71
app/api/financial/impact/route.ts
Normal file
71
app/api/financial/impact/route.ts
Normal file
@@ -0,0 +1,71 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import type { NextRequest } from "next/server";
|
||||
import { prisma } from "@/lib/prisma";
|
||||
import { requireSession } from "@/lib/auth/requireSession";
|
||||
import { computeFinancialImpact } from "@/lib/financial/impact";
|
||||
|
||||
const RANGE_MS: Record<string, number> = {
|
||||
"24h": 24 * 60 * 60 * 1000,
|
||||
"7d": 7 * 24 * 60 * 60 * 1000,
|
||||
"30d": 30 * 24 * 60 * 60 * 1000,
|
||||
};
|
||||
|
||||
function canManageFinancials(role?: string | null) {
|
||||
return role === "OWNER";
|
||||
}
|
||||
|
||||
function parseDate(input?: string | null) {
|
||||
if (!input) return null;
|
||||
const n = Number(input);
|
||||
if (!Number.isNaN(n)) return new Date(n);
|
||||
const d = new Date(input);
|
||||
return Number.isNaN(d.getTime()) ? null : d;
|
||||
}
|
||||
|
||||
function pickRange(req: NextRequest) {
|
||||
const url = new URL(req.url);
|
||||
const range = url.searchParams.get("range") ?? "7d";
|
||||
const now = new Date();
|
||||
|
||||
if (range === "custom") {
|
||||
const start = parseDate(url.searchParams.get("start")) ?? new Date(now.getTime() - RANGE_MS["24h"]);
|
||||
const end = parseDate(url.searchParams.get("end")) ?? now;
|
||||
return { start, end };
|
||||
}
|
||||
|
||||
const ms = RANGE_MS[range] ?? RANGE_MS["24h"];
|
||||
return { start: new Date(now.getTime() - ms), end: now };
|
||||
}
|
||||
|
||||
export async function GET(req: NextRequest) {
|
||||
const session = await requireSession();
|
||||
if (!session) return NextResponse.json({ ok: false, error: "Unauthorized" }, { status: 401 });
|
||||
|
||||
const membership = await prisma.orgUser.findUnique({
|
||||
where: { orgId_userId: { orgId: session.orgId, userId: session.userId } },
|
||||
select: { role: true },
|
||||
});
|
||||
if (!canManageFinancials(membership?.role)) {
|
||||
return NextResponse.json({ ok: false, error: "Forbidden" }, { status: 403 });
|
||||
}
|
||||
|
||||
const url = new URL(req.url);
|
||||
const { start, end } = pickRange(req);
|
||||
const machineId = url.searchParams.get("machineId") ?? undefined;
|
||||
const location = url.searchParams.get("location") ?? undefined;
|
||||
const sku = url.searchParams.get("sku") ?? undefined;
|
||||
const currency = url.searchParams.get("currency") ?? undefined;
|
||||
|
||||
const result = await computeFinancialImpact({
|
||||
orgId: session.orgId,
|
||||
start,
|
||||
end,
|
||||
machineId,
|
||||
location,
|
||||
sku,
|
||||
currency,
|
||||
includeEvents: false,
|
||||
});
|
||||
|
||||
return NextResponse.json({ ok: true, ...result });
|
||||
}
|
||||
7
app/api/health/route.ts
Normal file
7
app/api/health/route.ts
Normal file
@@ -0,0 +1,7 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import { logLine } from "@/lib/logger";
|
||||
|
||||
export async function GET() {
|
||||
logLine("health.hit", { ok: true });
|
||||
return NextResponse.json({ ok: true });
|
||||
}
|
||||
@@ -1,45 +1,132 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import { prisma } from "@/lib/prisma";
|
||||
import { getMachineAuth } from "@/lib/machineAuthCache";
|
||||
import { z } from "zod";
|
||||
|
||||
function asRecord(value: unknown): Record<string, unknown> | null {
|
||||
if (!value || typeof value !== "object" || Array.isArray(value)) return null;
|
||||
return value as Record<string, unknown>;
|
||||
}
|
||||
|
||||
function unwrapEnvelope(raw: unknown) {
|
||||
const record = asRecord(raw);
|
||||
if (!record) return raw;
|
||||
const payload = asRecord(record.payload);
|
||||
if (!payload) return raw;
|
||||
|
||||
const hasMeta =
|
||||
record.schemaVersion !== undefined ||
|
||||
record.machineId !== undefined ||
|
||||
record.tsMs !== undefined ||
|
||||
record.tsDevice !== undefined ||
|
||||
record.seq !== undefined ||
|
||||
record.type !== undefined;
|
||||
if (!hasMeta) return raw;
|
||||
|
||||
return {
|
||||
...payload,
|
||||
machineId: record.machineId ?? payload.machineId,
|
||||
tsMs: record.tsMs ?? payload.tsMs,
|
||||
tsDevice: record.tsDevice ?? payload.tsDevice,
|
||||
schemaVersion: record.schemaVersion ?? payload.schemaVersion,
|
||||
seq: record.seq ?? payload.seq,
|
||||
};
|
||||
}
|
||||
|
||||
const numberFromAny = z.preprocess((value) => {
|
||||
if (typeof value === "number") return value;
|
||||
if (typeof value === "string" && value.trim() !== "") return Number(value);
|
||||
return value;
|
||||
}, z.number().finite());
|
||||
|
||||
const intFromAny = z.preprocess((value) => {
|
||||
if (typeof value === "number") return Math.trunc(value);
|
||||
if (typeof value === "string" && value.trim() !== "") return Math.trunc(Number(value));
|
||||
return value;
|
||||
}, z.number().int().finite());
|
||||
|
||||
const machineIdSchema = z.string().uuid();
|
||||
|
||||
const cycleSchema = z
|
||||
.object({
|
||||
actual_cycle_time: numberFromAny,
|
||||
theoretical_cycle_time: numberFromAny.optional(),
|
||||
cycle_count: intFromAny.optional(),
|
||||
work_order_id: z.string().trim().max(64).optional(),
|
||||
sku: z.string().trim().max(64).optional(),
|
||||
cavities: intFromAny.optional(),
|
||||
good_delta: intFromAny.optional(),
|
||||
scrap_delta: intFromAny.optional(),
|
||||
timestamp: numberFromAny.optional(),
|
||||
ts: numberFromAny.optional(),
|
||||
event_timestamp: numberFromAny.optional(),
|
||||
})
|
||||
.passthrough();
|
||||
|
||||
export async function POST(req: Request) {
|
||||
const apiKey = req.headers.get("x-api-key");
|
||||
if (!apiKey) return NextResponse.json({ ok: false, error: "Missing api key" }, { status: 401 });
|
||||
|
||||
const body = await req.json().catch(() => null);
|
||||
if (!body?.machineId || !body?.cycle) {
|
||||
let body: unknown = await req.json().catch(() => null);
|
||||
body = unwrapEnvelope(body);
|
||||
const bodyRecord = asRecord(body) ?? {};
|
||||
|
||||
const machineId =
|
||||
bodyRecord.machineId ??
|
||||
bodyRecord.machine_id ??
|
||||
(asRecord(bodyRecord.machine)?.id ?? null);
|
||||
if (!machineId || !machineIdSchema.safeParse(String(machineId)).success) {
|
||||
return NextResponse.json({ ok: false, error: "Invalid payload" }, { status: 400 });
|
||||
}
|
||||
|
||||
const machine = await prisma.machine.findFirst({
|
||||
where: { id: String(body.machineId), apiKey },
|
||||
select: { id: true, orgId: true },
|
||||
});
|
||||
const machine = await getMachineAuth(String(machineId), apiKey);
|
||||
if (!machine) return NextResponse.json({ ok: false, error: "Unauthorized" }, { status: 401 });
|
||||
|
||||
const c = body.cycle;
|
||||
const cyclesRaw = bodyRecord.cycles ?? bodyRecord.cycle;
|
||||
if (!cyclesRaw) {
|
||||
return NextResponse.json({ ok: false, error: "Invalid payload" }, { status: 400 });
|
||||
}
|
||||
|
||||
const tsMs =
|
||||
(typeof c.timestamp === "number" && c.timestamp) ||
|
||||
(typeof c.ts === "number" && c.ts) ||
|
||||
(typeof c.event_timestamp === "number" && c.event_timestamp) ||
|
||||
const cycleList = Array.isArray(cyclesRaw) ? cyclesRaw : [cyclesRaw];
|
||||
const parsedCycles = z.array(cycleSchema).safeParse(cycleList);
|
||||
if (!parsedCycles.success) {
|
||||
return NextResponse.json({ ok: false, error: "Invalid payload" }, { status: 400 });
|
||||
}
|
||||
|
||||
const fallbackTsMs =
|
||||
(typeof bodyRecord.tsMs === "number" && bodyRecord.tsMs) ||
|
||||
(typeof bodyRecord.tsDevice === "number" && bodyRecord.tsDevice) ||
|
||||
undefined;
|
||||
|
||||
const rows = parsedCycles.data.map((data) => {
|
||||
const tsMs =
|
||||
(typeof data.timestamp === "number" && data.timestamp) ||
|
||||
(typeof data.ts === "number" && data.ts) ||
|
||||
(typeof data.event_timestamp === "number" && data.event_timestamp) ||
|
||||
fallbackTsMs;
|
||||
|
||||
const ts = tsMs ? new Date(tsMs) : new Date();
|
||||
|
||||
const row = await prisma.machineCycle.create({
|
||||
data: {
|
||||
return {
|
||||
orgId: machine.orgId,
|
||||
machineId: machine.id,
|
||||
ts,
|
||||
cycleCount: typeof c.cycle_count === "number" ? c.cycle_count : null,
|
||||
actualCycleTime: Number(c.actual_cycle_time),
|
||||
theoreticalCycleTime: c.theoretical_cycle_time != null ? Number(c.theoretical_cycle_time) : null,
|
||||
workOrderId: c.work_order_id ? String(c.work_order_id) : null,
|
||||
sku: c.sku ? String(c.sku) : null,
|
||||
cavities: typeof c.cavities === "number" ? c.cavities : null,
|
||||
goodDelta: typeof c.good_delta === "number" ? c.good_delta : null,
|
||||
scrapDelta: typeof c.scrap_delta === "number" ? c.scrap_delta : null,
|
||||
},
|
||||
cycleCount: typeof data.cycle_count === "number" ? data.cycle_count : null,
|
||||
actualCycleTime: data.actual_cycle_time,
|
||||
theoreticalCycleTime: typeof data.theoretical_cycle_time === "number" ? data.theoretical_cycle_time : null,
|
||||
workOrderId: data.work_order_id ? String(data.work_order_id) : null,
|
||||
sku: data.sku ? String(data.sku) : null,
|
||||
cavities: typeof data.cavities === "number" ? data.cavities : null,
|
||||
goodDelta: typeof data.good_delta === "number" ? data.good_delta : null,
|
||||
scrapDelta: typeof data.scrap_delta === "number" ? data.scrap_delta : null,
|
||||
};
|
||||
});
|
||||
|
||||
if (rows.length === 1) {
|
||||
const row = await prisma.machineCycle.create({ data: rows[0] });
|
||||
return NextResponse.json({ ok: true, id: row.id, ts: row.ts });
|
||||
}
|
||||
|
||||
const result = await prisma.machineCycle.createMany({ data: rows });
|
||||
return NextResponse.json({ ok: true, count: result.count });
|
||||
}
|
||||
|
||||
@@ -1,12 +1,21 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import { prisma } from "@/lib/prisma";
|
||||
import { getMachineAuth } from "@/lib/machineAuthCache";
|
||||
import { z } from "zod";
|
||||
import { evaluateAlertsForEvent } from "@/lib/alerts/engine";
|
||||
import { toJsonValue } from "@/lib/prismaJson";
|
||||
|
||||
const normalizeType = (t: any) =>
|
||||
const normalizeType = (t: unknown) =>
|
||||
String(t ?? "")
|
||||
.trim()
|
||||
.toLowerCase()
|
||||
.replace(/_/g, "-");
|
||||
|
||||
function asRecord(value: unknown): Record<string, unknown> | null {
|
||||
if (!value || typeof value !== "object" || Array.isArray(value)) return null;
|
||||
return value as Record<string, unknown>;
|
||||
}
|
||||
|
||||
const CANON_TYPE: Record<string, string> = {
|
||||
// Node-RED
|
||||
"production-stopped": "stop",
|
||||
@@ -27,76 +36,142 @@ const ALLOWED_TYPES = new Set([
|
||||
"slow-cycle",
|
||||
"microstop",
|
||||
"macrostop",
|
||||
"offline",
|
||||
"error",
|
||||
"oee-drop",
|
||||
"quality-spike",
|
||||
"performance-degradation",
|
||||
"predictive-oee-decline",
|
||||
]);
|
||||
|
||||
// thresholds for stop classification (tune later / move to machine config)
|
||||
const MICROSTOP_SEC = 60;
|
||||
const MACROSTOP_SEC = 300;
|
||||
const machineIdSchema = z.string().uuid();
|
||||
const MAX_EVENTS = 100;
|
||||
|
||||
//when no cycle time is configed
|
||||
const DEFAULT_MACROSTOP_SEC = 300;
|
||||
|
||||
|
||||
function clampText(value: unknown, maxLen: number) {
|
||||
if (value === null || value === undefined) return null;
|
||||
const text = String(value).trim().replace(/[\u0000-\u001f\u007f]/g, "");
|
||||
if (!text) return null;
|
||||
return text.length > maxLen ? text.slice(0, maxLen) : text;
|
||||
}
|
||||
|
||||
export async function POST(req: Request) {
|
||||
const apiKey = req.headers.get("x-api-key");
|
||||
if (!apiKey) {
|
||||
return NextResponse.json({ ok: false, error: "Missing api key" }, { status: 401 });
|
||||
if (!apiKey) return NextResponse.json({ ok: false, error: "Missing api key" }, { status: 401 });
|
||||
|
||||
let body: unknown = await req.json().catch(() => null);
|
||||
|
||||
// ✅ if Node-RED sent an array as the whole body, unwrap it
|
||||
if (Array.isArray(body)) body = body[0];
|
||||
const bodyRecord = asRecord(body) ?? {};
|
||||
const payloadRecord = asRecord(bodyRecord.payload) ?? {};
|
||||
|
||||
// ✅ accept multiple common keys
|
||||
const machineId =
|
||||
bodyRecord.machineId ??
|
||||
bodyRecord.machine_id ??
|
||||
(asRecord(bodyRecord.machine)?.id ?? null);
|
||||
let rawEvent =
|
||||
bodyRecord.event ??
|
||||
bodyRecord.events ??
|
||||
bodyRecord.anomalies ??
|
||||
payloadRecord.event ??
|
||||
payloadRecord.events ??
|
||||
payloadRecord.anomalies ??
|
||||
payloadRecord ??
|
||||
bodyRecord.data; // sometimes "data"
|
||||
|
||||
const rawEventRecord = asRecord(rawEvent);
|
||||
if (rawEventRecord?.event && typeof rawEventRecord.event === "object") rawEvent = rawEventRecord.event;
|
||||
if (Array.isArray(rawEventRecord?.events)) rawEvent = rawEventRecord.events;
|
||||
|
||||
if (!machineId || !rawEvent) {
|
||||
return NextResponse.json(
|
||||
{ ok: false, error: "Invalid payload", got: { hasMachineId: !!machineId, keys: Object.keys(bodyRecord) } },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
const body = await req.json().catch(() => null);
|
||||
if (!body?.machineId || !body?.event) {
|
||||
return NextResponse.json({ ok: false, error: "Invalid payload" }, { status: 400 });
|
||||
if (!machineIdSchema.safeParse(String(machineId)).success) {
|
||||
return NextResponse.json({ ok: false, error: "Invalid machine id" }, { status: 400 });
|
||||
}
|
||||
|
||||
|
||||
const machine = await prisma.machine.findFirst({
|
||||
where: { id: String(body.machineId), apiKey },
|
||||
select: { id: true, orgId: true },
|
||||
const machine = await getMachineAuth(String(machineId), apiKey);
|
||||
if (!machine) return NextResponse.json({ ok: false, error: "Unauthorized" }, { status: 401 });
|
||||
const orgSettings = await prisma.orgSettings.findUnique({
|
||||
where: { orgId: machine.orgId },
|
||||
select: { stoppageMultiplier: true, macroStoppageMultiplier: true },
|
||||
});
|
||||
if (!machine) {
|
||||
return NextResponse.json({ ok: false, error: "Unauthorized" }, { status: 401 });
|
||||
}
|
||||
|
||||
// Normalize to array (Node-RED sends array of anomalies)
|
||||
const rawEvent = body.event;
|
||||
const defaultMicroMultiplier = Number(orgSettings?.stoppageMultiplier ?? 1.5);
|
||||
const defaultMacroMultiplier = Math.max(
|
||||
defaultMicroMultiplier,
|
||||
Number(orgSettings?.macroStoppageMultiplier ?? 5)
|
||||
);
|
||||
|
||||
|
||||
// ✅ normalize to array no matter what
|
||||
const events = Array.isArray(rawEvent) ? rawEvent : [rawEvent];
|
||||
if (events.length > MAX_EVENTS) {
|
||||
return NextResponse.json({ ok: false, error: "Too many events" }, { status: 400 });
|
||||
}
|
||||
|
||||
const created: { id: string; ts: Date; eventType: string }[] = [];
|
||||
const skipped: any[] = [];
|
||||
const skipped: Array<Record<string, unknown>> = [];
|
||||
|
||||
for (const ev of events) {
|
||||
if (!ev || typeof ev !== "object") {
|
||||
const evRecord = asRecord(ev);
|
||||
if (!evRecord) {
|
||||
skipped.push({ reason: "invalid_event_object" });
|
||||
continue;
|
||||
}
|
||||
const evData = asRecord(evRecord.data) ?? {};
|
||||
|
||||
const rawType = (ev as any).eventType ?? (ev as any).anomaly_type ?? (ev as any).topic ?? body.topic ?? "";
|
||||
const rawType = evRecord.eventType ?? evRecord.anomaly_type ?? evRecord.topic ?? bodyRecord.topic ?? "";
|
||||
const typ0 = normalizeType(rawType);
|
||||
const typ = CANON_TYPE[typ0] ?? typ0;
|
||||
|
||||
// Determine timestamp
|
||||
const tsMs =
|
||||
(typeof (ev as any)?.timestamp === "number" && (ev as any).timestamp) ||
|
||||
(typeof (ev as any)?.data?.timestamp === "number" && (ev as any).data.timestamp) ||
|
||||
(typeof (ev as any)?.data?.event_timestamp === "number" && (ev as any).data.event_timestamp) ||
|
||||
(typeof evRecord.timestamp === "number" && evRecord.timestamp) ||
|
||||
(typeof evData.timestamp === "number" && evData.timestamp) ||
|
||||
(typeof evData.event_timestamp === "number" && evData.event_timestamp) ||
|
||||
null;
|
||||
|
||||
const ts = tsMs ? new Date(tsMs) : new Date();
|
||||
|
||||
// Severity defaulting (do not skip on severity — store for audit)
|
||||
let sev = String((ev as any).severity ?? "").trim().toLowerCase();
|
||||
let sev = String(evRecord.severity ?? "").trim().toLowerCase();
|
||||
if (!sev) sev = "warning";
|
||||
|
||||
// Stop classification -> microstop/macrostop
|
||||
let finalType = typ;
|
||||
if (typ === "stop") {
|
||||
const stopSec =
|
||||
(typeof (ev as any)?.data?.stoppage_duration_seconds === "number" && (ev as any).data.stoppage_duration_seconds) ||
|
||||
(typeof (ev as any)?.data?.stop_duration_seconds === "number" && (ev as any).data.stop_duration_seconds) ||
|
||||
(typeof evData.stoppage_duration_seconds === "number" && evData.stoppage_duration_seconds) ||
|
||||
(typeof evData.stop_duration_seconds === "number" && evData.stop_duration_seconds) ||
|
||||
null;
|
||||
|
||||
if (stopSec != null) {
|
||||
finalType = stopSec >= MACROSTOP_SEC ? "macrostop" : "microstop";
|
||||
const theoretical = Number(evData.theoretical_cycle_time ?? evData.theoreticalCycleTime ?? 0) || 0;
|
||||
|
||||
const microMultiplier = Number(
|
||||
evData.micro_threshold_multiplier ?? evData.threshold_multiplier ?? defaultMicroMultiplier
|
||||
);
|
||||
const macroMultiplier = Math.max(
|
||||
microMultiplier,
|
||||
Number(evData.macro_threshold_multiplier ?? defaultMacroMultiplier)
|
||||
);
|
||||
|
||||
if (theoretical > 0) {
|
||||
const macroThresholdSec = theoretical * macroMultiplier;
|
||||
finalType = stopSec >= macroThresholdSec ? "macrostop" : "microstop";
|
||||
} else {
|
||||
finalType = stopSec >= DEFAULT_MACROSTOP_SEC ? "macrostop" : "microstop";
|
||||
}
|
||||
} else {
|
||||
// missing duration -> conservative
|
||||
finalType = "microstop";
|
||||
@@ -109,44 +184,71 @@ export async function POST(req: Request) {
|
||||
}
|
||||
|
||||
const title =
|
||||
String((ev as any).title ?? "").trim() ||
|
||||
clampText(evRecord.title, 160) ||
|
||||
(finalType === "slow-cycle" ? "Slow Cycle Detected" :
|
||||
finalType === "macrostop" ? "Macrostop Detected" :
|
||||
finalType === "microstop" ? "Microstop Detected" :
|
||||
"Event");
|
||||
|
||||
const description = (ev as any).description ? String((ev as any).description) : null;
|
||||
const description = clampText(evRecord.description, 1000);
|
||||
|
||||
// store full blob, ensure object
|
||||
const rawData = (ev as any).data ?? ev;
|
||||
const dataObj = typeof rawData === "string" ? (() => {
|
||||
try { return JSON.parse(rawData); } catch { return { raw: rawData }; }
|
||||
})() : rawData;
|
||||
const rawData = evRecord.data ?? evRecord;
|
||||
const parsedData = typeof rawData === "string"
|
||||
? (() => {
|
||||
try {
|
||||
return JSON.parse(rawData);
|
||||
} catch {
|
||||
return { raw: rawData };
|
||||
}
|
||||
})()
|
||||
: rawData;
|
||||
const dataObj: Record<string, unknown> =
|
||||
parsedData && typeof parsedData === "object" && !Array.isArray(parsedData)
|
||||
? { ...(parsedData as Record<string, unknown>) }
|
||||
: { raw: parsedData };
|
||||
if (evRecord.status != null && dataObj.status == null) dataObj.status = evRecord.status;
|
||||
if (evRecord.alert_id != null && dataObj.alert_id == null) dataObj.alert_id = evRecord.alert_id;
|
||||
if (evRecord.is_update != null && dataObj.is_update == null) dataObj.is_update = evRecord.is_update;
|
||||
if (evRecord.is_auto_ack != null && dataObj.is_auto_ack == null) dataObj.is_auto_ack = evRecord.is_auto_ack;
|
||||
|
||||
const activeWorkOrder = asRecord(evRecord.activeWorkOrder);
|
||||
const dataActiveWorkOrder = asRecord(evData.activeWorkOrder);
|
||||
|
||||
const row = await prisma.machineEvent.create({
|
||||
data: {
|
||||
orgId: machine.orgId,
|
||||
machineId: machine.id,
|
||||
ts,
|
||||
topic: String((ev as any).topic ?? finalType),
|
||||
topic: clampText(evRecord.topic ?? finalType, 64) ?? finalType,
|
||||
eventType: finalType,
|
||||
severity: sev,
|
||||
requiresAck: !!(ev as any).requires_ack,
|
||||
requiresAck: !!evRecord.requires_ack,
|
||||
title,
|
||||
description,
|
||||
data: dataObj,
|
||||
data: toJsonValue(dataObj),
|
||||
workOrderId:
|
||||
(ev as any)?.work_order_id ? String((ev as any).work_order_id)
|
||||
: (ev as any)?.data?.work_order_id ? String((ev as any).data.work_order_id)
|
||||
: null,
|
||||
clampText(evRecord.work_order_id, 64) ??
|
||||
clampText(evData.work_order_id, 64) ??
|
||||
clampText(activeWorkOrder?.id, 64) ??
|
||||
clampText(dataActiveWorkOrder?.id, 64) ??
|
||||
null,
|
||||
sku:
|
||||
(ev as any)?.sku ? String((ev as any).sku)
|
||||
: (ev as any)?.data?.sku ? String((ev as any).data.sku)
|
||||
: null,
|
||||
clampText(evRecord.sku, 64) ??
|
||||
clampText(evData.sku, 64) ??
|
||||
clampText(activeWorkOrder?.sku, 64) ??
|
||||
clampText(dataActiveWorkOrder?.sku, 64) ??
|
||||
null,
|
||||
},
|
||||
});
|
||||
|
||||
created.push({ id: row.id, ts: row.ts, eventType: row.eventType });
|
||||
|
||||
try {
|
||||
await evaluateAlertsForEvent(row.id);
|
||||
} catch (err) {
|
||||
console.error("[alerts] evaluation failed", err);
|
||||
}
|
||||
}
|
||||
|
||||
return NextResponse.json({ ok: true, createdCount: created.length, created, skippedCount: skipped.length, skipped });
|
||||
|
||||
@@ -1,32 +1,161 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import { prisma } from "@/lib/prisma";
|
||||
import { getMachineAuth } from "@/lib/machineAuthCache";
|
||||
import { normalizeHeartbeatV1 } from "@/lib/contracts/v1";
|
||||
import { toJsonValue } from "@/lib/prismaJson";
|
||||
|
||||
export async function POST(req: Request) {
|
||||
const apiKey = req.headers.get("x-api-key");
|
||||
if (!apiKey) return NextResponse.json({ ok: false, error: "Missing api key" }, { status: 401 });
|
||||
|
||||
const body = await req.json().catch(() => null);
|
||||
if (!body?.machineId || !body?.status) {
|
||||
return NextResponse.json({ ok: false, error: "Invalid payload" }, { status: 400 });
|
||||
function getClientIp(req: Request) {
|
||||
const xf = req.headers.get("x-forwarded-for");
|
||||
if (xf) return xf.split(",")[0]?.trim() || null;
|
||||
return req.headers.get("x-real-ip") || null;
|
||||
}
|
||||
|
||||
const machine = await prisma.machine.findFirst({
|
||||
where: { id: String(body.machineId), apiKey },
|
||||
select: { id: true, orgId: true },
|
||||
function parseSeqToBigInt(seq: unknown): bigint | null {
|
||||
if (seq === null || seq === undefined) return null;
|
||||
if (typeof seq === "number") {
|
||||
if (!Number.isInteger(seq) || seq < 0) return null;
|
||||
return BigInt(seq);
|
||||
}
|
||||
if (typeof seq === "string" && /^\d+$/.test(seq)) return BigInt(seq);
|
||||
return null;
|
||||
}
|
||||
|
||||
export async function POST(req: Request) {
|
||||
const endpoint = "/api/ingest/heartbeat";
|
||||
const ip = getClientIp(req);
|
||||
const userAgent = req.headers.get("user-agent");
|
||||
|
||||
let rawBody: unknown = null;
|
||||
let orgId: string | null = null;
|
||||
let machineId: string | null = null;
|
||||
let seq: bigint | null = null;
|
||||
let schemaVersion: string | null = null;
|
||||
let tsDeviceDate: Date | null = null;
|
||||
|
||||
try {
|
||||
// 1) Auth header exists
|
||||
const apiKey = req.headers.get("x-api-key");
|
||||
if (!apiKey) {
|
||||
await prisma.ingestLog.create({
|
||||
data: { endpoint, ok: false, status: 401, errorCode: "MISSING_API_KEY", errorMsg: "Missing api key", ip, userAgent },
|
||||
});
|
||||
return NextResponse.json({ ok: false, error: "Missing api key" }, { status: 401 });
|
||||
}
|
||||
|
||||
if (!machine) return NextResponse.json({ ok: false, error: "Unauthorized" }, { status: 401 });
|
||||
// 2) Parse JSON
|
||||
rawBody = await req.json().catch(() => null);
|
||||
|
||||
// 3) Normalize to v1 (legacy tolerated)
|
||||
const normalized = normalizeHeartbeatV1(rawBody);
|
||||
if (!normalized.ok) {
|
||||
await prisma.ingestLog.create({
|
||||
data: {
|
||||
endpoint,
|
||||
ok: false,
|
||||
status: 400,
|
||||
errorCode: "INVALID_PAYLOAD",
|
||||
errorMsg: normalized.error,
|
||||
body: toJsonValue(rawBody),
|
||||
ip,
|
||||
userAgent,
|
||||
},
|
||||
});
|
||||
return NextResponse.json({ ok: false, error: "Invalid payload", detail: normalized.error }, { status: 400 });
|
||||
}
|
||||
|
||||
const body = normalized.value;
|
||||
schemaVersion = body.schemaVersion;
|
||||
machineId = body.machineId;
|
||||
seq = parseSeqToBigInt(body.seq);
|
||||
tsDeviceDate = new Date(body.tsDevice);
|
||||
|
||||
// 4) Authorize machineId + apiKey
|
||||
const machine = await getMachineAuth(machineId, apiKey);
|
||||
|
||||
if (!machine) {
|
||||
await prisma.ingestLog.create({
|
||||
data: {
|
||||
endpoint,
|
||||
ok: false,
|
||||
status: 401,
|
||||
errorCode: "UNAUTHORIZED",
|
||||
errorMsg: "Unauthorized (machineId/apiKey mismatch)",
|
||||
body: toJsonValue(rawBody),
|
||||
machineId,
|
||||
schemaVersion,
|
||||
seq,
|
||||
tsDevice: tsDeviceDate,
|
||||
ip,
|
||||
userAgent,
|
||||
},
|
||||
});
|
||||
return NextResponse.json({ ok: false, error: "Unauthorized" }, { status: 401 });
|
||||
}
|
||||
|
||||
orgId = machine.orgId;
|
||||
|
||||
// 5) Store heartbeat
|
||||
// Keep your legacy fields, but store meta fields too.
|
||||
const tsServerNow = new Date();
|
||||
const hb = await prisma.machineHeartbeat.create({
|
||||
data: {
|
||||
orgId: machine.orgId,
|
||||
orgId,
|
||||
machineId: machine.id,
|
||||
status: String(body.status),
|
||||
|
||||
// Phase 0 meta
|
||||
schemaVersion,
|
||||
seq,
|
||||
ts: tsDeviceDate,
|
||||
tsServer: tsServerNow,
|
||||
|
||||
// Legacy payload compatibility
|
||||
status: body.status ? String(body.status) : (body.online ? "RUN" : "STOP"),
|
||||
message: body.message ? String(body.message) : null,
|
||||
ip: body.ip ? String(body.ip) : null,
|
||||
fwVersion: body.fwVersion ? String(body.fwVersion) : null,
|
||||
},
|
||||
});
|
||||
|
||||
return NextResponse.json({ ok: true, id: hb.id, ts: hb.ts });
|
||||
// Optional: update machine last seen (same as KPI)
|
||||
await prisma.machine.update({
|
||||
where: { id: machine.id },
|
||||
data: {
|
||||
schemaVersion,
|
||||
seq,
|
||||
tsDevice: tsDeviceDate,
|
||||
tsServer: tsServerNow,
|
||||
},
|
||||
});
|
||||
|
||||
return NextResponse.json({
|
||||
ok: true,
|
||||
id: hb.id,
|
||||
tsDevice: hb.ts,
|
||||
tsServer: hb.tsServer,
|
||||
});
|
||||
} catch (err: unknown) {
|
||||
const msg = err instanceof Error ? err.message : "Unknown error";
|
||||
|
||||
try {
|
||||
await prisma.ingestLog.create({
|
||||
data: {
|
||||
orgId,
|
||||
machineId,
|
||||
endpoint,
|
||||
ok: false,
|
||||
status: 500,
|
||||
errorCode: "SERVER_ERROR",
|
||||
errorMsg: msg,
|
||||
schemaVersion,
|
||||
seq,
|
||||
tsDevice: tsDeviceDate ?? undefined,
|
||||
body: toJsonValue(rawBody),
|
||||
ip,
|
||||
userAgent,
|
||||
},
|
||||
});
|
||||
} catch {}
|
||||
|
||||
return NextResponse.json({ ok: false, error: "Server error", detail: msg }, { status: 500 });
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,41 +1,166 @@
|
||||
// mis-control-tower/app/api/ingest/kpi/route.ts
|
||||
import { NextResponse } from "next/server";
|
||||
import { prisma } from "@/lib/prisma";
|
||||
import { getMachineAuth } from "@/lib/machineAuthCache";
|
||||
import { normalizeSnapshotV1 } from "@/lib/contracts/v1";
|
||||
import { toJsonValue } from "@/lib/prismaJson";
|
||||
|
||||
export async function POST(req: Request) {
|
||||
const apiKey = req.headers.get("x-api-key");
|
||||
if (!apiKey) return NextResponse.json({ ok: false, error: "Missing api key" }, { status: 401 });
|
||||
|
||||
const body = await req.json().catch(() => null);
|
||||
if (!body?.machineId || !body?.kpis) {
|
||||
return NextResponse.json({ ok: false, error: "Invalid payload" }, { status: 400 });
|
||||
function getClientIp(req: Request) {
|
||||
const xf = req.headers.get("x-forwarded-for");
|
||||
if (xf) return xf.split(",")[0]?.trim() || null;
|
||||
return req.headers.get("x-real-ip") || null;
|
||||
}
|
||||
|
||||
const machine = await prisma.machine.findFirst({
|
||||
where: { id: String(body.machineId), apiKey },
|
||||
select: { id: true, orgId: true },
|
||||
function parseSeqToBigInt(seq: unknown): bigint | null {
|
||||
if (seq === null || seq === undefined) return null;
|
||||
if (typeof seq === "number") {
|
||||
if (!Number.isInteger(seq) || seq < 0) return null;
|
||||
return BigInt(seq);
|
||||
}
|
||||
if (typeof seq === "string" && /^\d+$/.test(seq)) return BigInt(seq);
|
||||
return null;
|
||||
}
|
||||
|
||||
export async function POST(req: Request) {
|
||||
const endpoint = "/api/ingest/kpi";
|
||||
const startedAt = Date.now();
|
||||
const ip = getClientIp(req);
|
||||
const userAgent = req.headers.get("user-agent");
|
||||
|
||||
let rawBody: unknown = null;
|
||||
let orgId: string | null = null;
|
||||
let machineId: string | null = null;
|
||||
let seq: bigint | null = null;
|
||||
let schemaVersion: string | null = null;
|
||||
let tsDeviceDate: Date | null = null;
|
||||
|
||||
try {
|
||||
const apiKey = req.headers.get("x-api-key");
|
||||
if (!apiKey) {
|
||||
await prisma.ingestLog.create({
|
||||
data: {
|
||||
endpoint,
|
||||
ok: false,
|
||||
status: 401,
|
||||
errorCode: "MISSING_API_KEY",
|
||||
errorMsg: "Missing api key",
|
||||
ip,
|
||||
userAgent,
|
||||
},
|
||||
});
|
||||
if (!machine) return NextResponse.json({ ok: false, error: "Unauthorized" }, { status: 401 });
|
||||
return NextResponse.json({ ok: false, error: "Missing api key" }, { status: 401 });
|
||||
}
|
||||
|
||||
const wo = body.activeWorkOrder ?? {};
|
||||
rawBody = await req.json().catch(() => null);
|
||||
const normalized = normalizeSnapshotV1(rawBody);
|
||||
if (!normalized.ok) {
|
||||
await prisma.ingestLog.create({
|
||||
data: {
|
||||
endpoint,
|
||||
ok: false,
|
||||
status: 400,
|
||||
errorCode: "INVALID_PAYLOAD",
|
||||
errorMsg: normalized.error,
|
||||
body: toJsonValue(rawBody),
|
||||
ip,
|
||||
userAgent,
|
||||
},
|
||||
});
|
||||
return NextResponse.json({ ok: false, error: "Invalid payload", detail: normalized.error }, { status: 400 });
|
||||
}
|
||||
|
||||
const body = normalized.value;
|
||||
|
||||
schemaVersion = body.schemaVersion;
|
||||
machineId = body.machineId;
|
||||
seq = parseSeqToBigInt(body.seq);
|
||||
tsDeviceDate = new Date(body.tsDevice);
|
||||
|
||||
// Auth: machineId + apiKey must match
|
||||
const machine = await getMachineAuth(machineId, apiKey);
|
||||
|
||||
if (!machine) {
|
||||
await prisma.ingestLog.create({
|
||||
data: {
|
||||
endpoint,
|
||||
ok: false,
|
||||
status: 401,
|
||||
errorCode: "UNAUTHORIZED",
|
||||
errorMsg: "Unauthorized (machineId/apiKey mismatch)",
|
||||
body: toJsonValue(rawBody),
|
||||
machineId,
|
||||
schemaVersion,
|
||||
seq,
|
||||
tsDevice: tsDeviceDate,
|
||||
ip,
|
||||
userAgent,
|
||||
},
|
||||
});
|
||||
return NextResponse.json({ ok: false, error: "Unauthorized" }, { status: 401 });
|
||||
}
|
||||
|
||||
orgId = machine.orgId;
|
||||
|
||||
const woRecord = (body.activeWorkOrder ?? {}) as Record<string, unknown>;
|
||||
const good =
|
||||
typeof woRecord.good === "number"
|
||||
? woRecord.good
|
||||
: typeof woRecord.goodParts === "number"
|
||||
? woRecord.goodParts
|
||||
: typeof woRecord.good_parts === "number"
|
||||
? woRecord.good_parts
|
||||
: null;
|
||||
const scrap =
|
||||
typeof woRecord.scrap === "number"
|
||||
? woRecord.scrap
|
||||
: typeof woRecord.scrapParts === "number"
|
||||
? woRecord.scrapParts
|
||||
: typeof woRecord.scrap_parts === "number"
|
||||
? woRecord.scrap_parts
|
||||
: null;
|
||||
const k = body.kpis ?? {};
|
||||
const safeCycleTime =
|
||||
typeof body.cycleTime === "number" && body.cycleTime > 0
|
||||
? body.cycleTime
|
||||
: typeof woRecord.cycleTime === "number" && woRecord.cycleTime > 0
|
||||
? woRecord.cycleTime
|
||||
: null;
|
||||
|
||||
const safeCavities =
|
||||
typeof body.cavities === "number" && body.cavities > 0
|
||||
? body.cavities
|
||||
: typeof woRecord.cavities === "number" && woRecord.cavities > 0
|
||||
? woRecord.cavities
|
||||
: null;
|
||||
// Write snapshot (ts = tsDevice; tsServer auto)
|
||||
const row = await prisma.machineKpiSnapshot.create({
|
||||
data: {
|
||||
orgId: machine.orgId,
|
||||
orgId,
|
||||
machineId: machine.id,
|
||||
|
||||
workOrderId: wo.id ? String(wo.id) : null,
|
||||
sku: wo.sku ? String(wo.sku) : null,
|
||||
// Phase 0 meta
|
||||
schemaVersion,
|
||||
seq,
|
||||
ts: tsDeviceDate, // store device-time in ts; server-time goes to ts_server
|
||||
|
||||
target: typeof wo.target === "number" ? wo.target : null,
|
||||
good: typeof wo.good === "number" ? wo.good : null,
|
||||
scrap: typeof wo.scrap === "number" ? wo.scrap : null,
|
||||
// Work order fields
|
||||
workOrderId: woRecord.id != null ? String(woRecord.id) : null,
|
||||
sku: woRecord.sku != null ? String(woRecord.sku) : null,
|
||||
target: typeof woRecord.target === "number" ? Math.trunc(woRecord.target) : null,
|
||||
good: good != null ? Math.trunc(good) : null,
|
||||
scrap: scrap != null ? Math.trunc(scrap) : null,
|
||||
|
||||
// Counters
|
||||
cycleCount: typeof body.cycle_count === "number" ? body.cycle_count : null,
|
||||
goodParts: typeof body.good_parts === "number" ? body.good_parts : null,
|
||||
scrapParts: typeof body.scrap_parts === "number" ? body.scrap_parts : null,
|
||||
cavities: safeCavities,
|
||||
|
||||
cycleTime: typeof body.cycleTime === "number" ? body.cycleTime : null,
|
||||
// Cycle times
|
||||
cycleTime: safeCycleTime,
|
||||
actualCycle: typeof body.actualCycleTime === "number" ? body.actualCycleTime : null,
|
||||
|
||||
// KPIs (0..100)
|
||||
availability: typeof k.availability === "number" ? k.availability : null,
|
||||
performance: typeof k.performance === "number" ? k.performance : null,
|
||||
quality: typeof k.quality === "number" ? k.quality : null,
|
||||
@@ -46,5 +171,50 @@ export async function POST(req: Request) {
|
||||
},
|
||||
});
|
||||
|
||||
return NextResponse.json({ ok: true, id: row.id, ts: row.ts });
|
||||
// Optional but useful: update machine "last seen" meta fields
|
||||
await prisma.machine.update({
|
||||
where: { id: machine.id },
|
||||
data: {
|
||||
schemaVersion,
|
||||
seq,
|
||||
tsDevice: tsDeviceDate,
|
||||
tsServer: new Date(),
|
||||
},
|
||||
});
|
||||
|
||||
return NextResponse.json({
|
||||
ok: true,
|
||||
id: row.id,
|
||||
tsDevice: row.ts,
|
||||
tsServer: row.tsServer,
|
||||
});
|
||||
} catch (err: unknown) {
|
||||
const msg = err instanceof Error ? err.message : "Unknown error";
|
||||
|
||||
// Never fail the request because logging failed
|
||||
try {
|
||||
await prisma.ingestLog.create({
|
||||
data: {
|
||||
orgId,
|
||||
machineId,
|
||||
endpoint,
|
||||
ok: false,
|
||||
status: 500,
|
||||
errorCode: "SERVER_ERROR",
|
||||
errorMsg: msg,
|
||||
schemaVersion,
|
||||
seq,
|
||||
tsDevice: tsDeviceDate ?? undefined,
|
||||
body: toJsonValue(rawBody),
|
||||
ip,
|
||||
userAgent,
|
||||
},
|
||||
});
|
||||
} catch {}
|
||||
|
||||
return NextResponse.json({ ok: false, error: "Server error", detail: msg }, { status: 500 });
|
||||
} finally {
|
||||
// (If later you add latency_ms to IngestLog, you can store Date.now() - startedAt here.)
|
||||
void startedAt;
|
||||
}
|
||||
}
|
||||
|
||||
150
app/api/ingest/reason/route.ts
Normal file
150
app/api/ingest/reason/route.ts
Normal file
@@ -0,0 +1,150 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import { prisma } from "@/lib/prisma";
|
||||
|
||||
const bad = (status: number, error: string) =>
|
||||
NextResponse.json({ ok: false, error }, { status });
|
||||
|
||||
const asTrimmedString = (v: any) => {
|
||||
if (v == null) return "";
|
||||
return String(v).trim();
|
||||
};
|
||||
|
||||
export async function POST(req: Request) {
|
||||
const apiKey = req.headers.get("x-api-key");
|
||||
if (!apiKey) return bad(401, "Missing api key");
|
||||
|
||||
const body = await req.json().catch(() => null);
|
||||
if (!body?.machineId || !body?.reason) return bad(400, "Invalid payload");
|
||||
|
||||
const machine = await prisma.machine.findFirst({
|
||||
where: { id: String(body.machineId), apiKey },
|
||||
select: { id: true, orgId: true },
|
||||
});
|
||||
if (!machine) return bad(401, "Unauthorized");
|
||||
|
||||
const r = body.reason;
|
||||
|
||||
const reasonId = asTrimmedString(r.reasonId);
|
||||
if (!reasonId) return bad(400, "Missing reason.reasonId");
|
||||
|
||||
const kind = asTrimmedString(r.kind).toLowerCase();
|
||||
if (kind !== "downtime" && kind !== "scrap")
|
||||
return bad(400, "Invalid reason.kind");
|
||||
|
||||
const capturedAtMs = r.capturedAtMs;
|
||||
if (typeof capturedAtMs !== "number" || !Number.isFinite(capturedAtMs)) {
|
||||
return bad(400, "Invalid reason.capturedAtMs");
|
||||
}
|
||||
const capturedAt = new Date(capturedAtMs);
|
||||
|
||||
const reasonCodeRaw = asTrimmedString(r.reasonCode);
|
||||
if (!reasonCodeRaw) return bad(400, "Missing reason.reasonCode");
|
||||
const reasonCode = reasonCodeRaw.toUpperCase(); // normalize for grouping/pareto
|
||||
|
||||
const reasonLabel = r.reasonLabel != null ? String(r.reasonLabel) : null;
|
||||
|
||||
let reasonText = r.reasonText != null ? String(r.reasonText).trim() : null;
|
||||
if (reasonCode === "OTHER") {
|
||||
if (!reasonText || reasonText.length < 2)
|
||||
return bad(400, "reason.reasonText required when reasonCode=OTHER");
|
||||
} else {
|
||||
// Non-OTHER must not store free text
|
||||
reasonText = null;
|
||||
}
|
||||
|
||||
// Optional shared fields
|
||||
const workOrderId =
|
||||
r.workOrderId != null && String(r.workOrderId).trim()
|
||||
? String(r.workOrderId).trim()
|
||||
: null;
|
||||
|
||||
const schemaVersion =
|
||||
typeof r.schemaVersion === "number" && Number.isFinite(r.schemaVersion)
|
||||
? Math.trunc(r.schemaVersion)
|
||||
: 1;
|
||||
|
||||
const meta = r.meta != null ? r.meta : null;
|
||||
|
||||
// Kind-specific fields
|
||||
let episodeId: string | null = null;
|
||||
let durationSeconds: number | null = null;
|
||||
let episodeEndTs: Date | null = null;
|
||||
|
||||
let scrapEntryId: string | null = null;
|
||||
let scrapQty: number | null = null;
|
||||
let scrapUnit: string | null = null;
|
||||
|
||||
if (kind === "downtime") {
|
||||
episodeId = asTrimmedString(r.episodeId) || null;
|
||||
if (!episodeId) return bad(400, "Missing reason.episodeId for downtime");
|
||||
|
||||
if (typeof r.durationSeconds !== "number" || !Number.isFinite(r.durationSeconds)) {
|
||||
return bad(400, "Invalid reason.durationSeconds for downtime");
|
||||
}
|
||||
durationSeconds = Math.max(0, Math.trunc(r.durationSeconds));
|
||||
|
||||
const episodeEndTsMs = r.episodeEndTsMs;
|
||||
if (episodeEndTsMs != null) {
|
||||
if (typeof episodeEndTsMs !== "number" || !Number.isFinite(episodeEndTsMs)) {
|
||||
return bad(400, "Invalid reason.episodeEndTsMs");
|
||||
}
|
||||
episodeEndTs = new Date(episodeEndTsMs);
|
||||
}
|
||||
} else {
|
||||
scrapEntryId = asTrimmedString(r.scrapEntryId) || null;
|
||||
if (!scrapEntryId) return bad(400, "Missing reason.scrapEntryId for scrap");
|
||||
|
||||
if (typeof r.scrapQty !== "number" || !Number.isFinite(r.scrapQty)) {
|
||||
return bad(400, "Invalid reason.scrapQty for scrap");
|
||||
}
|
||||
scrapQty = Math.max(0, Math.trunc(r.scrapQty));
|
||||
|
||||
scrapUnit =
|
||||
r.scrapUnit != null && String(r.scrapUnit).trim()
|
||||
? String(r.scrapUnit).trim()
|
||||
: null;
|
||||
}
|
||||
|
||||
// Idempotent upsert keyed by reasonId
|
||||
const row = await prisma.reasonEntry.upsert({
|
||||
where: { reasonId },
|
||||
create: {
|
||||
orgId: machine.orgId,
|
||||
machineId: machine.id,
|
||||
reasonId,
|
||||
kind,
|
||||
episodeId,
|
||||
durationSeconds,
|
||||
episodeEndTs,
|
||||
scrapEntryId,
|
||||
scrapQty,
|
||||
scrapUnit,
|
||||
reasonCode,
|
||||
reasonLabel,
|
||||
reasonText,
|
||||
capturedAt,
|
||||
workOrderId,
|
||||
meta,
|
||||
schemaVersion,
|
||||
},
|
||||
update: {
|
||||
kind,
|
||||
episodeId,
|
||||
durationSeconds,
|
||||
episodeEndTs,
|
||||
scrapEntryId,
|
||||
scrapQty,
|
||||
scrapUnit,
|
||||
reasonCode,
|
||||
reasonLabel,
|
||||
reasonText,
|
||||
capturedAt,
|
||||
workOrderId,
|
||||
meta,
|
||||
schemaVersion,
|
||||
},
|
||||
select: { id: true, reasonId: true },
|
||||
});
|
||||
|
||||
return NextResponse.json({ ok: true, id: row.id, reasonId: row.reasonId });
|
||||
}
|
||||
153
app/api/invites/[token]/route.ts
Normal file
153
app/api/invites/[token]/route.ts
Normal file
@@ -0,0 +1,153 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import type { NextRequest } from "next/server";
|
||||
import bcrypt from "bcrypt";
|
||||
import { prisma } from "@/lib/prisma";
|
||||
import { buildSessionCookieOptions, COOKIE_NAME, SESSION_DAYS } from "@/lib/auth/sessionCookie";
|
||||
import { z } from "zod";
|
||||
|
||||
const tokenSchema = z.string().regex(/^[a-f0-9]{48}$/i);
|
||||
const acceptSchema = z.object({
|
||||
name: z.string().trim().min(1).max(80).optional(),
|
||||
password: z.string().min(8).max(256),
|
||||
});
|
||||
|
||||
async function loadInvite(token: string) {
|
||||
return prisma.orgInvite.findFirst({
|
||||
where: {
|
||||
token,
|
||||
revokedAt: null,
|
||||
acceptedAt: null,
|
||||
expiresAt: { gt: new Date() },
|
||||
},
|
||||
include: {
|
||||
org: { select: { id: true, name: true, slug: true } },
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
export async function GET(
|
||||
_req: NextRequest,
|
||||
{ params }: { params: Promise<{ token: string }> }
|
||||
) {
|
||||
const { token } = await params;
|
||||
if (!tokenSchema.safeParse(token).success) {
|
||||
return NextResponse.json({ ok: false, error: "Invalid invite token" }, { status: 400 });
|
||||
}
|
||||
const invite = await loadInvite(token);
|
||||
if (!invite) {
|
||||
return NextResponse.json({ ok: false, error: "Invite not found" }, { status: 404 });
|
||||
}
|
||||
|
||||
return NextResponse.json({
|
||||
ok: true,
|
||||
invite: {
|
||||
email: invite.email,
|
||||
role: invite.role,
|
||||
org: invite.org,
|
||||
expiresAt: invite.expiresAt,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
export async function POST(
|
||||
req: NextRequest,
|
||||
{ params }: { params: Promise<{ token: string }> }
|
||||
) {
|
||||
const { token } = await params;
|
||||
if (!tokenSchema.safeParse(token).success) {
|
||||
return NextResponse.json({ ok: false, error: "Invalid invite token" }, { status: 400 });
|
||||
}
|
||||
const invite = await loadInvite(token);
|
||||
if (!invite) {
|
||||
return NextResponse.json({ ok: false, error: "Invite not found" }, { status: 404 });
|
||||
}
|
||||
|
||||
const body = await req.json().catch(() => ({}));
|
||||
const parsed = acceptSchema.safeParse(body);
|
||||
if (!parsed.success) {
|
||||
return NextResponse.json({ ok: false, error: "Invalid invite payload" }, { status: 400 });
|
||||
}
|
||||
const name = String(parsed.data.name || "").trim();
|
||||
const password = parsed.data.password;
|
||||
|
||||
const existingUser = await prisma.user.findUnique({
|
||||
where: { email: invite.email },
|
||||
});
|
||||
|
||||
if (!existingUser && !name) {
|
||||
return NextResponse.json({ ok: false, error: "Name is required" }, { status: 400 });
|
||||
}
|
||||
|
||||
let userId = existingUser?.id ?? null;
|
||||
if (existingUser) {
|
||||
if (!existingUser.isActive) {
|
||||
return NextResponse.json({ ok: false, error: "User is inactive" }, { status: 403 });
|
||||
}
|
||||
const ok = await bcrypt.compare(password, existingUser.passwordHash);
|
||||
if (!ok) {
|
||||
return NextResponse.json({ ok: false, error: "Invalid credentials" }, { status: 401 });
|
||||
}
|
||||
userId = existingUser.id;
|
||||
} else {
|
||||
const passwordHash = await bcrypt.hash(password, 10);
|
||||
const created = await prisma.user.create({
|
||||
data: {
|
||||
email: invite.email,
|
||||
name,
|
||||
passwordHash,
|
||||
emailVerifiedAt: new Date(),
|
||||
},
|
||||
});
|
||||
userId = created.id;
|
||||
}
|
||||
|
||||
const expiresAt = new Date(Date.now() + SESSION_DAYS * 24 * 60 * 60 * 1000);
|
||||
|
||||
const session = await prisma.$transaction(async (tx) => {
|
||||
if (existingUser && !existingUser.emailVerifiedAt) {
|
||||
await tx.user.update({
|
||||
where: { id: existingUser.id },
|
||||
data: {
|
||||
emailVerifiedAt: new Date(),
|
||||
emailVerificationToken: null,
|
||||
emailVerificationExpiresAt: null,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
await tx.orgUser.upsert({
|
||||
where: {
|
||||
orgId_userId: {
|
||||
orgId: invite.orgId,
|
||||
userId,
|
||||
},
|
||||
},
|
||||
update: {
|
||||
role: invite.role,
|
||||
},
|
||||
create: {
|
||||
orgId: invite.orgId,
|
||||
userId,
|
||||
role: invite.role,
|
||||
},
|
||||
});
|
||||
|
||||
await tx.orgInvite.update({
|
||||
where: { id: invite.id },
|
||||
data: { acceptedAt: new Date() },
|
||||
});
|
||||
|
||||
return tx.session.create({
|
||||
data: {
|
||||
userId,
|
||||
orgId: invite.orgId,
|
||||
expiresAt,
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
const res = NextResponse.json({ ok: true, next: "/machines" });
|
||||
res.cookies.set(COOKIE_NAME, session.id, buildSessionCookieOptions(req));
|
||||
|
||||
return res;
|
||||
}
|
||||
74
app/api/login copy/route.ts
Normal file
74
app/api/login copy/route.ts
Normal file
@@ -0,0 +1,74 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import bcrypt from "bcrypt";
|
||||
import { prisma } from "@/lib/prisma";
|
||||
import { z } from "zod";
|
||||
|
||||
const COOKIE_NAME = "mis_session";
|
||||
const SESSION_DAYS = 7;
|
||||
|
||||
const loginSchema = z.object({
|
||||
email: z.string().trim().min(1).max(254).email(),
|
||||
password: z.string().min(1).max(256),
|
||||
next: z.string().optional(),
|
||||
});
|
||||
|
||||
function safeNextPath(value: unknown) {
|
||||
const raw = String(value ?? "").trim();
|
||||
if (!raw) return "/machines";
|
||||
if (!raw.startsWith("/") || raw.startsWith("//")) return "/machines";
|
||||
return raw;
|
||||
}
|
||||
|
||||
export async function POST(req: Request) {
|
||||
const body = await req.json().catch(() => ({}));
|
||||
const parsed = loginSchema.safeParse(body);
|
||||
if (!parsed.success) {
|
||||
return NextResponse.json({ ok: false, error: "Invalid login payload" }, { status: 400 });
|
||||
}
|
||||
const email = parsed.data.email.toLowerCase();
|
||||
const password = parsed.data.password;
|
||||
const next = safeNextPath(parsed.data.next);
|
||||
|
||||
const user = await prisma.user.findUnique({ where: { email } });
|
||||
if (!user || !user.isActive) {
|
||||
return NextResponse.json({ ok: false, error: "Invalid credentials" }, { status: 401 });
|
||||
}
|
||||
|
||||
const ok = await bcrypt.compare(password, user.passwordHash);
|
||||
if (!ok) {
|
||||
return NextResponse.json({ ok: false, error: "Invalid credentials" }, { status: 401 });
|
||||
}
|
||||
|
||||
// Multiple orgs per user: pick the oldest membership for now
|
||||
const membership = await prisma.orgUser.findFirst({
|
||||
where: { userId: user.id },
|
||||
orderBy: { createdAt: "asc" },
|
||||
});
|
||||
|
||||
if (!membership) {
|
||||
return NextResponse.json({ ok: false, error: "User has no organization" }, { status: 403 });
|
||||
}
|
||||
|
||||
const expiresAt = new Date(Date.now() + SESSION_DAYS * 24 * 60 * 60 * 1000);
|
||||
|
||||
const session = await prisma.session.create({
|
||||
data: {
|
||||
userId: user.id,
|
||||
orgId: membership.orgId,
|
||||
expiresAt,
|
||||
// optional fields you can add later: ip/userAgent
|
||||
},
|
||||
});
|
||||
|
||||
const res = NextResponse.json({ ok: true, next });
|
||||
|
||||
res.cookies.set(COOKIE_NAME, session.id, {
|
||||
httpOnly: true,
|
||||
sameSite: "lax",
|
||||
secure: false, // set true once HTTPS only
|
||||
path: "/",
|
||||
maxAge: SESSION_DAYS * 24 * 60 * 60,
|
||||
});
|
||||
|
||||
return res;
|
||||
}
|
||||
@@ -1,25 +1,41 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import bcrypt from "bcrypt";
|
||||
import { prisma } from "@/lib/prisma";
|
||||
import { buildSessionCookieOptions, COOKIE_NAME, SESSION_DAYS } from "@/lib/auth/sessionCookie";
|
||||
import { z } from "zod";
|
||||
|
||||
const COOKIE_NAME = "mis_session";
|
||||
const SESSION_DAYS = 7;
|
||||
const loginSchema = z.object({
|
||||
email: z.string().trim().min(1).max(254).email(),
|
||||
password: z.string().min(1).max(256),
|
||||
next: z.string().optional(),
|
||||
});
|
||||
|
||||
function safeNextPath(value: unknown) {
|
||||
const raw = String(value ?? "").trim();
|
||||
if (!raw) return "/machines";
|
||||
if (!raw.startsWith("/") || raw.startsWith("//")) return "/machines";
|
||||
return raw;
|
||||
}
|
||||
|
||||
export async function POST(req: Request) {
|
||||
const body = await req.json().catch(() => ({}));
|
||||
const email = String(body.email || "").trim().toLowerCase();
|
||||
const password = String(body.password || "");
|
||||
const next = String(body.next || "/machines");
|
||||
|
||||
if (!email || !password) {
|
||||
return NextResponse.json({ ok: false, error: "Missing email/password" }, { status: 400 });
|
||||
const parsed = loginSchema.safeParse(body);
|
||||
if (!parsed.success) {
|
||||
return NextResponse.json({ ok: false, error: "Invalid login payload" }, { status: 400 });
|
||||
}
|
||||
const email = parsed.data.email.toLowerCase();
|
||||
const password = parsed.data.password;
|
||||
const next = safeNextPath(parsed.data.next);
|
||||
|
||||
const user = await prisma.user.findUnique({ where: { email } });
|
||||
if (!user || !user.isActive) {
|
||||
return NextResponse.json({ ok: false, error: "Invalid credentials" }, { status: 401 });
|
||||
}
|
||||
|
||||
if (!user.emailVerifiedAt) {
|
||||
return NextResponse.json({ ok: false, error: "Email not verified" }, { status: 403 });
|
||||
}
|
||||
|
||||
const ok = await bcrypt.compare(password, user.passwordHash);
|
||||
if (!ok) {
|
||||
return NextResponse.json({ ok: false, error: "Invalid credentials" }, { status: 401 });
|
||||
@@ -47,14 +63,7 @@ export async function POST(req: Request) {
|
||||
});
|
||||
|
||||
const res = NextResponse.json({ ok: true, next });
|
||||
|
||||
res.cookies.set(COOKIE_NAME, session.id, {
|
||||
httpOnly: true,
|
||||
sameSite: "lax",
|
||||
secure: false, // set true once HTTPS only
|
||||
path: "/",
|
||||
maxAge: SESSION_DAYS * 24 * 60 * 60,
|
||||
});
|
||||
res.cookies.set(COOKIE_NAME, session.id, buildSessionCookieOptions(req));
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
21
app/api/logout copy/route.ts
Normal file
21
app/api/logout copy/route.ts
Normal file
@@ -0,0 +1,21 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import { cookies } from "next/headers";
|
||||
import { prisma } from "@/lib/prisma";
|
||||
|
||||
const COOKIE_NAME = "mis_session";
|
||||
|
||||
export async function POST() {
|
||||
const jar = await cookies();
|
||||
const sessionId = jar.get(COOKIE_NAME)?.value;
|
||||
|
||||
if (sessionId) {
|
||||
await prisma.session.updateMany({
|
||||
where: { id: sessionId, revokedAt: null },
|
||||
data: { revokedAt: new Date() },
|
||||
}).catch(() => {});
|
||||
}
|
||||
|
||||
const res = NextResponse.json({ ok: true });
|
||||
res.cookies.set(COOKIE_NAME, "", { path: "/", maxAge: 0 });
|
||||
return res;
|
||||
}
|
||||
@@ -1,183 +1,69 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import type { NextRequest } from "next/server";
|
||||
import { z } from "zod";
|
||||
import { prisma } from "@/lib/prisma";
|
||||
import { requireSession } from "@/lib/auth/requireSession";
|
||||
import { normalizeEvent } from "@/lib/events/normalizeEvent";
|
||||
|
||||
function normalizeEvent(row: any) {
|
||||
// -----------------------------
|
||||
// 1) Parse row.data safely
|
||||
// data may be:
|
||||
// - object
|
||||
// - array of objects
|
||||
// - JSON string of either
|
||||
// -----------------------------
|
||||
const raw = row.data;
|
||||
const machineIdSchema = z.string().uuid();
|
||||
|
||||
let parsed: any = raw;
|
||||
if (typeof raw === "string") {
|
||||
try {
|
||||
parsed = JSON.parse(raw);
|
||||
} catch {
|
||||
parsed = raw; // keep as string if not JSON
|
||||
}
|
||||
const ALLOWED_EVENT_TYPES = new Set([
|
||||
"slow-cycle",
|
||||
"microstop",
|
||||
"macrostop",
|
||||
"offline",
|
||||
"error",
|
||||
"oee-drop",
|
||||
"quality-spike",
|
||||
"performance-degradation",
|
||||
"predictive-oee-decline",
|
||||
"alert-delivery-failed",
|
||||
]);
|
||||
|
||||
function canManageMachines(role?: string | null) {
|
||||
return role === "OWNER" || role === "ADMIN";
|
||||
}
|
||||
|
||||
// data can be object OR [object]
|
||||
const blob = Array.isArray(parsed) ? parsed[0] : parsed;
|
||||
|
||||
// some payloads nest details under blob.data
|
||||
const inner = blob?.data ?? blob ?? {};
|
||||
|
||||
const normalizeType = (t: any) =>
|
||||
String(t ?? "")
|
||||
.trim()
|
||||
.toLowerCase()
|
||||
.replace(/_/g, "-");
|
||||
|
||||
// -----------------------------
|
||||
// 2) Alias mapping (canonical types)
|
||||
// -----------------------------
|
||||
const ALIAS: Record<string, string> = {
|
||||
// Spanish / synonyms
|
||||
macroparo: "macrostop",
|
||||
"macro-stop": "macrostop",
|
||||
macro_stop: "macrostop",
|
||||
|
||||
microparo: "microstop",
|
||||
"micro-paro": "microstop",
|
||||
micro_stop: "microstop",
|
||||
|
||||
// Node-RED types
|
||||
"production-stopped": "stop", // we'll classify to micro/macro below
|
||||
|
||||
// legacy / generic
|
||||
down: "stop",
|
||||
};
|
||||
|
||||
// -----------------------------
|
||||
// 3) Determine event type from DB or blob
|
||||
// -----------------------------
|
||||
const fromDbType =
|
||||
row.eventType && row.eventType !== "unknown" ? row.eventType : null;
|
||||
|
||||
const fromBlobType =
|
||||
blob?.anomaly_type ??
|
||||
blob?.eventType ??
|
||||
blob?.topic ??
|
||||
inner?.anomaly_type ??
|
||||
inner?.eventType ??
|
||||
null;
|
||||
|
||||
// infer slow-cycle if signature exists
|
||||
const inferredType =
|
||||
fromDbType ??
|
||||
fromBlobType ??
|
||||
((inner?.actual_cycle_time && inner?.theoretical_cycle_time) ||
|
||||
(blob?.actual_cycle_time && blob?.theoretical_cycle_time)
|
||||
? "slow-cycle"
|
||||
: "unknown");
|
||||
|
||||
const eventTypeRaw = normalizeType(inferredType);
|
||||
let eventType = ALIAS[eventTypeRaw] ?? eventTypeRaw;
|
||||
|
||||
// -----------------------------
|
||||
// 4) Optional: classify "stop" into micro/macro based on duration if present
|
||||
// (keeps old rows usable even if they stored production-stopped)
|
||||
// -----------------------------
|
||||
if (eventType === "stop") {
|
||||
const stopSec =
|
||||
(typeof inner?.stoppage_duration_seconds === "number" && inner.stoppage_duration_seconds) ||
|
||||
(typeof blob?.stoppage_duration_seconds === "number" && blob.stoppage_duration_seconds) ||
|
||||
(typeof inner?.stop_duration_seconds === "number" && inner.stop_duration_seconds) ||
|
||||
null;
|
||||
|
||||
// tune these thresholds to match your MES spec
|
||||
const MACROSTOP_SEC = 300; // 5 min
|
||||
eventType = stopSec != null && stopSec >= MACROSTOP_SEC ? "macrostop" : "microstop";
|
||||
function isPlainObject(value: unknown): value is Record<string, unknown> {
|
||||
return !!value && typeof value === "object" && !Array.isArray(value);
|
||||
}
|
||||
|
||||
// -----------------------------
|
||||
// 5) Severity, title, description, timestamp
|
||||
// -----------------------------
|
||||
const severity =
|
||||
String(
|
||||
(row.severity && row.severity !== "info" ? row.severity : null) ??
|
||||
blob?.severity ??
|
||||
inner?.severity ??
|
||||
"info"
|
||||
)
|
||||
.trim()
|
||||
.toLowerCase();
|
||||
|
||||
const title =
|
||||
String(
|
||||
(row.title && row.title !== "Event" ? row.title : null) ??
|
||||
blob?.title ??
|
||||
inner?.title ??
|
||||
(eventType === "slow-cycle" ? "Slow Cycle Detected" : "Event")
|
||||
).trim();
|
||||
|
||||
const description =
|
||||
row.description ??
|
||||
blob?.description ??
|
||||
inner?.description ??
|
||||
(eventType === "slow-cycle" &&
|
||||
(inner?.actual_cycle_time ?? blob?.actual_cycle_time) &&
|
||||
(inner?.theoretical_cycle_time ?? blob?.theoretical_cycle_time) &&
|
||||
(inner?.delta_percent ?? blob?.delta_percent) != null
|
||||
? `Cycle took ${Number(inner?.actual_cycle_time ?? blob?.actual_cycle_time).toFixed(1)}s (+${Number(inner?.delta_percent ?? blob?.delta_percent)}% vs ${Number(inner?.theoretical_cycle_time ?? blob?.theoretical_cycle_time).toFixed(1)}s objetivo)`
|
||||
: null);
|
||||
|
||||
const ts =
|
||||
row.ts ??
|
||||
(typeof blob?.timestamp === "number" ? new Date(blob.timestamp) : null) ??
|
||||
(typeof inner?.timestamp === "number" ? new Date(inner.timestamp) : null) ??
|
||||
null;
|
||||
|
||||
const workOrderId =
|
||||
row.workOrderId ??
|
||||
blob?.work_order_id ??
|
||||
inner?.work_order_id ??
|
||||
null;
|
||||
|
||||
return {
|
||||
id: row.id,
|
||||
ts,
|
||||
topic: String(row.topic ?? blob?.topic ?? eventType),
|
||||
eventType,
|
||||
severity,
|
||||
title,
|
||||
description,
|
||||
requiresAck: !!row.requiresAck,
|
||||
workOrderId,
|
||||
};
|
||||
function parseNumber(value: string | null, fallback: number) {
|
||||
const parsed = Number(value);
|
||||
return Number.isFinite(parsed) ? parsed : fallback;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
export async function GET(
|
||||
_req: NextRequest,
|
||||
{ params }: { params: Promise<{ machineId: string }> }
|
||||
) {
|
||||
export async function GET(req: NextRequest, { params }: { params: Promise<{ machineId: string }> }) {
|
||||
const session = await requireSession();
|
||||
if (!session) {
|
||||
return NextResponse.json({ ok: false, error: "Unauthorized" }, { status: 401 });
|
||||
}
|
||||
|
||||
const { machineId } = await params;
|
||||
if (!machineIdSchema.safeParse(machineId).success) {
|
||||
return NextResponse.json({ ok: false, error: "Invalid machine id" }, { status: 400 });
|
||||
}
|
||||
|
||||
const machine = await prisma.machine.findFirst({
|
||||
const url = new URL(req.url);
|
||||
const windowSec = Math.max(0, parseNumber(url.searchParams.get("windowSec"), 3600));
|
||||
const eventsWindowSec = Math.max(0, parseNumber(url.searchParams.get("eventsWindowSec"), 21600));
|
||||
const eventsMode = url.searchParams.get("events") ?? "critical";
|
||||
const eventsOnly = url.searchParams.get("eventsOnly") === "1";
|
||||
|
||||
const [machineRow, orgSettings, machineSettings] = await Promise.all([
|
||||
prisma.machine.findFirst({
|
||||
where: { id: machineId, orgId: session.orgId },
|
||||
select: {
|
||||
id: true,
|
||||
name: true,
|
||||
code: true,
|
||||
location: true,
|
||||
createdAt: true,
|
||||
updatedAt: true,
|
||||
heartbeats: {
|
||||
orderBy: { ts: "desc" },
|
||||
orderBy: { tsServer: "desc" },
|
||||
take: 1,
|
||||
select: { ts: true, status: true, message: true, ip: true, fwVersion: true },
|
||||
select: { ts: true, tsServer: true, status: true, message: true, ip: true, fwVersion: true },
|
||||
},
|
||||
kpiSnapshots: {
|
||||
orderBy: { ts: "desc" },
|
||||
@@ -197,19 +83,102 @@ export async function GET(
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
}),
|
||||
prisma.orgSettings.findUnique({
|
||||
where: { orgId: session.orgId },
|
||||
select: { stoppageMultiplier: true, macroStoppageMultiplier: true },
|
||||
}),
|
||||
prisma.machineSettings.findUnique({
|
||||
where: { machineId },
|
||||
select: { overridesJson: true },
|
||||
}),
|
||||
]);
|
||||
|
||||
if (!machine) {
|
||||
return NextResponse.json({ ok: false, error: "Not found" }, { status: 404 });
|
||||
if (!machineRow) {
|
||||
return NextResponse.json({ ok: false, error: "Machine not found" }, { status: 404 });
|
||||
}
|
||||
|
||||
const rawEvents = await prisma.machineEvent.findMany({
|
||||
const overrides = isPlainObject(machineSettings?.overridesJson) ? machineSettings?.overridesJson : {};
|
||||
const thresholdsOverride = isPlainObject(overrides.thresholds) ? overrides.thresholds : {};
|
||||
const stoppageMultiplier =
|
||||
typeof thresholdsOverride.stoppageMultiplier === "number"
|
||||
? thresholdsOverride.stoppageMultiplier
|
||||
: Number(orgSettings?.stoppageMultiplier ?? 1.5);
|
||||
const macroStoppageMultiplier =
|
||||
typeof thresholdsOverride.macroStoppageMultiplier === "number"
|
||||
? thresholdsOverride.macroStoppageMultiplier
|
||||
: Number(orgSettings?.macroStoppageMultiplier ?? 5);
|
||||
|
||||
const thresholds = {
|
||||
stoppageMultiplier,
|
||||
macroStoppageMultiplier,
|
||||
};
|
||||
|
||||
const machine = {
|
||||
...machineRow,
|
||||
effectiveCycleTime: null,
|
||||
latestHeartbeat: machineRow.heartbeats[0] ?? null,
|
||||
latestKpi: machineRow.kpiSnapshots[0] ?? null,
|
||||
heartbeats: undefined,
|
||||
kpiSnapshots: undefined,
|
||||
};
|
||||
|
||||
const cycles = eventsOnly
|
||||
? []
|
||||
: await prisma.machineCycle.findMany({
|
||||
where: {
|
||||
orgId: session.orgId,
|
||||
machineId,
|
||||
ts: { gte: new Date(Date.now() - windowSec * 1000) },
|
||||
},
|
||||
orderBy: { ts: "asc" },
|
||||
select: {
|
||||
ts: true,
|
||||
tsServer: true,
|
||||
cycleCount: true,
|
||||
actualCycleTime: true,
|
||||
theoreticalCycleTime: true,
|
||||
workOrderId: true,
|
||||
sku: true,
|
||||
},
|
||||
});
|
||||
|
||||
const cyclesOut = cycles.map((row) => {
|
||||
const ts = row.tsServer ?? row.ts;
|
||||
return {
|
||||
ts,
|
||||
t: ts.getTime(),
|
||||
cycleCount: row.cycleCount ?? null,
|
||||
actual: row.actualCycleTime,
|
||||
ideal: row.theoreticalCycleTime ?? null,
|
||||
workOrderId: row.workOrderId ?? null,
|
||||
sku: row.sku ?? null,
|
||||
};
|
||||
});
|
||||
|
||||
const eventWindowStart = new Date(Date.now() - eventsWindowSec * 1000);
|
||||
const criticalSeverities = ["critical", "error", "high"];
|
||||
const eventWhere = {
|
||||
orgId: session.orgId,
|
||||
machineId,
|
||||
ts: { gte: eventWindowStart },
|
||||
eventType: { in: Array.from(ALLOWED_EVENT_TYPES) },
|
||||
...(eventsMode === "critical"
|
||||
? {
|
||||
OR: [
|
||||
{ eventType: "macrostop" },
|
||||
{ requiresAck: true },
|
||||
{ severity: { in: criticalSeverities } },
|
||||
],
|
||||
}
|
||||
: {}),
|
||||
};
|
||||
|
||||
const [rawEvents, eventsCountAll] = await Promise.all([
|
||||
prisma.machineEvent.findMany({
|
||||
where: eventWhere,
|
||||
orderBy: { ts: "desc" },
|
||||
take: 100, // pull more, we'll filter after normalization
|
||||
take: eventsOnly ? 300 : 120,
|
||||
select: {
|
||||
id: true,
|
||||
ts: true,
|
||||
@@ -222,104 +191,83 @@ export async function GET(
|
||||
data: true,
|
||||
workOrderId: true,
|
||||
},
|
||||
});
|
||||
|
||||
const normalized = rawEvents.map(normalizeEvent);
|
||||
|
||||
const ALLOWED_TYPES = new Set([
|
||||
"slow-cycle",
|
||||
"microstop",
|
||||
"macrostop",
|
||||
"oee-drop",
|
||||
"quality-spike",
|
||||
"performance-degradation",
|
||||
"predictive-oee-decline",
|
||||
}),
|
||||
prisma.machineEvent.count({ where: eventWhere }),
|
||||
]);
|
||||
|
||||
const events = normalized
|
||||
.filter((e) => ALLOWED_TYPES.has(e.eventType))
|
||||
// keep slow-cycle even if severity is info, otherwise require warning/critical/error
|
||||
.filter((e) =>
|
||||
["slow-cycle", "microstop", "macrostop"].includes(e.eventType) ||
|
||||
["warning", "critical", "error"].includes(e.severity)
|
||||
)
|
||||
.slice(0, 30);
|
||||
const normalized = rawEvents.map((row) =>
|
||||
normalizeEvent(row, { microMultiplier: stoppageMultiplier, macroMultiplier: macroStoppageMultiplier })
|
||||
);
|
||||
|
||||
|
||||
// ---- cycles window ----
|
||||
const url = new URL(_req.url);
|
||||
const windowSec = Number(url.searchParams.get("windowSec") ?? "10800"); // default 3h
|
||||
|
||||
const latestKpi = machine.kpiSnapshots[0] ?? null;
|
||||
|
||||
// If KPI cycleTime missing, fallback to DB cycles (we fetch 1 first)
|
||||
const latestCycleForIdeal = await prisma.machineCycle.findFirst({
|
||||
where: { orgId: session.orgId, machineId },
|
||||
orderBy: { ts: "desc" },
|
||||
select: { theoreticalCycleTime: true },
|
||||
const seen = new Set<string>();
|
||||
const deduped = normalized.filter((event) => {
|
||||
const key = `${event.eventType}-${event.ts ?? ""}-${event.title}`;
|
||||
if (seen.has(key)) return false;
|
||||
seen.add(key);
|
||||
return true;
|
||||
});
|
||||
|
||||
const effectiveCycleTime =
|
||||
latestKpi?.cycleTime ??
|
||||
latestCycleForIdeal?.theoreticalCycleTime ??
|
||||
null;
|
||||
|
||||
// Estimate how many cycles we need to cover the window.
|
||||
// Add buffer so the chart doesn’t look “tight”.
|
||||
const estCycleSec = Math.max(1, Number(effectiveCycleTime ?? 14));
|
||||
const needed = Math.ceil(windowSec / estCycleSec) + 50;
|
||||
|
||||
// Safety cap to avoid crazy payloads
|
||||
const takeCycles = Math.min(5000, Math.max(200, needed));
|
||||
|
||||
const rawCycles = await prisma.machineCycle.findMany({
|
||||
where: { orgId: session.orgId, machineId },
|
||||
orderBy: { ts: "desc" },
|
||||
take: takeCycles,
|
||||
select: {
|
||||
ts: true,
|
||||
cycleCount: true,
|
||||
actualCycleTime: true,
|
||||
theoreticalCycleTime: true,
|
||||
workOrderId: true,
|
||||
sku: true,
|
||||
},
|
||||
deduped.sort((a, b) => {
|
||||
const at = a.ts ? a.ts.getTime() : 0;
|
||||
const bt = b.ts ? b.ts.getTime() : 0;
|
||||
return bt - at;
|
||||
});
|
||||
|
||||
// chart-friendly: oldest -> newest + numeric timestamps
|
||||
const cycles = rawCycles
|
||||
.slice()
|
||||
.reverse()
|
||||
.map((c) => ({
|
||||
ts: c.ts,
|
||||
t: c.ts.getTime(),
|
||||
cycleCount: c.cycleCount ?? null,
|
||||
actual: c.actualCycleTime,
|
||||
ideal: c.theoreticalCycleTime ?? null,
|
||||
workOrderId: c.workOrderId ?? null,
|
||||
sku: c.sku ?? null,
|
||||
}));
|
||||
|
||||
|
||||
|
||||
|
||||
return NextResponse.json({
|
||||
ok: true,
|
||||
machine: {
|
||||
id: machine.id,
|
||||
name: machine.name,
|
||||
code: machine.code,
|
||||
location: machine.location,
|
||||
latestHeartbeat: machine.heartbeats[0] ?? null,
|
||||
latestKpi: machine.kpiSnapshots[0] ?? null,
|
||||
effectiveCycleTime
|
||||
|
||||
},
|
||||
events,
|
||||
cycles
|
||||
machine,
|
||||
events: deduped,
|
||||
eventsCountAll,
|
||||
cycles: cyclesOut,
|
||||
thresholds,
|
||||
activeStoppage: null,
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
export async function DELETE(_req: Request, { params }: { params: Promise<{ machineId: string }> }) {
|
||||
const session = await requireSession();
|
||||
if (!session) {
|
||||
return NextResponse.json({ ok: false, error: "Unauthorized" }, { status: 401 });
|
||||
}
|
||||
|
||||
const { machineId } = await params;
|
||||
if (!machineIdSchema.safeParse(machineId).success) {
|
||||
return NextResponse.json({ ok: false, error: "Invalid machine id" }, { status: 400 });
|
||||
}
|
||||
|
||||
const membership = await prisma.orgUser.findUnique({
|
||||
where: {
|
||||
orgId_userId: {
|
||||
orgId: session.orgId,
|
||||
userId: session.userId,
|
||||
},
|
||||
},
|
||||
select: { role: true },
|
||||
});
|
||||
|
||||
if (!canManageMachines(membership?.role)) {
|
||||
return NextResponse.json({ ok: false, error: "Forbidden" }, { status: 403 });
|
||||
}
|
||||
|
||||
const result = await prisma.$transaction(async (tx) => {
|
||||
await tx.machineCycle.deleteMany({
|
||||
where: {
|
||||
machineId,
|
||||
orgId: session.orgId,
|
||||
},
|
||||
});
|
||||
|
||||
return tx.machine.deleteMany({
|
||||
where: {
|
||||
id: machineId,
|
||||
orgId: session.orgId,
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
if (result.count === 0) {
|
||||
return NextResponse.json({ ok: false, error: "Machine not found" }, { status: 404 });
|
||||
}
|
||||
|
||||
return NextResponse.json({ ok: true });
|
||||
}
|
||||
|
||||
64
app/api/machines/pair/route.ts
Normal file
64
app/api/machines/pair/route.ts
Normal file
@@ -0,0 +1,64 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import { randomBytes } from "crypto";
|
||||
import { prisma } from "@/lib/prisma";
|
||||
import { getBaseUrl } from "@/lib/appUrl";
|
||||
import { normalizePairingCode } from "@/lib/pairingCode";
|
||||
import { z } from "zod";
|
||||
|
||||
const pairSchema = z.object({
|
||||
code: z.string().trim().max(16).optional(),
|
||||
pairingCode: z.string().trim().max(16).optional(),
|
||||
});
|
||||
|
||||
export async function POST(req: Request) {
|
||||
const body = await req.json().catch(() => ({}));
|
||||
const parsed = pairSchema.safeParse(body);
|
||||
if (!parsed.success) {
|
||||
return NextResponse.json({ ok: false, error: "Invalid pairing payload" }, { status: 400 });
|
||||
}
|
||||
const rawCode = String(parsed.data.code || parsed.data.pairingCode || "").trim();
|
||||
const code = normalizePairingCode(rawCode);
|
||||
|
||||
if (!code || code.length !== 5) {
|
||||
return NextResponse.json({ ok: false, error: "Invalid pairing code" }, { status: 400 });
|
||||
}
|
||||
|
||||
const now = new Date();
|
||||
|
||||
const machine = await prisma.machine.findFirst({
|
||||
where: {
|
||||
pairingCode: code,
|
||||
pairingCodeUsedAt: null,
|
||||
pairingCodeExpiresAt: { gt: now },
|
||||
},
|
||||
select: { id: true, orgId: true, apiKey: true },
|
||||
});
|
||||
|
||||
if (!machine) {
|
||||
return NextResponse.json({ ok: false, error: "Pairing code not found or expired" }, { status: 404 });
|
||||
}
|
||||
|
||||
let apiKey = machine.apiKey;
|
||||
if (!apiKey) {
|
||||
apiKey = randomBytes(24).toString("hex");
|
||||
}
|
||||
|
||||
await prisma.machine.update({
|
||||
where: { id: machine.id },
|
||||
data: {
|
||||
apiKey,
|
||||
pairingCode: null,
|
||||
pairingCodeExpiresAt: null,
|
||||
pairingCodeUsedAt: now,
|
||||
},
|
||||
});
|
||||
|
||||
return NextResponse.json({
|
||||
ok: true,
|
||||
config: {
|
||||
cloudBaseUrl: getBaseUrl(req),
|
||||
machineId: machine.id,
|
||||
apiKey,
|
||||
},
|
||||
});
|
||||
}
|
||||
@@ -1,17 +1,32 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import { randomBytes } from "crypto";
|
||||
import { prisma } from "@/lib/prisma";
|
||||
import { cookies } from "next/headers";
|
||||
import { generatePairingCode } from "@/lib/pairingCode";
|
||||
import { z } from "zod";
|
||||
|
||||
const COOKIE_NAME = "mis_session";
|
||||
|
||||
const createMachineSchema = z.object({
|
||||
name: z.string().trim().min(1).max(80),
|
||||
code: z.string().trim().max(40).optional(),
|
||||
location: z.string().trim().max(80).optional(),
|
||||
});
|
||||
|
||||
async function requireSession() {
|
||||
const sessionId = (await cookies()).get(COOKIE_NAME)?.value;
|
||||
if (!sessionId) return null;
|
||||
|
||||
return prisma.session.findFirst({
|
||||
const session = await prisma.session.findFirst({
|
||||
where: { id: sessionId, revokedAt: null, expiresAt: { gt: new Date() } },
|
||||
include: { org: true, user: true },
|
||||
});
|
||||
|
||||
if (!session || !session.user?.isActive || !session.user?.emailVerifiedAt) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return session;
|
||||
}
|
||||
|
||||
export async function GET() {
|
||||
@@ -29,19 +44,110 @@ export async function GET() {
|
||||
createdAt: true,
|
||||
updatedAt: true,
|
||||
heartbeats: {
|
||||
orderBy: { tsServer: "desc" },
|
||||
take: 1,
|
||||
select: { ts: true, tsServer: true, status: true, message: true, ip: true, fwVersion: true },
|
||||
},
|
||||
kpiSnapshots: {
|
||||
orderBy: { ts: "desc" },
|
||||
take: 1,
|
||||
select: { ts: true, status: true, message: true, ip: true, fwVersion: true },
|
||||
select: {
|
||||
ts: true,
|
||||
oee: true,
|
||||
availability: true,
|
||||
performance: true,
|
||||
quality: true,
|
||||
workOrderId: true,
|
||||
sku: true,
|
||||
good: true,
|
||||
scrap: true,
|
||||
target: true,
|
||||
cycleTime: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
|
||||
// flatten latest heartbeat for UI convenience
|
||||
const out = machines.map((m) => ({
|
||||
...m,
|
||||
latestHeartbeat: m.heartbeats[0] ?? null,
|
||||
latestKpi: m.kpiSnapshots[0] ?? null,
|
||||
heartbeats: undefined,
|
||||
kpiSnapshots: undefined,
|
||||
}));
|
||||
|
||||
return NextResponse.json({ ok: true, machines: out });
|
||||
}
|
||||
|
||||
export async function POST(req: Request) {
|
||||
const session = await requireSession();
|
||||
if (!session) return NextResponse.json({ ok: false, error: "Unauthorized" }, { status: 401 });
|
||||
|
||||
const body = await req.json().catch(() => ({}));
|
||||
const parsed = createMachineSchema.safeParse(body);
|
||||
if (!parsed.success) {
|
||||
return NextResponse.json({ ok: false, error: "Invalid machine payload" }, { status: 400 });
|
||||
}
|
||||
|
||||
const name = parsed.data.name;
|
||||
const codeRaw = parsed.data.code ?? "";
|
||||
const locationRaw = parsed.data.location ?? "";
|
||||
|
||||
const existing = await prisma.machine.findFirst({
|
||||
where: { orgId: session.orgId, name },
|
||||
select: { id: true },
|
||||
});
|
||||
|
||||
if (existing) {
|
||||
return NextResponse.json({ ok: false, error: "Machine name already exists" }, { status: 409 });
|
||||
}
|
||||
|
||||
const apiKey = randomBytes(24).toString("hex");
|
||||
const pairingExpiresAt = new Date(Date.now() + 24 * 60 * 60 * 1000);
|
||||
|
||||
let machine = null as null | {
|
||||
id: string;
|
||||
name: string;
|
||||
code?: string | null;
|
||||
location?: string | null;
|
||||
pairingCode?: string | null;
|
||||
pairingCodeExpiresAt?: Date | null;
|
||||
};
|
||||
|
||||
for (let attempt = 0; attempt < 5; attempt += 1) {
|
||||
const pairingCode = generatePairingCode();
|
||||
try {
|
||||
machine = await prisma.machine.create({
|
||||
data: {
|
||||
orgId: session.orgId,
|
||||
name,
|
||||
code: codeRaw || null,
|
||||
location: locationRaw || null,
|
||||
apiKey,
|
||||
pairingCode,
|
||||
pairingCodeExpiresAt: pairingExpiresAt,
|
||||
},
|
||||
select: {
|
||||
id: true,
|
||||
name: true,
|
||||
code: true,
|
||||
location: true,
|
||||
pairingCode: true,
|
||||
pairingCodeExpiresAt: true,
|
||||
},
|
||||
});
|
||||
break;
|
||||
} catch (err: unknown) {
|
||||
const code = typeof err === "object" && err !== null ? (err as { code?: string }).code : undefined;
|
||||
if (code !== "P2002") throw err;
|
||||
}
|
||||
}
|
||||
|
||||
if (!machine?.pairingCode) {
|
||||
return NextResponse.json({ ok: false, error: "Failed to generate pairing code" }, { status: 500 });
|
||||
}
|
||||
|
||||
return NextResponse.json({ ok: true, machine });
|
||||
}
|
||||
|
||||
@@ -2,13 +2,19 @@ import { NextResponse } from "next/server";
|
||||
import { requireSession } from "@/lib/auth/requireSession";
|
||||
import { prisma } from "@/lib/prisma";
|
||||
|
||||
|
||||
|
||||
export async function GET() {
|
||||
try {
|
||||
const { userId, orgId } = await requireSession();
|
||||
const session = await requireSession();
|
||||
if (!session) {
|
||||
return NextResponse.json({ ok: false }, { status: 401 });
|
||||
}
|
||||
const { userId, orgId } = session;
|
||||
|
||||
const user = await prisma.user.findUnique({
|
||||
where: { id: userId },
|
||||
select: { id: true, email: true, name: true },
|
||||
select: { id: true, email: true, name: true, phone: true },
|
||||
});
|
||||
|
||||
const org = await prisma.org.findUnique({
|
||||
@@ -16,7 +22,12 @@ export async function GET() {
|
||||
select: { id: true, name: true, slug: true },
|
||||
});
|
||||
|
||||
return NextResponse.json({ ok: true, user, org });
|
||||
const membership = await prisma.orgUser.findUnique({
|
||||
where: { orgId_userId: { orgId, userId } },
|
||||
select: { role: true },
|
||||
});
|
||||
|
||||
return NextResponse.json({ ok: true, user, org, membership });
|
||||
} catch {
|
||||
return NextResponse.json({ ok: false }, { status: 401 });
|
||||
}
|
||||
|
||||
57
app/api/org/invites/[inviteId]/route.ts
Normal file
57
app/api/org/invites/[inviteId]/route.ts
Normal file
@@ -0,0 +1,57 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import type { NextRequest } from "next/server";
|
||||
import { prisma } from "@/lib/prisma";
|
||||
import { requireSession } from "@/lib/auth/requireSession";
|
||||
import { z } from "zod";
|
||||
|
||||
function canManageMembers(role?: string | null) {
|
||||
return role === "OWNER" || role === "ADMIN";
|
||||
}
|
||||
|
||||
const inviteIdSchema = z.string().uuid();
|
||||
|
||||
export async function DELETE(
|
||||
_req: NextRequest,
|
||||
{ params }: { params: Promise<{ inviteId: string }> }
|
||||
) {
|
||||
try {
|
||||
const session = await requireSession();
|
||||
if (!session) {
|
||||
return NextResponse.json({ ok: false, error: "Unauthorized" }, { status: 401 });
|
||||
}
|
||||
const { inviteId } = await params;
|
||||
if (!inviteIdSchema.safeParse(inviteId).success) {
|
||||
return NextResponse.json({ ok: false, error: "Invalid invite id" }, { status: 400 });
|
||||
}
|
||||
|
||||
const membership = await prisma.orgUser.findUnique({
|
||||
where: {
|
||||
orgId_userId: {
|
||||
orgId: session.orgId,
|
||||
userId: session.userId,
|
||||
},
|
||||
},
|
||||
select: { role: true },
|
||||
});
|
||||
|
||||
if (!canManageMembers(membership?.role)) {
|
||||
return NextResponse.json({ ok: false, error: "Forbidden" }, { status: 403 });
|
||||
}
|
||||
|
||||
await prisma.orgInvite.updateMany({
|
||||
where: {
|
||||
id: inviteId,
|
||||
orgId: session.orgId,
|
||||
acceptedAt: null,
|
||||
revokedAt: null,
|
||||
},
|
||||
data: {
|
||||
revokedAt: new Date(),
|
||||
},
|
||||
});
|
||||
|
||||
return NextResponse.json({ ok: true });
|
||||
} catch {
|
||||
return NextResponse.json({ ok: false, error: "Unauthorized" }, { status: 401 });
|
||||
}
|
||||
}
|
||||
197
app/api/org/members/route.ts
Normal file
197
app/api/org/members/route.ts
Normal file
@@ -0,0 +1,197 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import { randomBytes } from "crypto";
|
||||
import { prisma } from "@/lib/prisma";
|
||||
import { requireSession } from "@/lib/auth/requireSession";
|
||||
import { buildInviteEmail, sendEmail } from "@/lib/email";
|
||||
import { getBaseUrl } from "@/lib/appUrl";
|
||||
import { z } from "zod";
|
||||
|
||||
const INVITE_DAYS = 7;
|
||||
const ROLES = new Set(["OWNER", "ADMIN", "MEMBER"]);
|
||||
const inviteSchema = z.object({
|
||||
email: z.string().trim().min(1).max(254).email(),
|
||||
role: z.string().trim().toUpperCase().optional(),
|
||||
});
|
||||
|
||||
function canManageMembers(role?: string | null) {
|
||||
return role === "OWNER" || role === "ADMIN";
|
||||
}
|
||||
|
||||
export async function GET() {
|
||||
try {
|
||||
|
||||
const session = await requireSession();
|
||||
if (!session) {
|
||||
return NextResponse.json({ ok: false, error: "Unauthorized" }, { status: 401 });
|
||||
}
|
||||
|
||||
const [org, members, invites] = await prisma.$transaction([
|
||||
prisma.org.findUnique({
|
||||
where: { id: session.orgId },
|
||||
select: { id: true, name: true, slug: true },
|
||||
}),
|
||||
prisma.orgUser.findMany({
|
||||
where: { orgId: session.orgId },
|
||||
orderBy: { createdAt: "asc" },
|
||||
include: {
|
||||
user: { select: { id: true, email: true, name: true, isActive: true, createdAt: true } },
|
||||
},
|
||||
}),
|
||||
prisma.orgInvite.findMany({
|
||||
where: {
|
||||
orgId: session.orgId,
|
||||
revokedAt: null,
|
||||
acceptedAt: null,
|
||||
expiresAt: { gt: new Date() },
|
||||
},
|
||||
orderBy: { createdAt: "desc" },
|
||||
select: {
|
||||
id: true,
|
||||
email: true,
|
||||
role: true,
|
||||
token: true,
|
||||
createdAt: true,
|
||||
expiresAt: true,
|
||||
},
|
||||
}),
|
||||
]);
|
||||
|
||||
const mappedMembers = members.map((m) => ({
|
||||
id: m.user.id,
|
||||
membershipId: m.id,
|
||||
email: m.user.email,
|
||||
name: m.user.name,
|
||||
role: m.role,
|
||||
isActive: m.user.isActive,
|
||||
joinedAt: m.createdAt,
|
||||
}));
|
||||
|
||||
return NextResponse.json({
|
||||
ok: true,
|
||||
org,
|
||||
members: mappedMembers,
|
||||
invites,
|
||||
});
|
||||
} catch {
|
||||
return NextResponse.json({ ok: false, error: "Unauthorized" }, { status: 401 });
|
||||
}
|
||||
}
|
||||
|
||||
export async function POST(req: Request) {
|
||||
try {
|
||||
const session = await requireSession();
|
||||
if (!session) {
|
||||
return NextResponse.json({ ok: false, error: "Unauthorized" }, { status: 401 });
|
||||
}
|
||||
const membership = await prisma.orgUser.findUnique({
|
||||
where: {
|
||||
orgId_userId: {
|
||||
orgId: session.orgId,
|
||||
userId: session.userId,
|
||||
},
|
||||
},
|
||||
select: { role: true },
|
||||
});
|
||||
|
||||
if (!canManageMembers(membership?.role)) {
|
||||
return NextResponse.json({ ok: false, error: "Forbidden" }, { status: 403 });
|
||||
}
|
||||
|
||||
const body = await req.json().catch(() => ({}));
|
||||
const parsed = inviteSchema.safeParse(body);
|
||||
if (!parsed.success) {
|
||||
return NextResponse.json({ ok: false, error: "Invalid invite payload" }, { status: 400 });
|
||||
}
|
||||
const email = parsed.data.email.toLowerCase();
|
||||
const role = String(parsed.data.role || "MEMBER").toUpperCase();
|
||||
|
||||
if (!ROLES.has(role)) {
|
||||
return NextResponse.json({ ok: false, error: "Invalid role" }, { status: 400 });
|
||||
}
|
||||
|
||||
const existingUser = await prisma.user.findUnique({ where: { email } });
|
||||
if (existingUser) {
|
||||
const existingMembership = await prisma.orgUser.findUnique({
|
||||
where: {
|
||||
orgId_userId: {
|
||||
orgId: session.orgId,
|
||||
userId: existingUser.id,
|
||||
},
|
||||
},
|
||||
});
|
||||
if (existingMembership) {
|
||||
return NextResponse.json({ ok: false, error: "User already in org" }, { status: 409 });
|
||||
}
|
||||
}
|
||||
|
||||
const existingInvite = await prisma.orgInvite.findFirst({
|
||||
where: {
|
||||
orgId: session.orgId,
|
||||
email,
|
||||
acceptedAt: null,
|
||||
revokedAt: null,
|
||||
expiresAt: { gt: new Date() },
|
||||
},
|
||||
orderBy: { createdAt: "desc" },
|
||||
});
|
||||
|
||||
if (existingInvite) {
|
||||
return NextResponse.json({ ok: true, invite: existingInvite });
|
||||
}
|
||||
|
||||
let invite = null;
|
||||
for (let i = 0; i < 3; i += 1) {
|
||||
const token = randomBytes(24).toString("hex");
|
||||
try {
|
||||
invite = await prisma.orgInvite.create({
|
||||
data: {
|
||||
orgId: session.orgId,
|
||||
email,
|
||||
role,
|
||||
token,
|
||||
invitedBy: session.userId,
|
||||
expiresAt: new Date(Date.now() + INVITE_DAYS * 24 * 60 * 60 * 1000),
|
||||
},
|
||||
});
|
||||
break;
|
||||
} catch (err: unknown) {
|
||||
const code = typeof err === "object" && err !== null ? (err as { code?: string }).code : undefined;
|
||||
if (code !== "P2002") throw err;
|
||||
}
|
||||
}
|
||||
|
||||
if (!invite) {
|
||||
return NextResponse.json({ ok: false, error: "Failed to create invite" }, { status: 500 });
|
||||
}
|
||||
|
||||
let emailSent = true;
|
||||
let emailError: string | null = null;
|
||||
try {
|
||||
const org = await prisma.org.findUnique({
|
||||
where: { id: session.orgId },
|
||||
select: { name: true },
|
||||
});
|
||||
const baseUrl = getBaseUrl(req);
|
||||
const inviteUrl = `${baseUrl}/invite/${invite.token}`;
|
||||
const appName = "MIS Control Tower";
|
||||
const content = buildInviteEmail({
|
||||
appName,
|
||||
orgName: org?.name || "your organization",
|
||||
inviteUrl,
|
||||
});
|
||||
await sendEmail({
|
||||
to: invite.email,
|
||||
subject: content.subject,
|
||||
text: content.text,
|
||||
html: content.html,
|
||||
});
|
||||
} catch (err: unknown) {
|
||||
emailSent = false;
|
||||
emailError = err instanceof Error ? err.message : "Failed to send invite email";
|
||||
}
|
||||
|
||||
return NextResponse.json({ ok: true, invite, emailSent, emailError });
|
||||
} catch {
|
||||
return NextResponse.json({ ok: false, error: "Unauthorized" }, { status: 401 });
|
||||
}
|
||||
}
|
||||
101
app/api/overview/route.ts
Normal file
101
app/api/overview/route.ts
Normal file
@@ -0,0 +1,101 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import type { NextRequest } from "next/server";
|
||||
import { createHash } from "crypto";
|
||||
import { prisma } from "@/lib/prisma";
|
||||
import { requireSession } from "@/lib/auth/requireSession";
|
||||
import { getOverviewData } from "@/lib/overview/getOverviewData";
|
||||
|
||||
function toMs(value?: Date | null) {
|
||||
return value ? value.getTime() : 0;
|
||||
}
|
||||
|
||||
export async function GET(req: NextRequest) {
|
||||
const session = await requireSession();
|
||||
if (!session) {
|
||||
return NextResponse.json({ ok: false, error: "Unauthorized" }, { status: 401 });
|
||||
}
|
||||
|
||||
const url = new URL(req.url);
|
||||
const eventsMode = url.searchParams.get("events") ?? "critical";
|
||||
const eventsWindowSecRaw = Number(url.searchParams.get("eventsWindowSec") ?? "21600");
|
||||
const eventsWindowSec = Number.isFinite(eventsWindowSecRaw) ? eventsWindowSecRaw : 21600;
|
||||
const eventMachinesRaw = Number(url.searchParams.get("eventMachines") ?? "6");
|
||||
const eventMachines = Number.isFinite(eventMachinesRaw) ? Math.max(1, eventMachinesRaw) : 6;
|
||||
const [machineAgg, heartbeatAgg, kpiAgg, eventAgg, orgSettings] = await Promise.all([
|
||||
prisma.machine.aggregate({
|
||||
where: { orgId: session.orgId },
|
||||
_max: { updatedAt: true },
|
||||
}),
|
||||
prisma.machineHeartbeat.aggregate({
|
||||
where: { orgId: session.orgId },
|
||||
_max: { tsServer: true },
|
||||
}),
|
||||
prisma.machineKpiSnapshot.aggregate({
|
||||
where: { orgId: session.orgId },
|
||||
_max: { tsServer: true },
|
||||
}),
|
||||
prisma.machineEvent.aggregate({
|
||||
where: { orgId: session.orgId },
|
||||
_max: { tsServer: true },
|
||||
}),
|
||||
prisma.orgSettings.findUnique({
|
||||
where: { orgId: session.orgId },
|
||||
select: { updatedAt: true, stoppageMultiplier: true, macroStoppageMultiplier: true },
|
||||
}),
|
||||
]);
|
||||
|
||||
const lastModifiedMs = Math.max(
|
||||
toMs(machineAgg._max.updatedAt),
|
||||
toMs(heartbeatAgg._max.tsServer),
|
||||
toMs(kpiAgg._max.tsServer),
|
||||
toMs(eventAgg._max.tsServer),
|
||||
toMs(orgSettings?.updatedAt)
|
||||
);
|
||||
|
||||
const versionParts = [
|
||||
session.orgId,
|
||||
eventsMode,
|
||||
eventsWindowSec,
|
||||
eventMachines,
|
||||
toMs(machineAgg._max.updatedAt),
|
||||
toMs(heartbeatAgg._max.tsServer),
|
||||
toMs(kpiAgg._max.tsServer),
|
||||
toMs(eventAgg._max.tsServer),
|
||||
toMs(orgSettings?.updatedAt),
|
||||
];
|
||||
|
||||
const etag = `W/"${createHash("sha1").update(versionParts.join("|")).digest("hex")}"`;
|
||||
const lastModified = new Date(lastModifiedMs || 0).toUTCString();
|
||||
const responseHeaders = new Headers({
|
||||
"Cache-Control": "private, no-cache, max-age=0, must-revalidate",
|
||||
ETag: etag,
|
||||
"Last-Modified": lastModified,
|
||||
Vary: "Cookie",
|
||||
});
|
||||
|
||||
const ifNoneMatch = req.headers.get("if-none-match");
|
||||
if (ifNoneMatch && ifNoneMatch === etag) {
|
||||
return new NextResponse(null, { status: 304, headers: responseHeaders });
|
||||
}
|
||||
|
||||
const ifModifiedSince = req.headers.get("if-modified-since");
|
||||
if (!ifNoneMatch && ifModifiedSince) {
|
||||
const since = Date.parse(ifModifiedSince);
|
||||
if (!Number.isNaN(since) && lastModifiedMs <= since) {
|
||||
return new NextResponse(null, { status: 304, headers: responseHeaders });
|
||||
}
|
||||
}
|
||||
|
||||
const { machines: machineRows, events } = await getOverviewData({
|
||||
orgId: session.orgId,
|
||||
eventsMode,
|
||||
eventsWindowSec,
|
||||
eventMachines,
|
||||
orgSettings,
|
||||
});
|
||||
|
||||
return NextResponse.json(
|
||||
{ ok: true, machines: machineRows, events },
|
||||
{ headers: responseHeaders }
|
||||
);
|
||||
}
|
||||
65
app/api/reports/filters/route.ts
Normal file
65
app/api/reports/filters/route.ts
Normal file
@@ -0,0 +1,65 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import type { NextRequest } from "next/server";
|
||||
import { prisma } from "@/lib/prisma";
|
||||
import { requireSession } from "@/lib/auth/requireSession";
|
||||
|
||||
const RANGE_MS: Record<string, number> = {
|
||||
"24h": 24 * 60 * 60 * 1000,
|
||||
"7d": 7 * 24 * 60 * 60 * 1000,
|
||||
"30d": 30 * 24 * 60 * 60 * 1000,
|
||||
};
|
||||
|
||||
function parseDate(input?: string | null) {
|
||||
if (!input) return null;
|
||||
const n = Number(input);
|
||||
if (!Number.isNaN(n)) return new Date(n);
|
||||
const d = new Date(input);
|
||||
return Number.isNaN(d.getTime()) ? null : d;
|
||||
}
|
||||
|
||||
function pickRange(req: NextRequest) {
|
||||
const url = new URL(req.url);
|
||||
const range = url.searchParams.get("range") ?? "24h";
|
||||
const now = new Date();
|
||||
|
||||
if (range === "custom") {
|
||||
const start = parseDate(url.searchParams.get("start")) ?? new Date(now.getTime() - RANGE_MS["24h"]);
|
||||
const end = parseDate(url.searchParams.get("end")) ?? now;
|
||||
return { start, end };
|
||||
}
|
||||
|
||||
const ms = RANGE_MS[range] ?? RANGE_MS["24h"];
|
||||
return { start: new Date(now.getTime() - ms), end: now };
|
||||
}
|
||||
|
||||
export async function GET(req: NextRequest) {
|
||||
const session = await requireSession();
|
||||
if (!session) return NextResponse.json({ ok: false, error: "Unauthorized" }, { status: 401 });
|
||||
|
||||
const url = new URL(req.url);
|
||||
const machineId = url.searchParams.get("machineId") ?? undefined;
|
||||
const { start, end } = pickRange(req);
|
||||
|
||||
const baseWhere = {
|
||||
orgId: session.orgId,
|
||||
...(machineId ? { machineId } : {}),
|
||||
ts: { gte: start, lte: end },
|
||||
};
|
||||
|
||||
const workOrderRows = await prisma.machineCycle.findMany({
|
||||
where: { ...baseWhere, workOrderId: { not: null } },
|
||||
distinct: ["workOrderId"],
|
||||
select: { workOrderId: true },
|
||||
});
|
||||
|
||||
const skuRows = await prisma.machineCycle.findMany({
|
||||
where: { ...baseWhere, sku: { not: null } },
|
||||
distinct: ["sku"],
|
||||
select: { sku: true },
|
||||
});
|
||||
|
||||
const workOrders = workOrderRows.map((r) => r.workOrderId).filter(Boolean) as string[];
|
||||
const skus = skuRows.map((r) => r.sku).filter(Boolean) as string[];
|
||||
|
||||
return NextResponse.json({ ok: true, workOrders, skus });
|
||||
}
|
||||
373
app/api/reports/route.ts
Normal file
373
app/api/reports/route.ts
Normal file
@@ -0,0 +1,373 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import type { NextRequest } from "next/server";
|
||||
import { prisma } from "@/lib/prisma";
|
||||
import { requireSession } from "@/lib/auth/requireSession";
|
||||
|
||||
const RANGE_MS: Record<string, number> = {
|
||||
"24h": 24 * 60 * 60 * 1000,
|
||||
"7d": 7 * 24 * 60 * 60 * 1000,
|
||||
"30d": 30 * 24 * 60 * 60 * 1000,
|
||||
};
|
||||
|
||||
function parseDate(input?: string | null) {
|
||||
if (!input) return null;
|
||||
const n = Number(input);
|
||||
if (!Number.isNaN(n)) return new Date(n);
|
||||
const d = new Date(input);
|
||||
return Number.isNaN(d.getTime()) ? null : d;
|
||||
}
|
||||
|
||||
function pickRange(req: NextRequest) {
|
||||
const url = new URL(req.url);
|
||||
const range = url.searchParams.get("range") ?? "24h";
|
||||
const now = new Date();
|
||||
|
||||
if (range === "custom") {
|
||||
const start = parseDate(url.searchParams.get("start")) ?? new Date(now.getTime() - RANGE_MS["24h"]);
|
||||
const end = parseDate(url.searchParams.get("end")) ?? now;
|
||||
return { start, end };
|
||||
}
|
||||
|
||||
const ms = RANGE_MS[range] ?? RANGE_MS["24h"];
|
||||
return { start: new Date(now.getTime() - ms), end: now };
|
||||
}
|
||||
|
||||
function safeNum(v: unknown) {
|
||||
return typeof v === "number" && Number.isFinite(v) ? v : null;
|
||||
}
|
||||
|
||||
export async function GET(req: NextRequest) {
|
||||
const session = await requireSession();
|
||||
if (!session) return NextResponse.json({ ok: false, error: "Unauthorized" }, { status: 401 });
|
||||
|
||||
const url = new URL(req.url);
|
||||
const machineId = url.searchParams.get("machineId") ?? undefined;
|
||||
const { start, end } = pickRange(req);
|
||||
const workOrderId = url.searchParams.get("workOrderId") ?? undefined;
|
||||
const sku = url.searchParams.get("sku") ?? undefined;
|
||||
const baseWhere = {
|
||||
orgId: session.orgId,
|
||||
...(machineId ? { machineId } : {}),
|
||||
...(workOrderId ? { workOrderId } : {}),
|
||||
...(sku ? { sku } : {}),
|
||||
};
|
||||
|
||||
const kpiRows = await prisma.machineKpiSnapshot.findMany({
|
||||
where: { ...baseWhere, ts: { gte: start, lte: end } },
|
||||
orderBy: { ts: "asc" },
|
||||
select: {
|
||||
ts: true,
|
||||
oee: true,
|
||||
availability: true,
|
||||
performance: true,
|
||||
quality: true,
|
||||
good: true,
|
||||
scrap: true,
|
||||
target: true,
|
||||
machineId: true,
|
||||
},
|
||||
});
|
||||
|
||||
let oeeSum = 0;
|
||||
let oeeCount = 0;
|
||||
let availSum = 0;
|
||||
let availCount = 0;
|
||||
let perfSum = 0;
|
||||
let perfCount = 0;
|
||||
let qualSum = 0;
|
||||
let qualCount = 0;
|
||||
|
||||
for (const k of kpiRows) {
|
||||
if (safeNum(k.oee) != null) {
|
||||
oeeSum += Number(k.oee);
|
||||
oeeCount += 1;
|
||||
}
|
||||
if (safeNum(k.availability) != null) {
|
||||
availSum += Number(k.availability);
|
||||
availCount += 1;
|
||||
}
|
||||
if (safeNum(k.performance) != null) {
|
||||
perfSum += Number(k.performance);
|
||||
perfCount += 1;
|
||||
}
|
||||
if (safeNum(k.quality) != null) {
|
||||
qualSum += Number(k.quality);
|
||||
qualCount += 1;
|
||||
}
|
||||
}
|
||||
|
||||
const cycles = await prisma.machineCycle.findMany({
|
||||
where: { ...baseWhere, ts: { gte: start, lte: end } },
|
||||
select: { goodDelta: true, scrapDelta: true },
|
||||
});
|
||||
|
||||
let goodTotal = 0;
|
||||
let scrapTotal = 0;
|
||||
|
||||
for (const c of cycles) {
|
||||
if (safeNum(c.goodDelta) != null) goodTotal += Number(c.goodDelta);
|
||||
if (safeNum(c.scrapDelta) != null) scrapTotal += Number(c.scrapDelta);
|
||||
}
|
||||
|
||||
const kpiAgg = await prisma.machineKpiSnapshot.groupBy({
|
||||
by: ["machineId"],
|
||||
where: { ...baseWhere, ts: { gte: start, lte: end } },
|
||||
_max: { good: true, scrap: true, target: true },
|
||||
_min: { good: true, scrap: true },
|
||||
_count: { _all: true },
|
||||
});
|
||||
|
||||
let targetTotal = 0;
|
||||
if (goodTotal === 0 && scrapTotal === 0) {
|
||||
let goodFallback = 0;
|
||||
let scrapFallback = 0;
|
||||
|
||||
for (const row of kpiAgg) {
|
||||
const count = row._count._all ?? 0;
|
||||
const maxGood = safeNum(row._max.good);
|
||||
const minGood = safeNum(row._min.good);
|
||||
const maxScrap = safeNum(row._max.scrap);
|
||||
const minScrap = safeNum(row._min.scrap);
|
||||
|
||||
if (count > 1 && maxGood != null && minGood != null) {
|
||||
goodFallback += Math.max(0, maxGood - minGood);
|
||||
} else if (maxGood != null) {
|
||||
goodFallback += maxGood;
|
||||
}
|
||||
|
||||
if (count > 1 && maxScrap != null && minScrap != null) {
|
||||
scrapFallback += Math.max(0, maxScrap - minScrap);
|
||||
} else if (maxScrap != null) {
|
||||
scrapFallback += maxScrap;
|
||||
}
|
||||
}
|
||||
|
||||
goodTotal = goodFallback;
|
||||
scrapTotal = scrapFallback;
|
||||
}
|
||||
|
||||
for (const row of kpiAgg) {
|
||||
const maxTarget = safeNum(row._max.target);
|
||||
if (maxTarget != null) targetTotal += maxTarget;
|
||||
}
|
||||
|
||||
const events = await prisma.machineEvent.findMany({
|
||||
where: { ...baseWhere, ts: { gte: start, lte: end } },
|
||||
select: { eventType: true, data: true },
|
||||
});
|
||||
|
||||
let macrostopSec = 0;
|
||||
let microstopSec = 0;
|
||||
let slowCycleCount = 0;
|
||||
let qualitySpikeCount = 0;
|
||||
let performanceDegradationCount = 0;
|
||||
let oeeDropCount = 0;
|
||||
|
||||
for (const e of events) {
|
||||
const type = String(e.eventType ?? "").toLowerCase();
|
||||
let blob: unknown = e.data;
|
||||
|
||||
if (typeof blob === "string") {
|
||||
try {
|
||||
blob = JSON.parse(blob);
|
||||
} catch {
|
||||
blob = null;
|
||||
}
|
||||
}
|
||||
|
||||
const blobRecord = typeof blob === "object" && blob !== null ? (blob as Record<string, unknown>) : null;
|
||||
const innerCandidate = blobRecord?.data ?? blobRecord ?? {};
|
||||
const inner =
|
||||
typeof innerCandidate === "object" && innerCandidate !== null
|
||||
? (innerCandidate as Record<string, unknown>)
|
||||
: {};
|
||||
const stopSec =
|
||||
(typeof inner?.stoppage_duration_seconds === "number" && inner.stoppage_duration_seconds) ||
|
||||
(typeof inner?.stop_duration_seconds === "number" && inner.stop_duration_seconds) ||
|
||||
0;
|
||||
|
||||
if (type === "macrostop") macrostopSec += Number(stopSec) || 0;
|
||||
else if (type === "microstop") microstopSec += Number(stopSec) || 0;
|
||||
else if (type === "slow-cycle") slowCycleCount += 1;
|
||||
else if (type === "quality-spike") qualitySpikeCount += 1;
|
||||
else if (type === "performance-degradation") performanceDegradationCount += 1;
|
||||
else if (type === "oee-drop") oeeDropCount += 1;
|
||||
}
|
||||
|
||||
type TrendPoint = { t: string; v: number };
|
||||
|
||||
const trend: {
|
||||
oee: TrendPoint[];
|
||||
availability: TrendPoint[];
|
||||
performance: TrendPoint[];
|
||||
quality: TrendPoint[];
|
||||
scrapRate: TrendPoint[];
|
||||
} = {
|
||||
oee: [],
|
||||
availability: [],
|
||||
performance: [],
|
||||
quality: [],
|
||||
scrapRate: [],
|
||||
};
|
||||
|
||||
for (const k of kpiRows) {
|
||||
const t = k.ts.toISOString();
|
||||
if (safeNum(k.oee) != null) trend.oee.push({ t, v: Number(k.oee) });
|
||||
if (safeNum(k.availability) != null) trend.availability.push({ t, v: Number(k.availability) });
|
||||
if (safeNum(k.performance) != null) trend.performance.push({ t, v: Number(k.performance) });
|
||||
if (safeNum(k.quality) != null) trend.quality.push({ t, v: Number(k.quality) });
|
||||
|
||||
const good = safeNum(k.good);
|
||||
const scrap = safeNum(k.scrap);
|
||||
if (good != null && scrap != null && good + scrap > 0) {
|
||||
trend.scrapRate.push({ t, v: (scrap / (good + scrap)) * 100 });
|
||||
}
|
||||
}
|
||||
const cycleRows = await prisma.machineCycle.findMany({
|
||||
where: { ...baseWhere, ts: { gte: start, lte: end } },
|
||||
select: { actualCycleTime: true },
|
||||
});
|
||||
|
||||
const values = cycleRows
|
||||
.map((c) => Number(c.actualCycleTime))
|
||||
.filter((v) => Number.isFinite(v) && v > 0)
|
||||
.sort((a, b) => a - b);
|
||||
|
||||
let cycleTimeBins: {
|
||||
label: string;
|
||||
count: number;
|
||||
rangeStart?: number;
|
||||
rangeEnd?: number;
|
||||
overflow?: "low" | "high";
|
||||
minValue?: number;
|
||||
maxValue?: number;
|
||||
}[] = [];
|
||||
|
||||
if (values.length) {
|
||||
const pct = (p: number) => {
|
||||
const idx = Math.max(0, Math.min(values.length - 1, Math.floor(p * (values.length - 1))));
|
||||
return values[idx];
|
||||
};
|
||||
|
||||
const p5 = pct(0.05);
|
||||
const p95 = pct(0.95);
|
||||
|
||||
const inRange = values.filter((v) => v >= p5 && v <= p95);
|
||||
const low = values.filter((v) => v < p5);
|
||||
const high = values.filter((v) => v > p95);
|
||||
|
||||
const binCount = 10;
|
||||
const span = Math.max(0.1, p95 - p5);
|
||||
const step = span / binCount;
|
||||
|
||||
const counts = new Array(binCount).fill(0);
|
||||
for (const v of inRange) {
|
||||
const idx = Math.min(binCount - 1, Math.floor((v - p5) / step));
|
||||
counts[idx] += 1;
|
||||
}
|
||||
const decimals = step < 0.1 ? 2 : step < 1 ? 1 : 0;
|
||||
|
||||
cycleTimeBins = counts.map((count, i) => {
|
||||
const a = p5 + step * i;
|
||||
const b = p5 + step * (i + 1);
|
||||
return {
|
||||
label: `${a.toFixed(decimals)}-${b.toFixed(decimals)}s`,
|
||||
count,
|
||||
rangeStart: a,
|
||||
rangeEnd: b,
|
||||
};
|
||||
});
|
||||
|
||||
|
||||
if (low.length) {
|
||||
cycleTimeBins.unshift({
|
||||
label: `< ${p5.toFixed(1)}s`,
|
||||
count: low.length,
|
||||
rangeEnd: p5,
|
||||
overflow: "low",
|
||||
minValue: low[0],
|
||||
maxValue: low[low.length - 1],
|
||||
});
|
||||
}
|
||||
|
||||
if (high.length) {
|
||||
cycleTimeBins.push({
|
||||
label: `> ${p95.toFixed(1)}s`,
|
||||
count: high.length,
|
||||
rangeStart: p95,
|
||||
overflow: "high",
|
||||
minValue: high[0],
|
||||
maxValue: high[high.length - 1],
|
||||
});
|
||||
}
|
||||
}
|
||||
const scrapRate =
|
||||
goodTotal + scrapTotal > 0 ? (scrapTotal / (goodTotal + scrapTotal)) * 100 : null;
|
||||
|
||||
|
||||
|
||||
// top scrap SKU / work order (from cycles)
|
||||
const scrapBySku = new Map<string, number>();
|
||||
const scrapByWo = new Map<string, number>();
|
||||
|
||||
const scrapRows = await prisma.machineCycle.findMany({
|
||||
where: { ...baseWhere, ts: { gte: start, lte: end } },
|
||||
select: { sku: true, workOrderId: true, scrapDelta: true },
|
||||
});
|
||||
|
||||
for (const row of scrapRows) {
|
||||
const scrap = safeNum(row.scrapDelta);
|
||||
if (scrap == null || scrap <= 0) continue;
|
||||
if (row.sku) scrapBySku.set(row.sku, (scrapBySku.get(row.sku) ?? 0) + scrap);
|
||||
if (row.workOrderId) scrapByWo.set(row.workOrderId, (scrapByWo.get(row.workOrderId) ?? 0) + scrap);
|
||||
}
|
||||
|
||||
const topScrapSku = [...scrapBySku.entries()].sort((a, b) => b[1] - a[1])[0]?.[0] ?? null;
|
||||
const topScrapWorkOrder = [...scrapByWo.entries()].sort((a, b) => b[1] - a[1])[0]?.[0] ?? null;
|
||||
|
||||
const oeeAvg = oeeCount ? oeeSum / oeeCount : null;
|
||||
const availabilityAvg = availCount ? availSum / availCount : null;
|
||||
const performanceAvg = perfCount ? perfSum / perfCount : null;
|
||||
const qualityAvg = qualCount ? qualSum / qualCount : null;
|
||||
|
||||
// insights
|
||||
const insights: string[] = [];
|
||||
if (scrapRate != null && scrapRate > 5) insights.push(`Scrap rate is ${scrapRate.toFixed(1)}% (above 5%).`);
|
||||
if (performanceAvg != null && performanceAvg < 85) insights.push("Performance below 85%.");
|
||||
if (availabilityAvg != null && availabilityAvg < 85) insights.push("Availability below 85%.");
|
||||
if (oeeAvg != null && oeeAvg < 85) insights.push("OEE below 85%.");
|
||||
if (macrostopSec > 1800) insights.push("Macrostop time exceeds 30 minutes in this range.");
|
||||
|
||||
|
||||
|
||||
return NextResponse.json({
|
||||
ok: true,
|
||||
summary: {
|
||||
oeeAvg,
|
||||
availabilityAvg,
|
||||
performanceAvg,
|
||||
qualityAvg,
|
||||
goodTotal,
|
||||
scrapTotal,
|
||||
targetTotal,
|
||||
scrapRate,
|
||||
topScrapSku,
|
||||
topScrapWorkOrder,
|
||||
},
|
||||
|
||||
downtime: {
|
||||
macrostopSec,
|
||||
microstopSec,
|
||||
slowCycleCount,
|
||||
qualitySpikeCount,
|
||||
performanceDegradationCount,
|
||||
oeeDropCount,
|
||||
},
|
||||
trend,
|
||||
insights,
|
||||
distribution: {
|
||||
cycleTime: cycleTimeBins
|
||||
},
|
||||
|
||||
});
|
||||
}
|
||||
410
app/api/settings/machines/[machineId]/route.ts
Normal file
410
app/api/settings/machines/[machineId]/route.ts
Normal file
@@ -0,0 +1,410 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import type { NextRequest } from "next/server";
|
||||
import { Prisma } from "@prisma/client";
|
||||
import { prisma } from "@/lib/prisma";
|
||||
import { requireSession } from "@/lib/auth/requireSession";
|
||||
import { toJsonValue } from "@/lib/prismaJson";
|
||||
import {
|
||||
DEFAULT_ALERTS,
|
||||
DEFAULT_DEFAULTS,
|
||||
DEFAULT_SHIFT,
|
||||
applyOverridePatch,
|
||||
buildSettingsPayload,
|
||||
deepMerge,
|
||||
validateDefaults,
|
||||
validateShiftFields,
|
||||
validateShiftSchedule,
|
||||
validateThresholds,
|
||||
} from "@/lib/settings";
|
||||
import { publishSettingsUpdate } from "@/lib/mqtt";
|
||||
import { z } from "zod";
|
||||
|
||||
function isPlainObject(value: unknown): value is Record<string, unknown> {
|
||||
return !!value && typeof value === "object" && !Array.isArray(value);
|
||||
}
|
||||
|
||||
function canManageSettings(role?: string | null) {
|
||||
return role === "OWNER" || role === "ADMIN";
|
||||
}
|
||||
|
||||
const machineIdSchema = z.string().uuid();
|
||||
const machineSettingsSchema = z
|
||||
.object({
|
||||
source: z.string().trim().max(40).optional(),
|
||||
overrides: z.any().optional(),
|
||||
})
|
||||
.passthrough();
|
||||
|
||||
function pickAllowedOverrides(raw: unknown) {
|
||||
if (!isPlainObject(raw)) return {};
|
||||
const out: Record<string, unknown> = {};
|
||||
for (const key of ["shiftSchedule", "thresholds", "alerts", "defaults"]) {
|
||||
if (raw[key] !== undefined) out[key] = raw[key];
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
async function ensureOrgSettings(
|
||||
tx: Prisma.TransactionClient,
|
||||
orgId: string,
|
||||
userId?: string | null
|
||||
) {
|
||||
let settings = await tx.orgSettings.findUnique({
|
||||
where: { orgId },
|
||||
});
|
||||
|
||||
if (settings) {
|
||||
let shifts = await tx.orgShift.findMany({
|
||||
where: { orgId },
|
||||
orderBy: { sortOrder: "asc" },
|
||||
});
|
||||
if (!shifts.length) {
|
||||
await tx.orgShift.create({
|
||||
data: {
|
||||
orgId,
|
||||
name: DEFAULT_SHIFT.name,
|
||||
startTime: DEFAULT_SHIFT.start,
|
||||
endTime: DEFAULT_SHIFT.end,
|
||||
sortOrder: 1,
|
||||
enabled: true,
|
||||
},
|
||||
});
|
||||
shifts = await tx.orgShift.findMany({
|
||||
where: { orgId },
|
||||
orderBy: { sortOrder: "asc" },
|
||||
});
|
||||
}
|
||||
return { settings, shifts };
|
||||
}
|
||||
|
||||
settings = await tx.orgSettings.create({
|
||||
data: {
|
||||
orgId,
|
||||
timezone: "UTC",
|
||||
shiftChangeCompMin: 10,
|
||||
lunchBreakMin: 30,
|
||||
stoppageMultiplier: 1.5,
|
||||
macroStoppageMultiplier: 5,
|
||||
oeeAlertThresholdPct: 90,
|
||||
performanceThresholdPct: 85,
|
||||
qualitySpikeDeltaPct: 5,
|
||||
alertsJson: DEFAULT_ALERTS,
|
||||
defaultsJson: DEFAULT_DEFAULTS,
|
||||
updatedBy: userId ?? null,
|
||||
},
|
||||
});
|
||||
|
||||
await tx.orgShift.create({
|
||||
data: {
|
||||
orgId,
|
||||
name: DEFAULT_SHIFT.name,
|
||||
startTime: DEFAULT_SHIFT.start,
|
||||
endTime: DEFAULT_SHIFT.end,
|
||||
sortOrder: 1,
|
||||
enabled: true,
|
||||
},
|
||||
});
|
||||
|
||||
const shifts = await tx.orgShift.findMany({
|
||||
where: { orgId },
|
||||
orderBy: { sortOrder: "asc" },
|
||||
});
|
||||
return { settings, shifts };
|
||||
}
|
||||
|
||||
export async function GET(
|
||||
req: NextRequest,
|
||||
{ params }: { params: Promise<{ machineId: string }> }
|
||||
) {
|
||||
const { machineId } = await params;
|
||||
if (!machineIdSchema.safeParse(machineId).success) {
|
||||
return NextResponse.json({ ok: false, error: "Invalid machine id" }, { status: 400 });
|
||||
}
|
||||
|
||||
const session = await requireSession();
|
||||
let orgId: string | null = null;
|
||||
let userId: string | null = null;
|
||||
let machine: { id: string; orgId: string } | null = null;
|
||||
|
||||
if (session) {
|
||||
machine = await prisma.machine.findFirst({
|
||||
where: { id: machineId, orgId: session.orgId },
|
||||
select: { id: true, orgId: true },
|
||||
});
|
||||
if (!machine) return NextResponse.json({ ok: false, error: "Not found" }, { status: 404 });
|
||||
orgId = machine.orgId;
|
||||
userId = session.userId;
|
||||
} else {
|
||||
const apiKey = req.headers.get("x-api-key");
|
||||
if (!apiKey) return NextResponse.json({ ok: false, error: "Unauthorized" }, { status: 401 });
|
||||
machine = await prisma.machine.findFirst({
|
||||
where: { id: machineId, apiKey },
|
||||
select: { id: true, orgId: true },
|
||||
});
|
||||
if (!machine) return NextResponse.json({ ok: false, error: "Unauthorized" }, { status: 401 });
|
||||
orgId = machine.orgId;
|
||||
}
|
||||
|
||||
const { settings, overrides } = await prisma.$transaction(async (tx) => {
|
||||
const orgSettings = await ensureOrgSettings(tx, orgId as string, userId);
|
||||
if (!orgSettings?.settings) throw new Error("SETTINGS_NOT_FOUND");
|
||||
|
||||
const machineSettings = await tx.machineSettings.findUnique({
|
||||
where: { machineId },
|
||||
select: { overridesJson: true },
|
||||
});
|
||||
|
||||
const orgPayload = buildSettingsPayload(orgSettings.settings, orgSettings.shifts ?? []);
|
||||
const rawOverrides = pickAllowedOverrides(machineSettings?.overridesJson ?? {});
|
||||
const effective = deepMerge(orgPayload, rawOverrides);
|
||||
|
||||
return { settings: { org: orgPayload, effective }, overrides: rawOverrides };
|
||||
});
|
||||
|
||||
return NextResponse.json({
|
||||
ok: true,
|
||||
machineId,
|
||||
orgSettings: settings.org,
|
||||
effectiveSettings: settings.effective,
|
||||
overrides,
|
||||
});
|
||||
}
|
||||
|
||||
export async function PUT(
|
||||
req: NextRequest,
|
||||
{ params }: { params: Promise<{ machineId: string }> }
|
||||
) {
|
||||
const session = await requireSession();
|
||||
if (!session) return NextResponse.json({ ok: false, error: "Unauthorized" }, { status: 401 });
|
||||
|
||||
const membership = await prisma.orgUser.findUnique({
|
||||
where: { orgId_userId: { orgId: session.orgId, userId: session.userId } },
|
||||
select: { role: true },
|
||||
});
|
||||
if (!canManageSettings(membership?.role)) {
|
||||
return NextResponse.json({ ok: false, error: "Forbidden" }, { status: 403 });
|
||||
}
|
||||
|
||||
const { machineId } = await params;
|
||||
if (!machineIdSchema.safeParse(machineId).success) {
|
||||
return NextResponse.json({ ok: false, error: "Invalid machine id" }, { status: 400 });
|
||||
}
|
||||
|
||||
const machine = await prisma.machine.findFirst({
|
||||
where: { id: machineId, orgId: session.orgId },
|
||||
select: { id: true },
|
||||
});
|
||||
|
||||
if (!machine) return NextResponse.json({ ok: false, error: "Not found" }, { status: 404 });
|
||||
|
||||
const body = await req.json().catch(() => ({}));
|
||||
const parsed = machineSettingsSchema.safeParse(body);
|
||||
if (!parsed.success) {
|
||||
return NextResponse.json({ ok: false, error: "Invalid settings payload" }, { status: 400 });
|
||||
}
|
||||
const source = String(parsed.data.source ?? "control_tower");
|
||||
|
||||
let patch = parsed.data.overrides ?? parsed.data;
|
||||
if (patch === null) {
|
||||
patch = null;
|
||||
}
|
||||
|
||||
if (patch && !isPlainObject(patch)) {
|
||||
return NextResponse.json({ ok: false, error: "overrides must be an object or null" }, { status: 400 });
|
||||
}
|
||||
|
||||
if (patch && Object.keys(patch).length === 0) {
|
||||
return NextResponse.json({ ok: false, error: "No overrides provided" }, { status: 400 });
|
||||
}
|
||||
|
||||
if (patch && Object.keys(pickAllowedOverrides(patch)).length !== Object.keys(patch).length) {
|
||||
return NextResponse.json({ ok: false, error: "overrides contain unsupported keys" }, { status: 400 });
|
||||
}
|
||||
|
||||
if (patch?.shiftSchedule && !isPlainObject(patch.shiftSchedule)) {
|
||||
return NextResponse.json({ ok: false, error: "shiftSchedule must be an object" }, { status: 400 });
|
||||
}
|
||||
if (patch?.thresholds !== undefined && patch.thresholds !== null && !isPlainObject(patch.thresholds)) {
|
||||
return NextResponse.json({ ok: false, error: "thresholds must be an object" }, { status: 400 });
|
||||
}
|
||||
if (patch?.alerts !== undefined && patch.alerts !== null && !isPlainObject(patch.alerts)) {
|
||||
return NextResponse.json({ ok: false, error: "alerts must be an object" }, { status: 400 });
|
||||
}
|
||||
if (patch?.defaults !== undefined && patch.defaults !== null && !isPlainObject(patch.defaults)) {
|
||||
return NextResponse.json({ ok: false, error: "defaults must be an object" }, { status: 400 });
|
||||
}
|
||||
|
||||
const shiftValidation = validateShiftFields(
|
||||
patch?.shiftSchedule?.shiftChangeCompensationMin,
|
||||
patch?.shiftSchedule?.lunchBreakMin
|
||||
);
|
||||
if (!shiftValidation.ok) {
|
||||
return NextResponse.json({ ok: false, error: shiftValidation.error }, { status: 400 });
|
||||
}
|
||||
|
||||
const thresholdsValidation = validateThresholds(patch?.thresholds);
|
||||
if (!thresholdsValidation.ok) {
|
||||
return NextResponse.json({ ok: false, error: thresholdsValidation.error }, { status: 400 });
|
||||
}
|
||||
|
||||
const defaultsValidation = validateDefaults(patch?.defaults);
|
||||
if (!defaultsValidation.ok) {
|
||||
return NextResponse.json({ ok: false, error: defaultsValidation.error }, { status: 400 });
|
||||
}
|
||||
|
||||
if (patch?.shiftSchedule?.shifts !== undefined) {
|
||||
const shiftResult = validateShiftSchedule(patch.shiftSchedule.shifts);
|
||||
if (!shiftResult.ok) {
|
||||
return NextResponse.json({ ok: false, error: shiftResult.error }, { status: 400 });
|
||||
}
|
||||
patch = {
|
||||
...patch,
|
||||
shiftSchedule: {
|
||||
...patch.shiftSchedule,
|
||||
shifts: shiftResult.shifts?.map((s) => ({
|
||||
name: s.name,
|
||||
start: s.startTime,
|
||||
end: s.endTime,
|
||||
enabled: s.enabled !== false,
|
||||
})),
|
||||
},
|
||||
};
|
||||
}
|
||||
if (patch?.shiftSchedule) {
|
||||
patch = {
|
||||
...patch,
|
||||
shiftSchedule: {
|
||||
...patch.shiftSchedule,
|
||||
shiftChangeCompensationMin:
|
||||
patch.shiftSchedule.shiftChangeCompensationMin !== undefined
|
||||
? Number(patch.shiftSchedule.shiftChangeCompensationMin)
|
||||
: patch.shiftSchedule.shiftChangeCompensationMin,
|
||||
lunchBreakMin:
|
||||
patch.shiftSchedule.lunchBreakMin !== undefined
|
||||
? Number(patch.shiftSchedule.lunchBreakMin)
|
||||
: patch.shiftSchedule.lunchBreakMin,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
if (patch?.thresholds) {
|
||||
patch = {
|
||||
...patch,
|
||||
thresholds: {
|
||||
...patch.thresholds,
|
||||
stoppageMultiplier:
|
||||
patch.thresholds.stoppageMultiplier !== undefined
|
||||
? Number(patch.thresholds.stoppageMultiplier)
|
||||
: patch.thresholds.stoppageMultiplier,
|
||||
macroStoppageMultiplier:
|
||||
patch.thresholds.macroStoppageMultiplier !== undefined
|
||||
? Number(patch.thresholds.macroStoppageMultiplier)
|
||||
: patch.thresholds.macroStoppageMultiplier,
|
||||
oeeAlertThresholdPct:
|
||||
patch.thresholds.oeeAlertThresholdPct !== undefined
|
||||
? Number(patch.thresholds.oeeAlertThresholdPct)
|
||||
: patch.thresholds.oeeAlertThresholdPct,
|
||||
performanceThresholdPct:
|
||||
patch.thresholds.performanceThresholdPct !== undefined
|
||||
? Number(patch.thresholds.performanceThresholdPct)
|
||||
: patch.thresholds.performanceThresholdPct,
|
||||
qualitySpikeDeltaPct:
|
||||
patch.thresholds.qualitySpikeDeltaPct !== undefined
|
||||
? Number(patch.thresholds.qualitySpikeDeltaPct)
|
||||
: patch.thresholds.qualitySpikeDeltaPct,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
if (patch?.defaults) {
|
||||
patch = {
|
||||
...patch,
|
||||
defaults: {
|
||||
...patch.defaults,
|
||||
moldTotal:
|
||||
patch.defaults.moldTotal !== undefined ? Number(patch.defaults.moldTotal) : patch.defaults.moldTotal,
|
||||
moldActive:
|
||||
patch.defaults.moldActive !== undefined ? Number(patch.defaults.moldActive) : patch.defaults.moldActive,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
const result = await prisma.$transaction(async (tx) => {
|
||||
const orgSettings = await ensureOrgSettings(tx, session.orgId, session.userId);
|
||||
if (!orgSettings?.settings) throw new Error("SETTINGS_NOT_FOUND");
|
||||
|
||||
const existing = await tx.machineSettings.findUnique({
|
||||
where: { machineId },
|
||||
select: { overridesJson: true },
|
||||
});
|
||||
|
||||
let nextOverrides: Record<string, unknown> | null = null;
|
||||
if (patch === null) {
|
||||
nextOverrides = null;
|
||||
} else {
|
||||
const merged = applyOverridePatch(existing?.overridesJson ?? {}, patch);
|
||||
nextOverrides = Object.keys(merged).length ? merged : null;
|
||||
}
|
||||
const nextOverridesJson =
|
||||
nextOverrides === null ? Prisma.DbNull : toJsonValue(nextOverrides);
|
||||
|
||||
const saved = await tx.machineSettings.upsert({
|
||||
where: { machineId },
|
||||
update: {
|
||||
overridesJson: nextOverridesJson,
|
||||
updatedBy: session.userId,
|
||||
},
|
||||
create: {
|
||||
machineId,
|
||||
orgId: session.orgId,
|
||||
overridesJson: nextOverridesJson,
|
||||
updatedBy: session.userId,
|
||||
},
|
||||
});
|
||||
|
||||
await tx.settingsAudit.create({
|
||||
data: {
|
||||
orgId: session.orgId,
|
||||
machineId,
|
||||
actorId: session.userId,
|
||||
source,
|
||||
payloadJson: body,
|
||||
},
|
||||
});
|
||||
|
||||
const orgPayload = buildSettingsPayload(orgSettings.settings, orgSettings.shifts ?? []);
|
||||
const overrides = pickAllowedOverrides(saved.overridesJson ?? {});
|
||||
const effective = deepMerge(orgPayload, overrides);
|
||||
|
||||
return {
|
||||
orgPayload,
|
||||
overrides,
|
||||
effective,
|
||||
overridesUpdatedAt: saved.updatedAt,
|
||||
};
|
||||
});
|
||||
|
||||
const overridesUpdatedAt =
|
||||
result.overridesUpdatedAt && result.overridesUpdatedAt instanceof Date
|
||||
? result.overridesUpdatedAt.toISOString()
|
||||
: undefined;
|
||||
try {
|
||||
await publishSettingsUpdate({
|
||||
orgId: session.orgId,
|
||||
machineId,
|
||||
version: Number(result.orgPayload.version ?? 0),
|
||||
source,
|
||||
overridesUpdatedAt,
|
||||
});
|
||||
} catch (err) {
|
||||
console.warn("[settings machine PUT] MQTT publish failed", err);
|
||||
}
|
||||
|
||||
return NextResponse.json({
|
||||
ok: true,
|
||||
machineId,
|
||||
orgSettings: result.orgPayload,
|
||||
effectiveSettings: result.effective,
|
||||
overrides: result.overrides,
|
||||
});
|
||||
}
|
||||
383
app/api/settings/route.ts
Normal file
383
app/api/settings/route.ts
Normal file
@@ -0,0 +1,383 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import { Prisma } from "@prisma/client";
|
||||
import { prisma } from "@/lib/prisma";
|
||||
import { requireSession } from "@/lib/auth/requireSession";
|
||||
import {
|
||||
DEFAULT_ALERTS,
|
||||
DEFAULT_DEFAULTS,
|
||||
DEFAULT_SHIFT,
|
||||
buildSettingsPayload,
|
||||
normalizeAlerts,
|
||||
normalizeDefaults,
|
||||
stripUndefined,
|
||||
validateDefaults,
|
||||
validateShiftFields,
|
||||
validateShiftSchedule,
|
||||
validateThresholds,
|
||||
} from "@/lib/settings";
|
||||
import { publishSettingsUpdate } from "@/lib/mqtt";
|
||||
import { z } from "zod";
|
||||
|
||||
type ValidShift = {
|
||||
name: string;
|
||||
startTime: string;
|
||||
endTime: string;
|
||||
sortOrder: number;
|
||||
enabled: boolean;
|
||||
};
|
||||
|
||||
function isPlainObject(value: unknown): value is Record<string, unknown> {
|
||||
return !!value && typeof value === "object" && !Array.isArray(value);
|
||||
}
|
||||
|
||||
function canManageSettings(role?: string | null) {
|
||||
return role === "OWNER" || role === "ADMIN";
|
||||
}
|
||||
|
||||
const settingsPayloadSchema = z
|
||||
.object({
|
||||
source: z.string().trim().max(40).optional(),
|
||||
modules: z.any().optional(),
|
||||
timezone: z.string().trim().max(64).optional(),
|
||||
shiftSchedule: z.any().optional(),
|
||||
thresholds: z.any().optional(),
|
||||
alerts: z.any().optional(),
|
||||
defaults: z.any().optional(),
|
||||
version: z.union([z.number(), z.string()]).optional(),
|
||||
})
|
||||
.passthrough();
|
||||
|
||||
async function ensureOrgSettings(tx: Prisma.TransactionClient, orgId: string, userId: string) {
|
||||
let settings = await tx.orgSettings.findUnique({
|
||||
where: { orgId },
|
||||
});
|
||||
|
||||
if (settings) {
|
||||
let shifts = await tx.orgShift.findMany({
|
||||
where: { orgId },
|
||||
orderBy: { sortOrder: "asc" },
|
||||
});
|
||||
if (!shifts.length) {
|
||||
await tx.orgShift.create({
|
||||
data: {
|
||||
orgId,
|
||||
name: DEFAULT_SHIFT.name,
|
||||
startTime: DEFAULT_SHIFT.start,
|
||||
endTime: DEFAULT_SHIFT.end,
|
||||
sortOrder: 1,
|
||||
enabled: true,
|
||||
},
|
||||
});
|
||||
shifts = await tx.orgShift.findMany({
|
||||
where: { orgId },
|
||||
orderBy: { sortOrder: "asc" },
|
||||
});
|
||||
}
|
||||
return { settings, shifts };
|
||||
}
|
||||
|
||||
settings = await tx.orgSettings.create({
|
||||
data: {
|
||||
orgId,
|
||||
timezone: "UTC",
|
||||
shiftChangeCompMin: 10,
|
||||
lunchBreakMin: 30,
|
||||
stoppageMultiplier: 1.5,
|
||||
macroStoppageMultiplier: 5,
|
||||
oeeAlertThresholdPct: 90,
|
||||
performanceThresholdPct: 85,
|
||||
qualitySpikeDeltaPct: 5,
|
||||
alertsJson: DEFAULT_ALERTS,
|
||||
defaultsJson: { ...(DEFAULT_DEFAULTS as any), modules: { screenlessMode: false } },
|
||||
updatedBy: userId,
|
||||
},
|
||||
});
|
||||
|
||||
await tx.orgShift.create({
|
||||
data: {
|
||||
orgId,
|
||||
name: DEFAULT_SHIFT.name,
|
||||
startTime: DEFAULT_SHIFT.start,
|
||||
endTime: DEFAULT_SHIFT.end,
|
||||
sortOrder: 1,
|
||||
enabled: true,
|
||||
},
|
||||
});
|
||||
|
||||
const shifts = await tx.orgShift.findMany({
|
||||
where: { orgId },
|
||||
orderBy: { sortOrder: "asc" },
|
||||
});
|
||||
return { settings, shifts };
|
||||
}
|
||||
|
||||
export async function GET() {
|
||||
const session = await requireSession();
|
||||
if (!session) return NextResponse.json({ ok: false, error: "Unauthorized" }, { status: 401 });
|
||||
|
||||
try {
|
||||
const loaded = await prisma.$transaction(async (tx) => {
|
||||
const found = await ensureOrgSettings(tx, session.orgId, session.userId);
|
||||
if (!found?.settings) throw new Error("SETTINGS_NOT_FOUND");
|
||||
return found;
|
||||
});
|
||||
|
||||
const payload = buildSettingsPayload(loaded.settings, loaded.shifts ?? []);
|
||||
|
||||
const defaultsRaw = isPlainObject(loaded.settings.defaultsJson) ? (loaded.settings.defaultsJson as any) : {};
|
||||
const modulesRaw = isPlainObject(defaultsRaw.modules) ? defaultsRaw.modules : {};
|
||||
const modules = { screenlessMode: modulesRaw.screenlessMode === true };
|
||||
|
||||
return NextResponse.json({ ok: true, settings: { ...payload, modules } });
|
||||
|
||||
} catch (err) {
|
||||
console.error("[settings GET] failed", err);
|
||||
const message = err instanceof Error ? err.message : "Internal error";
|
||||
return NextResponse.json({ ok: false, error: message }, { status: 500 });
|
||||
}
|
||||
}
|
||||
|
||||
export async function PUT(req: Request) {
|
||||
const session = await requireSession();
|
||||
if (!session) return NextResponse.json({ ok: false, error: "Unauthorized" }, { status: 401 });
|
||||
|
||||
const membership = await prisma.orgUser.findUnique({
|
||||
where: { orgId_userId: { orgId: session.orgId, userId: session.userId } },
|
||||
select: { role: true },
|
||||
});
|
||||
if (!canManageSettings(membership?.role)) {
|
||||
return NextResponse.json({ ok: false, error: "Forbidden" }, { status: 403 });
|
||||
}
|
||||
|
||||
try {
|
||||
const body = await req.json().catch(() => ({}));
|
||||
const parsed = settingsPayloadSchema.safeParse(body);
|
||||
if (!parsed.success) {
|
||||
return NextResponse.json({ ok: false, error: "Invalid settings payload" }, { status: 400 });
|
||||
}
|
||||
|
||||
const source = String(parsed.data.source ?? "control_tower");
|
||||
const timezone = parsed.data.timezone;
|
||||
const shiftSchedule = parsed.data.shiftSchedule;
|
||||
const thresholds = parsed.data.thresholds;
|
||||
const alerts = parsed.data.alerts;
|
||||
const defaults = parsed.data.defaults;
|
||||
const expectedVersion = parsed.data.version;
|
||||
const modules = parsed.data.modules;
|
||||
|
||||
|
||||
|
||||
if (
|
||||
timezone === undefined &&
|
||||
shiftSchedule === undefined &&
|
||||
thresholds === undefined &&
|
||||
alerts === undefined &&
|
||||
defaults === undefined &&
|
||||
modules === undefined
|
||||
|
||||
) {
|
||||
return NextResponse.json({ ok: false, error: "No settings provided" }, { status: 400 });
|
||||
}
|
||||
|
||||
if (shiftSchedule && !isPlainObject(shiftSchedule)) {
|
||||
return NextResponse.json({ ok: false, error: "shiftSchedule must be an object" }, { status: 400 });
|
||||
}
|
||||
if (thresholds !== undefined && !isPlainObject(thresholds)) {
|
||||
return NextResponse.json({ ok: false, error: "thresholds must be an object" }, { status: 400 });
|
||||
}
|
||||
if (alerts !== undefined && !isPlainObject(alerts)) {
|
||||
return NextResponse.json({ ok: false, error: "alerts must be an object" }, { status: 400 });
|
||||
}
|
||||
if (defaults !== undefined && !isPlainObject(defaults)) {
|
||||
return NextResponse.json({ ok: false, error: "defaults must be an object" }, { status: 400 });
|
||||
}
|
||||
if (modules !== undefined && !isPlainObject(modules)) {
|
||||
return NextResponse.json({ ok: false, error: "Invalid modules payload" }, { status: 400 });
|
||||
}
|
||||
|
||||
const screenlessMode =
|
||||
modules && typeof (modules as any).screenlessMode === "boolean"
|
||||
? (modules as any).screenlessMode
|
||||
: undefined;
|
||||
|
||||
|
||||
|
||||
const shiftValidation = validateShiftFields(
|
||||
shiftSchedule?.shiftChangeCompensationMin,
|
||||
shiftSchedule?.lunchBreakMin
|
||||
);
|
||||
if (!shiftValidation.ok) {
|
||||
return NextResponse.json({ ok: false, error: shiftValidation.error }, { status: 400 });
|
||||
}
|
||||
|
||||
const thresholdsValidation = validateThresholds(thresholds);
|
||||
if (!thresholdsValidation.ok) {
|
||||
return NextResponse.json({ ok: false, error: thresholdsValidation.error }, { status: 400 });
|
||||
}
|
||||
|
||||
let shiftRows: ValidShift[] | null = null;
|
||||
if (shiftSchedule?.shifts !== undefined) {
|
||||
const shiftResult = validateShiftSchedule(shiftSchedule.shifts);
|
||||
if (!shiftResult.ok) {
|
||||
return NextResponse.json({ ok: false, error: shiftResult.error }, { status: 400 });
|
||||
}
|
||||
shiftRows = shiftResult.shifts ?? [];
|
||||
}
|
||||
const shiftRowsSafe = shiftRows ?? [];
|
||||
|
||||
const updated = await prisma.$transaction(async (tx) => {
|
||||
const current = await ensureOrgSettings(tx, session.orgId, session.userId);
|
||||
if (!current?.settings) throw new Error("SETTINGS_NOT_FOUND");
|
||||
|
||||
if (expectedVersion != null && Number(expectedVersion) !== Number(current.settings.version)) {
|
||||
return { error: "VERSION_MISMATCH", currentVersion: current.settings.version } as const;
|
||||
}
|
||||
|
||||
const nextAlerts =
|
||||
alerts !== undefined ? { ...normalizeAlerts(current.settings.alertsJson), ...alerts } : undefined;
|
||||
const currentDefaultsRaw = isPlainObject(current.settings.defaultsJson)
|
||||
? (current.settings.defaultsJson as any)
|
||||
: {};
|
||||
const currentModulesRaw = isPlainObject(currentDefaultsRaw.modules) ? currentDefaultsRaw.modules : {};
|
||||
|
||||
// Merge defaults core (moldTotal, etc.)
|
||||
const nextDefaultsCore =
|
||||
defaults !== undefined ? { ...normalizeDefaults(currentDefaultsRaw), ...defaults } : undefined;
|
||||
|
||||
// Validate merged defaults
|
||||
if (nextDefaultsCore) {
|
||||
const dv = validateDefaults(nextDefaultsCore);
|
||||
if (!dv.ok) return { error: dv.error } as const;
|
||||
}
|
||||
|
||||
// Merge modules
|
||||
const nextModules =
|
||||
screenlessMode === undefined
|
||||
? currentModulesRaw
|
||||
: { ...currentModulesRaw, screenlessMode };
|
||||
|
||||
// Write defaultsJson if either defaults changed OR modules changed
|
||||
const shouldWriteDefaultsJson = !!nextDefaultsCore || screenlessMode !== undefined;
|
||||
|
||||
const nextDefaultsJson = shouldWriteDefaultsJson
|
||||
? { ...(nextDefaultsCore ?? normalizeDefaults(currentDefaultsRaw)), modules: nextModules }
|
||||
: undefined;
|
||||
|
||||
|
||||
const updateData = stripUndefined({
|
||||
timezone: timezone !== undefined ? String(timezone) : undefined,
|
||||
shiftChangeCompMin:
|
||||
shiftSchedule?.shiftChangeCompensationMin !== undefined
|
||||
? Number(shiftSchedule.shiftChangeCompensationMin)
|
||||
: undefined,
|
||||
lunchBreakMin:
|
||||
shiftSchedule?.lunchBreakMin !== undefined ? Number(shiftSchedule.lunchBreakMin) : undefined,
|
||||
stoppageMultiplier:
|
||||
thresholds?.stoppageMultiplier !== undefined ? Number(thresholds.stoppageMultiplier) : undefined,
|
||||
macroStoppageMultiplier:
|
||||
thresholds?.macroStoppageMultiplier !== undefined
|
||||
? Number(thresholds.macroStoppageMultiplier)
|
||||
: undefined,
|
||||
oeeAlertThresholdPct:
|
||||
thresholds?.oeeAlertThresholdPct !== undefined ? Number(thresholds.oeeAlertThresholdPct) : undefined,
|
||||
performanceThresholdPct:
|
||||
thresholds?.performanceThresholdPct !== undefined
|
||||
? Number(thresholds.performanceThresholdPct)
|
||||
: undefined,
|
||||
qualitySpikeDeltaPct:
|
||||
thresholds?.qualitySpikeDeltaPct !== undefined ? Number(thresholds.qualitySpikeDeltaPct) : undefined,
|
||||
alertsJson: nextAlerts,
|
||||
defaultsJson: nextDefaultsJson,
|
||||
});
|
||||
|
||||
const hasShiftUpdate = shiftRows !== null;
|
||||
const hasSettingsUpdate = Object.keys(updateData).length > 0;
|
||||
|
||||
if (!hasShiftUpdate && !hasSettingsUpdate) {
|
||||
return { error: "No settings provided" } as const;
|
||||
}
|
||||
|
||||
const updateWithMeta = {
|
||||
...updateData,
|
||||
version: current.settings.version + 1,
|
||||
updatedBy: session.userId,
|
||||
};
|
||||
|
||||
await tx.orgSettings.update({
|
||||
where: { orgId: session.orgId },
|
||||
data: updateWithMeta,
|
||||
});
|
||||
|
||||
if (hasShiftUpdate) {
|
||||
await tx.orgShift.deleteMany({ where: { orgId: session.orgId } });
|
||||
if (shiftRowsSafe.length) {
|
||||
await tx.orgShift.createMany({
|
||||
data: shiftRowsSafe.map((s) => ({
|
||||
...s,
|
||||
orgId: session.orgId,
|
||||
})),
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
const refreshed = await tx.orgSettings.findUnique({
|
||||
where: { orgId: session.orgId },
|
||||
});
|
||||
if (!refreshed) throw new Error("SETTINGS_NOT_FOUND");
|
||||
const refreshedShifts = await tx.orgShift.findMany({
|
||||
where: { orgId: session.orgId },
|
||||
orderBy: { sortOrder: "asc" },
|
||||
});
|
||||
|
||||
await tx.settingsAudit.create({
|
||||
data: {
|
||||
orgId: session.orgId,
|
||||
actorId: session.userId,
|
||||
source,
|
||||
payloadJson: body,
|
||||
},
|
||||
});
|
||||
|
||||
return { settings: refreshed, shifts: refreshedShifts };
|
||||
});
|
||||
|
||||
if ("error" in updated && updated.error === "VERSION_MISMATCH") {
|
||||
return NextResponse.json(
|
||||
{ ok: false, error: "Version mismatch", currentVersion: updated.currentVersion },
|
||||
{ status: 409 }
|
||||
);
|
||||
}
|
||||
|
||||
if ("error" in updated) {
|
||||
return NextResponse.json({ ok: false, error: updated.error }, { status: 400 });
|
||||
}
|
||||
|
||||
const payload = buildSettingsPayload(updated.settings, updated.shifts ?? []);
|
||||
const updatedAt =
|
||||
typeof payload.updatedAt === "string"
|
||||
? payload.updatedAt
|
||||
: payload.updatedAt
|
||||
? payload.updatedAt.toISOString()
|
||||
: undefined;
|
||||
try {
|
||||
await publishSettingsUpdate({
|
||||
orgId: session.orgId,
|
||||
version: Number(payload.version ?? 0),
|
||||
source,
|
||||
updatedAt,
|
||||
});
|
||||
} catch (err) {
|
||||
console.warn("[settings PUT] MQTT publish failed", err);
|
||||
}
|
||||
const defaultsRaw = isPlainObject(updated.settings.defaultsJson) ? (updated.settings.defaultsJson as any) : {};
|
||||
const modulesRaw = isPlainObject(defaultsRaw.modules) ? defaultsRaw.modules : {};
|
||||
const modulesOut = { screenlessMode: modulesRaw.screenlessMode === true };
|
||||
|
||||
return NextResponse.json({ ok: true, settings: { ...payload, modules: modulesOut } });
|
||||
|
||||
} catch (err) {
|
||||
console.error("[settings PUT] failed", err);
|
||||
const message = err instanceof Error ? err.message : "Internal error";
|
||||
return NextResponse.json({ ok: false, error: message }, { status: 500 });
|
||||
}
|
||||
}
|
||||
137
app/api/signup/route.ts
Normal file
137
app/api/signup/route.ts
Normal file
@@ -0,0 +1,137 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import bcrypt from "bcrypt";
|
||||
import { randomBytes } from "crypto";
|
||||
import { prisma } from "@/lib/prisma";
|
||||
import { DEFAULT_ALERTS, DEFAULT_DEFAULTS, DEFAULT_SHIFT } from "@/lib/settings";
|
||||
import { buildVerifyEmail, sendEmail } from "@/lib/email";
|
||||
import { getBaseUrl } from "@/lib/appUrl";
|
||||
import { logLine } from "@/lib/logger";
|
||||
import { z } from "zod";
|
||||
|
||||
const signupSchema = z.object({
|
||||
orgName: z.string().trim().min(1).max(120),
|
||||
name: z.string().trim().min(1).max(80),
|
||||
email: z.string().trim().min(1).max(254).email(),
|
||||
password: z.string().min(8).max(256),
|
||||
});
|
||||
|
||||
function slugify(input: string) {
|
||||
const trimmed = input.trim().toLowerCase();
|
||||
const slug = trimmed
|
||||
.replace(/[^a-z0-9]+/g, "-")
|
||||
.replace(/^-+|-+$/g, "");
|
||||
return slug || "org";
|
||||
}
|
||||
|
||||
export async function POST(req: Request) {
|
||||
const body = await req.json().catch(() => ({}));
|
||||
const parsed = signupSchema.safeParse(body);
|
||||
if (!parsed.success) {
|
||||
return NextResponse.json({ ok: false, error: "Invalid signup payload" }, { status: 400 });
|
||||
}
|
||||
const orgName = parsed.data.orgName;
|
||||
const name = parsed.data.name;
|
||||
const email = parsed.data.email.toLowerCase();
|
||||
const password = parsed.data.password;
|
||||
|
||||
const existing = await prisma.user.findUnique({ where: { email } });
|
||||
if (existing) {
|
||||
return NextResponse.json({ ok: false, error: "Email already in use" }, { status: 409 });
|
||||
}
|
||||
|
||||
const baseSlug = slugify(orgName);
|
||||
let slug = baseSlug;
|
||||
let counter = 1;
|
||||
while (await prisma.org.findUnique({ where: { slug } })) {
|
||||
counter += 1;
|
||||
slug = `${baseSlug}-${counter}`;
|
||||
}
|
||||
|
||||
const passwordHash = await bcrypt.hash(password, 10);
|
||||
const verificationToken = randomBytes(24).toString("hex");
|
||||
const verificationExpiresAt = new Date(Date.now() + 24 * 60 * 60 * 1000);
|
||||
|
||||
await prisma.$transaction(async (tx) => {
|
||||
const org = await tx.org.create({
|
||||
data: { name: orgName, slug },
|
||||
});
|
||||
|
||||
const user = await tx.user.create({
|
||||
data: {
|
||||
email,
|
||||
name,
|
||||
passwordHash,
|
||||
emailVerificationToken: verificationToken,
|
||||
emailVerificationExpiresAt: verificationExpiresAt,
|
||||
},
|
||||
});
|
||||
|
||||
await tx.orgUser.create({
|
||||
data: {
|
||||
orgId: org.id,
|
||||
userId: user.id,
|
||||
role: "OWNER",
|
||||
},
|
||||
});
|
||||
|
||||
await tx.orgSettings.create({
|
||||
data: {
|
||||
orgId: org.id,
|
||||
timezone: "UTC",
|
||||
shiftChangeCompMin: 10,
|
||||
lunchBreakMin: 30,
|
||||
stoppageMultiplier: 1.5,
|
||||
macroStoppageMultiplier: 5,
|
||||
oeeAlertThresholdPct: 90,
|
||||
performanceThresholdPct: 85,
|
||||
qualitySpikeDeltaPct: 5,
|
||||
alertsJson: DEFAULT_ALERTS,
|
||||
defaultsJson: DEFAULT_DEFAULTS,
|
||||
updatedBy: user.id,
|
||||
},
|
||||
});
|
||||
|
||||
await tx.orgShift.create({
|
||||
data: {
|
||||
orgId: org.id,
|
||||
name: DEFAULT_SHIFT.name,
|
||||
startTime: DEFAULT_SHIFT.start,
|
||||
endTime: DEFAULT_SHIFT.end,
|
||||
sortOrder: 1,
|
||||
enabled: true,
|
||||
},
|
||||
});
|
||||
|
||||
return { org, user };
|
||||
});
|
||||
|
||||
const baseUrl = getBaseUrl(req);
|
||||
const verifyUrl = `${baseUrl}/api/verify-email?token=${verificationToken}`;
|
||||
const appName = "MIS Control Tower";
|
||||
const emailContent = buildVerifyEmail({ appName, verifyUrl });
|
||||
|
||||
let emailSent = true;
|
||||
try {
|
||||
await sendEmail({
|
||||
to: email,
|
||||
subject: emailContent.subject,
|
||||
text: emailContent.text,
|
||||
html: emailContent.html,
|
||||
});
|
||||
} catch (err: unknown) {
|
||||
emailSent = false;
|
||||
const error = err as { message?: string; code?: string; response?: unknown; responseCode?: number };
|
||||
logLine("signup.verify_email.failed", {
|
||||
email,
|
||||
message: error?.message,
|
||||
code: error?.code,
|
||||
response: error?.response,
|
||||
responseCode: error?.responseCode,
|
||||
});
|
||||
}
|
||||
return NextResponse.json({
|
||||
ok: true,
|
||||
verificationRequired: true,
|
||||
emailSent,
|
||||
});
|
||||
}
|
||||
62
app/api/verify-email/route.ts
Normal file
62
app/api/verify-email/route.ts
Normal file
@@ -0,0 +1,62 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import { prisma } from "@/lib/prisma";
|
||||
import { buildSessionCookieOptions, COOKIE_NAME, SESSION_DAYS } from "@/lib/auth/sessionCookie";
|
||||
import { getBaseUrl } from "@/lib/appUrl";
|
||||
|
||||
export async function GET(req: Request) {
|
||||
const url = new URL(req.url);
|
||||
const token = url.searchParams.get("token");
|
||||
const wantsJson = req.headers.get("accept")?.includes("application/json");
|
||||
|
||||
if (!token) {
|
||||
return NextResponse.json({ ok: false, error: "Missing token" }, { status: 400 });
|
||||
}
|
||||
|
||||
const user = await prisma.user.findFirst({
|
||||
where: {
|
||||
emailVerificationToken: token,
|
||||
emailVerificationExpiresAt: { gt: new Date() },
|
||||
},
|
||||
});
|
||||
|
||||
if (!user) {
|
||||
return NextResponse.json({ ok: false, error: "Invalid or expired token" }, { status: 404 });
|
||||
}
|
||||
|
||||
await prisma.user.update({
|
||||
where: { id: user.id },
|
||||
data: {
|
||||
emailVerifiedAt: new Date(),
|
||||
emailVerificationToken: null,
|
||||
emailVerificationExpiresAt: null,
|
||||
},
|
||||
});
|
||||
|
||||
const membership = await prisma.orgUser.findFirst({
|
||||
where: { userId: user.id },
|
||||
orderBy: { createdAt: "asc" },
|
||||
});
|
||||
|
||||
if (!membership) {
|
||||
return NextResponse.json({ ok: false, error: "No organization found" }, { status: 403 });
|
||||
}
|
||||
|
||||
const expiresAt = new Date(Date.now() + SESSION_DAYS * 24 * 60 * 60 * 1000);
|
||||
const session = await prisma.session.create({
|
||||
data: {
|
||||
userId: user.id,
|
||||
orgId: membership.orgId,
|
||||
expiresAt,
|
||||
},
|
||||
});
|
||||
|
||||
if (wantsJson) {
|
||||
const res = NextResponse.json({ ok: true, next: "/machines" });
|
||||
res.cookies.set(COOKIE_NAME, session.id, buildSessionCookieOptions(req));
|
||||
return res;
|
||||
}
|
||||
|
||||
const res = NextResponse.redirect(new URL("/machines", getBaseUrl(req)));
|
||||
res.cookies.set(COOKIE_NAME, session.id, buildSessionCookieOptions(req));
|
||||
return res;
|
||||
}
|
||||
49
app/api/work-orders/machines/[machineId]/route.ts
Normal file
49
app/api/work-orders/machines/[machineId]/route.ts
Normal file
@@ -0,0 +1,49 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import type { NextRequest } from "next/server";
|
||||
import { prisma } from "@/lib/prisma";
|
||||
import { requireSession } from "@/lib/auth/requireSession";
|
||||
|
||||
export async function GET(
|
||||
req: NextRequest,
|
||||
{ params }: { params: Promise<{ machineId: string }> }
|
||||
) {
|
||||
const { machineId } = await params;
|
||||
|
||||
const session = await requireSession();
|
||||
let orgId: string | null = null;
|
||||
|
||||
if (session) {
|
||||
const machine = await prisma.machine.findFirst({
|
||||
where: { id: machineId, orgId: session.orgId },
|
||||
select: { id: true, orgId: true },
|
||||
});
|
||||
if (!machine) return NextResponse.json({ ok: false, error: "Not found" }, { status: 404 });
|
||||
orgId = machine.orgId;
|
||||
} else {
|
||||
const apiKey = req.headers.get("x-api-key");
|
||||
if (!apiKey) return NextResponse.json({ ok: false, error: "Unauthorized" }, { status: 401 });
|
||||
const machine = await prisma.machine.findFirst({
|
||||
where: { id: machineId, apiKey },
|
||||
select: { id: true, orgId: true },
|
||||
});
|
||||
if (!machine) return NextResponse.json({ ok: false, error: "Unauthorized" }, { status: 401 });
|
||||
orgId = machine.orgId;
|
||||
}
|
||||
|
||||
const rows = await prisma.machineWorkOrder.findMany({
|
||||
where: { machineId, orgId: orgId as string, status: { not: "DONE" } },
|
||||
orderBy: { createdAt: "desc" },
|
||||
});
|
||||
|
||||
return NextResponse.json({
|
||||
ok: true,
|
||||
machineId,
|
||||
workOrders: rows.map((row) => ({
|
||||
workOrderId: row.workOrderId,
|
||||
sku: row.sku,
|
||||
targetQty: row.targetQty,
|
||||
cycleTime: row.cycleTime,
|
||||
status: row.status,
|
||||
})),
|
||||
});
|
||||
}
|
||||
170
app/api/work-orders/route.ts
Normal file
170
app/api/work-orders/route.ts
Normal file
@@ -0,0 +1,170 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import type { NextRequest } from "next/server";
|
||||
import { prisma } from "@/lib/prisma";
|
||||
import { requireSession } from "@/lib/auth/requireSession";
|
||||
import { publishWorkOrdersUpdate } from "@/lib/mqtt";
|
||||
import { z } from "zod";
|
||||
|
||||
function canManage(role?: string | null) {
|
||||
return role === "OWNER" || role === "ADMIN";
|
||||
}
|
||||
|
||||
const MAX_WORK_ORDERS = 2000;
|
||||
const MAX_WORK_ORDER_ID_LENGTH = 64;
|
||||
const MAX_SKU_LENGTH = 64;
|
||||
const MAX_TARGET_QTY = 2_000_000_000;
|
||||
const MAX_CYCLE_TIME = 86_400;
|
||||
const WORK_ORDER_ID_RE = /^[A-Za-z0-9._-]+$/;
|
||||
|
||||
const uploadBodySchema = z.object({
|
||||
machineId: z.string().trim().min(1),
|
||||
workOrders: z.array(z.any()).optional(),
|
||||
orders: z.array(z.any()).optional(),
|
||||
workOrder: z.any().optional(),
|
||||
});
|
||||
|
||||
function cleanText(value: unknown, maxLen: number) {
|
||||
if (value === null || value === undefined) return null;
|
||||
const text = String(value).trim();
|
||||
if (!text) return null;
|
||||
const sanitized = text.replace(/[\u0000-\u001f\u007f]/g, "");
|
||||
if (!sanitized) return null;
|
||||
return sanitized.length > maxLen ? sanitized.slice(0, maxLen) : sanitized;
|
||||
}
|
||||
|
||||
function toIntOrNull(value: unknown) {
|
||||
if (value === null || value === undefined || value === "") return null;
|
||||
const n = Number(value);
|
||||
if (!Number.isFinite(n)) return null;
|
||||
return Math.trunc(n);
|
||||
}
|
||||
|
||||
function toFloatOrNull(value: unknown) {
|
||||
if (value === null || value === undefined || value === "") return null;
|
||||
const n = Number(value);
|
||||
if (!Number.isFinite(n)) return null;
|
||||
return n;
|
||||
}
|
||||
|
||||
type WorkOrderInput = {
|
||||
workOrderId: string;
|
||||
sku?: string | null;
|
||||
targetQty?: number | null;
|
||||
cycleTime?: number | null;
|
||||
};
|
||||
|
||||
function normalizeWorkOrders(raw: unknown[]) {
|
||||
const seen = new Set<string>();
|
||||
const cleaned: WorkOrderInput[] = [];
|
||||
|
||||
for (const item of raw) {
|
||||
const record = item && typeof item === "object" ? (item as Record<string, unknown>) : {};
|
||||
const idRaw = cleanText(
|
||||
record.workOrderId ?? record.id ?? record.work_order_id,
|
||||
MAX_WORK_ORDER_ID_LENGTH
|
||||
);
|
||||
if (!idRaw || !WORK_ORDER_ID_RE.test(idRaw) || seen.has(idRaw)) continue;
|
||||
seen.add(idRaw);
|
||||
|
||||
const sku = cleanText(record.sku ?? record.SKU ?? null, MAX_SKU_LENGTH);
|
||||
const targetQtyRaw = toIntOrNull(
|
||||
record.targetQty ?? record.target_qty ?? record.target ?? record.targetQuantity
|
||||
);
|
||||
const cycleTimeRaw = toFloatOrNull(
|
||||
record.cycleTime ?? record.theoreticalCycleTime ?? record.theoretical_cycle_time ?? record.cycle_time
|
||||
);
|
||||
const targetQty =
|
||||
targetQtyRaw == null ? null : Math.min(Math.max(targetQtyRaw, 0), MAX_TARGET_QTY);
|
||||
const cycleTime =
|
||||
cycleTimeRaw == null ? null : Math.min(Math.max(cycleTimeRaw, 0), MAX_CYCLE_TIME);
|
||||
|
||||
cleaned.push({
|
||||
workOrderId: idRaw,
|
||||
sku: sku ?? null,
|
||||
targetQty: targetQty ?? null,
|
||||
cycleTime: cycleTime ?? null,
|
||||
});
|
||||
}
|
||||
|
||||
return cleaned;
|
||||
}
|
||||
|
||||
export async function POST(req: NextRequest) {
|
||||
const session = await requireSession();
|
||||
if (!session) return NextResponse.json({ ok: false, error: "Unauthorized" }, { status: 401 });
|
||||
|
||||
const membership = await prisma.orgUser.findUnique({
|
||||
where: { orgId_userId: { orgId: session.orgId, userId: session.userId } },
|
||||
select: { role: true },
|
||||
});
|
||||
if (!canManage(membership?.role)) {
|
||||
return NextResponse.json({ ok: false, error: "Forbidden" }, { status: 403 });
|
||||
}
|
||||
|
||||
const body = await req.json().catch(() => ({}));
|
||||
const parsedBody = uploadBodySchema.safeParse(body);
|
||||
if (!parsedBody.success) {
|
||||
return NextResponse.json({ ok: false, error: "Invalid payload" }, { status: 400 });
|
||||
}
|
||||
|
||||
const machineId = String(parsedBody.data.machineId ?? "").trim();
|
||||
if (!machineId) {
|
||||
return NextResponse.json({ ok: false, error: "machineId is required" }, { status: 400 });
|
||||
}
|
||||
|
||||
const machine = await prisma.machine.findFirst({
|
||||
where: { id: machineId, orgId: session.orgId },
|
||||
select: { id: true },
|
||||
});
|
||||
if (!machine) return NextResponse.json({ ok: false, error: "Not found" }, { status: 404 });
|
||||
|
||||
const listRaw = Array.isArray(parsedBody.data.workOrders)
|
||||
? parsedBody.data.workOrders
|
||||
: Array.isArray(parsedBody.data.orders)
|
||||
? parsedBody.data.orders
|
||||
: parsedBody.data.workOrder
|
||||
? [parsedBody.data.workOrder]
|
||||
: [];
|
||||
|
||||
if (listRaw.length > MAX_WORK_ORDERS) {
|
||||
return NextResponse.json(
|
||||
{ ok: false, error: `Too many work orders (max ${MAX_WORK_ORDERS})` },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
const cleaned = normalizeWorkOrders(listRaw);
|
||||
if (!cleaned.length) {
|
||||
return NextResponse.json({ ok: false, error: "No valid work orders provided" }, { status: 400 });
|
||||
}
|
||||
|
||||
const created = await prisma.machineWorkOrder.createMany({
|
||||
data: cleaned.map((row) => ({
|
||||
orgId: session.orgId,
|
||||
machineId,
|
||||
workOrderId: row.workOrderId,
|
||||
sku: row.sku ?? null,
|
||||
targetQty: row.targetQty ?? null,
|
||||
cycleTime: row.cycleTime ?? null,
|
||||
status: "PENDING",
|
||||
})),
|
||||
skipDuplicates: true,
|
||||
});
|
||||
|
||||
try {
|
||||
await publishWorkOrdersUpdate({
|
||||
orgId: session.orgId,
|
||||
machineId,
|
||||
count: created.count,
|
||||
});
|
||||
} catch (err) {
|
||||
console.warn("[work orders POST] MQTT publish failed", err);
|
||||
}
|
||||
|
||||
return NextResponse.json({
|
||||
ok: true,
|
||||
machineId,
|
||||
inserted: created.count,
|
||||
total: cleaned.length,
|
||||
});
|
||||
}
|
||||
161
app/globals.css
161
app/globals.css
@@ -1,31 +1,168 @@
|
||||
@import "tailwindcss";
|
||||
|
||||
:root {
|
||||
--background: #ffffff;
|
||||
--foreground: #171717;
|
||||
color-scheme: dark;
|
||||
--font-geist-sans: "Segoe UI", system-ui, sans-serif;
|
||||
--font-geist-mono: ui-monospace, SFMono-Regular, Menlo, monospace;
|
||||
--app-bg: #0b0f14;
|
||||
--app-surface: rgba(255, 255, 255, 0.05);
|
||||
--app-surface-2: rgba(255, 255, 255, 0.08);
|
||||
--app-surface-3: rgba(0, 0, 0, 0.28);
|
||||
--app-surface-4: rgba(0, 0, 0, 0.42);
|
||||
--app-border: rgba(148, 163, 184, 0.18);
|
||||
--app-text: #e5e7eb;
|
||||
--app-text-strong: #f8fafc;
|
||||
--app-text-muted: #94a3b8;
|
||||
--app-text-subtle: #6b7280;
|
||||
--app-text-faint: #475569;
|
||||
--app-text-on-accent: #0b0f14;
|
||||
|
||||
--app-good-text: #6ee7b7;
|
||||
--app-good-bg: rgba(34, 197, 94, 0.18);
|
||||
--app-good-border: rgba(34, 197, 94, 0.28);
|
||||
--app-good-solid: #34d399;
|
||||
|
||||
--app-warn-text: #facc15;
|
||||
--app-warn-bg: rgba(250, 204, 21, 0.18);
|
||||
--app-warn-border: rgba(250, 204, 21, 0.32);
|
||||
|
||||
--app-bad-text: #f87171;
|
||||
--app-bad-bg: rgba(248, 113, 113, 0.18);
|
||||
--app-bad-border: rgba(248, 113, 113, 0.32);
|
||||
|
||||
--app-info-text: #7ab8ff;
|
||||
--app-info-bg: rgba(59, 130, 246, 0.18);
|
||||
--app-info-border: rgba(59, 130, 246, 0.3);
|
||||
|
||||
--app-overlay: rgba(3, 6, 12, 0.65);
|
||||
--app-modal-bg: rgba(9, 13, 19, 0.92);
|
||||
|
||||
--app-chart-grid: rgba(148, 163, 184, 0.2);
|
||||
--app-chart-tick: #9ca3af;
|
||||
--app-chart-tooltip-bg: rgba(2, 6, 23, 0.88);
|
||||
--app-chart-tooltip-border: rgba(148, 163, 184, 0.25);
|
||||
--app-chart-label: #f8fafc;
|
||||
--app-chart-shadow: 0 0 30px rgba(2, 6, 23, 0.6);
|
||||
}
|
||||
|
||||
@theme inline {
|
||||
--color-background: var(--background);
|
||||
--color-foreground: var(--foreground);
|
||||
--color-background: var(--app-bg);
|
||||
--color-foreground: var(--app-text);
|
||||
--font-sans: var(--font-geist-sans);
|
||||
--font-mono: var(--font-geist-mono);
|
||||
}
|
||||
|
||||
@media (prefers-color-scheme: dark) {
|
||||
:root {
|
||||
--background: #0a0a0a;
|
||||
--foreground: #ededed;
|
||||
}
|
||||
:root[data-theme="light"] {
|
||||
color-scheme: light;
|
||||
--app-bg: #f4f6f9;
|
||||
--app-surface: #ffffff;
|
||||
--app-surface-2: #eef2f6;
|
||||
--app-surface-3: #e7ecf2;
|
||||
--app-surface-4: #dde3ea;
|
||||
--app-border: rgba(15, 23, 42, 0.12);
|
||||
--app-text: #1f2937;
|
||||
--app-text-strong: #0f172a;
|
||||
--app-text-muted: #4b5563;
|
||||
--app-text-subtle: #6b7280;
|
||||
--app-text-faint: #8b95a3;
|
||||
--app-text-on-accent: #0f172a;
|
||||
|
||||
--app-good-text: #0f7a3e;
|
||||
--app-good-bg: rgba(34, 197, 94, 0.16);
|
||||
--app-good-border: rgba(34, 197, 94, 0.3);
|
||||
--app-good-solid: #22c55e;
|
||||
|
||||
--app-warn-text: #a16207;
|
||||
--app-warn-bg: rgba(234, 179, 8, 0.18);
|
||||
--app-warn-border: rgba(234, 179, 8, 0.36);
|
||||
|
||||
--app-bad-text: #b91c1c;
|
||||
--app-bad-bg: rgba(239, 68, 68, 0.16);
|
||||
--app-bad-border: rgba(239, 68, 68, 0.34);
|
||||
|
||||
--app-info-text: #1d4ed8;
|
||||
--app-info-bg: rgba(59, 130, 246, 0.16);
|
||||
--app-info-border: rgba(59, 130, 246, 0.3);
|
||||
|
||||
--app-overlay: rgba(15, 23, 42, 0.45);
|
||||
--app-modal-bg: rgba(255, 255, 255, 0.92);
|
||||
|
||||
--app-chart-grid: rgba(15, 23, 42, 0.12);
|
||||
--app-chart-tick: #6b7280;
|
||||
--app-chart-tooltip-bg: #ffffff;
|
||||
--app-chart-tooltip-border: rgba(15, 23, 42, 0.16);
|
||||
--app-chart-label: #0f172a;
|
||||
--app-chart-shadow: 0 0 24px rgba(15, 23, 42, 0.12);
|
||||
}
|
||||
|
||||
body {
|
||||
background: var(--background);
|
||||
color: var(--foreground);
|
||||
font-family: Arial, Helvetica, sans-serif;
|
||||
background: var(--app-bg);
|
||||
color: var(--app-text);
|
||||
font-family: var(--font-geist-sans), "Segoe UI", system-ui, sans-serif;
|
||||
}
|
||||
|
||||
/* Hide scrollbar but keep scrolling */
|
||||
.no-scrollbar::-webkit-scrollbar { display: none; }
|
||||
.no-scrollbar { -ms-overflow-style: none; scrollbar-width: none; }
|
||||
|
||||
/* Theme-friendly overrides for common utility classes */
|
||||
.text-white { color: var(--app-text-strong) !important; }
|
||||
.text-black { color: var(--app-text-on-accent) !important; }
|
||||
.text-zinc-200 { color: var(--app-text) !important; }
|
||||
.text-zinc-300 { color: var(--app-text-muted) !important; }
|
||||
.text-zinc-400 { color: var(--app-text-subtle) !important; }
|
||||
.text-zinc-500 { color: var(--app-text-faint) !important; }
|
||||
.text-emerald-100,
|
||||
.text-emerald-200,
|
||||
.text-emerald-300 { color: var(--app-good-text) !important; }
|
||||
.text-yellow-300 { color: var(--app-warn-text) !important; }
|
||||
.text-red-200,
|
||||
.text-red-300,
|
||||
.text-red-400 { color: var(--app-bad-text) !important; }
|
||||
.text-blue-300 { color: var(--app-info-text) !important; }
|
||||
.text-orange-300 { color: var(--app-warn-text) !important; }
|
||||
.text-rose-300 { color: var(--app-bad-text) !important; }
|
||||
|
||||
.bg-black { background-color: var(--app-bg) !important; }
|
||||
.bg-black\/20 { background-color: var(--app-surface-2) !important; }
|
||||
.bg-black\/25 { background-color: var(--app-surface-3) !important; }
|
||||
.bg-black\/30 { background-color: var(--app-surface-3) !important; }
|
||||
.bg-black\/40 { background-color: var(--app-surface-4) !important; }
|
||||
.bg-black\/70 { background-color: var(--app-overlay) !important; }
|
||||
.bg-zinc-950\/80,
|
||||
.bg-zinc-950\/95 { background-color: var(--app-modal-bg) !important; }
|
||||
.bg-white\/5 { background-color: var(--app-surface) !important; }
|
||||
.bg-white\/10 { background-color: var(--app-surface-2) !important; }
|
||||
|
||||
.border-white\/10,
|
||||
.border-white\/5 { border-color: var(--app-border) !important; }
|
||||
.border-emerald-500\/20,
|
||||
.border-emerald-400\/40,
|
||||
.border-emerald-500\/30 { border-color: var(--app-good-border) !important; }
|
||||
.border-red-500\/20,
|
||||
.border-red-500\/30 { border-color: var(--app-bad-border) !important; }
|
||||
.border-yellow-500\/20,
|
||||
.border-orange-500\/20 { border-color: var(--app-warn-border) !important; }
|
||||
.border-rose-500\/20 { border-color: var(--app-bad-border) !important; }
|
||||
.border-blue-500\/20 { border-color: var(--app-info-border) !important; }
|
||||
|
||||
.bg-emerald-500\/10,
|
||||
.bg-emerald-500\/15,
|
||||
.bg-emerald-500\/20,
|
||||
.bg-emerald-500\/30 { background-color: var(--app-good-bg) !important; }
|
||||
.bg-emerald-400 { background-color: var(--app-good-solid) !important; }
|
||||
.bg-yellow-500\/15 { background-color: var(--app-warn-bg) !important; }
|
||||
.bg-red-500\/10,
|
||||
.bg-red-500\/15,
|
||||
.bg-red-500\/20 { background-color: var(--app-bad-bg) !important; }
|
||||
.bg-blue-500\/15 { background-color: var(--app-info-bg) !important; }
|
||||
.bg-orange-500\/15 { background-color: var(--app-warn-bg) !important; }
|
||||
.bg-rose-500\/15 { background-color: var(--app-bad-bg) !important; }
|
||||
|
||||
.placeholder\:text-zinc-500::placeholder { color: var(--app-text-faint) !important; }
|
||||
|
||||
.hover\:bg-white\/5:hover { background-color: var(--app-surface) !important; }
|
||||
.hover\:bg-white\/10:hover { background-color: var(--app-surface-2) !important; }
|
||||
.hover\:bg-emerald-500\/30:hover { background-color: var(--app-good-bg) !important; }
|
||||
.hover\:bg-red-500\/20:hover { background-color: var(--app-bad-bg) !important; }
|
||||
.hover\:text-white:hover { color: var(--app-text-strong) !important; }
|
||||
|
||||
155
app/invite/[token]/InviteAcceptForm.tsx
Normal file
155
app/invite/[token]/InviteAcceptForm.tsx
Normal file
@@ -0,0 +1,155 @@
|
||||
"use client";
|
||||
|
||||
import { useEffect, useState } from "react";
|
||||
import { useRouter } from "next/navigation";
|
||||
import { useI18n } from "@/lib/i18n/useI18n";
|
||||
|
||||
type InviteInfo = {
|
||||
email: string;
|
||||
role: string;
|
||||
org: { id: string; name: string; slug: string };
|
||||
expiresAt: string;
|
||||
};
|
||||
|
||||
type InviteAcceptFormProps = {
|
||||
token: string;
|
||||
initialInvite?: InviteInfo | null;
|
||||
initialError?: string | null;
|
||||
};
|
||||
|
||||
export default function InviteAcceptForm({
|
||||
token,
|
||||
initialInvite = null,
|
||||
initialError = null,
|
||||
}: InviteAcceptFormProps) {
|
||||
const router = useRouter();
|
||||
const { t } = useI18n();
|
||||
const cleanedToken = token.trim();
|
||||
const [invite, setInvite] = useState<InviteInfo | null>(initialInvite);
|
||||
const [loading, setLoading] = useState(!initialInvite && !initialError);
|
||||
const [submitting, setSubmitting] = useState(false);
|
||||
const [error, setError] = useState<string | null>(initialError);
|
||||
const [name, setName] = useState("");
|
||||
const [password, setPassword] = useState("");
|
||||
|
||||
useEffect(() => {
|
||||
if (initialInvite || initialError) {
|
||||
setLoading(false);
|
||||
return;
|
||||
}
|
||||
|
||||
let alive = true;
|
||||
async function loadInvite() {
|
||||
setLoading(true);
|
||||
setError(null);
|
||||
try {
|
||||
const res = await fetch(`/api/invites/${encodeURIComponent(cleanedToken)}`, {
|
||||
cache: "no-store",
|
||||
});
|
||||
const data = await res.json().catch(() => ({}));
|
||||
if (!res.ok || !data.ok) {
|
||||
throw new Error(data.error || t("invite.error.notFound"));
|
||||
}
|
||||
if (alive) setInvite(data.invite);
|
||||
} catch (err: unknown) {
|
||||
const message = err instanceof Error ? err.message : null;
|
||||
if (alive) setError(message || t("invite.error.notFound"));
|
||||
} finally {
|
||||
if (alive) setLoading(false);
|
||||
}
|
||||
}
|
||||
|
||||
loadInvite();
|
||||
return () => {
|
||||
alive = false;
|
||||
};
|
||||
}, [cleanedToken, initialInvite, initialError, t]);
|
||||
|
||||
async function onSubmit(e: React.FormEvent) {
|
||||
e.preventDefault();
|
||||
setError(null);
|
||||
setSubmitting(true);
|
||||
try {
|
||||
const res = await fetch(`/api/invites/${encodeURIComponent(cleanedToken)}`, {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ name, password }),
|
||||
});
|
||||
const data = await res.json().catch(() => ({}));
|
||||
if (!res.ok || !data.ok) {
|
||||
throw new Error(data.error || t("invite.error.acceptFailed"));
|
||||
}
|
||||
router.push("/machines");
|
||||
router.refresh();
|
||||
} catch (err: unknown) {
|
||||
const message = err instanceof Error ? err.message : null;
|
||||
setError(message || t("invite.error.acceptFailed"));
|
||||
} finally {
|
||||
setSubmitting(false);
|
||||
}
|
||||
}
|
||||
|
||||
if (loading) {
|
||||
return (
|
||||
<div className="min-h-screen bg-black flex items-center justify-center p-6 text-zinc-300">
|
||||
{t("invite.loading")}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
if (!invite) {
|
||||
return (
|
||||
<div className="min-h-screen bg-black flex items-center justify-center p-6">
|
||||
<div className="max-w-md rounded-2xl border border-red-500/30 bg-red-500/10 p-6 text-sm text-red-200">
|
||||
{error || t("invite.notFound")}
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="min-h-screen bg-black flex items-center justify-center p-6">
|
||||
<form onSubmit={onSubmit} className="w-full max-w-lg rounded-2xl border border-white/10 bg-white/5 p-8">
|
||||
<h1 className="text-2xl font-semibold text-white">
|
||||
{t("invite.joinTitle", { org: invite.org.name })}
|
||||
</h1>
|
||||
<p className="mt-1 text-sm text-zinc-400">
|
||||
{t("invite.acceptCopy", { email: invite.email, role: invite.role })}
|
||||
</p>
|
||||
|
||||
<div className="mt-6 space-y-4">
|
||||
<div>
|
||||
<label className="text-sm text-zinc-300">{t("invite.yourName")}</label>
|
||||
<input
|
||||
className="mt-1 w-full rounded-xl border border-white/10 bg-black/40 px-4 py-3 text-white outline-none"
|
||||
value={name}
|
||||
onChange={(e) => setName(e.target.value)}
|
||||
autoComplete="name"
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<label className="text-sm text-zinc-300">{t("invite.password")}</label>
|
||||
<input
|
||||
type="password"
|
||||
className="mt-1 w-full rounded-xl border border-white/10 bg-black/40 px-4 py-3 text-white outline-none"
|
||||
value={password}
|
||||
onChange={(e) => setPassword(e.target.value)}
|
||||
autoComplete="new-password"
|
||||
/>
|
||||
</div>
|
||||
|
||||
{error && <div className="text-sm text-red-400">{error}</div>}
|
||||
|
||||
<button
|
||||
type="submit"
|
||||
disabled={submitting}
|
||||
className="mt-2 w-full rounded-xl bg-emerald-400 py-3 font-semibold text-black disabled:opacity-70"
|
||||
>
|
||||
{submitting ? t("invite.submit.loading") : t("invite.submit.default")}
|
||||
</button>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
52
app/invite/[token]/page.tsx
Normal file
52
app/invite/[token]/page.tsx
Normal file
@@ -0,0 +1,52 @@
|
||||
import { cookies } from "next/headers";
|
||||
import { redirect } from "next/navigation";
|
||||
import { prisma } from "@/lib/prisma";
|
||||
import InviteAcceptForm from "./InviteAcceptForm";
|
||||
|
||||
export default async function InvitePage({ params }: { params: { token: string } | Promise<{ token: string }> }) {
|
||||
const session = (await cookies()).get("mis_session")?.value;
|
||||
if (session) {
|
||||
redirect("/machines");
|
||||
}
|
||||
|
||||
const resolvedParams = await Promise.resolve(params);
|
||||
const token = String(resolvedParams?.token || "").trim().toLowerCase();
|
||||
let invite = null;
|
||||
let error: string | null = null;
|
||||
|
||||
if (!token) {
|
||||
error = "Invite not found";
|
||||
} else {
|
||||
invite = await prisma.orgInvite.findFirst({
|
||||
where: {
|
||||
token,
|
||||
revokedAt: null,
|
||||
acceptedAt: null,
|
||||
expiresAt: { gt: new Date() },
|
||||
},
|
||||
include: {
|
||||
org: { select: { id: true, name: true, slug: true } },
|
||||
},
|
||||
});
|
||||
if (!invite) {
|
||||
error = "Invite not found";
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<InviteAcceptForm
|
||||
token={token}
|
||||
initialInvite={
|
||||
invite
|
||||
? {
|
||||
email: invite.email,
|
||||
role: invite.role,
|
||||
org: invite.org,
|
||||
expiresAt: invite.expiresAt.toISOString(),
|
||||
}
|
||||
: null
|
||||
}
|
||||
initialError={error}
|
||||
/>
|
||||
);
|
||||
}
|
||||
@@ -1,19 +1,22 @@
|
||||
import type { Metadata } from "next";
|
||||
import { Geist, Geist_Mono } from "next/font/google";
|
||||
import { cookies } from "next/headers";
|
||||
import "./globals.css";
|
||||
|
||||
const geistSans = Geist({ variable: "--font-geist-sans", subsets: ["latin"] });
|
||||
const geistMono = Geist_Mono({ variable: "--font-geist-mono", subsets: ["latin"] });
|
||||
|
||||
export const metadata: Metadata = {
|
||||
title: "MIS Control Tower",
|
||||
description: "MaliounTech Industrial Suite",
|
||||
};
|
||||
|
||||
export default function RootLayout({ children }: { children: React.ReactNode }) {
|
||||
export default async function RootLayout({ children }: { children: React.ReactNode }) {
|
||||
const cookieJar = await cookies();
|
||||
const themeCookie = cookieJar.get("mis_theme")?.value;
|
||||
const localeCookie = cookieJar.get("mis_locale")?.value;
|
||||
const theme = themeCookie === "light" ? "light" : "dark";
|
||||
const locale = localeCookie === "es-MX" ? "es-MX" : "en";
|
||||
|
||||
return (
|
||||
<html lang="en">
|
||||
<body className={`${geistSans.variable} ${geistMono.variable} antialiased`}>
|
||||
<html lang={locale} data-theme={theme}>
|
||||
<body className="antialiased">
|
||||
{children}
|
||||
</body>
|
||||
</html>
|
||||
|
||||
@@ -2,11 +2,13 @@
|
||||
|
||||
import { useSearchParams, useRouter } from "next/navigation";
|
||||
import { useState } from "react";
|
||||
import { useI18n } from "@/lib/i18n/useI18n";
|
||||
|
||||
export default function LoginForm() {
|
||||
const router = useRouter();
|
||||
const searchParams = useSearchParams();
|
||||
const next = searchParams.get("next") || "/machines";
|
||||
const { t } = useI18n();
|
||||
|
||||
const [email, setEmail] = useState("");
|
||||
const [password, setPassword] = useState("");
|
||||
@@ -27,14 +29,15 @@ export default function LoginForm() {
|
||||
|
||||
const data = await res.json().catch(() => ({}));
|
||||
if (!res.ok || !data.ok) {
|
||||
setErr(data.error || "Login failed");
|
||||
setErr(data.error || t("login.error.default"));
|
||||
return;
|
||||
}
|
||||
|
||||
router.push(next);
|
||||
router.refresh();
|
||||
} catch (e: any) {
|
||||
setErr(e?.message || "Network error");
|
||||
} catch (e: unknown) {
|
||||
const message = e instanceof Error ? e.message : null;
|
||||
setErr(message || t("login.error.network"));
|
||||
} finally {
|
||||
setLoading(false);
|
||||
}
|
||||
@@ -43,12 +46,12 @@ export default function LoginForm() {
|
||||
return (
|
||||
<div className="min-h-screen bg-black flex items-center justify-center p-6">
|
||||
<form onSubmit={onSubmit} className="w-full max-w-md rounded-2xl border border-white/10 bg-white/5 p-8">
|
||||
<h1 className="text-2xl font-semibold text-white">Control Tower</h1>
|
||||
<p className="mt-1 text-sm text-zinc-400">Sign in to your organization</p>
|
||||
<h1 className="text-2xl font-semibold text-white">{t("login.title")}</h1>
|
||||
<p className="mt-1 text-sm text-zinc-400">{t("login.subtitle")}</p>
|
||||
|
||||
<div className="mt-6 space-y-4">
|
||||
<div>
|
||||
<label className="text-sm text-zinc-300">Email</label>
|
||||
<label className="text-sm text-zinc-300">{t("login.email")}</label>
|
||||
<input
|
||||
className="mt-1 w-full rounded-xl border border-white/10 bg-black/40 px-4 py-3 text-white outline-none"
|
||||
value={email}
|
||||
@@ -58,7 +61,7 @@ export default function LoginForm() {
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<label className="text-sm text-zinc-300">Password</label>
|
||||
<label className="text-sm text-zinc-300">{t("login.password")}</label>
|
||||
<input
|
||||
type="password"
|
||||
className="mt-1 w-full rounded-xl border border-white/10 bg-black/40 px-4 py-3 text-white outline-none"
|
||||
@@ -75,10 +78,15 @@ export default function LoginForm() {
|
||||
disabled={loading}
|
||||
className="mt-2 w-full rounded-xl bg-emerald-400 py-3 font-semibold text-black disabled:opacity-70"
|
||||
>
|
||||
{loading ? "Signing in..." : "Login"}
|
||||
{loading ? t("login.submit.loading") : t("login.submit.default")}
|
||||
</button>
|
||||
|
||||
<div className="text-xs text-zinc-500">(Dev mode) This will be replaced with JWT auth later.</div>
|
||||
<div className="text-xs text-zinc-500">
|
||||
{t("login.newHere")}{" "}
|
||||
<a href="/signup" className="text-emerald-300 hover:text-emerald-200">
|
||||
{t("login.createAccount")}
|
||||
</a>
|
||||
</div>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
|
||||
141
app/signup/SignupForm.tsx
Normal file
141
app/signup/SignupForm.tsx
Normal file
@@ -0,0 +1,141 @@
|
||||
"use client";
|
||||
|
||||
import { useState } from "react";
|
||||
import { useI18n } from "@/lib/i18n/useI18n";
|
||||
|
||||
export default function SignupForm() {
|
||||
const { t } = useI18n();
|
||||
const [orgName, setOrgName] = useState("");
|
||||
const [name, setName] = useState("");
|
||||
const [email, setEmail] = useState("");
|
||||
const [password, setPassword] = useState("");
|
||||
const [loading, setLoading] = useState(false);
|
||||
const [err, setErr] = useState<string | null>(null);
|
||||
const [verificationSent, setVerificationSent] = useState(false);
|
||||
const [emailSent, setEmailSent] = useState(true);
|
||||
|
||||
async function onSubmit(e: React.FormEvent) {
|
||||
e.preventDefault();
|
||||
setErr(null);
|
||||
setLoading(true);
|
||||
|
||||
try {
|
||||
const res = await fetch("/api/signup", {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ orgName, name, email, password }),
|
||||
});
|
||||
|
||||
const data = await res.json().catch(() => ({}));
|
||||
if (!res.ok || !data.ok) {
|
||||
setErr(data.error || t("signup.error.default"));
|
||||
return;
|
||||
}
|
||||
|
||||
setVerificationSent(true);
|
||||
setEmailSent(data.emailSent !== false);
|
||||
} catch (e: unknown) {
|
||||
const message = e instanceof Error ? e.message : null;
|
||||
setErr(message || t("signup.error.network"));
|
||||
} finally {
|
||||
setLoading(false);
|
||||
}
|
||||
}
|
||||
|
||||
if (verificationSent) {
|
||||
return (
|
||||
<div className="min-h-screen bg-black flex items-center justify-center p-6">
|
||||
<div className="w-full max-w-lg rounded-2xl border border-white/10 bg-white/5 p-8">
|
||||
<h1 className="text-2xl font-semibold text-white">{t("signup.verify.title")}</h1>
|
||||
<p className="mt-2 text-sm text-zinc-300">
|
||||
{t("signup.verify.sent", { email: email || t("common.na") })}
|
||||
</p>
|
||||
{!emailSent && (
|
||||
<div className="mt-3 rounded-xl border border-red-500/30 bg-red-500/10 p-3 text-xs text-red-200">
|
||||
{t("signup.verify.failed")}
|
||||
</div>
|
||||
)}
|
||||
<div className="mt-4 text-xs text-zinc-500">{t("signup.verify.notice")}</div>
|
||||
<div className="mt-6">
|
||||
<a
|
||||
href="/login"
|
||||
className="inline-flex rounded-xl border border-white/10 bg-white/5 px-4 py-2 text-sm text-white hover:bg-white/10"
|
||||
>
|
||||
{t("signup.verify.back")}
|
||||
</a>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="min-h-screen bg-black flex items-center justify-center p-6">
|
||||
<form onSubmit={onSubmit} className="w-full max-w-lg rounded-2xl border border-white/10 bg-white/5 p-8">
|
||||
<h1 className="text-2xl font-semibold text-white">{t("signup.title")}</h1>
|
||||
<p className="mt-1 text-sm text-zinc-400">{t("signup.subtitle")}</p>
|
||||
|
||||
<div className="mt-6 space-y-4">
|
||||
<div>
|
||||
<label className="text-sm text-zinc-300">{t("signup.orgName")}</label>
|
||||
<input
|
||||
className="mt-1 w-full rounded-xl border border-white/10 bg-black/40 px-4 py-3 text-white outline-none"
|
||||
value={orgName}
|
||||
onChange={(e) => setOrgName(e.target.value)}
|
||||
autoComplete="organization"
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div className="grid grid-cols-1 gap-4 md:grid-cols-2">
|
||||
<div>
|
||||
<label className="text-sm text-zinc-300">{t("signup.yourName")}</label>
|
||||
<input
|
||||
className="mt-1 w-full rounded-xl border border-white/10 bg-black/40 px-4 py-3 text-white outline-none"
|
||||
value={name}
|
||||
onChange={(e) => setName(e.target.value)}
|
||||
autoComplete="name"
|
||||
/>
|
||||
</div>
|
||||
<div>
|
||||
<label className="text-sm text-zinc-300">{t("signup.email")}</label>
|
||||
<input
|
||||
className="mt-1 w-full rounded-xl border border-white/10 bg-black/40 px-4 py-3 text-white outline-none"
|
||||
value={email}
|
||||
onChange={(e) => setEmail(e.target.value)}
|
||||
autoComplete="email"
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<label className="text-sm text-zinc-300">{t("signup.password")}</label>
|
||||
<input
|
||||
type="password"
|
||||
className="mt-1 w-full rounded-xl border border-white/10 bg-black/40 px-4 py-3 text-white outline-none"
|
||||
value={password}
|
||||
onChange={(e) => setPassword(e.target.value)}
|
||||
autoComplete="new-password"
|
||||
/>
|
||||
</div>
|
||||
|
||||
{err && <div className="text-sm text-red-400">{err}</div>}
|
||||
|
||||
<button
|
||||
type="submit"
|
||||
disabled={loading}
|
||||
className="mt-2 w-full rounded-xl bg-emerald-400 py-3 font-semibold text-black disabled:opacity-70"
|
||||
>
|
||||
{loading ? t("signup.submit.loading") : t("signup.submit.default")}
|
||||
</button>
|
||||
|
||||
<div className="text-xs text-zinc-500">
|
||||
{t("signup.alreadyHave")}{" "}
|
||||
<a href="/login" className="text-emerald-300 hover:text-emerald-200">
|
||||
{t("signup.signIn")}
|
||||
</a>
|
||||
</div>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
12
app/signup/page.tsx
Normal file
12
app/signup/page.tsx
Normal file
@@ -0,0 +1,12 @@
|
||||
import { cookies } from "next/headers";
|
||||
import { redirect } from "next/navigation";
|
||||
import SignupForm from "./SignupForm";
|
||||
|
||||
export default async function SignupPage() {
|
||||
const session = (await cookies()).get("mis_session")?.value;
|
||||
if (session) {
|
||||
redirect("/machines");
|
||||
}
|
||||
|
||||
return <SignupForm />;
|
||||
}
|
||||
329
components/analytics/DowntimeParetoCard.tsx
Normal file
329
components/analytics/DowntimeParetoCard.tsx
Normal file
@@ -0,0 +1,329 @@
|
||||
"use client";
|
||||
|
||||
import { useEffect, useMemo, useState } from "react";
|
||||
import { DOWNTIME_RANGES, type DowntimeRange } from "@/lib/analytics/downtimeRange";
|
||||
import Link from "next/link";
|
||||
import {
|
||||
Bar,
|
||||
CartesianGrid,
|
||||
ComposedChart,
|
||||
Line,
|
||||
ReferenceLine,
|
||||
ResponsiveContainer,
|
||||
Tooltip,
|
||||
XAxis,
|
||||
YAxis,
|
||||
} from "recharts";
|
||||
import { useI18n } from "@/lib/i18n/useI18n";
|
||||
|
||||
|
||||
type ParetoRow = {
|
||||
reasonCode: string;
|
||||
reasonLabel: string;
|
||||
minutesLost?: number; // downtime
|
||||
scrapQty?: number; // scrap (future)
|
||||
pctOfTotal: number; // 0..100
|
||||
cumulativePct: number; // 0..100
|
||||
};
|
||||
|
||||
type ParetoResponse = {
|
||||
ok?: boolean;
|
||||
rows?: ParetoRow[];
|
||||
top3?: ParetoRow[];
|
||||
totalMinutesLost?: number;
|
||||
threshold80?: { reasonCode: string; reasonLabel: string; index: number } | null;
|
||||
error?: string;
|
||||
};
|
||||
|
||||
type CoverageResponse = {
|
||||
ok?: boolean;
|
||||
totalDowntimeMinutes?: number;
|
||||
receivedMinutes?: number;
|
||||
receivedCoveragePct?: number; // could be 0..1 or 0..100 depending on your impl
|
||||
pendingEpisodesCount?: number;
|
||||
};
|
||||
|
||||
function clampLabel(s: string, max = 18) {
|
||||
if (!s) return "";
|
||||
return s.length > max ? `${s.slice(0, max - 1)}…` : s;
|
||||
}
|
||||
|
||||
function normalizePct(v?: number | null) {
|
||||
if (v == null || Number.isNaN(v)) return null;
|
||||
// If API returns 0..1, convert to 0..100
|
||||
return v <= 1 ? v * 100 : v;
|
||||
}
|
||||
|
||||
export default function DowntimeParetoCard({
|
||||
machineId,
|
||||
range = "7d",
|
||||
showCoverage = true,
|
||||
showOpenFullReport = true,
|
||||
variant = "summary",
|
||||
maxBars,
|
||||
}: {
|
||||
machineId?: string;
|
||||
range?: DowntimeRange;
|
||||
showCoverage?: boolean;
|
||||
showOpenFullReport?: boolean;
|
||||
variant?: "summary" | "full";
|
||||
maxBars?: number; // optional override
|
||||
}) {
|
||||
const { t } = useI18n();
|
||||
const isSummary = variant === "summary";
|
||||
const barsLimit = maxBars ?? (isSummary ? 5 : 12);
|
||||
const chartHeightClass = isSummary ? "h-[240px]" : "h-[360px]";
|
||||
const containerPad = isSummary ? "p-4" : "p-5";
|
||||
const [loading, setLoading] = useState(true);
|
||||
const [err, setErr] = useState<string | null>(null);
|
||||
const [pareto, setPareto] = useState<ParetoResponse | null>(null);
|
||||
const [coverage, setCoverage] = useState<CoverageResponse | null>(null);
|
||||
|
||||
useEffect(() => {
|
||||
const controller = new AbortController();
|
||||
|
||||
async function load() {
|
||||
setLoading(true);
|
||||
setErr(null);
|
||||
|
||||
try {
|
||||
const qs = new URLSearchParams();
|
||||
qs.set("kind", "downtime");
|
||||
qs.set("range", range);
|
||||
if (machineId) qs.set("machineId", machineId);
|
||||
|
||||
const res = await fetch(`/api/analytics/pareto?${qs.toString()}`, {
|
||||
cache: "no-cache",
|
||||
credentials: "include",
|
||||
signal: controller.signal,
|
||||
});
|
||||
|
||||
const json = (await res.json().catch(() => ({}))) as ParetoResponse;
|
||||
|
||||
if (!res.ok || json?.ok === false) {
|
||||
setPareto(null);
|
||||
setErr(json?.error ?? "Failed to load pareto.");
|
||||
setLoading(false);
|
||||
return;
|
||||
}
|
||||
|
||||
setPareto(json);
|
||||
|
||||
// Optional coverage (fail silently if endpoint not ready)
|
||||
if (showCoverage) {
|
||||
const cqs = new URLSearchParams();
|
||||
cqs.set("kind", "downtime");
|
||||
cqs.set("range", range);
|
||||
if (machineId) cqs.set("machineId", machineId);
|
||||
|
||||
fetch(`/api/analytics/coverage?${cqs.toString()}`, {
|
||||
cache: "no-cache",
|
||||
credentials: "include",
|
||||
signal: controller.signal,
|
||||
})
|
||||
.then((r) => (r.ok ? r.json() : null))
|
||||
.then((cj) => (cj ? (cj as CoverageResponse) : null))
|
||||
.then((cj) => {
|
||||
if (cj) setCoverage(cj);
|
||||
})
|
||||
.catch(() => {
|
||||
// ignore
|
||||
});
|
||||
}
|
||||
|
||||
setLoading(false);
|
||||
} catch (e: any) {
|
||||
if (e?.name === "AbortError") return;
|
||||
setErr("Network error.");
|
||||
setLoading(false);
|
||||
}
|
||||
}
|
||||
|
||||
load();
|
||||
return () => controller.abort();
|
||||
}, [machineId, range, showCoverage]);
|
||||
|
||||
const rows = pareto?.rows ?? [];
|
||||
|
||||
const chartData = useMemo(() => {
|
||||
return rows.slice(0, barsLimit).map((r, idx) => ({
|
||||
i: idx,
|
||||
reasonCode: r.reasonCode,
|
||||
reasonLabel: r.reasonLabel,
|
||||
label: clampLabel(r.reasonLabel || r.reasonCode, isSummary ? 16 : 22),
|
||||
minutes: Number(r.minutesLost ?? 0),
|
||||
pctOfTotal: Number(r.pctOfTotal ?? 0),
|
||||
cumulativePct: Number(r.cumulativePct ?? 0),
|
||||
}));
|
||||
}, [rows, barsLimit, isSummary]);
|
||||
|
||||
|
||||
const top3 = useMemo(() => {
|
||||
if (pareto?.top3?.length) return pareto.top3.slice(0, 3);
|
||||
return [...rows]
|
||||
.sort((a, b) => Number(b.minutesLost ?? 0) - Number(a.minutesLost ?? 0))
|
||||
.slice(0, 3);
|
||||
}, [pareto?.top3, rows]);
|
||||
|
||||
const totalMinutes = Number(pareto?.totalMinutesLost ?? 0);
|
||||
|
||||
const covPct = normalizePct(coverage?.receivedCoveragePct ?? null);
|
||||
const pending = coverage?.pendingEpisodesCount ?? null;
|
||||
|
||||
const title =
|
||||
range === "24h"
|
||||
? "Downtime Pareto (24h)"
|
||||
: range === "30d"
|
||||
? "Downtime Pareto (30d)"
|
||||
: range === "mtd"
|
||||
? "Downtime Pareto (MTD)"
|
||||
: "Downtime Pareto (7d)";
|
||||
|
||||
|
||||
const reportHref = machineId
|
||||
? `/downtime?machineId=${encodeURIComponent(machineId)}&range=${encodeURIComponent(range)}`
|
||||
: `/downtime?range=${encodeURIComponent(range)}`;
|
||||
|
||||
return (
|
||||
<div className={`rounded-2xl border border-white/10 bg-white/5 ${containerPad}`}>
|
||||
<div className="flex flex-wrap items-start justify-between gap-3">
|
||||
<div>
|
||||
<div className="text-sm font-semibold text-white">{title}</div>
|
||||
<div className="mt-1 text-xs text-zinc-400">
|
||||
Total: <span className="text-white">{totalMinutes.toFixed(0)} min</span>
|
||||
{covPct != null ? (
|
||||
<>
|
||||
<span className="mx-2 text-zinc-600">•</span>
|
||||
Coverage: <span className="text-white">{covPct.toFixed(0)}%</span>
|
||||
{pending != null ? (
|
||||
<>
|
||||
<span className="mx-2 text-zinc-600">•</span>
|
||||
Pending: <span className="text-white">{pending}</span>
|
||||
</>
|
||||
) : null}
|
||||
</>
|
||||
) : null}
|
||||
</div>
|
||||
</div>
|
||||
{showOpenFullReport ? (
|
||||
<Link
|
||||
href={reportHref}
|
||||
className="rounded-xl border border-white/10 bg-white/5 px-3 py-1.5 text-xs text-white hover:bg-white/10"
|
||||
>
|
||||
View full report →
|
||||
</Link>
|
||||
) : null}
|
||||
</div>
|
||||
|
||||
{loading ? (
|
||||
<div className="mt-4 text-sm text-zinc-400">{t("machine.detail.loading")}</div>
|
||||
) : err ? (
|
||||
<div className="mt-4 rounded-2xl border border-red-500/20 bg-red-500/10 p-4 text-sm text-red-200">
|
||||
{err}
|
||||
</div>
|
||||
) : rows.length === 0 ? (
|
||||
<div className="mt-4 text-sm text-zinc-400">No downtime reasons found for this range.</div>
|
||||
) : (
|
||||
<div className="mt-4 grid grid-cols-1 gap-4 lg:grid-cols-3">
|
||||
<div
|
||||
className={`${chartHeightClass} rounded-3xl border border-white/10 bg-black/30 p-4 backdrop-blur lg:col-span-2`}
|
||||
style={{ boxShadow: "var(--app-chart-shadow)" }}
|
||||
>
|
||||
<ResponsiveContainer width="100%" height="100%">
|
||||
<ComposedChart data={chartData} margin={{ top: 10, right: 24, left: 0, bottom: 10 }}>
|
||||
<CartesianGrid strokeDasharray="3 3" stroke="var(--app-chart-grid)" />
|
||||
<XAxis
|
||||
dataKey="label"
|
||||
interval={0}
|
||||
tick={{ fill: "var(--app-chart-tick)", fontSize: 11 }}
|
||||
/>
|
||||
<YAxis
|
||||
yAxisId="left"
|
||||
tick={{ fill: "var(--app-chart-tick)" }}
|
||||
width={40}
|
||||
/>
|
||||
<YAxis
|
||||
yAxisId="right"
|
||||
orientation="right"
|
||||
domain={[0, 100]}
|
||||
tick={{ fill: "var(--app-chart-tick)" }}
|
||||
tickFormatter={(v) => `${v}%`}
|
||||
width={44}
|
||||
/>
|
||||
<Tooltip
|
||||
cursor={{ stroke: "var(--app-chart-grid)" }}
|
||||
contentStyle={{
|
||||
background: "var(--app-chart-tooltip-bg)",
|
||||
border: "1px solid var(--app-chart-tooltip-border)",
|
||||
}}
|
||||
labelStyle={{ color: "var(--app-chart-label)" }}
|
||||
formatter={(val: any, name: any, ctx: any) => {
|
||||
if (name === "minutes") return [`${Number(val).toFixed(1)} min`, "Minutes"];
|
||||
if (name === "cumulativePct") return [`${Number(val).toFixed(1)}%`, "Cumulative"];
|
||||
return [val, name];
|
||||
}}
|
||||
/>
|
||||
|
||||
<ReferenceLine
|
||||
yAxisId="right"
|
||||
y={80}
|
||||
stroke="rgba(255,255,255,0.25)"
|
||||
strokeDasharray="6 6"
|
||||
/>
|
||||
|
||||
<Bar
|
||||
yAxisId="left"
|
||||
dataKey="minutes"
|
||||
radius={[10, 10, 0, 0]}
|
||||
isAnimationActive={false}
|
||||
fill="#FF7A00"
|
||||
/>
|
||||
<Line
|
||||
yAxisId="right"
|
||||
dataKey="cumulativePct"
|
||||
dot={false}
|
||||
strokeWidth={2}
|
||||
isAnimationActive={false}
|
||||
stroke="#12D18E"
|
||||
/>
|
||||
</ComposedChart>
|
||||
</ResponsiveContainer>
|
||||
</div>
|
||||
|
||||
<div className={`rounded-2xl border border-white/10 bg-black/20 ${isSummary ? "p-3" : "p-4"}`}>
|
||||
<div className="text-xs font-semibold text-white">Top 3 reasons</div>
|
||||
<div className="mt-3 space-y-3">
|
||||
{top3.map((r) => (
|
||||
<div key={r.reasonCode} className="rounded-xl border border-white/10 bg-white/5 p-3">
|
||||
<div className="flex items-start justify-between gap-3">
|
||||
<div className="min-w-0">
|
||||
<div className="truncate text-sm font-semibold text-white">
|
||||
{r.reasonLabel || r.reasonCode}
|
||||
</div>
|
||||
<div className="mt-1 text-xs text-zinc-400">{r.reasonCode}</div>
|
||||
</div>
|
||||
<div className="shrink-0 text-right">
|
||||
<div className="text-sm font-semibold text-white">
|
||||
{(r.minutesLost ?? 0).toFixed(0)}m
|
||||
</div>
|
||||
<div className="text-xs text-zinc-400">{(r.pctOfTotal ?? 0).toFixed(1)}%</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
|
||||
{!isSummary && pareto?.threshold80 ? (
|
||||
<div className="mt-4 rounded-xl border border-white/10 bg-white/5 p-3 text-xs text-zinc-300">
|
||||
80% cutoff:{" "}
|
||||
<span className="text-white">
|
||||
{pareto.threshold80.reasonLabel} ({pareto.threshold80.reasonCode})
|
||||
</span>
|
||||
</div>
|
||||
) : null}
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -1,25 +1,34 @@
|
||||
"use client";
|
||||
|
||||
import { usePathname, useRouter } from "next/navigation";
|
||||
import { useEffect, useState } from "react";
|
||||
import { useEffect, useSyncExternalStore } from "react";
|
||||
|
||||
function subscribe(callback: () => void) {
|
||||
if (typeof window === "undefined") return () => {};
|
||||
window.addEventListener("storage", callback);
|
||||
return () => window.removeEventListener("storage", callback);
|
||||
}
|
||||
|
||||
function getSnapshot() {
|
||||
if (typeof window === "undefined") return null;
|
||||
return localStorage.getItem("ct_token");
|
||||
}
|
||||
|
||||
export function RequireAuth({ children }: { children: React.ReactNode }) {
|
||||
const router = useRouter();
|
||||
const pathname = usePathname();
|
||||
const [ready, setReady] = useState(false);
|
||||
const token = useSyncExternalStore(subscribe, getSnapshot, () => null);
|
||||
const hasToken = Boolean(token);
|
||||
|
||||
useEffect(() => {
|
||||
const token = localStorage.getItem("ct_token");
|
||||
if (!token) {
|
||||
if (!hasToken) {
|
||||
router.replace("/login");
|
||||
return;
|
||||
}
|
||||
setReady(true);
|
||||
}, [router, pathname]);
|
||||
}, [router, pathname, hasToken]);
|
||||
|
||||
if (!ready) {
|
||||
if (!hasToken) {
|
||||
return (
|
||||
<div className="min-h-screen bg-[#070A0C] text-zinc-200 flex items-center justify-center">
|
||||
<div className="min-h-screen bg-black text-zinc-200 flex items-center justify-center">
|
||||
Loading…
|
||||
</div>
|
||||
);
|
||||
|
||||
2205
components/downtime/DowntimePageClient.tsx
Normal file
2205
components/downtime/DowntimePageClient.tsx
Normal file
File diff suppressed because it is too large
Load Diff
72
components/layout/AppShell.tsx
Normal file
72
components/layout/AppShell.tsx
Normal file
@@ -0,0 +1,72 @@
|
||||
"use client";
|
||||
|
||||
import { useEffect, useState } from "react";
|
||||
import { Menu } from "lucide-react";
|
||||
import { Sidebar } from "@/components/layout/Sidebar";
|
||||
import { UtilityControls } from "@/components/layout/UtilityControls";
|
||||
import { useI18n } from "@/lib/i18n/useI18n";
|
||||
|
||||
export function AppShell({
|
||||
children,
|
||||
initialTheme,
|
||||
}: {
|
||||
children: React.ReactNode;
|
||||
initialTheme?: "dark" | "light";
|
||||
}) {
|
||||
const { t } = useI18n();
|
||||
const [drawerOpen, setDrawerOpen] = useState(false);
|
||||
|
||||
useEffect(() => {
|
||||
if (!drawerOpen) return;
|
||||
const onKey = (event: KeyboardEvent) => {
|
||||
if (event.key === "Escape") setDrawerOpen(false);
|
||||
};
|
||||
window.addEventListener("keydown", onKey);
|
||||
document.body.style.overflow = "hidden";
|
||||
return () => {
|
||||
window.removeEventListener("keydown", onKey);
|
||||
document.body.style.overflow = "";
|
||||
};
|
||||
}, [drawerOpen]);
|
||||
|
||||
return (
|
||||
<div className="h-screen overflow-hidden bg-black text-white">
|
||||
<div className="flex h-full">
|
||||
<Sidebar />
|
||||
<div className="flex h-full flex-1 flex-col">
|
||||
<header className="sticky top-0 z-30 flex min-h-[3.5rem] flex-wrap items-center justify-between gap-3 border-b border-white/10 bg-black/20 px-4 py-2 backdrop-blur">
|
||||
<div className="flex items-center gap-3">
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => setDrawerOpen(true)}
|
||||
aria-label={t("common.openMenu")}
|
||||
className="md:hidden rounded-lg border border-white/10 bg-white/5 p-2 text-zinc-300 hover:bg-white/10 hover:text-white"
|
||||
>
|
||||
<Menu className="h-4 w-4" />
|
||||
</button>
|
||||
<div className="text-sm font-semibold tracking-wide md:hidden">
|
||||
{t("sidebar.productTitle")}
|
||||
</div>
|
||||
</div>
|
||||
<UtilityControls initialTheme={initialTheme} />
|
||||
</header>
|
||||
<main className="flex-1 overflow-y-auto">{children}</main>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{drawerOpen && (
|
||||
<div className="fixed inset-0 z-40 md:hidden" role="dialog" aria-modal="true">
|
||||
<button
|
||||
type="button"
|
||||
className="absolute inset-0 bg-black/70"
|
||||
aria-label={t("common.close")}
|
||||
onClick={() => setDrawerOpen(false)}
|
||||
/>
|
||||
<div className="absolute inset-y-0 left-0 flex w-72 max-w-[85vw] flex-col bg-black/40">
|
||||
<Sidebar variant="drawer" onNavigate={() => setDrawerOpen(false)} onClose={() => setDrawerOpen(false)} />
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -2,38 +2,138 @@
|
||||
|
||||
import Link from "next/link";
|
||||
import { usePathname, useRouter } from "next/navigation";
|
||||
import { useEffect, useMemo, useState } from "react";
|
||||
import { BarChart3, Bell, DollarSign, LayoutGrid, LogOut, Settings, Wrench, X } from "lucide-react";
|
||||
import type { LucideIcon } from "lucide-react";
|
||||
import { useI18n } from "@/lib/i18n/useI18n";
|
||||
import { useScreenlessMode } from "@/lib/ui/screenlessMode";
|
||||
|
||||
|
||||
type NavItem = {
|
||||
href: string;
|
||||
labelKey: string;
|
||||
icon: LucideIcon;
|
||||
ownerOnly?: boolean;
|
||||
};
|
||||
|
||||
const items: NavItem[] = [
|
||||
{ href: "/overview", labelKey: "nav.overview", icon: LayoutGrid },
|
||||
{ href: "/machines", labelKey: "nav.machines", icon: Wrench },
|
||||
{ href: "/reports", labelKey: "nav.reports", icon: BarChart3 },
|
||||
{ href: "/alerts", labelKey: "nav.alerts", icon: Bell },
|
||||
{ href: "/financial", labelKey: "nav.financial", icon: DollarSign, ownerOnly: true },
|
||||
{ href: "/settings", labelKey: "nav.settings", icon: Settings },
|
||||
{ href: "/downtime", labelKey: "nav.downtime", icon: BarChart3 },
|
||||
|
||||
const items = [
|
||||
{ href: "/overview", label: "Overview", icon: "🏠" },
|
||||
{ href: "/machines", label: "Machines", icon: "🏭" },
|
||||
{ href: "/reports", label: "Reports", icon: "📊" },
|
||||
{ href: "/settings", label: "Settings", icon: "⚙️" },
|
||||
];
|
||||
|
||||
export function Sidebar() {
|
||||
type SidebarProps = {
|
||||
variant?: "desktop" | "drawer";
|
||||
onNavigate?: () => void;
|
||||
onClose?: () => void;
|
||||
};
|
||||
|
||||
export function Sidebar({ variant = "desktop", onNavigate, onClose }: SidebarProps) {
|
||||
const pathname = usePathname();
|
||||
const router = useRouter();
|
||||
const { t } = useI18n();
|
||||
const { screenlessMode } = useScreenlessMode();
|
||||
const [me, setMe] = useState<{
|
||||
user?: { name?: string | null; email?: string | null };
|
||||
org?: { name?: string | null };
|
||||
membership?: { role?: string | null };
|
||||
} | null>(null);
|
||||
|
||||
useEffect(() => {
|
||||
let alive = true;
|
||||
async function loadMe() {
|
||||
try {
|
||||
const res = await fetch("/api/me", { cache: "no-store" });
|
||||
const data = await res.json().catch(() => ({}));
|
||||
if (alive && res.ok && data?.ok) {
|
||||
setMe(data);
|
||||
}
|
||||
} catch {
|
||||
if (alive) setMe(null);
|
||||
}
|
||||
}
|
||||
loadMe();
|
||||
return () => {
|
||||
alive = false;
|
||||
};
|
||||
}, []);
|
||||
|
||||
async function onLogout() {
|
||||
await fetch("/api/logout", { method: "POST" });
|
||||
router.push("/login");
|
||||
router.refresh();
|
||||
onNavigate?.();
|
||||
}
|
||||
|
||||
const roleKey = (me?.membership?.role || "MEMBER").toLowerCase();
|
||||
const isOwner = roleKey === "owner";
|
||||
const visibleItems = useMemo(() => {
|
||||
return items.filter((it) => {
|
||||
if (it.ownerOnly && !isOwner) return false;
|
||||
if (screenlessMode && it.href === "/downtime") return false;
|
||||
return true;
|
||||
});
|
||||
}, [isOwner, screenlessMode]);
|
||||
|
||||
useEffect(() => {
|
||||
if (screenlessMode && pathname.startsWith("/downtime")) {
|
||||
router.replace("/overview");
|
||||
}
|
||||
}, [screenlessMode, pathname, router]);
|
||||
|
||||
useEffect(() => {
|
||||
if (!screenlessMode) return;
|
||||
if (pathname === "/downtime" || pathname.startsWith("/downtime/")) {
|
||||
router.replace("/overview");
|
||||
}
|
||||
}, [screenlessMode, pathname, router]);
|
||||
|
||||
|
||||
|
||||
useEffect(() => {
|
||||
visibleItems.forEach((it) => {
|
||||
router.prefetch(it.href);
|
||||
});
|
||||
}, [router, visibleItems]);
|
||||
const shellClass = [
|
||||
"relative z-20 flex flex-col border-r border-white/10 bg-black/40 shrink-0",
|
||||
variant === "desktop" ? "hidden md:flex h-screen w-64" : "flex h-full w-72 max-w-[85vw]",
|
||||
].join(" ");
|
||||
|
||||
return (
|
||||
<aside className="hidden md:flex h-screen w-64 flex-col border-r border-white/10 bg-black/40">
|
||||
<div className="px-5 py-4">
|
||||
<div className="text-white font-semibold tracking-wide">MIS</div>
|
||||
<div className="text-xs text-zinc-500">Control Tower</div>
|
||||
<aside className={shellClass} aria-label={t("sidebar.productTitle")}>
|
||||
<div className="px-5 py-4 flex items-center justify-between gap-3">
|
||||
<div>
|
||||
<div className="text-white font-semibold tracking-wide">{t("sidebar.productTitle")}</div>
|
||||
<div className="text-xs text-zinc-500">{t("sidebar.productSubtitle")}</div>
|
||||
</div>
|
||||
{variant === "drawer" && onClose && (
|
||||
<button
|
||||
type="button"
|
||||
onClick={onClose}
|
||||
aria-label={t("common.close")}
|
||||
className="rounded-lg border border-white/10 bg-white/5 p-2 text-zinc-300 hover:bg-white/10 hover:text-white md:hidden"
|
||||
>
|
||||
<X className="h-4 w-4" />
|
||||
</button>
|
||||
)}
|
||||
</div>
|
||||
|
||||
<nav className="px-3 py-2 flex-1 space-y-1">
|
||||
{items.map((it) => {
|
||||
{visibleItems.map((it) => {
|
||||
const active = pathname === it.href || pathname.startsWith(it.href + "/");
|
||||
const Icon = it.icon;
|
||||
return (
|
||||
<Link
|
||||
key={it.href}
|
||||
href={it.href}
|
||||
onMouseEnter={() => router.prefetch(it.href)}
|
||||
onClick={onNavigate}
|
||||
className={[
|
||||
"flex items-center gap-3 rounded-xl px-3 py-2 text-sm transition",
|
||||
active
|
||||
@@ -41,8 +141,8 @@ export function Sidebar() {
|
||||
: "text-zinc-300 hover:bg-white/5 hover:text-white",
|
||||
].join(" ")}
|
||||
>
|
||||
<span className="text-lg">{it.icon}</span>
|
||||
<span>{it.label}</span>
|
||||
<Icon className="h-4 w-4" />
|
||||
<span>{t(it.labelKey)}</span>
|
||||
</Link>
|
||||
);
|
||||
})}
|
||||
@@ -50,15 +150,24 @@ export function Sidebar() {
|
||||
|
||||
<div className="px-5 py-4 border-t border-white/10 space-y-3">
|
||||
<div>
|
||||
<div className="text-sm text-white">Juan Pérez</div>
|
||||
<div className="text-xs text-zinc-500">Plant Manager</div>
|
||||
<div className="text-sm text-white">
|
||||
{me?.user?.name || me?.user?.email || t("sidebar.userFallback")}
|
||||
</div>
|
||||
<div className="text-xs text-zinc-500">
|
||||
{me?.org?.name
|
||||
? `${me.org.name} - ${t(`sidebar.role.${roleKey}`)}`
|
||||
: t("sidebar.loadingOrg")}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<button
|
||||
onClick={onLogout}
|
||||
className="w-full rounded-xl border border-white/10 bg-white/5 px-3 py-2 text-sm text-zinc-200 hover:bg-white/10"
|
||||
>
|
||||
🚪 Logout
|
||||
<span className="flex items-center justify-center gap-2">
|
||||
<LogOut className="h-4 w-4" />
|
||||
{t("sidebar.logout")}
|
||||
</span>
|
||||
</button>
|
||||
</div>
|
||||
</aside>
|
||||
|
||||
109
components/layout/UtilityControls.tsx
Normal file
109
components/layout/UtilityControls.tsx
Normal file
@@ -0,0 +1,109 @@
|
||||
"use client";
|
||||
|
||||
import { useState } from "react";
|
||||
import { useRouter } from "next/navigation";
|
||||
import { useI18n } from "@/lib/i18n/useI18n";
|
||||
|
||||
const THEME_COOKIE = "mis_theme";
|
||||
|
||||
const SunIcon = ({ className }: { className?: string }) => (
|
||||
<svg
|
||||
className={className}
|
||||
viewBox="0 0 24 24"
|
||||
fill="none"
|
||||
stroke="currentColor"
|
||||
strokeWidth="1.6"
|
||||
strokeLinecap="round"
|
||||
strokeLinejoin="round"
|
||||
aria-hidden="true"
|
||||
>
|
||||
<circle cx="12" cy="12" r="4" />
|
||||
<path d="M12 2v2" />
|
||||
<path d="M12 20v2" />
|
||||
<path d="M4.93 4.93l1.41 1.41" />
|
||||
<path d="M17.66 17.66l1.41 1.41" />
|
||||
<path d="M2 12h2" />
|
||||
<path d="M20 12h2" />
|
||||
<path d="M4.93 19.07l1.41-1.41" />
|
||||
<path d="M17.66 6.34l1.41-1.41" />
|
||||
</svg>
|
||||
);
|
||||
|
||||
const MoonIcon = ({ className }: { className?: string }) => (
|
||||
<svg
|
||||
className={className}
|
||||
viewBox="0 0 24 24"
|
||||
fill="none"
|
||||
stroke="currentColor"
|
||||
strokeWidth="1.6"
|
||||
strokeLinecap="round"
|
||||
strokeLinejoin="round"
|
||||
aria-hidden="true"
|
||||
>
|
||||
<path d="M21 12.5A8.5 8.5 0 0 1 11.5 3a8.5 8.5 0 1 0 9.5 9.5z" />
|
||||
</svg>
|
||||
);
|
||||
|
||||
type UtilityControlsProps = {
|
||||
className?: string;
|
||||
initialTheme?: "dark" | "light";
|
||||
};
|
||||
|
||||
export function UtilityControls({ className, initialTheme = "dark" }: UtilityControlsProps) {
|
||||
const router = useRouter();
|
||||
const { locale, setLocale, t } = useI18n();
|
||||
const [theme, setTheme] = useState<"dark" | "light">(initialTheme);
|
||||
|
||||
function applyTheme(next: "light" | "dark") {
|
||||
document.documentElement.setAttribute("data-theme", next);
|
||||
document.cookie = `${THEME_COOKIE}=${next}; Path=/; Max-Age=31536000; SameSite=Lax`;
|
||||
setTheme(next);
|
||||
}
|
||||
|
||||
function toggleTheme() {
|
||||
applyTheme(theme === "light" ? "dark" : "light");
|
||||
}
|
||||
|
||||
function switchLocale(nextLocale: "en" | "es-MX") {
|
||||
setLocale(nextLocale);
|
||||
router.refresh();
|
||||
}
|
||||
|
||||
return (
|
||||
<div
|
||||
className={[
|
||||
"pointer-events-auto flex flex-wrap items-center gap-2 rounded-xl border border-white/10 bg-white/5 px-2 py-1 sm:gap-3 sm:px-3 sm:py-2",
|
||||
className ?? "",
|
||||
].join(" ")}
|
||||
title={t("sidebar.themeTooltip")}
|
||||
>
|
||||
<button
|
||||
type="button"
|
||||
onClick={toggleTheme}
|
||||
aria-label={theme === "light" ? t("sidebar.switchToDark") : t("sidebar.switchToLight")}
|
||||
className="flex h-8 w-8 items-center justify-center rounded-full border border-white/10 bg-black/30 text-white hover:bg-white/10 transition"
|
||||
>
|
||||
{theme === "light" ? <SunIcon className="h-4 w-4" /> : <MoonIcon className="h-4 w-4" />}
|
||||
</button>
|
||||
<div className="flex items-center gap-2 text-[10px] font-semibold tracking-[0.2em] sm:text-[11px]">
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => switchLocale("en")}
|
||||
aria-pressed={locale === "en"}
|
||||
className={locale === "en" ? "text-white" : "text-zinc-400 hover:text-white"}
|
||||
>
|
||||
EN
|
||||
</button>
|
||||
<span className="text-zinc-500">|</span>
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => switchLocale("es-MX")}
|
||||
aria-pressed={locale === "es-MX"}
|
||||
className={locale === "es-MX" ? "text-white" : "text-zinc-400 hover:text-white"}
|
||||
>
|
||||
ES
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
110
components/reports/DowntimeParetoReportClient.tsx
Normal file
110
components/reports/DowntimeParetoReportClient.tsx
Normal file
@@ -0,0 +1,110 @@
|
||||
"use client";
|
||||
|
||||
import { useEffect, useMemo, useState } from "react";
|
||||
import { useSearchParams, useRouter } from "next/navigation";
|
||||
import DowntimeParetoCard from "@/components/analytics/DowntimeParetoCard";
|
||||
import { usePathname } from "next/navigation";
|
||||
|
||||
|
||||
import { DOWNTIME_RANGES, coerceDowntimeRange, type DowntimeRange } from "@/lib/analytics/downtimeRange";
|
||||
|
||||
type MachineLite = {
|
||||
id: string;
|
||||
name: string;
|
||||
siteName?: string | null; // optional for later
|
||||
};
|
||||
|
||||
export default function DowntimeParetoReportClient() {
|
||||
const sp = useSearchParams();
|
||||
const router = useRouter();
|
||||
const pathname = usePathname();
|
||||
|
||||
const [range, setRange] = useState<DowntimeRange>(coerceDowntimeRange(sp.get("range")));
|
||||
const [machineId, setMachineId] = useState<string>(sp.get("machineId") || "");
|
||||
const [machines, setMachines] = useState<MachineLite[]>([]);
|
||||
const [loadingMachines, setLoadingMachines] = useState(true);
|
||||
|
||||
// Keep URL in sync (so deep-links work)
|
||||
|
||||
useEffect(() => {
|
||||
const qs = new URLSearchParams();
|
||||
if (range) qs.set("range", range);
|
||||
if (machineId) qs.set("machineId", machineId);
|
||||
|
||||
const next = `${pathname}?${qs.toString()}`;
|
||||
const current = `${pathname}?${sp.toString()}`;
|
||||
|
||||
// avoid needless replace loops
|
||||
if (next !== current) router.replace(next);
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||
}, [range, machineId, pathname]);
|
||||
|
||||
|
||||
useEffect(() => {
|
||||
let cancelled = false;
|
||||
|
||||
async function loadMachines() {
|
||||
setLoadingMachines(true);
|
||||
try {
|
||||
// Use whatever endpoint you already have for listing machines:
|
||||
// If you don’t have one, easiest is GET /api/machines returning [{id,name}]
|
||||
const res = await fetch("/api/machines", { credentials: "include" });
|
||||
const json = await res.json();
|
||||
if (!cancelled && res.ok) setMachines(json.machines ?? json ?? []);
|
||||
} finally {
|
||||
if (!cancelled) setLoadingMachines(false);
|
||||
}
|
||||
}
|
||||
|
||||
loadMachines();
|
||||
return () => {
|
||||
cancelled = true;
|
||||
};
|
||||
}, []);
|
||||
|
||||
const machineOptions = useMemo(() => {
|
||||
return [{ id: "", name: "All machines" }, ...machines];
|
||||
}, [machines]);
|
||||
|
||||
return (
|
||||
<div className="space-y-4">
|
||||
<div className="flex flex-wrap items-center justify-between gap-3">
|
||||
<div>
|
||||
<div className="text-lg font-semibold text-white">Downtime Pareto</div>
|
||||
<div className="text-sm text-zinc-400">Org-wide report with drilldown</div>
|
||||
</div>
|
||||
|
||||
<div className="flex flex-wrap gap-2">
|
||||
<select
|
||||
className="rounded-xl border border-white/10 bg-black/40 px-3 py-2 text-sm text-white"
|
||||
value={range}
|
||||
onChange={(e) => setRange(e.target.value as DowntimeRange)}
|
||||
>
|
||||
<option className="bg-black text-white" value="24h">Last 24h</option>
|
||||
<option className="bg-black text-white" value="7d">Last 7d</option>
|
||||
<option className="bg-black text-white" value="30d">Last 30d</option>
|
||||
</select>
|
||||
|
||||
<select
|
||||
className="min-w-[240px] rounded-xl border border-white/10 bg-white/5 px-3 py-2 text-sm text-white"
|
||||
value={machineId}
|
||||
onChange={(e) => setMachineId(e.target.value)}
|
||||
disabled={loadingMachines}
|
||||
>
|
||||
{machineOptions.map((m) => (
|
||||
<option className="bg-black text-white" key={m.id || "all"} value={m.id}>
|
||||
{m.name}
|
||||
</option>
|
||||
))}
|
||||
</select>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<DowntimeParetoCard
|
||||
range={range}
|
||||
machineId={machineId || undefined}
|
||||
showOpenFullReport={false}
|
||||
/>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
777
components/settings/AlertsConfig.tsx
Normal file
777
components/settings/AlertsConfig.tsx
Normal file
@@ -0,0 +1,777 @@
|
||||
"use client";
|
||||
|
||||
import { useEffect, useMemo, useState } from "react";
|
||||
import { useI18n } from "@/lib/i18n/useI18n";
|
||||
|
||||
type RoleName = "MEMBER" | "ADMIN" | "OWNER";
|
||||
type Channel = "email" | "sms";
|
||||
|
||||
type RoleRule = {
|
||||
enabled: boolean;
|
||||
afterMinutes: number;
|
||||
channels: Channel[];
|
||||
};
|
||||
|
||||
type AlertRule = {
|
||||
id: string;
|
||||
eventType: string;
|
||||
roles: Record<RoleName, RoleRule>;
|
||||
repeatMinutes?: number;
|
||||
};
|
||||
|
||||
type AlertPolicy = {
|
||||
version: number;
|
||||
defaults: Record<RoleName, RoleRule>;
|
||||
rules: AlertRule[];
|
||||
};
|
||||
|
||||
type AlertContact = {
|
||||
id: string;
|
||||
name: string;
|
||||
roleScope: string;
|
||||
email?: string | null;
|
||||
phone?: string | null;
|
||||
eventTypes?: string[] | null;
|
||||
isActive: boolean;
|
||||
userId?: string | null;
|
||||
};
|
||||
|
||||
type ContactDraft = {
|
||||
name: string;
|
||||
roleScope: string;
|
||||
email: string;
|
||||
phone: string;
|
||||
eventTypes: string[];
|
||||
isActive: boolean;
|
||||
};
|
||||
|
||||
const ROLE_ORDER: RoleName[] = ["MEMBER", "ADMIN", "OWNER"];
|
||||
const CHANNELS: Channel[] = ["email", "sms"];
|
||||
const EVENT_TYPES = [
|
||||
{ value: "macrostop", labelKey: "alerts.event.macrostop" },
|
||||
{ value: "microstop", labelKey: "alerts.event.microstop" },
|
||||
{ value: "slow-cycle", labelKey: "alerts.event.slow-cycle" },
|
||||
{ value: "offline", labelKey: "alerts.event.offline" },
|
||||
{ value: "error", labelKey: "alerts.event.error" },
|
||||
] as const;
|
||||
|
||||
function normalizeContactDraft(contact: AlertContact): ContactDraft {
|
||||
return {
|
||||
name: contact.name,
|
||||
roleScope: contact.roleScope,
|
||||
email: contact.email ?? "",
|
||||
phone: contact.phone ?? "",
|
||||
eventTypes: Array.isArray(contact.eventTypes) ? contact.eventTypes : [],
|
||||
isActive: contact.isActive,
|
||||
};
|
||||
}
|
||||
|
||||
export function AlertsConfig() {
|
||||
const { t } = useI18n();
|
||||
const [policy, setPolicy] = useState<AlertPolicy | null>(null);
|
||||
const [policyDraft, setPolicyDraft] = useState<AlertPolicy | null>(null);
|
||||
const [contacts, setContacts] = useState<AlertContact[]>([]);
|
||||
const [contactEdits, setContactEdits] = useState<Record<string, ContactDraft>>({});
|
||||
const [loading, setLoading] = useState(true);
|
||||
const [role, setRole] = useState<RoleName>("MEMBER");
|
||||
const [savingPolicy, setSavingPolicy] = useState(false);
|
||||
const [policyError, setPolicyError] = useState<string | null>(null);
|
||||
const [contactsError, setContactsError] = useState<string | null>(null);
|
||||
const [savingContactId, setSavingContactId] = useState<string | null>(null);
|
||||
const [deletingContactId, setDeletingContactId] = useState<string | null>(null);
|
||||
const [selectedEventType, setSelectedEventType] = useState<string>("");
|
||||
|
||||
const [newContact, setNewContact] = useState<ContactDraft>({
|
||||
name: "",
|
||||
roleScope: "CUSTOM",
|
||||
email: "",
|
||||
phone: "",
|
||||
eventTypes: [],
|
||||
isActive: true,
|
||||
});
|
||||
const [creatingContact, setCreatingContact] = useState(false);
|
||||
const [createError, setCreateError] = useState<string | null>(null);
|
||||
|
||||
useEffect(() => {
|
||||
let alive = true;
|
||||
|
||||
async function load() {
|
||||
setLoading(true);
|
||||
setPolicyError(null);
|
||||
setContactsError(null);
|
||||
try {
|
||||
const [policyRes, contactsRes, meRes] = await Promise.all([
|
||||
fetch("/api/alerts/policy", { cache: "no-store" }),
|
||||
fetch("/api/alerts/contacts", { cache: "no-store" }),
|
||||
fetch("/api/me", { cache: "no-store" }),
|
||||
]);
|
||||
const policyJson = await policyRes.json().catch(() => ({}));
|
||||
const contactsJson = await contactsRes.json().catch(() => ({}));
|
||||
const meJson = await meRes.json().catch(() => ({}));
|
||||
|
||||
if (!alive) return;
|
||||
|
||||
if (!policyRes.ok || !policyJson?.ok) {
|
||||
setPolicyError(policyJson?.error || t("alerts.error.loadPolicy"));
|
||||
} else {
|
||||
setPolicy(policyJson.policy);
|
||||
setPolicyDraft(policyJson.policy);
|
||||
if (policyJson.policy?.rules?.length) {
|
||||
setSelectedEventType((prev) => prev || policyJson.policy.rules[0].eventType);
|
||||
}
|
||||
}
|
||||
|
||||
if (!contactsRes.ok || !contactsJson?.ok) {
|
||||
setContactsError(contactsJson?.error || t("alerts.error.loadContacts"));
|
||||
} else {
|
||||
setContacts(contactsJson.contacts ?? []);
|
||||
const nextEdits: Record<string, ContactDraft> = {};
|
||||
for (const contact of contactsJson.contacts ?? []) {
|
||||
nextEdits[contact.id] = normalizeContactDraft(contact);
|
||||
}
|
||||
setContactEdits(nextEdits);
|
||||
}
|
||||
|
||||
if (meRes.ok && meJson?.ok && meJson?.membership?.role) {
|
||||
setRole(String(meJson.membership.role).toUpperCase() as RoleName);
|
||||
}
|
||||
} catch {
|
||||
if (!alive) return;
|
||||
setPolicyError(t("alerts.error.loadPolicy"));
|
||||
setContactsError(t("alerts.error.loadContacts"));
|
||||
} finally {
|
||||
if (alive) setLoading(false);
|
||||
}
|
||||
}
|
||||
|
||||
load();
|
||||
return () => {
|
||||
alive = false;
|
||||
};
|
||||
}, [t]);
|
||||
|
||||
useEffect(() => {
|
||||
if (!policyDraft?.rules?.length) return;
|
||||
setSelectedEventType((prev) => {
|
||||
if (prev && policyDraft.rules.some((rule) => rule.eventType === prev)) {
|
||||
return prev;
|
||||
}
|
||||
return policyDraft.rules[0].eventType;
|
||||
});
|
||||
}, [policyDraft]);
|
||||
|
||||
function updatePolicyDefaults(role: RoleName, patch: Partial<RoleRule>) {
|
||||
setPolicyDraft((prev) => {
|
||||
if (!prev) return prev;
|
||||
return {
|
||||
...prev,
|
||||
defaults: {
|
||||
...prev.defaults,
|
||||
[role]: {
|
||||
...prev.defaults[role],
|
||||
...patch,
|
||||
},
|
||||
},
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
function updateRule(eventType: string, patch: Partial<AlertRule>) {
|
||||
setPolicyDraft((prev) => {
|
||||
if (!prev) return prev;
|
||||
return {
|
||||
...prev,
|
||||
rules: prev.rules.map((rule) =>
|
||||
rule.eventType === eventType ? { ...rule, ...patch } : rule
|
||||
),
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
function updateRuleRole(eventType: string, role: RoleName, patch: Partial<RoleRule>) {
|
||||
setPolicyDraft((prev) => {
|
||||
if (!prev) return prev;
|
||||
return {
|
||||
...prev,
|
||||
rules: prev.rules.map((rule) => {
|
||||
if (rule.eventType !== eventType) return rule;
|
||||
return {
|
||||
...rule,
|
||||
roles: {
|
||||
...rule.roles,
|
||||
[role]: {
|
||||
...rule.roles[role],
|
||||
...patch,
|
||||
},
|
||||
},
|
||||
};
|
||||
}),
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
function applyDefaultsToEvent(eventType: string) {
|
||||
setPolicyDraft((prev) => {
|
||||
if (!prev) return prev;
|
||||
return {
|
||||
...prev,
|
||||
rules: prev.rules.map((rule) => {
|
||||
if (rule.eventType !== eventType) return rule;
|
||||
return {
|
||||
...rule,
|
||||
roles: {
|
||||
MEMBER: { ...prev.defaults.MEMBER },
|
||||
ADMIN: { ...prev.defaults.ADMIN },
|
||||
OWNER: { ...prev.defaults.OWNER },
|
||||
},
|
||||
};
|
||||
}),
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
async function savePolicy() {
|
||||
if (!policyDraft) return;
|
||||
setSavingPolicy(true);
|
||||
setPolicyError(null);
|
||||
try {
|
||||
const res = await fetch("/api/alerts/policy", {
|
||||
method: "PUT",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ policy: policyDraft }),
|
||||
});
|
||||
const json = await res.json().catch(() => ({}));
|
||||
if (!res.ok || !json?.ok) {
|
||||
setPolicyError(json?.error || t("alerts.error.savePolicy"));
|
||||
} else {
|
||||
setPolicy(policyDraft);
|
||||
}
|
||||
} catch {
|
||||
setPolicyError(t("alerts.error.savePolicy"));
|
||||
} finally {
|
||||
setSavingPolicy(false);
|
||||
}
|
||||
}
|
||||
|
||||
function updateContactDraft(id: string, patch: Partial<ContactDraft>) {
|
||||
setContactEdits((prev) => ({
|
||||
...prev,
|
||||
[id]: {
|
||||
...(prev[id] ?? { name: "", roleScope: "CUSTOM", email: "", phone: "", eventTypes: [], isActive: true }),
|
||||
...patch,
|
||||
},
|
||||
}));
|
||||
}
|
||||
|
||||
async function saveContact(id: string) {
|
||||
const payload = contactEdits[id];
|
||||
if (!payload) return;
|
||||
setSavingContactId(id);
|
||||
try {
|
||||
const res = await fetch(`/api/alerts/contacts/${id}`, {
|
||||
method: "PUT",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify(payload),
|
||||
});
|
||||
const json = await res.json().catch(() => ({}));
|
||||
if (!res.ok || !json?.ok) {
|
||||
setContactsError(json?.error || t("alerts.error.saveContact"));
|
||||
} else if (json.contact) {
|
||||
const contact = json.contact as AlertContact;
|
||||
setContacts((prev) => prev.map((row) => (row.id === id ? contact : row)));
|
||||
setContactEdits((prev) => ({ ...prev, [id]: normalizeContactDraft(contact) }));
|
||||
}
|
||||
} catch {
|
||||
setContactsError(t("alerts.error.saveContact"));
|
||||
} finally {
|
||||
setSavingContactId(null);
|
||||
}
|
||||
}
|
||||
|
||||
async function deleteContact(id: string) {
|
||||
setDeletingContactId(id);
|
||||
try {
|
||||
const res = await fetch(`/api/alerts/contacts/${id}`, { method: "DELETE" });
|
||||
const json = await res.json().catch(() => ({}));
|
||||
if (!res.ok || !json?.ok) {
|
||||
setContactsError(json?.error || t("alerts.error.deleteContact"));
|
||||
} else {
|
||||
setContacts((prev) => prev.filter((row) => row.id !== id));
|
||||
setContactEdits((prev) => {
|
||||
const next = { ...prev };
|
||||
delete next[id];
|
||||
return next;
|
||||
});
|
||||
}
|
||||
} catch {
|
||||
setContactsError(t("alerts.error.deleteContact"));
|
||||
} finally {
|
||||
setDeletingContactId(null);
|
||||
}
|
||||
}
|
||||
|
||||
async function createContact() {
|
||||
setCreatingContact(true);
|
||||
setCreateError(null);
|
||||
try {
|
||||
const res = await fetch("/api/alerts/contacts", {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify(newContact),
|
||||
});
|
||||
const json = await res.json().catch(() => ({}));
|
||||
if (!res.ok || !json?.ok) {
|
||||
setCreateError(json?.error || t("alerts.error.createContact"));
|
||||
return;
|
||||
}
|
||||
const contact = json.contact as AlertContact;
|
||||
setContacts((prev) => [contact, ...prev]);
|
||||
setContactEdits((prev) => ({ ...prev, [contact.id]: normalizeContactDraft(contact) }));
|
||||
setNewContact({
|
||||
name: "",
|
||||
roleScope: "CUSTOM",
|
||||
email: "",
|
||||
phone: "",
|
||||
eventTypes: [],
|
||||
isActive: true,
|
||||
});
|
||||
} catch {
|
||||
setCreateError(t("alerts.error.createContact"));
|
||||
} finally {
|
||||
setCreatingContact(false);
|
||||
}
|
||||
}
|
||||
|
||||
const policyDirty = useMemo(
|
||||
() => JSON.stringify(policy) !== JSON.stringify(policyDraft),
|
||||
[policy, policyDraft]
|
||||
);
|
||||
const canEdit = role === "OWNER";
|
||||
|
||||
return (
|
||||
<div className="space-y-6">
|
||||
{loading && (
|
||||
<div className="text-sm text-zinc-400">{t("alerts.loading")}</div>
|
||||
)}
|
||||
|
||||
{!loading && policyError && (
|
||||
<div className="rounded-xl border border-red-500/20 bg-red-500/10 p-3 text-sm text-red-200">
|
||||
{policyError}
|
||||
</div>
|
||||
)}
|
||||
|
||||
{!loading && policyDraft && (
|
||||
<div className="rounded-2xl border border-white/10 bg-white/5 p-5">
|
||||
<div className="mb-4 flex items-center justify-between gap-4">
|
||||
<div>
|
||||
<div className="text-sm font-semibold text-white">{t("alerts.policy.title")}</div>
|
||||
<div className="text-xs text-zinc-400">{t("alerts.policy.subtitle")}</div>
|
||||
</div>
|
||||
<button
|
||||
type="button"
|
||||
onClick={savePolicy}
|
||||
disabled={!canEdit || !policyDirty || savingPolicy}
|
||||
className="rounded-xl border border-white/10 bg-white/10 px-4 py-2 text-sm text-white disabled:opacity-40"
|
||||
>
|
||||
{savingPolicy ? t("alerts.policy.saving") : t("alerts.policy.save")}
|
||||
</button>
|
||||
</div>
|
||||
|
||||
{!canEdit && (
|
||||
<div className="mb-4 rounded-xl border border-white/10 bg-black/20 p-3 text-sm text-zinc-300">
|
||||
{t("alerts.policy.readOnly")}
|
||||
</div>
|
||||
)}
|
||||
|
||||
<div className="rounded-xl border border-white/10 bg-black/20 p-4">
|
||||
<div className="mb-3 text-xs text-zinc-400">{t("alerts.policy.defaults")}</div>
|
||||
<div className="mb-4 text-xs text-zinc-500">{t("alerts.policy.defaultsHelp")}</div>
|
||||
<div className="grid grid-cols-1 gap-4 md:grid-cols-3">
|
||||
{ROLE_ORDER.map((role) => {
|
||||
const rule = policyDraft.defaults[role];
|
||||
return (
|
||||
<div key={role} className="rounded-xl border border-white/10 bg-black/20 p-3">
|
||||
<div className="text-sm font-semibold text-white">{role}</div>
|
||||
<label className="mt-3 flex items-center gap-2 text-xs text-zinc-400">
|
||||
<input
|
||||
type="checkbox"
|
||||
checked={rule.enabled}
|
||||
onChange={(event) => updatePolicyDefaults(role, { enabled: event.target.checked })}
|
||||
disabled={!canEdit}
|
||||
className="h-4 w-4 rounded border border-white/20 bg-black/20"
|
||||
/>
|
||||
{t("alerts.policy.enabled")}
|
||||
</label>
|
||||
<label className="mt-3 block text-xs text-zinc-400">
|
||||
{t("alerts.policy.afterMinutes")}
|
||||
<input
|
||||
type="number"
|
||||
min={0}
|
||||
value={rule.afterMinutes}
|
||||
onChange={(event) =>
|
||||
updatePolicyDefaults(role, { afterMinutes: Number(event.target.value) })
|
||||
}
|
||||
disabled={!canEdit}
|
||||
className="mt-2 w-full rounded-lg border border-white/10 bg-black/30 px-3 py-2 text-sm text-white"
|
||||
/>
|
||||
</label>
|
||||
<div className="mt-3 text-xs text-zinc-400">{t("alerts.policy.channels")}</div>
|
||||
<div className="mt-2 flex flex-wrap gap-3">
|
||||
{CHANNELS.map((channel) => (
|
||||
<label key={channel} className="flex items-center gap-2 text-xs text-zinc-400">
|
||||
<input
|
||||
type="checkbox"
|
||||
checked={rule.channels.includes(channel)}
|
||||
onChange={(event) => {
|
||||
const next = event.target.checked
|
||||
? [...rule.channels, channel]
|
||||
: rule.channels.filter((c) => c !== channel);
|
||||
updatePolicyDefaults(role, { channels: next });
|
||||
}}
|
||||
disabled={!canEdit}
|
||||
className="h-4 w-4 rounded border border-white/20 bg-black/20"
|
||||
/>
|
||||
{channel.toUpperCase()}
|
||||
</label>
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
})}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="mt-5 grid grid-cols-1 gap-4">
|
||||
<div className="rounded-xl border border-white/10 bg-black/20 p-4">
|
||||
<div className="flex flex-wrap items-center justify-between gap-3">
|
||||
<div>
|
||||
<div className="text-sm font-semibold text-white">{t("alerts.policy.eventSelectLabel")}</div>
|
||||
<div className="text-xs text-zinc-400">{t("alerts.policy.eventSelectHelper")}</div>
|
||||
</div>
|
||||
<div className="flex items-center gap-3">
|
||||
<select
|
||||
value={selectedEventType}
|
||||
onChange={(event) => setSelectedEventType(event.target.value)}
|
||||
disabled={!canEdit}
|
||||
className="rounded-lg border border-white/10 bg-black/30 px-3 py-2 text-sm text-white"
|
||||
>
|
||||
{policyDraft.rules.map((rule) => (
|
||||
<option key={rule.eventType} value={rule.eventType}>
|
||||
{t(`alerts.event.${rule.eventType}`)}
|
||||
</option>
|
||||
))}
|
||||
</select>
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => applyDefaultsToEvent(selectedEventType)}
|
||||
disabled={!canEdit || !selectedEventType}
|
||||
className="rounded-lg border border-white/10 bg-white/10 px-3 py-2 text-xs text-white disabled:opacity-40"
|
||||
>
|
||||
{t("alerts.policy.applyDefaults")}
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{policyDraft.rules
|
||||
.filter((rule) => rule.eventType === selectedEventType)
|
||||
.map((rule) => (
|
||||
<div key={rule.eventType} className="rounded-xl border border-white/10 bg-black/20 p-4">
|
||||
<div className="flex items-center justify-between gap-3">
|
||||
<div className="text-sm font-semibold text-white">
|
||||
{t(`alerts.event.${rule.eventType}`)}
|
||||
</div>
|
||||
<label className="text-xs text-zinc-400">
|
||||
{t("alerts.policy.repeatMinutes")}
|
||||
<input
|
||||
type="number"
|
||||
min={0}
|
||||
value={rule.repeatMinutes ?? 0}
|
||||
onChange={(event) =>
|
||||
updateRule(rule.eventType, { repeatMinutes: Number(event.target.value) })
|
||||
}
|
||||
disabled={!canEdit}
|
||||
className="ml-2 w-20 rounded-lg border border-white/10 bg-black/30 px-2 py-1 text-xs text-white"
|
||||
/>
|
||||
</label>
|
||||
</div>
|
||||
|
||||
<div className="mt-3 grid grid-cols-1 gap-3 md:grid-cols-3">
|
||||
{ROLE_ORDER.map((role) => {
|
||||
const roleRule = rule.roles[role];
|
||||
return (
|
||||
<div key={role} className="rounded-xl border border-white/10 bg-black/20 p-3">
|
||||
<div className="text-sm font-semibold text-white">{role}</div>
|
||||
<label className="mt-2 flex items-center gap-2 text-xs text-zinc-400">
|
||||
<input
|
||||
type="checkbox"
|
||||
checked={roleRule.enabled}
|
||||
onChange={(event) =>
|
||||
updateRuleRole(rule.eventType, role, { enabled: event.target.checked })
|
||||
}
|
||||
disabled={!canEdit}
|
||||
className="h-4 w-4 rounded border border-white/20 bg-black/20"
|
||||
/>
|
||||
{t("alerts.policy.enabled")}
|
||||
</label>
|
||||
<label className="mt-3 block text-xs text-zinc-400">
|
||||
{t("alerts.policy.afterMinutes")}
|
||||
<input
|
||||
type="number"
|
||||
min={0}
|
||||
value={roleRule.afterMinutes}
|
||||
onChange={(event) =>
|
||||
updateRuleRole(rule.eventType, role, { afterMinutes: Number(event.target.value) })
|
||||
}
|
||||
disabled={!canEdit}
|
||||
className="mt-2 w-full rounded-lg border border-white/10 bg-black/30 px-3 py-2 text-sm text-white"
|
||||
/>
|
||||
</label>
|
||||
<div className="mt-3 text-xs text-zinc-400">{t("alerts.policy.channels")}</div>
|
||||
<div className="mt-2 flex flex-wrap gap-3">
|
||||
{CHANNELS.map((channel) => (
|
||||
<label key={channel} className="flex items-center gap-2 text-xs text-zinc-400">
|
||||
<input
|
||||
type="checkbox"
|
||||
checked={roleRule.channels.includes(channel)}
|
||||
onChange={(event) => {
|
||||
const next = event.target.checked
|
||||
? [...roleRule.channels, channel]
|
||||
: roleRule.channels.filter((c) => c !== channel);
|
||||
updateRuleRole(rule.eventType, role, { channels: next });
|
||||
}}
|
||||
disabled={!canEdit}
|
||||
className="h-4 w-4 rounded border border-white/20 bg-black/20"
|
||||
/>
|
||||
{channel.toUpperCase()}
|
||||
</label>
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
})}
|
||||
</div>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
|
||||
<div className="rounded-2xl border border-white/10 bg-white/5 p-5">
|
||||
<div className="mb-4 flex items-center justify-between gap-4">
|
||||
<div>
|
||||
<div className="text-sm font-semibold text-white">{t("alerts.contacts.title")}</div>
|
||||
<div className="text-xs text-zinc-400">{t("alerts.contacts.subtitle")}</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{!canEdit && (
|
||||
<div className="mb-4 rounded-xl border border-white/10 bg-black/20 p-3 text-sm text-zinc-300">
|
||||
{t("alerts.contacts.readOnly")}
|
||||
</div>
|
||||
)}
|
||||
|
||||
{contactsError && (
|
||||
<div className="mb-3 rounded-xl border border-red-500/20 bg-red-500/10 p-3 text-sm text-red-200">
|
||||
{contactsError}
|
||||
</div>
|
||||
)}
|
||||
|
||||
<div className="grid grid-cols-1 gap-3 md:grid-cols-2">
|
||||
<label className="rounded-xl border border-white/10 bg-black/20 p-3 text-xs text-zinc-400">
|
||||
{t("alerts.contacts.name")}
|
||||
<input
|
||||
value={newContact.name}
|
||||
onChange={(event) => setNewContact((prev) => ({ ...prev, name: event.target.value }))}
|
||||
disabled={!canEdit}
|
||||
className="mt-2 w-full rounded-lg border border-white/10 bg-black/30 px-3 py-2 text-sm text-white"
|
||||
/>
|
||||
</label>
|
||||
<label className="rounded-xl border border-white/10 bg-black/20 p-3 text-xs text-zinc-400">
|
||||
{t("alerts.contacts.roleScope")}
|
||||
<select
|
||||
value={newContact.roleScope}
|
||||
onChange={(event) => setNewContact((prev) => ({ ...prev, roleScope: event.target.value }))}
|
||||
disabled={!canEdit}
|
||||
className="mt-2 w-full rounded-lg border border-white/10 bg-black/30 px-3 py-2 text-sm text-white"
|
||||
>
|
||||
<option value="CUSTOM">{t("alerts.contacts.role.custom")}</option>
|
||||
<option value="MEMBER">{t("alerts.contacts.role.member")}</option>
|
||||
<option value="ADMIN">{t("alerts.contacts.role.admin")}</option>
|
||||
<option value="OWNER">{t("alerts.contacts.role.owner")}</option>
|
||||
</select>
|
||||
</label>
|
||||
<label className="rounded-xl border border-white/10 bg-black/20 p-3 text-xs text-zinc-400">
|
||||
{t("alerts.contacts.email")}
|
||||
<input
|
||||
value={newContact.email}
|
||||
onChange={(event) => setNewContact((prev) => ({ ...prev, email: event.target.value }))}
|
||||
disabled={!canEdit}
|
||||
className="mt-2 w-full rounded-lg border border-white/10 bg-black/30 px-3 py-2 text-sm text-white"
|
||||
/>
|
||||
</label>
|
||||
<label className="rounded-xl border border-white/10 bg-black/20 p-3 text-xs text-zinc-400">
|
||||
{t("alerts.contacts.phone")}
|
||||
<input
|
||||
value={newContact.phone}
|
||||
onChange={(event) => setNewContact((prev) => ({ ...prev, phone: event.target.value }))}
|
||||
disabled={!canEdit}
|
||||
className="mt-2 w-full rounded-lg border border-white/10 bg-black/30 px-3 py-2 text-sm text-white"
|
||||
/>
|
||||
</label>
|
||||
<label className="rounded-xl border border-white/10 bg-black/20 p-3 text-xs text-zinc-400 md:col-span-2">
|
||||
{t("alerts.contacts.eventTypes")}
|
||||
<div className="mt-2 flex flex-wrap gap-3">
|
||||
{EVENT_TYPES.map((eventType) => (
|
||||
<label key={eventType.value} className="flex items-center gap-2 text-xs text-zinc-400">
|
||||
<input
|
||||
type="checkbox"
|
||||
checked={newContact.eventTypes.includes(eventType.value)}
|
||||
onChange={(event) => {
|
||||
const next = event.target.checked
|
||||
? [...newContact.eventTypes, eventType.value]
|
||||
: newContact.eventTypes.filter((value) => value !== eventType.value);
|
||||
setNewContact((prev) => ({ ...prev, eventTypes: next }));
|
||||
}}
|
||||
disabled={!canEdit}
|
||||
className="h-4 w-4 rounded border border-white/20 bg-black/20"
|
||||
/>
|
||||
{t(eventType.labelKey)}
|
||||
</label>
|
||||
))}
|
||||
</div>
|
||||
<div className="mt-2 text-xs text-zinc-500">{t("alerts.contacts.eventTypesHelper")}</div>
|
||||
</label>
|
||||
</div>
|
||||
|
||||
{createError && (
|
||||
<div className="mt-3 rounded-xl border border-red-500/20 bg-red-500/10 p-3 text-sm text-red-200">
|
||||
{createError}
|
||||
</div>
|
||||
)}
|
||||
|
||||
<div className="mt-3">
|
||||
<button
|
||||
type="button"
|
||||
onClick={createContact}
|
||||
disabled={!canEdit || creatingContact}
|
||||
className="rounded-xl border border-white/10 bg-white/10 px-4 py-2 text-sm text-white disabled:opacity-40"
|
||||
>
|
||||
{creatingContact ? t("alerts.contacts.creating") : t("alerts.contacts.add")}
|
||||
</button>
|
||||
</div>
|
||||
|
||||
<div className="mt-6 space-y-3">
|
||||
{contacts.length === 0 && (
|
||||
<div className="text-sm text-zinc-400">{t("alerts.contacts.empty")}</div>
|
||||
)}
|
||||
{contacts.map((contact) => {
|
||||
const draft = contactEdits[contact.id] ?? normalizeContactDraft(contact);
|
||||
const locked = !!contact.userId;
|
||||
return (
|
||||
<div key={contact.id} className="rounded-xl border border-white/10 bg-black/20 p-3">
|
||||
<div className="grid grid-cols-1 gap-3 md:grid-cols-2">
|
||||
<label className="text-xs text-zinc-400">
|
||||
{t("alerts.contacts.name")}
|
||||
<input
|
||||
value={draft.name}
|
||||
onChange={(event) => updateContactDraft(contact.id, { name: event.target.value })}
|
||||
disabled={!canEdit || locked}
|
||||
className="mt-2 w-full rounded-lg border border-white/10 bg-black/30 px-3 py-2 text-sm text-white disabled:opacity-50"
|
||||
/>
|
||||
</label>
|
||||
<label className="text-xs text-zinc-400">
|
||||
{t("alerts.contacts.roleScope")}
|
||||
<select
|
||||
value={draft.roleScope}
|
||||
onChange={(event) => updateContactDraft(contact.id, { roleScope: event.target.value })}
|
||||
disabled={!canEdit}
|
||||
className="mt-2 w-full rounded-lg border border-white/10 bg-black/30 px-3 py-2 text-sm text-white"
|
||||
>
|
||||
<option value="CUSTOM">{t("alerts.contacts.role.custom")}</option>
|
||||
<option value="MEMBER">{t("alerts.contacts.role.member")}</option>
|
||||
<option value="ADMIN">{t("alerts.contacts.role.admin")}</option>
|
||||
<option value="OWNER">{t("alerts.contacts.role.owner")}</option>
|
||||
</select>
|
||||
</label>
|
||||
<label className="text-xs text-zinc-400">
|
||||
{t("alerts.contacts.email")}
|
||||
<input
|
||||
value={draft.email}
|
||||
onChange={(event) => updateContactDraft(contact.id, { email: event.target.value })}
|
||||
disabled={!canEdit || locked}
|
||||
className="mt-2 w-full rounded-lg border border-white/10 bg-black/30 px-3 py-2 text-sm text-white disabled:opacity-50"
|
||||
/>
|
||||
</label>
|
||||
<label className="text-xs text-zinc-400">
|
||||
{t("alerts.contacts.phone")}
|
||||
<input
|
||||
value={draft.phone}
|
||||
onChange={(event) => updateContactDraft(contact.id, { phone: event.target.value })}
|
||||
disabled={!canEdit || locked}
|
||||
className="mt-2 w-full rounded-lg border border-white/10 bg-black/30 px-3 py-2 text-sm text-white disabled:opacity-50"
|
||||
/>
|
||||
</label>
|
||||
<label className="text-xs text-zinc-400 md:col-span-2">
|
||||
{t("alerts.contacts.eventTypes")}
|
||||
<div className="mt-2 flex flex-wrap gap-3">
|
||||
{EVENT_TYPES.map((eventType) => (
|
||||
<label key={eventType.value} className="flex items-center gap-2 text-xs text-zinc-400">
|
||||
<input
|
||||
type="checkbox"
|
||||
checked={draft.eventTypes.includes(eventType.value)}
|
||||
onChange={(event) => {
|
||||
const next = event.target.checked
|
||||
? [...draft.eventTypes, eventType.value]
|
||||
: draft.eventTypes.filter((value) => value !== eventType.value);
|
||||
updateContactDraft(contact.id, { eventTypes: next });
|
||||
}}
|
||||
disabled={!canEdit}
|
||||
className="h-4 w-4 rounded border border-white/20 bg-black/20"
|
||||
/>
|
||||
{t(eventType.labelKey)}
|
||||
</label>
|
||||
))}
|
||||
</div>
|
||||
<div className="mt-2 text-xs text-zinc-500">{t("alerts.contacts.eventTypesHelper")}</div>
|
||||
</label>
|
||||
<label className="flex items-center gap-2 text-xs text-zinc-400">
|
||||
<input
|
||||
type="checkbox"
|
||||
checked={draft.isActive}
|
||||
onChange={(event) => updateContactDraft(contact.id, { isActive: event.target.checked })}
|
||||
disabled={!canEdit}
|
||||
className="h-4 w-4 rounded border border-white/20 bg-black/20"
|
||||
/>
|
||||
{t("alerts.contacts.active")}
|
||||
</label>
|
||||
</div>
|
||||
<div className="mt-3 flex flex-wrap gap-3">
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => saveContact(contact.id)}
|
||||
disabled={!canEdit || savingContactId === contact.id}
|
||||
className="rounded-xl border border-white/10 bg-white/10 px-4 py-2 text-xs text-white disabled:opacity-40"
|
||||
>
|
||||
{savingContactId === contact.id ? t("alerts.contacts.saving") : t("alerts.contacts.save")}
|
||||
</button>
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => deleteContact(contact.id)}
|
||||
disabled={!canEdit || deletingContactId === contact.id}
|
||||
className="rounded-xl border border-red-500/30 bg-red-500/10 px-4 py-2 text-xs text-red-200 disabled:opacity-40"
|
||||
>
|
||||
{deletingContactId === contact.id ? t("alerts.contacts.deleting") : t("alerts.contacts.delete")}
|
||||
</button>
|
||||
{locked && (
|
||||
<span className="text-xs text-zinc-500">{t("alerts.contacts.linkedUser")}</span>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
})}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
682
components/settings/FinancialCostConfig.tsx
Normal file
682
components/settings/FinancialCostConfig.tsx
Normal file
@@ -0,0 +1,682 @@
|
||||
"use client";
|
||||
|
||||
import { useEffect, useMemo, useState } from "react";
|
||||
import { useI18n } from "@/lib/i18n/useI18n";
|
||||
|
||||
type OrgProfile = {
|
||||
orgId: string;
|
||||
defaultCurrency: string;
|
||||
machineCostPerMin?: number | null;
|
||||
operatorCostPerMin?: number | null;
|
||||
ratedRunningKw?: number | null;
|
||||
idleKw?: number | null;
|
||||
kwhRate?: number | null;
|
||||
energyMultiplier?: number | null;
|
||||
energyCostPerMin?: number | null;
|
||||
scrapCostPerUnit?: number | null;
|
||||
rawMaterialCostPerUnit?: number | null;
|
||||
};
|
||||
|
||||
type LocationOverride = {
|
||||
id: string;
|
||||
location: string;
|
||||
currency?: string | null;
|
||||
machineCostPerMin?: number | null;
|
||||
operatorCostPerMin?: number | null;
|
||||
ratedRunningKw?: number | null;
|
||||
idleKw?: number | null;
|
||||
kwhRate?: number | null;
|
||||
energyMultiplier?: number | null;
|
||||
energyCostPerMin?: number | null;
|
||||
scrapCostPerUnit?: number | null;
|
||||
rawMaterialCostPerUnit?: number | null;
|
||||
};
|
||||
|
||||
type MachineOverride = {
|
||||
id: string;
|
||||
machineId: string;
|
||||
currency?: string | null;
|
||||
machineCostPerMin?: number | null;
|
||||
operatorCostPerMin?: number | null;
|
||||
ratedRunningKw?: number | null;
|
||||
idleKw?: number | null;
|
||||
kwhRate?: number | null;
|
||||
energyMultiplier?: number | null;
|
||||
energyCostPerMin?: number | null;
|
||||
scrapCostPerUnit?: number | null;
|
||||
rawMaterialCostPerUnit?: number | null;
|
||||
};
|
||||
|
||||
type ProductOverride = {
|
||||
id: string;
|
||||
sku: string;
|
||||
currency?: string | null;
|
||||
rawMaterialCostPerUnit?: number | null;
|
||||
};
|
||||
|
||||
type MachineRow = {
|
||||
id: string;
|
||||
name: string;
|
||||
location?: string | null;
|
||||
};
|
||||
|
||||
type CostConfig = {
|
||||
org: OrgProfile | null;
|
||||
locations: LocationOverride[];
|
||||
machines: MachineOverride[];
|
||||
products: ProductOverride[];
|
||||
};
|
||||
|
||||
type OrgForm = {
|
||||
defaultCurrency: string;
|
||||
machineCostPerMin: string;
|
||||
operatorCostPerMin: string;
|
||||
ratedRunningKw: string;
|
||||
idleKw: string;
|
||||
kwhRate: string;
|
||||
energyMultiplier: string;
|
||||
energyCostPerMin: string;
|
||||
scrapCostPerUnit: string;
|
||||
rawMaterialCostPerUnit: string;
|
||||
};
|
||||
|
||||
type OverrideForm = {
|
||||
id: string;
|
||||
location?: string;
|
||||
machineId?: string;
|
||||
currency: string;
|
||||
machineCostPerMin: string;
|
||||
operatorCostPerMin: string;
|
||||
ratedRunningKw: string;
|
||||
idleKw: string;
|
||||
kwhRate: string;
|
||||
energyMultiplier: string;
|
||||
energyCostPerMin: string;
|
||||
scrapCostPerUnit: string;
|
||||
rawMaterialCostPerUnit: string;
|
||||
};
|
||||
|
||||
type ProductForm = {
|
||||
id: string;
|
||||
sku: string;
|
||||
currency: string;
|
||||
rawMaterialCostPerUnit: string;
|
||||
};
|
||||
|
||||
const COST_FIELDS = [
|
||||
{ key: "machineCostPerMin", labelKey: "financial.field.machineCostPerMin" },
|
||||
{ key: "operatorCostPerMin", labelKey: "financial.field.operatorCostPerMin" },
|
||||
{ key: "ratedRunningKw", labelKey: "financial.field.ratedRunningKw" },
|
||||
{ key: "idleKw", labelKey: "financial.field.idleKw" },
|
||||
{ key: "kwhRate", labelKey: "financial.field.kwhRate" },
|
||||
{ key: "energyMultiplier", labelKey: "financial.field.energyMultiplier" },
|
||||
{ key: "energyCostPerMin", labelKey: "financial.field.energyCostPerMin" },
|
||||
{ key: "scrapCostPerUnit", labelKey: "financial.field.scrapCostPerUnit" },
|
||||
{ key: "rawMaterialCostPerUnit", labelKey: "financial.field.rawMaterialCostPerUnit" },
|
||||
] as const;
|
||||
|
||||
type CostFieldKey = (typeof COST_FIELDS)[number]["key"];
|
||||
|
||||
function makeId(prefix: string) {
|
||||
return `${prefix}-${Math.random().toString(36).slice(2, 10)}`;
|
||||
}
|
||||
|
||||
function toFieldValue(value?: number | null) {
|
||||
if (value === null || value === undefined || Number.isNaN(value)) return "";
|
||||
return String(value);
|
||||
}
|
||||
|
||||
function parseNumber(input: string) {
|
||||
const trimmed = input.trim();
|
||||
if (!trimmed) return null;
|
||||
const n = Number(trimmed);
|
||||
return Number.isFinite(n) ? n : null;
|
||||
}
|
||||
|
||||
export function FinancialCostConfig() {
|
||||
const { t } = useI18n();
|
||||
const [role, setRole] = useState<string | null>(null);
|
||||
const [machines, setMachines] = useState<MachineRow[]>([]);
|
||||
const [config, setConfig] = useState<CostConfig | null>(null);
|
||||
const [orgForm, setOrgForm] = useState<OrgForm>({
|
||||
defaultCurrency: "USD",
|
||||
machineCostPerMin: "",
|
||||
operatorCostPerMin: "",
|
||||
ratedRunningKw: "",
|
||||
idleKw: "",
|
||||
kwhRate: "",
|
||||
energyMultiplier: "1",
|
||||
energyCostPerMin: "",
|
||||
scrapCostPerUnit: "",
|
||||
rawMaterialCostPerUnit: "",
|
||||
});
|
||||
const [locationRows, setLocationRows] = useState<OverrideForm[]>([]);
|
||||
const [machineRows, setMachineRows] = useState<OverrideForm[]>([]);
|
||||
const [productRows, setProductRows] = useState<ProductForm[]>([]);
|
||||
const [loading, setLoading] = useState(true);
|
||||
const [saving, setSaving] = useState(false);
|
||||
const [saveStatus, setSaveStatus] = useState<string | null>(null);
|
||||
|
||||
const locations = useMemo(() => {
|
||||
const seen = new Set<string>();
|
||||
for (const m of machines) {
|
||||
if (!m.location) continue;
|
||||
seen.add(m.location);
|
||||
}
|
||||
return Array.from(seen).sort();
|
||||
}, [machines]);
|
||||
|
||||
useEffect(() => {
|
||||
let alive = true;
|
||||
|
||||
async function loadMe() {
|
||||
try {
|
||||
const res = await fetch("/api/me", { cache: "no-store" });
|
||||
const data = await res.json().catch(() => ({}));
|
||||
if (!alive) return;
|
||||
setRole(data?.membership?.role ?? null);
|
||||
} catch {
|
||||
if (alive) setRole(null);
|
||||
}
|
||||
}
|
||||
|
||||
loadMe();
|
||||
return () => {
|
||||
alive = false;
|
||||
};
|
||||
}, []);
|
||||
|
||||
useEffect(() => {
|
||||
let alive = true;
|
||||
|
||||
async function load() {
|
||||
try {
|
||||
const [machinesRes, costsRes] = await Promise.all([
|
||||
fetch("/api/machines", { cache: "no-store" }),
|
||||
fetch("/api/financial/costs", { cache: "no-store" }),
|
||||
]);
|
||||
const machinesJson = await machinesRes.json().catch(() => ({}));
|
||||
const costsJson = await costsRes.json().catch(() => ({}));
|
||||
if (!alive) return;
|
||||
setMachines(machinesJson.machines ?? []);
|
||||
setConfig({
|
||||
org: costsJson.org ?? null,
|
||||
locations: costsJson.locations ?? [],
|
||||
machines: costsJson.machines ?? [],
|
||||
products: costsJson.products ?? [],
|
||||
});
|
||||
} catch {
|
||||
if (!alive) return;
|
||||
} finally {
|
||||
if (alive) setLoading(false);
|
||||
}
|
||||
}
|
||||
|
||||
load();
|
||||
return () => {
|
||||
alive = false;
|
||||
};
|
||||
}, []);
|
||||
|
||||
useEffect(() => {
|
||||
if (!config) return;
|
||||
const org = config.org;
|
||||
setOrgForm({
|
||||
defaultCurrency: org?.defaultCurrency ?? "USD",
|
||||
machineCostPerMin: toFieldValue(org?.machineCostPerMin),
|
||||
operatorCostPerMin: toFieldValue(org?.operatorCostPerMin),
|
||||
ratedRunningKw: toFieldValue(org?.ratedRunningKw),
|
||||
idleKw: toFieldValue(org?.idleKw),
|
||||
kwhRate: toFieldValue(org?.kwhRate),
|
||||
energyMultiplier: toFieldValue(org?.energyMultiplier ?? 1),
|
||||
energyCostPerMin: toFieldValue(org?.energyCostPerMin),
|
||||
scrapCostPerUnit: toFieldValue(org?.scrapCostPerUnit),
|
||||
rawMaterialCostPerUnit: toFieldValue(org?.rawMaterialCostPerUnit),
|
||||
});
|
||||
|
||||
setLocationRows(
|
||||
(config.locations ?? []).map((row) => ({
|
||||
id: row.id ?? makeId("loc"),
|
||||
location: row.location,
|
||||
currency: row.currency ?? "",
|
||||
machineCostPerMin: toFieldValue(row.machineCostPerMin),
|
||||
operatorCostPerMin: toFieldValue(row.operatorCostPerMin),
|
||||
ratedRunningKw: toFieldValue(row.ratedRunningKw),
|
||||
idleKw: toFieldValue(row.idleKw),
|
||||
kwhRate: toFieldValue(row.kwhRate),
|
||||
energyMultiplier: toFieldValue(row.energyMultiplier),
|
||||
energyCostPerMin: toFieldValue(row.energyCostPerMin),
|
||||
scrapCostPerUnit: toFieldValue(row.scrapCostPerUnit),
|
||||
rawMaterialCostPerUnit: toFieldValue(row.rawMaterialCostPerUnit),
|
||||
}))
|
||||
);
|
||||
|
||||
setMachineRows(
|
||||
(config.machines ?? []).map((row) => ({
|
||||
id: row.id ?? makeId("machine"),
|
||||
machineId: row.machineId,
|
||||
currency: row.currency ?? "",
|
||||
machineCostPerMin: toFieldValue(row.machineCostPerMin),
|
||||
operatorCostPerMin: toFieldValue(row.operatorCostPerMin),
|
||||
ratedRunningKw: toFieldValue(row.ratedRunningKw),
|
||||
idleKw: toFieldValue(row.idleKw),
|
||||
kwhRate: toFieldValue(row.kwhRate),
|
||||
energyMultiplier: toFieldValue(row.energyMultiplier),
|
||||
energyCostPerMin: toFieldValue(row.energyCostPerMin),
|
||||
scrapCostPerUnit: toFieldValue(row.scrapCostPerUnit),
|
||||
rawMaterialCostPerUnit: toFieldValue(row.rawMaterialCostPerUnit),
|
||||
}))
|
||||
);
|
||||
|
||||
setProductRows(
|
||||
(config.products ?? []).map((row) => ({
|
||||
id: row.id ?? makeId("product"),
|
||||
sku: row.sku,
|
||||
currency: row.currency ?? "",
|
||||
rawMaterialCostPerUnit: toFieldValue(row.rawMaterialCostPerUnit),
|
||||
}))
|
||||
);
|
||||
}, [config]);
|
||||
|
||||
async function handleSave() {
|
||||
setSaving(true);
|
||||
setSaveStatus(null);
|
||||
|
||||
const orgPayload = {
|
||||
defaultCurrency: orgForm.defaultCurrency.trim() || undefined,
|
||||
machineCostPerMin: parseNumber(orgForm.machineCostPerMin),
|
||||
operatorCostPerMin: parseNumber(orgForm.operatorCostPerMin),
|
||||
ratedRunningKw: parseNumber(orgForm.ratedRunningKw),
|
||||
idleKw: parseNumber(orgForm.idleKw),
|
||||
kwhRate: parseNumber(orgForm.kwhRate),
|
||||
energyMultiplier: parseNumber(orgForm.energyMultiplier),
|
||||
energyCostPerMin: parseNumber(orgForm.energyCostPerMin),
|
||||
scrapCostPerUnit: parseNumber(orgForm.scrapCostPerUnit),
|
||||
rawMaterialCostPerUnit: parseNumber(orgForm.rawMaterialCostPerUnit),
|
||||
};
|
||||
|
||||
const locationPayload = locationRows
|
||||
.filter((row) => row.location)
|
||||
.map((row) => ({
|
||||
location: row.location || "",
|
||||
currency: row.currency.trim() || null,
|
||||
machineCostPerMin: parseNumber(row.machineCostPerMin),
|
||||
operatorCostPerMin: parseNumber(row.operatorCostPerMin),
|
||||
ratedRunningKw: parseNumber(row.ratedRunningKw),
|
||||
idleKw: parseNumber(row.idleKw),
|
||||
kwhRate: parseNumber(row.kwhRate),
|
||||
energyMultiplier: parseNumber(row.energyMultiplier),
|
||||
energyCostPerMin: parseNumber(row.energyCostPerMin),
|
||||
scrapCostPerUnit: parseNumber(row.scrapCostPerUnit),
|
||||
rawMaterialCostPerUnit: parseNumber(row.rawMaterialCostPerUnit),
|
||||
}));
|
||||
|
||||
const machinePayload = machineRows
|
||||
.filter((row) => row.machineId)
|
||||
.map((row) => ({
|
||||
machineId: row.machineId || "",
|
||||
currency: row.currency.trim() || null,
|
||||
machineCostPerMin: parseNumber(row.machineCostPerMin),
|
||||
operatorCostPerMin: parseNumber(row.operatorCostPerMin),
|
||||
ratedRunningKw: parseNumber(row.ratedRunningKw),
|
||||
idleKw: parseNumber(row.idleKw),
|
||||
kwhRate: parseNumber(row.kwhRate),
|
||||
energyMultiplier: parseNumber(row.energyMultiplier),
|
||||
energyCostPerMin: parseNumber(row.energyCostPerMin),
|
||||
scrapCostPerUnit: parseNumber(row.scrapCostPerUnit),
|
||||
rawMaterialCostPerUnit: parseNumber(row.rawMaterialCostPerUnit),
|
||||
}));
|
||||
|
||||
const productPayload = productRows
|
||||
.filter((row) => row.sku)
|
||||
.map((row) => ({
|
||||
sku: row.sku.trim(),
|
||||
currency: row.currency.trim() || null,
|
||||
rawMaterialCostPerUnit: parseNumber(row.rawMaterialCostPerUnit),
|
||||
}));
|
||||
|
||||
try {
|
||||
const res = await fetch("/api/financial/costs", {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({
|
||||
org: orgPayload,
|
||||
locations: locationPayload,
|
||||
machines: machinePayload,
|
||||
products: productPayload,
|
||||
}),
|
||||
});
|
||||
const json = await res.json().catch(() => ({}));
|
||||
if (!res.ok) {
|
||||
setSaveStatus(json?.error ?? t("financial.config.saveFailed"));
|
||||
} else {
|
||||
setConfig({
|
||||
org: json.org ?? null,
|
||||
locations: json.locations ?? [],
|
||||
machines: json.machines ?? [],
|
||||
products: json.products ?? [],
|
||||
});
|
||||
setSaveStatus(t("financial.config.saved"));
|
||||
}
|
||||
} catch {
|
||||
setSaveStatus(t("financial.config.saveFailed"));
|
||||
} finally {
|
||||
setSaving(false);
|
||||
}
|
||||
}
|
||||
|
||||
function updateOrgField(key: CostFieldKey, value: string) {
|
||||
setOrgForm((prev) => ({ ...prev, [key]: value }));
|
||||
}
|
||||
|
||||
function updateLocationRow(id: string, key: keyof OverrideForm, value: string) {
|
||||
setLocationRows((prev) => prev.map((row) => (row.id === id ? { ...row, [key]: value } : row)));
|
||||
}
|
||||
|
||||
function updateMachineRow(id: string, key: keyof OverrideForm, value: string) {
|
||||
setMachineRows((prev) => prev.map((row) => (row.id === id ? { ...row, [key]: value } : row)));
|
||||
}
|
||||
|
||||
function updateProductRow(id: string, key: keyof ProductForm, value: string) {
|
||||
setProductRows((prev) => prev.map((row) => (row.id === id ? { ...row, [key]: value } : row)));
|
||||
}
|
||||
|
||||
function addLocationRow() {
|
||||
setLocationRows((prev) => [
|
||||
...prev,
|
||||
{
|
||||
id: makeId("loc"),
|
||||
location: "",
|
||||
currency: "",
|
||||
machineCostPerMin: "",
|
||||
operatorCostPerMin: "",
|
||||
ratedRunningKw: "",
|
||||
idleKw: "",
|
||||
kwhRate: "",
|
||||
energyMultiplier: "",
|
||||
energyCostPerMin: "",
|
||||
scrapCostPerUnit: "",
|
||||
rawMaterialCostPerUnit: "",
|
||||
},
|
||||
]);
|
||||
}
|
||||
|
||||
function addMachineRow() {
|
||||
setMachineRows((prev) => [
|
||||
...prev,
|
||||
{
|
||||
id: makeId("machine"),
|
||||
machineId: "",
|
||||
currency: "",
|
||||
machineCostPerMin: "",
|
||||
operatorCostPerMin: "",
|
||||
ratedRunningKw: "",
|
||||
idleKw: "",
|
||||
kwhRate: "",
|
||||
energyMultiplier: "",
|
||||
energyCostPerMin: "",
|
||||
scrapCostPerUnit: "",
|
||||
rawMaterialCostPerUnit: "",
|
||||
},
|
||||
]);
|
||||
}
|
||||
|
||||
function addProductRow() {
|
||||
setProductRows((prev) => [
|
||||
...prev,
|
||||
{ id: makeId("product"), sku: "", currency: "", rawMaterialCostPerUnit: "" },
|
||||
]);
|
||||
}
|
||||
|
||||
function applyOrgToAllMachines() {
|
||||
setMachineRows(
|
||||
machines.map((m) => ({
|
||||
id: makeId("machine"),
|
||||
machineId: m.id,
|
||||
currency: orgForm.defaultCurrency,
|
||||
machineCostPerMin: orgForm.machineCostPerMin,
|
||||
operatorCostPerMin: orgForm.operatorCostPerMin,
|
||||
ratedRunningKw: orgForm.ratedRunningKw,
|
||||
idleKw: orgForm.idleKw,
|
||||
kwhRate: orgForm.kwhRate,
|
||||
energyMultiplier: orgForm.energyMultiplier,
|
||||
energyCostPerMin: orgForm.energyCostPerMin,
|
||||
scrapCostPerUnit: orgForm.scrapCostPerUnit,
|
||||
rawMaterialCostPerUnit: orgForm.rawMaterialCostPerUnit,
|
||||
}))
|
||||
);
|
||||
}
|
||||
|
||||
if (role && role !== "OWNER") {
|
||||
return (
|
||||
<div className="rounded-2xl border border-white/10 bg-black/40 p-6 text-zinc-300">
|
||||
{t("financial.config.ownerOnly")}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="rounded-2xl border border-white/10 bg-black/40 p-4 space-y-6">
|
||||
<div className="flex flex-wrap items-center justify-between gap-3">
|
||||
<div>
|
||||
<h2 className="text-lg font-semibold text-white">{t("financial.config.title")}</h2>
|
||||
<p className="text-xs text-zinc-500">{t("financial.config.subtitle")}</p>
|
||||
</div>
|
||||
<div className="flex items-center gap-3">
|
||||
<button
|
||||
type="button"
|
||||
onClick={applyOrgToAllMachines}
|
||||
className="rounded-lg border border-white/10 bg-white/5 px-3 py-2 text-xs text-zinc-200 hover:bg-white/10"
|
||||
>
|
||||
{t("financial.config.applyOrg")}
|
||||
</button>
|
||||
<button
|
||||
type="button"
|
||||
onClick={handleSave}
|
||||
disabled={saving}
|
||||
className="rounded-lg bg-emerald-500/80 px-4 py-2 text-xs font-semibold text-white hover:bg-emerald-500"
|
||||
>
|
||||
{saving ? t("financial.config.saving") : t("financial.config.save")}
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{saveStatus && <div className="text-xs text-zinc-400">{saveStatus}</div>}
|
||||
|
||||
<div className="grid gap-4">
|
||||
<div className="rounded-xl border border-white/10 bg-black/20 p-4">
|
||||
<div className="mb-4 flex items-center gap-4">
|
||||
<div className="text-sm font-semibold text-white">{t("financial.config.orgDefaults")}</div>
|
||||
<div className="flex-1" />
|
||||
<input
|
||||
className="w-28 rounded-lg border border-white/10 bg-black/30 px-3 py-2 text-sm text-zinc-200"
|
||||
value={orgForm.defaultCurrency}
|
||||
onChange={(event) =>
|
||||
setOrgForm((prev) => ({ ...prev, defaultCurrency: event.target.value.toUpperCase() }))
|
||||
}
|
||||
placeholder="USD"
|
||||
/>
|
||||
</div>
|
||||
<div className="grid gap-3 md:grid-cols-3">
|
||||
{COST_FIELDS.map((field) => (
|
||||
<label key={field.key} className="text-xs text-zinc-400">
|
||||
{t(field.labelKey)}
|
||||
<input
|
||||
className="mt-2 w-full rounded-lg border border-white/10 bg-black/30 px-3 py-2 text-sm text-zinc-200"
|
||||
value={orgForm[field.key]}
|
||||
onChange={(event) => updateOrgField(field.key, event.target.value)}
|
||||
/>
|
||||
</label>
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="rounded-xl border border-white/10 bg-black/20 p-4 space-y-4">
|
||||
<div className="flex items-center justify-between">
|
||||
<div className="text-sm font-semibold text-white">{t("financial.config.locationOverrides")}</div>
|
||||
<button
|
||||
type="button"
|
||||
onClick={addLocationRow}
|
||||
className="rounded-lg border border-white/10 bg-white/5 px-3 py-2 text-xs text-zinc-200 hover:bg-white/10"
|
||||
>
|
||||
{t("financial.config.addLocation")}
|
||||
</button>
|
||||
</div>
|
||||
{locationRows.length === 0 && (
|
||||
<div className="text-xs text-zinc-500">{t("financial.config.noneLocation")}</div>
|
||||
)}
|
||||
{locationRows.map((row) => (
|
||||
<div key={row.id} className="rounded-xl border border-white/10 bg-black/30 p-4">
|
||||
<div className="grid gap-3 md:grid-cols-3">
|
||||
<label className="text-xs text-zinc-400">
|
||||
{t("financial.config.location")}
|
||||
<select
|
||||
className="mt-2 w-full rounded-lg border border-white/10 bg-black/30 px-3 py-2 text-sm text-zinc-200"
|
||||
value={row.location ?? ""}
|
||||
onChange={(event) => updateLocationRow(row.id, "location", event.target.value)}
|
||||
>
|
||||
<option value="">{t("financial.config.selectLocation")}</option>
|
||||
{locations.map((loc) => (
|
||||
<option key={loc} value={loc}>
|
||||
{loc}
|
||||
</option>
|
||||
))}
|
||||
</select>
|
||||
</label>
|
||||
<label className="text-xs text-zinc-400">
|
||||
{t("financial.config.currency")}
|
||||
<input
|
||||
className="mt-2 w-full rounded-lg border border-white/10 bg-black/30 px-3 py-2 text-sm text-zinc-200"
|
||||
value={row.currency}
|
||||
onChange={(event) => updateLocationRow(row.id, "currency", event.target.value.toUpperCase())}
|
||||
placeholder="MXN"
|
||||
/>
|
||||
</label>
|
||||
</div>
|
||||
<div className="mt-4 grid gap-3 md:grid-cols-3">
|
||||
{COST_FIELDS.map((field) => (
|
||||
<label key={field.key} className="text-xs text-zinc-400">
|
||||
{t(field.labelKey)}
|
||||
<input
|
||||
className="mt-2 w-full rounded-lg border border-white/10 bg-black/30 px-3 py-2 text-sm text-zinc-200"
|
||||
value={row[field.key]}
|
||||
onChange={(event) => updateLocationRow(row.id, field.key, event.target.value)}
|
||||
/>
|
||||
</label>
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
|
||||
<div className="rounded-xl border border-white/10 bg-black/20 p-4 space-y-4">
|
||||
<div className="flex items-center justify-between">
|
||||
<div className="text-sm font-semibold text-white">{t("financial.config.machineOverrides")}</div>
|
||||
<button
|
||||
type="button"
|
||||
onClick={addMachineRow}
|
||||
className="rounded-lg border border-white/10 bg-white/5 px-3 py-2 text-xs text-zinc-200 hover:bg-white/10"
|
||||
>
|
||||
{t("financial.config.addMachine")}
|
||||
</button>
|
||||
</div>
|
||||
{machineRows.length === 0 && (
|
||||
<div className="text-xs text-zinc-500">{t("financial.config.noneMachine")}</div>
|
||||
)}
|
||||
{machineRows.map((row) => (
|
||||
<div key={row.id} className="rounded-xl border border-white/10 bg-black/30 p-4">
|
||||
<div className="grid gap-3 md:grid-cols-3">
|
||||
<label className="text-xs text-zinc-400">
|
||||
{t("financial.config.machine")}
|
||||
<select
|
||||
className="mt-2 w-full rounded-lg border border-white/10 bg-black/30 px-3 py-2 text-sm text-zinc-200"
|
||||
value={row.machineId ?? ""}
|
||||
onChange={(event) => updateMachineRow(row.id, "machineId", event.target.value)}
|
||||
>
|
||||
<option value="">{t("financial.config.selectMachine")}</option>
|
||||
{machines.map((machine) => (
|
||||
<option key={machine.id} value={machine.id}>
|
||||
{machine.name}
|
||||
</option>
|
||||
))}
|
||||
</select>
|
||||
</label>
|
||||
<label className="text-xs text-zinc-400">
|
||||
{t("financial.config.currency")}
|
||||
<input
|
||||
className="mt-2 w-full rounded-lg border border-white/10 bg-black/30 px-3 py-2 text-sm text-zinc-200"
|
||||
value={row.currency}
|
||||
onChange={(event) => updateMachineRow(row.id, "currency", event.target.value.toUpperCase())}
|
||||
placeholder="MXN"
|
||||
/>
|
||||
</label>
|
||||
</div>
|
||||
<div className="mt-4 grid gap-3 md:grid-cols-3">
|
||||
{COST_FIELDS.map((field) => (
|
||||
<label key={field.key} className="text-xs text-zinc-400">
|
||||
{t(field.labelKey)}
|
||||
<input
|
||||
className="mt-2 w-full rounded-lg border border-white/10 bg-black/30 px-3 py-2 text-sm text-zinc-200"
|
||||
value={row[field.key]}
|
||||
onChange={(event) => updateMachineRow(row.id, field.key, event.target.value)}
|
||||
/>
|
||||
</label>
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
|
||||
<div className="rounded-xl border border-white/10 bg-black/20 p-4 space-y-4">
|
||||
<div className="flex items-center justify-between">
|
||||
<div className="text-sm font-semibold text-white">{t("financial.config.productOverrides")}</div>
|
||||
<button
|
||||
type="button"
|
||||
onClick={addProductRow}
|
||||
className="rounded-lg border border-white/10 bg-white/5 px-3 py-2 text-xs text-zinc-200 hover:bg-white/10"
|
||||
>
|
||||
{t("financial.config.addProduct")}
|
||||
</button>
|
||||
</div>
|
||||
{productRows.length === 0 && (
|
||||
<div className="text-xs text-zinc-500">{t("financial.config.noneProduct")}</div>
|
||||
)}
|
||||
{productRows.map((row) => (
|
||||
<div key={row.id} className="rounded-xl border border-white/10 bg-black/30 p-4">
|
||||
<div className="grid gap-3 md:grid-cols-3">
|
||||
<label className="text-xs text-zinc-400">
|
||||
{t("financial.config.sku")}
|
||||
<input
|
||||
className="mt-2 w-full rounded-lg border border-white/10 bg-black/30 px-3 py-2 text-sm text-zinc-200"
|
||||
value={row.sku}
|
||||
onChange={(event) => updateProductRow(row.id, "sku", event.target.value)}
|
||||
placeholder="SKU-001"
|
||||
/>
|
||||
</label>
|
||||
<label className="text-xs text-zinc-400">
|
||||
{t("financial.config.currency")}
|
||||
<input
|
||||
className="mt-2 w-full rounded-lg border border-white/10 bg-black/30 px-3 py-2 text-sm text-zinc-200"
|
||||
value={row.currency}
|
||||
onChange={(event) => updateProductRow(row.id, "currency", event.target.value.toUpperCase())}
|
||||
placeholder="MXN"
|
||||
/>
|
||||
</label>
|
||||
<label className="text-xs text-zinc-400">
|
||||
{t("financial.config.rawMaterialUnit")}
|
||||
<input
|
||||
className="mt-2 w-full rounded-lg border border-white/10 bg-black/30 px-3 py-2 text-sm text-zinc-200"
|
||||
value={row.rawMaterialCostPerUnit}
|
||||
onChange={(event) => updateProductRow(row.id, "rawMaterialCostPerUnit", event.target.value)}
|
||||
/>
|
||||
</label>
|
||||
</div>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{loading && <div className="text-xs text-zinc-500">{t("financial.config.loading")}</div>}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
74
data_validation_edge_contract.md
Normal file
74
data_validation_edge_contract.md
Normal file
@@ -0,0 +1,74 @@
|
||||
# MIS Edge → Cloud Contract (v1.0)
|
||||
|
||||
All ingest payloads MUST include these top-level meta fields:
|
||||
|
||||
- schemaVersion: "1.0"
|
||||
- machineId: UUID
|
||||
- tsDevice: epoch milliseconds (number)
|
||||
- seq: monotonic integer per machine (persisted across reboots)
|
||||
|
||||
## POST /api/ingest/heartbeat
|
||||
{
|
||||
"schemaVersion": "1.0",
|
||||
"machineId": "uuid",
|
||||
"tsDevice": 1766427568335,
|
||||
"seq": 123,
|
||||
"online": true,
|
||||
"message": "NR heartbeat",
|
||||
"ip": "192.168.18.33",
|
||||
"fwVersion": "raspi-nodered-1.0"
|
||||
}
|
||||
|
||||
## POST /api/ingest/kpi (snapshot)
|
||||
{
|
||||
"schemaVersion": "1.0",
|
||||
"machineId": "uuid",
|
||||
"tsDevice": 1766427568335,
|
||||
"seq": 124,
|
||||
"activeWorkOrder": { "id": "OT-10001", "sku": "YoguFrut", "target": 600000, "good": 312640, "scrap": 0 },
|
||||
"cycle_count": 31264,
|
||||
"good_parts": 312640,
|
||||
"trackingEnabled": true,
|
||||
"productionStarted": true,
|
||||
"cycleTime": 14,
|
||||
"kpis": { "oee": 100, "availability": 100, "performance": 100, "quality": 100 }
|
||||
}
|
||||
|
||||
## POST /api/ingest/cycle
|
||||
{
|
||||
"schemaVersion": "1.0",
|
||||
"machineId": "uuid",
|
||||
"tsDevice": 1766427568335,
|
||||
"seq": 125,
|
||||
"cycle": {
|
||||
"timestamp": 1766427568335,
|
||||
"cycle_count": 31264,
|
||||
"actual_cycle_time": 10.141,
|
||||
"theoretical_cycle_time": 14,
|
||||
"work_order_id": "OT-10001",
|
||||
"sku": "YoguFrut",
|
||||
"cavities": 10,
|
||||
"good_delta": 10,
|
||||
"scrap_total": 0
|
||||
}
|
||||
}
|
||||
|
||||
## POST /api/ingest/event
|
||||
Edge MUST split arrays; cloud expects one event per request.
|
||||
{
|
||||
"schemaVersion": "1.0",
|
||||
"machineId": "uuid",
|
||||
"tsDevice": 1766427568335,
|
||||
"seq": 126,
|
||||
"event": {
|
||||
"anomaly_type": "slow-cycle",
|
||||
"severity": "warning",
|
||||
"title": "Slow Cycle Detected",
|
||||
"description": "Cycle took 23.6s",
|
||||
"timestamp": 1766427568335,
|
||||
"work_order_id": "OT-10001",
|
||||
"cycle_count": 31265,
|
||||
"data": {},
|
||||
"kpi_snapshot": {}
|
||||
}
|
||||
}
|
||||
387
dictionary_en_es.md
Normal file
387
dictionary_en_es.md
Normal file
@@ -0,0 +1,387 @@
|
||||
# EN/ES Dictionary
|
||||
|
||||
This dictionary captures UI copy for Control Tower. EN is the source; ES-MX is the translation.
|
||||
Main KPIs remain English in ES-MX (OEE, KPI, SKU, AVAILABILITY, PERFORMANCE, QUALITY).
|
||||
|
||||
## Common
|
||||
| Key | EN | ES-MX |
|
||||
| --- | --- | --- |
|
||||
| common.loading | Loading... | Cargando... |
|
||||
| common.loadingShort | Loading | Cargando |
|
||||
| common.never | never | nunca |
|
||||
| common.na | -- | -- |
|
||||
| common.back | Back | Volver |
|
||||
| common.cancel | Cancel | Cancelar |
|
||||
| common.close | Close | Cerrar |
|
||||
| common.save | Save | Guardar |
|
||||
| common.copy | Copy | Copiar |
|
||||
|
||||
## Navigation
|
||||
| Key | EN | ES-MX |
|
||||
| --- | --- | --- |
|
||||
| nav.overview | Overview | Resumen |
|
||||
| nav.machines | Machines | Máquinas |
|
||||
| nav.reports | Reports | Reportes |
|
||||
| nav.settings | Settings | Configuración |
|
||||
|
||||
## Sidebar
|
||||
| Key | EN | ES-MX |
|
||||
| --- | --- | --- |
|
||||
| sidebar.productTitle | MIS | MIS |
|
||||
| sidebar.productSubtitle | Control Tower | Control Tower |
|
||||
| sidebar.userFallback | User | Usuario |
|
||||
| sidebar.loadingOrg | Loading... | Cargando... |
|
||||
| sidebar.themeTooltip | Theme and language settings | Tema e idioma |
|
||||
| sidebar.switchToDark | Switch to dark mode | Cambiar a modo oscuro |
|
||||
| sidebar.switchToLight | Switch to light mode | Cambiar a modo claro |
|
||||
| sidebar.logout | Logout | Cerrar sesión |
|
||||
| sidebar.role.member | MEMBER | MIEMBRO |
|
||||
| sidebar.role.admin | ADMIN | ADMIN |
|
||||
| sidebar.role.owner | OWNER | PROPIETARIO |
|
||||
|
||||
## Login
|
||||
| Key | EN | ES-MX |
|
||||
| --- | --- | --- |
|
||||
| login.title | Control Tower | Control Tower |
|
||||
| login.subtitle | Sign in to your organization | Inicia sesión en tu organización |
|
||||
| login.email | Email | Correo electrónico |
|
||||
| login.password | Password | Contraseña |
|
||||
| login.error.default | Login failed | Inicio de sesión fallido |
|
||||
| login.error.network | Network error | Error de red |
|
||||
| login.submit.loading | Signing in... | Iniciando sesión... |
|
||||
| login.submit.default | Login | Iniciar sesión |
|
||||
| login.newHere | New here? | ¿Nuevo aquí? |
|
||||
| login.createAccount | Create an account | Crear cuenta |
|
||||
|
||||
## Signup
|
||||
| Key | EN | ES-MX |
|
||||
| --- | --- | --- |
|
||||
| signup.verify.title | Verify your email | Verifica tu correo |
|
||||
| signup.verify.sent | We sent a verification link to {email}. | Enviamos un enlace de verificación a {email}. |
|
||||
| signup.verify.failed | Verification email failed to send. Please contact support. | No se pudo enviar el correo de verificación. Contacta a soporte. |
|
||||
| signup.verify.notice | Once verified, you can sign in and invite your team. | Después de verificar, puedes iniciar sesión e invitar a tu equipo. |
|
||||
| signup.verify.back | Back to login | Volver al inicio de sesión |
|
||||
| signup.title | Create your Control Tower | Crea tu Control Tower |
|
||||
| signup.subtitle | Set up your organization and invite the team. | Configura tu organización e invita al equipo. |
|
||||
| signup.orgName | Organization name | Nombre de la organización |
|
||||
| signup.yourName | Your name | Tu nombre |
|
||||
| signup.email | Email | Correo electrónico |
|
||||
| signup.password | Password | Contraseña |
|
||||
| signup.error.default | Signup failed | Registro fallido |
|
||||
| signup.error.network | Network error | Error de red |
|
||||
| signup.submit.loading | Creating account... | Creando cuenta... |
|
||||
| signup.submit.default | Create account | Crear cuenta |
|
||||
| signup.alreadyHave | Already have access? | ¿Ya tienes acceso? |
|
||||
| signup.signIn | Sign in | Iniciar sesión |
|
||||
|
||||
## Invite
|
||||
| Key | EN | ES-MX |
|
||||
| --- | --- | --- |
|
||||
| invite.loading | Loading invite... | Cargando invitación... |
|
||||
| invite.notFound | Invite not found. | Invitación no encontrada. |
|
||||
| invite.joinTitle | Join {org} | Únete a {org} |
|
||||
| invite.acceptCopy | Accept the invite for {email} as {role}. | Acepta la invitación para {email} como {role}. |
|
||||
| invite.yourName | Your name | Tu nombre |
|
||||
| invite.password | Password | Contraseña |
|
||||
| invite.error.notFound | Invite not found | Invitación no encontrada |
|
||||
| invite.error.acceptFailed | Invite acceptance failed | No se pudo aceptar la invitación |
|
||||
| invite.submit.loading | Joining... | Uniéndote... |
|
||||
| invite.submit.default | Join organization | Unirse a la organización |
|
||||
|
||||
## Overview
|
||||
| Key | EN | ES-MX |
|
||||
| --- | --- | --- |
|
||||
| overview.title | Overview | Resumen |
|
||||
| overview.subtitle | Fleet pulse, alerts, and top attention items. | Pulso de flota, alertas y elementos prioritarios. |
|
||||
| overview.viewMachines | View Machines | Ver Máquinas |
|
||||
| overview.loading | Loading overview... | Cargando resumen... |
|
||||
| overview.fleetHealth | Fleet Health | Salud de flota |
|
||||
| overview.machinesTotal | Machines total | Máquinas totales |
|
||||
| overview.online | Online | En línea |
|
||||
| overview.offline | Offline | Fuera de línea |
|
||||
| overview.run | Run | En marcha |
|
||||
| overview.idle | Idle | En espera |
|
||||
| overview.stop | Stop | Paro |
|
||||
| overview.productionTotals | Production Totals | Totales de producción |
|
||||
| overview.good | Good | Buenas |
|
||||
| overview.scrap | Scrap | Scrap |
|
||||
| overview.target | Target | Meta |
|
||||
| overview.kpiSumNote | Sum of latest KPIs across machines. | Suma de los últimos KPIs por máquina. |
|
||||
| overview.activityFeed | Activity Feed | Actividad |
|
||||
| overview.eventsRefreshing | Refreshing recent events... | Actualizando eventos recientes... |
|
||||
| overview.eventsLast30 | Last 30 merged events | Últimos 30 eventos combinados |
|
||||
| overview.eventsNone | No recent events. | Sin eventos recientes. |
|
||||
| overview.oeeAvg | OEE (avg) | OEE (avg) |
|
||||
| overview.availabilityAvg | Availability (avg) | Availability (avg) |
|
||||
| overview.performanceAvg | Performance (avg) | Performance (avg) |
|
||||
| overview.qualityAvg | Quality (avg) | Quality (avg) |
|
||||
| overview.attentionList | Attention List | Lista de atención |
|
||||
| overview.shown | shown | mostrados |
|
||||
| overview.noUrgent | No urgent issues detected. | No se detectaron problemas urgentes. |
|
||||
| overview.timeline | Unified Timeline | Línea de tiempo unificada |
|
||||
| overview.items | items | elementos |
|
||||
| overview.noEvents | No events yet. | Sin eventos aún. |
|
||||
| overview.ack | ACK | ACK |
|
||||
| overview.severity.critical | CRITICAL | CRÍTICO |
|
||||
| overview.severity.warning | WARNING | ADVERTENCIA |
|
||||
| overview.severity.info | INFO | INFO |
|
||||
| overview.source.ingested | ingested | ingestado |
|
||||
| overview.source.derived | derived | derivado |
|
||||
| overview.event.macrostop | macrostop | macroparo |
|
||||
| overview.event.microstop | microstop | microparo |
|
||||
| overview.event.slow-cycle | slow-cycle | ciclo lento |
|
||||
| overview.status.offline | OFFLINE | FUERA DE LÍNEA |
|
||||
| overview.status.online | ONLINE | EN LÍNEA |
|
||||
|
||||
## Machines
|
||||
| Key | EN | ES-MX |
|
||||
| --- | --- | --- |
|
||||
| machines.title | Machines | Máquinas |
|
||||
| machines.subtitle | Select a machine to view live KPIs. | Selecciona una máquina para ver KPIs en vivo. |
|
||||
| machines.cancel | Cancel | Cancelar |
|
||||
| machines.addMachine | Add Machine | Agregar máquina |
|
||||
| machines.backOverview | Back to Overview | Volver al Resumen |
|
||||
| machines.addCardTitle | Add a machine | Agregar máquina |
|
||||
| machines.addCardSubtitle | Generate the machine ID and API key for your Node-RED edge. | Genera el ID de máquina y la API key para tu edge Node-RED. |
|
||||
| machines.field.name | Machine Name | Nombre de la máquina |
|
||||
| machines.field.code | Code (optional) | Código (opcional) |
|
||||
| machines.field.location | Location (optional) | Ubicación (opcional) |
|
||||
| machines.create.loading | Creating... | Creando... |
|
||||
| machines.create.default | Create Machine | Crear máquina |
|
||||
| machines.create.error.nameRequired | Machine name is required | El nombre de la máquina es obligatorio |
|
||||
| machines.create.error.failed | Failed to create machine | No se pudo crear la máquina |
|
||||
| machines.pairing.title | Edge pairing code | Código de emparejamiento |
|
||||
| machines.pairing.machine | Machine: | Máquina: |
|
||||
| machines.pairing.codeLabel | Pairing code | Código de emparejamiento |
|
||||
| machines.pairing.expires | Expires | Expira |
|
||||
| machines.pairing.soon | soon | pronto |
|
||||
| machines.pairing.instructions | Enter this code on the Node-RED Control Tower settings screen to link the edge device. | Ingresa este código en la pantalla de configuración de Node-RED Control Tower para vincular el dispositivo. |
|
||||
| machines.pairing.copy | Copy Code | Copiar código |
|
||||
| machines.pairing.copied | Copied | Copiado |
|
||||
| machines.pairing.copyUnsupported | Copy not supported | Copiar no disponible |
|
||||
| machines.pairing.copyFailed | Copy failed | Falló la copia |
|
||||
| machines.loading | Loading machines... | Cargando máquinas... |
|
||||
| machines.empty | No machines found for this org. | No se encontraron máquinas para esta organización. |
|
||||
| machines.status | Status | Estado |
|
||||
| machines.status.noHeartbeat | No heartbeat | Sin heartbeat |
|
||||
| machines.status.ok | OK | OK |
|
||||
| machines.status.offline | OFFLINE | FUERA DE LÍNEA |
|
||||
| machines.status.unknown | UNKNOWN | DESCONOCIDO |
|
||||
| machines.lastSeen | Last seen {time} | Visto hace {time} |
|
||||
|
||||
## Machine Detail
|
||||
| Key | EN | ES-MX |
|
||||
| --- | --- | --- |
|
||||
| machine.detail.titleFallback | Machine | Máquina |
|
||||
| machine.detail.lastSeen | Last seen {time} | Visto hace {time} |
|
||||
| machine.detail.loading | Loading... | Cargando... |
|
||||
| machine.detail.error.failed | Failed to load machine | No se pudo cargar la máquina |
|
||||
| machine.detail.error.network | Network error | Error de red |
|
||||
| machine.detail.back | Back | Volver |
|
||||
| machine.detail.workOrders.upload | Upload Work Orders | Subir ordenes de trabajo |
|
||||
| machine.detail.workOrders.uploading | Uploading... | Subiendo... |
|
||||
| machine.detail.workOrders.uploadParsing | Parsing file... | Leyendo archivo... |
|
||||
| machine.detail.workOrders.uploadHint | CSV or XLSX with Work Order ID, SKU, Theoretical Cycle Time (Seconds), Target Quantity. | CSV o XLSX con Work Order ID, SKU, Theoretical Cycle Time (Seconds), Target Quantity. |
|
||||
| machine.detail.workOrders.uploadSuccess | Uploaded {count} work orders | Se cargaron {count} ordenes de trabajo |
|
||||
| machine.detail.workOrders.uploadError | Upload failed | No se pudo cargar |
|
||||
| machine.detail.workOrders.uploadInvalid | No valid work orders found | No se encontraron ordenes de trabajo validas |
|
||||
| machine.detail.workOrders.uploadUnauthorized | Not authorized to upload work orders | No autorizado para cargar ordenes de trabajo |
|
||||
| machine.detail.status.offline | OFFLINE | FUERA DE LÍNEA |
|
||||
| machine.detail.status.unknown | UNKNOWN | DESCONOCIDO |
|
||||
| machine.detail.status.run | RUN | EN MARCHA |
|
||||
| machine.detail.status.idle | IDLE | EN ESPERA |
|
||||
| machine.detail.status.stop | STOP | PARO |
|
||||
| machine.detail.status.down | DOWN | CAÍDA |
|
||||
| machine.detail.bucket.normal | Normal Cycle | Ciclo normal |
|
||||
| machine.detail.bucket.slow | Slow Cycle | Ciclo lento |
|
||||
| machine.detail.bucket.microstop | Microstop | Microparo |
|
||||
| machine.detail.bucket.macrostop | Macrostop | Macroparo |
|
||||
| machine.detail.bucket.unknown | Unknown | Desconocido |
|
||||
| machine.detail.activity.title | Machine Activity Timeline | Línea de tiempo de actividad |
|
||||
| machine.detail.activity.subtitle | Real-time analysis of production cycles | Análisis en tiempo real de ciclos de producción |
|
||||
| machine.detail.activity.noData | No timeline data yet. | Sin datos de línea de tiempo. |
|
||||
| machine.detail.tooltip.cycle | Cycle: {label} | Ciclo: {label} |
|
||||
| machine.detail.tooltip.duration | Duration | Duración |
|
||||
| machine.detail.tooltip.ideal | Ideal | Ideal |
|
||||
| machine.detail.tooltip.deviation | Deviation | Desviación |
|
||||
| machine.detail.kpi.updated | Updated {time} | Actualizado {time} |
|
||||
| machine.detail.currentWorkOrder | Current Work Order | Orden de trabajo actual |
|
||||
| machine.detail.recentEvents | Recent Events | Eventos recientes |
|
||||
| machine.detail.noEvents | No events yet. | Sin eventos aún. |
|
||||
| machine.detail.cycleTarget | Cycle target | Ciclo objetivo |
|
||||
| machine.detail.mini.events | Detected Events | Eventos detectados |
|
||||
| machine.detail.mini.events.subtitle | Count by type (cycles) | Conteo por tipo (ciclos) |
|
||||
| machine.detail.mini.deviation | Actual vs Standard Cycle | Ciclo real vs estándar |
|
||||
| machine.detail.mini.deviation.subtitle | Average deviation | Desviación promedio |
|
||||
| machine.detail.mini.impact | Production Impact | Impacto en producción |
|
||||
| machine.detail.mini.impact.subtitle | Extra time vs ideal | Tiempo extra vs ideal |
|
||||
| machine.detail.modal.events | Detected Events | Eventos detectados |
|
||||
| machine.detail.modal.deviation | Actual vs Standard Cycle | Ciclo real vs estándar |
|
||||
| machine.detail.modal.impact | Production Impact | Impacto en producción |
|
||||
| machine.detail.modal.standardCycle | Standard cycle (ideal) | Ciclo estándar (ideal) |
|
||||
| machine.detail.modal.avgDeviation | Average deviation | Desviación promedio |
|
||||
| machine.detail.modal.sample | Sample | Muestra |
|
||||
| machine.detail.modal.cycles | cycles | ciclos |
|
||||
| machine.detail.modal.tip | Tip: the faint line is the ideal. Each point is a real cycle. | Tip: la línea tenue es el ideal. Cada punto es un ciclo real. |
|
||||
| machine.detail.modal.totalExtra | Total extra time | Tiempo extra total |
|
||||
| machine.detail.modal.microstops | Microstops | Microparos |
|
||||
| machine.detail.modal.macroStops | Macro stops | Macroparos |
|
||||
| machine.detail.modal.extraTimeLabel | Extra time | Tiempo extra |
|
||||
| machine.detail.modal.extraTimeNote | This is "lost time" vs ideal, distributed by event type. | Esto es "tiempo perdido" vs ideal, distribuido por tipo de evento. |
|
||||
|
||||
## Reports
|
||||
| Key | EN | ES-MX |
|
||||
| --- | --- | --- |
|
||||
| reports.title | Reports | Reportes |
|
||||
| reports.subtitle | Trends, downtime, and quality analytics across machines. | Tendencias, paros y analítica de calidad por máquina. |
|
||||
| reports.exportCsv | Export CSV | Exportar CSV |
|
||||
| reports.exportPdf | Export PDF | Exportar PDF |
|
||||
| reports.filters | Filters | Filtros |
|
||||
| reports.rangeLabel.last24 | Last 24 hours | Últimas 24 horas |
|
||||
| reports.rangeLabel.last7 | Last 7 days | Últimos 7 días |
|
||||
| reports.rangeLabel.last30 | Last 30 days | Últimos 30 días |
|
||||
| reports.rangeLabel.custom | Custom range | Rango personalizado |
|
||||
| reports.filter.range | Range | Rango |
|
||||
| reports.filter.machine | Machine | Máquina |
|
||||
| reports.filter.workOrder | Work Order | Orden de trabajo |
|
||||
| reports.filter.sku | SKU | SKU |
|
||||
| reports.filter.allMachines | All machines | Todas las máquinas |
|
||||
| reports.filter.allWorkOrders | All work orders | Todas las órdenes |
|
||||
| reports.filter.allSkus | All SKUs | Todos los SKUs |
|
||||
| reports.loading | Loading reports... | Cargando reportes... |
|
||||
| reports.error.failed | Failed to load reports | No se pudieron cargar los reportes |
|
||||
| reports.error.network | Network error | Error de red |
|
||||
| reports.kpi.note.withData | Computed from KPI snapshots. | Calculado a partir de KPIs. |
|
||||
| reports.kpi.note.noData | No data in selected range. | Sin datos en el rango seleccionado. |
|
||||
| reports.oeeTrend | OEE Trend | Tendencia de OEE |
|
||||
| reports.downtimePareto | Downtime Pareto | Pareto de paros |
|
||||
| reports.cycleDistribution | Cycle Time Distribution | Distribución de tiempos de ciclo |
|
||||
| reports.scrapTrend | Scrap Trend | Tendencia de scrap |
|
||||
| reports.topLossDrivers | Top Loss Drivers | Principales causas de pérdida |
|
||||
| reports.qualitySummary | Quality Summary | Resumen de calidad |
|
||||
| reports.notes | Notes for Ops | Notas para operaciones |
|
||||
| reports.notes.suggested | Suggested actions | Acciones sugeridas |
|
||||
| reports.notes.none | No insights yet. Generate reports after data collection. | Sin insights todavía. Genera reportes tras recolectar datos. |
|
||||
| reports.noTrend | No trend data yet. | Sin datos de tendencia. |
|
||||
| reports.noDowntime | No downtime data yet. | Sin datos de paros. |
|
||||
| reports.noCycle | No cycle data yet. | Sin datos de ciclo. |
|
||||
| reports.scrapRate | Scrap Rate | Scrap Rate |
|
||||
| reports.topScrapSku | Top Scrap SKU | SKU con más scrap |
|
||||
| reports.topScrapWorkOrder | Top Scrap Work Order | Orden con más scrap |
|
||||
| reports.loss.macrostop | Macrostop | Macroparo |
|
||||
| reports.loss.microstop | Microstop | Microparo |
|
||||
| reports.loss.slowCycle | Slow Cycle | Ciclo lento |
|
||||
| reports.loss.qualitySpike | Quality Spike | Pico de calidad |
|
||||
| reports.loss.oeeDrop | OEE Drop | Caída de OEE |
|
||||
| reports.loss.perfDegradation | Perf Degradation | Baja de desempeño |
|
||||
| reports.tooltip.cycles | Cycles | Ciclos |
|
||||
| reports.tooltip.range | Range | Rango |
|
||||
| reports.tooltip.below | Below | Debajo de |
|
||||
| reports.tooltip.above | Above | Encima de |
|
||||
| reports.tooltip.extremes | Extremes | Extremos |
|
||||
| reports.tooltip.downtime | Downtime | Tiempo de paro |
|
||||
| reports.tooltip.extraTime | Extra time | Tiempo extra |
|
||||
| reports.csv.section | section | sección |
|
||||
| reports.csv.key | key | clave |
|
||||
| reports.csv.value | value | valor |
|
||||
| reports.pdf.title | Report Export | Exportación de reporte |
|
||||
| reports.pdf.range | Range | Rango |
|
||||
| reports.pdf.machine | Machine | Máquina |
|
||||
| reports.pdf.workOrder | Work Order | Orden de trabajo |
|
||||
| reports.pdf.sku | SKU | SKU |
|
||||
| reports.pdf.metric | Metric | Métrica |
|
||||
| reports.pdf.value | Value | Valor |
|
||||
| reports.pdf.topLoss | Top Loss Drivers | Principales causas de pérdida |
|
||||
| reports.pdf.qualitySummary | Quality Summary | Resumen de calidad |
|
||||
| reports.pdf.cycleDistribution | Cycle Time Distribution | Distribución de tiempos de ciclo |
|
||||
| reports.pdf.notes | Notes for Ops | Notas para operaciones |
|
||||
| reports.pdf.none | None | Ninguna |
|
||||
|
||||
## Settings
|
||||
| Key | EN | ES-MX |
|
||||
| --- | --- | --- |
|
||||
| settings.title | Settings | Configuración |
|
||||
| settings.subtitle | Live configuration for shifts, alerts, and defaults. | Configuración en vivo para turnos, alertas y valores predeterminados. |
|
||||
| settings.loading | Loading settings... | Cargando configuración... |
|
||||
| settings.loadingTeam | Loading team... | Cargando equipo... |
|
||||
| settings.refresh | Refresh | Actualizar |
|
||||
| settings.save | Save changes | Guardar cambios |
|
||||
| settings.saving | Saving... | Guardando... |
|
||||
| settings.saved | Settings saved | Configuración guardada |
|
||||
| settings.failedLoad | Failed to load settings | No se pudo cargar la configuración |
|
||||
| settings.failedTeam | Failed to load team | No se pudo cargar el equipo |
|
||||
| settings.failedSave | Failed to save settings | No se pudo guardar la configuración |
|
||||
| settings.unavailable | Settings are unavailable. | La configuración no está disponible. |
|
||||
| settings.conflict | Settings changed elsewhere. Refresh and try again. | La configuración cambió en otro lugar. Actualiza e intenta de nuevo. |
|
||||
| settings.org.title | Organization | Organización |
|
||||
| settings.org.plantName | Plant Name | Nombre de planta |
|
||||
| settings.org.slug | Slug | Slug |
|
||||
| settings.org.timeZone | Time Zone | Zona horaria |
|
||||
| settings.shiftSchedule | Shift Schedule | Turnos |
|
||||
| settings.shiftSubtitle | Define active shifts and downtime compensation. | Define turnos activos y compensación de paros. |
|
||||
| settings.shiftName | Shift name | Nombre del turno |
|
||||
| settings.shiftStart | Start | Inicio |
|
||||
| settings.shiftEnd | End | Fin |
|
||||
| settings.shiftEnabled | Enabled | Activo |
|
||||
| settings.shiftAdd | Add shift | Agregar turno |
|
||||
| settings.shiftRemove | Remove | Eliminar |
|
||||
| settings.shiftComp | Shift change compensation | Compensación por cambio de turno |
|
||||
| settings.lunchBreak | Lunch break | Comida |
|
||||
| settings.minutes | minutes | minutos |
|
||||
| settings.shiftHint | Max 3 shifts, HH:mm | Máx 3 turnos, HH:mm |
|
||||
| settings.shiftTo | to | a |
|
||||
| settings.shiftCompLabel | Shift change compensation (min) | Compensación por cambio de turno (min) |
|
||||
| settings.lunchBreakLabel | Lunch break (min) | Comida (min) |
|
||||
| settings.shift.defaultName | Shift {index} | Turno {index} |
|
||||
| settings.thresholds | Alert thresholds | Umbrales de alertas |
|
||||
| settings.thresholdsSubtitle | Tune production health alerts. | Ajusta alertas de salud de producción. |
|
||||
| settings.thresholds.appliesAll | Applies to all machines | Aplica a todas las máquinas |
|
||||
| settings.thresholds.oee | OEE alert threshold | Umbral de alerta OEE |
|
||||
| settings.thresholds.performance | Performance threshold | Umbral de Performance |
|
||||
| settings.thresholds.qualitySpike | Quality spike delta | Delta de pico de calidad |
|
||||
| settings.thresholds.stoppage | Stoppage multiplier | Multiplicador de paro |
|
||||
| settings.alerts | Alerts | Alertas |
|
||||
| settings.alertsSubtitle | Choose which alerts to notify. | Elige qué alertas notificar. |
|
||||
| settings.thresholds.macroStoppage | Macro stoppage multiplier | Multiplicador de macroparo |
|
||||
| settings.alerts.oeeDrop | OEE drop alerts | Alertas por caída de OEE |
|
||||
| settings.alerts.oeeDropHelper | Notify when OEE falls below threshold | Notificar cuando OEE esté por debajo del umbral |
|
||||
| settings.alerts.performanceDegradation | Performance degradation alerts | Alertas por baja de Performance |
|
||||
| settings.alerts.performanceDegradationHelper | Flag prolonged slow cycles | Marcar ciclos lentos prolongados |
|
||||
| settings.alerts.qualitySpike | Quality spike alerts | Alertas por picos de calidad |
|
||||
| settings.alerts.qualitySpikeHelper | Alert on scrap spikes | Alertar por picos de scrap |
|
||||
| settings.alerts.predictive | Predictive OEE decline alerts | Alertas predictivas de caída OEE |
|
||||
| settings.alerts.predictiveHelper | Warn before OEE drops | Avisar antes de que OEE caiga |
|
||||
| settings.defaults | Mold Defaults | Valores predeterminados de moldes |
|
||||
| settings.defaults.moldTotal | Total molds | Moldes totales |
|
||||
| settings.defaults.moldActive | Active molds | Moldes activos |
|
||||
| settings.updated | Updated | Actualizado |
|
||||
| settings.updatedBy | Updated by | Actualizado por |
|
||||
| settings.team | Team Members | Miembros del equipo |
|
||||
| settings.teamTotal | {count} total | {count} total |
|
||||
| settings.teamNone | No team members yet. | Sin miembros del equipo. |
|
||||
| settings.invites | Invitations | Invitaciones |
|
||||
| settings.inviteEmail | Invite email | Correo de invitación |
|
||||
| settings.inviteRole | Role | Rol |
|
||||
| settings.inviteSend | Create invite | Crear invitación |
|
||||
| settings.inviteSending | Creating... | Creando... |
|
||||
| settings.inviteStatus.copied | Invite link copied | Enlace de invitación copiado |
|
||||
| settings.inviteStatus.emailRequired | Email is required | El correo es obligatorio |
|
||||
| settings.inviteStatus.failed | Failed to revoke invite | No se pudo revocar la invitación |
|
||||
| settings.inviteStatus.sent | Invite email sent | Correo de invitación enviado |
|
||||
| settings.inviteStatus.createFailed | Failed to create invite | No se pudo crear la invitación |
|
||||
| settings.inviteStatus.emailFailed | Invite created, email failed: {url} | Invitación creada, falló el correo: {url} |
|
||||
| settings.inviteNone | No pending invites. | Sin invitaciones pendientes. |
|
||||
| settings.inviteExpires | Expires {date} | Expira {date} |
|
||||
| settings.inviteRole.member | Member | Miembro |
|
||||
| settings.inviteRole.admin | Admin | Admin |
|
||||
| settings.inviteRole.owner | Owner | Propietario |
|
||||
| settings.inviteCopy | Copy link | Copiar enlace |
|
||||
| settings.inviteRevoke | Revoke | Revocar |
|
||||
| settings.role.owner | Owner | Propietario |
|
||||
| settings.role.admin | Admin | Admin |
|
||||
| settings.role.member | Member | Miembro |
|
||||
| settings.role.inactive | Inactive | Inactivo |
|
||||
| settings.integrations | Integrations | Integraciones |
|
||||
| settings.integrations.webhook | Webhook URL | Webhook URL |
|
||||
| settings.integrations.erp | ERP Sync | ERP Sync |
|
||||
| settings.integrations.erpNotConfigured | Not configured | No configurado |
|
||||
414
lib/alerts/engine.ts
Normal file
414
lib/alerts/engine.ts
Normal file
@@ -0,0 +1,414 @@
|
||||
import { prisma } from "@/lib/prisma";
|
||||
import { sendEmail } from "@/lib/email";
|
||||
import { sendSms } from "@/lib/sms";
|
||||
import { AlertPolicySchema, DEFAULT_POLICY } from "@/lib/alerts/policy";
|
||||
|
||||
type Recipient = {
|
||||
userId?: string;
|
||||
contactId?: string;
|
||||
name?: string | null;
|
||||
email?: string | null;
|
||||
phone?: string | null;
|
||||
role: string;
|
||||
};
|
||||
|
||||
function normalizeEventType(value: unknown) {
|
||||
const raw = String(value ?? "").trim().toLowerCase();
|
||||
if (!raw) return raw;
|
||||
const cleaned = raw.replace(/[_\s]+/g, "-").replace(/-+/g, "-");
|
||||
if (cleaned === "micro-stop") return "microstop";
|
||||
if (cleaned === "macro-stop") return "macrostop";
|
||||
if (cleaned === "slowcycle") return "slow-cycle";
|
||||
return cleaned;
|
||||
}
|
||||
|
||||
function asRecord(value: unknown): Record<string, unknown> | null {
|
||||
if (!value || typeof value !== "object" || Array.isArray(value)) return null;
|
||||
return value as Record<string, unknown>;
|
||||
}
|
||||
|
||||
function unwrapEventData(raw: unknown) {
|
||||
const payload = asRecord(raw);
|
||||
const inner = asRecord(payload?.data) ?? payload;
|
||||
return { payload, inner };
|
||||
}
|
||||
|
||||
function readString(value: unknown) {
|
||||
return typeof value === "string" ? value : null;
|
||||
}
|
||||
|
||||
function readNumber(value: unknown) {
|
||||
const n = typeof value === "number" ? value : Number(value);
|
||||
return Number.isFinite(n) ? n : null;
|
||||
}
|
||||
|
||||
function readBool(value: unknown) {
|
||||
return value === true;
|
||||
}
|
||||
|
||||
function extractDurationSec(raw: unknown): number | null {
|
||||
const payload = asRecord(raw);
|
||||
if (!payload) return null;
|
||||
const data = asRecord(payload.data) ?? payload;
|
||||
const candidates = [
|
||||
data?.duration_seconds,
|
||||
data?.duration_sec,
|
||||
data?.stoppage_duration_seconds,
|
||||
data?.stop_duration_seconds,
|
||||
];
|
||||
for (const val of candidates) {
|
||||
if (typeof val === "number" && Number.isFinite(val) && val >= 0) return val;
|
||||
}
|
||||
|
||||
const msCandidates = [data?.duration_ms, data?.durationMs];
|
||||
for (const val of msCandidates) {
|
||||
if (typeof val === "number" && Number.isFinite(val) && val >= 0) {
|
||||
return Math.round(val / 1000);
|
||||
}
|
||||
}
|
||||
|
||||
const startMs = data?.start_ts ?? data?.startTs ?? null;
|
||||
const endMs = data?.end_ts ?? data?.endTs ?? null;
|
||||
if (typeof startMs === "number" && typeof endMs === "number" && endMs >= startMs) {
|
||||
return Math.round((endMs - startMs) / 1000);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
async function ensurePolicy(orgId: string) {
|
||||
const existing = await prisma.alertPolicy.findUnique({
|
||||
where: { orgId },
|
||||
select: { id: true, policyJson: true },
|
||||
});
|
||||
if (existing) {
|
||||
const parsed = AlertPolicySchema.safeParse(existing.policyJson);
|
||||
return parsed.success ? parsed.data : DEFAULT_POLICY;
|
||||
}
|
||||
|
||||
await prisma.alertPolicy.create({
|
||||
data: {
|
||||
orgId,
|
||||
policyJson: DEFAULT_POLICY,
|
||||
},
|
||||
});
|
||||
|
||||
return DEFAULT_POLICY;
|
||||
}
|
||||
|
||||
async function loadRecipients(orgId: string, role: string, eventType: string): Promise<Recipient[]> {
|
||||
const roleUpper = role.toUpperCase();
|
||||
const normalizedEventType = normalizeEventType(eventType);
|
||||
const [members, external] = await Promise.all([
|
||||
prisma.orgUser.findMany({
|
||||
where: { orgId, role: roleUpper },
|
||||
select: {
|
||||
userId: true,
|
||||
user: { select: { name: true, email: true, phone: true, isActive: true } },
|
||||
},
|
||||
}),
|
||||
prisma.alertContact.findMany({
|
||||
where: {
|
||||
orgId,
|
||||
isActive: true,
|
||||
OR: [{ roleScope: roleUpper }, { roleScope: "CUSTOM" }],
|
||||
},
|
||||
select: {
|
||||
id: true,
|
||||
name: true,
|
||||
email: true,
|
||||
phone: true,
|
||||
eventTypes: true,
|
||||
},
|
||||
}),
|
||||
]);
|
||||
|
||||
const memberRecipients = members
|
||||
.filter((m) => m.user?.isActive !== false)
|
||||
.map((m) => ({
|
||||
userId: m.userId,
|
||||
name: m.user?.name ?? null,
|
||||
email: m.user?.email ?? null,
|
||||
phone: m.user?.phone ?? null,
|
||||
role: roleUpper,
|
||||
}));
|
||||
|
||||
const externalRecipients = external
|
||||
.filter((c) => {
|
||||
const types = Array.isArray(c.eventTypes) ? c.eventTypes : null;
|
||||
if (!types || !types.length) return true;
|
||||
return types.some((type) => normalizeEventType(type) === normalizedEventType);
|
||||
})
|
||||
.map((c) => ({
|
||||
contactId: c.id,
|
||||
name: c.name ?? null,
|
||||
email: c.email ?? null,
|
||||
phone: c.phone ?? null,
|
||||
role: roleUpper,
|
||||
}));
|
||||
|
||||
return [...memberRecipients, ...externalRecipients];
|
||||
}
|
||||
|
||||
function buildAlertMessage(params: {
|
||||
machineName: string;
|
||||
machineCode?: string | null;
|
||||
eventType: string;
|
||||
title: string;
|
||||
description?: string | null;
|
||||
durationMin?: number | null;
|
||||
}) {
|
||||
const durationLabel =
|
||||
params.durationMin != null ? `${Math.round(params.durationMin)} min` : "n/a";
|
||||
const subject = `[MIS] ${params.eventType} - ${params.machineName}`;
|
||||
const text = [
|
||||
`Machine: ${params.machineName}${params.machineCode ? ` (${params.machineCode})` : ""}`,
|
||||
`Event: ${params.eventType}`,
|
||||
`Title: ${params.title}`,
|
||||
params.description ? `Description: ${params.description}` : null,
|
||||
`Duration: ${durationLabel}`,
|
||||
]
|
||||
.filter(Boolean)
|
||||
.join("\n");
|
||||
const html = text.replace(/\n/g, "<br/>");
|
||||
return { subject, text, html };
|
||||
}
|
||||
|
||||
async function shouldSendNotification(params: {
|
||||
eventIds: string[];
|
||||
ruleId: string;
|
||||
role: string;
|
||||
channel: string;
|
||||
contactId?: string;
|
||||
userId?: string;
|
||||
repeatMinutes?: number;
|
||||
}) {
|
||||
const existing = await prisma.alertNotification.findFirst({
|
||||
where: {
|
||||
eventId: { in: params.eventIds },
|
||||
ruleId: params.ruleId,
|
||||
role: params.role,
|
||||
channel: params.channel,
|
||||
...(params.contactId ? { contactId: params.contactId } : {}),
|
||||
...(params.userId ? { userId: params.userId } : {}),
|
||||
},
|
||||
orderBy: { sentAt: "desc" },
|
||||
select: { sentAt: true },
|
||||
});
|
||||
|
||||
if (!existing) return true;
|
||||
const repeatMin = Number(params.repeatMinutes ?? 0);
|
||||
if (!repeatMin || repeatMin <= 0) return false;
|
||||
const elapsed = Date.now() - new Date(existing.sentAt).getTime();
|
||||
return elapsed >= repeatMin * 60 * 1000;
|
||||
}
|
||||
|
||||
async function resolveAlertEventIds(orgId: string, alertId: string, fallbackId: string) {
|
||||
const events = await prisma.machineEvent.findMany({
|
||||
where: {
|
||||
orgId,
|
||||
data: {
|
||||
path: ["alert_id"],
|
||||
equals: alertId,
|
||||
},
|
||||
},
|
||||
select: { id: true },
|
||||
});
|
||||
const ids = events.map((row) => row.id);
|
||||
if (!ids.includes(fallbackId)) ids.push(fallbackId);
|
||||
return ids;
|
||||
}
|
||||
|
||||
async function recordNotification(params: {
|
||||
orgId: string;
|
||||
machineId: string;
|
||||
eventId: string;
|
||||
eventType: string;
|
||||
ruleId: string;
|
||||
role: string;
|
||||
channel: string;
|
||||
contactId?: string;
|
||||
userId?: string;
|
||||
status: string;
|
||||
error?: string | null;
|
||||
}) {
|
||||
await prisma.alertNotification.create({
|
||||
data: {
|
||||
orgId: params.orgId,
|
||||
machineId: params.machineId,
|
||||
eventId: params.eventId,
|
||||
eventType: params.eventType,
|
||||
ruleId: params.ruleId,
|
||||
role: params.role,
|
||||
channel: params.channel,
|
||||
contactId: params.contactId ?? null,
|
||||
userId: params.userId ?? null,
|
||||
status: params.status,
|
||||
error: params.error ?? null,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
async function emitFailureEvent(params: {
|
||||
orgId: string;
|
||||
machineId: string;
|
||||
eventType: string;
|
||||
role: string;
|
||||
channel: string;
|
||||
error: string;
|
||||
}) {
|
||||
await prisma.machineEvent.create({
|
||||
data: {
|
||||
orgId: params.orgId,
|
||||
machineId: params.machineId,
|
||||
ts: new Date(),
|
||||
topic: "alert-delivery-failed",
|
||||
eventType: "alert-delivery-failed",
|
||||
severity: "critical",
|
||||
requiresAck: true,
|
||||
title: "Alert delivery failed",
|
||||
description: params.error,
|
||||
data: {
|
||||
sourceEventType: params.eventType,
|
||||
role: params.role,
|
||||
channel: params.channel,
|
||||
error: params.error,
|
||||
},
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
export async function evaluateAlertsForEvent(eventId: string) {
|
||||
const event = await prisma.machineEvent.findUnique({
|
||||
where: { id: eventId },
|
||||
select: {
|
||||
id: true,
|
||||
orgId: true,
|
||||
machineId: true,
|
||||
eventType: true,
|
||||
title: true,
|
||||
description: true,
|
||||
data: true,
|
||||
},
|
||||
});
|
||||
if (!event) return;
|
||||
|
||||
const policy = await ensurePolicy(event.orgId);
|
||||
const eventType = normalizeEventType(event.eventType);
|
||||
const rule = policy.rules.find((r) => normalizeEventType(r.eventType) === eventType);
|
||||
if (!rule) return;
|
||||
|
||||
const { payload, inner } = unwrapEventData(event.data);
|
||||
const alertId = readString(payload?.alert_id ?? inner?.alert_id);
|
||||
const isUpdate = readBool(payload?.is_update ?? inner?.is_update);
|
||||
const isAutoAck = readBool(payload?.is_auto_ack ?? inner?.is_auto_ack);
|
||||
const lastCycleTs = readNumber(payload?.last_cycle_timestamp ?? inner?.last_cycle_timestamp);
|
||||
const theoreticalSec = readNumber(payload?.theoretical_cycle_time ?? inner?.theoretical_cycle_time);
|
||||
if (isAutoAck) return;
|
||||
if (isUpdate && !(rule.repeatMinutes && rule.repeatMinutes > 0)) return;
|
||||
if ((eventType === "microstop" || eventType === "macrostop") && theoreticalSec && lastCycleTs == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
const durationSec = extractDurationSec(event.data);
|
||||
const durationMin = durationSec != null ? durationSec / 60 : 0;
|
||||
const machine = await prisma.machine.findUnique({
|
||||
where: { id: event.machineId },
|
||||
select: { name: true, code: true },
|
||||
});
|
||||
const delivered = new Set<string>();
|
||||
const notificationEventIds = alertId
|
||||
? await resolveAlertEventIds(event.orgId, alertId, event.id)
|
||||
: [event.id];
|
||||
|
||||
for (const [roleName, roleRule] of Object.entries(rule.roles)) {
|
||||
if (!roleRule?.enabled) continue;
|
||||
if (durationMin < Number(roleRule.afterMinutes ?? 0)) continue;
|
||||
|
||||
const recipients = await loadRecipients(event.orgId, roleName, eventType);
|
||||
if (!recipients.length) continue;
|
||||
|
||||
const message = buildAlertMessage({
|
||||
machineName: machine?.name ?? "Unknown Machine",
|
||||
machineCode: machine?.code ?? null,
|
||||
eventType,
|
||||
title: event.title ?? "Alert",
|
||||
description: event.description ?? null,
|
||||
durationMin,
|
||||
});
|
||||
|
||||
for (const recipient of recipients) {
|
||||
for (const channel of roleRule.channels ?? []) {
|
||||
const canSend =
|
||||
channel === "email" ? !!recipient.email : channel === "sms" ? !!recipient.phone : false;
|
||||
if (!canSend) continue;
|
||||
const key = `${channel}:${recipient.userId ?? recipient.contactId ?? recipient.email ?? recipient.phone ?? ""}`;
|
||||
if (delivered.has(key)) continue;
|
||||
|
||||
const allowed = await shouldSendNotification({
|
||||
eventIds: notificationEventIds,
|
||||
ruleId: rule.id,
|
||||
role: roleName,
|
||||
channel,
|
||||
contactId: recipient.contactId,
|
||||
userId: recipient.userId,
|
||||
repeatMinutes: rule.repeatMinutes,
|
||||
});
|
||||
if (!allowed) continue;
|
||||
|
||||
try {
|
||||
if (channel === "email") {
|
||||
await sendEmail({
|
||||
to: recipient.email as string,
|
||||
subject: message.subject,
|
||||
text: message.text,
|
||||
html: message.html,
|
||||
});
|
||||
} else if (channel === "sms") {
|
||||
await sendSms({
|
||||
to: recipient.phone as string,
|
||||
body: message.text,
|
||||
});
|
||||
}
|
||||
|
||||
await recordNotification({
|
||||
orgId: event.orgId,
|
||||
machineId: event.machineId,
|
||||
eventId: event.id,
|
||||
eventType,
|
||||
ruleId: rule.id,
|
||||
role: roleName,
|
||||
channel,
|
||||
contactId: recipient.contactId,
|
||||
userId: recipient.userId,
|
||||
status: "sent",
|
||||
});
|
||||
delivered.add(key);
|
||||
} catch (err: unknown) {
|
||||
const msg = err instanceof Error ? err.message : "notification_failed";
|
||||
await recordNotification({
|
||||
orgId: event.orgId,
|
||||
machineId: event.machineId,
|
||||
eventId: event.id,
|
||||
eventType,
|
||||
ruleId: rule.id,
|
||||
role: roleName,
|
||||
channel,
|
||||
contactId: recipient.contactId,
|
||||
userId: recipient.userId,
|
||||
status: "failed",
|
||||
error: msg,
|
||||
});
|
||||
await emitFailureEvent({
|
||||
orgId: event.orgId,
|
||||
machineId: event.machineId,
|
||||
eventType,
|
||||
role: roleName,
|
||||
channel,
|
||||
error: msg,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
261
lib/alerts/getAlertsInboxData.ts
Normal file
261
lib/alerts/getAlertsInboxData.ts
Normal file
@@ -0,0 +1,261 @@
|
||||
import { prisma } from "@/lib/prisma";
|
||||
|
||||
const RANGE_MS: Record<string, number> = {
|
||||
"24h": 24 * 60 * 60 * 1000,
|
||||
"7d": 7 * 24 * 60 * 60 * 1000,
|
||||
"30d": 30 * 24 * 60 * 60 * 1000,
|
||||
};
|
||||
|
||||
type AlertsInboxParams = {
|
||||
orgId: string;
|
||||
range?: string;
|
||||
start?: Date | null;
|
||||
end?: Date | null;
|
||||
machineId?: string;
|
||||
location?: string;
|
||||
eventType?: string;
|
||||
severity?: string;
|
||||
status?: string;
|
||||
shift?: string;
|
||||
includeUpdates?: boolean;
|
||||
limit?: number;
|
||||
};
|
||||
|
||||
function pickRange(range: string, start?: Date | null, end?: Date | null) {
|
||||
const now = new Date();
|
||||
if (range === "custom") {
|
||||
const startFallback = new Date(now.getTime() - RANGE_MS["24h"]);
|
||||
return {
|
||||
range,
|
||||
start: start ?? startFallback,
|
||||
end: end ?? now,
|
||||
};
|
||||
}
|
||||
const ms = RANGE_MS[range] ?? RANGE_MS["24h"];
|
||||
return { range, start: new Date(now.getTime() - ms), end: now };
|
||||
}
|
||||
|
||||
function safeString(value: unknown) {
|
||||
if (typeof value !== "string") return null;
|
||||
const trimmed = value.trim();
|
||||
return trimmed ? trimmed : null;
|
||||
}
|
||||
|
||||
function safeNumber(value: unknown) {
|
||||
const n = typeof value === "number" ? value : Number(value);
|
||||
return Number.isFinite(n) ? n : null;
|
||||
}
|
||||
|
||||
function safeBool(value: unknown) {
|
||||
return value === true;
|
||||
}
|
||||
|
||||
function parsePayload(raw: unknown) {
|
||||
let parsed: unknown = raw;
|
||||
if (typeof raw === "string") {
|
||||
try {
|
||||
parsed = JSON.parse(raw);
|
||||
} catch {
|
||||
parsed = raw;
|
||||
}
|
||||
}
|
||||
const payload =
|
||||
parsed && typeof parsed === "object" && !Array.isArray(parsed)
|
||||
? (parsed as Record<string, unknown>)
|
||||
: {};
|
||||
const innerCandidate = payload.data;
|
||||
const inner =
|
||||
innerCandidate && typeof innerCandidate === "object" && !Array.isArray(innerCandidate)
|
||||
? (innerCandidate as Record<string, unknown>)
|
||||
: payload;
|
||||
return { payload, inner };
|
||||
}
|
||||
|
||||
function extractDurationSec(raw: unknown) {
|
||||
const { payload, inner } = parsePayload(raw);
|
||||
const candidates = [
|
||||
inner?.duration_seconds,
|
||||
inner?.duration_sec,
|
||||
inner?.stoppage_duration_seconds,
|
||||
inner?.stop_duration_seconds,
|
||||
payload?.duration_seconds,
|
||||
payload?.duration_sec,
|
||||
payload?.stoppage_duration_seconds,
|
||||
payload?.stop_duration_seconds,
|
||||
];
|
||||
for (const val of candidates) {
|
||||
if (typeof val === "number" && Number.isFinite(val) && val >= 0) return val;
|
||||
}
|
||||
const msCandidates = [inner?.duration_ms, inner?.durationMs, payload?.duration_ms, payload?.durationMs];
|
||||
for (const val of msCandidates) {
|
||||
if (typeof val === "number" && Number.isFinite(val) && val >= 0) {
|
||||
return Math.round(val / 1000);
|
||||
}
|
||||
}
|
||||
|
||||
const startMs = inner.start_ts ?? inner.startTs ?? payload.start_ts ?? payload.startTs ?? null;
|
||||
const endMs = inner.end_ts ?? inner.endTs ?? payload.end_ts ?? payload.endTs ?? null;
|
||||
if (typeof startMs === "number" && typeof endMs === "number" && endMs >= startMs) {
|
||||
return Math.round((endMs - startMs) / 1000);
|
||||
}
|
||||
|
||||
const actual = safeNumber(inner.actual_cycle_time ?? payload.actual_cycle_time);
|
||||
const theoretical = safeNumber(inner.theoretical_cycle_time ?? payload.theoretical_cycle_time);
|
||||
if (actual != null && theoretical != null) {
|
||||
return Math.max(0, actual - theoretical);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
function parseTimeMinutes(value?: string | null) {
|
||||
if (!value || !/^\d{2}:\d{2}$/.test(value)) return null;
|
||||
const [hh, mm] = value.split(":").map((n) => Number(n));
|
||||
if (!Number.isFinite(hh) || !Number.isFinite(mm)) return null;
|
||||
return hh * 60 + mm;
|
||||
}
|
||||
|
||||
function getLocalMinutes(ts: Date, timeZone: string) {
|
||||
try {
|
||||
const parts = new Intl.DateTimeFormat("en-US", {
|
||||
timeZone,
|
||||
hour: "2-digit",
|
||||
minute: "2-digit",
|
||||
hourCycle: "h23",
|
||||
}).formatToParts(ts);
|
||||
const hours = Number(parts.find((p) => p.type === "hour")?.value ?? "0");
|
||||
const minutes = Number(parts.find((p) => p.type === "minute")?.value ?? "0");
|
||||
return hours * 60 + minutes;
|
||||
} catch {
|
||||
return ts.getUTCHours() * 60 + ts.getUTCMinutes();
|
||||
}
|
||||
}
|
||||
|
||||
function resolveShift(
|
||||
shifts: Array<{ name: string; startTime: string; endTime: string; enabled?: boolean }>,
|
||||
ts: Date,
|
||||
timeZone: string
|
||||
) {
|
||||
if (!shifts.length) return null;
|
||||
const nowMin = getLocalMinutes(ts, timeZone);
|
||||
for (const shift of shifts) {
|
||||
if (shift.enabled === false) continue;
|
||||
const start = parseTimeMinutes(shift.startTime);
|
||||
const end = parseTimeMinutes(shift.endTime);
|
||||
if (start == null || end == null) continue;
|
||||
if (start <= end) {
|
||||
if (nowMin >= start && nowMin < end) return shift.name;
|
||||
} else {
|
||||
if (nowMin >= start || nowMin < end) return shift.name;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
export async function getAlertsInboxData(params: AlertsInboxParams) {
|
||||
const {
|
||||
orgId,
|
||||
range = "24h",
|
||||
start,
|
||||
end,
|
||||
machineId,
|
||||
location,
|
||||
eventType,
|
||||
severity,
|
||||
status,
|
||||
shift,
|
||||
includeUpdates = false,
|
||||
limit = 200,
|
||||
} = params;
|
||||
|
||||
const picked = pickRange(range, start, end);
|
||||
const normalizedStatus = safeString(status)?.toLowerCase();
|
||||
const normalizedShift = safeString(shift);
|
||||
const safeLimit = Number.isFinite(limit) ? Math.min(Math.max(limit, 1), 500) : 200;
|
||||
|
||||
const where = {
|
||||
orgId,
|
||||
ts: { gte: picked.start, lte: picked.end },
|
||||
...(machineId ? { machineId } : {}),
|
||||
...(eventType ? { eventType } : {}),
|
||||
...(severity ? { severity } : {}),
|
||||
...(location ? { machine: { location } } : {}),
|
||||
};
|
||||
|
||||
const [events, shifts, settings] = await Promise.all([
|
||||
prisma.machineEvent.findMany({
|
||||
where,
|
||||
orderBy: { ts: "desc" },
|
||||
take: safeLimit,
|
||||
select: {
|
||||
id: true,
|
||||
ts: true,
|
||||
eventType: true,
|
||||
severity: true,
|
||||
title: true,
|
||||
description: true,
|
||||
data: true,
|
||||
machineId: true,
|
||||
workOrderId: true,
|
||||
sku: true,
|
||||
machine: {
|
||||
select: {
|
||||
name: true,
|
||||
location: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
}),
|
||||
prisma.orgShift.findMany({
|
||||
where: { orgId },
|
||||
orderBy: { sortOrder: "asc" },
|
||||
select: { name: true, startTime: true, endTime: true, enabled: true },
|
||||
}),
|
||||
prisma.orgSettings.findUnique({
|
||||
where: { orgId },
|
||||
select: { timezone: true },
|
||||
}),
|
||||
]);
|
||||
|
||||
const timeZone = settings?.timezone || "UTC";
|
||||
const mapped = [];
|
||||
|
||||
for (const ev of events) {
|
||||
const { payload, inner } = parsePayload(ev.data);
|
||||
const rawStatus = safeString(payload?.status ?? inner?.status);
|
||||
const isUpdate = safeBool(payload?.is_update ?? inner?.is_update);
|
||||
const isAutoAck = safeBool(payload?.is_auto_ack ?? inner?.is_auto_ack);
|
||||
if (!includeUpdates && (isUpdate || isAutoAck)) continue;
|
||||
|
||||
const shiftName = resolveShift(shifts, ev.ts, timeZone);
|
||||
if (normalizedShift && shiftName !== normalizedShift) continue;
|
||||
|
||||
const statusLabel = rawStatus ? rawStatus.toLowerCase() : "unknown";
|
||||
if (normalizedStatus && statusLabel !== normalizedStatus) continue;
|
||||
|
||||
mapped.push({
|
||||
id: ev.id,
|
||||
ts: ev.ts,
|
||||
eventType: ev.eventType,
|
||||
severity: ev.severity,
|
||||
title: ev.title,
|
||||
description: ev.description,
|
||||
machineId: ev.machineId,
|
||||
machineName: ev.machine?.name ?? null,
|
||||
location: ev.machine?.location ?? null,
|
||||
workOrderId: ev.workOrderId ?? null,
|
||||
sku: ev.sku ?? null,
|
||||
durationSec: extractDurationSec(ev.data),
|
||||
status: statusLabel,
|
||||
shift: shiftName,
|
||||
alertId: safeString(payload?.alert_id ?? inner?.alert_id),
|
||||
isUpdate,
|
||||
isAutoAck,
|
||||
});
|
||||
}
|
||||
|
||||
return {
|
||||
range: { range: picked.range, start: picked.start, end: picked.end },
|
||||
events: mapped,
|
||||
};
|
||||
}
|
||||
59
lib/alerts/policy.ts
Normal file
59
lib/alerts/policy.ts
Normal file
@@ -0,0 +1,59 @@
|
||||
import { z } from "zod";
|
||||
|
||||
const ROLE_NAMES = ["MEMBER", "ADMIN", "OWNER"] as const;
|
||||
const CHANNELS = ["email", "sms"] as const;
|
||||
const EVENT_TYPES = ["macrostop", "microstop", "slow-cycle", "offline", "error"] as const;
|
||||
|
||||
const RoleRule = z.object({
|
||||
enabled: z.boolean(),
|
||||
afterMinutes: z.number().int().min(0),
|
||||
channels: z.array(z.enum(CHANNELS)).default(["email"]),
|
||||
});
|
||||
|
||||
const Rule = z.object({
|
||||
id: z.string(),
|
||||
eventType: z.enum(EVENT_TYPES),
|
||||
roles: z.record(z.enum(ROLE_NAMES), RoleRule),
|
||||
repeatMinutes: z.number().int().min(0).optional(),
|
||||
});
|
||||
|
||||
export const AlertPolicySchema = z.object({
|
||||
version: z.number().int().min(1).default(1),
|
||||
defaults: z.record(z.enum(ROLE_NAMES), RoleRule),
|
||||
rules: z.array(Rule),
|
||||
});
|
||||
|
||||
export type AlertPolicy = z.infer<typeof AlertPolicySchema>;
|
||||
|
||||
export const DEFAULT_POLICY: AlertPolicy = {
|
||||
version: 1,
|
||||
defaults: {
|
||||
MEMBER: { enabled: true, afterMinutes: 0, channels: ["email"] },
|
||||
ADMIN: { enabled: true, afterMinutes: 10, channels: ["email", "sms"] },
|
||||
OWNER: { enabled: true, afterMinutes: 30, channels: ["sms"] },
|
||||
},
|
||||
rules: EVENT_TYPES.map((eventType) => ({
|
||||
id: eventType,
|
||||
eventType,
|
||||
roles: {
|
||||
MEMBER: { enabled: true, afterMinutes: 0, channels: ["email"] },
|
||||
ADMIN: { enabled: true, afterMinutes: 10, channels: ["email", "sms"] },
|
||||
OWNER: { enabled: true, afterMinutes: 30, channels: ["sms"] },
|
||||
},
|
||||
repeatMinutes: 15,
|
||||
})),
|
||||
};
|
||||
|
||||
export function normalizeAlertPolicy(raw: unknown): AlertPolicy {
|
||||
const parsed = AlertPolicySchema.safeParse(raw);
|
||||
if (parsed.success) return parsed.data;
|
||||
return DEFAULT_POLICY;
|
||||
}
|
||||
|
||||
export function isRoleName(value: string) {
|
||||
return ROLE_NAMES.includes(value as (typeof ROLE_NAMES)[number]);
|
||||
}
|
||||
|
||||
export function isChannel(value: string) {
|
||||
return CHANNELS.includes(value as (typeof CHANNELS)[number]);
|
||||
}
|
||||
24
lib/analytics/downtimeRange.ts
Normal file
24
lib/analytics/downtimeRange.ts
Normal file
@@ -0,0 +1,24 @@
|
||||
export const DOWNTIME_RANGES = ["24h", "7d", "30d", "mtd"] as const;
|
||||
export type DowntimeRange = (typeof DOWNTIME_RANGES)[number];
|
||||
|
||||
export function coerceDowntimeRange(v?: string | null): DowntimeRange {
|
||||
const s = (v ?? "").toLowerCase();
|
||||
return (DOWNTIME_RANGES as readonly string[]).includes(s) ? (s as DowntimeRange) : "7d";
|
||||
}
|
||||
|
||||
// server-friendly helper
|
||||
export function rangeToStart(range: DowntimeRange) {
|
||||
const now = new Date();
|
||||
if (range === "24h") return new Date(Date.now() - 24 * 60 * 60 * 1000);
|
||||
if (range === "30d") return new Date(Date.now() - 30 * 24 * 60 * 60 * 1000);
|
||||
if (range === "mtd") return new Date(now.getFullYear(), now.getMonth(), 1);
|
||||
return new Date(Date.now() - 7 * 24 * 60 * 60 * 1000);
|
||||
}
|
||||
|
||||
// UI label helper (replaces ternaries everywhere)
|
||||
export const DOWNTIME_RANGE_LABEL: Record<DowntimeRange, string> = {
|
||||
"24h": "Last 24h",
|
||||
"7d": "Last 7d",
|
||||
"30d": "Last 30d",
|
||||
"mtd": "MTD",
|
||||
};
|
||||
14
lib/appUrl.ts
Normal file
14
lib/appUrl.ts
Normal file
@@ -0,0 +1,14 @@
|
||||
export function getBaseUrl(req?: Request) {
|
||||
const envUrl = process.env.APP_BASE_URL || process.env.NEXT_PUBLIC_APP_URL;
|
||||
if (envUrl) return String(envUrl).replace(/\/+$/, "");
|
||||
if (!req) return "http://localhost:3000";
|
||||
|
||||
const forwardedProto = req.headers.get("x-forwarded-proto");
|
||||
const proto = forwardedProto ? forwardedProto.split(",")[0].trim() : new URL(req.url).protocol.replace(":", "");
|
||||
const host =
|
||||
req.headers.get("x-forwarded-host") ||
|
||||
req.headers.get("host") ||
|
||||
new URL(req.url).host;
|
||||
|
||||
return `${proto}://${host}`;
|
||||
}
|
||||
@@ -6,7 +6,7 @@ const COOKIE_NAME = "mis_session";
|
||||
export async function requireSession() {
|
||||
const jar = await cookies();
|
||||
const sessionId = jar.get(COOKIE_NAME)?.value;
|
||||
if (!sessionId) throw new Error("UNAUTHORIZED");
|
||||
if (!sessionId) return null;
|
||||
|
||||
const session = await prisma.session.findFirst({
|
||||
where: {
|
||||
@@ -14,9 +14,21 @@ export async function requireSession() {
|
||||
revokedAt: null,
|
||||
expiresAt: { gt: new Date() },
|
||||
},
|
||||
include: {
|
||||
user: {
|
||||
select: { isActive: true, emailVerifiedAt: true },
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
if (!session) throw new Error("UNAUTHORIZED");
|
||||
if (!session) return null;
|
||||
|
||||
if (!session.user?.isActive || !session.user?.emailVerifiedAt) {
|
||||
await prisma.session
|
||||
.update({ where: { id: session.id }, data: { revokedAt: new Date() } })
|
||||
.catch(() => {});
|
||||
return null;
|
||||
}
|
||||
|
||||
// Optional: update lastSeenAt (useful later)
|
||||
await prisma.session
|
||||
|
||||
20
lib/auth/sessionCookie.ts
Normal file
20
lib/auth/sessionCookie.ts
Normal file
@@ -0,0 +1,20 @@
|
||||
export const COOKIE_NAME = "mis_session";
|
||||
export const SESSION_DAYS = 7;
|
||||
|
||||
export function isSecureRequest(req: Request) {
|
||||
const forwardedProto = req.headers.get("x-forwarded-proto");
|
||||
if (forwardedProto) {
|
||||
return forwardedProto.split(",")[0].trim() === "https";
|
||||
}
|
||||
return new URL(req.url).protocol === "https:";
|
||||
}
|
||||
|
||||
export function buildSessionCookieOptions(req: Request) {
|
||||
return {
|
||||
httpOnly: true,
|
||||
sameSite: "lax" as const,
|
||||
secure: isSecureRequest(req),
|
||||
path: "/",
|
||||
maxAge: SESSION_DAYS * 24 * 60 * 60,
|
||||
};
|
||||
}
|
||||
381
lib/contracts/v1.ts
Normal file
381
lib/contracts/v1.ts
Normal file
@@ -0,0 +1,381 @@
|
||||
// /home/mdares/mis-control-tower/lib/contracts/v1.ts
|
||||
import { z } from "zod";
|
||||
|
||||
/**
|
||||
* Phase 0: freeze schema version string now and never change it without bumping.
|
||||
* If you later create v2, make a new file or new constant.
|
||||
*/
|
||||
export const SCHEMA_VERSION = "1.0";
|
||||
|
||||
// KPI scale is frozen as 0..100 (you confirmed)
|
||||
const KPI_0_100 = z.number().min(0).max(100);
|
||||
|
||||
function asRecord(value: unknown): Record<string, unknown> | null {
|
||||
if (!value || typeof value !== "object" || Array.isArray(value)) return null;
|
||||
return value as Record<string, unknown>;
|
||||
}
|
||||
|
||||
function unwrapCanonicalEnvelope(raw: unknown) {
|
||||
if (!raw || typeof raw !== "object") return raw;
|
||||
const obj = asRecord(raw);
|
||||
if (!obj) return raw;
|
||||
const payload = asRecord(obj.payload);
|
||||
if (!payload) return raw;
|
||||
|
||||
const hasMeta =
|
||||
obj.schemaVersion !== undefined ||
|
||||
obj.machineId !== undefined ||
|
||||
obj.tsMs !== undefined ||
|
||||
obj.tsDevice !== undefined ||
|
||||
obj.seq !== undefined ||
|
||||
obj.type !== undefined;
|
||||
if (!hasMeta) return raw;
|
||||
|
||||
const tsDevice =
|
||||
typeof obj.tsDevice === "number"
|
||||
? obj.tsDevice
|
||||
: typeof obj.tsMs === "number"
|
||||
? obj.tsMs
|
||||
: typeof payload.tsDevice === "number"
|
||||
? payload.tsDevice
|
||||
: typeof payload.tsMs === "number"
|
||||
? payload.tsMs
|
||||
: undefined;
|
||||
|
||||
return {
|
||||
...payload,
|
||||
schemaVersion: obj.schemaVersion ?? payload.schemaVersion,
|
||||
machineId: obj.machineId ?? payload.machineId,
|
||||
tsDevice: tsDevice ?? payload.tsDevice,
|
||||
seq: obj.seq ?? payload.seq,
|
||||
};
|
||||
}
|
||||
|
||||
function normalizeTsDevice(raw: unknown) {
|
||||
if (!raw || typeof raw !== "object") return raw;
|
||||
const obj = asRecord(raw);
|
||||
if (!obj) return raw;
|
||||
if (typeof obj.tsDevice === "number") return obj;
|
||||
if (typeof obj.tsMs === "number") return { ...obj, tsDevice: obj.tsMs };
|
||||
return obj;
|
||||
}
|
||||
|
||||
function preprocessPayload(raw: unknown) {
|
||||
return normalizeTsDevice(unwrapCanonicalEnvelope(raw));
|
||||
}
|
||||
|
||||
export const SnapshotV1 = z
|
||||
.object({
|
||||
schemaVersion: z.literal(SCHEMA_VERSION),
|
||||
machineId: z.string().uuid(),
|
||||
tsDevice: z.number().int().nonnegative(), // epoch ms
|
||||
// IMPORTANT: seq should be sent as string if it can ever exceed JS safe int
|
||||
seq: z.union([z.number().int().nonnegative(), z.string().regex(/^\d+$/)]),
|
||||
|
||||
// current shape (keep it flat so Node-RED changes are minimal)
|
||||
activeWorkOrder: z
|
||||
.object({
|
||||
id: z.string(),
|
||||
sku: z.string().optional(),
|
||||
target: z.number().optional(),
|
||||
good: z.number().optional(),
|
||||
scrap: z.number().optional(),
|
||||
|
||||
// add the ones you actually rely on
|
||||
cycleTime: z.number().optional(),
|
||||
cavities: z.number().optional(),
|
||||
progressPercent: z.number().optional(),
|
||||
status: z.string().optional(),
|
||||
lastUpdateIso: z.string().optional(),
|
||||
cycle_count: z.number().optional(),
|
||||
good_parts: z.number().optional(),
|
||||
scrap_parts: z.number().optional(),
|
||||
})
|
||||
.partial()
|
||||
.passthrough()
|
||||
.optional(),
|
||||
|
||||
cycle_count: z.number().int().nonnegative().optional(),
|
||||
good_parts: z.number().int().nonnegative().optional(),
|
||||
scrap_parts: z.number().int().nonnegative().optional(),
|
||||
cavities: z.number().int().positive().optional(),
|
||||
|
||||
cycleTime: z.number().nonnegative().optional(), // theoretical/target cycle time
|
||||
actualCycleTime: z.number().nonnegative().optional(), // optional
|
||||
|
||||
trackingEnabled: z.boolean().optional(),
|
||||
productionStarted: z.boolean().optional(),
|
||||
|
||||
kpis: z.object({
|
||||
oee: KPI_0_100,
|
||||
availability: KPI_0_100,
|
||||
performance: KPI_0_100,
|
||||
quality: KPI_0_100,
|
||||
}),
|
||||
})
|
||||
.passthrough();
|
||||
|
||||
/**
|
||||
* TEMPORARY: Accept your current legacy payload while Node-RED is not sending
|
||||
* schemaVersion/tsDevice/seq yet. Remove this once edge is upgraded.
|
||||
*/
|
||||
const SnapshotLegacy = z
|
||||
.object({
|
||||
machineId: z.any(),
|
||||
kpis: z.any(),
|
||||
})
|
||||
.passthrough();
|
||||
|
||||
export type SnapshotV1Type = z.infer<typeof SnapshotV1>;
|
||||
|
||||
export function normalizeSnapshotV1(raw: unknown): { ok: true; value: SnapshotV1Type } | { ok: false; error: string } {
|
||||
const candidate = preprocessPayload(raw);
|
||||
const strict = SnapshotV1.safeParse(candidate);
|
||||
if (strict.success) return { ok: true, value: strict.data };
|
||||
|
||||
// Legacy fallback (temporary)
|
||||
const legacy = SnapshotLegacy.safeParse(candidate);
|
||||
if (!legacy.success) {
|
||||
return { ok: false, error: strict.error.message };
|
||||
}
|
||||
/*
|
||||
const b: any = legacy.data;
|
||||
|
||||
// Build a "best effort" SnapshotV1 so ingest works during transition.
|
||||
// seq is intentionally set to "0" if missing (so you can still store);
|
||||
// once Node-RED emits real seq, dedupe and ordering become reliable.
|
||||
const migrated: any = {
|
||||
schemaVersion: SCHEMA_VERSION,
|
||||
machineId: String(b.machineId),
|
||||
tsDevice: typeof b.tsDevice === "number" ? b.tsDevice : Date.now(),
|
||||
seq: typeof b.seq === "number" || typeof b.seq === "string" ? b.seq : "0",
|
||||
...b,
|
||||
};
|
||||
|
||||
const recheck = SnapshotV1.safeParse(migrated);
|
||||
if (!recheck.success) return { ok: false, error: recheck.error.message };
|
||||
return { ok: true, value: recheck.data };
|
||||
*/
|
||||
const b = asRecord(legacy.data) ?? {};
|
||||
const activeWorkOrder = asRecord(b.activeWorkOrder);
|
||||
const kpis = asRecord(b.kpis);
|
||||
const kpiSnapshot = asRecord(b.kpi_snapshot);
|
||||
|
||||
const legacyCycleTime =
|
||||
b.cycleTime ??
|
||||
b.cycle_time ??
|
||||
b.theoretical_cycle_time ??
|
||||
b.theoreticalCycleTime ??
|
||||
b.standard_cycle_time ??
|
||||
kpiSnapshot?.cycleTime ??
|
||||
kpiSnapshot?.cycle_time ??
|
||||
undefined;
|
||||
|
||||
const legacyActualCycleTime =
|
||||
b.actualCycleTime ??
|
||||
b.actual_cycle_time ??
|
||||
b.actualCycleSeconds ??
|
||||
kpiSnapshot?.actualCycleTime ??
|
||||
kpiSnapshot?.actual_cycle_time ??
|
||||
undefined;
|
||||
|
||||
const legacyWorkOrderId =
|
||||
activeWorkOrder?.id ??
|
||||
b.work_order_id ??
|
||||
b.workOrderId ??
|
||||
kpis?.workOrderId ??
|
||||
kpiSnapshot?.work_order_id ??
|
||||
undefined;
|
||||
|
||||
const legacySku =
|
||||
activeWorkOrder?.sku ??
|
||||
b.sku ??
|
||||
kpis?.sku ??
|
||||
kpiSnapshot?.sku ??
|
||||
undefined;
|
||||
|
||||
const legacyTarget =
|
||||
activeWorkOrder?.target ??
|
||||
b.target ??
|
||||
kpis?.target ??
|
||||
kpiSnapshot?.target ??
|
||||
undefined;
|
||||
|
||||
const legacyGood =
|
||||
activeWorkOrder?.good ??
|
||||
b.good_parts ??
|
||||
b.good ??
|
||||
kpis?.good ??
|
||||
kpiSnapshot?.good_parts ??
|
||||
undefined;
|
||||
|
||||
const legacyScrap =
|
||||
activeWorkOrder?.scrap ??
|
||||
b.scrap_parts ??
|
||||
b.scrap ??
|
||||
kpis?.scrap ??
|
||||
kpiSnapshot?.scrap_parts ??
|
||||
undefined;
|
||||
|
||||
const migrated: Record<string, unknown> = {
|
||||
schemaVersion: SCHEMA_VERSION,
|
||||
machineId: String(b.machineId),
|
||||
tsDevice: typeof b.tsDevice === "number" ? b.tsDevice : Date.now(),
|
||||
seq: typeof b.seq === "number" || typeof b.seq === "string" ? b.seq : "0",
|
||||
|
||||
// canonical fields (force them)
|
||||
cycleTime: legacyCycleTime != null ? Number(legacyCycleTime) : undefined,
|
||||
actualCycleTime: legacyActualCycleTime != null ? Number(legacyActualCycleTime) : undefined,
|
||||
|
||||
activeWorkOrder: legacyWorkOrderId
|
||||
? {
|
||||
id: String(legacyWorkOrderId),
|
||||
sku: legacySku != null ? String(legacySku) : undefined,
|
||||
target: legacyTarget != null ? Number(legacyTarget) : undefined,
|
||||
good: legacyGood != null ? Number(legacyGood) : undefined,
|
||||
scrap: legacyScrap != null ? Number(legacyScrap) : undefined,
|
||||
}
|
||||
: activeWorkOrder,
|
||||
|
||||
// keep everything else
|
||||
...b,
|
||||
};
|
||||
const recheck = SnapshotV1.safeParse(migrated);
|
||||
if (!recheck.success) return { ok: false, error: recheck.error.message };
|
||||
return { ok: true, value: recheck.data };
|
||||
|
||||
}
|
||||
|
||||
const HeartbeatV1 = z.object({
|
||||
schemaVersion: z.literal(SCHEMA_VERSION),
|
||||
machineId: z.string().uuid(),
|
||||
tsDevice: z.number().int().nonnegative(),
|
||||
seq: z.union([z.number().int().nonnegative(), z.string().regex(/^\d+$/)]),
|
||||
|
||||
// legacy shape you currently send: status/message/ip/fwVersion
|
||||
status: z.string().optional(),
|
||||
message: z.string().optional(),
|
||||
ip: z.string().optional(),
|
||||
fwVersion: z.string().optional(),
|
||||
|
||||
// new canonical boolean
|
||||
online: z.boolean().optional(),
|
||||
}).passthrough();
|
||||
|
||||
export function normalizeHeartbeatV1(raw: unknown) {
|
||||
const candidate = preprocessPayload(raw);
|
||||
const strict = HeartbeatV1.safeParse(candidate);
|
||||
if (strict.success) return { ok: true as const, value: strict.data };
|
||||
|
||||
// legacy fallback: allow missing meta
|
||||
const legacy = z.object({ machineId: z.any() }).passthrough().safeParse(candidate);
|
||||
if (!legacy.success) return { ok: false as const, error: strict.error.message };
|
||||
|
||||
const b = asRecord(legacy.data) ?? {};
|
||||
const migrated: Record<string, unknown> = {
|
||||
schemaVersion: SCHEMA_VERSION,
|
||||
machineId: String(b.machineId),
|
||||
tsDevice: typeof b.tsDevice === "number" ? b.tsDevice : Date.now(),
|
||||
seq: typeof b.seq === "number" || typeof b.seq === "string" ? b.seq : "0",
|
||||
...b,
|
||||
};
|
||||
|
||||
const recheck = HeartbeatV1.safeParse(migrated);
|
||||
if (!recheck.success) return { ok: false as const, error: recheck.error.message };
|
||||
return { ok: true as const, value: recheck.data };
|
||||
}
|
||||
|
||||
const CycleV1 = z.object({
|
||||
schemaVersion: z.literal(SCHEMA_VERSION),
|
||||
machineId: z.string().uuid(),
|
||||
tsDevice: z.number().int().nonnegative(),
|
||||
seq: z.union([z.number().int().nonnegative(), z.string().regex(/^\d+$/)]),
|
||||
|
||||
cycle: z.object({
|
||||
timestamp: z.number().int().positive(),
|
||||
cycle_count: z.number().int().nonnegative(),
|
||||
actual_cycle_time: z.number(),
|
||||
theoretical_cycle_time: z.number().optional(),
|
||||
work_order_id: z.string(),
|
||||
sku: z.string().optional(),
|
||||
cavities: z.number().optional(),
|
||||
good_delta: z.number().optional(),
|
||||
scrap_total: z.number().optional(),
|
||||
}).passthrough(),
|
||||
}).passthrough();
|
||||
|
||||
export function normalizeCycleV1(raw: unknown) {
|
||||
const candidate = preprocessPayload(raw);
|
||||
const strict = CycleV1.safeParse(candidate);
|
||||
if (strict.success) return { ok: true as const, value: strict.data };
|
||||
|
||||
// legacy fallback: { machineId, cycle }
|
||||
const legacy = z.object({ machineId: z.any(), cycle: z.any() }).passthrough().safeParse(candidate);
|
||||
if (!legacy.success) return { ok: false as const, error: strict.error.message };
|
||||
|
||||
const b = asRecord(legacy.data) ?? {};
|
||||
const cycle = asRecord(b.cycle);
|
||||
const tsDevice =
|
||||
typeof b.tsDevice === "number" ? b.tsDevice : (cycle?.timestamp as number | undefined) ?? Date.now();
|
||||
const seq =
|
||||
typeof b.seq === "number" || typeof b.seq === "string"
|
||||
? b.seq
|
||||
: (cycle?.cycle_count as number | string | undefined) ?? "0";
|
||||
|
||||
const migrated: Record<string, unknown> = {
|
||||
schemaVersion: SCHEMA_VERSION,
|
||||
machineId: String(b.machineId),
|
||||
tsDevice,
|
||||
seq,
|
||||
...b,
|
||||
};
|
||||
const recheck = CycleV1.safeParse(migrated);
|
||||
if (!recheck.success) return { ok: false as const, error: recheck.error.message };
|
||||
return { ok: true as const, value: recheck.data };
|
||||
}
|
||||
|
||||
const EventV1 = z.object({
|
||||
schemaVersion: z.literal(SCHEMA_VERSION),
|
||||
machineId: z.string().uuid(),
|
||||
tsDevice: z.number().int().nonnegative(),
|
||||
seq: z.union([z.number().int().nonnegative(), z.string().regex(/^\d+$/)]),
|
||||
|
||||
// IMPORTANT: event must be an object, not an array
|
||||
event: z.object({
|
||||
anomaly_type: z.string(),
|
||||
severity: z.string(),
|
||||
title: z.string(),
|
||||
description: z.string().optional(),
|
||||
timestamp: z.number().int().positive(),
|
||||
work_order_id: z.string(),
|
||||
cycle_count: z.number().optional(),
|
||||
data: z.any().optional(),
|
||||
kpi_snapshot: z.any().optional(),
|
||||
}).passthrough(),
|
||||
}).passthrough();
|
||||
|
||||
export function normalizeEventV1(raw: unknown) {
|
||||
const candidate = preprocessPayload(raw);
|
||||
const strict = EventV1.safeParse(candidate);
|
||||
if (strict.success) return { ok: true as const, value: strict.data };
|
||||
|
||||
// legacy fallback: allow missing meta, but STILL reject arrays later
|
||||
const legacy = z.object({ machineId: z.any(), event: z.any() }).passthrough().safeParse(candidate);
|
||||
if (!legacy.success) return { ok: false as const, error: strict.error.message };
|
||||
|
||||
const b = asRecord(legacy.data) ?? {};
|
||||
const event = asRecord(b.event);
|
||||
const tsDevice =
|
||||
typeof b.tsDevice === "number" ? b.tsDevice : (event?.timestamp as number | undefined) ?? Date.now();
|
||||
const migrated: Record<string, unknown> = {
|
||||
schemaVersion: SCHEMA_VERSION,
|
||||
machineId: String(b.machineId),
|
||||
tsDevice,
|
||||
seq: typeof b.seq === "number" || typeof b.seq === "string" ? b.seq : "0",
|
||||
...b,
|
||||
};
|
||||
|
||||
const recheck = EventV1.safeParse(migrated);
|
||||
if (!recheck.success) return { ok: false as const, error: recheck.error.message };
|
||||
return { ok: true as const, value: recheck.data };
|
||||
}
|
||||
214
lib/email.ts
Normal file
214
lib/email.ts
Normal file
@@ -0,0 +1,214 @@
|
||||
import nodemailer from "nodemailer";
|
||||
import { logLine } from "@/lib/logger";
|
||||
type EmailPayload = {
|
||||
to: string;
|
||||
subject: string;
|
||||
text: string;
|
||||
html: string;
|
||||
};
|
||||
|
||||
let cachedTransport: nodemailer.Transporter | null = null;
|
||||
|
||||
function getTransporter() {
|
||||
if (cachedTransport) return cachedTransport;
|
||||
|
||||
const host = process.env.SMTP_HOST;
|
||||
const port = Number(process.env.SMTP_PORT || 465);
|
||||
const user = process.env.SMTP_USER;
|
||||
const pass = process.env.SMTP_PASS;
|
||||
const secure =
|
||||
process.env.SMTP_SECURE !== undefined
|
||||
? process.env.SMTP_SECURE === "true"
|
||||
: port === 465;
|
||||
|
||||
if (!host || !user || !pass) {
|
||||
throw new Error("SMTP not configured");
|
||||
}
|
||||
|
||||
const smtpDebug = process.env.SMTP_DEBUG === "true";
|
||||
logLine("smtp.config", {
|
||||
host,
|
||||
port,
|
||||
secure,
|
||||
user,
|
||||
from: process.env.SMTP_FROM,
|
||||
smtpDebug,
|
||||
});
|
||||
|
||||
cachedTransport = nodemailer.createTransport({
|
||||
host,
|
||||
port,
|
||||
secure,
|
||||
auth: { user, pass },
|
||||
logger: smtpDebug,
|
||||
debug: smtpDebug,
|
||||
});
|
||||
|
||||
return cachedTransport;
|
||||
}
|
||||
|
||||
export async function sendEmail(payload: EmailPayload) {
|
||||
const from = process.env.SMTP_FROM || process.env.SMTP_USER;
|
||||
if (!from) {
|
||||
throw new Error("SMTP_FROM not configured");
|
||||
}
|
||||
logLine("email.send.start", {
|
||||
to: payload.to,
|
||||
subject: payload.subject,
|
||||
from,
|
||||
});
|
||||
|
||||
const transporter = getTransporter();
|
||||
try {
|
||||
const info = await transporter.sendMail({
|
||||
from,
|
||||
to: payload.to,
|
||||
subject: payload.subject,
|
||||
text: payload.text,
|
||||
html: payload.html,
|
||||
headers: {
|
||||
"X-Mailer": "MIS Control Tower",
|
||||
},
|
||||
|
||||
replyTo: from,
|
||||
});
|
||||
|
||||
// Nodemailer response details:
|
||||
const pending = "pending" in info ? (info as { pending?: string[] }).pending : undefined;
|
||||
logLine("email.send.ok", {
|
||||
to: payload.to,
|
||||
from,
|
||||
messageId: info.messageId,
|
||||
response: info.response,
|
||||
accepted: info.accepted,
|
||||
rejected: info.rejected,
|
||||
pending,
|
||||
});
|
||||
|
||||
return info;
|
||||
} catch (err: unknown) {
|
||||
const error = err as {
|
||||
name?: string;
|
||||
message?: string;
|
||||
code?: string;
|
||||
command?: string;
|
||||
response?: unknown;
|
||||
responseCode?: number;
|
||||
stack?: string;
|
||||
};
|
||||
logLine("email.send.err", {
|
||||
to: payload.to,
|
||||
from,
|
||||
name: error?.name,
|
||||
message: error?.message,
|
||||
code: error?.code,
|
||||
command: error?.command,
|
||||
response: error?.response,
|
||||
responseCode: error?.responseCode,
|
||||
stack: error?.stack,
|
||||
});
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
|
||||
export function buildVerifyEmail(params: { appName: string; verifyUrl: string }) {
|
||||
const subject = `Verify your ${params.appName} account`;
|
||||
const text =
|
||||
`Welcome to ${params.appName}.\n\n` +
|
||||
`Verify your email to activate your account:\n${params.verifyUrl}\n\n` +
|
||||
`If you did not request this, ignore this email.`;
|
||||
const html =
|
||||
`<p>Welcome to ${params.appName}.</p>` +
|
||||
`<p>Verify your email to activate your account:</p>` +
|
||||
`<p><a href="${params.verifyUrl}">${params.verifyUrl}</a></p>` +
|
||||
`<p>If you did not request this, ignore this email.</p>`;
|
||||
|
||||
return { subject, text, html };
|
||||
}
|
||||
|
||||
export function buildInviteEmail(params: {
|
||||
appName: string;
|
||||
orgName: string;
|
||||
inviteUrl: string;
|
||||
}) {
|
||||
const subject = `You're invited to ${params.orgName} on ${params.appName}`;
|
||||
const text =
|
||||
`You have been invited to join ${params.orgName} on ${params.appName}.\n\n` +
|
||||
`Accept the invite here:\n${params.inviteUrl}\n\n` +
|
||||
`If you did not expect this invite, you can ignore this email.`;
|
||||
const html =
|
||||
`<p>You have been invited to join ${params.orgName} on ${params.appName}.</p>` +
|
||||
`<p>Accept the invite here:</p>` +
|
||||
`<p><a href="${params.inviteUrl}">${params.inviteUrl}</a></p>` +
|
||||
`<p>If you did not expect this invite, you can ignore this email.</p>`;
|
||||
|
||||
return { subject, text, html };
|
||||
}
|
||||
|
||||
export function buildDowntimeActionAssignedEmail(params: {
|
||||
appName: string;
|
||||
orgName: string;
|
||||
actionTitle: string;
|
||||
assigneeName: string;
|
||||
dueDate: string | null;
|
||||
actionUrl: string;
|
||||
priority: string;
|
||||
status: string;
|
||||
}) {
|
||||
const dueLabel = params.dueDate ? `Due ${params.dueDate}` : "No due date";
|
||||
const subject = `Action assigned: ${params.actionTitle}`;
|
||||
const text =
|
||||
`Hi ${params.assigneeName},\n\n` +
|
||||
`You have been assigned an action in ${params.orgName} (${params.appName}).\n\n` +
|
||||
`Title: ${params.actionTitle}\n` +
|
||||
`Status: ${params.status}\n` +
|
||||
`Priority: ${params.priority}\n` +
|
||||
`${dueLabel}\n\n` +
|
||||
`Open in Control Tower:\n${params.actionUrl}\n\n` +
|
||||
`If you did not expect this assignment, please contact your admin.`;
|
||||
const html =
|
||||
`<p>Hi ${params.assigneeName},</p>` +
|
||||
`<p>You have been assigned an action in ${params.orgName} (${params.appName}).</p>` +
|
||||
`<p><strong>Title:</strong> ${params.actionTitle}<br />` +
|
||||
`<strong>Status:</strong> ${params.status}<br />` +
|
||||
`<strong>Priority:</strong> ${params.priority}<br />` +
|
||||
`<strong>${dueLabel}</strong></p>` +
|
||||
`<p><a href="${params.actionUrl}">Open in Control Tower</a></p>` +
|
||||
`<p>If you did not expect this assignment, please contact your admin.</p>`;
|
||||
|
||||
return { subject, text, html };
|
||||
}
|
||||
|
||||
export function buildDowntimeActionReminderEmail(params: {
|
||||
appName: string;
|
||||
orgName: string;
|
||||
actionTitle: string;
|
||||
assigneeName: string;
|
||||
dueDate: string | null;
|
||||
actionUrl: string;
|
||||
priority: string;
|
||||
status: string;
|
||||
}) {
|
||||
const dueLabel = params.dueDate ? `Due ${params.dueDate}` : "No due date";
|
||||
const subject = `Reminder: ${params.actionTitle}`;
|
||||
const text =
|
||||
`Hi ${params.assigneeName},\n\n` +
|
||||
`Reminder for your action in ${params.orgName} (${params.appName}).\n\n` +
|
||||
`Title: ${params.actionTitle}\n` +
|
||||
`Status: ${params.status}\n` +
|
||||
`Priority: ${params.priority}\n` +
|
||||
`${dueLabel}\n\n` +
|
||||
`Open in Control Tower:\n${params.actionUrl}\n\n` +
|
||||
`If you have already completed this action, you can mark it done in the app.`;
|
||||
const html =
|
||||
`<p>Hi ${params.assigneeName},</p>` +
|
||||
`<p>Reminder for your action in ${params.orgName} (${params.appName}).</p>` +
|
||||
`<p><strong>Title:</strong> ${params.actionTitle}<br />` +
|
||||
`<strong>Status:</strong> ${params.status}<br />` +
|
||||
`<strong>Priority:</strong> ${params.priority}<br />` +
|
||||
`<strong>${dueLabel}</strong></p>` +
|
||||
`<p><a href="${params.actionUrl}">Open in Control Tower</a></p>` +
|
||||
`<p>If you have already completed this action, you can mark it done in the app.</p>`;
|
||||
|
||||
return { subject, text, html };
|
||||
}
|
||||
214
lib/events/normalizeEvent.ts
Normal file
214
lib/events/normalizeEvent.ts
Normal file
@@ -0,0 +1,214 @@
|
||||
type NormalizeThresholds = {
|
||||
microMultiplier: number;
|
||||
macroMultiplier: number;
|
||||
};
|
||||
|
||||
type RawEventRow = {
|
||||
id: string;
|
||||
ts?: Date | null;
|
||||
topic?: string | null;
|
||||
eventType?: string | null;
|
||||
severity?: string | null;
|
||||
title?: string | null;
|
||||
description?: string | null;
|
||||
requiresAck?: boolean | null;
|
||||
data?: unknown;
|
||||
workOrderId?: string | null;
|
||||
};
|
||||
|
||||
function coerceString(value: unknown) {
|
||||
if (value === null || value === undefined) return null;
|
||||
if (typeof value === "string") return value;
|
||||
if (typeof value === "number") return String(value);
|
||||
return null;
|
||||
}
|
||||
|
||||
export function normalizeEvent(row: RawEventRow, thresholds: NormalizeThresholds) {
|
||||
// -----------------------------
|
||||
// 1) Parse row.data safely
|
||||
// data may be:
|
||||
// - object
|
||||
// - array of objects
|
||||
// - JSON string of either
|
||||
// -----------------------------
|
||||
const raw = row.data;
|
||||
|
||||
let parsed: unknown = raw;
|
||||
if (typeof raw === "string") {
|
||||
try {
|
||||
parsed = JSON.parse(raw);
|
||||
} catch {
|
||||
parsed = raw; // keep as string if not JSON
|
||||
}
|
||||
}
|
||||
|
||||
// data can be object OR [object]
|
||||
const blob = Array.isArray(parsed) ? parsed[0] : parsed;
|
||||
|
||||
// some payloads nest details under blob.data
|
||||
const inner = (blob as { data?: unknown })?.data ?? blob ?? {};
|
||||
|
||||
const normalizeType = (t: unknown) =>
|
||||
String(t ?? "")
|
||||
.trim()
|
||||
.toLowerCase()
|
||||
.replace(/_/g, "-");
|
||||
|
||||
// -----------------------------
|
||||
// 2) Alias mapping (canonical types)
|
||||
// -----------------------------
|
||||
const ALIAS: Record<string, string> = {
|
||||
// Spanish / synonyms
|
||||
macroparo: "macrostop",
|
||||
"macro-stop": "macrostop",
|
||||
macro_stop: "macrostop",
|
||||
|
||||
microparo: "microstop",
|
||||
"micro-paro": "microstop",
|
||||
micro_stop: "microstop",
|
||||
|
||||
// Node-RED types
|
||||
"production-stopped": "stop", // we'll classify to micro/macro below
|
||||
|
||||
// legacy / generic
|
||||
down: "stop",
|
||||
};
|
||||
|
||||
// -----------------------------
|
||||
// 3) Determine event type from DB or blob
|
||||
// -----------------------------
|
||||
const fromDbType = row.eventType && row.eventType !== "unknown" ? row.eventType : null;
|
||||
|
||||
const fromBlobType =
|
||||
(blob as { anomaly_type?: unknown; eventType?: unknown; topic?: unknown })?.anomaly_type ??
|
||||
(blob as { anomaly_type?: unknown; eventType?: unknown; topic?: unknown })?.eventType ??
|
||||
(blob as { anomaly_type?: unknown; eventType?: unknown; topic?: unknown })?.topic ??
|
||||
(inner as { anomaly_type?: unknown; eventType?: unknown })?.anomaly_type ??
|
||||
(inner as { anomaly_type?: unknown; eventType?: unknown })?.eventType ??
|
||||
null;
|
||||
|
||||
// infer slow-cycle if signature exists
|
||||
const inferredType =
|
||||
fromDbType ??
|
||||
fromBlobType ??
|
||||
(((inner as { actual_cycle_time?: unknown; theoretical_cycle_time?: unknown })?.actual_cycle_time &&
|
||||
(inner as { actual_cycle_time?: unknown; theoretical_cycle_time?: unknown })?.theoretical_cycle_time) ||
|
||||
((blob as { actual_cycle_time?: unknown; theoretical_cycle_time?: unknown })?.actual_cycle_time &&
|
||||
(blob as { actual_cycle_time?: unknown; theoretical_cycle_time?: unknown })?.theoretical_cycle_time)
|
||||
? "slow-cycle"
|
||||
: "unknown");
|
||||
|
||||
const eventTypeRaw = normalizeType(inferredType);
|
||||
let eventType = ALIAS[eventTypeRaw] ?? eventTypeRaw;
|
||||
|
||||
// -----------------------------
|
||||
// 4) Optional: classify "stop" into micro/macro based on duration if present
|
||||
// (keeps old rows usable even if they stored production-stopped)
|
||||
// -----------------------------
|
||||
if (eventType === "stop") {
|
||||
const innerData = inner as {
|
||||
stoppage_duration_seconds?: unknown;
|
||||
stop_duration_seconds?: unknown;
|
||||
theoretical_cycle_time?: unknown;
|
||||
};
|
||||
const blobData = blob as {
|
||||
stoppage_duration_seconds?: unknown;
|
||||
stop_duration_seconds?: unknown;
|
||||
theoretical_cycle_time?: unknown;
|
||||
};
|
||||
|
||||
const stopSec =
|
||||
(typeof innerData?.stoppage_duration_seconds === "number" && innerData.stoppage_duration_seconds) ||
|
||||
(typeof blobData?.stoppage_duration_seconds === "number" && blobData.stoppage_duration_seconds) ||
|
||||
(typeof innerData?.stop_duration_seconds === "number" && innerData.stop_duration_seconds) ||
|
||||
null;
|
||||
|
||||
const microMultiplier = Number(thresholds?.microMultiplier ?? 1.5);
|
||||
const macroMultiplier = Math.max(microMultiplier, Number(thresholds?.macroMultiplier ?? 5));
|
||||
|
||||
const theoreticalCycle =
|
||||
Number(innerData?.theoretical_cycle_time ?? blobData?.theoretical_cycle_time) || 0;
|
||||
|
||||
if (stopSec != null) {
|
||||
if (theoreticalCycle > 0) {
|
||||
const macroThresholdSec = theoreticalCycle * macroMultiplier;
|
||||
eventType = stopSec >= macroThresholdSec ? "macrostop" : "microstop";
|
||||
} else {
|
||||
const fallbackMacroSec = 300;
|
||||
eventType = stopSec >= fallbackMacroSec ? "macrostop" : "microstop";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// -----------------------------
|
||||
// 5) Severity, title, description, timestamp
|
||||
// -----------------------------
|
||||
const severity =
|
||||
String(
|
||||
(row.severity && row.severity !== "info" ? row.severity : null) ??
|
||||
(blob as { severity?: unknown })?.severity ??
|
||||
(inner as { severity?: unknown })?.severity ??
|
||||
"info"
|
||||
)
|
||||
.trim()
|
||||
.toLowerCase();
|
||||
|
||||
const title =
|
||||
String(
|
||||
(row.title && row.title !== "Event" ? row.title : null) ??
|
||||
(blob as { title?: unknown })?.title ??
|
||||
(inner as { title?: unknown })?.title ??
|
||||
(eventType === "slow-cycle" ? "Slow Cycle Detected" : "Event")
|
||||
).trim();
|
||||
|
||||
const description =
|
||||
row.description ??
|
||||
(blob as { description?: string | null })?.description ??
|
||||
(inner as { description?: string | null })?.description ??
|
||||
(eventType === "slow-cycle" &&
|
||||
((inner as { actual_cycle_time?: unknown })?.actual_cycle_time ??
|
||||
(blob as { actual_cycle_time?: unknown })?.actual_cycle_time) &&
|
||||
((inner as { theoretical_cycle_time?: unknown })?.theoretical_cycle_time ??
|
||||
(blob as { theoretical_cycle_time?: unknown })?.theoretical_cycle_time) &&
|
||||
((inner as { delta_percent?: unknown })?.delta_percent ??
|
||||
(blob as { delta_percent?: unknown })?.delta_percent) != null
|
||||
? `Cycle took ${Number(
|
||||
(inner as { actual_cycle_time?: unknown })?.actual_cycle_time ??
|
||||
(blob as { actual_cycle_time?: unknown })?.actual_cycle_time
|
||||
).toFixed(1)}s (+${Number(
|
||||
(inner as { delta_percent?: unknown })?.delta_percent ??
|
||||
(blob as { delta_percent?: unknown })?.delta_percent
|
||||
)}% vs ${Number(
|
||||
(inner as { theoretical_cycle_time?: unknown })?.theoretical_cycle_time ??
|
||||
(blob as { theoretical_cycle_time?: unknown })?.theoretical_cycle_time
|
||||
).toFixed(1)}s objetivo)`
|
||||
: null);
|
||||
|
||||
const ts =
|
||||
row.ts ??
|
||||
(typeof (blob as { timestamp?: unknown })?.timestamp === "number"
|
||||
? new Date((blob as { timestamp?: number }).timestamp as number)
|
||||
: null) ??
|
||||
(typeof (inner as { timestamp?: unknown })?.timestamp === "number"
|
||||
? new Date((inner as { timestamp?: number }).timestamp as number)
|
||||
: null) ??
|
||||
null;
|
||||
|
||||
const workOrderId =
|
||||
coerceString(row.workOrderId) ??
|
||||
coerceString((blob as { work_order_id?: unknown })?.work_order_id) ??
|
||||
coerceString((inner as { work_order_id?: unknown })?.work_order_id) ??
|
||||
null;
|
||||
|
||||
return {
|
||||
id: row.id,
|
||||
ts,
|
||||
topic: String(row.topic ?? (blob as { topic?: unknown })?.topic ?? eventType),
|
||||
eventType,
|
||||
severity,
|
||||
title,
|
||||
description,
|
||||
requiresAck: !!row.requiresAck,
|
||||
workOrderId,
|
||||
};
|
||||
}
|
||||
418
lib/financial/impact.ts
Normal file
418
lib/financial/impact.ts
Normal file
@@ -0,0 +1,418 @@
|
||||
import { prisma } from "@/lib/prisma";
|
||||
|
||||
const COST_EVENT_TYPES = ["slow-cycle", "microstop", "macrostop", "quality-spike"] as const;
|
||||
|
||||
type CostProfile = {
|
||||
currency: string;
|
||||
machineCostPerMin: number | null;
|
||||
operatorCostPerMin: number | null;
|
||||
ratedRunningKw: number | null;
|
||||
idleKw: number | null;
|
||||
kwhRate: number | null;
|
||||
energyMultiplier: number | null;
|
||||
energyCostPerMin: number | null;
|
||||
scrapCostPerUnit: number | null;
|
||||
rawMaterialCostPerUnit: number | null;
|
||||
};
|
||||
|
||||
|
||||
|
||||
type CostProfileOverride = Omit<Partial<CostProfile>, "currency">;
|
||||
type Category = "slowCycle" | "microstop" | "macrostop" | "scrap";
|
||||
type Totals = { total: number } & Record<Category, number>;
|
||||
type DayRow = { day: string } & Totals;
|
||||
|
||||
export type FinancialEventDetail = {
|
||||
id: string;
|
||||
ts: Date;
|
||||
eventType: string;
|
||||
status: string;
|
||||
severity: string;
|
||||
category: Category;
|
||||
machineId: string;
|
||||
machineName: string | null;
|
||||
location: string | null;
|
||||
workOrderId: string | null;
|
||||
sku: string | null;
|
||||
durationSec: number | null;
|
||||
costMachine: number;
|
||||
costOperator: number;
|
||||
costEnergy: number;
|
||||
costScrap: number;
|
||||
costRawMaterial: number;
|
||||
costTotal: number;
|
||||
currency: string;
|
||||
};
|
||||
|
||||
export type FinancialImpactSummary = {
|
||||
currency: string;
|
||||
totals: Totals;
|
||||
byDay: DayRow[];
|
||||
};
|
||||
|
||||
export type FinancialImpactResult = {
|
||||
range: { start: Date; end: Date };
|
||||
currencySummaries: FinancialImpactSummary[];
|
||||
eventsEvaluated: number;
|
||||
eventsIncluded: number;
|
||||
events: FinancialEventDetail[];
|
||||
filters: {
|
||||
machineId?: string;
|
||||
location?: string;
|
||||
sku?: string;
|
||||
currency?: string;
|
||||
};
|
||||
};
|
||||
|
||||
export type FinancialImpactParams = {
|
||||
orgId: string;
|
||||
start: Date;
|
||||
end: Date;
|
||||
machineId?: string;
|
||||
location?: string;
|
||||
sku?: string;
|
||||
currency?: string;
|
||||
includeEvents?: boolean;
|
||||
};
|
||||
|
||||
function safeNumber(value: unknown) {
|
||||
const n = typeof value === "number" ? value : Number(value);
|
||||
return Number.isFinite(n) ? n : null;
|
||||
}
|
||||
|
||||
function parseBlob(raw: unknown) {
|
||||
let parsed: unknown = raw;
|
||||
if (typeof raw === "string") {
|
||||
try {
|
||||
parsed = JSON.parse(raw);
|
||||
} catch {
|
||||
parsed = raw;
|
||||
}
|
||||
}
|
||||
const blob = Array.isArray(parsed) ? parsed[0] : parsed;
|
||||
const blobRecord = typeof blob === "object" && blob !== null ? (blob as Record<string, unknown>) : null;
|
||||
const innerCandidate = blobRecord?.data ?? blobRecord ?? {};
|
||||
const inner =
|
||||
typeof innerCandidate === "object" && innerCandidate !== null
|
||||
? (innerCandidate as Record<string, unknown>)
|
||||
: {};
|
||||
return { blob: blobRecord, inner } as const;
|
||||
}
|
||||
|
||||
function dateKey(ts: Date) {
|
||||
return ts.toISOString().slice(0, 10);
|
||||
}
|
||||
|
||||
function applyOverride(
|
||||
base: CostProfile,
|
||||
override?: CostProfileOverride | null,
|
||||
currency?: string | null
|
||||
) {
|
||||
const out: CostProfile = { ...base };
|
||||
if (currency) out.currency = currency;
|
||||
if (!override) return out;
|
||||
|
||||
if (override.machineCostPerMin != null) out.machineCostPerMin = override.machineCostPerMin;
|
||||
if (override.operatorCostPerMin != null) out.operatorCostPerMin = override.operatorCostPerMin;
|
||||
if (override.ratedRunningKw != null) out.ratedRunningKw = override.ratedRunningKw;
|
||||
if (override.idleKw != null) out.idleKw = override.idleKw;
|
||||
if (override.kwhRate != null) out.kwhRate = override.kwhRate;
|
||||
if (override.energyMultiplier != null) out.energyMultiplier = override.energyMultiplier;
|
||||
if (override.energyCostPerMin != null) out.energyCostPerMin = override.energyCostPerMin;
|
||||
if (override.scrapCostPerUnit != null) out.scrapCostPerUnit = override.scrapCostPerUnit;
|
||||
if (override.rawMaterialCostPerUnit != null) out.rawMaterialCostPerUnit = override.rawMaterialCostPerUnit;
|
||||
return out;
|
||||
}
|
||||
|
||||
function computeEnergyCostPerMin(profile: CostProfile, mode: "running" | "idle") {
|
||||
if (profile.energyCostPerMin != null) return profile.energyCostPerMin;
|
||||
const kw = mode === "running" ? profile.ratedRunningKw : profile.idleKw;
|
||||
const rate = profile.kwhRate;
|
||||
if (kw == null || rate == null) return null;
|
||||
const multiplier = profile.energyMultiplier ?? 1;
|
||||
return (kw / 60) * rate * multiplier;
|
||||
}
|
||||
|
||||
export async function computeFinancialImpact(params: FinancialImpactParams): Promise<FinancialImpactResult> {
|
||||
const { orgId, start, end, machineId, location, sku, currency, includeEvents } = params;
|
||||
|
||||
const machines = await prisma.machine.findMany({
|
||||
where: { orgId },
|
||||
select: { id: true, name: true, location: true },
|
||||
});
|
||||
|
||||
const machineMap = new Map(machines.map((m) => [m.id, m]));
|
||||
|
||||
let machineIds = machines.map((m) => m.id);
|
||||
if (location) {
|
||||
machineIds = machines.filter((m) => m.location === location).map((m) => m.id);
|
||||
}
|
||||
if (machineId) {
|
||||
machineIds = machineIds.includes(machineId) ? [machineId] : [];
|
||||
}
|
||||
|
||||
if (!machineIds.length) {
|
||||
return {
|
||||
range: { start, end },
|
||||
currencySummaries: [],
|
||||
eventsEvaluated: 0,
|
||||
eventsIncluded: 0,
|
||||
events: [],
|
||||
filters: { machineId, location, sku, currency },
|
||||
};
|
||||
}
|
||||
|
||||
const events = await prisma.machineEvent.findMany({
|
||||
where: {
|
||||
orgId,
|
||||
ts: { gte: start, lte: end },
|
||||
machineId: { in: machineIds },
|
||||
eventType: { in: COST_EVENT_TYPES as unknown as string[] },
|
||||
},
|
||||
orderBy: { ts: "asc" },
|
||||
select: {
|
||||
id: true,
|
||||
ts: true,
|
||||
eventType: true,
|
||||
data: true,
|
||||
machineId: true,
|
||||
workOrderId: true,
|
||||
sku: true,
|
||||
severity: true,
|
||||
},
|
||||
});
|
||||
|
||||
const missingSkuPairs = events
|
||||
.filter((e) => !e.sku && e.workOrderId)
|
||||
.map((e) => ({ machineId: e.machineId, workOrderId: e.workOrderId as string }));
|
||||
const workOrderIds = Array.from(new Set(missingSkuPairs.map((p) => p.workOrderId)));
|
||||
const workOrderMachines = Array.from(new Set(missingSkuPairs.map((p) => p.machineId)));
|
||||
|
||||
const workOrders = workOrderIds.length
|
||||
? await prisma.machineWorkOrder.findMany({
|
||||
where: {
|
||||
orgId,
|
||||
workOrderId: { in: workOrderIds },
|
||||
machineId: { in: workOrderMachines },
|
||||
},
|
||||
select: { machineId: true, workOrderId: true, sku: true },
|
||||
})
|
||||
: [];
|
||||
|
||||
const workOrderSku = new Map<string, string>();
|
||||
for (const row of workOrders) {
|
||||
if (row.sku) {
|
||||
workOrderSku.set(`${row.machineId}:${row.workOrderId}`, row.sku);
|
||||
}
|
||||
}
|
||||
|
||||
const [orgProfileRaw, locationOverrides, machineOverrides, productOverrides] = await Promise.all([
|
||||
prisma.orgFinancialProfile.findUnique({ where: { orgId } }),
|
||||
prisma.locationFinancialOverride.findMany({ where: { orgId } }),
|
||||
prisma.machineFinancialOverride.findMany({ where: { orgId } }),
|
||||
prisma.productCostOverride.findMany({ where: { orgId } }),
|
||||
]);
|
||||
|
||||
const orgProfile: CostProfile = {
|
||||
currency: orgProfileRaw?.defaultCurrency ?? "USD",
|
||||
machineCostPerMin: orgProfileRaw?.machineCostPerMin ?? null,
|
||||
operatorCostPerMin: orgProfileRaw?.operatorCostPerMin ?? null,
|
||||
ratedRunningKw: orgProfileRaw?.ratedRunningKw ?? null,
|
||||
idleKw: orgProfileRaw?.idleKw ?? null,
|
||||
kwhRate: orgProfileRaw?.kwhRate ?? null,
|
||||
energyMultiplier: orgProfileRaw?.energyMultiplier ?? 1,
|
||||
energyCostPerMin: orgProfileRaw?.energyCostPerMin ?? null,
|
||||
scrapCostPerUnit: orgProfileRaw?.scrapCostPerUnit ?? null,
|
||||
rawMaterialCostPerUnit: orgProfileRaw?.rawMaterialCostPerUnit ?? null,
|
||||
};
|
||||
|
||||
const locationMap = new Map(locationOverrides.map((o) => [o.location, o]));
|
||||
const machineOverrideMap = new Map(machineOverrides.map((o) => [o.machineId, o]));
|
||||
const productMap = new Map(productOverrides.map((o) => [o.sku, o]));
|
||||
|
||||
const summaries = new Map<
|
||||
string,
|
||||
{
|
||||
currency: string;
|
||||
totals: Totals;
|
||||
byDay: Map<string, DayRow>;
|
||||
}
|
||||
>();
|
||||
|
||||
const detailed: FinancialEventDetail[] = [];
|
||||
let eventsIncluded = 0;
|
||||
|
||||
for (const ev of events) {
|
||||
const eventType = String(ev.eventType ?? "").toLowerCase();
|
||||
const { blob, inner } = parseBlob(ev.data);
|
||||
const status = String(blob?.status ?? inner?.status ?? "").toLowerCase();
|
||||
const severity = String(ev.severity ?? "").toLowerCase();
|
||||
const isAutoAck = Boolean(blob?.is_auto_ack ?? inner?.is_auto_ack);
|
||||
const isUpdate = Boolean(blob?.is_update ?? inner?.is_update);
|
||||
|
||||
const machine = machineMap.get(ev.machineId);
|
||||
const locationName = machine?.location ?? null;
|
||||
const skuResolved =
|
||||
ev.sku ??
|
||||
(ev.workOrderId ? workOrderSku.get(`${ev.machineId}:${ev.workOrderId}`) : null) ??
|
||||
null;
|
||||
|
||||
if (sku && skuResolved !== sku) continue;
|
||||
if (isAutoAck || isUpdate) continue;
|
||||
|
||||
const locationOverride = locationName ? locationMap.get(locationName) : null;
|
||||
const machineOverride = machineOverrideMap.get(ev.machineId) ?? null;
|
||||
|
||||
let profile = applyOverride(orgProfile, locationOverride, locationOverride?.currency ?? null);
|
||||
profile = applyOverride(profile, machineOverride, machineOverride?.currency ?? null);
|
||||
|
||||
const productOverride = skuResolved ? productMap.get(skuResolved) : null;
|
||||
if (productOverride?.rawMaterialCostPerUnit != null) {
|
||||
profile.rawMaterialCostPerUnit = productOverride.rawMaterialCostPerUnit;
|
||||
}
|
||||
if (productOverride?.currency) {
|
||||
profile.currency = productOverride.currency;
|
||||
}
|
||||
|
||||
let category: Category | null = null;
|
||||
let durationSec: number | null = null;
|
||||
let costMachine = 0;
|
||||
let costOperator = 0;
|
||||
let costEnergy = 0;
|
||||
let costScrap = 0;
|
||||
let costRawMaterial = 0;
|
||||
|
||||
if (eventType === "slow-cycle") {
|
||||
const actual =
|
||||
safeNumber(inner?.actual_cycle_time ?? blob?.actual_cycle_time ?? inner?.actualCycleTime ?? blob?.actualCycleTime) ??
|
||||
null;
|
||||
const theoretical =
|
||||
safeNumber(
|
||||
inner?.theoretical_cycle_time ??
|
||||
blob?.theoretical_cycle_time ??
|
||||
inner?.theoreticalCycleTime ??
|
||||
blob?.theoreticalCycleTime
|
||||
) ?? null;
|
||||
if (actual == null || theoretical == null) continue;
|
||||
durationSec = Math.max(0, actual - theoretical);
|
||||
if (!durationSec) continue;
|
||||
const durationMin = durationSec / 60;
|
||||
costMachine = durationMin * (profile.machineCostPerMin ?? 0);
|
||||
costOperator = durationMin * (profile.operatorCostPerMin ?? 0);
|
||||
costEnergy = durationMin * (computeEnergyCostPerMin(profile, "running") ?? 0);
|
||||
category = "slowCycle";
|
||||
} else if (eventType === "microstop" || eventType === "macrostop") {
|
||||
//future activestoppage handling
|
||||
if (status === "active") continue;
|
||||
const rawDurationSec =
|
||||
safeNumber(
|
||||
inner?.stoppage_duration_seconds ??
|
||||
blob?.stoppage_duration_seconds ??
|
||||
inner?.stop_duration_seconds ??
|
||||
blob?.stop_duration_seconds
|
||||
) ?? 0;
|
||||
if (!rawDurationSec || rawDurationSec <= 0) continue;
|
||||
const theoreticalSec =
|
||||
safeNumber(
|
||||
inner?.theoretical_cycle_time ??
|
||||
blob?.theoretical_cycle_time ??
|
||||
inner?.theoreticalCycleTime ??
|
||||
blob?.theoreticalCycleTime
|
||||
) ?? null;
|
||||
const lastCycleTimestamp = safeNumber(inner?.last_cycle_timestamp ?? blob?.last_cycle_timestamp);
|
||||
const isCycleGapStop = theoreticalSec != null && theoreticalSec > 0 && lastCycleTimestamp == null;
|
||||
durationSec = isCycleGapStop ? Math.max(0, rawDurationSec - theoreticalSec) : rawDurationSec;
|
||||
if (!durationSec || durationSec <= 0) continue;
|
||||
const durationMin = durationSec / 60;
|
||||
costMachine = durationMin * (profile.machineCostPerMin ?? 0);
|
||||
costOperator = durationMin * (profile.operatorCostPerMin ?? 0);
|
||||
costEnergy = durationMin * (computeEnergyCostPerMin(profile, "idle") ?? 0);
|
||||
category = eventType === "macrostop" ? "macrostop" : "microstop";
|
||||
} else if (eventType === "quality-spike") {
|
||||
if (severity === "info" || status === "resolved") continue;
|
||||
const scrapParts =
|
||||
safeNumber(
|
||||
inner?.scrap_parts ??
|
||||
blob?.scrap_parts ??
|
||||
inner?.scrapParts ??
|
||||
blob?.scrapParts
|
||||
) ?? 0;
|
||||
if (scrapParts <= 0) continue;
|
||||
costScrap = scrapParts * (profile.scrapCostPerUnit ?? 0);
|
||||
costRawMaterial = scrapParts * (profile.rawMaterialCostPerUnit ?? 0);
|
||||
category = "scrap";
|
||||
}
|
||||
|
||||
if (!category) continue;
|
||||
|
||||
const costTotal = costMachine + costOperator + costEnergy + costScrap + costRawMaterial;
|
||||
if (costTotal <= 0) continue;
|
||||
if (currency && profile.currency !== currency) continue;
|
||||
|
||||
const key = profile.currency || "USD";
|
||||
const bucket = summaries.get(key) ?? {
|
||||
currency: key,
|
||||
totals: { total: 0, slowCycle: 0, microstop: 0, macrostop: 0, scrap: 0 },
|
||||
byDay: new Map<string, DayRow>(),
|
||||
};
|
||||
|
||||
bucket.totals.total += costTotal;
|
||||
bucket.totals[category] += costTotal;
|
||||
|
||||
const day = dateKey(ev.ts);
|
||||
const dayRow: DayRow = bucket.byDay.get(day) ?? {
|
||||
day,
|
||||
total: 0,
|
||||
slowCycle: 0,
|
||||
microstop: 0,
|
||||
macrostop: 0,
|
||||
scrap: 0,
|
||||
};
|
||||
dayRow.total += costTotal;
|
||||
dayRow[category] += costTotal;
|
||||
bucket.byDay.set(day, dayRow);
|
||||
|
||||
summaries.set(key, bucket);
|
||||
eventsIncluded += 1;
|
||||
|
||||
if (includeEvents) {
|
||||
detailed.push({
|
||||
id: ev.id,
|
||||
ts: ev.ts,
|
||||
eventType,
|
||||
status,
|
||||
severity,
|
||||
category,
|
||||
machineId: ev.machineId,
|
||||
machineName: machine?.name ?? null,
|
||||
location: locationName,
|
||||
workOrderId: ev.workOrderId ?? null,
|
||||
sku: skuResolved,
|
||||
durationSec,
|
||||
costMachine,
|
||||
costOperator,
|
||||
costEnergy,
|
||||
costScrap,
|
||||
costRawMaterial,
|
||||
costTotal,
|
||||
currency: key,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
const currencySummaries = Array.from(summaries.values()).map((summary) => {
|
||||
const byDay = Array.from(summary.byDay.values()).sort((a, b) => {
|
||||
return String(a.day).localeCompare(String(b.day));
|
||||
});
|
||||
return { currency: summary.currency, totals: summary.totals, byDay };
|
||||
});
|
||||
|
||||
return {
|
||||
range: { start, end },
|
||||
currencySummaries,
|
||||
eventsEvaluated: events.length,
|
||||
eventsIncluded,
|
||||
events: detailed,
|
||||
filters: { machineId, location, sku, currency },
|
||||
};
|
||||
}
|
||||
520
lib/i18n/en.json
Normal file
520
lib/i18n/en.json
Normal file
@@ -0,0 +1,520 @@
|
||||
{
|
||||
"---": "---",
|
||||
"common.loading": "Loading...",
|
||||
"common.loadingShort": "Loading",
|
||||
"common.never": "never",
|
||||
"common.na": "--",
|
||||
"common.back": "Back",
|
||||
"common.cancel": "Cancel",
|
||||
"common.close": "Close",
|
||||
"common.save": "Save",
|
||||
"common.copy": "Copy",
|
||||
"nav.overview": "Overview",
|
||||
"nav.machines": "Machines",
|
||||
"nav.reports": "Reports",
|
||||
"nav.alerts": "Alerts",
|
||||
"nav.financial": "Financial",
|
||||
"nav.settings": "Settings",
|
||||
"sidebar.productTitle": "MIS",
|
||||
"sidebar.productSubtitle": "Control Tower",
|
||||
"sidebar.userFallback": "User",
|
||||
"sidebar.loadingOrg": "Loading...",
|
||||
"sidebar.themeTooltip": "Theme and language settings",
|
||||
"sidebar.switchToDark": "Switch to dark mode",
|
||||
"sidebar.switchToLight": "Switch to light mode",
|
||||
"sidebar.logout": "Logout",
|
||||
"sidebar.role.member": "MEMBER",
|
||||
"sidebar.role.admin": "ADMIN",
|
||||
"sidebar.role.owner": "OWNER",
|
||||
"login.title": "Control Tower",
|
||||
"login.subtitle": "Sign in to your organization",
|
||||
"login.email": "Email",
|
||||
"login.password": "Password",
|
||||
"login.error.default": "Login failed",
|
||||
"login.error.network": "Network error",
|
||||
"login.submit.loading": "Signing in...",
|
||||
"login.submit.default": "Login",
|
||||
"login.newHere": "New here?",
|
||||
"login.createAccount": "Create an account",
|
||||
"signup.verify.title": "Verify your email",
|
||||
"signup.verify.sent": "We sent a verification link to {email}.",
|
||||
"signup.verify.failed": "Verification email failed to send. Please contact support.",
|
||||
"signup.verify.notice": "Once verified, you can sign in and invite your team.",
|
||||
"signup.verify.back": "Back to login",
|
||||
"signup.title": "Create your Control Tower",
|
||||
"signup.subtitle": "Set up your organization and invite the team.",
|
||||
"signup.orgName": "Organization name",
|
||||
"signup.yourName": "Your name",
|
||||
"signup.email": "Email",
|
||||
"signup.password": "Password",
|
||||
"signup.error.default": "Signup failed",
|
||||
"signup.error.network": "Network error",
|
||||
"signup.submit.loading": "Creating account...",
|
||||
"signup.submit.default": "Create account",
|
||||
"signup.alreadyHave": "Already have access?",
|
||||
"signup.signIn": "Sign in",
|
||||
"invite.loading": "Loading invite...",
|
||||
"invite.notFound": "Invite not found.",
|
||||
"invite.joinTitle": "Join {org}",
|
||||
"invite.acceptCopy": "Accept the invite for {email} as {role}.",
|
||||
"invite.yourName": "Your name",
|
||||
"invite.password": "Password",
|
||||
"invite.error.notFound": "Invite not found",
|
||||
"invite.error.acceptFailed": "Invite acceptance failed",
|
||||
"invite.submit.loading": "Joining...",
|
||||
"invite.submit.default": "Join organization",
|
||||
"overview.title": "Overview",
|
||||
"overview.subtitle": "Fleet pulse, alerts, and top attention items.",
|
||||
"overview.viewMachines": "View Machines",
|
||||
"overview.loading": "Loading overview...",
|
||||
"overview.fleetHealth": "Fleet Health",
|
||||
"overview.machinesTotal": "Machines total",
|
||||
"overview.online": "Online",
|
||||
"overview.offline": "Offline",
|
||||
"overview.run": "Run",
|
||||
"overview.idle": "Idle",
|
||||
"overview.stop": "Stop",
|
||||
"overview.productionTotals": "Production Totals",
|
||||
"overview.good": "Good",
|
||||
"overview.scrap": "Scrap",
|
||||
"overview.target": "Target",
|
||||
"overview.kpiSumNote": "Sum of latest KPIs across machines.",
|
||||
"overview.activityFeed": "Activity Feed",
|
||||
"overview.eventsRefreshing": "Refreshing recent events...",
|
||||
"overview.eventsLast30": "Last 30 merged events",
|
||||
"overview.eventsNone": "No recent events.",
|
||||
"overview.oeeAvg": "OEE (avg)",
|
||||
"overview.availabilityAvg": "Availability (avg)",
|
||||
"overview.performanceAvg": "Performance (avg)",
|
||||
"overview.qualityAvg": "Quality (avg)",
|
||||
"overview.attentionList": "Attention List",
|
||||
"overview.shown": "shown",
|
||||
"overview.noUrgent": "No urgent issues detected.",
|
||||
"overview.timeline": "Unified Timeline",
|
||||
"overview.items": "items",
|
||||
"overview.noEvents": "No events yet.",
|
||||
"overview.ack": "ACK",
|
||||
"overview.severity.critical": "CRITICAL",
|
||||
"overview.severity.warning": "WARNING",
|
||||
"overview.severity.info": "INFO",
|
||||
"overview.source.ingested": "ingested",
|
||||
"overview.source.derived": "derived",
|
||||
"overview.event.macrostop": "macrostop",
|
||||
"overview.event.microstop": "microstop",
|
||||
"overview.event.slow-cycle": "slow-cycle",
|
||||
"overview.status.offline": "OFFLINE",
|
||||
"overview.status.online": "ONLINE",
|
||||
"machines.title": "Machines",
|
||||
"machines.subtitle": "Select a machine to view live KPIs.",
|
||||
"machines.cancel": "Cancel",
|
||||
"machines.addMachine": "Add Machine",
|
||||
"machines.backOverview": "Back to Overview",
|
||||
"machines.addCardTitle": "Add a machine",
|
||||
"machines.addCardSubtitle": "Generate the machine ID and API key for your Node-RED edge.",
|
||||
"machines.field.name": "Machine Name",
|
||||
"machines.field.code": "Code (optional)",
|
||||
"machines.field.location": "Location (optional)",
|
||||
"machines.create.loading": "Creating...",
|
||||
"machines.create.default": "Create Machine",
|
||||
"machines.create.error.nameRequired": "Machine name is required",
|
||||
"machines.create.error.failed": "Failed to create machine",
|
||||
"machines.delete": "Remove",
|
||||
"machines.delete.loading": "Removing...",
|
||||
"machines.delete.confirm": "Remove {name}? This will delete the machine and its data.",
|
||||
"machines.delete.error.failed": "Failed to remove machine",
|
||||
"machines.pairing.title": "Edge pairing code",
|
||||
"machines.pairing.machine": "Machine:",
|
||||
"machines.pairing.codeLabel": "Pairing code",
|
||||
"machines.pairing.expires": "Expires",
|
||||
"machines.pairing.soon": "soon",
|
||||
"machines.pairing.instructions": "Enter this code on the Node-RED Control Tower settings screen to link the edge device.",
|
||||
"machines.pairing.copy": "Copy Code",
|
||||
"machines.pairing.copied": "Copied",
|
||||
"machines.pairing.copyUnsupported": "Copy not supported",
|
||||
"machines.pairing.copyFailed": "Copy failed",
|
||||
"machines.loading": "Loading machines...",
|
||||
"machines.empty": "No machines found for this org.",
|
||||
"machines.status": "Status",
|
||||
"machines.status.noHeartbeat": "No heartbeat",
|
||||
"machines.status.ok": "Heartbeat",
|
||||
"machines.status.offline": "OFFLINE",
|
||||
"machines.status.unknown": "UNKNOWN",
|
||||
"machines.lastSeen": "Last seen {time}",
|
||||
"machine.detail.titleFallback": "Machine",
|
||||
"machine.detail.lastSeen": "Last seen {time}",
|
||||
"machine.detail.loading": "Loading...",
|
||||
"machine.detail.error.failed": "Failed to load machine",
|
||||
"machine.detail.error.network": "Network error",
|
||||
"machine.detail.back": "Back",
|
||||
"machine.detail.workOrders.upload": "Upload Work Orders",
|
||||
"machine.detail.workOrders.uploading": "Uploading...",
|
||||
"machine.detail.workOrders.uploadParsing": "Parsing file...",
|
||||
"machine.detail.workOrders.uploadHint": "CSV or XLSX with Work Order ID, SKU, Theoretical Cycle Time (Seconds), Target Quantity.",
|
||||
"machine.detail.workOrders.uploadSuccess": "Uploaded {count} work orders",
|
||||
"machine.detail.workOrders.uploadError": "Upload failed",
|
||||
"machine.detail.workOrders.uploadInvalid": "No valid work orders found",
|
||||
"machine.detail.workOrders.uploadUnauthorized": "Not authorized to upload work orders",
|
||||
"machine.detail.status.offline": "OFFLINE",
|
||||
"machine.detail.status.unknown": "UNKNOWN",
|
||||
"machine.detail.status.run": "RUN",
|
||||
"machine.detail.status.idle": "IDLE",
|
||||
"machine.detail.status.stop": "STOP",
|
||||
"machine.detail.status.down": "DOWN",
|
||||
"machine.detail.bucket.normal": "Normal Cycle",
|
||||
"machine.detail.bucket.slow": "Slow Cycle",
|
||||
"machine.detail.bucket.microstop": "Microstop",
|
||||
"machine.detail.bucket.macrostop": "Macrostop",
|
||||
"machine.detail.bucket.unknown": "Unknown",
|
||||
"machine.detail.activity.title": "Machine Activity Timeline",
|
||||
"machine.detail.activity.subtitle": "Real-time analysis of production cycles",
|
||||
"machine.detail.activity.noData": "No timeline data yet.",
|
||||
"machine.detail.tooltip.cycle": "Cycle: {label}",
|
||||
"machine.detail.tooltip.duration": "Duration",
|
||||
"machine.detail.tooltip.ideal": "Ideal",
|
||||
"machine.detail.tooltip.deviation": "Deviation",
|
||||
"machine.detail.kpi.updated": "Updated {time}",
|
||||
"machine.detail.currentWorkOrder": "Current Work Order",
|
||||
"machine.detail.recentEvents": "Critical Events",
|
||||
"machine.detail.noEvents": "No events yet.",
|
||||
"machine.detail.cycleTarget": "Cycle target",
|
||||
"machine.detail.mini.events": "Detected Events",
|
||||
"machine.detail.mini.events.subtitle": "Canonical events (all)",
|
||||
"machine.detail.mini.deviation": "Actual vs Standard Cycle",
|
||||
"machine.detail.mini.deviation.subtitle": "Average deviation",
|
||||
"machine.detail.mini.impact": "Production Impact",
|
||||
"machine.detail.mini.impact.subtitle": "Extra time vs ideal",
|
||||
"machine.detail.modal.events": "Detected Events",
|
||||
"machine.detail.modal.deviation": "Actual vs Standard Cycle",
|
||||
"machine.detail.modal.impact": "Production Impact",
|
||||
"machine.detail.modal.standardCycle": "Standard cycle (ideal)",
|
||||
"machine.detail.modal.avgDeviation": "Average deviation",
|
||||
"machine.detail.modal.sample": "Sample",
|
||||
"machine.detail.modal.cycles": "cycles",
|
||||
"machine.detail.modal.tip": "Tip: the faint line is the ideal. Each point is a real cycle.",
|
||||
"machine.detail.modal.totalExtra": "Total extra time",
|
||||
"machine.detail.modal.microstops": "Microstops",
|
||||
"machine.detail.modal.macroStops": "Macro stops",
|
||||
"machine.detail.modal.extraTimeLabel": "Extra time",
|
||||
"machine.detail.modal.extraTimeNote": "This is \"lost time\" vs ideal, distributed by event type.",
|
||||
"reports.title": "Reports",
|
||||
"reports.subtitle": "Trends, downtime, and quality analytics across machines.",
|
||||
"reports.exportCsv": "Export CSV",
|
||||
"reports.exportPdf": "Export PDF",
|
||||
"reports.filters": "Filters",
|
||||
"reports.rangeLabel.last24": "Last 24 hours",
|
||||
"reports.rangeLabel.last7": "Last 7 days",
|
||||
"reports.rangeLabel.last30": "Last 30 days",
|
||||
"reports.rangeLabel.custom": "Custom range",
|
||||
"reports.filter.range": "Range",
|
||||
"reports.filter.machine": "Machine",
|
||||
"reports.filter.workOrder": "Work Order",
|
||||
"reports.filter.sku": "SKU",
|
||||
"reports.filter.allMachines": "All machines",
|
||||
"reports.filter.allWorkOrders": "All work orders",
|
||||
"reports.filter.allSkus": "All SKUs",
|
||||
"reports.loading": "Loading reports...",
|
||||
"reports.error.failed": "Failed to load reports",
|
||||
"reports.error.network": "Network error",
|
||||
"reports.kpi.note.withData": "Computed from KPI snapshots.",
|
||||
"reports.kpi.note.noData": "No data in selected range.",
|
||||
"reports.oeeTrend": "OEE Trend",
|
||||
"reports.downtimePareto": "Downtime Pareto",
|
||||
"reports.cycleDistribution": "Cycle Time Distribution",
|
||||
"reports.scrapTrend": "Scrap Trend",
|
||||
"reports.topLossDrivers": "Top Loss Drivers",
|
||||
"reports.qualitySummary": "Quality Summary",
|
||||
"reports.notes": "Notes for Ops",
|
||||
"alerts.title": "Alerts",
|
||||
"alerts.subtitle": "Alert history with filters and drilldowns.",
|
||||
"alerts.comingSoon": "Alert configuration UI is coming soon.",
|
||||
"alerts.loading": "Loading alerts...",
|
||||
"alerts.error.loadPolicy": "Failed to load alert policy.",
|
||||
"alerts.error.savePolicy": "Failed to save alert policy.",
|
||||
"alerts.error.loadContacts": "Failed to load alert contacts.",
|
||||
"alerts.error.saveContacts": "Failed to save alert contact.",
|
||||
"alerts.error.deleteContact": "Failed to delete alert contact.",
|
||||
"alerts.error.createContact": "Failed to create alert contact.",
|
||||
"alerts.policy.title": "Alert policy",
|
||||
"alerts.policy.subtitle": "Configure escalation by role, channel, and duration.",
|
||||
"alerts.policy.save": "Save policy",
|
||||
"alerts.policy.saving": "Saving...",
|
||||
"alerts.policy.defaults": "Default escalation (per role)",
|
||||
"alerts.policy.enabled": "Enabled",
|
||||
"alerts.policy.afterMinutes": "After minutes",
|
||||
"alerts.policy.channels": "Channels",
|
||||
"alerts.policy.repeatMinutes": "Repeat (min)",
|
||||
"alerts.policy.readOnly": "You can view alert policy settings, but only owners can edit.",
|
||||
"alerts.policy.defaultsHelp": "Defaults apply when a specific event is reset or not customized.",
|
||||
"alerts.policy.eventSelectLabel": "Event type",
|
||||
"alerts.policy.eventSelectHelper": "Adjust escalation rules for a single event type.",
|
||||
"alerts.policy.applyDefaults": "Apply defaults",
|
||||
"alerts.event.macrostop": "Macrostop",
|
||||
"alerts.event.microstop": "Microstop",
|
||||
"alerts.event.slow-cycle": "Slow cycle",
|
||||
"alerts.event.offline": "Offline",
|
||||
"alerts.event.error": "Error",
|
||||
"alerts.contacts.title": "Alert contacts",
|
||||
"alerts.contacts.subtitle": "External recipients and role targeting.",
|
||||
"alerts.contacts.name": "Name",
|
||||
"alerts.contacts.roleScope": "Role scope",
|
||||
"alerts.contacts.email": "Email",
|
||||
"alerts.contacts.phone": "Phone",
|
||||
"alerts.contacts.eventTypes": "Event types (optional)",
|
||||
"alerts.contacts.eventTypesPlaceholder": "macrostop, microstop, offline",
|
||||
"alerts.contacts.eventTypesHelper": "Leave empty to receive all event types.",
|
||||
"alerts.contacts.add": "Add contact",
|
||||
"alerts.contacts.creating": "Adding...",
|
||||
"alerts.contacts.empty": "No alert contacts yet.",
|
||||
"alerts.contacts.save": "Save",
|
||||
"alerts.contacts.saving": "Saving...",
|
||||
"alerts.contacts.delete": "Delete",
|
||||
"alerts.contacts.deleting": "Deleting...",
|
||||
"alerts.contacts.active": "Active",
|
||||
"alerts.contacts.linkedUser": "Linked user (edit in profile)",
|
||||
"alerts.contacts.role.custom": "Custom",
|
||||
"alerts.contacts.role.member": "Member",
|
||||
"alerts.contacts.role.admin": "Admin",
|
||||
"alerts.contacts.role.owner": "Owner",
|
||||
"alerts.contacts.readOnly": "You can view contacts, but only owners can add or edit.",
|
||||
"alerts.inbox.title": "Alerts Inbox",
|
||||
"alerts.inbox.loading": "Loading alerts...",
|
||||
"alerts.inbox.loadingFilters": "Loading filters...",
|
||||
"alerts.inbox.empty": "No alerts found.",
|
||||
"alerts.inbox.error": "Failed to load alerts.",
|
||||
"alerts.inbox.range.24h": "Last 24 hours",
|
||||
"alerts.inbox.range.7d": "Last 7 days",
|
||||
"alerts.inbox.range.30d": "Last 30 days",
|
||||
"alerts.inbox.range.custom": "Custom",
|
||||
"alerts.inbox.filters.title": "Filters",
|
||||
"alerts.inbox.filters.range": "Range",
|
||||
"alerts.inbox.filters.start": "Start",
|
||||
"alerts.inbox.filters.end": "End",
|
||||
"alerts.inbox.filters.machine": "Machine",
|
||||
"alerts.inbox.filters.site": "Site",
|
||||
"alerts.inbox.filters.shift": "Shift",
|
||||
"alerts.inbox.filters.type": "Classification",
|
||||
"alerts.inbox.filters.severity": "Severity",
|
||||
"alerts.inbox.filters.status": "Status",
|
||||
"alerts.inbox.filters.search": "Search",
|
||||
"alerts.inbox.filters.searchPlaceholder": "Title, description, machine...",
|
||||
"alerts.inbox.filters.includeUpdates": "Include updates",
|
||||
"alerts.inbox.filters.allMachines": "All machines",
|
||||
"alerts.inbox.filters.allSites": "All sites",
|
||||
"alerts.inbox.filters.allShifts": "All shifts",
|
||||
"alerts.inbox.filters.allTypes": "All types",
|
||||
"alerts.inbox.filters.allSeverities": "All severities",
|
||||
"alerts.inbox.filters.allStatuses": "All statuses",
|
||||
"alerts.inbox.table.time": "Time",
|
||||
"alerts.inbox.table.machine": "Machine",
|
||||
"alerts.inbox.table.site": "Site",
|
||||
"alerts.inbox.table.shift": "Shift",
|
||||
"alerts.inbox.table.type": "Type",
|
||||
"alerts.inbox.table.severity": "Severity",
|
||||
"alerts.inbox.table.status": "Status",
|
||||
"alerts.inbox.table.duration": "Duration",
|
||||
"alerts.inbox.table.title": "Title",
|
||||
"alerts.inbox.table.unknown": "Unknown",
|
||||
"alerts.inbox.status.active": "Active",
|
||||
"alerts.inbox.status.resolved": "Resolved",
|
||||
"alerts.inbox.status.unknown": "Unknown",
|
||||
"alerts.inbox.duration.na": "n/a",
|
||||
"alerts.inbox.duration.sec": "s",
|
||||
"alerts.inbox.duration.min": " min",
|
||||
"alerts.inbox.duration.hr": " h",
|
||||
"alerts.inbox.meta.workOrder": "WO",
|
||||
"alerts.inbox.meta.sku": "SKU",
|
||||
"reports.notes.suggested": "Suggested actions",
|
||||
"reports.notes.none": "No insights yet. Generate reports after data collection.",
|
||||
"reports.noTrend": "No trend data yet.",
|
||||
"reports.noDowntime": "No downtime data yet.",
|
||||
"reports.noCycle": "No cycle data yet.",
|
||||
"reports.scrapRate": "Scrap Rate",
|
||||
"reports.topScrapSku": "Top Scrap SKU",
|
||||
"reports.topScrapWorkOrder": "Top Scrap Work Order",
|
||||
"reports.loss.macrostop": "Macrostop",
|
||||
"reports.loss.microstop": "Microstop",
|
||||
"reports.loss.slowCycle": "Slow Cycle",
|
||||
"reports.loss.qualitySpike": "Quality Spike",
|
||||
"reports.loss.oeeDrop": "OEE Drop",
|
||||
"reports.loss.perfDegradation": "Perf Degradation",
|
||||
"reports.tooltip.cycles": "Cycles",
|
||||
"reports.tooltip.range": "Range",
|
||||
"reports.tooltip.below": "Below",
|
||||
"reports.tooltip.above": "Above",
|
||||
"reports.tooltip.extremes": "Extremes",
|
||||
"reports.tooltip.downtime": "Downtime",
|
||||
"reports.tooltip.extraTime": "Extra time",
|
||||
"reports.csv.section": "section",
|
||||
"reports.csv.key": "key",
|
||||
"reports.csv.value": "value",
|
||||
"reports.pdf.title": "Report Export",
|
||||
"reports.pdf.range": "Range",
|
||||
"reports.pdf.machine": "Machine",
|
||||
"reports.pdf.workOrder": "Work Order",
|
||||
"reports.pdf.sku": "SKU",
|
||||
"reports.pdf.metric": "Metric",
|
||||
"reports.pdf.value": "Value",
|
||||
"reports.pdf.topLoss": "Top Loss Drivers",
|
||||
"reports.pdf.qualitySummary": "Quality Summary",
|
||||
"reports.pdf.cycleDistribution": "Cycle Time Distribution",
|
||||
"reports.pdf.notes": "Notes for Ops",
|
||||
"reports.pdf.none": "None",
|
||||
"settings.title": "Settings",
|
||||
"settings.subtitle": "Live configuration for shifts, alerts, and defaults.",
|
||||
"settings.tabs.general": "General",
|
||||
"settings.tabs.shifts": "Shifts",
|
||||
"settings.tabs.thresholds": "Thresholds",
|
||||
"settings.tabs.alerts": "Alerts",
|
||||
"settings.tabs.financial": "Financial",
|
||||
"settings.tabs.team": "Team",
|
||||
"settings.loading": "Loading settings...",
|
||||
"settings.loadingTeam": "Loading team...",
|
||||
"settings.refresh": "Refresh",
|
||||
"settings.save": "Save changes",
|
||||
"settings.saving": "Saving...",
|
||||
"settings.saved": "Settings saved",
|
||||
"settings.failedLoad": "Failed to load settings",
|
||||
"settings.failedTeam": "Failed to load team",
|
||||
"settings.failedSave": "Failed to save settings",
|
||||
"settings.unavailable": "Settings are unavailable.",
|
||||
"settings.conflict": "Settings changed elsewhere. Refresh and try again.",
|
||||
"settings.org.title": "Organization",
|
||||
"settings.org.plantName": "Plant Name",
|
||||
"settings.org.slug": "Slug",
|
||||
"settings.org.timeZone": "Time Zone",
|
||||
"settings.shiftSchedule": "Shift Schedule",
|
||||
"settings.shiftSubtitle": "Define active shifts and downtime compensation.",
|
||||
"settings.shiftName": "Shift name",
|
||||
"settings.shiftStart": "Start",
|
||||
"settings.shiftEnd": "End",
|
||||
"settings.shiftEnabled": "Enabled",
|
||||
"settings.shiftAdd": "Add shift",
|
||||
"settings.shiftRemove": "Remove",
|
||||
"settings.shiftComp": "Shift change compensation",
|
||||
"settings.lunchBreak": "Lunch break",
|
||||
"settings.minutes": "minutes",
|
||||
"settings.shiftHint": "Max 3 shifts, HH:mm",
|
||||
"settings.shiftTo": "to",
|
||||
"settings.shiftCompLabel": "Shift change compensation (min)",
|
||||
"settings.lunchBreakLabel": "Lunch break (min)",
|
||||
"settings.shift.defaultName": "Shift {index}",
|
||||
"settings.thresholds": "Alert thresholds",
|
||||
"settings.thresholdsSubtitle": "Tune production health alerts.",
|
||||
"settings.thresholds.appliesAll": "Applies to all machines",
|
||||
"settings.thresholds.oee": "OEE alert threshold",
|
||||
"settings.thresholds.performance": "Performance threshold",
|
||||
"settings.thresholds.qualitySpike": "Quality spike delta",
|
||||
"settings.thresholds.stoppage": "Stoppage multiplier",
|
||||
"settings.thresholds.macroStoppage": "Macro stoppage multiplier",
|
||||
"settings.alerts": "Alerts",
|
||||
"settings.alertsSubtitle": "Choose which alerts to notify.",
|
||||
"settings.alerts.oeeDrop": "OEE drop alerts",
|
||||
"settings.alerts.oeeDropHelper": "Notify when OEE falls below threshold",
|
||||
"settings.alerts.performanceDegradation": "Performance degradation alerts",
|
||||
"settings.alerts.performanceDegradationHelper": "Flag prolonged slow cycles",
|
||||
"settings.alerts.qualitySpike": "Quality spike alerts",
|
||||
"settings.alerts.qualitySpikeHelper": "Alert on scrap spikes",
|
||||
"settings.alerts.predictive": "Predictive OEE decline alerts",
|
||||
"settings.alerts.predictiveHelper": "Warn before OEE drops",
|
||||
"settings.defaults": "Mold Defaults",
|
||||
"settings.defaults.moldTotal": "Total molds",
|
||||
"settings.defaults.moldActive": "Active molds",
|
||||
"settings.updated": "Updated",
|
||||
"settings.updatedBy": "Updated by",
|
||||
"settings.team": "Team Members",
|
||||
"settings.teamTotal": "{count} total",
|
||||
"settings.teamNone": "No team members yet.",
|
||||
"settings.invites": "Invitations",
|
||||
"settings.inviteEmail": "Invite email",
|
||||
"settings.inviteRole": "Role",
|
||||
"settings.inviteSend": "Create invite",
|
||||
"settings.inviteSending": "Creating...",
|
||||
"settings.inviteStatus.copied": "Invite link copied",
|
||||
"settings.inviteStatus.emailRequired": "Email is required",
|
||||
"settings.inviteStatus.failed": "Failed to revoke invite",
|
||||
"settings.inviteStatus.sent": "Invite email sent",
|
||||
"settings.inviteStatus.createFailed": "Failed to create invite",
|
||||
"settings.inviteStatus.emailFailed": "Invite created, email failed: {url}",
|
||||
"settings.inviteNone": "No pending invites.",
|
||||
"settings.inviteExpires": "Expires {date}",
|
||||
"settings.inviteRole.member": "Member",
|
||||
"settings.inviteRole.admin": "Admin",
|
||||
"settings.inviteRole.owner": "Owner",
|
||||
"settings.inviteCopy": "Copy link",
|
||||
"settings.inviteRevoke": "Revoke",
|
||||
"settings.role.owner": "Owner",
|
||||
"settings.role.admin": "Admin",
|
||||
"settings.role.member": "Member",
|
||||
"settings.role.inactive": "Inactive",
|
||||
"settings.integrations": "Integrations",
|
||||
"settings.integrations.webhook": "Webhook URL",
|
||||
"settings.integrations.erp": "ERP Sync",
|
||||
"settings.integrations.erpNotConfigured": "Not configured",
|
||||
"financial.title": "Financial Impact",
|
||||
"financial.subtitle": "Translate downtime, slow cycles, and scrap into money.",
|
||||
"financial.ownerOnly": "Financial impact is available only to owners.",
|
||||
"financial.costsMoved": "Cost settings are now in",
|
||||
"financial.costsMovedLink": "Settings -> Financial",
|
||||
"financial.export.html": "HTML",
|
||||
"financial.export.csv": "CSV",
|
||||
"financial.totalLoss": "Total Loss",
|
||||
"financial.currencyLabel": "Currency: {currency}",
|
||||
"financial.noImpact": "No impact data yet.",
|
||||
"financial.chart.title": "Lost Money Over Time",
|
||||
"financial.chart.subtitle": "Stacked by event type",
|
||||
"financial.range.day": "Day",
|
||||
"financial.range.week": "Week",
|
||||
"financial.range.month": "Month",
|
||||
"financial.filters.title": "Filters",
|
||||
"financial.filters.machine": "Machine",
|
||||
"financial.filters.location": "Location",
|
||||
"financial.filters.sku": "SKU",
|
||||
"financial.filters.currency": "Currency",
|
||||
"financial.filters.allMachines": "All machines",
|
||||
"financial.filters.allLocations": "All locations",
|
||||
"financial.filters.skuPlaceholder": "Filter by SKU",
|
||||
"financial.filters.currencyPlaceholder": "MXN",
|
||||
"financial.loadingMachines": "Loading machines...",
|
||||
"financial.config.title": "Cost Parameters",
|
||||
"financial.config.subtitle": "Defaults apply to all machines unless overridden.",
|
||||
"financial.config.applyOrg": "Apply org defaults to all machines",
|
||||
"financial.config.save": "Save",
|
||||
"financial.config.saving": "Saving...",
|
||||
"financial.config.saved": "Saved",
|
||||
"financial.config.saveFailed": "Save failed",
|
||||
"financial.config.orgDefaults": "Org Defaults",
|
||||
"financial.config.locationOverrides": "Location Overrides",
|
||||
"financial.config.machineOverrides": "Machine Overrides",
|
||||
"financial.config.productOverrides": "Product Overrides",
|
||||
"financial.config.addLocation": "Add location override",
|
||||
"financial.config.addMachine": "Add machine override",
|
||||
"financial.config.addProduct": "Add product override",
|
||||
"financial.config.noneLocation": "No location overrides yet.",
|
||||
"financial.config.noneMachine": "No machine overrides yet.",
|
||||
"financial.config.noneProduct": "No product overrides yet.",
|
||||
"financial.config.location": "Location",
|
||||
"financial.config.selectLocation": "Select location",
|
||||
"financial.config.machine": "Machine",
|
||||
"financial.config.selectMachine": "Select machine",
|
||||
"financial.config.currency": "Currency",
|
||||
"financial.config.sku": "SKU",
|
||||
"financial.config.rawMaterialUnit": "Raw material / unit",
|
||||
"financial.config.ownerOnly": "Financial cost settings are available only to owners.",
|
||||
"financial.config.loading": "Loading financials...",
|
||||
"financial.field.machineCostPerMin": "Machine cost / min",
|
||||
"financial.field.operatorCostPerMin": "Operator cost / min",
|
||||
"financial.field.ratedRunningKw": "Running kW",
|
||||
"financial.field.idleKw": "Idle kW",
|
||||
"financial.field.kwhRate": "kWh rate",
|
||||
"financial.field.energyMultiplier": "Energy multiplier",
|
||||
"financial.field.energyCostPerMin": "Energy cost / min",
|
||||
"financial.field.scrapCostPerUnit": "Scrap cost / unit",
|
||||
"financial.field.rawMaterialCostPerUnit": "Raw material / unit",
|
||||
"nav.downtime": "Downtime",
|
||||
"settings.tabs.modules": "Modules",
|
||||
"settings.modules.title": "Modules",
|
||||
"settings.modules.subtitle": "Enable/disable UI modules depending on how the plant operates.",
|
||||
"settings.modules.screenless.title": "Screenless mode",
|
||||
"settings.modules.screenless.helper": "Hide the Downtime module from navigation (for plants without Node-RED reason capture).",
|
||||
"settings.modules.note": "This setting is org-wide."
|
||||
}
|
||||
520
lib/i18n/es-MX.json
Normal file
520
lib/i18n/es-MX.json
Normal file
@@ -0,0 +1,520 @@
|
||||
{
|
||||
"---": "---",
|
||||
"common.loading": "Cargando...",
|
||||
"common.loadingShort": "Cargando",
|
||||
"common.never": "nunca",
|
||||
"common.na": "--",
|
||||
"common.back": "Volver",
|
||||
"common.cancel": "Cancelar",
|
||||
"common.close": "Cerrar",
|
||||
"common.save": "Guardar",
|
||||
"common.copy": "Copiar",
|
||||
"nav.overview": "Resumen",
|
||||
"nav.machines": "Máquinas",
|
||||
"nav.reports": "Reportes",
|
||||
"nav.alerts": "Alertas",
|
||||
"nav.financial": "Finanzas",
|
||||
"nav.settings": "Configuración",
|
||||
"sidebar.productTitle": "MIS",
|
||||
"sidebar.productSubtitle": "Control Tower",
|
||||
"sidebar.userFallback": "Usuario",
|
||||
"sidebar.loadingOrg": "Cargando...",
|
||||
"sidebar.themeTooltip": "Tema e idioma",
|
||||
"sidebar.switchToDark": "Cambiar a modo oscuro",
|
||||
"sidebar.switchToLight": "Cambiar a modo claro",
|
||||
"sidebar.logout": "Cerrar sesión",
|
||||
"sidebar.role.member": "MIEMBRO",
|
||||
"sidebar.role.admin": "ADMIN",
|
||||
"sidebar.role.owner": "PROPIETARIO",
|
||||
"login.title": "Control Tower",
|
||||
"login.subtitle": "Inicia sesión en tu organización",
|
||||
"login.email": "Correo electrónico",
|
||||
"login.password": "Contraseña",
|
||||
"login.error.default": "Inicio de sesión fallido",
|
||||
"login.error.network": "Error de red",
|
||||
"login.submit.loading": "Iniciando sesión...",
|
||||
"login.submit.default": "Iniciar sesión",
|
||||
"login.newHere": "¿Nuevo aquí?",
|
||||
"login.createAccount": "Crear cuenta",
|
||||
"signup.verify.title": "Verifica tu correo",
|
||||
"signup.verify.sent": "Enviamos un enlace de verificación a {email}.",
|
||||
"signup.verify.failed": "No se pudo enviar el correo de verificación. Contacta a soporte.",
|
||||
"signup.verify.notice": "Después de verificar, puedes iniciar sesión e invitar a tu equipo.",
|
||||
"signup.verify.back": "Volver al inicio de sesión",
|
||||
"signup.title": "Crea tu Control Tower",
|
||||
"signup.subtitle": "Configura tu organización e invita al equipo.",
|
||||
"signup.orgName": "Nombre de la organización",
|
||||
"signup.yourName": "Tu nombre",
|
||||
"signup.email": "Correo electrónico",
|
||||
"signup.password": "Contraseña",
|
||||
"signup.error.default": "Registro fallido",
|
||||
"signup.error.network": "Error de red",
|
||||
"signup.submit.loading": "Creando cuenta...",
|
||||
"signup.submit.default": "Crear cuenta",
|
||||
"signup.alreadyHave": "¿Ya tienes acceso?",
|
||||
"signup.signIn": "Iniciar sesión",
|
||||
"invite.loading": "Cargando invitación...",
|
||||
"invite.notFound": "Invitación no encontrada.",
|
||||
"invite.joinTitle": "Únete a {org}",
|
||||
"invite.acceptCopy": "Acepta la invitación para {email} como {role}.",
|
||||
"invite.yourName": "Tu nombre",
|
||||
"invite.password": "Contraseña",
|
||||
"invite.error.notFound": "Invitación no encontrada",
|
||||
"invite.error.acceptFailed": "No se pudo aceptar la invitación",
|
||||
"invite.submit.loading": "Uniéndote...",
|
||||
"invite.submit.default": "Unirse a la organización",
|
||||
"overview.title": "Resumen",
|
||||
"overview.subtitle": "Pulso de flota, alertas y elementos prioritarios.",
|
||||
"overview.viewMachines": "Ver Máquinas",
|
||||
"overview.loading": "Cargando resumen...",
|
||||
"overview.fleetHealth": "Salud de flota",
|
||||
"overview.machinesTotal": "Máquinas totales",
|
||||
"overview.online": "En línea",
|
||||
"overview.offline": "Fuera de línea",
|
||||
"overview.run": "En marcha",
|
||||
"overview.idle": "En espera",
|
||||
"overview.stop": "Paro",
|
||||
"overview.productionTotals": "Totales de producción",
|
||||
"overview.good": "Buenas",
|
||||
"overview.scrap": "Scrap",
|
||||
"overview.target": "Meta",
|
||||
"overview.kpiSumNote": "Suma de los últimos KPIs por máquina.",
|
||||
"overview.activityFeed": "Actividad",
|
||||
"overview.eventsRefreshing": "Actualizando eventos recientes...",
|
||||
"overview.eventsLast30": "Últimos 30 eventos combinados",
|
||||
"overview.eventsNone": "Sin eventos recientes.",
|
||||
"overview.oeeAvg": "OEE (avg)",
|
||||
"overview.availabilityAvg": "Availability (avg)",
|
||||
"overview.performanceAvg": "Performance (avg)",
|
||||
"overview.qualityAvg": "Quality (avg)",
|
||||
"overview.attentionList": "Lista de atención",
|
||||
"overview.shown": "mostrados",
|
||||
"overview.noUrgent": "No se detectaron problemas urgentes.",
|
||||
"overview.timeline": "Línea de tiempo unificada",
|
||||
"overview.items": "elementos",
|
||||
"overview.noEvents": "Sin eventos aún.",
|
||||
"overview.ack": "ACK",
|
||||
"overview.severity.critical": "CRÍTICO",
|
||||
"overview.severity.warning": "ADVERTENCIA",
|
||||
"overview.severity.info": "INFO",
|
||||
"overview.source.ingested": "ingestado",
|
||||
"overview.source.derived": "derivado",
|
||||
"overview.event.macrostop": "macroparo",
|
||||
"overview.event.microstop": "microparo",
|
||||
"overview.event.slow-cycle": "ciclo lento",
|
||||
"overview.status.offline": "FUERA DE LÍNEA",
|
||||
"overview.status.online": "EN LÍNEA",
|
||||
"machines.title": "Máquinas",
|
||||
"machines.subtitle": "Selecciona una máquina para ver KPIs en vivo.",
|
||||
"machines.cancel": "Cancelar",
|
||||
"machines.addMachine": "Agregar máquina",
|
||||
"machines.backOverview": "Volver al Resumen",
|
||||
"machines.addCardTitle": "Agregar máquina",
|
||||
"machines.addCardSubtitle": "Genera el ID de máquina y la API key para tu edge Node-RED.",
|
||||
"machines.field.name": "Nombre de la máquina",
|
||||
"machines.field.code": "Código (opcional)",
|
||||
"machines.field.location": "Ubicación (opcional)",
|
||||
"machines.create.loading": "Creando...",
|
||||
"machines.create.default": "Crear máquina",
|
||||
"machines.create.error.nameRequired": "El nombre de la máquina es obligatorio",
|
||||
"machines.create.error.failed": "No se pudo crear la máquina",
|
||||
"machines.delete": "Eliminar",
|
||||
"machines.delete.loading": "Eliminando...",
|
||||
"machines.delete.confirm": "¿Eliminar {name}? Esto borrará la máquina y sus datos.",
|
||||
"machines.delete.error.failed": "No se pudo eliminar la máquina",
|
||||
"machines.pairing.title": "Código de emparejamiento",
|
||||
"machines.pairing.machine": "Máquina:",
|
||||
"machines.pairing.codeLabel": "Código de emparejamiento",
|
||||
"machines.pairing.expires": "Expira",
|
||||
"machines.pairing.soon": "pronto",
|
||||
"machines.pairing.instructions": "Ingresa este código en la pantalla de configuración de Node-RED Control Tower para vincular el dispositivo.",
|
||||
"machines.pairing.copy": "Copiar código",
|
||||
"machines.pairing.copied": "Copiado",
|
||||
"machines.pairing.copyUnsupported": "Copiar no disponible",
|
||||
"machines.pairing.copyFailed": "Falló la copia",
|
||||
"machines.loading": "Cargando máquinas...",
|
||||
"machines.empty": "No se encontraron máquinas para esta organización.",
|
||||
"machines.status": "Estado",
|
||||
"machines.status.noHeartbeat": "Sin heartbeat",
|
||||
"machines.status.ok": "Latido",
|
||||
"machines.status.offline": "FUERA DE LÍNEA",
|
||||
"machines.status.unknown": "DESCONOCIDO",
|
||||
"machines.lastSeen": "Visto hace {time}",
|
||||
"machine.detail.titleFallback": "Máquina",
|
||||
"machine.detail.lastSeen": "Visto hace {time}",
|
||||
"machine.detail.loading": "Cargando...",
|
||||
"machine.detail.error.failed": "No se pudo cargar la máquina",
|
||||
"machine.detail.error.network": "Error de red",
|
||||
"machine.detail.back": "Volver",
|
||||
"machine.detail.workOrders.upload": "Subir ordenes de trabajo",
|
||||
"machine.detail.workOrders.uploading": "Subiendo...",
|
||||
"machine.detail.workOrders.uploadParsing": "Leyendo archivo...",
|
||||
"machine.detail.workOrders.uploadHint": "CSV o XLSX con Work Order ID, SKU, Theoretical Cycle Time (Seconds), Target Quantity.",
|
||||
"machine.detail.workOrders.uploadSuccess": "Se cargaron {count} ordenes de trabajo",
|
||||
"machine.detail.workOrders.uploadError": "No se pudo cargar",
|
||||
"machine.detail.workOrders.uploadInvalid": "No se encontraron ordenes de trabajo validas",
|
||||
"machine.detail.workOrders.uploadUnauthorized": "No autorizado para cargar ordenes de trabajo",
|
||||
"machine.detail.status.offline": "FUERA DE LÍNEA",
|
||||
"machine.detail.status.unknown": "DESCONOCIDO",
|
||||
"machine.detail.status.run": "EN MARCHA",
|
||||
"machine.detail.status.idle": "EN ESPERA",
|
||||
"machine.detail.status.stop": "PARO",
|
||||
"machine.detail.status.down": "CAÍDA",
|
||||
"machine.detail.bucket.normal": "Ciclo normal",
|
||||
"machine.detail.bucket.slow": "Ciclo lento",
|
||||
"machine.detail.bucket.microstop": "Microparo",
|
||||
"machine.detail.bucket.macrostop": "Macroparo",
|
||||
"machine.detail.bucket.unknown": "Desconocido",
|
||||
"machine.detail.activity.title": "Línea de tiempo de actividad",
|
||||
"machine.detail.activity.subtitle": "Análisis en tiempo real de ciclos de producción",
|
||||
"machine.detail.activity.noData": "Sin datos de línea de tiempo.",
|
||||
"machine.detail.tooltip.cycle": "Ciclo: {label}",
|
||||
"machine.detail.tooltip.duration": "Duración",
|
||||
"machine.detail.tooltip.ideal": "Ideal",
|
||||
"machine.detail.tooltip.deviation": "Desviación",
|
||||
"machine.detail.kpi.updated": "Actualizado {time}",
|
||||
"machine.detail.currentWorkOrder": "Orden de trabajo actual",
|
||||
"machine.detail.recentEvents": "Eventos críticos",
|
||||
"machine.detail.noEvents": "Sin eventos aún.",
|
||||
"machine.detail.cycleTarget": "Ciclo objetivo",
|
||||
"machine.detail.mini.events": "Eventos detectados",
|
||||
"machine.detail.mini.events.subtitle": "Eventos canónicos (todos)",
|
||||
"machine.detail.mini.deviation": "Ciclo real vs estándar",
|
||||
"machine.detail.mini.deviation.subtitle": "Desviación promedio",
|
||||
"machine.detail.mini.impact": "Impacto en producción",
|
||||
"machine.detail.mini.impact.subtitle": "Tiempo extra vs ideal",
|
||||
"machine.detail.modal.events": "Eventos detectados",
|
||||
"machine.detail.modal.deviation": "Ciclo real vs estándar",
|
||||
"machine.detail.modal.impact": "Impacto en producción",
|
||||
"machine.detail.modal.standardCycle": "Ciclo estándar (ideal)",
|
||||
"machine.detail.modal.avgDeviation": "Desviación promedio",
|
||||
"machine.detail.modal.sample": "Muestra",
|
||||
"machine.detail.modal.cycles": "ciclos",
|
||||
"machine.detail.modal.tip": "Tip: la línea tenue es el ideal. Cada punto es un ciclo real.",
|
||||
"machine.detail.modal.totalExtra": "Tiempo extra total",
|
||||
"machine.detail.modal.microstops": "Microparos",
|
||||
"machine.detail.modal.macroStops": "Macroparos",
|
||||
"machine.detail.modal.extraTimeLabel": "Tiempo extra",
|
||||
"machine.detail.modal.extraTimeNote": "Esto es \"tiempo perdido\" vs ideal, distribuido por tipo de evento.",
|
||||
"reports.title": "Reportes",
|
||||
"reports.subtitle": "Tendencias, paros y analítica de calidad por máquina.",
|
||||
"reports.exportCsv": "Exportar CSV",
|
||||
"reports.exportPdf": "Exportar PDF",
|
||||
"reports.filters": "Filtros",
|
||||
"reports.rangeLabel.last24": "Últimas 24 horas",
|
||||
"reports.rangeLabel.last7": "Últimos 7 días",
|
||||
"reports.rangeLabel.last30": "Últimos 30 días",
|
||||
"reports.rangeLabel.custom": "Rango personalizado",
|
||||
"reports.filter.range": "Rango",
|
||||
"reports.filter.machine": "Máquina",
|
||||
"reports.filter.workOrder": "Orden de trabajo",
|
||||
"reports.filter.sku": "SKU",
|
||||
"reports.filter.allMachines": "Todas las máquinas",
|
||||
"reports.filter.allWorkOrders": "Todas las órdenes",
|
||||
"reports.filter.allSkus": "Todos los SKUs",
|
||||
"reports.loading": "Cargando reportes...",
|
||||
"reports.error.failed": "No se pudieron cargar los reportes",
|
||||
"reports.error.network": "Error de red",
|
||||
"reports.kpi.note.withData": "Calculado a partir de KPIs.",
|
||||
"reports.kpi.note.noData": "Sin datos en el rango seleccionado.",
|
||||
"reports.oeeTrend": "Tendencia de OEE",
|
||||
"reports.downtimePareto": "Pareto de paros",
|
||||
"reports.cycleDistribution": "Distribución de tiempos de ciclo",
|
||||
"reports.scrapTrend": "Tendencia de scrap",
|
||||
"reports.topLossDrivers": "Principales causas de pérdida",
|
||||
"reports.qualitySummary": "Resumen de calidad",
|
||||
"reports.notes": "Notas para operaciones",
|
||||
"alerts.title": "Alertas",
|
||||
"alerts.subtitle": "Historial de alertas con filtros y detalle.",
|
||||
"alerts.comingSoon": "La configuracion de alertas estara disponible pronto.",
|
||||
"alerts.loading": "Cargando alertas...",
|
||||
"alerts.error.loadPolicy": "No se pudo cargar la politica de alertas.",
|
||||
"alerts.error.savePolicy": "No se pudo guardar la politica de alertas.",
|
||||
"alerts.error.loadContacts": "No se pudieron cargar los contactos de alertas.",
|
||||
"alerts.error.saveContacts": "No se pudo guardar el contacto de alertas.",
|
||||
"alerts.error.deleteContact": "No se pudo eliminar el contacto de alertas.",
|
||||
"alerts.error.createContact": "No se pudo crear el contacto de alertas.",
|
||||
"alerts.policy.title": "Politica de alertas",
|
||||
"alerts.policy.subtitle": "Configura escalamiento por rol, canal y duracion.",
|
||||
"alerts.policy.save": "Guardar politica",
|
||||
"alerts.policy.saving": "Guardando...",
|
||||
"alerts.policy.defaults": "Escalamiento por defecto (por rol)",
|
||||
"alerts.policy.enabled": "Habilitado",
|
||||
"alerts.policy.afterMinutes": "Despues de minutos",
|
||||
"alerts.policy.channels": "Canales",
|
||||
"alerts.policy.repeatMinutes": "Repetir (min)",
|
||||
"alerts.policy.readOnly": "Puedes ver la politica de alertas, pero solo propietarios pueden editar.",
|
||||
"alerts.policy.defaultsHelp": "Los valores por defecto aplican cuando un evento se reinicia o no se personaliza.",
|
||||
"alerts.policy.eventSelectLabel": "Tipo de evento",
|
||||
"alerts.policy.eventSelectHelper": "Ajusta escalamiento para un solo tipo de evento.",
|
||||
"alerts.policy.applyDefaults": "Aplicar por defecto",
|
||||
"alerts.event.macrostop": "Macroparo",
|
||||
"alerts.event.microstop": "Microparo",
|
||||
"alerts.event.slow-cycle": "Ciclo lento",
|
||||
"alerts.event.offline": "Fuera de linea",
|
||||
"alerts.event.error": "Error",
|
||||
"alerts.contacts.title": "Contactos de alertas",
|
||||
"alerts.contacts.subtitle": "Destinatarios externos y alcance por rol.",
|
||||
"alerts.contacts.name": "Nombre",
|
||||
"alerts.contacts.roleScope": "Rol",
|
||||
"alerts.contacts.email": "Correo",
|
||||
"alerts.contacts.phone": "Telefono",
|
||||
"alerts.contacts.eventTypes": "Tipos de evento (opcional)",
|
||||
"alerts.contacts.eventTypesPlaceholder": "macroparo, microparo, fuera-de-linea",
|
||||
"alerts.contacts.eventTypesHelper": "Deja vacío para recibir todos los tipos de evento.",
|
||||
"alerts.contacts.add": "Agregar contacto",
|
||||
"alerts.contacts.creating": "Agregando...",
|
||||
"alerts.contacts.empty": "Sin contactos de alertas.",
|
||||
"alerts.contacts.save": "Guardar",
|
||||
"alerts.contacts.saving": "Guardando...",
|
||||
"alerts.contacts.delete": "Eliminar",
|
||||
"alerts.contacts.deleting": "Eliminando...",
|
||||
"alerts.contacts.active": "Activo",
|
||||
"alerts.contacts.linkedUser": "Usuario vinculado (editar en perfil)",
|
||||
"alerts.contacts.role.custom": "Personalizado",
|
||||
"alerts.contacts.role.member": "Miembro",
|
||||
"alerts.contacts.role.admin": "Admin",
|
||||
"alerts.contacts.role.owner": "Propietario",
|
||||
"alerts.contacts.readOnly": "Puedes ver contactos, pero solo propietarios pueden agregar o editar.",
|
||||
"alerts.inbox.title": "Bandeja de alertas",
|
||||
"alerts.inbox.loading": "Cargando alertas...",
|
||||
"alerts.inbox.loadingFilters": "Cargando filtros...",
|
||||
"alerts.inbox.empty": "No se encontraron alertas.",
|
||||
"alerts.inbox.error": "No se pudieron cargar las alertas.",
|
||||
"alerts.inbox.range.24h": "Últimas 24 horas",
|
||||
"alerts.inbox.range.7d": "Últimos 7 días",
|
||||
"alerts.inbox.range.30d": "Últimos 30 días",
|
||||
"alerts.inbox.range.custom": "Personalizado",
|
||||
"alerts.inbox.filters.title": "Filtros",
|
||||
"alerts.inbox.filters.range": "Rango",
|
||||
"alerts.inbox.filters.start": "Inicio",
|
||||
"alerts.inbox.filters.end": "Fin",
|
||||
"alerts.inbox.filters.machine": "Máquina",
|
||||
"alerts.inbox.filters.site": "Sitio",
|
||||
"alerts.inbox.filters.shift": "Turno",
|
||||
"alerts.inbox.filters.type": "Clasificación",
|
||||
"alerts.inbox.filters.severity": "Severidad",
|
||||
"alerts.inbox.filters.status": "Estado",
|
||||
"alerts.inbox.filters.search": "Buscar",
|
||||
"alerts.inbox.filters.searchPlaceholder": "Título, descripción, máquina...",
|
||||
"alerts.inbox.filters.includeUpdates": "Incluir actualizaciones",
|
||||
"alerts.inbox.filters.allMachines": "Todas las máquinas",
|
||||
"alerts.inbox.filters.allSites": "Todos los sitios",
|
||||
"alerts.inbox.filters.allShifts": "Todos los turnos",
|
||||
"alerts.inbox.filters.allTypes": "Todas las clasificaciones",
|
||||
"alerts.inbox.filters.allSeverities": "Todas las severidades",
|
||||
"alerts.inbox.filters.allStatuses": "Todos los estados",
|
||||
"alerts.inbox.table.time": "Hora",
|
||||
"alerts.inbox.table.machine": "Máquina",
|
||||
"alerts.inbox.table.site": "Sitio",
|
||||
"alerts.inbox.table.shift": "Turno",
|
||||
"alerts.inbox.table.type": "Tipo",
|
||||
"alerts.inbox.table.severity": "Severidad",
|
||||
"alerts.inbox.table.status": "Estado",
|
||||
"alerts.inbox.table.duration": "Duración",
|
||||
"alerts.inbox.table.title": "Título",
|
||||
"alerts.inbox.table.unknown": "Sin dato",
|
||||
"alerts.inbox.status.active": "Activa",
|
||||
"alerts.inbox.status.resolved": "Resuelta",
|
||||
"alerts.inbox.status.unknown": "Sin dato",
|
||||
"alerts.inbox.duration.na": "n/d",
|
||||
"alerts.inbox.duration.sec": "s",
|
||||
"alerts.inbox.duration.min": " min",
|
||||
"alerts.inbox.duration.hr": " h",
|
||||
"alerts.inbox.meta.workOrder": "OT",
|
||||
"alerts.inbox.meta.sku": "SKU",
|
||||
"reports.notes.suggested": "Acciones sugeridas",
|
||||
"reports.notes.none": "Sin insights todavía. Genera reportes tras recolectar datos.",
|
||||
"reports.noTrend": "Sin datos de tendencia.",
|
||||
"reports.noDowntime": "Sin datos de paros.",
|
||||
"reports.noCycle": "Sin datos de ciclo.",
|
||||
"reports.scrapRate": "Scrap Rate",
|
||||
"reports.topScrapSku": "SKU con más scrap",
|
||||
"reports.topScrapWorkOrder": "Orden con más scrap",
|
||||
"reports.loss.macrostop": "Macroparo",
|
||||
"reports.loss.microstop": "Microparo",
|
||||
"reports.loss.slowCycle": "Ciclo lento",
|
||||
"reports.loss.qualitySpike": "Pico de calidad",
|
||||
"reports.loss.oeeDrop": "Caída de OEE",
|
||||
"reports.loss.perfDegradation": "Baja de desempeño",
|
||||
"reports.tooltip.cycles": "Ciclos",
|
||||
"reports.tooltip.range": "Rango",
|
||||
"reports.tooltip.below": "Debajo de",
|
||||
"reports.tooltip.above": "Encima de",
|
||||
"reports.tooltip.extremes": "Extremos",
|
||||
"reports.tooltip.downtime": "Tiempo de paro",
|
||||
"reports.tooltip.extraTime": "Tiempo extra",
|
||||
"reports.csv.section": "sección",
|
||||
"reports.csv.key": "clave",
|
||||
"reports.csv.value": "valor",
|
||||
"reports.pdf.title": "Exportación de reporte",
|
||||
"reports.pdf.range": "Rango",
|
||||
"reports.pdf.machine": "Máquina",
|
||||
"reports.pdf.workOrder": "Orden de trabajo",
|
||||
"reports.pdf.sku": "SKU",
|
||||
"reports.pdf.metric": "Métrica",
|
||||
"reports.pdf.value": "Valor",
|
||||
"reports.pdf.topLoss": "Principales causas de pérdida",
|
||||
"reports.pdf.qualitySummary": "Resumen de calidad",
|
||||
"reports.pdf.cycleDistribution": "Distribución de tiempos de ciclo",
|
||||
"reports.pdf.notes": "Notas para operaciones",
|
||||
"reports.pdf.none": "Ninguna",
|
||||
"settings.title": "Configuración",
|
||||
"settings.subtitle": "Configuración en vivo para turnos, alertas y valores predeterminados.",
|
||||
"settings.tabs.general": "General",
|
||||
"settings.tabs.shifts": "Turnos",
|
||||
"settings.tabs.thresholds": "Umbrales",
|
||||
"settings.tabs.alerts": "Alertas",
|
||||
"settings.tabs.financial": "Finanzas",
|
||||
"settings.tabs.team": "Equipo",
|
||||
"settings.loading": "Cargando configuración...",
|
||||
"settings.loadingTeam": "Cargando equipo...",
|
||||
"settings.refresh": "Actualizar",
|
||||
"settings.save": "Guardar cambios",
|
||||
"settings.saving": "Guardando...",
|
||||
"settings.saved": "Configuración guardada",
|
||||
"settings.failedLoad": "No se pudo cargar la configuración",
|
||||
"settings.failedTeam": "No se pudo cargar el equipo",
|
||||
"settings.failedSave": "No se pudo guardar la configuración",
|
||||
"settings.unavailable": "La configuración no está disponible.",
|
||||
"settings.conflict": "La configuración cambió en otro lugar. Actualiza e intenta de nuevo.",
|
||||
"settings.org.title": "Organización",
|
||||
"settings.org.plantName": "Nombre de planta",
|
||||
"settings.org.slug": "Slug",
|
||||
"settings.org.timeZone": "Zona horaria",
|
||||
"settings.shiftSchedule": "Turnos",
|
||||
"settings.shiftSubtitle": "Define turnos activos y compensación de paros.",
|
||||
"settings.shiftName": "Nombre del turno",
|
||||
"settings.shiftStart": "Inicio",
|
||||
"settings.shiftEnd": "Fin",
|
||||
"settings.shiftEnabled": "Activo",
|
||||
"settings.shiftAdd": "Agregar turno",
|
||||
"settings.shiftRemove": "Eliminar",
|
||||
"settings.shiftComp": "Compensación por cambio de turno",
|
||||
"settings.lunchBreak": "Comida",
|
||||
"settings.minutes": "minutos",
|
||||
"settings.shiftHint": "Máx 3 turnos, HH:mm",
|
||||
"settings.shiftTo": "a",
|
||||
"settings.shiftCompLabel": "Compensación por cambio de turno (min)",
|
||||
"settings.lunchBreakLabel": "Comida (min)",
|
||||
"settings.shift.defaultName": "Turno {index}",
|
||||
"settings.thresholds": "Umbrales de alertas",
|
||||
"settings.thresholdsSubtitle": "Ajusta alertas de salud de producción.",
|
||||
"settings.thresholds.appliesAll": "Aplica a todas las máquinas",
|
||||
"settings.thresholds.oee": "Umbral de alerta OEE",
|
||||
"settings.thresholds.performance": "Umbral de Performance",
|
||||
"settings.thresholds.qualitySpike": "Delta de pico de calidad",
|
||||
"settings.thresholds.stoppage": "Multiplicador de paro",
|
||||
"settings.thresholds.macroStoppage": "Multiplicador de macroparo",
|
||||
"settings.alerts": "Alertas",
|
||||
"settings.alertsSubtitle": "Elige qué alertas notificar.",
|
||||
"settings.alerts.oeeDrop": "Alertas por caída de OEE",
|
||||
"settings.alerts.oeeDropHelper": "Notificar cuando OEE esté por debajo del umbral",
|
||||
"settings.alerts.performanceDegradation": "Alertas por baja de Performance",
|
||||
"settings.alerts.performanceDegradationHelper": "Marcar ciclos lentos prolongados",
|
||||
"settings.alerts.qualitySpike": "Alertas por picos de calidad",
|
||||
"settings.alerts.qualitySpikeHelper": "Alertar por picos de scrap",
|
||||
"settings.alerts.predictive": "Alertas predictivas de caída OEE",
|
||||
"settings.alerts.predictiveHelper": "Avisar antes de que OEE caiga",
|
||||
"settings.defaults": "Valores predeterminados de moldes",
|
||||
"settings.defaults.moldTotal": "Moldes totales",
|
||||
"settings.defaults.moldActive": "Moldes activos",
|
||||
"settings.updated": "Actualizado",
|
||||
"settings.updatedBy": "Actualizado por",
|
||||
"settings.team": "Miembros del equipo",
|
||||
"settings.teamTotal": "{count} total",
|
||||
"settings.teamNone": "Sin miembros del equipo.",
|
||||
"settings.invites": "Invitaciones",
|
||||
"settings.inviteEmail": "Correo de invitación",
|
||||
"settings.inviteRole": "Rol",
|
||||
"settings.inviteSend": "Crear invitación",
|
||||
"settings.inviteSending": "Creando...",
|
||||
"settings.inviteStatus.copied": "Enlace de invitación copiado",
|
||||
"settings.inviteStatus.emailRequired": "El correo es obligatorio",
|
||||
"settings.inviteStatus.failed": "No se pudo revocar la invitación",
|
||||
"settings.inviteStatus.sent": "Correo de invitación enviado",
|
||||
"settings.inviteStatus.createFailed": "No se pudo crear la invitación",
|
||||
"settings.inviteStatus.emailFailed": "Invitación creada, falló el correo: {url}",
|
||||
"settings.inviteNone": "Sin invitaciones pendientes.",
|
||||
"settings.inviteExpires": "Expira {date}",
|
||||
"settings.inviteRole.member": "Miembro",
|
||||
"settings.inviteRole.admin": "Admin",
|
||||
"settings.inviteRole.owner": "Propietario",
|
||||
"settings.inviteCopy": "Copiar enlace",
|
||||
"settings.inviteRevoke": "Revocar",
|
||||
"settings.role.owner": "Propietario",
|
||||
"settings.role.admin": "Admin",
|
||||
"settings.role.member": "Miembro",
|
||||
"settings.role.inactive": "Inactivo",
|
||||
"settings.integrations": "Integraciones",
|
||||
"settings.integrations.webhook": "Webhook URL",
|
||||
"settings.integrations.erp": "ERP Sync",
|
||||
"settings.integrations.erpNotConfigured": "No configurado",
|
||||
"financial.title": "Impacto financiero",
|
||||
"financial.subtitle": "Convierte paros, ciclos lentos y scrap en dinero.",
|
||||
"financial.ownerOnly": "El impacto financiero solo está disponible para propietarios.",
|
||||
"financial.costsMoved": "Los costos ahora están en",
|
||||
"financial.costsMovedLink": "Configuración -> Finanzas",
|
||||
"financial.export.html": "HTML",
|
||||
"financial.export.csv": "CSV",
|
||||
"financial.totalLoss": "Pérdida total",
|
||||
"financial.currencyLabel": "Moneda: {currency}",
|
||||
"financial.noImpact": "Sin datos de impacto.",
|
||||
"financial.chart.title": "Pérdida de dinero en el tiempo",
|
||||
"financial.chart.subtitle": "Acumulado por tipo de evento",
|
||||
"financial.range.day": "Día",
|
||||
"financial.range.week": "Semana",
|
||||
"financial.range.month": "Mes",
|
||||
"financial.filters.title": "Filtros",
|
||||
"financial.filters.machine": "Máquina",
|
||||
"financial.filters.location": "Ubicación",
|
||||
"financial.filters.sku": "SKU",
|
||||
"financial.filters.currency": "Moneda",
|
||||
"financial.filters.allMachines": "Todas las máquinas",
|
||||
"financial.filters.allLocations": "Todas las ubicaciones",
|
||||
"financial.filters.skuPlaceholder": "Filtrar por SKU",
|
||||
"financial.filters.currencyPlaceholder": "MXN",
|
||||
"financial.loadingMachines": "Cargando máquinas...",
|
||||
"financial.config.title": "Parámetros de costo",
|
||||
"financial.config.subtitle": "Los valores aplican a todas las máquinas salvo override.",
|
||||
"financial.config.applyOrg": "Aplicar valores de organización a todas",
|
||||
"financial.config.save": "Guardar",
|
||||
"financial.config.saving": "Guardando...",
|
||||
"financial.config.saved": "Guardado",
|
||||
"financial.config.saveFailed": "No se pudo guardar",
|
||||
"financial.config.orgDefaults": "Valores de organización",
|
||||
"financial.config.locationOverrides": "Overrides por ubicación",
|
||||
"financial.config.machineOverrides": "Overrides por máquina",
|
||||
"financial.config.productOverrides": "Overrides por producto",
|
||||
"financial.config.addLocation": "Agregar override de ubicación",
|
||||
"financial.config.addMachine": "Agregar override de máquina",
|
||||
"financial.config.addProduct": "Agregar override de producto",
|
||||
"financial.config.noneLocation": "Sin overrides de ubicación.",
|
||||
"financial.config.noneMachine": "Sin overrides de máquina.",
|
||||
"financial.config.noneProduct": "Sin overrides de producto.",
|
||||
"financial.config.location": "Ubicación",
|
||||
"financial.config.selectLocation": "Selecciona ubicación",
|
||||
"financial.config.machine": "Máquina",
|
||||
"financial.config.selectMachine": "Selecciona máquina",
|
||||
"financial.config.currency": "Moneda",
|
||||
"financial.config.sku": "SKU",
|
||||
"financial.config.rawMaterialUnit": "Materia prima / unidad",
|
||||
"financial.config.ownerOnly": "Los costos financieros solo están disponibles para propietarios.",
|
||||
"financial.config.loading": "Cargando finanzas...",
|
||||
"financial.field.machineCostPerMin": "Costo máquina / min",
|
||||
"financial.field.operatorCostPerMin": "Costo operador / min",
|
||||
"financial.field.ratedRunningKw": "kW en operación",
|
||||
"financial.field.idleKw": "kW en espera",
|
||||
"financial.field.kwhRate": "Tarifa kWh",
|
||||
"financial.field.energyMultiplier": "Multiplicador de energía",
|
||||
"financial.field.energyCostPerMin": "Costo energía / min",
|
||||
"financial.field.scrapCostPerUnit": "Costo scrap / unidad",
|
||||
"financial.field.rawMaterialCostPerUnit": "Costo materia prima / unidad",
|
||||
"nav.downtime": "Downtime",
|
||||
"settings.tabs.modules": "Módulos",
|
||||
"settings.modules.title": "Módulos",
|
||||
"settings.modules.subtitle": "Activa/desactiva módulos según cómo opera la planta.",
|
||||
"settings.modules.screenless.title": "Modo sin pantalla",
|
||||
"settings.modules.screenless.helper": "Oculta el módulo de Paros (Downtime) del menú (para plantas sin captura de razones en Node-RED).",
|
||||
"settings.modules.note": "Este ajuste aplica a toda la organización."
|
||||
}
|
||||
61
lib/i18n/localeStore.ts
Normal file
61
lib/i18n/localeStore.ts
Normal file
@@ -0,0 +1,61 @@
|
||||
import type { Locale } from "./translations";
|
||||
|
||||
const LOCALE_COOKIE = "mis_locale";
|
||||
|
||||
let initialized = false;
|
||||
let currentLocale: Locale = "en";
|
||||
const listeners = new Set<() => void>();
|
||||
|
||||
const isBrowser = () => typeof document !== "undefined";
|
||||
|
||||
function readCookieLocale(): Locale | null {
|
||||
if (!isBrowser()) return null;
|
||||
|
||||
const match = document.cookie
|
||||
.split(";")
|
||||
.map((part) => part.trim())
|
||||
.find((part) => part.startsWith(`${LOCALE_COOKIE}=`));
|
||||
|
||||
if (!match) return null;
|
||||
const value = match.split("=")[1];
|
||||
if (value === "es-MX" || value === "en") return value;
|
||||
return null;
|
||||
}
|
||||
|
||||
function readLocaleFromDocument(): Locale {
|
||||
if (!isBrowser()) return "en";
|
||||
|
||||
const cookieLocale = readCookieLocale();
|
||||
if (cookieLocale) return cookieLocale;
|
||||
|
||||
const docLang = document.documentElement.getAttribute("lang");
|
||||
if (docLang === "es-MX" || docLang === "en") return docLang;
|
||||
|
||||
return "en";
|
||||
}
|
||||
|
||||
function ensureInitialized() {
|
||||
if (initialized) return;
|
||||
currentLocale = readLocaleFromDocument();
|
||||
initialized = true;
|
||||
}
|
||||
|
||||
export function getLocaleSnapshot(): Locale {
|
||||
if (!isBrowser()) return "en";
|
||||
ensureInitialized();
|
||||
return currentLocale;
|
||||
}
|
||||
|
||||
export function subscribeLocale(listener: () => void) {
|
||||
listeners.add(listener);
|
||||
return () => listeners.delete(listener);
|
||||
}
|
||||
|
||||
export function setLocale(next: Locale) {
|
||||
if (isBrowser()) {
|
||||
document.documentElement.setAttribute("lang", next);
|
||||
document.cookie = `${LOCALE_COOKIE}=${next}; Path=/; Max-Age=31536000; SameSite=Lax`;
|
||||
}
|
||||
currentLocale = next;
|
||||
listeners.forEach((listener) => listener());
|
||||
}
|
||||
30
lib/i18n/translations.ts
Normal file
30
lib/i18n/translations.ts
Normal file
@@ -0,0 +1,30 @@
|
||||
import en from "./en.json";
|
||||
import esMX from "./es-MX.json";
|
||||
|
||||
export type Locale = "en" | "es-MX";
|
||||
|
||||
type Dictionary = Record<string, string>;
|
||||
|
||||
export const translations: Record<Locale, Dictionary> = {
|
||||
en,
|
||||
"es-MX": esMX,
|
||||
};
|
||||
|
||||
export const defaultLocale: Locale = "en";
|
||||
|
||||
export function translate(
|
||||
locale: Locale,
|
||||
key: string,
|
||||
vars?: Record<string, string | number>
|
||||
): string {
|
||||
const table = translations[locale] ?? translations[defaultLocale];
|
||||
const fallback = translations[defaultLocale];
|
||||
let text = table[key] ?? fallback[key] ?? key;
|
||||
if (vars) {
|
||||
text = text.replace(/\{(\w+)\}/g, (match, token) => {
|
||||
const value = vars[token];
|
||||
return value == null ? match : String(value);
|
||||
});
|
||||
}
|
||||
return text;
|
||||
}
|
||||
63
lib/i18n/useI18n.ts
Normal file
63
lib/i18n/useI18n.ts
Normal file
@@ -0,0 +1,63 @@
|
||||
"use client";
|
||||
|
||||
import { useCallback, useEffect, useMemo, useState } from "react";
|
||||
import { defaultLocale, Locale, translate } from "./translations";
|
||||
|
||||
const LOCALE_COOKIE = "mis_locale";
|
||||
const LOCALE_EVENT = "mis-locale-change";
|
||||
|
||||
function readCookieLocale(): Locale | null {
|
||||
const match = document.cookie
|
||||
.split(";")
|
||||
.map((part) => part.trim())
|
||||
.find((part) => part.startsWith(`${LOCALE_COOKIE}=`));
|
||||
if (!match) return null;
|
||||
const value = match.split("=")[1];
|
||||
if (value === "es-MX" || value === "en") return value;
|
||||
return null;
|
||||
}
|
||||
|
||||
function readLocale(): Locale {
|
||||
const docLang = document.documentElement.getAttribute("lang");
|
||||
if (docLang === "es-MX" || docLang === "en") return docLang;
|
||||
return readCookieLocale() ?? defaultLocale;
|
||||
}
|
||||
|
||||
export function useI18n() {
|
||||
const [locale, setLocale] = useState<Locale>(() => readLocale());
|
||||
|
||||
useEffect(() => {
|
||||
const handler = (event: Event) => {
|
||||
const detail = (event as CustomEvent).detail;
|
||||
if (detail === "es-MX" || detail === "en") {
|
||||
setLocale(detail);
|
||||
}
|
||||
};
|
||||
window.addEventListener(LOCALE_EVENT, handler);
|
||||
return () => window.removeEventListener(LOCALE_EVENT, handler);
|
||||
}, []);
|
||||
|
||||
const setLocaleAndPersist = useCallback(
|
||||
(next: Locale) => {
|
||||
document.documentElement.setAttribute("lang", next);
|
||||
document.cookie = `${LOCALE_COOKIE}=${next}; Path=/; Max-Age=31536000; SameSite=Lax`;
|
||||
setLocale(next);
|
||||
window.dispatchEvent(new CustomEvent(LOCALE_EVENT, { detail: next }));
|
||||
},
|
||||
[setLocale]
|
||||
);
|
||||
|
||||
const t = useCallback(
|
||||
(key: string, vars?: Record<string, string | number>) => translate(locale, key, vars),
|
||||
[locale]
|
||||
);
|
||||
|
||||
return useMemo(
|
||||
() => ({
|
||||
locale,
|
||||
setLocale: setLocaleAndPersist,
|
||||
t,
|
||||
}),
|
||||
[locale, setLocaleAndPersist, t]
|
||||
);
|
||||
}
|
||||
20
lib/logger.ts
Normal file
20
lib/logger.ts
Normal file
@@ -0,0 +1,20 @@
|
||||
import fs from "fs";
|
||||
import path from "path";
|
||||
|
||||
const LOG_PATH = process.env.LOG_FILE || "/tmp/mis-control-tower.log";
|
||||
|
||||
export function logLine(event: string, data: Record<string, unknown> = {}) {
|
||||
const line = JSON.stringify({
|
||||
ts: new Date().toISOString(),
|
||||
event,
|
||||
...data,
|
||||
});
|
||||
console.log(line);
|
||||
try {
|
||||
fs.mkdirSync(path.dirname(LOG_PATH), { recursive: true });
|
||||
fs.appendFileSync(LOG_PATH, line + "\n", { encoding: "utf8" });
|
||||
} catch {
|
||||
// If file logging fails, we still want something:
|
||||
console.error("[logLine-failed]", line);
|
||||
}
|
||||
}
|
||||
38
lib/machineAuthCache.ts
Normal file
38
lib/machineAuthCache.ts
Normal file
@@ -0,0 +1,38 @@
|
||||
import { prisma } from "@/lib/prisma";
|
||||
|
||||
type MachineAuth = { id: string; orgId: string };
|
||||
|
||||
const TTL_MS = 60_000;
|
||||
const MAX_SIZE = 1000;
|
||||
const cache = new Map<string, { value: MachineAuth; expiresAt: number }>();
|
||||
|
||||
function makeKey(machineId: string, apiKey: string) {
|
||||
return `${machineId}:${apiKey}`;
|
||||
}
|
||||
|
||||
export async function getMachineAuth(machineId: string, apiKey: string) {
|
||||
const key = makeKey(machineId, apiKey);
|
||||
const now = Date.now();
|
||||
const hit = cache.get(key);
|
||||
|
||||
if (hit && hit.expiresAt > now) {
|
||||
return hit.value;
|
||||
}
|
||||
|
||||
const machine = await prisma.machine.findFirst({
|
||||
where: { id: machineId, apiKey },
|
||||
select: { id: true, orgId: true },
|
||||
});
|
||||
|
||||
if (!machine) {
|
||||
cache.delete(key);
|
||||
return null;
|
||||
}
|
||||
|
||||
if (cache.size > MAX_SIZE) {
|
||||
cache.clear();
|
||||
}
|
||||
|
||||
cache.set(key, { value: machine, expiresAt: now + TTL_MS });
|
||||
return machine;
|
||||
}
|
||||
124
lib/mqtt.ts
Normal file
124
lib/mqtt.ts
Normal file
@@ -0,0 +1,124 @@
|
||||
import "server-only";
|
||||
|
||||
import mqtt, { MqttClient } from "mqtt";
|
||||
|
||||
type SettingsUpdate = {
|
||||
orgId: string;
|
||||
version: number;
|
||||
source?: string;
|
||||
updatedAt?: string;
|
||||
machineId?: string;
|
||||
overridesUpdatedAt?: string;
|
||||
};
|
||||
|
||||
type WorkOrdersUpdate = {
|
||||
orgId: string;
|
||||
machineId: string;
|
||||
count?: number;
|
||||
source?: string;
|
||||
updatedAt?: string;
|
||||
};
|
||||
|
||||
const MQTT_URL = process.env.MQTT_BROKER_URL || "";
|
||||
const MQTT_USERNAME = process.env.MQTT_USERNAME;
|
||||
const MQTT_PASSWORD = process.env.MQTT_PASSWORD;
|
||||
const MQTT_CLIENT_ID = process.env.MQTT_CLIENT_ID;
|
||||
const MQTT_TOPIC_PREFIX = (process.env.MQTT_TOPIC_PREFIX || "mis").replace(/\/+$/, "");
|
||||
const MQTT_QOS_RAW = Number(process.env.MQTT_QOS ?? "2");
|
||||
const MQTT_QOS = MQTT_QOS_RAW === 0 || MQTT_QOS_RAW === 1 || MQTT_QOS_RAW === 2 ? MQTT_QOS_RAW : 2;
|
||||
|
||||
let client: MqttClient | null = null;
|
||||
let connecting: Promise<MqttClient> | null = null;
|
||||
|
||||
function buildSettingsTopic(orgId: string, machineId?: string) {
|
||||
const base = `${MQTT_TOPIC_PREFIX}/org/${orgId}`;
|
||||
if (machineId) return `${base}/machines/${machineId}/settings/updated`;
|
||||
return `${base}/settings/updated`;
|
||||
}
|
||||
|
||||
function buildWorkOrdersTopic(orgId: string, machineId: string) {
|
||||
const base = `${MQTT_TOPIC_PREFIX}/org/${orgId}`;
|
||||
return `${base}/machines/${machineId}/work_orders/updated`;
|
||||
}
|
||||
|
||||
async function getClient() {
|
||||
if (!MQTT_URL) return null;
|
||||
if (client?.connected) return client;
|
||||
if (connecting) return connecting;
|
||||
|
||||
connecting = new Promise((resolve, reject) => {
|
||||
const next = mqtt.connect(MQTT_URL, {
|
||||
clientId: MQTT_CLIENT_ID,
|
||||
username: MQTT_USERNAME,
|
||||
password: MQTT_PASSWORD,
|
||||
clean: true,
|
||||
reconnectPeriod: 5000,
|
||||
});
|
||||
|
||||
next.once("connect", () => {
|
||||
client = next;
|
||||
connecting = null;
|
||||
resolve(next);
|
||||
});
|
||||
|
||||
next.once("error", (err) => {
|
||||
next.end(true);
|
||||
client = null;
|
||||
connecting = null;
|
||||
reject(err);
|
||||
});
|
||||
|
||||
next.once("close", () => {
|
||||
client = null;
|
||||
});
|
||||
});
|
||||
|
||||
return connecting;
|
||||
}
|
||||
|
||||
export async function publishSettingsUpdate(update: SettingsUpdate) {
|
||||
if (!MQTT_URL) return { ok: false, reason: "MQTT_NOT_CONFIGURED" as const };
|
||||
const mqttClient = await getClient();
|
||||
if (!mqttClient) return { ok: false, reason: "MQTT_NOT_CONFIGURED" as const };
|
||||
|
||||
const topic = buildSettingsTopic(update.orgId, update.machineId);
|
||||
const payload = JSON.stringify({
|
||||
type: update.machineId ? "machine_settings_updated" : "org_settings_updated",
|
||||
orgId: update.orgId,
|
||||
machineId: update.machineId,
|
||||
version: update.version,
|
||||
source: update.source || "control_tower",
|
||||
updatedAt: update.updatedAt,
|
||||
overridesUpdatedAt: update.overridesUpdatedAt,
|
||||
});
|
||||
|
||||
return new Promise<{ ok: true }>((resolve, reject) => {
|
||||
mqttClient.publish(topic, payload, { qos: MQTT_QOS }, (err) => {
|
||||
if (err) return reject(err);
|
||||
resolve({ ok: true });
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
export async function publishWorkOrdersUpdate(update: WorkOrdersUpdate) {
|
||||
if (!MQTT_URL) return { ok: false, reason: "MQTT_NOT_CONFIGURED" as const };
|
||||
const mqttClient = await getClient();
|
||||
if (!mqttClient) return { ok: false, reason: "MQTT_NOT_CONFIGURED" as const };
|
||||
|
||||
const topic = buildWorkOrdersTopic(update.orgId, update.machineId);
|
||||
const payload = JSON.stringify({
|
||||
type: "work_orders_updated",
|
||||
orgId: update.orgId,
|
||||
machineId: update.machineId,
|
||||
count: update.count ?? null,
|
||||
source: update.source || "control_tower",
|
||||
updatedAt: update.updatedAt,
|
||||
});
|
||||
|
||||
return new Promise<{ ok: true }>((resolve, reject) => {
|
||||
mqttClient.publish(topic, payload, { qos: MQTT_QOS }, (err) => {
|
||||
if (err) return reject(err);
|
||||
resolve({ ok: true });
|
||||
});
|
||||
});
|
||||
}
|
||||
193
lib/overview/getOverviewData.ts
Normal file
193
lib/overview/getOverviewData.ts
Normal file
@@ -0,0 +1,193 @@
|
||||
import { prisma } from "@/lib/prisma";
|
||||
import { normalizeEvent } from "@/lib/events/normalizeEvent";
|
||||
|
||||
const ALLOWED_TYPES = new Set([
|
||||
"slow-cycle",
|
||||
"microstop",
|
||||
"macrostop",
|
||||
"offline",
|
||||
"error",
|
||||
"oee-drop",
|
||||
"quality-spike",
|
||||
"performance-degradation",
|
||||
"predictive-oee-decline",
|
||||
"alert-delivery-failed",
|
||||
]);
|
||||
|
||||
type OrgSettings = {
|
||||
stoppageMultiplier?: number | null;
|
||||
macroStoppageMultiplier?: number | null;
|
||||
};
|
||||
|
||||
type OverviewParams = {
|
||||
orgId: string;
|
||||
eventsMode?: string;
|
||||
eventsWindowSec?: number;
|
||||
eventMachines?: number;
|
||||
orgSettings?: OrgSettings | null;
|
||||
};
|
||||
|
||||
function heartbeatTime(hb?: { ts?: Date | null; tsServer?: Date | null } | null) {
|
||||
return hb?.tsServer ?? hb?.ts ?? null;
|
||||
}
|
||||
|
||||
export async function getOverviewData({
|
||||
orgId,
|
||||
eventsMode = "critical",
|
||||
eventsWindowSec = 21600,
|
||||
eventMachines = 6,
|
||||
orgSettings,
|
||||
}: OverviewParams) {
|
||||
const machines = await prisma.machine.findMany({
|
||||
where: { orgId },
|
||||
orderBy: { createdAt: "desc" },
|
||||
select: {
|
||||
id: true,
|
||||
name: true,
|
||||
code: true,
|
||||
location: true,
|
||||
createdAt: true,
|
||||
updatedAt: true,
|
||||
heartbeats: {
|
||||
orderBy: { tsServer: "desc" },
|
||||
take: 1,
|
||||
select: { ts: true, tsServer: true, status: true, message: true, ip: true, fwVersion: true },
|
||||
},
|
||||
kpiSnapshots: {
|
||||
orderBy: { ts: "desc" },
|
||||
take: 1,
|
||||
select: {
|
||||
ts: true,
|
||||
oee: true,
|
||||
availability: true,
|
||||
performance: true,
|
||||
quality: true,
|
||||
workOrderId: true,
|
||||
sku: true,
|
||||
good: true,
|
||||
scrap: true,
|
||||
target: true,
|
||||
cycleTime: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
const machineRows = machines.map((m) => ({
|
||||
...m,
|
||||
latestHeartbeat: m.heartbeats[0] ?? null,
|
||||
latestKpi: m.kpiSnapshots[0] ?? null,
|
||||
heartbeats: undefined,
|
||||
kpiSnapshots: undefined,
|
||||
}));
|
||||
|
||||
const safeEventMachines = Number.isFinite(eventMachines) ? Math.max(1, Math.floor(eventMachines)) : 6;
|
||||
const safeWindowSec = Number.isFinite(eventsWindowSec) ? eventsWindowSec : 21600;
|
||||
|
||||
const topMachines = machineRows
|
||||
.slice()
|
||||
.sort((a, b) => {
|
||||
const at = heartbeatTime(a.latestHeartbeat);
|
||||
const bt = heartbeatTime(b.latestHeartbeat);
|
||||
const atMs = at ? at.getTime() : 0;
|
||||
const btMs = bt ? bt.getTime() : 0;
|
||||
return btMs - atMs;
|
||||
})
|
||||
.slice(0, safeEventMachines);
|
||||
|
||||
const targetIds = topMachines.map((m) => m.id);
|
||||
|
||||
let events = [] as Array<{
|
||||
id: string;
|
||||
ts: Date | null;
|
||||
topic: string;
|
||||
eventType: string;
|
||||
severity: string;
|
||||
title: string;
|
||||
description?: string | null;
|
||||
requiresAck: boolean;
|
||||
workOrderId?: string | null;
|
||||
machineId: string;
|
||||
machineName?: string | null;
|
||||
source: "ingested";
|
||||
}>;
|
||||
|
||||
if (targetIds.length) {
|
||||
let settings = orgSettings ?? null;
|
||||
if (!settings) {
|
||||
settings = await prisma.orgSettings.findUnique({
|
||||
where: { orgId },
|
||||
select: { stoppageMultiplier: true, macroStoppageMultiplier: true },
|
||||
});
|
||||
}
|
||||
|
||||
const microMultiplier = Number(settings?.stoppageMultiplier ?? 1.5);
|
||||
const macroMultiplier = Math.max(microMultiplier, Number(settings?.macroStoppageMultiplier ?? 5));
|
||||
const windowStart = new Date(Date.now() - Math.max(0, safeWindowSec) * 1000);
|
||||
|
||||
const rawEvents = await prisma.machineEvent.findMany({
|
||||
where: {
|
||||
orgId,
|
||||
machineId: { in: targetIds },
|
||||
ts: { gte: windowStart },
|
||||
},
|
||||
orderBy: { ts: "desc" },
|
||||
take: Math.min(300, Math.max(60, targetIds.length * 40)),
|
||||
select: {
|
||||
id: true,
|
||||
ts: true,
|
||||
topic: true,
|
||||
eventType: true,
|
||||
severity: true,
|
||||
title: true,
|
||||
description: true,
|
||||
requiresAck: true,
|
||||
data: true,
|
||||
workOrderId: true,
|
||||
machineId: true,
|
||||
machine: { select: { name: true } },
|
||||
},
|
||||
});
|
||||
|
||||
const normalized = rawEvents
|
||||
.map((row) => ({
|
||||
...normalizeEvent(row, { microMultiplier, macroMultiplier }),
|
||||
machineId: row.machineId,
|
||||
machineName: row.machine?.name ?? null,
|
||||
source: "ingested" as const,
|
||||
}))
|
||||
.filter((event) => event.ts);
|
||||
|
||||
const allowed = normalized.filter((event) => ALLOWED_TYPES.has(event.eventType));
|
||||
const isCritical = (event: (typeof allowed)[number]) => {
|
||||
const severity = String(event.severity ?? "").toLowerCase();
|
||||
return (
|
||||
event.eventType === "macrostop" ||
|
||||
event.requiresAck === true ||
|
||||
severity === "critical" ||
|
||||
severity === "error" ||
|
||||
severity === "high"
|
||||
);
|
||||
};
|
||||
|
||||
const filtered = eventsMode === "critical" ? allowed.filter(isCritical) : allowed;
|
||||
|
||||
const seen = new Set<string>();
|
||||
const deduped = filtered.filter((event) => {
|
||||
const key = `${event.machineId}-${event.eventType}-${event.ts ?? ""}-${event.title}`;
|
||||
if (seen.has(key)) return false;
|
||||
seen.add(key);
|
||||
return true;
|
||||
});
|
||||
|
||||
deduped.sort((a, b) => {
|
||||
const at = a.ts ? a.ts.getTime() : 0;
|
||||
const bt = b.ts ? b.ts.getTime() : 0;
|
||||
return bt - at;
|
||||
});
|
||||
|
||||
events = deduped.slice(0, 30);
|
||||
}
|
||||
|
||||
return { machines: machineRows, events };
|
||||
}
|
||||
16
lib/pairingCode.ts
Normal file
16
lib/pairingCode.ts
Normal file
@@ -0,0 +1,16 @@
|
||||
import { randomBytes } from "crypto";
|
||||
|
||||
const PAIRING_ALPHABET = "ABCDEFGHJKLMNPQRSTUVWXYZ23456789";
|
||||
|
||||
export function generatePairingCode(length = 5) {
|
||||
const bytes = randomBytes(length);
|
||||
let code = "";
|
||||
for (let i = 0; i < length; i += 1) {
|
||||
code += PAIRING_ALPHABET[bytes[i] % PAIRING_ALPHABET.length];
|
||||
}
|
||||
return code;
|
||||
}
|
||||
|
||||
export function normalizePairingCode(input: string) {
|
||||
return input.trim().toUpperCase().replace(/[^A-Z0-9]/g, "");
|
||||
}
|
||||
33
lib/prismaJson.ts
Normal file
33
lib/prismaJson.ts
Normal file
@@ -0,0 +1,33 @@
|
||||
import { Prisma } from "@prisma/client";
|
||||
|
||||
function isRecord(value: unknown): value is Record<string, unknown> {
|
||||
return !!value && typeof value === "object" && !Array.isArray(value);
|
||||
}
|
||||
|
||||
export function toJsonValue(value: unknown): Prisma.InputJsonValue {
|
||||
if (value === null || value === undefined) {
|
||||
return Prisma.JsonNull as unknown as Prisma.InputJsonValue;
|
||||
}
|
||||
if (typeof value === "string" || typeof value === "number" || typeof value === "boolean") {
|
||||
return value;
|
||||
}
|
||||
if (Array.isArray(value)) {
|
||||
return value.map((item) => (item === undefined ? (Prisma.JsonNull as unknown as Prisma.InputJsonValue) : toJsonValue(item)));
|
||||
}
|
||||
if (isRecord(value)) {
|
||||
const out: Record<string, Prisma.InputJsonValue> = {};
|
||||
for (const [key, val] of Object.entries(value)) {
|
||||
if (val === undefined) continue;
|
||||
out[key] = toJsonValue(val);
|
||||
}
|
||||
return out;
|
||||
}
|
||||
return String(value);
|
||||
}
|
||||
|
||||
export function toNullableJsonValue(
|
||||
value: unknown
|
||||
): Prisma.NullableJsonNullValueInput | Prisma.InputJsonValue {
|
||||
if (value === null || value === undefined) return Prisma.DbNull;
|
||||
return toJsonValue(value);
|
||||
}
|
||||
265
lib/settings.ts
Normal file
265
lib/settings.ts
Normal file
@@ -0,0 +1,265 @@
|
||||
const TIME_RE = /^([01]\d|2[0-3]):[0-5]\d$/;
|
||||
|
||||
export const DEFAULT_ALERTS = {
|
||||
oeeDropEnabled: true,
|
||||
performanceDegradationEnabled: true,
|
||||
qualitySpikeEnabled: true,
|
||||
predictiveOeeDeclineEnabled: true,
|
||||
};
|
||||
|
||||
export const DEFAULT_DEFAULTS = {
|
||||
moldTotal: 1,
|
||||
moldActive: 1,
|
||||
};
|
||||
|
||||
export const DEFAULT_SHIFT = {
|
||||
name: "Shift 1",
|
||||
start: "06:00",
|
||||
end: "15:00",
|
||||
};
|
||||
|
||||
type AnyRecord = Record<string, unknown>;
|
||||
|
||||
function isPlainObject(value: unknown): value is AnyRecord {
|
||||
return !!value && typeof value === "object" && !Array.isArray(value);
|
||||
}
|
||||
|
||||
export function normalizeAlerts(raw: unknown) {
|
||||
if (!isPlainObject(raw)) return { ...DEFAULT_ALERTS };
|
||||
return { ...DEFAULT_ALERTS, ...raw };
|
||||
}
|
||||
|
||||
export function normalizeDefaults(raw: unknown) {
|
||||
if (!isPlainObject(raw)) return { ...DEFAULT_DEFAULTS };
|
||||
return { ...DEFAULT_DEFAULTS, ...raw };
|
||||
}
|
||||
|
||||
type SettingsRow = {
|
||||
orgId: string;
|
||||
version: number;
|
||||
timezone: string;
|
||||
shiftChangeCompMin?: number | null;
|
||||
lunchBreakMin?: number | null;
|
||||
stoppageMultiplier?: number | null;
|
||||
macroStoppageMultiplier?: number | null;
|
||||
oeeAlertThresholdPct?: number | null;
|
||||
performanceThresholdPct?: number | null;
|
||||
qualitySpikeDeltaPct?: number | null;
|
||||
alertsJson?: unknown;
|
||||
defaultsJson?: unknown;
|
||||
updatedAt?: Date | string | null;
|
||||
updatedBy?: string | null;
|
||||
};
|
||||
|
||||
type ShiftRow = {
|
||||
name?: string | null;
|
||||
startTime?: string | null;
|
||||
endTime?: string | null;
|
||||
enabled?: boolean | null;
|
||||
sortOrder?: number | null;
|
||||
};
|
||||
|
||||
export function buildSettingsPayload(settings: SettingsRow, shifts: ShiftRow[]) {
|
||||
const ordered = [...(shifts ?? [])].sort((a, b) => (a.sortOrder ?? 0) - (b.sortOrder ?? 0));
|
||||
const mappedShifts = ordered.map((s, idx) => ({
|
||||
name: s.name || `Shift ${idx + 1}`,
|
||||
start: s.startTime,
|
||||
end: s.endTime,
|
||||
enabled: s.enabled !== false,
|
||||
}));
|
||||
|
||||
return {
|
||||
orgId: settings.orgId,
|
||||
version: settings.version,
|
||||
timezone: settings.timezone,
|
||||
shiftSchedule: {
|
||||
shifts: mappedShifts,
|
||||
shiftChangeCompensationMin: settings.shiftChangeCompMin,
|
||||
lunchBreakMin: settings.lunchBreakMin,
|
||||
},
|
||||
thresholds: {
|
||||
stoppageMultiplier: settings.stoppageMultiplier,
|
||||
macroStoppageMultiplier: settings.macroStoppageMultiplier,
|
||||
oeeAlertThresholdPct: settings.oeeAlertThresholdPct,
|
||||
performanceThresholdPct: settings.performanceThresholdPct,
|
||||
qualitySpikeDeltaPct: settings.qualitySpikeDeltaPct,
|
||||
},
|
||||
alerts: normalizeAlerts(settings.alertsJson),
|
||||
defaults: normalizeDefaults(settings.defaultsJson),
|
||||
updatedAt: settings.updatedAt,
|
||||
updatedBy: settings.updatedBy,
|
||||
};
|
||||
}
|
||||
|
||||
export function deepMerge(base: unknown, override: unknown): unknown {
|
||||
if (!isPlainObject(base) || !isPlainObject(override)) return override;
|
||||
const out: AnyRecord = { ...base };
|
||||
for (const [key, value] of Object.entries(override)) {
|
||||
if (value === undefined) continue;
|
||||
if (isPlainObject(value) && isPlainObject(out[key])) {
|
||||
out[key] = deepMerge(out[key], value);
|
||||
} else {
|
||||
out[key] = value;
|
||||
}
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
export function applyOverridePatch(existing: unknown, patch: unknown) {
|
||||
const base: AnyRecord = isPlainObject(existing) ? { ...existing } : {};
|
||||
if (!isPlainObject(patch)) return base;
|
||||
|
||||
for (const [key, value] of Object.entries(patch)) {
|
||||
if (value === null) {
|
||||
delete base[key];
|
||||
continue;
|
||||
}
|
||||
|
||||
if (isPlainObject(value)) {
|
||||
const merged = applyOverridePatch(isPlainObject(base[key]) ? base[key] : {}, value);
|
||||
if (Object.keys(merged).length === 0) {
|
||||
delete base[key];
|
||||
} else {
|
||||
base[key] = merged;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
base[key] = value;
|
||||
}
|
||||
|
||||
return base;
|
||||
}
|
||||
|
||||
type NormalizedShift = {
|
||||
name: string;
|
||||
startTime: string;
|
||||
endTime: string;
|
||||
sortOrder: number;
|
||||
enabled: boolean;
|
||||
};
|
||||
|
||||
type ShiftValidationResult = NormalizedShift | { error: string };
|
||||
|
||||
export function validateShiftSchedule(shifts: unknown) {
|
||||
if (!Array.isArray(shifts)) return { ok: false, error: "shifts must be an array" };
|
||||
if (shifts.length > 3) return { ok: false, error: "shifts max is 3" };
|
||||
|
||||
const normalized: ShiftValidationResult[] = shifts.map((raw, idx) => {
|
||||
const record = isPlainObject(raw) ? raw : {};
|
||||
const start = String(record.start ?? "").trim();
|
||||
const end = String(record.end ?? "").trim();
|
||||
if (!TIME_RE.test(start) || !TIME_RE.test(end)) {
|
||||
return { error: `shift ${idx + 1} start/end must be HH:mm` };
|
||||
}
|
||||
const name = String(record.name ?? `Shift ${idx + 1}`).trim() || `Shift ${idx + 1}`;
|
||||
const enabled = record.enabled !== false;
|
||||
return {
|
||||
name,
|
||||
startTime: start,
|
||||
endTime: end,
|
||||
sortOrder: idx + 1,
|
||||
enabled,
|
||||
};
|
||||
});
|
||||
|
||||
const firstError = normalized.find((s): s is { error: string } => "error" in s);
|
||||
if (firstError) return { ok: false, error: firstError.error };
|
||||
|
||||
return { ok: true, shifts: normalized as NormalizedShift[] };
|
||||
}
|
||||
|
||||
export function validateShiftFields(shiftChangeCompensationMin?: unknown, lunchBreakMin?: unknown) {
|
||||
if (shiftChangeCompensationMin != null) {
|
||||
const v = Number(shiftChangeCompensationMin);
|
||||
if (!Number.isFinite(v) || v < 0 || v > 480) {
|
||||
return { ok: false, error: "shiftChangeCompensationMin must be 0-480" };
|
||||
}
|
||||
}
|
||||
if (lunchBreakMin != null) {
|
||||
const v = Number(lunchBreakMin);
|
||||
if (!Number.isFinite(v) || v < 0 || v > 480) {
|
||||
return { ok: false, error: "lunchBreakMin must be 0-480" };
|
||||
}
|
||||
}
|
||||
return { ok: true };
|
||||
}
|
||||
|
||||
export function validateThresholds(thresholds: unknown) {
|
||||
if (!isPlainObject(thresholds)) return { ok: true };
|
||||
|
||||
const stoppage = thresholds.stoppageMultiplier;
|
||||
if (stoppage != null) {
|
||||
const v = Number(stoppage);
|
||||
if (!Number.isFinite(v) || v < 1.1 || v > 5.0) {
|
||||
return { ok: false, error: "stoppageMultiplier must be 1.1-5.0" };
|
||||
}
|
||||
}
|
||||
|
||||
const macroStoppage = thresholds.macroStoppageMultiplier;
|
||||
if (macroStoppage != null) {
|
||||
const v = Number(macroStoppage);
|
||||
if (!Number.isFinite(v) || v < 1.1 || v > 20.0) {
|
||||
return { ok: false, error: "macroStoppageMultiplier must be 1.1-20.0" };
|
||||
}
|
||||
}
|
||||
|
||||
const oee = thresholds.oeeAlertThresholdPct;
|
||||
if (oee != null) {
|
||||
const v = Number(oee);
|
||||
if (!Number.isFinite(v) || v < 50 || v > 100) {
|
||||
return { ok: false, error: "oeeAlertThresholdPct must be 50-100" };
|
||||
}
|
||||
}
|
||||
|
||||
const perf = thresholds.performanceThresholdPct;
|
||||
if (perf != null) {
|
||||
const v = Number(perf);
|
||||
if (!Number.isFinite(v) || v < 50 || v > 100) {
|
||||
return { ok: false, error: "performanceThresholdPct must be 50-100" };
|
||||
}
|
||||
}
|
||||
|
||||
const quality = thresholds.qualitySpikeDeltaPct;
|
||||
if (quality != null) {
|
||||
const v = Number(quality);
|
||||
if (!Number.isFinite(v) || v < 0 || v > 100) {
|
||||
return { ok: false, error: "qualitySpikeDeltaPct must be 0-100" };
|
||||
}
|
||||
}
|
||||
|
||||
return { ok: true };
|
||||
}
|
||||
|
||||
export function validateDefaults(defaults: unknown) {
|
||||
if (!isPlainObject(defaults)) return { ok: true };
|
||||
|
||||
const moldTotal = defaults.moldTotal != null ? Number(defaults.moldTotal) : null;
|
||||
const moldActive = defaults.moldActive != null ? Number(defaults.moldActive) : null;
|
||||
|
||||
if (moldTotal != null && (!Number.isFinite(moldTotal) || moldTotal < 0)) {
|
||||
return { ok: false, error: "moldTotal must be >= 0" };
|
||||
}
|
||||
|
||||
if (moldActive != null && (!Number.isFinite(moldActive) || moldActive < 0)) {
|
||||
return { ok: false, error: "moldActive must be >= 0" };
|
||||
}
|
||||
|
||||
if (moldTotal != null && moldActive != null && moldActive > moldTotal) {
|
||||
return { ok: false, error: "moldActive must be <= moldTotal" };
|
||||
}
|
||||
|
||||
return { ok: true };
|
||||
}
|
||||
|
||||
export function pickUpdateValue(input: unknown) {
|
||||
return input === undefined ? undefined : input;
|
||||
}
|
||||
|
||||
export function stripUndefined(obj: AnyRecord) {
|
||||
const out: AnyRecord = {};
|
||||
for (const [key, value] of Object.entries(obj)) {
|
||||
if (value !== undefined) out[key] = value;
|
||||
}
|
||||
return out;
|
||||
}
|
||||
9
lib/sms.ts
Normal file
9
lib/sms.ts
Normal file
@@ -0,0 +1,9 @@
|
||||
type SmsPayload = {
|
||||
to: string;
|
||||
body: string;
|
||||
};
|
||||
|
||||
export async function sendSms(payload: SmsPayload) {
|
||||
void payload;
|
||||
throw new Error("SMS not configured");
|
||||
}
|
||||
52
lib/ui/screenlessMode.ts
Normal file
52
lib/ui/screenlessMode.ts
Normal file
@@ -0,0 +1,52 @@
|
||||
"use client";
|
||||
|
||||
import { useEffect, useState } from "react";
|
||||
|
||||
let current: boolean | null = null;
|
||||
let inflight: Promise<void> | null = null;
|
||||
const listeners = new Set<(next: boolean) => void>();
|
||||
|
||||
function notify(next: boolean) {
|
||||
current = next;
|
||||
listeners.forEach((fn) => fn(next));
|
||||
}
|
||||
|
||||
async function loadScreenlessMode() {
|
||||
if (inflight) return inflight;
|
||||
inflight = (async () => {
|
||||
try {
|
||||
const res = await fetch("/api/settings", { cache: "no-store" });
|
||||
const data = await res.json().catch(() => ({}));
|
||||
if (res.ok && data?.ok) {
|
||||
const mode = data?.settings?.modules?.screenlessMode === true;
|
||||
notify(mode);
|
||||
}
|
||||
} catch {
|
||||
// ignore fetch failures; keep current state
|
||||
} finally {
|
||||
inflight = null;
|
||||
}
|
||||
})();
|
||||
return inflight;
|
||||
}
|
||||
|
||||
export function useScreenlessMode() {
|
||||
const [screenlessMode, setScreenlessMode] = useState(current ?? false);
|
||||
|
||||
useEffect(() => {
|
||||
listeners.add(setScreenlessMode);
|
||||
return () => {
|
||||
listeners.delete(setScreenlessMode);
|
||||
};
|
||||
}, []);
|
||||
|
||||
useEffect(() => {
|
||||
void loadScreenlessMode();
|
||||
}, []);
|
||||
|
||||
function set(next: boolean) {
|
||||
notify(next);
|
||||
}
|
||||
|
||||
return { screenlessMode, setScreenlessMode: set };
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user