Mobile friendly, lint correction, typescript error clear

This commit is contained in:
Marcelo
2026-01-16 22:39:16 +00:00
parent 0f88207f3f
commit c183dda383
58 changed files with 7199 additions and 2714 deletions

View 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>
);
}

View File

@@ -1,796 +1,46 @@
"use client"; 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";
import { useEffect, useMemo, useState } from "react"; export default async function AlertsPage() {
import { useI18n } from "@/lib/i18n/useI18n"; const session = await requireSession();
if (!session) redirect("/login?next=/alerts");
type RoleName = "MEMBER" | "ADMIN" | "OWNER"; const [machines, shiftRows, inbox] = await Promise.all([
type Channel = "email" | "sms"; prisma.machine.findMany({
where: { orgId: session.orgId },
type RoleRule = { orderBy: { createdAt: "desc" },
enabled: boolean; select: { id: true, name: true, location: true },
afterMinutes: number; }),
channels: Channel[]; prisma.orgShift.findMany({
}; where: { orgId: session.orgId },
orderBy: { sortOrder: "asc" },
type AlertRule = { select: { name: true, enabled: true },
id: string; }),
eventType: string; getAlertsInboxData({
roles: Record<RoleName, RoleRule>; orgId: session.orgId,
repeatMinutes?: number; range: "24h",
}; limit: 250,
}),
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 default function AlertsPage() {
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; const initialEvents = inbox.events.map((event) => ({
...event,
if (!policyRes.ok || !policyJson?.ok) { ts: event.ts ? event.ts.toISOString() : "",
setPolicyError(policyJson?.error || t("alerts.error.loadPolicy"));
} else {
setPolicy(policyJson.policy);
setPolicyDraft(policyJson.policy);
if (!selectedEventType && policyJson.policy?.rules?.length) {
setSelectedEventType(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"));
return;
}
setPolicy(policyDraft);
} catch {
setPolicyError(t("alerts.error.savePolicy"));
} finally {
setSavingPolicy(false);
}
}
function updateContactDraft(id: string, patch: Partial<ContactDraft>) {
setContactEdits((prev) => ({
...prev,
[id]: {
...(prev[id] ?? normalizeContactDraft(contacts.find((c) => c.id === id)!)),
...patch,
},
})); }));
}
async function saveContact(id: string) { const initialShifts = shiftRows.map((shift) => ({
const draft = contactEdits[id]; name: shift.name,
if (!draft) return; enabled: shift.enabled !== false,
setSavingContactId(id); }));
try {
const eventTypes = draft.eventTypes.length ? draft.eventTypes : null;
const res = await fetch(`/api/alerts/contacts/${id}`, {
method: "PATCH",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
name: draft.name,
roleScope: draft.roleScope,
email: draft.email || null,
phone: draft.phone || null,
eventTypes,
isActive: draft.isActive,
}),
});
const json = await res.json().catch(() => ({}));
if (!res.ok || !json?.ok) {
setContactsError(json?.error || t("alerts.error.saveContacts"));
return;
}
const updated = json.contact as AlertContact;
setContacts((prev) => prev.map((c) => (c.id === id ? updated : c)));
setContactEdits((prev) => ({ ...prev, [id]: normalizeContactDraft(updated) }));
} catch {
setContactsError(t("alerts.error.saveContacts"));
} 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"));
return;
}
setContacts((prev) => prev.filter((c) => c.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 eventTypes = newContact.eventTypes.length ? newContact.eventTypes : null;
const res = await fetch("/api/alerts/contacts", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
name: newContact.name,
roleScope: newContact.roleScope,
email: newContact.email || null,
phone: newContact.phone || null,
eventTypes,
}),
});
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 ( return (
<div className="mx-auto flex w-full max-w-6xl flex-col gap-6 px-6 py-8"> <AlertsClient
<div> initialMachines={machines}
<h1 className="text-2xl font-semibold text-white">{t("alerts.title")}</h1> initialShifts={initialShifts}
<p className="mt-2 text-sm text-zinc-400">{t("alerts.subtitle")}</p> initialEvents={initialEvents}
</div>
{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>
); );
} }

View 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>
);
}

View 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 }}
/>
);
}

View File

@@ -6,7 +6,10 @@ import { prisma } from "@/lib/prisma";
const COOKIE_NAME = "mis_session"; const COOKIE_NAME = "mis_session";
export default async function AppLayout({ children }: { children: React.ReactNode }) { 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"); if (!sessionId) redirect("/login?next=/machines");
@@ -24,5 +27,5 @@ export default async function AppLayout({ children }: { children: React.ReactNod
redirect("/login?next=/machines"); redirect("/login?next=/machines");
} }
return <AppShell>{children}</AppShell>; return <AppShell initialTheme={initialTheme}>{children}</AppShell>;
} }

View File

@@ -0,0 +1,325 @@
"use client";
import Link from "next/link";
import { useEffect, useState } 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 [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);
}
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 (
<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 : 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>
</Link>
);
})}
</div>
</div>
);
}

View File

@@ -1,6 +1,6 @@
"use client"; "use client";
import { useEffect, useMemo, useRef, useState } from "react"; import { useEffect, useMemo, useRef, useState, type ChangeEvent } from "react";
import Link from "next/link"; import Link from "next/link";
import { useParams } from "next/navigation"; import { useParams } from "next/navigation";
import { import {
@@ -73,6 +73,7 @@ type MachineDetail = {
name: string; name: string;
code?: string | null; code?: string | null;
location?: string | null; location?: string | null;
effectiveCycleTime?: number | null;
latestHeartbeat: Heartbeat | null; latestHeartbeat: Heartbeat | null;
latestKpi: Kpi | null; latestKpi: Kpi | null;
}; };
@@ -111,11 +112,54 @@ type WorkOrderUpload = {
cycleTime?: number; cycleTime?: number;
}; };
type WorkOrderRow = Record<string, string | number | boolean>;
type TooltipPayload<T> = { payload?: T; name?: string; value?: number | string };
type SimpleTooltipProps<T> = {
active?: boolean;
payload?: Array<TooltipPayload<T>>;
label?: string | number;
};
type ActiveRingProps = { cx?: number; cy?: number; fill?: string };
type ScatterPointProps = { cx?: number; cy?: number; payload?: { bucket?: string } };
const TOL = 0.10; const TOL = 0.10;
const DEFAULT_MICRO_MULT = 1.5; const DEFAULT_MICRO_MULT = 1.5;
const DEFAULT_MACRO_MULT = 5; const DEFAULT_MACRO_MULT = 5;
const NORMAL_TOL_SEC = 0.1; const NORMAL_TOL_SEC = 0.1;
const BUCKET = {
normal: {
labelKey: "machine.detail.bucket.normal",
dot: "#12D18E",
glow: "rgba(18,209,142,.35)",
chip: "bg-emerald-500/15 text-emerald-300 border-emerald-500/20",
},
slow: {
labelKey: "machine.detail.bucket.slow",
dot: "#F7B500",
glow: "rgba(247,181,0,.35)",
chip: "bg-yellow-500/15 text-yellow-300 border-yellow-500/20",
},
microstop: {
labelKey: "machine.detail.bucket.microstop",
dot: "#FF7A00",
glow: "rgba(255,122,0,.35)",
chip: "bg-orange-500/15 text-orange-300 border-orange-500/20",
},
macrostop: {
labelKey: "machine.detail.bucket.macrostop",
dot: "#FF3B5C",
glow: "rgba(255,59,92,.35)",
chip: "bg-rose-500/15 text-rose-300 border-rose-500/20",
},
unknown: {
labelKey: "machine.detail.bucket.unknown",
dot: "#A1A1AA",
glow: "rgba(161,161,170,.25)",
chip: "bg-white/10 text-zinc-200 border-white/10",
},
} as const;
function resolveMultipliers(thresholds?: Thresholds | null) { function resolveMultipliers(thresholds?: Thresholds | null) {
const micro = Number(thresholds?.stoppageMultiplier ?? DEFAULT_MICRO_MULT); const micro = Number(thresholds?.stoppageMultiplier ?? DEFAULT_MICRO_MULT);
@@ -213,14 +257,14 @@ function parseCsvText(text: string) {
}); });
} }
function pickRowValue(row: Record<string, any>, keys: Set<string>) { function pickRowValue(row: WorkOrderRow, keys: Set<string>) {
for (const [key, value] of Object.entries(row)) { for (const [key, value] of Object.entries(row)) {
if (keys.has(normalizeKey(key))) return value; if (keys.has(normalizeKey(key))) return value;
} }
return undefined; return undefined;
} }
function rowsToWorkOrders(rows: Array<Record<string, any>>): WorkOrderUpload[] { function rowsToWorkOrders(rows: WorkOrderRow[]): WorkOrderUpload[] {
const seen = new Set<string>(); const seen = new Set<string>();
const out: WorkOrderUpload[] = []; const out: WorkOrderUpload[] = [];
@@ -261,42 +305,8 @@ export default function MachineDetailClient() {
const [open, setOpen] = useState<null | "events" | "deviation" | "impact">(null); const [open, setOpen] = useState<null | "events" | "deviation" | "impact">(null);
const fileInputRef = useRef<HTMLInputElement | null>(null); const fileInputRef = useRef<HTMLInputElement | null>(null);
const [uploadState, setUploadState] = useState<UploadState>({ status: "idle" }); const [uploadState, setUploadState] = useState<UploadState>({ status: "idle" });
const [nowMs, setNowMs] = useState(() => Date.now());
const BUCKET = {
normal: {
labelKey: "machine.detail.bucket.normal",
dot: "#12D18E",
glow: "rgba(18,209,142,.35)",
chip: "bg-emerald-500/15 text-emerald-300 border-emerald-500/20",
},
slow: {
labelKey: "machine.detail.bucket.slow",
dot: "#F7B500",
glow: "rgba(247,181,0,.35)",
chip: "bg-yellow-500/15 text-yellow-300 border-yellow-500/20",
},
microstop: {
labelKey: "machine.detail.bucket.microstop",
dot: "#FF7A00",
glow: "rgba(255,122,0,.35)",
chip: "bg-orange-500/15 text-orange-300 border-orange-500/20",
},
macrostop: {
labelKey: "machine.detail.bucket.macrostop",
dot: "#FF3B5C",
glow: "rgba(255,59,92,.35)",
chip: "bg-rose-500/15 text-rose-300 border-rose-500/20",
},
unknown: {
labelKey: "machine.detail.bucket.unknown",
dot: "#A1A1AA",
glow: "rgba(161,161,170,.25)",
chip: "bg-white/10 text-zinc-200 border-white/10",
},
} as const;
useEffect(() => { useEffect(() => {
if (!machineId) return; if (!machineId) return;
@@ -305,9 +315,16 @@ export default function MachineDetailClient() {
async function load() { async function load() {
try { try {
const res = await fetch(`/api/machines/${machineId}?windowSec=3600&events=critical`, { const res = await fetch(`/api/machines/${machineId}?windowSec=3600&events=critical`, {
cache: "no-store", cache: "no-cache",
credentials: "include", credentials: "include",
}); });
if (res.status === 304) {
if (!alive) return;
setLoading(false);
return;
}
const json = await res.json().catch(() => ({})); const json = await res.json().catch(() => ({}));
if (!alive) return; if (!alive) return;
@@ -341,31 +358,30 @@ export default function MachineDetailClient() {
}; };
}, [machineId, t]); }, [machineId, t]);
useEffect(() => {
const timer = setInterval(() => setNowMs(Date.now()), 1000);
return () => clearInterval(timer);
}, []);
useEffect(() => { useEffect(() => {
if (open !== "events" || !machineId) return; if (open !== "events" || !machineId) return;
let alive = true; let alive = true;
setDetectedEventsLoading(true); setDetectedEventsLoading(true);
fetch(`/api/machines/${machineId}?events=all&eventsOnly=1&eventsWindowSec=21600`, { async function loadDetected() {
cache: "no-store", try {
const res = await fetch(`/api/machines/${machineId}?events=all&eventsOnly=1&eventsWindowSec=21600`, {
cache: "no-cache",
credentials: "include", credentials: "include",
}) });
.then((res) => res.json()) if (res.status === 304) return;
.then((json) => { const json = await res.json().catch(() => ({}));
if (!alive) return; if (!alive) return;
setDetectedEvents(json.events ?? []); setDetectedEvents(json.events ?? []);
setEventsCountAll(typeof json.eventsCountAll === "number" ? json.eventsCountAll : eventsCountAll); setEventsCountAll(typeof json.eventsCountAll === "number" ? json.eventsCountAll : eventsCountAll);
}) } catch {
.catch(() => {}) } finally {
.finally(() => {
if (alive) setDetectedEventsLoading(false); if (alive) setDetectedEventsLoading(false);
}); }
}
loadDetected();
return () => { return () => {
alive = false; alive = false;
@@ -385,15 +401,15 @@ export default function MachineDetailClient() {
const workbook = xlsx.read(buffer, { type: "array" }); const workbook = xlsx.read(buffer, { type: "array" });
const sheet = workbook.Sheets[workbook.SheetNames[0]]; const sheet = workbook.Sheets[workbook.SheetNames[0]];
if (!sheet) return []; if (!sheet) return [];
const rows = xlsx.utils.sheet_to_json(sheet, { defval: "" }); const rows = xlsx.utils.sheet_to_json<WorkOrderRow>(sheet, { defval: "" });
return rowsToWorkOrders(rows as Array<Record<string, any>>); return rowsToWorkOrders(rows);
} }
return null; return null;
} }
async function handleWorkOrderUpload(event: any) { async function handleWorkOrderUpload(event: ChangeEvent<HTMLInputElement>) {
const file = event?.target?.files?.[0] as File | undefined; const file = event.target.files?.[0];
if (!file) return; if (!file) return;
if (!machineId) { if (!machineId) {
@@ -477,19 +493,6 @@ export default function MachineDetailClient() {
return `${v}`; return `${v}`;
} }
function formatDurationShort(totalSec?: number | null) {
if (totalSec === null || totalSec === undefined || Number.isNaN(totalSec)) {
return t("common.na");
}
const sec = Math.max(0, Math.floor(totalSec));
const h = Math.floor(sec / 3600);
const m = Math.floor((sec % 3600) / 60);
const s = sec % 60;
if (h > 0) return `${h}h ${m}m`;
if (m > 0) return `${m}m ${s}s`;
return `${s}s`;
}
function timeAgo(ts?: string) { function timeAgo(ts?: string) {
if (!ts) return t("common.never"); if (!ts) return t("common.never");
@@ -554,15 +557,14 @@ export default function MachineDetailClient() {
const label = t(key); const label = t(key);
return label === key ? normalizedStatus : label; return label === key ? normalizedStatus : label;
})(); })();
const cycleTarget = (machine as any)?.effectiveCycleTime ?? kpi?.cycleTime ?? null; const cycleTarget = machine?.effectiveCycleTime ?? kpi?.cycleTime ?? null;
const machineCode = machine?.code ?? t("common.na"); const machineCode = machine?.code ?? t("common.na");
const machineLocation = machine?.location ?? t("common.na"); const machineLocation = machine?.location ?? t("common.na");
const lastSeenLabel = t("machine.detail.lastSeen", { const lastSeenLabel = t("machine.detail.lastSeen", {
time: hbTs ? timeAgo(hbTs) : t("common.never"), time: hbTs ? timeAgo(hbTs) : t("common.never"),
}); });
const ActiveRing = (props: any) => { const ActiveRing = ({ cx, cy, fill }: ActiveRingProps) => {
const { cx, cy, fill } = props;
if (cx == null || cy == null) return null; if (cx == null || cy == null) return null;
return ( return (
<g> <g>
@@ -609,12 +611,84 @@ export default function MachineDetailClient() {
} }
function MachineActivityTimeline({ function MachineActivityTimeline({
segments, cycles,
windowSec, cycleTarget,
thresholds,
activeStoppage,
}: { }: {
segments: TimelineSeg[]; cycles: CycleRow[];
windowSec: number; cycleTarget: number | null;
thresholds: Thresholds | null;
activeStoppage: ActiveStoppage | null;
}) { }) {
const [nowMs, setNowMs] = useState(() => Date.now());
useEffect(() => {
const timer = setInterval(() => setNowMs(Date.now()), 1000);
return () => clearInterval(timer);
}, []);
const timeline = useMemo(() => {
const rows = cycles ?? [];
const windowSec = rows.length < 1 ? 10800 : 3600;
const end = nowMs;
const start = end - windowSec * 1000;
if (rows.length < 1) {
return {
windowSec,
segments: [] as TimelineSeg[],
start,
end,
};
}
const segs: TimelineSeg[] = [];
for (const cycle of rows) {
const ideal = (cycle.ideal ?? cycleTarget ?? 0) as number;
const actual = cycle.actual ?? 0;
if (!ideal || ideal <= 0 || !actual || actual <= 0) continue;
const cycleEnd = cycle.t;
const cycleStart = cycleEnd - actual * 1000;
if (cycleEnd <= start || cycleStart >= end) continue;
const segStart = Math.max(cycleStart, start);
const segEnd = Math.min(cycleEnd, end);
if (segEnd <= segStart) continue;
const state = classifyCycleDuration(actual, ideal, thresholds);
segs.push({
start: segStart,
end: segEnd,
durationSec: (segEnd - segStart) / 1000,
state,
});
}
if (activeStoppage?.startedAt) {
const stoppageStart = new Date(activeStoppage.startedAt).getTime();
const segStart = Math.max(stoppageStart, start);
const segEnd = Math.min(end, nowMs);
if (segEnd > segStart) {
segs.push({
start: segStart,
end: segEnd,
durationSec: (segEnd - segStart) / 1000,
state: activeStoppage.state,
});
}
}
segs.sort((a, b) => a.start - b.start);
return { windowSec, segments: segs, start, end };
}, [activeStoppage, cycles, cycleTarget, nowMs, thresholds]);
const { segments, windowSec } = timeline;
return ( return (
<div className="rounded-2xl border border-white/10 bg-white/5 p-5"> <div className="rounded-2xl border border-white/10 bg-white/5 p-5">
<div className="flex items-start justify-between gap-4"> <div className="flex items-start justify-between gap-4">
@@ -647,7 +721,7 @@ export default function MachineDetailClient() {
</div> </div>
) : ( ) : (
segments.map((seg, idx) => { segments.map((seg, idx) => {
const wPct = Math.max(0.25, (seg.durationSec / windowSec) * 100); const wPct = Math.max(0, (seg.durationSec / windowSec) * 100);
const meta = BUCKET[seg.state]; const meta = BUCKET[seg.state];
const glow = const glow =
seg.state === "microstop" || seg.state === "macrostop" seg.state === "microstop" || seg.state === "macrostop"
@@ -718,11 +792,16 @@ export default function MachineDetailClient() {
); );
} }
function CycleTooltip({ active, payload, label }: any) { function CycleTooltip({
active,
payload,
label,
}: SimpleTooltipProps<{ actual?: number; ideal?: number; deltaPct?: number }>) {
if (!active || !payload?.length) return null; if (!active || !payload?.length) return null;
const p = payload[0]?.payload; const p = payload[0]?.payload;
if (!p) return null; if (!p) return null;
const safeLabel = label ?? "";
const ideal = p.ideal ?? null; const ideal = p.ideal ?? null;
const actual = p.actual ?? null; const actual = p.actual ?? null;
@@ -731,7 +810,7 @@ export default function MachineDetailClient() {
return ( return (
<div className="rounded-xl border border-white/10 bg-zinc-950/95 px-4 py-3 shadow-lg"> <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"> <div className="text-sm font-semibold text-white">
{t("machine.detail.tooltip.cycle", { label })} {t("machine.detail.tooltip.cycle", { label: safeLabel })}
</div> </div>
<div className="mt-2 space-y-1 text-xs text-zinc-300"> <div className="mt-2 space-y-1 text-xs text-zinc-300">
<div> <div>
@@ -756,7 +835,6 @@ export default function MachineDetailClient() {
const cycleDerived = useMemo(() => { const cycleDerived = useMemo(() => {
const rows = cycles ?? []; const rows = cycles ?? [];
const { micro, macro } = resolveMultipliers(thresholds);
const mapped: CycleDerivedRow[] = rows.map((cycle) => { const mapped: CycleDerivedRow[] = rows.map((cycle) => {
const ideal = cycle.ideal ?? null; const ideal = cycle.ideal ?? null;
@@ -833,62 +911,15 @@ export default function MachineDetailClient() {
const total = rows.reduce((sum, row) => sum + row.seconds, 0); const total = rows.reduce((sum, row) => sum + row.seconds, 0);
return { rows, total }; return { rows, total };
}, [BUCKET, cycleDerived.mapped, t]); }, [cycleDerived.mapped, t]);
const timeline = useMemo(() => {
const rows = cycles ?? [];
if (rows.length < 1) {
return {
windowSec: 10800,
segments: [] as TimelineSeg[],
start: null as number | null,
end: null as number | null,
};
}
const windowSec = 3600;
const end = rows[rows.length - 1].t;
const start = end - windowSec * 1000;
const segs: TimelineSeg[] = [];
for (const cycle of rows) {
const ideal = (cycle.ideal ?? cycleTarget ?? 0) as number;
const actual = cycle.actual ?? 0;
if (!ideal || ideal <= 0 || !actual || actual <= 0) continue;
const cycleEnd = cycle.t;
const cycleStart = cycleEnd - actual * 1000;
if (cycleEnd <= start || cycleStart >= end) continue;
const segStart = Math.max(cycleStart, start);
const segEnd = Math.min(cycleEnd, end);
if (segEnd <= segStart) continue;
const state = classifyCycleDuration(actual, ideal, thresholds);
segs.push({
start: segStart,
end: segEnd,
durationSec: (segEnd - segStart) / 1000,
state,
});
}
return { windowSec, segments: segs, start, end };
}, [cycles, cycleTarget, thresholds]);
const cycleTargetLabel = cycleTarget ? `${cycleTarget}s` : t("common.na"); const cycleTargetLabel = cycleTarget ? `${cycleTarget}s` : t("common.na");
const workOrderLabel = kpi?.workOrderId ?? t("common.na"); const workOrderLabel = kpi?.workOrderId ?? t("common.na");
const skuLabel = kpi?.sku ?? t("common.na"); const skuLabel = kpi?.sku ?? t("common.na");
return ( return (
<div className="p-6"> <div className="p-4 sm:p-6">
<div className="mb-6 flex items-start justify-between gap-4"> <div className="mb-6 flex flex-col gap-4 sm:flex-row sm:items-start sm:justify-between">
<div className="min-w-0"> <div className="min-w-0">
<div className="flex flex-wrap items-center gap-3"> <div className="flex flex-wrap items-center gap-3">
<h1 className="truncate text-2xl font-semibold text-white"> <h1 className="truncate text-2xl font-semibold text-white">
@@ -903,8 +934,8 @@ export default function MachineDetailClient() {
</div> </div>
</div> </div>
<div className="flex shrink-0 flex-col items-end gap-2"> <div className="flex w-full flex-col items-start gap-2 sm:w-auto sm:items-end">
<div className="flex items-center gap-2"> <div className="flex w-full flex-wrap items-center gap-2 sm:w-auto sm:flex-nowrap">
<input <input
ref={fileInputRef} ref={fileInputRef}
type="file" type="file"
@@ -916,18 +947,18 @@ export default function MachineDetailClient() {
type="button" type="button"
onClick={() => fileInputRef.current?.click()} onClick={() => fileInputRef.current?.click()}
disabled={isUploading} disabled={isUploading}
className="rounded-xl border border-emerald-500/30 bg-emerald-500/10 px-4 py-2 text-sm text-emerald-100 transition hover:bg-emerald-500/20 disabled:cursor-not-allowed disabled:opacity-60" className="w-full rounded-xl border border-emerald-500/30 bg-emerald-500/10 px-4 py-2 text-sm text-emerald-100 transition hover:bg-emerald-500/20 disabled:cursor-not-allowed disabled:opacity-60 sm:w-auto"
> >
{uploadButtonLabel} {uploadButtonLabel}
</button> </button>
<Link <Link
href="/machines" href="/machines"
className="rounded-xl border border-white/10 bg-white/5 px-4 py-2 text-sm text-white hover:bg-white/10" 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("machine.detail.back")} {t("machine.detail.back")}
</Link> </Link>
</div> </div>
<div className="text-right text-[11px] text-zinc-500"> <div className="text-left text-[11px] text-zinc-500 sm:text-right">
{t("machine.detail.workOrders.uploadHint")} {t("machine.detail.workOrders.uploadHint")}
</div> </div>
{uploadState.status !== "idle" && uploadState.message && ( {uploadState.status !== "idle" && uploadState.message && (
@@ -975,7 +1006,12 @@ export default function MachineDetailClient() {
</div> </div>
<div className="mt-6"> <div className="mt-6">
<MachineActivityTimeline segments={timeline.segments} windowSec={timeline.windowSec} /> <MachineActivityTimeline
cycles={cycles}
cycleTarget={cycleTarget}
thresholds={thresholds}
activeStoppage={activeStoppage}
/>
</div> </div>
<div className="mt-6 grid grid-cols-1 gap-4 xl:grid-cols-3"> <div className="mt-6 grid grid-cols-1 gap-4 xl:grid-cols-3">
@@ -1197,9 +1233,9 @@ export default function MachineDetailClient() {
dataKey="actual" dataKey="actual"
isAnimationActive={false} isAnimationActive={false}
activeShape={<ActiveRing />} activeShape={<ActiveRing />}
shape={(props: any) => { shape={({ cx, cy, payload }: ScatterPointProps) => {
const { cx, cy, payload } = props; const meta =
const meta = BUCKET[payload.bucket as keyof typeof BUCKET] ?? BUCKET.unknown; BUCKET[(payload?.bucket as keyof typeof BUCKET) ?? "unknown"] ?? BUCKET.unknown;
return ( return (
<circle <circle
@@ -1259,7 +1295,10 @@ export default function MachineDetailClient() {
border: "1px solid var(--app-chart-tooltip-border)", border: "1px solid var(--app-chart-tooltip-border)",
}} }}
labelStyle={{ color: "var(--app-chart-label)" }} labelStyle={{ color: "var(--app-chart-label)" }}
formatter={(val: any) => [`${Number(val).toFixed(1)}s`, t("machine.detail.modal.extraTimeLabel")]} formatter={(val: number | string | undefined) => [
`${val == null ? 0 : Number(val).toFixed(1)}s`,
t("machine.detail.modal.extraTimeLabel"),
]}
/> />
<Bar dataKey="seconds" radius={[10, 10, 0, 0]} isAnimationActive={false}> <Bar dataKey="seconds" radius={[10, 10, 0, 0]} isAnimationActive={false}>
{impactAgg.rows.map((row, idx) => { {impactAgg.rows.map((row, idx) => {

View File

@@ -1,324 +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"; function toIso(value?: Date | null) {
import { useEffect, useState } from "react"; return value ? value.toISOString() : null;
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) { export default async function MachinesPage() {
if (!ts) return true; const session = await requireSession();
return Date.now() - new Date(ts).getTime() > 30000; // 30s threshold if (!session) redirect("/login?next=/machines");
}
function normalizeStatus(status?: string) { const machines = await prisma.machine.findMany({
const s = (status ?? "").toUpperCase(); where: { orgId: session.orgId },
if (s === "ONLINE") return "RUN"; orderBy: { createdAt: "desc" },
return s; select: {
} id: true,
name: true,
function badgeClass(status?: string, offline?: boolean) { code: true,
if (offline) return "bg-white/10 text-zinc-300"; location: true,
const s = (status ?? "").toUpperCase(); createdAt: true,
if (s === "RUN") return "bg-emerald-500/15 text-emerald-300"; updatedAt: true,
if (s === "IDLE") return "bg-yellow-500/15 text-yellow-300"; heartbeats: {
if (s === "STOP" || s === "DOWN") return "bg-red-500/15 text-red-300"; orderBy: { tsServer: "desc" },
return "bg-white/10 text-white"; take: 1,
} select: { ts: true, tsServer: true, status: true, message: true, ip: true, fwVersion: true },
},
export default function MachinesPage() { },
const { t, locale } = useI18n();
const [machines, setMachines] = useState<MachineRow[]>([]);
const [loading, setLoading] = useState(true);
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) { const initialMachines = machines.map((machine) => ({
throw new Error(data.error || t("machines.create.error.failed")); ...machine,
latestHeartbeat: machine.heartbeats[0]
? {
...machine.heartbeats[0],
ts: toIso(machine.heartbeats[0].ts) ?? "",
tsServer: toIso(machine.heartbeats[0].tsServer),
} }
: null,
heartbeats: undefined,
}));
const nextMachine = { return <MachinesClient initialMachines={initialMachines} />;
...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: any) {
setCreateError(err?.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);
}
const showCreateCard = showCreate || (!loading && machines.length === 0);
return (
<div className="p-6">
<div className="mb-6 flex items-center 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 items-center gap-2">
<button
type="button"
onClick={() => setShowCreate((prev) => !prev)}
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"
>
{showCreate ? t("machines.cancel") : t("machines.addMachine")}
</button>
<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"
>
{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 items-center justify-between gap-4">
<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 (
<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 : 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>
</Link>
);
})}
</div>
</div>
);
} }

View 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>
);
}

View File

@@ -1,522 +1,47 @@
"use client"; import { redirect } from "next/navigation";
import { requireSession } from "@/lib/auth/requireSession";
import { getOverviewData } from "@/lib/overview/getOverviewData";
import OverviewClient from "./OverviewClient";
import Link from "next/link"; function toIso(value?: Date | null) {
import { useEffect, useMemo, useState } from "react"; return value ? value.toISOString() : null;
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) { export default async function OverviewPage() {
if (!ts) return true; const session = await requireSession();
return Date.now() - new Date(ts).getTime() > OFFLINE_MS; if (!session) redirect("/login?next=/overview");
}
function normalizeStatus(status?: string) { const { machines, events } = await getOverviewData({
const s = (status ?? "").toUpperCase(); orgId: session.orgId,
if (s === "ONLINE") return "RUN"; eventsMode: "critical",
return s; eventsWindowSec: 21600,
} eventMachines: 6,
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"]) {
return "bg-white/10 text-zinc-200";
}
export default function OverviewPage() {
const { t, locale } = useI18n();
const [machines, setMachines] = useState<MachineRow[]>([]);
const [events, setEvents] = useState<EventRow[]>([]);
const [loading, setLoading] = useState(true);
const [eventsLoading, setEventsLoading] = useState(false);
useEffect(() => {
let alive = true;
async function load() {
try {
const res = await fetch("/api/machines", { cache: "no-store" });
const json = await res.json();
if (!alive) return;
setMachines(json.machines ?? []);
setLoading(false);
} catch {
if (!alive) return;
setLoading(false);
}
}
load();
const t = setInterval(load, 30000);
return () => {
alive = false;
clearInterval(t);
};
}, []);
useEffect(() => {
if (!machines.length) {
setEvents([]);
return;
}
let alive = true;
const controller = new AbortController();
async function loadEvents() {
setEventsLoading(true);
const sorted = [...machines].sort((a, b) => {
const at = heartbeatTime(a.latestHeartbeat);
const bt = heartbeatTime(b.latestHeartbeat);
const atMs = at ? new Date(at).getTime() : 0;
const btMs = bt ? new Date(bt).getTime() : 0;
return btMs - atMs;
}); });
const targets = sorted.slice(0, MAX_EVENT_MACHINES); const initialMachines = machines.map((machine) => ({
...machine,
try { createdAt: toIso(machine.createdAt),
const results = await Promise.all( updatedAt: toIso(machine.updatedAt),
targets.map(async (m) => { latestHeartbeat: machine.latestHeartbeat
const res = await fetch(`/api/machines/${m.id}?events=critical&eventsOnly=1`, { ? {
cache: "no-store", ...machine.latestHeartbeat,
signal: controller.signal, ts: toIso(machine.latestHeartbeat.ts) ?? "",
}); tsServer: toIso(machine.latestHeartbeat.tsServer),
const json = await res.json();
return { machine: m, payload: json };
})
);
if (!alive) return;
const combined: EventRow[] = [];
for (const { machine, payload } of results) {
const ingested = Array.isArray(payload?.events) ? payload.events : [];
for (const e of ingested) {
if (!e?.ts) continue;
combined.push({
...e,
machineId: machine.id,
machineName: machine.name,
source: "ingested",
});
} }
: null,
latestKpi: machine.latestKpi
? {
...machine.latestKpi,
ts: toIso(machine.latestKpi.ts) ?? "",
} }
: null,
}));
const seen = new Set<string>(); const initialEvents = events.map((event) => ({
const deduped = combined.filter((e) => { ...event,
const key = `${e.machineId ?? ""}-${e.eventType}-${e.ts}-${e.title}`; ts: event.ts ? event.ts.toISOString() : "",
if (seen.has(key)) return false; machineName: event.machineName ?? undefined,
seen.add(key); }));
return true;
});
deduped.sort((a, b) => new Date(b.ts).getTime() - new Date(a.ts).getTime()); return <OverviewClient initialMachines={initialMachines} initialEvents={initialEvents} />;
setEvents(deduped.slice(0, 30));
} catch {
if (!alive) return;
setEvents([]);
} finally {
if (alive) setEventsLoading(false);
}
}
loadEvents();
return () => {
alive = false;
controller.abort();
};
}, [machines]);
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-6">
<div className="mb-6 flex items-start justify-between gap-4">
<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="rounded-xl border border-white/10 bg-white/5 px-4 py-2 text-sm text-white hover:bg-white/10"
>
{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-3 gap-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>
);
} }

View 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>
);
}

View File

@@ -68,17 +68,19 @@ type ReportPayload = {
type MachineOption = { id: string; name: string }; type MachineOption = { id: string; name: string };
type FilterOptions = { workOrders: string[]; skus: string[] }; type FilterOptions = { workOrders: string[]; skus: string[] };
type Translator = (key: string, vars?: Record<string, string | number>) => 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) { function fmtPct(v?: number | null) {
if (v === null || v === undefined || Number.isNaN(v)) return "--"; if (v === null || v === undefined || Number.isNaN(v)) return "--";
return `${v.toFixed(1)}%`; return `${v.toFixed(1)}%`;
} }
function fmtNum(v?: number | null) {
if (v === null || v === undefined || Number.isNaN(v)) return "--";
return `${Math.round(v)}`;
}
function fmtDuration(sec?: number | null) { function fmtDuration(sec?: number | null) {
if (!sec) return "--"; if (!sec) return "--";
const h = Math.floor(sec / 3600); const h = Math.floor(sec / 3600);
@@ -104,7 +106,7 @@ function formatTickLabel(ts: string, range: RangeKey) {
return `${month}-${day}`; return `${month}-${day}`;
} }
function CycleTooltip({ active, payload, t }: any) { function CycleTooltip({ active, payload, t }: SimpleTooltipProps<CycleHistogramRow> & { t: Translator }) {
if (!active || !payload?.length) return null; if (!active || !payload?.length) return null;
const p = payload[0]?.payload; const p = payload[0]?.payload;
if (!p) return null; if (!p) return null;
@@ -141,7 +143,7 @@ function CycleTooltip({ active, payload, t }: any) {
); );
} }
function DowntimeTooltip({ active, payload, t }: any) { function DowntimeTooltip({ active, payload, t }: SimpleTooltipProps<{ name?: string; value?: number }> & { t: Translator }) {
if (!active || !payload?.length) return null; if (!active || !payload?.length) return null;
const row = payload[0]?.payload ?? {}; const row = payload[0]?.payload ?? {};
const label = row.name ?? payload[0]?.name ?? ""; const label = row.name ?? payload[0]?.name ?? "";
@@ -157,6 +159,15 @@ function DowntimeTooltip({ active, payload, t }: any) {
); );
} }
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) { function buildCsv(report: ReportPayload, t: Translator) {
const rows = new Map<string, Record<string, string | number>>(); const rows = new Map<string, Record<string, string | number>>();
const addSeries = (series: ReportTrendPoint[], key: string) => { const addSeries = (series: ReportTrendPoint[], key: string) => {
@@ -386,13 +397,29 @@ export default function ReportsPage() {
const res = await fetch("/api/machines", { cache: "no-store" }); const res = await fetch("/api/machines", { cache: "no-store" });
const json = await res.json(); const json = await res.json();
if (!alive) return; if (!alive) return;
setMachines((json?.machines ?? []).map((m: any) => ({ id: m.id, name: m.name }))); 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 { } catch {
if (!alive) return; if (!alive) return;
setMachines([]); setMachines([]);
} }
} }
loadMachines();
return () => {
alive = false;
};
}, []);
useEffect(() => {
let alive = true;
const controller = new AbortController();
async function load() { async function load() {
setLoading(true); setLoading(true);
setError(null); setError(null);
@@ -402,7 +429,10 @@ export default function ReportsPage() {
if (workOrderId) params.set("workOrderId", workOrderId); if (workOrderId) params.set("workOrderId", workOrderId);
if (sku) params.set("sku", sku); if (sku) params.set("sku", sku);
const res = await fetch(`/api/reports?${params.toString()}`, { cache: "no-store" }); const res = await fetch(`/api/reports?${params.toString()}`, {
cache: "no-store",
signal: controller.signal,
});
const json = await res.json(); const json = await res.json();
if (!alive) return; if (!alive) return;
if (!res.ok || json?.ok === false) { if (!res.ok || json?.ok === false) {
@@ -420,21 +450,25 @@ export default function ReportsPage() {
} }
} }
loadMachines();
load(); load();
return () => { return () => {
alive = false; alive = false;
controller.abort();
}; };
}, [range, machineId, workOrderId, sku]); }, [range, machineId, workOrderId, sku, t]);
useEffect(() => { useEffect(() => {
let alive = true; let alive = true;
const controller = new AbortController();
async function loadFilters() { async function loadFilters() {
try { try {
const params = new URLSearchParams({ range }); const params = new URLSearchParams({ range });
if (machineId) params.set("machineId", machineId); if (machineId) params.set("machineId", machineId);
const res = await fetch(`/api/reports/filters?${params.toString()}`, { cache: "no-store" }); const res = await fetch(`/api/reports/filters?${params.toString()}`, {
cache: "no-store",
signal: controller.signal,
});
const json = await res.json(); const json = await res.json();
if (!alive) return; if (!alive) return;
if (!res.ok || json?.ok === false) { if (!res.ok || json?.ok === false) {
@@ -454,6 +488,7 @@ export default function ReportsPage() {
loadFilters(); loadFilters();
return () => { return () => {
alive = false; alive = false;
controller.abort();
}; };
}, [range, machineId]); }, [range, machineId]);
@@ -536,23 +571,23 @@ export default function ReportsPage() {
}; };
return ( return (
<div className="p-6"> <div className="p-4 sm:p-6">
<div className="mb-6 flex flex-wrap items-start justify-between gap-4"> <div className="mb-6 flex flex-col gap-4 sm:flex-row sm:items-start sm:justify-between">
<div> <div>
<h1 className="text-2xl font-semibold text-white">{t("reports.title")}</h1> <h1 className="text-2xl font-semibold text-white">{t("reports.title")}</h1>
<p className="text-sm text-zinc-400">{t("reports.subtitle")}</p> <p className="text-sm text-zinc-400">{t("reports.subtitle")}</p>
</div> </div>
<div className="flex items-center gap-2"> <div className="flex w-full flex-wrap items-center gap-2 sm:w-auto">
<button <button
onClick={handleExportCsv} onClick={handleExportCsv}
className="rounded-xl border border-white/10 bg-white/5 px-4 py-2 text-sm text-white hover:bg-white/10" 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")} {t("reports.exportCsv")}
</button> </button>
<button <button
onClick={handleExportPdf} onClick={handleExportPdf}
className="rounded-xl border border-white/10 bg-white/5 px-4 py-2 text-sm text-white hover:bg-white/10" 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")} {t("reports.exportPdf")}
</button> </button>
@@ -681,7 +716,10 @@ export default function ReportsPage() {
const row = payload?.[0]?.payload; const row = payload?.[0]?.payload;
return row?.ts ? new Date(row.ts).toLocaleString(locale) : ""; return row?.ts ? new Date(row.ts).toLocaleString(locale) : "";
}} }}
formatter={(val: any) => [`${Number(val).toFixed(1)}%`, "OEE"]} formatter={(val: number | string | undefined) => [
val == null ? "--" : `${Number(val).toFixed(1)}%`,
"OEE",
]}
/> />
<Line type="monotone" dataKey="value" stroke="#34d399" dot={false} strokeWidth={2} /> <Line type="monotone" dataKey="value" stroke="#34d399" dot={false} strokeWidth={2} />
</LineChart> </LineChart>
@@ -761,7 +799,10 @@ export default function ReportsPage() {
const row = payload?.[0]?.payload; const row = payload?.[0]?.payload;
return row?.ts ? new Date(row.ts).toLocaleString(locale) : ""; return row?.ts ? new Date(row.ts).toLocaleString(locale) : "";
}} }}
formatter={(val: any) => [`${Number(val).toFixed(1)}%`, t("reports.scrapRate")]} 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} /> <Line type="monotone" dataKey="value" stroke="#f97316" dot={false} strokeWidth={2} />
</LineChart> </LineChart>

View File

@@ -1,6 +1,8 @@
"use client"; "use client";
import { useCallback, useEffect, useMemo, useState } from "react"; import { useCallback, useEffect, useMemo, useState } from "react";
import { AlertsConfig } from "@/components/settings/AlertsConfig";
import { FinancialCostConfig } from "@/components/settings/FinancialCostConfig";
import { useI18n } from "@/lib/i18n/useI18n"; import { useI18n } from "@/lib/i18n/useI18n";
type Shift = { type Shift = {
@@ -101,28 +103,95 @@ const DEFAULT_SETTINGS: SettingsPayload = {
updatedBy: "", updatedBy: "",
}; };
async function readResponse(response: Response) { const SETTINGS_TABS = [
{ id: "general", labelKey: "settings.tabs.general" },
{ id: "shifts", labelKey: "settings.tabs.shifts" },
{ id: "thresholds", labelKey: "settings.tabs.thresholds" },
{ id: "alerts", labelKey: "settings.tabs.alerts" },
{ id: "financial", labelKey: "settings.tabs.financial" },
{ id: "team", labelKey: "settings.tabs.team" },
] as const;
type ReadResponse<T> = { data: T | null; text: string };
type ApiEnvelope = { ok: boolean; error?: string; message?: string };
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 unwrapApiResponse(data: unknown): { ok: boolean; error: string | null; record: Record<string, unknown> | null } {
const record = asRecord(data);
const ok = typeof record?.ok === "boolean" ? record.ok : false;
const error =
typeof record?.error === "string"
? record.error
: typeof record?.message === "string"
? record.message
: null;
return { ok, error, record };
}
function isOrgInfo(value: unknown): value is OrgInfo {
const record = asRecord(value);
return (
!!record &&
typeof record.id === "string" &&
typeof record.name === "string" &&
typeof record.slug === "string"
);
}
function isMemberRow(value: unknown): value is MemberRow {
const record = asRecord(value);
return (
!!record &&
typeof record.id === "string" &&
typeof record.membershipId === "string" &&
typeof record.email === "string" &&
typeof record.role === "string" &&
typeof record.isActive === "boolean" &&
typeof record.joinedAt === "string"
);
}
function isInviteRow(value: unknown): value is InviteRow {
const record = asRecord(value);
return (
!!record &&
typeof record.id === "string" &&
typeof record.email === "string" &&
typeof record.role === "string" &&
typeof record.token === "string" &&
typeof record.createdAt === "string" &&
typeof record.expiresAt === "string"
);
}
async function readResponse<T>(response: Response): Promise<ReadResponse<T>> {
const text = await response.text(); const text = await response.text();
if (!text) { if (!text) {
return { data: null as any, text: "" }; return { data: null, text: "" };
} }
try { try {
return { data: JSON.parse(text), text }; return { data: JSON.parse(text) as T, text };
} catch { } catch {
return { data: null as any, text }; return { data: null, text };
} }
} }
function normalizeShift(raw: any, index: number, fallbackName: string): Shift { function normalizeShift(raw: unknown, fallbackName: string): Shift {
const name = String(raw?.name || fallbackName); const record = asRecord(raw);
const start = String(raw?.start || raw?.startTime || DEFAULT_SHIFT.start); const name = String(record?.name ?? fallbackName);
const end = String(raw?.end || raw?.endTime || DEFAULT_SHIFT.end); const start = String(record?.start ?? record?.startTime ?? DEFAULT_SHIFT.start);
const enabled = raw?.enabled !== false; const end = String(record?.end ?? record?.endTime ?? DEFAULT_SHIFT.end);
const enabled = record?.enabled !== false;
return { name, start, end, enabled }; return { name, start, end, enabled };
} }
function normalizeSettings(raw: any, fallbackName: (index: number) => string): SettingsPayload { function normalizeSettings(raw: unknown, fallbackName: (index: number) => string): SettingsPayload {
if (!raw || typeof raw !== "object") { const record = asRecord(raw);
if (!record) {
return { return {
...DEFAULT_SETTINGS, ...DEFAULT_SETTINGS,
shiftSchedule: { shiftSchedule: {
@@ -132,16 +201,19 @@ function normalizeSettings(raw: any, fallbackName: (index: number) => string): S
}; };
} }
const shiftSchedule = raw.shiftSchedule || {}; const shiftSchedule = asRecord(record.shiftSchedule) ?? {};
const shiftsRaw = Array.isArray(shiftSchedule.shifts) ? shiftSchedule.shifts : []; const shiftsRaw = Array.isArray(shiftSchedule.shifts) ? shiftSchedule.shifts : [];
const shifts = shiftsRaw.length const shifts = shiftsRaw.length
? shiftsRaw.map((s: any, idx: number) => normalizeShift(s, idx, fallbackName(idx + 1))) ? shiftsRaw.map((s, idx) => normalizeShift(s, fallbackName(idx + 1)))
: [{ name: fallbackName(1), ...DEFAULT_SHIFT }]; : [{ name: fallbackName(1), ...DEFAULT_SHIFT }];
const thresholds = asRecord(record.thresholds) ?? {};
const alerts = asRecord(record.alerts) ?? {};
const defaults = asRecord(record.defaults) ?? {};
return { return {
orgId: String(raw.orgId || ""), orgId: String(record.orgId ?? ""),
version: Number(raw.version || 0), version: Number(record.version ?? 0),
timezone: String(raw.timezone || DEFAULT_SETTINGS.timezone), timezone: String(record.timezone ?? DEFAULT_SETTINGS.timezone),
shiftSchedule: { shiftSchedule: {
shifts, shifts,
shiftChangeCompensationMin: Number( shiftChangeCompensationMin: Number(
@@ -151,35 +223,38 @@ function normalizeSettings(raw: any, fallbackName: (index: number) => string): S
}, },
thresholds: { thresholds: {
stoppageMultiplier: Number( stoppageMultiplier: Number(
raw.thresholds?.stoppageMultiplier ?? DEFAULT_SETTINGS.thresholds.stoppageMultiplier thresholds.stoppageMultiplier ?? DEFAULT_SETTINGS.thresholds.stoppageMultiplier
), ),
macroStoppageMultiplier: Number( macroStoppageMultiplier: Number(
raw.thresholds?.macroStoppageMultiplier ?? DEFAULT_SETTINGS.thresholds.macroStoppageMultiplier thresholds.macroStoppageMultiplier ?? DEFAULT_SETTINGS.thresholds.macroStoppageMultiplier
), ),
oeeAlertThresholdPct: Number( oeeAlertThresholdPct: Number(
raw.thresholds?.oeeAlertThresholdPct ?? DEFAULT_SETTINGS.thresholds.oeeAlertThresholdPct thresholds.oeeAlertThresholdPct ?? DEFAULT_SETTINGS.thresholds.oeeAlertThresholdPct
), ),
performanceThresholdPct: Number( performanceThresholdPct: Number(
raw.thresholds?.performanceThresholdPct ?? DEFAULT_SETTINGS.thresholds.performanceThresholdPct thresholds.performanceThresholdPct ?? DEFAULT_SETTINGS.thresholds.performanceThresholdPct
), ),
qualitySpikeDeltaPct: Number( qualitySpikeDeltaPct: Number(
raw.thresholds?.qualitySpikeDeltaPct ?? DEFAULT_SETTINGS.thresholds.qualitySpikeDeltaPct thresholds.qualitySpikeDeltaPct ?? DEFAULT_SETTINGS.thresholds.qualitySpikeDeltaPct
), ),
}, },
alerts: { alerts: {
oeeDropEnabled: raw.alerts?.oeeDropEnabled ?? DEFAULT_SETTINGS.alerts.oeeDropEnabled, oeeDropEnabled: (alerts.oeeDropEnabled as boolean | undefined) ?? DEFAULT_SETTINGS.alerts.oeeDropEnabled,
performanceDegradationEnabled: performanceDegradationEnabled:
raw.alerts?.performanceDegradationEnabled ?? DEFAULT_SETTINGS.alerts.performanceDegradationEnabled, (alerts.performanceDegradationEnabled as boolean | undefined) ??
qualitySpikeEnabled: raw.alerts?.qualitySpikeEnabled ?? DEFAULT_SETTINGS.alerts.qualitySpikeEnabled, DEFAULT_SETTINGS.alerts.performanceDegradationEnabled,
qualitySpikeEnabled:
(alerts.qualitySpikeEnabled as boolean | undefined) ?? DEFAULT_SETTINGS.alerts.qualitySpikeEnabled,
predictiveOeeDeclineEnabled: predictiveOeeDeclineEnabled:
raw.alerts?.predictiveOeeDeclineEnabled ?? DEFAULT_SETTINGS.alerts.predictiveOeeDeclineEnabled, (alerts.predictiveOeeDeclineEnabled as boolean | undefined) ??
DEFAULT_SETTINGS.alerts.predictiveOeeDeclineEnabled,
}, },
defaults: { defaults: {
moldTotal: Number(raw.defaults?.moldTotal ?? DEFAULT_SETTINGS.defaults.moldTotal), moldTotal: Number(defaults.moldTotal ?? DEFAULT_SETTINGS.defaults.moldTotal),
moldActive: Number(raw.defaults?.moldActive ?? DEFAULT_SETTINGS.defaults.moldActive), moldActive: Number(defaults.moldActive ?? DEFAULT_SETTINGS.defaults.moldActive),
}, },
updatedAt: raw.updatedAt ? String(raw.updatedAt) : "", updatedAt: record.updatedAt ? String(record.updatedAt) : "",
updatedBy: raw.updatedBy ? String(raw.updatedBy) : "", updatedBy: record.updatedBy ? String(record.updatedBy) : "",
}; };
} }
@@ -235,6 +310,7 @@ export default function SettingsPage() {
const [inviteRole, setInviteRole] = useState("MEMBER"); const [inviteRole, setInviteRole] = useState("MEMBER");
const [inviteStatus, setInviteStatus] = useState<string | null>(null); const [inviteStatus, setInviteStatus] = useState<string | null>(null);
const [inviteSubmitting, setInviteSubmitting] = useState(false); const [inviteSubmitting, setInviteSubmitting] = useState(false);
const [activeTab, setActiveTab] = useState<(typeof SETTINGS_TABS)[number]["id"]>("general");
const defaultShiftName = useCallback( const defaultShiftName = useCallback(
(index: number) => t("settings.shift.defaultName", { index }), (index: number) => t("settings.shift.defaultName", { index }),
[t] [t]
@@ -246,11 +322,12 @@ export default function SettingsPage() {
try { try {
const response = await fetch("/api/settings", { cache: "no-store" }); const response = await fetch("/api/settings", { cache: "no-store" });
const { data, text } = await readResponse(response); const { data, text } = await readResponse(response);
if (!response.ok || !data?.ok) { const api = unwrapApiResponse(data);
const message = data?.error || data?.message || text || t("settings.failedLoad"); if (!response.ok || !api.ok) {
const message = api.error || text || t("settings.failedLoad");
throw new Error(message); throw new Error(message);
} }
const next = normalizeSettings(data.settings, defaultShiftName); const next = normalizeSettings(api.record?.settings, defaultShiftName);
setDraft(next); setDraft(next);
} catch (err) { } catch (err) {
setError(err instanceof Error ? err.message : t("settings.failedLoad")); setError(err instanceof Error ? err.message : t("settings.failedLoad"));
@@ -270,13 +347,16 @@ export default function SettingsPage() {
try { try {
const response = await fetch("/api/org/members", { cache: "no-store" }); const response = await fetch("/api/org/members", { cache: "no-store" });
const { data, text } = await readResponse(response); const { data, text } = await readResponse(response);
if (!response.ok || !data?.ok) { const api = unwrapApiResponse(data);
const message = data?.error || data?.message || text || t("settings.failedTeam"); if (!response.ok || !api.ok) {
const message = api.error || text || t("settings.failedTeam");
throw new Error(message); throw new Error(message);
} }
setOrgInfo(data.org ?? null); setOrgInfo(isOrgInfo(api.record?.org) ? api.record?.org : null);
setMembers(Array.isArray(data.members) ? data.members : []); const membersRaw = Array.isArray(api.record?.members) ? api.record?.members : [];
setInvites(Array.isArray(data.invites) ? data.invites : []); const invitesRaw = Array.isArray(api.record?.invites) ? api.record?.invites : [];
setMembers(membersRaw.filter(isMemberRow));
setInvites(invitesRaw.filter(isInviteRow));
} catch (err) { } catch (err) {
setTeamError(err instanceof Error ? err.message : t("settings.failedTeam")); setTeamError(err instanceof Error ? err.message : t("settings.failedTeam"));
} finally { } finally {
@@ -434,8 +514,9 @@ export default function SettingsPage() {
try { try {
const response = await fetch(`/api/org/invites/${inviteId}`, { method: "DELETE" }); const response = await fetch(`/api/org/invites/${inviteId}`, { method: "DELETE" });
const { data, text } = await readResponse(response); const { data, text } = await readResponse(response);
if (!response.ok || !data?.ok) { const api = unwrapApiResponse(data);
const message = data?.error || data?.message || text || t("settings.inviteStatus.failed"); if (!response.ok || !api.ok) {
const message = api.error || text || t("settings.inviteStatus.failed");
throw new Error(message); throw new Error(message);
} }
setInvites((prev) => prev.filter((invite) => invite.id !== inviteId)); setInvites((prev) => prev.filter((invite) => invite.id !== inviteId));
@@ -458,15 +539,16 @@ export default function SettingsPage() {
body: JSON.stringify({ email: inviteEmail, role: inviteRole }), body: JSON.stringify({ email: inviteEmail, role: inviteRole }),
}); });
const { data, text } = await readResponse(response); const { data, text } = await readResponse(response);
if (!response.ok || !data?.ok) { const api = unwrapApiResponse(data);
const message = data?.error || data?.message || text || t("settings.inviteStatus.createFailed"); if (!response.ok || !api.ok) {
const message = api.error || text || t("settings.inviteStatus.createFailed");
throw new Error(message); throw new Error(message);
} }
const nextInvite = data.invite; const nextInvite = api.record?.invite;
if (nextInvite) { if (isInviteRow(nextInvite)) {
setInvites((prev) => [nextInvite, ...prev.filter((invite) => invite.id !== nextInvite.id)]); setInvites((prev) => [nextInvite, ...prev.filter((invite) => invite.id !== nextInvite.id)]);
const inviteUrl = buildInviteUrl(nextInvite.token); const inviteUrl = buildInviteUrl(nextInvite.token);
if (data.emailSent === false) { if (api.record?.emailSent === false) {
setInviteStatus(t("settings.inviteStatus.emailFailed", { url: inviteUrl })); setInviteStatus(t("settings.inviteStatus.emailFailed", { url: inviteUrl }));
} else { } else {
setInviteStatus(t("settings.inviteStatus.sent")); setInviteStatus(t("settings.inviteStatus.sent"));
@@ -501,14 +583,15 @@ export default function SettingsPage() {
}), }),
}); });
const { data, text } = await readResponse(response); const { data, text } = await readResponse(response);
if (!response.ok || !data?.ok) { const api = unwrapApiResponse(data);
if (!response.ok || !api.ok) {
if (response.status === 409) { if (response.status === 409) {
throw new Error(t("settings.conflict")); throw new Error(t("settings.conflict"));
} }
const message = data?.error || data?.message || text || t("settings.failedSave"); const message = api.error || text || t("settings.failedSave");
throw new Error(message); throw new Error(message);
} }
const next = normalizeSettings(data.settings, defaultShiftName); const next = normalizeSettings(api.record?.settings, defaultShiftName);
setDraft(next); setDraft(next);
setSaveStatus("saved"); setSaveStatus("saved");
} catch (err) { } catch (err) {
@@ -537,7 +620,7 @@ export default function SettingsPage() {
if (loading && !draft) { if (loading && !draft) {
return ( return (
<div className="p-6"> <div className="p-4 sm:p-6">
<div className="rounded-2xl border border-white/10 bg-white/5 p-6 text-sm text-zinc-300"> <div className="rounded-2xl border border-white/10 bg-white/5 p-6 text-sm text-zinc-300">
{t("settings.loading")} {t("settings.loading")}
</div> </div>
@@ -547,7 +630,7 @@ export default function SettingsPage() {
if (!draft) { if (!draft) {
return ( return (
<div className="p-6"> <div className="p-4 sm:p-6">
<div className="rounded-2xl border border-red-500/30 bg-red-500/10 p-6 text-sm text-red-200"> <div className="rounded-2xl border border-red-500/30 bg-red-500/10 p-6 text-sm text-red-200">
{error || t("settings.unavailable")} {error || t("settings.unavailable")}
</div> </div>
@@ -556,23 +639,23 @@ export default function SettingsPage() {
} }
return ( return (
<div className="p-6"> <div className="p-4 sm:p-6">
<div className="mb-6 flex flex-wrap items-center justify-between gap-4"> <div className="mb-6 flex flex-col gap-4 sm:flex-row sm:items-center sm:justify-between">
<div> <div>
<h1 className="text-2xl font-semibold text-white">{t("settings.title")}</h1> <h1 className="text-2xl font-semibold text-white">{t("settings.title")}</h1>
<p className="text-sm text-zinc-400">{t("settings.subtitle")}</p> <p className="text-sm text-zinc-400">{t("settings.subtitle")}</p>
</div> </div>
<div className="flex items-center gap-2"> <div className="flex w-full flex-wrap items-center gap-2 sm:w-auto">
<button <button
onClick={loadSettings} onClick={loadSettings}
className="rounded-xl border border-white/10 bg-white/5 px-4 py-2 text-sm text-white hover:bg-white/10" 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("settings.refresh")} {t("settings.refresh")}
</button> </button>
<button <button
onClick={saveSettings} onClick={saveSettings}
disabled={saving} disabled={saving}
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:cursor-not-allowed disabled:opacity-60" className="w-full rounded-xl border border-emerald-400/40 bg-emerald-500/20 px-4 py-2 text-center text-sm text-emerald-100 hover:bg-emerald-500/30 disabled:cursor-not-allowed disabled:opacity-60 sm:w-auto"
> >
{t("settings.save")} {t("settings.save")}
</button> </button>
@@ -585,8 +668,26 @@ export default function SettingsPage() {
</div> </div>
)} )}
<div className="grid grid-cols-1 gap-4 xl:grid-cols-3"> <div className="mb-6 flex flex-wrap gap-2 rounded-2xl border border-white/10 bg-white/5 p-2">
<div className="rounded-2xl border border-white/10 bg-white/5 p-5 xl:col-span-1"> {SETTINGS_TABS.map((tab) => (
<button
key={tab.id}
type="button"
onClick={() => setActiveTab(tab.id)}
className={
tab.id === activeTab
? "rounded-xl bg-emerald-500/20 px-4 py-2 text-sm text-emerald-200"
: "rounded-xl border border-white/10 bg-white/5 px-4 py-2 text-sm text-zinc-200 hover:bg-white/10"
}
>
{t(tab.labelKey)}
</button>
))}
</div>
{activeTab === "general" && (
<div className="space-y-6">
<div className="rounded-2xl border border-white/10 bg-white/5 p-5">
<div className="text-sm font-semibold text-white">{t("settings.org.title")}</div> <div className="text-sm font-semibold text-white">{t("settings.org.title")}</div>
<div className="mt-4 space-y-3"> <div className="mt-4 space-y-3">
<div className="rounded-xl border border-white/10 bg-black/20 p-3"> <div className="rounded-xl border border-white/10 bg-black/20 p-3">
@@ -622,7 +723,53 @@ export default function SettingsPage() {
</div> </div>
</div> </div>
<div className="rounded-2xl border border-white/10 bg-white/5 p-5 xl:col-span-2"> <div className="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("settings.defaults")}</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("settings.defaults.moldTotal")}
<input
type="number"
min={0}
value={draft.defaults.moldTotal}
onChange={(event) => updateDefaults("moldTotal", Number(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("settings.defaults.moldActive")}
<input
type="number"
min={0}
value={draft.defaults.moldActive}
onChange={(event) => updateDefaults("moldActive", Number(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>
<div className="rounded-2xl border border-white/10 bg-white/5 p-5">
<div className="mb-3 text-sm font-semibold text-white">{t("settings.integrations")}</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("settings.integrations.webhook")}</div>
<div className="mt-1 text-sm text-white">https://hooks.example.com/iiot</div>
</div>
<div className="rounded-xl border border-white/10 bg-black/20 p-3">
<div className="text-xs text-zinc-400">{t("settings.integrations.erp")}</div>
<div className="mt-1 text-sm text-zinc-300">{t("settings.integrations.erpNotConfigured")}</div>
</div>
</div>
</div>
</div>
</div>
)}
{activeTab === "thresholds" && (
<div className="space-y-6">
<div className="rounded-2xl border border-white/10 bg-white/5 p-5">
<div className="mb-3 flex items-center justify-between gap-4"> <div className="mb-3 flex items-center justify-between gap-4">
<div className="text-sm font-semibold text-white">{t("settings.thresholds")}</div> <div className="text-sm font-semibold text-white">{t("settings.thresholds")}</div>
<div className="text-xs text-zinc-400">{t("settings.thresholds.appliesAll")}</div> <div className="text-xs text-zinc-400">{t("settings.thresholds.appliesAll")}</div>
@@ -699,9 +846,11 @@ export default function SettingsPage() {
</div> </div>
</div> </div>
</div> </div>
)}
<div className="mt-6 grid grid-cols-1 gap-4 xl:grid-cols-3"> {activeTab === "shifts" && (
<div className="rounded-2xl border border-white/10 bg-white/5 p-5 xl:col-span-2"> <div className="space-y-6">
<div className="rounded-2xl border border-white/10 bg-white/5 p-5">
<div className="mb-3 flex items-center justify-between gap-4"> <div className="mb-3 flex items-center justify-between gap-4">
<div className="text-sm font-semibold text-white">{t("settings.shiftSchedule")}</div> <div className="text-sm font-semibold text-white">{t("settings.shiftSchedule")}</div>
<div className="text-xs text-zinc-400">{t("settings.shiftHint")}</div> <div className="text-xs text-zinc-400">{t("settings.shiftHint")}</div>
@@ -790,7 +939,11 @@ export default function SettingsPage() {
</div> </div>
</div> </div>
</div> </div>
</div>
)}
{activeTab === "alerts" && (
<div className="space-y-6">
<div className="rounded-2xl border border-white/10 bg-white/5 p-5"> <div className="rounded-2xl border border-white/10 bg-white/5 p-5">
<div className="text-sm font-semibold text-white">{t("settings.alerts")}</div> <div className="text-sm font-semibold text-white">{t("settings.alerts")}</div>
<div className="mt-4 space-y-3"> <div className="mt-4 space-y-3">
@@ -820,51 +973,19 @@ export default function SettingsPage() {
/> />
</div> </div>
</div> </div>
</div>
<div className="mt-6 grid grid-cols-1 gap-4 xl:grid-cols-2"> <AlertsConfig />
<div className="rounded-2xl border border-white/10 bg-white/5 p-5">
<div className="mb-3 text-sm font-semibold text-white">{t("settings.defaults")}</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("settings.defaults.moldTotal")}
<input
type="number"
min={0}
value={draft.defaults.moldTotal}
onChange={(event) => updateDefaults("moldTotal", Number(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("settings.defaults.moldActive")}
<input
type="number"
min={0}
value={draft.defaults.moldActive}
onChange={(event) => updateDefaults("moldActive", Number(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> </div>
)}
<div className="rounded-2xl border border-white/10 bg-white/5 p-5"> {activeTab === "financial" && (
<div className="mb-3 text-sm font-semibold text-white">{t("settings.integrations")}</div> <div className="space-y-6">
<div className="space-y-3 text-sm text-zinc-300"> <FinancialCostConfig />
<div className="rounded-xl border border-white/10 bg-black/20 p-3">
<div className="text-xs text-zinc-400">{t("settings.integrations.webhook")}</div>
<div className="mt-1 text-sm text-white">https://hooks.example.com/iiot</div>
</div>
<div className="rounded-xl border border-white/10 bg-black/20 p-3">
<div className="text-xs text-zinc-400">{t("settings.integrations.erp")}</div>
<div className="mt-1 text-sm text-zinc-300">{t("settings.integrations.erpNotConfigured")}</div>
</div>
</div>
</div>
</div> </div>
)}
<div className="mt-6 grid grid-cols-1 gap-4 xl:grid-cols-2"> {activeTab === "team" && (
<div className="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="rounded-2xl border border-white/10 bg-white/5 p-5">
<div className="mb-3 flex items-center justify-between"> <div className="mb-3 flex items-center justify-between">
<div className="text-sm font-semibold text-white">{t("settings.team")}</div> <div className="text-sm font-semibold text-white">{t("settings.team")}</div>
@@ -996,6 +1117,7 @@ export default function SettingsPage() {
</div> </div>
</div> </div>
</div> </div>
)}
</div> </div>
); );
} }

View File

@@ -50,6 +50,7 @@ export async function PATCH(req: Request, { params }: { params: Promise<{ id: st
} }
const { userId: _userId, eventTypes, ...updateData } = parsed.data; const { userId: _userId, eventTypes, ...updateData } = parsed.data;
void _userId;
const normalizedEventTypes = const normalizedEventTypes =
eventTypes === null ? Prisma.DbNull : eventTypes ?? undefined; eventTypes === null ? Prisma.DbNull : eventTypes ?? undefined;
const data = normalizedEventTypes === undefined const data = normalizedEventTypes === undefined

View 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 });
}

View 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 });
}

View 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}\"`,
},
});
}

View 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, "&amp;")
.replace(/</g, "&lt;")
.replace(/>/g, "&gt;")
.replace(/\"/g, "&quot;")
.replace(/'/g, "&#39;");
}
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}\"`,
},
});
}

View 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 });
}

View File

@@ -3,27 +3,33 @@ import { prisma } from "@/lib/prisma";
import { getMachineAuth } from "@/lib/machineAuthCache"; import { getMachineAuth } from "@/lib/machineAuthCache";
import { z } from "zod"; import { z } from "zod";
function unwrapEnvelope(raw: any) { function asRecord(value: unknown): Record<string, unknown> | null {
if (!raw || typeof raw !== "object") return raw; if (!value || typeof value !== "object" || Array.isArray(value)) return null;
const payload = raw.payload; return value as Record<string, unknown>;
if (!payload || typeof payload !== "object") return raw; }
function unwrapEnvelope(raw: unknown) {
const record = asRecord(raw);
if (!record) return raw;
const payload = asRecord(record.payload);
if (!payload) return raw;
const hasMeta = const hasMeta =
raw.schemaVersion !== undefined || record.schemaVersion !== undefined ||
raw.machineId !== undefined || record.machineId !== undefined ||
raw.tsMs !== undefined || record.tsMs !== undefined ||
raw.tsDevice !== undefined || record.tsDevice !== undefined ||
raw.seq !== undefined || record.seq !== undefined ||
raw.type !== undefined; record.type !== undefined;
if (!hasMeta) return raw; if (!hasMeta) return raw;
return { return {
...payload, ...payload,
machineId: raw.machineId ?? payload.machineId, machineId: record.machineId ?? payload.machineId,
tsMs: raw.tsMs ?? payload.tsMs, tsMs: record.tsMs ?? payload.tsMs,
tsDevice: raw.tsDevice ?? payload.tsDevice, tsDevice: record.tsDevice ?? payload.tsDevice,
schemaVersion: raw.schemaVersion ?? payload.schemaVersion, schemaVersion: record.schemaVersion ?? payload.schemaVersion,
seq: raw.seq ?? payload.seq, seq: record.seq ?? payload.seq,
}; };
} }
@@ -61,10 +67,14 @@ export async function POST(req: Request) {
const apiKey = req.headers.get("x-api-key"); 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 = await req.json().catch(() => null); let body: unknown = await req.json().catch(() => null);
body = unwrapEnvelope(body); body = unwrapEnvelope(body);
const bodyRecord = asRecord(body) ?? {};
const machineId = body?.machineId ?? body?.machine_id ?? body?.machine?.id; const machineId =
bodyRecord.machineId ??
bodyRecord.machine_id ??
(asRecord(bodyRecord.machine)?.id ?? null);
if (!machineId || !machineIdSchema.safeParse(String(machineId)).success) { if (!machineId || !machineIdSchema.safeParse(String(machineId)).success) {
return NextResponse.json({ ok: false, error: "Invalid payload" }, { status: 400 }); return NextResponse.json({ ok: false, error: "Invalid payload" }, { status: 400 });
} }
@@ -72,8 +82,7 @@ export async function POST(req: Request) {
const machine = await getMachineAuth(String(machineId), apiKey); const machine = await getMachineAuth(String(machineId), apiKey);
if (!machine) return NextResponse.json({ ok: false, error: "Unauthorized" }, { status: 401 }); if (!machine) return NextResponse.json({ ok: false, error: "Unauthorized" }, { status: 401 });
const raw = body as any; const cyclesRaw = bodyRecord.cycles ?? bodyRecord.cycle;
const cyclesRaw = raw?.cycles ?? raw?.cycle;
if (!cyclesRaw) { if (!cyclesRaw) {
return NextResponse.json({ ok: false, error: "Invalid payload" }, { status: 400 }); return NextResponse.json({ ok: false, error: "Invalid payload" }, { status: 400 });
} }
@@ -85,8 +94,8 @@ export async function POST(req: Request) {
} }
const fallbackTsMs = const fallbackTsMs =
(typeof raw?.tsMs === "number" && raw.tsMs) || (typeof bodyRecord.tsMs === "number" && bodyRecord.tsMs) ||
(typeof raw?.tsDevice === "number" && raw.tsDevice) || (typeof bodyRecord.tsDevice === "number" && bodyRecord.tsDevice) ||
undefined; undefined;
const rows = parsedCycles.data.map((data) => { const rows = parsedCycles.data.map((data) => {

View File

@@ -3,13 +3,19 @@ import { prisma } from "@/lib/prisma";
import { getMachineAuth } from "@/lib/machineAuthCache"; import { getMachineAuth } from "@/lib/machineAuthCache";
import { z } from "zod"; import { z } from "zod";
import { evaluateAlertsForEvent } from "@/lib/alerts/engine"; import { evaluateAlertsForEvent } from "@/lib/alerts/engine";
import { toJsonValue } from "@/lib/prismaJson";
const normalizeType = (t: any) => const normalizeType = (t: unknown) =>
String(t ?? "") String(t ?? "")
.trim() .trim()
.toLowerCase() .toLowerCase()
.replace(/_/g, "-"); .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> = { const CANON_TYPE: Record<string, string> = {
// Node-RED // Node-RED
"production-stopped": "stop", "production-stopped": "stop",
@@ -56,29 +62,35 @@ export async function POST(req: Request) {
const apiKey = req.headers.get("x-api-key"); 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: any = await req.json().catch(() => null); let body: unknown = await req.json().catch(() => null);
// ✅ if Node-RED sent an array as the whole body, unwrap it // ✅ if Node-RED sent an array as the whole body, unwrap it
if (Array.isArray(body)) body = body[0]; if (Array.isArray(body)) body = body[0];
const bodyRecord = asRecord(body) ?? {};
const payloadRecord = asRecord(bodyRecord.payload) ?? {};
// ✅ accept multiple common keys // ✅ accept multiple common keys
const machineId = body?.machineId ?? body?.machine_id ?? body?.machine?.id; const machineId =
bodyRecord.machineId ??
bodyRecord.machine_id ??
(asRecord(bodyRecord.machine)?.id ?? null);
let rawEvent = let rawEvent =
body?.event ?? bodyRecord.event ??
body?.events ?? bodyRecord.events ??
body?.anomalies ?? bodyRecord.anomalies ??
body?.payload?.event ?? payloadRecord.event ??
body?.payload?.events ?? payloadRecord.events ??
body?.payload?.anomalies ?? payloadRecord.anomalies ??
body?.payload ?? payloadRecord ??
body?.data; // sometimes "data" bodyRecord.data; // sometimes "data"
if (rawEvent?.event && typeof rawEvent.event === "object") rawEvent = rawEvent.event; const rawEventRecord = asRecord(rawEvent);
if (Array.isArray(rawEvent?.events)) rawEvent = rawEvent.events; if (rawEventRecord?.event && typeof rawEventRecord.event === "object") rawEvent = rawEventRecord.event;
if (Array.isArray(rawEventRecord?.events)) rawEvent = rawEventRecord.events;
if (!machineId || !rawEvent) { if (!machineId || !rawEvent) {
return NextResponse.json( return NextResponse.json(
{ ok: false, error: "Invalid payload", got: { hasMachineId: !!machineId, keys: Object.keys(body ?? {}) } }, { ok: false, error: "Invalid payload", got: { hasMachineId: !!machineId, keys: Object.keys(bodyRecord) } },
{ status: 400 } { status: 400 }
); );
} }
@@ -108,55 +120,50 @@ export async function POST(req: Request) {
} }
const created: { id: string; ts: Date; eventType: string }[] = []; const created: { id: string; ts: Date; eventType: string }[] = [];
const skipped: any[] = []; const skipped: Array<Record<string, unknown>> = [];
for (const ev of events) { for (const ev of events) {
if (!ev || typeof ev !== "object") { const evRecord = asRecord(ev);
if (!evRecord) {
skipped.push({ reason: "invalid_event_object" }); skipped.push({ reason: "invalid_event_object" });
continue; 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 typ0 = normalizeType(rawType);
const typ = CANON_TYPE[typ0] ?? typ0; const typ = CANON_TYPE[typ0] ?? typ0;
// Determine timestamp // Determine timestamp
const tsMs = const tsMs =
(typeof (ev as any)?.timestamp === "number" && (ev as any).timestamp) || (typeof evRecord.timestamp === "number" && evRecord.timestamp) ||
(typeof (ev as any)?.data?.timestamp === "number" && (ev as any).data.timestamp) || (typeof evData.timestamp === "number" && evData.timestamp) ||
(typeof (ev as any)?.data?.event_timestamp === "number" && (ev as any).data.event_timestamp) || (typeof evData.event_timestamp === "number" && evData.event_timestamp) ||
null; null;
const ts = tsMs ? new Date(tsMs) : new Date(); const ts = tsMs ? new Date(tsMs) : new Date();
// Severity defaulting (do not skip on severity — store for audit) // 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"; if (!sev) sev = "warning";
// Stop classification -> microstop/macrostop // Stop classification -> microstop/macrostop
let finalType = typ; let finalType = typ;
if (typ === "stop") { if (typ === "stop") {
const stopSec = const stopSec =
(typeof (ev as any)?.data?.stoppage_duration_seconds === "number" && (ev as any).data.stoppage_duration_seconds) || (typeof evData.stoppage_duration_seconds === "number" && evData.stoppage_duration_seconds) ||
(typeof (ev as any)?.data?.stop_duration_seconds === "number" && (ev as any).data.stop_duration_seconds) || (typeof evData.stop_duration_seconds === "number" && evData.stop_duration_seconds) ||
null; null;
if (stopSec != null) { if (stopSec != null) {
const theoretical = const theoretical = Number(evData.theoretical_cycle_time ?? evData.theoreticalCycleTime ?? 0) || 0;
Number(
(ev as any)?.data?.theoretical_cycle_time ??
(ev as any)?.data?.theoreticalCycleTime ??
0
) || 0;
const microMultiplier = Number( const microMultiplier = Number(
(ev as any)?.data?.micro_threshold_multiplier ?? evData.micro_threshold_multiplier ?? evData.threshold_multiplier ?? defaultMicroMultiplier
(ev as any)?.data?.threshold_multiplier ??
defaultMicroMultiplier
); );
const macroMultiplier = Math.max( const macroMultiplier = Math.max(
microMultiplier, microMultiplier,
Number((ev as any)?.data?.macro_threshold_multiplier ?? defaultMacroMultiplier) Number(evData.macro_threshold_multiplier ?? defaultMacroMultiplier)
); );
if (theoretical > 0) { if (theoretical > 0) {
@@ -177,39 +184,60 @@ export async function POST(req: Request) {
} }
const title = const title =
clampText((ev as any).title, 160) || clampText(evRecord.title, 160) ||
(finalType === "slow-cycle" ? "Slow Cycle Detected" : (finalType === "slow-cycle" ? "Slow Cycle Detected" :
finalType === "macrostop" ? "Macrostop Detected" : finalType === "macrostop" ? "Macrostop Detected" :
finalType === "microstop" ? "Microstop Detected" : finalType === "microstop" ? "Microstop Detected" :
"Event"); "Event");
const description = clampText((ev as any).description, 1000); const description = clampText(evRecord.description, 1000);
// store full blob, ensure object // store full blob, ensure object
const rawData = (ev as any).data ?? ev; const rawData = evRecord.data ?? evRecord;
const dataObj = typeof rawData === "string" ? (() => { const parsedData = typeof rawData === "string"
try { return JSON.parse(rawData); } catch { return { raw: rawData }; } ? (() => {
})() : rawData; 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({ const row = await prisma.machineEvent.create({
data: { data: {
orgId: machine.orgId, orgId: machine.orgId,
machineId: machine.id, machineId: machine.id,
ts, ts,
topic: clampText((ev as any).topic ?? finalType, 64) ?? finalType, topic: clampText(evRecord.topic ?? finalType, 64) ?? finalType,
eventType: finalType, eventType: finalType,
severity: sev, severity: sev,
requiresAck: !!(ev as any).requires_ack, requiresAck: !!evRecord.requires_ack,
title, title,
description, description,
data: dataObj, data: toJsonValue(dataObj),
workOrderId: workOrderId:
clampText((ev as any)?.work_order_id, 64) ?? clampText(evRecord.work_order_id, 64) ??
clampText((ev as any)?.data?.work_order_id, 64) ?? clampText(evData.work_order_id, 64) ??
clampText(activeWorkOrder?.id, 64) ??
clampText(dataActiveWorkOrder?.id, 64) ??
null, null,
sku: sku:
clampText((ev as any)?.sku, 64) ?? clampText(evRecord.sku, 64) ??
clampText((ev as any)?.data?.sku, 64) ?? clampText(evData.sku, 64) ??
clampText(activeWorkOrder?.sku, 64) ??
clampText(dataActiveWorkOrder?.sku, 64) ??
null, null,
}, },
}); });

View File

@@ -2,6 +2,7 @@ import { NextResponse } from "next/server";
import { prisma } from "@/lib/prisma"; import { prisma } from "@/lib/prisma";
import { getMachineAuth } from "@/lib/machineAuthCache"; import { getMachineAuth } from "@/lib/machineAuthCache";
import { normalizeHeartbeatV1 } from "@/lib/contracts/v1"; import { normalizeHeartbeatV1 } from "@/lib/contracts/v1";
import { toJsonValue } from "@/lib/prismaJson";
function getClientIp(req: Request) { function getClientIp(req: Request) {
const xf = req.headers.get("x-forwarded-for"); const xf = req.headers.get("x-forwarded-for");
@@ -24,7 +25,7 @@ export async function POST(req: Request) {
const ip = getClientIp(req); const ip = getClientIp(req);
const userAgent = req.headers.get("user-agent"); const userAgent = req.headers.get("user-agent");
let rawBody: any = null; let rawBody: unknown = null;
let orgId: string | null = null; let orgId: string | null = null;
let machineId: string | null = null; let machineId: string | null = null;
let seq: bigint | null = null; let seq: bigint | null = null;
@@ -48,7 +49,16 @@ export async function POST(req: Request) {
const normalized = normalizeHeartbeatV1(rawBody); const normalized = normalizeHeartbeatV1(rawBody);
if (!normalized.ok) { if (!normalized.ok) {
await prisma.ingestLog.create({ await prisma.ingestLog.create({
data: { endpoint, ok: false, status: 400, errorCode: "INVALID_PAYLOAD", errorMsg: normalized.error, body: rawBody, ip, userAgent }, 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 }); return NextResponse.json({ ok: false, error: "Invalid payload", detail: normalized.error }, { status: 400 });
} }
@@ -70,7 +80,7 @@ export async function POST(req: Request) {
status: 401, status: 401,
errorCode: "UNAUTHORIZED", errorCode: "UNAUTHORIZED",
errorMsg: "Unauthorized (machineId/apiKey mismatch)", errorMsg: "Unauthorized (machineId/apiKey mismatch)",
body: rawBody, body: toJsonValue(rawBody),
machineId, machineId,
schemaVersion, schemaVersion,
seq, seq,
@@ -123,8 +133,8 @@ export async function POST(req: Request) {
tsDevice: hb.ts, tsDevice: hb.ts,
tsServer: hb.tsServer, tsServer: hb.tsServer,
}); });
} catch (err: any) { } catch (err: unknown) {
const msg = err?.message ? String(err.message) : "Unknown error"; const msg = err instanceof Error ? err.message : "Unknown error";
try { try {
await prisma.ingestLog.create({ await prisma.ingestLog.create({
@@ -139,7 +149,7 @@ export async function POST(req: Request) {
schemaVersion, schemaVersion,
seq, seq,
tsDevice: tsDeviceDate ?? undefined, tsDevice: tsDeviceDate ?? undefined,
body: rawBody, body: toJsonValue(rawBody),
ip, ip,
userAgent, userAgent,
}, },

View File

@@ -3,6 +3,7 @@ import { NextResponse } from "next/server";
import { prisma } from "@/lib/prisma"; import { prisma } from "@/lib/prisma";
import { getMachineAuth } from "@/lib/machineAuthCache"; import { getMachineAuth } from "@/lib/machineAuthCache";
import { normalizeSnapshotV1 } from "@/lib/contracts/v1"; import { normalizeSnapshotV1 } from "@/lib/contracts/v1";
import { toJsonValue } from "@/lib/prismaJson";
function getClientIp(req: Request) { function getClientIp(req: Request) {
const xf = req.headers.get("x-forwarded-for"); const xf = req.headers.get("x-forwarded-for");
@@ -26,7 +27,7 @@ export async function POST(req: Request) {
const ip = getClientIp(req); const ip = getClientIp(req);
const userAgent = req.headers.get("user-agent"); const userAgent = req.headers.get("user-agent");
let rawBody: any = null; let rawBody: unknown = null;
let orgId: string | null = null; let orgId: string | null = null;
let machineId: string | null = null; let machineId: string | null = null;
let seq: bigint | null = null; let seq: bigint | null = null;
@@ -60,7 +61,7 @@ export async function POST(req: Request) {
status: 400, status: 400,
errorCode: "INVALID_PAYLOAD", errorCode: "INVALID_PAYLOAD",
errorMsg: normalized.error, errorMsg: normalized.error,
body: rawBody, body: toJsonValue(rawBody),
ip, ip,
userAgent, userAgent,
}, },
@@ -86,7 +87,7 @@ export async function POST(req: Request) {
status: 401, status: 401,
errorCode: "UNAUTHORIZED", errorCode: "UNAUTHORIZED",
errorMsg: "Unauthorized (machineId/apiKey mismatch)", errorMsg: "Unauthorized (machineId/apiKey mismatch)",
body: rawBody, body: toJsonValue(rawBody),
machineId, machineId,
schemaVersion, schemaVersion,
seq, seq,
@@ -100,19 +101,37 @@ export async function POST(req: Request) {
orgId = machine.orgId; orgId = machine.orgId;
const wo = body.activeWorkOrder ?? {}; const woRecord = (body.activeWorkOrder ?? {}) as Record<string, unknown>;
const good = typeof wo.good === "number" ? wo.good : (typeof wo.goodParts === "number" ? wo.goodParts : null); const good =
const scrap = typeof wo.scrap === "number" ? wo.scrap : (typeof wo.scrapParts === "number" ? wo.scrapParts : null) 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 k = body.kpis ?? {};
const safeCycleTime = const safeCycleTime =
typeof body.cycleTime === "number" && body.cycleTime > 0 typeof body.cycleTime === "number" && body.cycleTime > 0
? body.cycleTime ? body.cycleTime
: (typeof (wo as any).cycleTime === "number" && (wo as any).cycleTime > 0 ? (wo as any).cycleTime : null); : typeof woRecord.cycleTime === "number" && woRecord.cycleTime > 0
? woRecord.cycleTime
: null;
const safeCavities = const safeCavities =
typeof body.cavities === "number" && body.cavities > 0 typeof body.cavities === "number" && body.cavities > 0
? body.cavities ? body.cavities
: (typeof (wo as any).cavities === "number" && (wo as any).cavities > 0 ? (wo as any).cavities : null); : typeof woRecord.cavities === "number" && woRecord.cavities > 0
? woRecord.cavities
: null;
// Write snapshot (ts = tsDevice; tsServer auto) // Write snapshot (ts = tsDevice; tsServer auto)
const row = await prisma.machineKpiSnapshot.create({ const row = await prisma.machineKpiSnapshot.create({
data: { data: {
@@ -125,9 +144,9 @@ export async function POST(req: Request) {
ts: tsDeviceDate, // store device-time in ts; server-time goes to ts_server ts: tsDeviceDate, // store device-time in ts; server-time goes to ts_server
// Work order fields // Work order fields
workOrderId: wo.id ? String(wo.id) : null, workOrderId: woRecord.id != null ? String(woRecord.id) : null,
sku: wo.sku ? String(wo.sku) : null, sku: woRecord.sku != null ? String(woRecord.sku) : null,
target: typeof wo.target === "number" ? Math.trunc(wo.target) : null, target: typeof woRecord.target === "number" ? Math.trunc(woRecord.target) : null,
good: good != null ? Math.trunc(good) : null, good: good != null ? Math.trunc(good) : null,
scrap: scrap != null ? Math.trunc(scrap) : null, scrap: scrap != null ? Math.trunc(scrap) : null,
@@ -169,8 +188,8 @@ export async function POST(req: Request) {
tsDevice: row.ts, tsDevice: row.ts,
tsServer: row.tsServer, tsServer: row.tsServer,
}); });
} catch (err: any) { } catch (err: unknown) {
const msg = err?.message ? String(err.message) : "Unknown error"; const msg = err instanceof Error ? err.message : "Unknown error";
// Never fail the request because logging failed // Never fail the request because logging failed
try { try {
@@ -186,7 +205,7 @@ export async function POST(req: Request) {
schemaVersion, schemaVersion,
seq, seq,
tsDevice: tsDeviceDate ?? undefined, tsDevice: tsDeviceDate ?? undefined,
body: rawBody, body: toJsonValue(rawBody),
ip, ip,
userAgent, userAgent,
}, },

View File

@@ -1,177 +1,9 @@
import { NextResponse } from "next/server"; import { NextResponse } from "next/server";
import type { NextRequest } from "next/server"; import type { NextRequest } from "next/server";
import { createHash } from "crypto";
import { prisma } from "@/lib/prisma"; import { prisma } from "@/lib/prisma";
import { requireSession } from "@/lib/auth/requireSession"; import { requireSession } from "@/lib/auth/requireSession";
import { normalizeEvent } from "@/lib/events/normalizeEvent";
function normalizeEvent(
row: any,
thresholds: { microMultiplier: number; macroMultiplier: number }
) {
// -----------------------------
// 1) Parse row.data safely
// data may be:
// - object
// - array of objects
// - JSON string of either
// -----------------------------
const raw = row.data;
let parsed: any = 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?.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;
const microMultiplier = Number(thresholds?.microMultiplier ?? 1.5);
const macroMultiplier = Math.max(
microMultiplier,
Number(thresholds?.macroMultiplier ?? 5)
);
const theoreticalCycle =
Number(inner?.theoretical_cycle_time ?? blob?.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?.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,
};
}
export async function GET( export async function GET(
@@ -188,9 +20,89 @@ export async function GET(
const eventsOnly = url.searchParams.get("eventsOnly") === "1"; const eventsOnly = url.searchParams.get("eventsOnly") === "1";
const eventsWindowSec = Number(url.searchParams.get("eventsWindowSec") ?? "21600"); // default 6h const eventsWindowSec = Number(url.searchParams.get("eventsWindowSec") ?? "21600"); // default 6h
const eventsWindowStart = new Date(Date.now() - Math.max(0, eventsWindowSec) * 1000); const eventsWindowStart = new Date(Date.now() - Math.max(0, eventsWindowSec) * 1000);
const windowSec = Number(url.searchParams.get("windowSec") ?? "3600"); // default 1h
const { machineId } = await params; const { machineId } = await params;
const machineBase = await prisma.machine.findFirst({
where: { id: machineId, orgId: session.orgId },
select: { id: true, updatedAt: true },
});
if (!machineBase) {
return NextResponse.json({ ok: false, error: "Not found" }, { status: 404 });
}
const [heartbeatAgg, kpiAgg, eventAgg, cycleAgg, orgSettingsAgg] = await Promise.all([
prisma.machineHeartbeat.aggregate({
where: { orgId: session.orgId, machineId },
_max: { tsServer: true },
}),
prisma.machineKpiSnapshot.aggregate({
where: { orgId: session.orgId, machineId },
_max: { tsServer: true },
}),
prisma.machineEvent.aggregate({
where: { orgId: session.orgId, machineId, ts: { gte: eventsWindowStart } },
_max: { tsServer: true },
}),
prisma.machineCycle.aggregate({
where: { orgId: session.orgId, machineId },
_max: { ts: true },
}),
prisma.orgSettings.findUnique({
where: { orgId: session.orgId },
select: { updatedAt: true, stoppageMultiplier: true, macroStoppageMultiplier: true },
}),
]);
const toMs = (value?: Date | null) => (value ? value.getTime() : 0);
const lastModifiedMs = Math.max(
toMs(machineBase.updatedAt),
toMs(heartbeatAgg._max.tsServer),
toMs(kpiAgg._max.tsServer),
toMs(eventAgg._max.tsServer),
toMs(cycleAgg._max.ts),
toMs(orgSettingsAgg?.updatedAt)
);
const versionParts = [
session.orgId,
machineId,
eventsMode,
eventsOnly ? "1" : "0",
eventsWindowSec,
windowSec,
toMs(machineBase.updatedAt),
toMs(heartbeatAgg._max.tsServer),
toMs(kpiAgg._max.tsServer),
toMs(eventAgg._max.tsServer),
toMs(cycleAgg._max.ts),
toMs(orgSettingsAgg?.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 machine = await prisma.machine.findFirst({ const machine = await prisma.machine.findFirst({
where: { id: machineId, orgId: session.orgId }, where: { id: machineId, orgId: session.orgId },
select: { select: {
@@ -227,15 +139,10 @@ export async function GET(
return NextResponse.json({ ok: false, error: "Not found" }, { status: 404 }); return NextResponse.json({ ok: false, error: "Not found" }, { status: 404 });
} }
const orgSettings = await prisma.orgSettings.findUnique({ const microMultiplier = Number(orgSettingsAgg?.stoppageMultiplier ?? 1.5);
where: { orgId: session.orgId },
select: { stoppageMultiplier: true, macroStoppageMultiplier: true },
});
const microMultiplier = Number(orgSettings?.stoppageMultiplier ?? 1.5);
const macroMultiplier = Math.max( const macroMultiplier = Math.max(
microMultiplier, microMultiplier,
Number(orgSettings?.macroStoppageMultiplier ?? 5) Number(orgSettingsAgg?.macroStoppageMultiplier ?? 5)
); );
const rawEvents = await prisma.machineEvent.findMany({ const rawEvents = await prisma.machineEvent.findMany({
@@ -296,12 +203,14 @@ export async function GET(
const eventsCountCritical = allEvents.filter(isCritical).length; const eventsCountCritical = allEvents.filter(isCritical).length;
if (eventsOnly) { if (eventsOnly) {
return NextResponse.json({ ok: true, events, eventsCountAll, eventsCountCritical }); return NextResponse.json(
{ ok: true, events, eventsCountAll, eventsCountCritical },
{ headers: responseHeaders }
);
} }
// ---- cycles window ---- // ---- cycles window ----
const windowSec = Number(url.searchParams.get("windowSec") ?? "3600"); // default 1h
const latestKpi = machine.kpiSnapshots[0] ?? null; const latestKpi = machine.kpiSnapshots[0] ?? null;
@@ -380,11 +289,8 @@ const cycles = rawCycles
workOrderId: c.workOrderId ?? null, workOrderId: c.workOrderId ?? null,
sku: c.sku ?? null, sku: c.sku ?? null,
})); }));
return NextResponse.json(
{
return NextResponse.json({
ok: true, ok: true,
machine: { machine: {
id: machine.id, id: machine.id,
@@ -404,6 +310,7 @@ const cycles = rawCycles
eventsCountAll, eventsCountAll,
eventsCountCritical, eventsCountCritical,
cycles, cycles,
}); },
{ headers: responseHeaders }
);
} }

View File

@@ -139,10 +139,9 @@ export async function POST(req: Request) {
}, },
}); });
break; break;
} catch (err: any) { } catch (err: unknown) {
if (err?.code !== "P2002") { const code = typeof err === "object" && err !== null ? (err as { code?: string }).code : undefined;
throw err; if (code !== "P2002") throw err;
}
} }
} }

View File

@@ -154,8 +154,9 @@ export async function POST(req: Request) {
}, },
}); });
break; break;
} catch (err: any) { } catch (err: unknown) {
if (err?.code !== "P2002") throw err; const code = typeof err === "object" && err !== null ? (err as { code?: string }).code : undefined;
if (code !== "P2002") throw err;
} }
} }
@@ -184,9 +185,9 @@ export async function POST(req: Request) {
text: content.text, text: content.text,
html: content.html, html: content.html,
}); });
} catch (err: any) { } catch (err: unknown) {
emailSent = false; emailSent = false;
emailError = err?.message || "Failed to send invite email"; emailError = err instanceof Error ? err.message : "Failed to send invite email";
} }
return NextResponse.json({ ok: true, invite, emailSent, emailError }); return NextResponse.json({ ok: true, invite, emailSent, emailError });

101
app/api/overview/route.ts Normal file
View 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 }
);
}

View File

@@ -165,7 +165,7 @@ export async function GET(req: NextRequest) {
for (const e of events) { for (const e of events) {
const type = String(e.eventType ?? "").toLowerCase(); const type = String(e.eventType ?? "").toLowerCase();
let blob: any = e.data; let blob: unknown = e.data;
if (typeof blob === "string") { if (typeof blob === "string") {
try { try {
@@ -175,7 +175,12 @@ export async function GET(req: NextRequest) {
} }
} }
const inner = blob?.data ?? blob ?? {}; 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 = const stopSec =
(typeof inner?.stoppage_duration_seconds === "number" && inner.stoppage_duration_seconds) || (typeof inner?.stoppage_duration_seconds === "number" && inner.stoppage_duration_seconds) ||
(typeof inner?.stop_duration_seconds === "number" && inner.stop_duration_seconds) || (typeof inner?.stop_duration_seconds === "number" && inner.stop_duration_seconds) ||

View File

@@ -3,6 +3,7 @@ import type { NextRequest } from "next/server";
import { Prisma } from "@prisma/client"; import { Prisma } from "@prisma/client";
import { prisma } from "@/lib/prisma"; import { prisma } from "@/lib/prisma";
import { requireSession } from "@/lib/auth/requireSession"; import { requireSession } from "@/lib/auth/requireSession";
import { toJsonValue } from "@/lib/prismaJson";
import { import {
DEFAULT_ALERTS, DEFAULT_ALERTS,
DEFAULT_DEFAULTS, DEFAULT_DEFAULTS,
@@ -18,7 +19,7 @@ import {
import { publishSettingsUpdate } from "@/lib/mqtt"; import { publishSettingsUpdate } from "@/lib/mqtt";
import { z } from "zod"; import { z } from "zod";
function isPlainObject(value: any): value is Record<string, any> { function isPlainObject(value: unknown): value is Record<string, unknown> {
return !!value && typeof value === "object" && !Array.isArray(value); return !!value && typeof value === "object" && !Array.isArray(value);
} }
@@ -34,9 +35,9 @@ const machineSettingsSchema = z
}) })
.passthrough(); .passthrough();
function pickAllowedOverrides(raw: any) { function pickAllowedOverrides(raw: unknown) {
if (!isPlainObject(raw)) return {}; if (!isPlainObject(raw)) return {};
const out: Record<string, any> = {}; const out: Record<string, unknown> = {};
for (const key of ["shiftSchedule", "thresholds", "alerts", "defaults"]) { for (const key of ["shiftSchedule", "thresholds", "alerts", "defaults"]) {
if (raw[key] !== undefined) out[key] = raw[key]; if (raw[key] !== undefined) out[key] = raw[key];
} }
@@ -337,24 +338,26 @@ export async function PUT(
select: { overridesJson: true }, select: { overridesJson: true },
}); });
let nextOverrides: any = null; let nextOverrides: Record<string, unknown> | null = null;
if (patch === null) { if (patch === null) {
nextOverrides = null; nextOverrides = null;
} else { } else {
const merged = applyOverridePatch(existing?.overridesJson ?? {}, patch); const merged = applyOverridePatch(existing?.overridesJson ?? {}, patch);
nextOverrides = Object.keys(merged).length ? merged : null; nextOverrides = Object.keys(merged).length ? merged : null;
} }
const nextOverridesJson =
nextOverrides === null ? Prisma.DbNull : toJsonValue(nextOverrides);
const saved = await tx.machineSettings.upsert({ const saved = await tx.machineSettings.upsert({
where: { machineId }, where: { machineId },
update: { update: {
overridesJson: nextOverrides, overridesJson: nextOverridesJson,
updatedBy: session.userId, updatedBy: session.userId,
}, },
create: { create: {
machineId, machineId,
orgId: session.orgId, orgId: session.orgId,
overridesJson: nextOverrides, overridesJson: nextOverridesJson,
updatedBy: session.userId, updatedBy: session.userId,
}, },
}); });

View File

@@ -1,5 +1,4 @@
import { NextResponse } from "next/server"; import { NextResponse } from "next/server";
import type { NextRequest } from "next/server";
import { Prisma } from "@prisma/client"; import { Prisma } from "@prisma/client";
import { prisma } from "@/lib/prisma"; import { prisma } from "@/lib/prisma";
import { requireSession } from "@/lib/auth/requireSession"; import { requireSession } from "@/lib/auth/requireSession";
@@ -19,7 +18,15 @@ import {
import { publishSettingsUpdate } from "@/lib/mqtt"; import { publishSettingsUpdate } from "@/lib/mqtt";
import { z } from "zod"; import { z } from "zod";
function isPlainObject(value: any): value is Record<string, any> { 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); return !!value && typeof value === "object" && !Array.isArray(value);
} }
@@ -191,7 +198,7 @@ export async function PUT(req: Request) {
return NextResponse.json({ ok: false, error: defaultsValidation.error }, { status: 400 }); return NextResponse.json({ ok: false, error: defaultsValidation.error }, { status: 400 });
} }
let shiftRows: any[] | null = null; let shiftRows: ValidShift[] | null = null;
if (shiftSchedule?.shifts !== undefined) { if (shiftSchedule?.shifts !== undefined) {
const shiftResult = validateShiftSchedule(shiftSchedule.shifts); const shiftResult = validateShiftSchedule(shiftSchedule.shifts);
if (!shiftResult.ok) { if (!shiftResult.ok) {
@@ -291,15 +298,15 @@ export async function PUT(req: Request) {
return { settings: refreshed, shifts: refreshedShifts }; return { settings: refreshed, shifts: refreshedShifts };
}); });
if ((updated as any)?.error === "VERSION_MISMATCH") { if ("error" in updated && updated.error === "VERSION_MISMATCH") {
return NextResponse.json( return NextResponse.json(
{ ok: false, error: "Version mismatch", currentVersion: (updated as any).currentVersion }, { ok: false, error: "Version mismatch", currentVersion: updated.currentVersion },
{ status: 409 } { status: 409 }
); );
} }
if ((updated as any)?.error) { if ("error" in updated) {
return NextResponse.json({ ok: false, error: (updated as any).error }, { status: 400 }); return NextResponse.json({ ok: false, error: updated.error }, { status: 400 });
} }
const payload = buildSettingsPayload(updated.settings, updated.shifts ?? []); const payload = buildSettingsPayload(updated.settings, updated.shifts ?? []);

View File

@@ -51,7 +51,7 @@ export async function POST(req: Request) {
const verificationToken = randomBytes(24).toString("hex"); const verificationToken = randomBytes(24).toString("hex");
const verificationExpiresAt = new Date(Date.now() + 24 * 60 * 60 * 1000); const verificationExpiresAt = new Date(Date.now() + 24 * 60 * 60 * 1000);
const result = await prisma.$transaction(async (tx) => { await prisma.$transaction(async (tx) => {
const org = await tx.org.create({ const org = await tx.org.create({
data: { name: orgName, slug }, data: { name: orgName, slug },
}); });
@@ -118,14 +118,15 @@ export async function POST(req: Request) {
text: emailContent.text, text: emailContent.text,
html: emailContent.html, html: emailContent.html,
}); });
} catch (err: any) { } catch (err: unknown) {
emailSent = false; emailSent = false;
const error = err as { message?: string; code?: string; response?: unknown; responseCode?: number };
logLine("signup.verify_email.failed", { logLine("signup.verify_email.failed", {
email, email,
message: err?.message, message: error?.message,
code: err?.code, code: error?.code,
response: err?.response, response: error?.response,
responseCode: err?.responseCode, responseCode: error?.responseCode,
}); });
} }
return NextResponse.json({ return NextResponse.json({

View File

@@ -53,19 +53,25 @@ type WorkOrderInput = {
cycleTime?: number | null; cycleTime?: number | null;
}; };
function normalizeWorkOrders(raw: any[]) { function normalizeWorkOrders(raw: unknown[]) {
const seen = new Set<string>(); const seen = new Set<string>();
const cleaned: WorkOrderInput[] = []; const cleaned: WorkOrderInput[] = [];
for (const item of raw) { for (const item of raw) {
const idRaw = cleanText(item?.workOrderId ?? item?.id ?? item?.work_order_id, MAX_WORK_ORDER_ID_LENGTH); 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; if (!idRaw || !WORK_ORDER_ID_RE.test(idRaw) || seen.has(idRaw)) continue;
seen.add(idRaw); seen.add(idRaw);
const sku = cleanText(item?.sku ?? item?.SKU ?? null, MAX_SKU_LENGTH); const sku = cleanText(record.sku ?? record.SKU ?? null, MAX_SKU_LENGTH);
const targetQtyRaw = toIntOrNull(item?.targetQty ?? item?.target_qty ?? item?.target ?? item?.targetQuantity); const targetQtyRaw = toIntOrNull(
record.targetQty ?? record.target_qty ?? record.target ?? record.targetQuantity
);
const cycleTimeRaw = toFloatOrNull( const cycleTimeRaw = toFloatOrNull(
item?.cycleTime ?? item?.theoreticalCycleTime ?? item?.theoretical_cycle_time ?? item?.cycle_time record.cycleTime ?? record.theoreticalCycleTime ?? record.theoretical_cycle_time ?? record.cycle_time
); );
const targetQty = const targetQty =
targetQtyRaw == null ? null : Math.min(Math.max(targetQtyRaw, 0), MAX_TARGET_QTY); targetQtyRaw == null ? null : Math.min(Math.max(targetQtyRaw, 0), MAX_TARGET_QTY);

View File

@@ -2,6 +2,8 @@
:root { :root {
color-scheme: dark; 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-bg: #0b0f14;
--app-surface: rgba(255, 255, 255, 0.05); --app-surface: rgba(255, 255, 255, 0.05);
--app-surface-2: rgba(255, 255, 255, 0.08); --app-surface-2: rgba(255, 255, 255, 0.08);

View File

@@ -51,8 +51,9 @@ export default function InviteAcceptForm({
throw new Error(data.error || t("invite.error.notFound")); throw new Error(data.error || t("invite.error.notFound"));
} }
if (alive) setInvite(data.invite); if (alive) setInvite(data.invite);
} catch (err: any) { } catch (err: unknown) {
if (alive) setError(err?.message || t("invite.error.notFound")); const message = err instanceof Error ? err.message : null;
if (alive) setError(message || t("invite.error.notFound"));
} finally { } finally {
if (alive) setLoading(false); if (alive) setLoading(false);
} }
@@ -62,7 +63,7 @@ export default function InviteAcceptForm({
return () => { return () => {
alive = false; alive = false;
}; };
}, [cleanedToken, initialInvite, initialError]); }, [cleanedToken, initialInvite, initialError, t]);
async function onSubmit(e: React.FormEvent) { async function onSubmit(e: React.FormEvent) {
e.preventDefault(); e.preventDefault();
@@ -80,8 +81,9 @@ export default function InviteAcceptForm({
} }
router.push("/machines"); router.push("/machines");
router.refresh(); router.refresh();
} catch (err: any) { } catch (err: unknown) {
setError(err?.message || t("invite.error.acceptFailed")); const message = err instanceof Error ? err.message : null;
setError(message || t("invite.error.acceptFailed"));
} finally { } finally {
setSubmitting(false); setSubmitting(false);
} }

View File

@@ -1,11 +1,7 @@
import type { Metadata } from "next"; import type { Metadata } from "next";
import { Geist, Geist_Mono } from "next/font/google";
import { cookies } from "next/headers"; import { cookies } from "next/headers";
import "./globals.css"; 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 = { export const metadata: Metadata = {
title: "MIS Control Tower", title: "MIS Control Tower",
description: "MaliounTech Industrial Suite", description: "MaliounTech Industrial Suite",
@@ -20,7 +16,7 @@ export default async function RootLayout({ children }: { children: React.ReactNo
return ( return (
<html lang={locale} data-theme={theme}> <html lang={locale} data-theme={theme}>
<body className={`${geistSans.variable} ${geistMono.variable} antialiased`}> <body className="antialiased">
{children} {children}
</body> </body>
</html> </html>

View File

@@ -35,8 +35,9 @@ export default function LoginForm() {
router.push(next); router.push(next);
router.refresh(); router.refresh();
} catch (e: any) { } catch (e: unknown) {
setErr(e?.message || t("login.error.network")); const message = e instanceof Error ? e.message : null;
setErr(message || t("login.error.network"));
} finally { } finally {
setLoading(false); setLoading(false);
} }

View File

@@ -34,8 +34,9 @@ export default function SignupForm() {
setVerificationSent(true); setVerificationSent(true);
setEmailSent(data.emailSent !== false); setEmailSent(data.emailSent !== false);
} catch (e: any) { } catch (e: unknown) {
setErr(e?.message || t("signup.error.network")); const message = e instanceof Error ? e.message : null;
setErr(message || t("signup.error.network"));
} finally { } finally {
setLoading(false); setLoading(false);
} }

View File

@@ -1,23 +1,32 @@
"use client"; "use client";
import { usePathname, useRouter } from "next/navigation"; 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 }) { export function RequireAuth({ children }: { children: React.ReactNode }) {
const router = useRouter(); const router = useRouter();
const pathname = usePathname(); const pathname = usePathname();
const [ready, setReady] = useState(false); const token = useSyncExternalStore(subscribe, getSnapshot, () => null);
const hasToken = Boolean(token);
useEffect(() => { useEffect(() => {
const token = localStorage.getItem("ct_token"); if (!hasToken) {
if (!token) {
router.replace("/login"); router.replace("/login");
return;
} }
setReady(true); }, [router, pathname, hasToken]);
}, [router, pathname]);
if (!ready) { if (!hasToken) {
return ( return (
<div className="min-h-screen bg-black text-zinc-200 flex items-center justify-center"> <div className="min-h-screen bg-black text-zinc-200 flex items-center justify-center">
Loading Loading

View File

@@ -2,20 +2,20 @@
import { useEffect, useState } from "react"; import { useEffect, useState } from "react";
import { Menu } from "lucide-react"; import { Menu } from "lucide-react";
import { usePathname } from "next/navigation";
import { Sidebar } from "@/components/layout/Sidebar"; import { Sidebar } from "@/components/layout/Sidebar";
import { UtilityControls } from "@/components/layout/UtilityControls"; import { UtilityControls } from "@/components/layout/UtilityControls";
import { useI18n } from "@/lib/i18n/useI18n"; import { useI18n } from "@/lib/i18n/useI18n";
export function AppShell({ children }: { children: React.ReactNode }) { export function AppShell({
children,
initialTheme,
}: {
children: React.ReactNode;
initialTheme?: "dark" | "light";
}) {
const { t } = useI18n(); const { t } = useI18n();
const pathname = usePathname();
const [drawerOpen, setDrawerOpen] = useState(false); const [drawerOpen, setDrawerOpen] = useState(false);
useEffect(() => {
setDrawerOpen(false);
}, [pathname]);
useEffect(() => { useEffect(() => {
if (!drawerOpen) return; if (!drawerOpen) return;
const onKey = (event: KeyboardEvent) => { const onKey = (event: KeyboardEvent) => {
@@ -34,7 +34,7 @@ export function AppShell({ children }: { children: React.ReactNode }) {
<div className="flex min-h-screen"> <div className="flex min-h-screen">
<Sidebar /> <Sidebar />
<div className="flex min-h-screen flex-1 flex-col"> <div className="flex min-h-screen flex-1 flex-col">
<header className="sticky top-0 z-30 flex h-14 items-center justify-between border-b border-white/10 bg-black/20 px-4 backdrop-blur"> <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"> <div className="flex items-center gap-3">
<button <button
type="button" type="button"
@@ -48,7 +48,7 @@ export function AppShell({ children }: { children: React.ReactNode }) {
{t("sidebar.productTitle")} {t("sidebar.productTitle")}
</div> </div>
</div> </div>
<UtilityControls /> <UtilityControls initialTheme={initialTheme} />
</header> </header>
<main className="flex-1">{children}</main> <main className="flex-1">{children}</main>
</div> </div>

View File

@@ -2,17 +2,26 @@
import Link from "next/link"; import Link from "next/link";
import { usePathname, useRouter } from "next/navigation"; import { usePathname, useRouter } from "next/navigation";
import { useEffect, useState } from "react"; import { useEffect, useMemo, useState } from "react";
import { BarChart3, Bell, LayoutGrid, LogOut, Settings, Wrench, X } from "lucide-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 { useI18n } from "@/lib/i18n/useI18n";
const items = [ type NavItem = {
href: string;
labelKey: string;
icon: LucideIcon;
ownerOnly?: boolean;
};
const items: NavItem[] = [
{ href: "/overview", labelKey: "nav.overview", icon: LayoutGrid }, { href: "/overview", labelKey: "nav.overview", icon: LayoutGrid },
{ href: "/machines", labelKey: "nav.machines", icon: Wrench }, { href: "/machines", labelKey: "nav.machines", icon: Wrench },
{ href: "/reports", labelKey: "nav.reports", icon: BarChart3 }, { href: "/reports", labelKey: "nav.reports", icon: BarChart3 },
{ href: "/alerts", labelKey: "nav.alerts", icon: Bell }, { href: "/alerts", labelKey: "nav.alerts", icon: Bell },
{ href: "/financial", labelKey: "nav.financial", icon: DollarSign, ownerOnly: true },
{ href: "/settings", labelKey: "nav.settings", icon: Settings }, { href: "/settings", labelKey: "nav.settings", icon: Settings },
] as const; ];
type SidebarProps = { type SidebarProps = {
variant?: "desktop" | "drawer"; variant?: "desktop" | "drawer";
@@ -57,6 +66,14 @@ export function Sidebar({ variant = "desktop", onNavigate, onClose }: SidebarPro
} }
const roleKey = (me?.membership?.role || "MEMBER").toLowerCase(); const roleKey = (me?.membership?.role || "MEMBER").toLowerCase();
const isOwner = roleKey === "owner";
const visibleItems = useMemo(() => items.filter((it) => !it.ownerOnly || isOwner), [isOwner]);
useEffect(() => {
visibleItems.forEach((it) => {
router.prefetch(it.href);
});
}, [router, visibleItems]);
const shellClass = [ const shellClass = [
"relative z-20 flex flex-col border-r border-white/10 bg-black/40", "relative z-20 flex flex-col border-r border-white/10 bg-black/40",
variant === "desktop" ? "hidden md:flex h-screen w-64" : "flex h-full w-72 max-w-[85vw]", variant === "desktop" ? "hidden md:flex h-screen w-64" : "flex h-full w-72 max-w-[85vw]",
@@ -82,13 +99,14 @@ export function Sidebar({ variant = "desktop", onNavigate, onClose }: SidebarPro
</div> </div>
<nav className="px-3 py-2 flex-1 space-y-1"> <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 active = pathname === it.href || pathname.startsWith(it.href + "/");
const Icon = it.icon; const Icon = it.icon;
return ( return (
<Link <Link
key={it.href} key={it.href}
href={it.href} href={it.href}
onMouseEnter={() => router.prefetch(it.href)}
onClick={onNavigate} onClick={onNavigate}
className={[ className={[
"flex items-center gap-3 rounded-xl px-3 py-2 text-sm transition", "flex items-center gap-3 rounded-xl px-3 py-2 text-sm transition",

View File

@@ -1,6 +1,6 @@
"use client"; "use client";
import { useEffect, useState } from "react"; import { useState } from "react";
import { useRouter } from "next/navigation"; import { useRouter } from "next/navigation";
import { useI18n } from "@/lib/i18n/useI18n"; import { useI18n } from "@/lib/i18n/useI18n";
@@ -46,19 +46,13 @@ const MoonIcon = ({ className }: { className?: string }) => (
type UtilityControlsProps = { type UtilityControlsProps = {
className?: string; className?: string;
initialTheme?: "dark" | "light";
}; };
export function UtilityControls({ className }: UtilityControlsProps) { export function UtilityControls({ className, initialTheme = "dark" }: UtilityControlsProps) {
const router = useRouter(); const router = useRouter();
const { locale, setLocale, t } = useI18n(); const { locale, setLocale, t } = useI18n();
const [theme, setTheme] = useState<"dark" | "light">("dark"); const [theme, setTheme] = useState<"dark" | "light">(initialTheme);
useEffect(() => {
const current = document.documentElement.getAttribute("data-theme");
if (current === "light" || current === "dark") {
setTheme(current);
}
}, []);
function applyTheme(next: "light" | "dark") { function applyTheme(next: "light" | "dark") {
document.documentElement.setAttribute("data-theme", next); document.documentElement.setAttribute("data-theme", next);
@@ -78,7 +72,7 @@ export function UtilityControls({ className }: UtilityControlsProps) {
return ( return (
<div <div
className={[ className={[
"pointer-events-auto flex items-center gap-3 rounded-xl border border-white/10 bg-white/5 px-3 py-2", "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 ?? "", className ?? "",
].join(" ")} ].join(" ")}
title={t("sidebar.themeTooltip")} title={t("sidebar.themeTooltip")}
@@ -91,7 +85,7 @@ export function UtilityControls({ className }: UtilityControlsProps) {
> >
{theme === "light" ? <SunIcon className="h-4 w-4" /> : <MoonIcon className="h-4 w-4" />} {theme === "light" ? <SunIcon className="h-4 w-4" /> : <MoonIcon className="h-4 w-4" />}
</button> </button>
<div className="flex items-center gap-2 text-[11px] font-semibold tracking-[0.2em]"> <div className="flex items-center gap-2 text-[10px] font-semibold tracking-[0.2em] sm:text-[11px]">
<button <button
type="button" type="button"
onClick={() => switchLocale("en")} onClick={() => switchLocale("en")}

View 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>
);
}

View 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>
);
}

View File

@@ -1,7 +1,7 @@
import { prisma } from "@/lib/prisma"; import { prisma } from "@/lib/prisma";
import { sendEmail } from "@/lib/email"; import { sendEmail } from "@/lib/email";
import { sendSms } from "@/lib/sms"; import { sendSms } from "@/lib/sms";
import { AlertPolicySchema, DEFAULT_POLICY, normalizeAlertPolicy } from "@/lib/alerts/policy"; import { AlertPolicySchema, DEFAULT_POLICY } from "@/lib/alerts/policy";
type Recipient = { type Recipient = {
userId?: string; userId?: string;
@@ -13,12 +13,43 @@ type Recipient = {
}; };
function normalizeEventType(value: unknown) { function normalizeEventType(value: unknown) {
return String(value ?? "").trim().toLowerCase(); 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 extractDurationSec(raw: any): number | null { function asRecord(value: unknown): Record<string, unknown> | null {
if (!raw || typeof raw !== "object") return null; if (!value || typeof value !== "object" || Array.isArray(value)) return null;
const data = raw.data ?? raw; 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 = [ const candidates = [
data?.duration_seconds, data?.duration_seconds,
data?.duration_sec, data?.duration_sec,
@@ -67,6 +98,7 @@ async function ensurePolicy(orgId: string) {
async function loadRecipients(orgId: string, role: string, eventType: string): Promise<Recipient[]> { async function loadRecipients(orgId: string, role: string, eventType: string): Promise<Recipient[]> {
const roleUpper = role.toUpperCase(); const roleUpper = role.toUpperCase();
const normalizedEventType = normalizeEventType(eventType);
const [members, external] = await Promise.all([ const [members, external] = await Promise.all([
prisma.orgUser.findMany({ prisma.orgUser.findMany({
where: { orgId, role: roleUpper }, where: { orgId, role: roleUpper },
@@ -105,7 +137,7 @@ async function loadRecipients(orgId: string, role: string, eventType: string): P
.filter((c) => { .filter((c) => {
const types = Array.isArray(c.eventTypes) ? c.eventTypes : null; const types = Array.isArray(c.eventTypes) ? c.eventTypes : null;
if (!types || !types.length) return true; if (!types || !types.length) return true;
return types.includes(eventType); return types.some((type) => normalizeEventType(type) === normalizedEventType);
}) })
.map((c) => ({ .map((c) => ({
contactId: c.id, contactId: c.id,
@@ -143,7 +175,7 @@ function buildAlertMessage(params: {
} }
async function shouldSendNotification(params: { async function shouldSendNotification(params: {
eventId: string; eventIds: string[];
ruleId: string; ruleId: string;
role: string; role: string;
channel: string; channel: string;
@@ -153,7 +185,7 @@ async function shouldSendNotification(params: {
}) { }) {
const existing = await prisma.alertNotification.findFirst({ const existing = await prisma.alertNotification.findFirst({
where: { where: {
eventId: params.eventId, eventId: { in: params.eventIds },
ruleId: params.ruleId, ruleId: params.ruleId,
role: params.role, role: params.role,
channel: params.channel, channel: params.channel,
@@ -171,6 +203,22 @@ async function shouldSendNotification(params: {
return elapsed >= repeatMin * 60 * 1000; 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: { async function recordNotification(params: {
orgId: string; orgId: string;
machineId: string; machineId: string;
@@ -250,6 +298,18 @@ export async function evaluateAlertsForEvent(eventId: string) {
const rule = policy.rules.find((r) => normalizeEventType(r.eventType) === eventType); const rule = policy.rules.find((r) => normalizeEventType(r.eventType) === eventType);
if (!rule) return; 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 durationSec = extractDurationSec(event.data);
const durationMin = durationSec != null ? durationSec / 60 : 0; const durationMin = durationSec != null ? durationSec / 60 : 0;
const machine = await prisma.machine.findUnique({ const machine = await prisma.machine.findUnique({
@@ -257,6 +317,9 @@ export async function evaluateAlertsForEvent(eventId: string) {
select: { name: true, code: true }, select: { name: true, code: true },
}); });
const delivered = new Set<string>(); 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)) { for (const [roleName, roleRule] of Object.entries(rule.roles)) {
if (!roleRule?.enabled) continue; if (!roleRule?.enabled) continue;
@@ -283,7 +346,7 @@ export async function evaluateAlertsForEvent(eventId: string) {
if (delivered.has(key)) continue; if (delivered.has(key)) continue;
const allowed = await shouldSendNotification({ const allowed = await shouldSendNotification({
eventId: event.id, eventIds: notificationEventIds,
ruleId: rule.id, ruleId: rule.id,
role: roleName, role: roleName,
channel, channel,
@@ -321,8 +384,8 @@ export async function evaluateAlertsForEvent(eventId: string) {
status: "sent", status: "sent",
}); });
delivered.add(key); delivered.add(key);
} catch (err: any) { } catch (err: unknown) {
const msg = err?.message ? String(err.message) : "notification_failed"; const msg = err instanceof Error ? err.message : "notification_failed";
await recordNotification({ await recordNotification({
orgId: event.orgId, orgId: event.orgId,
machineId: event.machineId, machineId: event.machineId,

View 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,
};
}

View File

@@ -10,11 +10,17 @@ export const SCHEMA_VERSION = "1.0";
// KPI scale is frozen as 0..100 (you confirmed) // KPI scale is frozen as 0..100 (you confirmed)
const KPI_0_100 = z.number().min(0).max(100); 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) { function unwrapCanonicalEnvelope(raw: unknown) {
if (!raw || typeof raw !== "object") return raw; if (!raw || typeof raw !== "object") return raw;
const obj: any = raw; const obj = asRecord(raw);
const payload = obj.payload; if (!obj) return raw;
if (!payload || typeof payload !== "object") return raw; const payload = asRecord(obj.payload);
if (!payload) return raw;
const hasMeta = const hasMeta =
obj.schemaVersion !== undefined || obj.schemaVersion !== undefined ||
@@ -47,7 +53,8 @@ function unwrapCanonicalEnvelope(raw: unknown) {
function normalizeTsDevice(raw: unknown) { function normalizeTsDevice(raw: unknown) {
if (!raw || typeof raw !== "object") return raw; if (!raw || typeof raw !== "object") return raw;
const obj: any = raw; const obj = asRecord(raw);
if (!obj) return raw;
if (typeof obj.tsDevice === "number") return obj; if (typeof obj.tsDevice === "number") return obj;
if (typeof obj.tsMs === "number") return { ...obj, tsDevice: obj.tsMs }; if (typeof obj.tsMs === "number") return { ...obj, tsDevice: obj.tsMs };
return obj; return obj;
@@ -149,7 +156,10 @@ export function normalizeSnapshotV1(raw: unknown): { ok: true; value: SnapshotV1
if (!recheck.success) return { ok: false, error: recheck.error.message }; if (!recheck.success) return { ok: false, error: recheck.error.message };
return { ok: true, value: recheck.data }; return { ok: true, value: recheck.data };
*/ */
const b: any = legacy.data; const b = asRecord(legacy.data) ?? {};
const activeWorkOrder = asRecord(b.activeWorkOrder);
const kpis = asRecord(b.kpis);
const kpiSnapshot = asRecord(b.kpi_snapshot);
const legacyCycleTime = const legacyCycleTime =
b.cycleTime ?? b.cycleTime ??
@@ -157,57 +167,57 @@ export function normalizeSnapshotV1(raw: unknown): { ok: true; value: SnapshotV1
b.theoretical_cycle_time ?? b.theoretical_cycle_time ??
b.theoreticalCycleTime ?? b.theoreticalCycleTime ??
b.standard_cycle_time ?? b.standard_cycle_time ??
b.kpi_snapshot?.cycleTime ?? kpiSnapshot?.cycleTime ??
b.kpi_snapshot?.cycle_time ?? kpiSnapshot?.cycle_time ??
undefined; undefined;
const legacyActualCycleTime = const legacyActualCycleTime =
b.actualCycleTime ?? b.actualCycleTime ??
b.actual_cycle_time ?? b.actual_cycle_time ??
b.actualCycleSeconds ?? b.actualCycleSeconds ??
b.kpi_snapshot?.actualCycleTime ?? kpiSnapshot?.actualCycleTime ??
b.kpi_snapshot?.actual_cycle_time ?? kpiSnapshot?.actual_cycle_time ??
undefined; undefined;
const legacyWorkOrderId = const legacyWorkOrderId =
b.activeWorkOrder?.id ?? activeWorkOrder?.id ??
b.work_order_id ?? b.work_order_id ??
b.workOrderId ?? b.workOrderId ??
b.kpis?.workOrderId ?? kpis?.workOrderId ??
b.kpi_snapshot?.work_order_id ?? kpiSnapshot?.work_order_id ??
undefined; undefined;
const legacySku = const legacySku =
b.activeWorkOrder?.sku ?? activeWorkOrder?.sku ??
b.sku ?? b.sku ??
b.kpis?.sku ?? kpis?.sku ??
b.kpi_snapshot?.sku ?? kpiSnapshot?.sku ??
undefined; undefined;
const legacyTarget = const legacyTarget =
b.activeWorkOrder?.target ?? activeWorkOrder?.target ??
b.target ?? b.target ??
b.kpis?.target ?? kpis?.target ??
b.kpi_snapshot?.target ?? kpiSnapshot?.target ??
undefined; undefined;
const legacyGood = const legacyGood =
b.activeWorkOrder?.good ?? activeWorkOrder?.good ??
b.good_parts ?? b.good_parts ??
b.good ?? b.good ??
b.kpis?.good ?? kpis?.good ??
b.kpi_snapshot?.good_parts ?? kpiSnapshot?.good_parts ??
undefined; undefined;
const legacyScrap = const legacyScrap =
b.activeWorkOrder?.scrap ?? activeWorkOrder?.scrap ??
b.scrap_parts ?? b.scrap_parts ??
b.scrap ?? b.scrap ??
b.kpis?.scrap ?? kpis?.scrap ??
b.kpi_snapshot?.scrap_parts ?? kpiSnapshot?.scrap_parts ??
undefined; undefined;
const migrated: any = { const migrated: Record<string, unknown> = {
schemaVersion: SCHEMA_VERSION, schemaVersion: SCHEMA_VERSION,
machineId: String(b.machineId), machineId: String(b.machineId),
tsDevice: typeof b.tsDevice === "number" ? b.tsDevice : Date.now(), tsDevice: typeof b.tsDevice === "number" ? b.tsDevice : Date.now(),
@@ -225,11 +235,10 @@ export function normalizeSnapshotV1(raw: unknown): { ok: true; value: SnapshotV1
good: legacyGood != null ? Number(legacyGood) : undefined, good: legacyGood != null ? Number(legacyGood) : undefined,
scrap: legacyScrap != null ? Number(legacyScrap) : undefined, scrap: legacyScrap != null ? Number(legacyScrap) : undefined,
} }
: b.activeWorkOrder, : activeWorkOrder,
// keep everything else // keep everything else
...b, ...b,
}; };
const recheck = SnapshotV1.safeParse(migrated); const recheck = SnapshotV1.safeParse(migrated);
if (!recheck.success) return { ok: false, error: recheck.error.message }; if (!recheck.success) return { ok: false, error: recheck.error.message };
@@ -262,8 +271,8 @@ export function normalizeHeartbeatV1(raw: unknown) {
const legacy = z.object({ machineId: z.any() }).passthrough().safeParse(candidate); const legacy = z.object({ machineId: z.any() }).passthrough().safeParse(candidate);
if (!legacy.success) return { ok: false as const, error: strict.error.message }; if (!legacy.success) return { ok: false as const, error: strict.error.message };
const b: any = legacy.data; const b = asRecord(legacy.data) ?? {};
const migrated: any = { const migrated: Record<string, unknown> = {
schemaVersion: SCHEMA_VERSION, schemaVersion: SCHEMA_VERSION,
machineId: String(b.machineId), machineId: String(b.machineId),
tsDevice: typeof b.tsDevice === "number" ? b.tsDevice : Date.now(), tsDevice: typeof b.tsDevice === "number" ? b.tsDevice : Date.now(),
@@ -304,11 +313,22 @@ export function normalizeCycleV1(raw: unknown) {
const legacy = z.object({ machineId: z.any(), cycle: z.any() }).passthrough().safeParse(candidate); 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 }; if (!legacy.success) return { ok: false as const, error: strict.error.message };
const b: any = legacy.data; const b = asRecord(legacy.data) ?? {};
const tsDevice = typeof b.tsDevice === "number" ? b.tsDevice : (b.cycle?.timestamp ?? Date.now()); const cycle = asRecord(b.cycle);
const seq = typeof b.seq === "number" || typeof b.seq === "string" ? b.seq : (b.cycle?.cycle_count ?? "0"); 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: any = { schemaVersion: SCHEMA_VERSION, machineId: String(b.machineId), tsDevice, seq, ...b }; const migrated: Record<string, unknown> = {
schemaVersion: SCHEMA_VERSION,
machineId: String(b.machineId),
tsDevice,
seq,
...b,
};
const recheck = CycleV1.safeParse(migrated); const recheck = CycleV1.safeParse(migrated);
if (!recheck.success) return { ok: false as const, error: recheck.error.message }; if (!recheck.success) return { ok: false as const, error: recheck.error.message };
return { ok: true as const, value: recheck.data }; return { ok: true as const, value: recheck.data };
@@ -343,9 +363,11 @@ export function normalizeEventV1(raw: unknown) {
const legacy = z.object({ machineId: z.any(), event: z.any() }).passthrough().safeParse(candidate); 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 }; if (!legacy.success) return { ok: false as const, error: strict.error.message };
const b: any = legacy.data; const b = asRecord(legacy.data) ?? {};
const tsDevice = typeof b.tsDevice === "number" ? b.tsDevice : (b.event?.timestamp ?? Date.now()); const event = asRecord(b.event);
const migrated: any = { const tsDevice =
typeof b.tsDevice === "number" ? b.tsDevice : (event?.timestamp as number | undefined) ?? Date.now();
const migrated: Record<string, unknown> = {
schemaVersion: SCHEMA_VERSION, schemaVersion: SCHEMA_VERSION,
machineId: String(b.machineId), machineId: String(b.machineId),
tsDevice, tsDevice,

View File

@@ -1,7 +1,5 @@
import nodemailer from "nodemailer"; import nodemailer from "nodemailer";
import { logLine } from "@/lib/logger"; import { logLine } from "@/lib/logger";
type EmailPayload = { type EmailPayload = {
to: string; to: string;
subject: string; subject: string;
@@ -60,7 +58,6 @@ export async function sendEmail(payload: EmailPayload) {
from, from,
}); });
const transporter = getTransporter(); const transporter = getTransporter();
try { try {
const info = await transporter.sendMail({ const info = await transporter.sendMail({
@@ -77,6 +74,7 @@ export async function sendEmail(payload: EmailPayload) {
}); });
// Nodemailer response details: // Nodemailer response details:
const pending = "pending" in info ? (info as { pending?: string[] }).pending : undefined;
logLine("email.send.ok", { logLine("email.send.ok", {
to: payload.to, to: payload.to,
from, from,
@@ -84,25 +82,33 @@ export async function sendEmail(payload: EmailPayload) {
response: info.response, response: info.response,
accepted: info.accepted, accepted: info.accepted,
rejected: info.rejected, rejected: info.rejected,
pending: (info as any).pending, pending,
}); });
return info; return info;
} catch (err: any) { } 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", { logLine("email.send.err", {
to: payload.to, to: payload.to,
from, from,
name: err?.name, name: error?.name,
message: err?.message, message: error?.message,
code: err?.code, code: error?.code,
command: err?.command, command: error?.command,
response: err?.response, response: error?.response,
responseCode: err?.responseCode, responseCode: error?.responseCode,
stack: err?.stack, stack: error?.stack,
}); });
throw err; throw err;
} }
} }
export function buildVerifyEmail(params: { appName: string; verifyUrl: string }) { export function buildVerifyEmail(params: { appName: string; verifyUrl: string }) {

View 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
View 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 },
};
}

View File

@@ -13,6 +13,7 @@
"nav.machines": "Machines", "nav.machines": "Machines",
"nav.reports": "Reports", "nav.reports": "Reports",
"nav.alerts": "Alerts", "nav.alerts": "Alerts",
"nav.financial": "Financial",
"nav.settings": "Settings", "nav.settings": "Settings",
"sidebar.productTitle": "MIS", "sidebar.productTitle": "MIS",
"sidebar.productSubtitle": "Control Tower", "sidebar.productSubtitle": "Control Tower",
@@ -220,7 +221,7 @@
"reports.qualitySummary": "Quality Summary", "reports.qualitySummary": "Quality Summary",
"reports.notes": "Notes for Ops", "reports.notes": "Notes for Ops",
"alerts.title": "Alerts", "alerts.title": "Alerts",
"alerts.subtitle": "Escalation policies, channels, and contacts.", "alerts.subtitle": "Alert history with filters and drilldowns.",
"alerts.comingSoon": "Alert configuration UI is coming soon.", "alerts.comingSoon": "Alert configuration UI is coming soon.",
"alerts.loading": "Loading alerts...", "alerts.loading": "Loading alerts...",
"alerts.error.loadPolicy": "Failed to load alert policy.", "alerts.error.loadPolicy": "Failed to load alert policy.",
@@ -271,6 +272,53 @@
"alerts.contacts.role.admin": "Admin", "alerts.contacts.role.admin": "Admin",
"alerts.contacts.role.owner": "Owner", "alerts.contacts.role.owner": "Owner",
"alerts.contacts.readOnly": "You can view contacts, but only owners can add or edit.", "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.suggested": "Suggested actions",
"reports.notes.none": "No insights yet. Generate reports after data collection.", "reports.notes.none": "No insights yet. Generate reports after data collection.",
"reports.noTrend": "No trend data yet.", "reports.noTrend": "No trend data yet.",
@@ -309,6 +357,12 @@
"reports.pdf.none": "None", "reports.pdf.none": "None",
"settings.title": "Settings", "settings.title": "Settings",
"settings.subtitle": "Live configuration for shifts, alerts, and defaults.", "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.loading": "Loading settings...",
"settings.loadingTeam": "Loading team...", "settings.loadingTeam": "Loading team...",
"settings.refresh": "Refresh", "settings.refresh": "Refresh",
@@ -391,5 +445,65 @@
"settings.integrations": "Integrations", "settings.integrations": "Integrations",
"settings.integrations.webhook": "Webhook URL", "settings.integrations.webhook": "Webhook URL",
"settings.integrations.erp": "ERP Sync", "settings.integrations.erp": "ERP Sync",
"settings.integrations.erpNotConfigured": "Not configured" "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"
} }

View File

@@ -13,6 +13,7 @@
"nav.machines": "Máquinas", "nav.machines": "Máquinas",
"nav.reports": "Reportes", "nav.reports": "Reportes",
"nav.alerts": "Alertas", "nav.alerts": "Alertas",
"nav.financial": "Finanzas",
"nav.settings": "Configuración", "nav.settings": "Configuración",
"sidebar.productTitle": "MIS", "sidebar.productTitle": "MIS",
"sidebar.productSubtitle": "Control Tower", "sidebar.productSubtitle": "Control Tower",
@@ -220,7 +221,7 @@
"reports.qualitySummary": "Resumen de calidad", "reports.qualitySummary": "Resumen de calidad",
"reports.notes": "Notas para operaciones", "reports.notes": "Notas para operaciones",
"alerts.title": "Alertas", "alerts.title": "Alertas",
"alerts.subtitle": "Politicas de escalamiento, canales y contactos.", "alerts.subtitle": "Historial de alertas con filtros y detalle.",
"alerts.comingSoon": "La configuracion de alertas estara disponible pronto.", "alerts.comingSoon": "La configuracion de alertas estara disponible pronto.",
"alerts.loading": "Cargando alertas...", "alerts.loading": "Cargando alertas...",
"alerts.error.loadPolicy": "No se pudo cargar la politica de alertas.", "alerts.error.loadPolicy": "No se pudo cargar la politica de alertas.",
@@ -271,6 +272,53 @@
"alerts.contacts.role.admin": "Admin", "alerts.contacts.role.admin": "Admin",
"alerts.contacts.role.owner": "Propietario", "alerts.contacts.role.owner": "Propietario",
"alerts.contacts.readOnly": "Puedes ver contactos, pero solo propietarios pueden agregar o editar.", "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.suggested": "Acciones sugeridas",
"reports.notes.none": "Sin insights todavía. Genera reportes tras recolectar datos.", "reports.notes.none": "Sin insights todavía. Genera reportes tras recolectar datos.",
"reports.noTrend": "Sin datos de tendencia.", "reports.noTrend": "Sin datos de tendencia.",
@@ -309,6 +357,12 @@
"reports.pdf.none": "Ninguna", "reports.pdf.none": "Ninguna",
"settings.title": "Configuración", "settings.title": "Configuración",
"settings.subtitle": "Configuración en vivo para turnos, alertas y valores predeterminados.", "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.loading": "Cargando configuración...",
"settings.loadingTeam": "Cargando equipo...", "settings.loadingTeam": "Cargando equipo...",
"settings.refresh": "Actualizar", "settings.refresh": "Actualizar",
@@ -391,5 +445,65 @@
"settings.integrations": "Integraciones", "settings.integrations": "Integraciones",
"settings.integrations.webhook": "Webhook URL", "settings.integrations.webhook": "Webhook URL",
"settings.integrations.erp": "ERP Sync", "settings.integrations.erp": "ERP Sync",
"settings.integrations.erpNotConfigured": "No configurado" "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"
} }

View File

@@ -3,7 +3,7 @@ import path from "path";
const LOG_PATH = process.env.LOG_FILE || "/tmp/mis-control-tower.log"; const LOG_PATH = process.env.LOG_FILE || "/tmp/mis-control-tower.log";
export function logLine(event: string, data: Record<string, any> = {}) { export function logLine(event: string, data: Record<string, unknown> = {}) {
const line = JSON.stringify({ const line = JSON.stringify({
ts: new Date().toISOString(), ts: new Date().toISOString(),
event, event,

View 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 };
}

33
lib/prismaJson.ts Normal file
View 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);
}

View File

@@ -18,23 +18,48 @@ export const DEFAULT_SHIFT = {
end: "15:00", end: "15:00",
}; };
type AnyRecord = Record<string, any>; type AnyRecord = Record<string, unknown>;
function isPlainObject(value: any): value is AnyRecord { function isPlainObject(value: unknown): value is AnyRecord {
return !!value && typeof value === "object" && !Array.isArray(value); return !!value && typeof value === "object" && !Array.isArray(value);
} }
export function normalizeAlerts(raw: any) { export function normalizeAlerts(raw: unknown) {
if (!isPlainObject(raw)) return { ...DEFAULT_ALERTS }; if (!isPlainObject(raw)) return { ...DEFAULT_ALERTS };
return { ...DEFAULT_ALERTS, ...raw }; return { ...DEFAULT_ALERTS, ...raw };
} }
export function normalizeDefaults(raw: any) { export function normalizeDefaults(raw: unknown) {
if (!isPlainObject(raw)) return { ...DEFAULT_DEFAULTS }; if (!isPlainObject(raw)) return { ...DEFAULT_DEFAULTS };
return { ...DEFAULT_DEFAULTS, ...raw }; return { ...DEFAULT_DEFAULTS, ...raw };
} }
export function buildSettingsPayload(settings: any, shifts: any[]) { 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 ordered = [...(shifts ?? [])].sort((a, b) => (a.sortOrder ?? 0) - (b.sortOrder ?? 0));
const mappedShifts = ordered.map((s, idx) => ({ const mappedShifts = ordered.map((s, idx) => ({
name: s.name || `Shift ${idx + 1}`, name: s.name || `Shift ${idx + 1}`,
@@ -66,7 +91,7 @@ export function buildSettingsPayload(settings: any, shifts: any[]) {
}; };
} }
export function deepMerge(base: any, override: any): any { export function deepMerge(base: unknown, override: unknown): unknown {
if (!isPlainObject(base) || !isPlainObject(override)) return override; if (!isPlainObject(base) || !isPlainObject(override)) return override;
const out: AnyRecord = { ...base }; const out: AnyRecord = { ...base };
for (const [key, value] of Object.entries(override)) { for (const [key, value] of Object.entries(override)) {
@@ -80,7 +105,7 @@ export function deepMerge(base: any, override: any): any {
return out; return out;
} }
export function applyOverridePatch(existing: any, patch: any) { export function applyOverridePatch(existing: unknown, patch: unknown) {
const base: AnyRecord = isPlainObject(existing) ? { ...existing } : {}; const base: AnyRecord = isPlainObject(existing) ? { ...existing } : {};
if (!isPlainObject(patch)) return base; if (!isPlainObject(patch)) return base;
@@ -106,18 +131,29 @@ export function applyOverridePatch(existing: any, patch: any) {
return base; return base;
} }
export function validateShiftSchedule(shifts: any[]) { 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 (!Array.isArray(shifts)) return { ok: false, error: "shifts must be an array" };
if (shifts.length > 3) return { ok: false, error: "shifts max is 3" }; if (shifts.length > 3) return { ok: false, error: "shifts max is 3" };
const normalized = shifts.map((raw, idx) => { const normalized: ShiftValidationResult[] = shifts.map((raw, idx) => {
const start = String(raw?.start ?? "").trim(); const record = isPlainObject(raw) ? raw : {};
const end = String(raw?.end ?? "").trim(); const start = String(record.start ?? "").trim();
const end = String(record.end ?? "").trim();
if (!TIME_RE.test(start) || !TIME_RE.test(end)) { if (!TIME_RE.test(start) || !TIME_RE.test(end)) {
return { error: `shift ${idx + 1} start/end must be HH:mm` }; return { error: `shift ${idx + 1} start/end must be HH:mm` };
} }
const name = String(raw?.name ?? `Shift ${idx + 1}`).trim() || `Shift ${idx + 1}`; const name = String(record.name ?? `Shift ${idx + 1}`).trim() || `Shift ${idx + 1}`;
const enabled = raw?.enabled !== false; const enabled = record.enabled !== false;
return { return {
name, name,
startTime: start, startTime: start,
@@ -127,13 +163,13 @@ export function validateShiftSchedule(shifts: any[]) {
}; };
}); });
const firstError = normalized.find((s: any) => s?.error); const firstError = normalized.find((s): s is { error: string } => "error" in s);
if (firstError) return { ok: false, error: firstError.error }; if (firstError) return { ok: false, error: firstError.error };
return { ok: true, shifts: normalized as any[] }; return { ok: true, shifts: normalized as NormalizedShift[] };
} }
export function validateShiftFields(shiftChangeCompensationMin?: any, lunchBreakMin?: any) { export function validateShiftFields(shiftChangeCompensationMin?: unknown, lunchBreakMin?: unknown) {
if (shiftChangeCompensationMin != null) { if (shiftChangeCompensationMin != null) {
const v = Number(shiftChangeCompensationMin); const v = Number(shiftChangeCompensationMin);
if (!Number.isFinite(v) || v < 0 || v > 480) { if (!Number.isFinite(v) || v < 0 || v > 480) {
@@ -149,7 +185,7 @@ export function validateShiftFields(shiftChangeCompensationMin?: any, lunchBreak
return { ok: true }; return { ok: true };
} }
export function validateThresholds(thresholds: any) { export function validateThresholds(thresholds: unknown) {
if (!isPlainObject(thresholds)) return { ok: true }; if (!isPlainObject(thresholds)) return { ok: true };
const stoppage = thresholds.stoppageMultiplier; const stoppage = thresholds.stoppageMultiplier;
@@ -195,7 +231,7 @@ export function validateThresholds(thresholds: any) {
return { ok: true }; return { ok: true };
} }
export function validateDefaults(defaults: any) { export function validateDefaults(defaults: unknown) {
if (!isPlainObject(defaults)) return { ok: true }; if (!isPlainObject(defaults)) return { ok: true };
const moldTotal = defaults.moldTotal != null ? Number(defaults.moldTotal) : null; const moldTotal = defaults.moldTotal != null ? Number(defaults.moldTotal) : null;
@@ -216,7 +252,7 @@ export function validateDefaults(defaults: any) {
return { ok: true }; return { ok: true };
} }
export function pickUpdateValue(input: any) { export function pickUpdateValue(input: unknown) {
return input === undefined ? undefined : input; return input === undefined ? undefined : input;
} }

View File

@@ -3,6 +3,7 @@ type SmsPayload = {
body: string; body: string;
}; };
export async function sendSms(_payload: SmsPayload) { export async function sendSms(payload: SmsPayload) {
void payload;
throw new Error("SMS not configured"); throw new Error("SMS not configured");
} }

View File

@@ -0,0 +1,110 @@
-- CreateTable
CREATE TABLE "org_financial_profiles" (
"org_id" TEXT NOT NULL,
"default_currency" TEXT NOT NULL DEFAULT 'USD',
"machine_cost_per_min" DOUBLE PRECISION,
"operator_cost_per_min" DOUBLE PRECISION,
"rated_running_kw" DOUBLE PRECISION,
"idle_kw" DOUBLE PRECISION,
"kwh_rate" DOUBLE PRECISION,
"energy_multiplier" DOUBLE PRECISION NOT NULL DEFAULT 1.0,
"energy_cost_per_min" DOUBLE PRECISION,
"scrap_cost_per_unit" DOUBLE PRECISION,
"raw_material_cost_per_unit" DOUBLE PRECISION,
"created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updated_at" TIMESTAMP(3) NOT NULL,
"updated_by" TEXT,
CONSTRAINT "org_financial_profiles_pkey" PRIMARY KEY ("org_id")
);
-- CreateTable
CREATE TABLE "location_financial_overrides" (
"id" TEXT NOT NULL,
"org_id" TEXT NOT NULL,
"location" TEXT NOT NULL,
"currency" TEXT,
"machine_cost_per_min" DOUBLE PRECISION,
"operator_cost_per_min" DOUBLE PRECISION,
"rated_running_kw" DOUBLE PRECISION,
"idle_kw" DOUBLE PRECISION,
"kwh_rate" DOUBLE PRECISION,
"energy_multiplier" DOUBLE PRECISION,
"energy_cost_per_min" DOUBLE PRECISION,
"scrap_cost_per_unit" DOUBLE PRECISION,
"raw_material_cost_per_unit" DOUBLE PRECISION,
"created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updated_at" TIMESTAMP(3) NOT NULL,
"updated_by" TEXT,
CONSTRAINT "location_financial_overrides_pkey" PRIMARY KEY ("id")
);
-- CreateTable
CREATE TABLE "machine_financial_overrides" (
"id" TEXT NOT NULL,
"org_id" TEXT NOT NULL,
"machine_id" TEXT NOT NULL,
"currency" TEXT,
"machine_cost_per_min" DOUBLE PRECISION,
"operator_cost_per_min" DOUBLE PRECISION,
"rated_running_kw" DOUBLE PRECISION,
"idle_kw" DOUBLE PRECISION,
"kwh_rate" DOUBLE PRECISION,
"energy_multiplier" DOUBLE PRECISION,
"energy_cost_per_min" DOUBLE PRECISION,
"scrap_cost_per_unit" DOUBLE PRECISION,
"raw_material_cost_per_unit" DOUBLE PRECISION,
"created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updated_at" TIMESTAMP(3) NOT NULL,
"updated_by" TEXT,
CONSTRAINT "machine_financial_overrides_pkey" PRIMARY KEY ("id")
);
-- CreateTable
CREATE TABLE "product_cost_overrides" (
"id" TEXT NOT NULL,
"org_id" TEXT NOT NULL,
"sku" TEXT NOT NULL,
"currency" TEXT,
"raw_material_cost_per_unit" DOUBLE PRECISION,
"created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updated_at" TIMESTAMP(3) NOT NULL,
"updated_by" TEXT,
CONSTRAINT "product_cost_overrides_pkey" PRIMARY KEY ("id")
);
-- CreateIndex
CREATE UNIQUE INDEX "location_financial_overrides_org_id_location_key" ON "location_financial_overrides"("org_id", "location");
-- CreateIndex
CREATE UNIQUE INDEX "machine_financial_overrides_org_id_machine_id_key" ON "machine_financial_overrides"("org_id", "machine_id");
-- CreateIndex
CREATE UNIQUE INDEX "product_cost_overrides_org_id_sku_key" ON "product_cost_overrides"("org_id", "sku");
-- CreateIndex
CREATE INDEX "location_financial_overrides_org_id_idx" ON "location_financial_overrides"("org_id");
-- CreateIndex
CREATE INDEX "machine_financial_overrides_org_id_idx" ON "machine_financial_overrides"("org_id");
-- CreateIndex
CREATE INDEX "product_cost_overrides_org_id_idx" ON "product_cost_overrides"("org_id");
-- AddForeignKey
ALTER TABLE "org_financial_profiles" ADD CONSTRAINT "org_financial_profiles_org_id_fkey" FOREIGN KEY ("org_id") REFERENCES "Org"("id") ON DELETE CASCADE ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "location_financial_overrides" ADD CONSTRAINT "location_financial_overrides_org_id_fkey" FOREIGN KEY ("org_id") REFERENCES "Org"("id") ON DELETE CASCADE ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "machine_financial_overrides" ADD CONSTRAINT "machine_financial_overrides_org_id_fkey" FOREIGN KEY ("org_id") REFERENCES "Org"("id") ON DELETE CASCADE ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "machine_financial_overrides" ADD CONSTRAINT "machine_financial_overrides_machine_id_fkey" FOREIGN KEY ("machine_id") REFERENCES "Machine"("id") ON DELETE CASCADE ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "product_cost_overrides" ADD CONSTRAINT "product_cost_overrides_org_id_fkey" FOREIGN KEY ("org_id") REFERENCES "Org"("id") ON DELETE CASCADE ON UPDATE CASCADE;

View File

@@ -28,6 +28,10 @@ model Org {
alertPolicies AlertPolicy[] alertPolicies AlertPolicy[]
alertContacts AlertContact[] alertContacts AlertContact[]
alertNotifications AlertNotification[] alertNotifications AlertNotification[]
financialProfile OrgFinancialProfile?
locationFinancialOverrides LocationFinancialOverride[]
machineFinancialOverrides MachineFinancialOverride[]
productCostOverrides ProductCostOverride[]
} }
model User { model User {
@@ -130,6 +134,7 @@ model Machine {
settings MachineSettings? settings MachineSettings?
settingsAudits SettingsAudit[] settingsAudits SettingsAudit[]
alertNotifications AlertNotification[] alertNotifications AlertNotification[]
financialOverrides MachineFinancialOverride[]
@@unique([orgId, name]) @@unique([orgId, name])
@@index([orgId]) @@index([orgId])
@@ -313,6 +318,95 @@ model OrgSettings {
@@map("org_settings") @@map("org_settings")
} }
model OrgFinancialProfile {
orgId String @id @map("org_id")
defaultCurrency String @default("USD") @map("default_currency")
machineCostPerMin Float? @map("machine_cost_per_min")
operatorCostPerMin Float? @map("operator_cost_per_min")
ratedRunningKw Float? @map("rated_running_kw")
idleKw Float? @map("idle_kw")
kwhRate Float? @map("kwh_rate")
energyMultiplier Float @default(1.0) @map("energy_multiplier")
energyCostPerMin Float? @map("energy_cost_per_min")
scrapCostPerUnit Float? @map("scrap_cost_per_unit")
rawMaterialCostPerUnit Float? @map("raw_material_cost_per_unit")
createdAt DateTime @default(now()) @map("created_at")
updatedAt DateTime @updatedAt @map("updated_at")
updatedBy String? @map("updated_by")
org Org @relation(fields: [orgId], references: [id], onDelete: Cascade)
@@map("org_financial_profiles")
}
model LocationFinancialOverride {
id String @id @default(uuid())
orgId String @map("org_id")
location String
currency String?
machineCostPerMin Float? @map("machine_cost_per_min")
operatorCostPerMin Float? @map("operator_cost_per_min")
ratedRunningKw Float? @map("rated_running_kw")
idleKw Float? @map("idle_kw")
kwhRate Float? @map("kwh_rate")
energyMultiplier Float? @map("energy_multiplier")
energyCostPerMin Float? @map("energy_cost_per_min")
scrapCostPerUnit Float? @map("scrap_cost_per_unit")
rawMaterialCostPerUnit Float? @map("raw_material_cost_per_unit")
createdAt DateTime @default(now()) @map("created_at")
updatedAt DateTime @updatedAt @map("updated_at")
updatedBy String? @map("updated_by")
org Org @relation(fields: [orgId], references: [id], onDelete: Cascade)
@@unique([orgId, location])
@@index([orgId])
@@map("location_financial_overrides")
}
model MachineFinancialOverride {
id String @id @default(uuid())
orgId String @map("org_id")
machineId String @map("machine_id")
currency String?
machineCostPerMin Float? @map("machine_cost_per_min")
operatorCostPerMin Float? @map("operator_cost_per_min")
ratedRunningKw Float? @map("rated_running_kw")
idleKw Float? @map("idle_kw")
kwhRate Float? @map("kwh_rate")
energyMultiplier Float? @map("energy_multiplier")
energyCostPerMin Float? @map("energy_cost_per_min")
scrapCostPerUnit Float? @map("scrap_cost_per_unit")
rawMaterialCostPerUnit Float? @map("raw_material_cost_per_unit")
createdAt DateTime @default(now()) @map("created_at")
updatedAt DateTime @updatedAt @map("updated_at")
updatedBy String? @map("updated_by")
org Org @relation(fields: [orgId], references: [id], onDelete: Cascade)
machine Machine @relation(fields: [machineId], references: [id], onDelete: Cascade)
@@unique([orgId, machineId])
@@index([orgId])
@@map("machine_financial_overrides")
}
model ProductCostOverride {
id String @id @default(uuid())
orgId String @map("org_id")
sku String
currency String?
rawMaterialCostPerUnit Float? @map("raw_material_cost_per_unit")
createdAt DateTime @default(now()) @map("created_at")
updatedAt DateTime @updatedAt @map("updated_at")
updatedBy String? @map("updated_by")
org Org @relation(fields: [orgId], references: [id], onDelete: Cascade)
@@unique([orgId, sku])
@@index([orgId])
@@map("product_cost_overrides")
}
model AlertPolicy { model AlertPolicy {
id String @id @default(uuid()) id String @id @default(uuid())
orgId String @map("org_id") orgId String @map("org_id")