Compare commits
32 Commits
main
...
b2214ec46f
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b2214ec46f | ||
|
|
5e7ddaa0db | ||
|
|
62169b163c | ||
|
|
7e0fe5c2e1 | ||
|
|
66c89f9bf4 | ||
|
|
30513ff73d | ||
|
|
5d3a2c533f | ||
|
|
6aaafb9115 | ||
|
|
4973c18dc3 | ||
|
|
e705f5e965 | ||
|
|
2707fd974a | ||
|
|
80d27f83b6 | ||
|
|
ac1a7900c8 | ||
|
|
511d80b629 | ||
|
|
c183dda383 | ||
|
|
0f88207f3f | ||
|
|
9f1af71d15 | ||
|
|
f231d87ae3 | ||
|
|
d0ab254dd7 | ||
|
|
7790361a0a | ||
|
|
05a30b2a21 | ||
|
|
ea92b32618 | ||
|
|
538b06bd4b | ||
|
|
b6eed8b6db | ||
|
|
a0ed517047 | ||
|
|
0ad2451dd4 | ||
|
|
d172eaf629 | ||
|
|
363c9fbf4f | ||
|
|
a369a69978 | ||
|
|
1fe0b4dbf9 | ||
|
|
945ff2dc09 | ||
|
|
ffc39a5c90 |
84
LOGGING.md
Normal file
84
LOGGING.md
Normal file
@@ -0,0 +1,84 @@
|
||||
# Logging & debugging errors
|
||||
|
||||
## Where errors are logged
|
||||
|
||||
### 1. **Log file** (JSON lines)
|
||||
|
||||
- **Path:** `LOG_FILE` env var, or **`/tmp/mis-control-tower.log`** if unset.
|
||||
- **Contents:** JSON lines for `requireSession.error`, `getOverviewData.error`, `OverviewPage.getOverviewData.error`, plus any `logLine(...)` usage (e.g. health, signup).
|
||||
|
||||
**View recent entries:**
|
||||
```bash
|
||||
tail -f /tmp/mis-control-tower.log
|
||||
```
|
||||
|
||||
**Or with a custom path:**
|
||||
```bashls -la
|
||||
export LOG_FILE=/var/log/mis-control-tower.log
|
||||
# then start the app; tail that path
|
||||
tail -f /var/log/mis-control-tower.log
|
||||
```
|
||||
|
||||
### 2. **Process stdout / stderr**
|
||||
|
||||
- **`console.error`** and **`console.log`** go to the process that runs Next.js.
|
||||
- **Dev:** terminal where you run `npm run dev`.
|
||||
- **Production:** PM2 logs (`pm2 logs`), Docker (`docker logs ...`), systemd (`journalctl -u your-service -f`), etc.
|
||||
|
||||
### 3. **Debug logs API** (optional)
|
||||
|
||||
- **URL:** `GET /api/debug/logs?key=YOUR_DEBUG_LOGS_KEY`
|
||||
- **Purpose:** Returns the last 100 lines of the log file as JSON.
|
||||
- **Setup:** Add to `.env`:
|
||||
```
|
||||
DEBUG_LOGS_KEY=your-secret-string
|
||||
```
|
||||
- **Usage:**
|
||||
`curl "https://mis.maliountech.com.mx/api/debug/logs?key=your-secret-string"`
|
||||
- If `DEBUG_LOGS_KEY` is unset or the `key` param is wrong, the route returns 401.
|
||||
|
||||
## Error events we log
|
||||
|
||||
| Event | When |
|
||||
|-------|------|
|
||||
| `requireSession.error` | Session lookup (cookies / DB) fails |
|
||||
| `getOverviewData.error` | Overview data fetch (DB) fails |
|
||||
| `OverviewPage.getOverviewData.error` | Overview page catch-around fetch fails |
|
||||
|
||||
Each includes `message` and `stack` when available.
|
||||
|
||||
## Quick checks when you see "Internal Server Error"
|
||||
|
||||
1. **Tail the log file:**
|
||||
`tail -f /tmp/mis-control-tower.log`
|
||||
(or `$LOG_FILE` if you set it.)
|
||||
|
||||
2. **Check process logs:**
|
||||
Wherever `next start` or `npm run dev` runs (PM2, Docker, systemd). Look for `[requireSession]`, `[getOverviewData]`, `[OverviewPage]`, or `[middleware]`.
|
||||
|
||||
3. **Call the debug API** (if configured):
|
||||
`curl "https://your-domain/api/debug/logs?key=YOUR_DEBUG_LOGS_KEY"`
|
||||
and inspect the `entries` array for recent errors.
|
||||
|
||||
## KPI quality trace (Node-RED vs processing)
|
||||
|
||||
Use this when `Quality` is shown as `0` and you need to see exactly what was received and saved.
|
||||
|
||||
1. Enable trace logging:
|
||||
`TRACE_KPI_INGEST=1`
|
||||
|
||||
2. Send KPI payloads as usual from Node-RED.
|
||||
|
||||
3. Inspect logs:
|
||||
`tail -f /tmp/mis-control-tower.log`
|
||||
or:
|
||||
`curl "https://your-domain/api/debug/logs?key=YOUR_DEBUG_LOGS_KEY"`
|
||||
|
||||
4. Look for event `ingest.kpi.trace`, which includes:
|
||||
`trace.rawQualityCandidates` (raw payload values found at multiple paths),
|
||||
`trace.normalizedQuality` (post-normalization),
|
||||
`trace.persistedQuality` (value written to DB).
|
||||
|
||||
Optional one-shot trace without env var:
|
||||
- Send header `x-debug-ingest: 1` on a KPI request.
|
||||
- The response will include a `trace` object with the same quality details.
|
||||
104
README.md
104
README.md
@@ -20,7 +20,109 @@ You can start editing the page by modifying `app/page.tsx`. The page auto-update
|
||||
|
||||
This project uses [`next/font`](https://nextjs.org/docs/app/building-your-application/optimizing/fonts) to automatically optimize and load [Geist](https://vercel.com/font), a new font family for Vercel.
|
||||
|
||||
## Learn More
|
||||
## Downtime Action Reminders
|
||||
|
||||
Reminders are sent by calling `POST /api/downtime/actions/reminders`. This endpoint does not run automatically, so you need to schedule it with cron or systemd. It sends at most one reminder per threshold (1w/1d/1h/overdue) and resets if the due date changes.
|
||||
The secret can be any random string; it just needs to match what your scheduler sends in the Authorization header.
|
||||
|
||||
1) Set a secret in your env file (example: `/etc/mis-control-tower.env`):
|
||||
|
||||
```
|
||||
DOWNTIME_ACTION_REMINDER_SECRET=your-secret-here
|
||||
APP_BASE_URL=https://your-domain
|
||||
```
|
||||
|
||||
2) Cron example (runs hourly for 1w/1d/1h/overdue thresholds):
|
||||
|
||||
```
|
||||
0 * * * * . /etc/mis-control-tower.env && curl -s -X POST "$APP_BASE_URL/api/downtime/actions/reminders?dueInDays=7" -H "Authorization: Bearer $DOWNTIME_ACTION_REMINDER_SECRET"
|
||||
```
|
||||
|
||||
If you prefer systemd instead of cron, you can create a small service + timer that runs the same curl command.
|
||||
|
||||
Example systemd units:
|
||||
|
||||
`/etc/systemd/system/mis-control-tower-reminders.service`
|
||||
|
||||
```
|
||||
[Unit]
|
||||
Description=MIS Control Tower downtime action reminders
|
||||
|
||||
[Service]
|
||||
Type=oneshot
|
||||
EnvironmentFile=/etc/mis-control-tower.env
|
||||
ExecStart=/usr/bin/curl -s -X POST "$APP_BASE_URL/api/downtime/actions/reminders?dueInDays=7" -H "Authorization: Bearer $DOWNTIME_ACTION_REMINDER_SECRET"
|
||||
```
|
||||
|
||||
`/etc/systemd/system/mis-control-tower-reminders.timer`
|
||||
|
||||
```
|
||||
[Unit]
|
||||
Description=Run MIS Control Tower reminders hourly
|
||||
|
||||
[Timer]
|
||||
OnCalendar=hourly
|
||||
Persistent=true
|
||||
|
||||
[Install]
|
||||
WantedBy=timers.target
|
||||
```
|
||||
|
||||
Enable with:
|
||||
|
||||
```
|
||||
sudo systemctl daemon-reload
|
||||
sudo systemctl enable --now mis-control-tower-reminders.timer
|
||||
```
|
||||
|
||||
## Downtime Reason Backfill
|
||||
|
||||
Control-Tower now preserves manual downtime reasons from `downtime-acknowledged` events when later default stop events (`PENDIENTE` / `UNCLASSIFIED`) arrive for the same incident.
|
||||
|
||||
If historical rows were already overwritten, run the one-time backfill:
|
||||
|
||||
1) Dry run (default lookback: 30 days):
|
||||
|
||||
```bash
|
||||
npm run backfill:downtime-reasons -- --dry-run --since 30d
|
||||
```
|
||||
|
||||
2) Apply updates:
|
||||
|
||||
```bash
|
||||
npm run backfill:downtime-reasons -- --since 30d
|
||||
```
|
||||
|
||||
Optional filters:
|
||||
|
||||
```bash
|
||||
npm run backfill:downtime-reasons -- --dry-run --since 14d --org-id <orgId> --machine-id <machineId>
|
||||
```
|
||||
|
||||
Quick verification query (shows recent incidents with reason + source):
|
||||
|
||||
```bash
|
||||
node -e 'const {PrismaClient}=require("@prisma/client");const p=new PrismaClient();(async()=>{const rows=await p.reasonEntry.findMany({where:{kind:"downtime"},orderBy:{capturedAt:"desc"},take:30,select:{id:true,orgId:true,machineId:true,episodeId:true,reasonCode:true,reasonLabel:true,capturedAt:true,meta:true}});console.log(JSON.stringify(rows,(_,v)=>typeof v==="bigint"?v.toString():v,2));})().finally(()=>p.$disconnect());'
|
||||
```
|
||||
|
||||
## Production build and deploy
|
||||
|
||||
**Dev uses Turbopack, production build uses Webpack.** Next.js 16 defaults to Turbopack for both, but Turbopack production builds have known issues. This project uses:
|
||||
|
||||
- `npm run dev` → `next dev --turbopack` (fast dev)
|
||||
- `npm run build` → `next build --webpack` (stable production build)
|
||||
|
||||
**When deploying** (e.g. for `https://mis.maliountech.com.mx`):
|
||||
|
||||
1. **Build:** Run `npm run build` (Webpack).
|
||||
2. **Start:** Run `npm run start` (or your process manager) to serve the built app.
|
||||
3. If you previously built with Turbopack, run `rm -rf .next` then `npm run build` for a clean Webpack build.
|
||||
4. Hard-refresh the browser (or clear site data) after redeploying so clients don’t load old Turbopack chunks.
|
||||
|
||||
## Logging and debugging
|
||||
|
||||
See **[LOGGING.md](./LOGGING.md)** for where errors are logged (log file, process stdout, optional `/api/debug/logs`), how to tail them, and how to debug "Internal Server Error".
|
||||
|
||||
|
||||
To learn more about Next.js, take a look at the following resources:
|
||||
|
||||
|
||||
161
Reliability.md
Normal file
161
Reliability.md
Normal file
@@ -0,0 +1,161 @@
|
||||
Data Reliability — Handoff Prompt
|
||||
Problem
|
||||
Same machine shows different numbers in 3 places:
|
||||
|
||||
Home UI (Node-RED): goodParts=353, OEE=77.9%
|
||||
Recap: goodParts=185, OEE=56%
|
||||
Machine detail: OEE=4.3%, "Sin datos" in 1h timeline
|
||||
Root cause: each view queries a different table with different logic. No single source of truth.
|
||||
|
||||
Rule: pick one source per metric, reuse across views
|
||||
Metric Authoritative source Why
|
||||
goodParts, scrapParts (per WO) MachineWorkOrder.good_parts / scrap_parts Node-RED writes this via UPDATE work_orders SET .... It's what Home UI shows.
|
||||
cycleCount MachineWorkOrder.cycle_count Same reason.
|
||||
oee / availability / performance / quality time-weighted avg of MachineKpiSnapshot rows in window Snapshots are minute-by-minute; Node-RED already sends them. Don't recompute.
|
||||
Stops (count, duration) MachineEvent filtered by eventType IN (microstop, macrostop, mold-change) + data->>'status' != 'active' + !is_update && !is_auto_ack Deduped at source.
|
||||
Timeline segments UNION of: MachineWorkOrder status spans, MachineEvent (mold-change/micro/macro), filled with idle Only way to get continuous coverage.
|
||||
Backend changes
|
||||
/api/recap/[machineId]/route.ts and /api/recap/summary/route.ts
|
||||
goodParts aggregation — stop summing MachineCycle.good_delta. Instead:
|
||||
|
||||
// For window [start, end], sum good_parts from WOs that had activity in window
|
||||
const wos = await prisma.machineWorkOrder.findMany({
|
||||
where: { machineId, orgId, updatedAt: { gte: start } },
|
||||
select: { workOrderId: true, sku: true, good_parts: true, scrap_parts: true, target_qty: true, status: true, updatedAt: true }
|
||||
});
|
||||
const goodParts = wos.reduce((s, w) => s + (w.good_parts ?? 0), 0);
|
||||
const scrapParts = wos.reduce((s, w) => s + (w.scrap_parts ?? 0), 0);
|
||||
Optionally scope to WOs that were RUNNING during the window; but for 24h window this rarely matters.
|
||||
|
||||
OEE aggregation — time-weighted average:
|
||||
|
||||
const snaps = await prisma.machineKpiSnapshot.findMany({
|
||||
where: { machineId, orgId, ts: { gte: start, lte: end } },
|
||||
orderBy: { ts: 'asc' },
|
||||
select: { ts: true, oee: true, availability: true, performance: true, quality: true }
|
||||
});
|
||||
|
||||
function weightedAvg(field: 'oee' | 'availability' | 'performance' | 'quality') {
|
||||
if (snaps.length === 0) return null;
|
||||
let totalMs = 0, sum = 0;
|
||||
for (let i = 0; i < snaps.length; i++) {
|
||||
const nextTs = (snaps[i+1]?.ts ?? end).getTime();
|
||||
const dt = Math.max(0, nextTs - snaps[i].ts.getTime());
|
||||
sum += (snaps[i][field] ?? 0) * dt;
|
||||
totalMs += dt;
|
||||
}
|
||||
return totalMs > 0 ? sum / totalMs : null;
|
||||
}
|
||||
Return null (not 0, not 100) when no snapshots. Frontend renders — for null.
|
||||
|
||||
Stops aggregation — filter properly:
|
||||
|
||||
const stops = await prisma.machineEvent.findMany({
|
||||
where: {
|
||||
machineId, orgId,
|
||||
ts: { gte: start, lte: end },
|
||||
eventType: { in: ['microstop','macrostop'] },
|
||||
}
|
||||
});
|
||||
const real = stops.filter(e => {
|
||||
const d = e.data as any;
|
||||
return d?.status !== 'active' && !d?.is_auto_ack && !d?.is_update;
|
||||
});
|
||||
const stopsCount = real.length;
|
||||
const stopsMin = real.reduce((s, e) => s + (((e.data as any)?.stoppage_duration_seconds ?? 0) / 60), 0);
|
||||
/api/recap/[machineId]/timeline — MUST include mold-change
|
||||
Segment builder in priority order (higher priority wins when overlapping):
|
||||
|
||||
mold-change segments (pair active→resolved by incidentKey, duration from data.duration_sec)
|
||||
macrostop segments (same pairing)
|
||||
microstop segments (merge runs <60s apart into cluster)
|
||||
production segments — derived from WO status history, use MachineWorkOrder.status transitions + MachineCycle density (no cycles for >threshold → not production)
|
||||
idle gap-fill
|
||||
Never return empty array if any event exists in window. "Sin datos" only if literally zero rows in both MachineEvent and MachineCycle for the window.
|
||||
|
||||
Merge rules:
|
||||
|
||||
Same-type consecutive segments separated by <30s → merge
|
||||
Any segment <30s duration, absorb into neighbor
|
||||
Return format:
|
||||
|
||||
{
|
||||
range: { start, end },
|
||||
segments: Array<{
|
||||
type: 'production' | 'mold-change' | 'macrostop' | 'microstop' | 'idle',
|
||||
startMs, endMs, durationSec,
|
||||
label?: string, // WO id, mold ids, reason
|
||||
workOrderId?: string,
|
||||
sku?: string,
|
||||
reasonLabel?: string,
|
||||
}>,
|
||||
hasData: boolean // false only if literally empty
|
||||
}
|
||||
Frontend changes
|
||||
RecapMachineCard.tsx / Machine detail page / OverviewTimeline.tsx
|
||||
All three MUST consume the same endpoint and render from the same shape. Timeline in Machine detail page (app/(app)/machines/[machineId]/MachineDetailClient.tsx) currently queries its own source — refactor to call /api/recap/[machineId]/timeline with range=1h for the small timeline, range=24h for the recap.
|
||||
|
||||
"Sin datos" fallback: render only when hasData === false. If timeline has any mold-change or stop segment, render the bar.
|
||||
|
||||
Null handling for OEE
|
||||
If backend returns oee: null:
|
||||
|
||||
<div className="text-2xl font-semibold text-zinc-400">—</div>
|
||||
<div className="text-xs text-zinc-500">Sin datos de KPI</div>
|
||||
Not 0.0%. Not 100%. Dash. User knows "no data" vs. "bad performance".
|
||||
|
||||
Reconciling with Home UI live numbers
|
||||
Home UI reads live state.activeWorkOrder.goodParts from Node-RED. Recap reads MachineWorkOrder.good_parts from CT DB.
|
||||
|
||||
These WILL briefly differ because of outbox lag (cycle POST → DB insert → next recap query). Mitigate:
|
||||
|
||||
Cache recap endpoints 30-60s max (shorter than current 2-5 min).
|
||||
On recap header, show "Actualizado hace Xs" timestamp so user sees freshness.
|
||||
Pi cycle outbox should already be fast (<5s normally). If backlog is persistent, flag it in the UI with a "CT desincronizado" warning (compare MachineHeartbeat.ts to now; if >5min, show amber status).
|
||||
Sanity check queries for debugging
|
||||
Run on CT to audit one machine:
|
||||
|
||||
-- Authoritative WO state (matches Home UI)
|
||||
SELECT work_order_id, sku, good_parts, scrap_parts, cycle_count, status, "updatedAt"
|
||||
FROM "MachineWorkOrder"
|
||||
WHERE "machineId" = '<uuid>'
|
||||
ORDER BY "updatedAt" DESC LIMIT 5;
|
||||
|
||||
-- What KPI snapshots exist in last 24h
|
||||
SELECT ts, oee, availability, performance, quality
|
||||
FROM "MachineKpiSnapshot"
|
||||
WHERE "machineId" = '<uuid>' AND ts > NOW() - INTERVAL '24 hours'
|
||||
ORDER BY ts DESC LIMIT 20;
|
||||
|
||||
-- Events breakdown
|
||||
SELECT "eventType",
|
||||
COUNT(*) AS total,
|
||||
COUNT(*) FILTER (WHERE data->>'status' = 'active') AS active,
|
||||
COUNT(*) FILTER (WHERE (data->>'is_update')::bool) AS updates,
|
||||
COUNT(*) FILTER (WHERE (data->>'is_auto_ack')::bool) AS auto_acks
|
||||
FROM "MachineEvent"
|
||||
WHERE "machineId" = '<uuid>' AND ts > NOW() - INTERVAL '24 hours'
|
||||
GROUP BY "eventType";
|
||||
If MachineWorkOrder.good_parts says 353 and Home UI says 353 but recap says 185 → recap is still using old aggregation.
|
||||
If MachineKpiSnapshot count is 0 for last hour → Node-RED isn't sending snapshots (check outbox).
|
||||
|
||||
Checklist
|
||||
not done
|
||||
Recap endpoints use MachineWorkOrder.good_parts not cycle sum
|
||||
not done
|
||||
OEE uses time-weighted MachineKpiSnapshot avg, returns null when empty
|
||||
not done
|
||||
Timeline includes mold-change events
|
||||
not done
|
||||
Machine detail timeline uses same endpoint as recap
|
||||
not done
|
||||
"Sin datos" fallback only when hasData: false
|
||||
not done
|
||||
Null OEE renders as —, not 0 or 100
|
||||
not done
|
||||
Same endpoint feeds recap grid mini timeline + detail full timeline
|
||||
not done
|
||||
Cache TTL reduced to 30-60s
|
||||
not done
|
||||
Staleness indicator visible in UI header
|
||||
Non-goals: no schema changes, no Node-RED changes, no new ingest endpoints
|
||||
499
app/(app)/alerts/AlertsClient.tsx
Normal file
499
app/(app)/alerts/AlertsClient.tsx
Normal file
@@ -0,0 +1,499 @@
|
||||
"use client";
|
||||
|
||||
import { useEffect, useMemo, useRef, useState } from "react";
|
||||
import { useI18n } from "@/lib/i18n/useI18n";
|
||||
|
||||
type MachineRow = {
|
||||
id: string;
|
||||
name: string;
|
||||
location?: string | null;
|
||||
};
|
||||
|
||||
type ShiftRow = {
|
||||
name: string;
|
||||
enabled: boolean;
|
||||
};
|
||||
|
||||
type AlertEvent = {
|
||||
id: string;
|
||||
ts: string;
|
||||
eventType: string;
|
||||
severity: string;
|
||||
title: string;
|
||||
description?: string | null;
|
||||
machineId: string;
|
||||
machineName?: string | null;
|
||||
location?: string | null;
|
||||
workOrderId?: string | null;
|
||||
sku?: string | null;
|
||||
durationSec?: number | null;
|
||||
status?: string | null;
|
||||
shift?: string | null;
|
||||
alertId?: string | null;
|
||||
isUpdate?: boolean;
|
||||
isAutoAck?: boolean;
|
||||
};
|
||||
|
||||
const RANGE_OPTIONS = [
|
||||
{ value: "24h", labelKey: "alerts.inbox.range.24h" },
|
||||
{ value: "7d", labelKey: "alerts.inbox.range.7d" },
|
||||
{ value: "30d", labelKey: "alerts.inbox.range.30d" },
|
||||
{ value: "custom", labelKey: "alerts.inbox.range.custom" },
|
||||
] as const;
|
||||
|
||||
function formatDuration(seconds: number | null | undefined, t: (key: string) => string) {
|
||||
if (seconds == null || !Number.isFinite(seconds)) return t("alerts.inbox.duration.na");
|
||||
if (seconds < 60) return `${Math.round(seconds)}${t("alerts.inbox.duration.sec")}`;
|
||||
if (seconds < 3600) return `${Math.round(seconds / 60)}${t("alerts.inbox.duration.min")}`;
|
||||
return `${(seconds / 3600).toFixed(1)}${t("alerts.inbox.duration.hr")}`;
|
||||
}
|
||||
|
||||
function normalizeLabel(value?: string | null) {
|
||||
if (!value) return "";
|
||||
return String(value).trim();
|
||||
}
|
||||
|
||||
export default function AlertsClient({
|
||||
initialMachines = [],
|
||||
initialShifts = [],
|
||||
initialEvents = [],
|
||||
}: {
|
||||
initialMachines?: MachineRow[];
|
||||
initialShifts?: ShiftRow[];
|
||||
initialEvents?: AlertEvent[];
|
||||
}) {
|
||||
const { t, locale } = useI18n();
|
||||
const [events, setEvents] = useState<AlertEvent[]>(() => initialEvents);
|
||||
const [machines, setMachines] = useState<MachineRow[]>(() => initialMachines);
|
||||
const [shifts, setShifts] = useState<ShiftRow[]>(() => initialShifts);
|
||||
const [loading, setLoading] = useState(() => initialMachines.length === 0 || initialShifts.length === 0);
|
||||
const [loadingEvents, setLoadingEvents] = useState(false);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
const [range, setRange] = useState<string>("24h");
|
||||
const [start, setStart] = useState<string>("");
|
||||
const [end, setEnd] = useState<string>("");
|
||||
const [machineId, setMachineId] = useState<string>("");
|
||||
const [location, setLocation] = useState<string>("");
|
||||
const [shift, setShift] = useState<string>("");
|
||||
const [eventType, setEventType] = useState<string>("");
|
||||
const [severity, setSeverity] = useState<string>("");
|
||||
const [status, setStatus] = useState<string>("");
|
||||
const [includeUpdates, setIncludeUpdates] = useState(false);
|
||||
const [search, setSearch] = useState("");
|
||||
const skipInitialEventsRef = useRef(true);
|
||||
|
||||
const locations = useMemo(() => {
|
||||
const seen = new Set<string>();
|
||||
for (const machine of machines) {
|
||||
if (!machine.location) continue;
|
||||
seen.add(machine.location);
|
||||
}
|
||||
return Array.from(seen).sort();
|
||||
}, [machines]);
|
||||
|
||||
useEffect(() => {
|
||||
if (initialMachines.length && initialShifts.length) {
|
||||
setLoading(false);
|
||||
return;
|
||||
}
|
||||
let alive = true;
|
||||
|
||||
async function loadFilters() {
|
||||
setLoading(true);
|
||||
try {
|
||||
const [machinesRes, settingsRes] = await Promise.all([
|
||||
fetch("/api/machines", { cache: "no-store" }),
|
||||
fetch("/api/settings", { cache: "no-store" }),
|
||||
]);
|
||||
const machinesJson = await machinesRes.json().catch(() => ({}));
|
||||
const settingsJson = await settingsRes.json().catch(() => ({}));
|
||||
if (!alive) return;
|
||||
setMachines(machinesJson.machines ?? []);
|
||||
const shiftRows = settingsJson?.settings?.shiftSchedule?.shifts ?? [];
|
||||
setShifts(
|
||||
Array.isArray(shiftRows)
|
||||
? shiftRows
|
||||
.map((row: unknown) => {
|
||||
const data = row && typeof row === "object" ? (row as Record<string, unknown>) : {};
|
||||
const name = typeof data.name === "string" ? data.name : "";
|
||||
const enabled = data.enabled !== false;
|
||||
return { name, enabled };
|
||||
})
|
||||
.filter((row) => row.name)
|
||||
: []
|
||||
);
|
||||
} catch {
|
||||
if (!alive) return;
|
||||
} finally {
|
||||
if (alive) setLoading(false);
|
||||
}
|
||||
}
|
||||
|
||||
loadFilters();
|
||||
return () => {
|
||||
alive = false;
|
||||
};
|
||||
}, [initialMachines, initialShifts]);
|
||||
|
||||
useEffect(() => {
|
||||
let alive = true;
|
||||
const controller = new AbortController();
|
||||
|
||||
async function loadEvents() {
|
||||
const isDefault =
|
||||
range === "24h" &&
|
||||
!start &&
|
||||
!end &&
|
||||
!machineId &&
|
||||
!location &&
|
||||
!shift &&
|
||||
!eventType &&
|
||||
!severity &&
|
||||
!status &&
|
||||
!includeUpdates;
|
||||
if (skipInitialEventsRef.current) {
|
||||
skipInitialEventsRef.current = false;
|
||||
if (initialEvents.length && isDefault) return;
|
||||
}
|
||||
|
||||
setLoadingEvents(true);
|
||||
setError(null);
|
||||
const params = new URLSearchParams();
|
||||
params.set("range", range);
|
||||
if (range === "custom") {
|
||||
if (start) params.set("start", start);
|
||||
if (end) params.set("end", end);
|
||||
}
|
||||
if (machineId) params.set("machineId", machineId);
|
||||
if (location) params.set("location", location);
|
||||
if (shift) params.set("shift", shift);
|
||||
if (eventType) params.set("eventType", eventType);
|
||||
if (severity) params.set("severity", severity);
|
||||
if (status) params.set("status", status);
|
||||
if (includeUpdates) params.set("includeUpdates", "1");
|
||||
params.set("limit", "250");
|
||||
|
||||
try {
|
||||
const res = await fetch(`/api/alerts/inbox?${params.toString()}`, {
|
||||
cache: "no-store",
|
||||
signal: controller.signal,
|
||||
});
|
||||
const json = await res.json().catch(() => ({}));
|
||||
if (!alive) return;
|
||||
if (!res.ok || !json?.ok) {
|
||||
setError(json?.error || t("alerts.inbox.error"));
|
||||
setEvents([]);
|
||||
} else {
|
||||
setEvents(json.events ?? []);
|
||||
}
|
||||
} catch {
|
||||
if (alive) {
|
||||
setError(t("alerts.inbox.error"));
|
||||
setEvents([]);
|
||||
}
|
||||
} finally {
|
||||
if (alive) setLoadingEvents(false);
|
||||
}
|
||||
}
|
||||
|
||||
loadEvents();
|
||||
return () => {
|
||||
alive = false;
|
||||
controller.abort();
|
||||
};
|
||||
}, [
|
||||
range,
|
||||
start,
|
||||
end,
|
||||
machineId,
|
||||
location,
|
||||
shift,
|
||||
eventType,
|
||||
severity,
|
||||
status,
|
||||
includeUpdates,
|
||||
t,
|
||||
initialEvents.length,
|
||||
]);
|
||||
|
||||
const eventTypes = useMemo(() => {
|
||||
const seen = new Set<string>();
|
||||
for (const ev of events) {
|
||||
if (ev.eventType) seen.add(ev.eventType);
|
||||
}
|
||||
return Array.from(seen).sort();
|
||||
}, [events]);
|
||||
|
||||
const severities = useMemo(() => {
|
||||
const seen = new Set<string>();
|
||||
for (const ev of events) {
|
||||
if (ev.severity) seen.add(ev.severity);
|
||||
}
|
||||
return Array.from(seen).sort();
|
||||
}, [events]);
|
||||
|
||||
const statuses = useMemo(() => {
|
||||
const seen = new Set<string>();
|
||||
for (const ev of events) {
|
||||
if (ev.status) seen.add(ev.status);
|
||||
}
|
||||
return Array.from(seen).sort();
|
||||
}, [events]);
|
||||
|
||||
const filteredEvents = useMemo(() => {
|
||||
if (!search.trim()) return events;
|
||||
const needle = search.trim().toLowerCase();
|
||||
return events.filter((ev) => {
|
||||
return (
|
||||
normalizeLabel(ev.title).toLowerCase().includes(needle) ||
|
||||
normalizeLabel(ev.description).toLowerCase().includes(needle) ||
|
||||
normalizeLabel(ev.machineName).toLowerCase().includes(needle) ||
|
||||
normalizeLabel(ev.location).toLowerCase().includes(needle) ||
|
||||
normalizeLabel(ev.eventType).toLowerCase().includes(needle)
|
||||
);
|
||||
});
|
||||
}, [events, search]);
|
||||
|
||||
function formatEventTypeLabel(value: string) {
|
||||
const key = `alerts.event.${value}`;
|
||||
const label = t(key);
|
||||
return label === key ? value : label;
|
||||
}
|
||||
|
||||
function formatStatusLabel(value?: string | null) {
|
||||
if (!value) return t("alerts.inbox.table.unknown");
|
||||
const key = `alerts.inbox.status.${value}`;
|
||||
const label = t(key);
|
||||
return label === key ? value : label;
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="mx-auto flex w-full max-w-7xl flex-col gap-6 px-4 py-6 sm:px-6 sm:py-8">
|
||||
<div>
|
||||
<h1 className="text-2xl font-semibold text-white">{t("alerts.title")}</h1>
|
||||
<p className="mt-2 text-sm text-zinc-400">{t("alerts.subtitle")}</p>
|
||||
</div>
|
||||
|
||||
<div className="rounded-2xl border border-white/10 bg-white/5 p-5">
|
||||
<div className="mb-4 flex flex-col gap-2 sm:flex-row sm:items-center sm:justify-between">
|
||||
<div className="text-sm font-semibold text-white">{t("alerts.inbox.filters.title")}</div>
|
||||
{loading && <div className="text-xs text-zinc-500">{t("alerts.inbox.loadingFilters")}</div>}
|
||||
</div>
|
||||
<div className="grid grid-cols-1 gap-3 md:grid-cols-2 xl:grid-cols-4">
|
||||
<label className="text-xs text-zinc-400">
|
||||
{t("alerts.inbox.filters.range")}
|
||||
<select
|
||||
className="mt-2 w-full rounded-lg border border-white/10 bg-black/30 px-3 py-2 text-sm text-white"
|
||||
value={range}
|
||||
onChange={(event) => setRange(event.target.value)}
|
||||
>
|
||||
{RANGE_OPTIONS.map((option) => (
|
||||
<option key={option.value} value={option.value}>
|
||||
{t(option.labelKey)}
|
||||
</option>
|
||||
))}
|
||||
</select>
|
||||
</label>
|
||||
{range === "custom" && (
|
||||
<>
|
||||
<label className="text-xs text-zinc-400">
|
||||
{t("alerts.inbox.filters.start")}
|
||||
<input
|
||||
type="date"
|
||||
className="mt-2 w-full rounded-lg border border-white/10 bg-black/30 px-3 py-2 text-sm text-white"
|
||||
value={start}
|
||||
onChange={(event) => setStart(event.target.value)}
|
||||
/>
|
||||
</label>
|
||||
<label className="text-xs text-zinc-400">
|
||||
{t("alerts.inbox.filters.end")}
|
||||
<input
|
||||
type="date"
|
||||
className="mt-2 w-full rounded-lg border border-white/10 bg-black/30 px-3 py-2 text-sm text-white"
|
||||
value={end}
|
||||
onChange={(event) => setEnd(event.target.value)}
|
||||
/>
|
||||
</label>
|
||||
</>
|
||||
)}
|
||||
<label className="text-xs text-zinc-400">
|
||||
{t("alerts.inbox.filters.machine")}
|
||||
<select
|
||||
className="mt-2 w-full rounded-lg border border-white/10 bg-black/30 px-3 py-2 text-sm text-white"
|
||||
value={machineId}
|
||||
onChange={(event) => setMachineId(event.target.value)}
|
||||
>
|
||||
<option value="">{t("alerts.inbox.filters.allMachines")}</option>
|
||||
{machines.map((machine) => (
|
||||
<option key={machine.id} value={machine.id}>
|
||||
{machine.name}
|
||||
</option>
|
||||
))}
|
||||
</select>
|
||||
</label>
|
||||
<label className="text-xs text-zinc-400">
|
||||
{t("alerts.inbox.filters.site")}
|
||||
<select
|
||||
className="mt-2 w-full rounded-lg border border-white/10 bg-black/30 px-3 py-2 text-sm text-white"
|
||||
value={location}
|
||||
onChange={(event) => setLocation(event.target.value)}
|
||||
>
|
||||
<option value="">{t("alerts.inbox.filters.allSites")}</option>
|
||||
{locations.map((loc) => (
|
||||
<option key={loc} value={loc}>
|
||||
{loc}
|
||||
</option>
|
||||
))}
|
||||
</select>
|
||||
</label>
|
||||
<label className="text-xs text-zinc-400">
|
||||
{t("alerts.inbox.filters.shift")}
|
||||
<select
|
||||
className="mt-2 w-full rounded-lg border border-white/10 bg-black/30 px-3 py-2 text-sm text-white"
|
||||
value={shift}
|
||||
onChange={(event) => setShift(event.target.value)}
|
||||
>
|
||||
<option value="">{t("alerts.inbox.filters.allShifts")}</option>
|
||||
{shifts
|
||||
.filter((row) => row.enabled)
|
||||
.map((row) => (
|
||||
<option key={row.name} value={row.name}>
|
||||
{row.name}
|
||||
</option>
|
||||
))}
|
||||
</select>
|
||||
</label>
|
||||
<label className="text-xs text-zinc-400">
|
||||
{t("alerts.inbox.filters.type")}
|
||||
<select
|
||||
className="mt-2 w-full rounded-lg border border-white/10 bg-black/30 px-3 py-2 text-sm text-white"
|
||||
value={eventType}
|
||||
onChange={(event) => setEventType(event.target.value)}
|
||||
>
|
||||
<option value="">{t("alerts.inbox.filters.allTypes")}</option>
|
||||
{eventTypes.map((value) => (
|
||||
<option key={value} value={value}>
|
||||
{formatEventTypeLabel(value)}
|
||||
</option>
|
||||
))}
|
||||
</select>
|
||||
</label>
|
||||
<label className="text-xs text-zinc-400">
|
||||
{t("alerts.inbox.filters.severity")}
|
||||
<select
|
||||
className="mt-2 w-full rounded-lg border border-white/10 bg-black/30 px-3 py-2 text-sm text-white"
|
||||
value={severity}
|
||||
onChange={(event) => setSeverity(event.target.value)}
|
||||
>
|
||||
<option value="">{t("alerts.inbox.filters.allSeverities")}</option>
|
||||
{severities.map((value) => (
|
||||
<option key={value} value={value}>
|
||||
{value}
|
||||
</option>
|
||||
))}
|
||||
</select>
|
||||
</label>
|
||||
<label className="text-xs text-zinc-400">
|
||||
{t("alerts.inbox.filters.status")}
|
||||
<select
|
||||
className="mt-2 w-full rounded-lg border border-white/10 bg-black/30 px-3 py-2 text-sm text-white"
|
||||
value={status}
|
||||
onChange={(event) => setStatus(event.target.value)}
|
||||
>
|
||||
<option value="">{t("alerts.inbox.filters.allStatuses")}</option>
|
||||
{statuses.map((value) => (
|
||||
<option key={value} value={value}>
|
||||
{formatStatusLabel(value)}
|
||||
</option>
|
||||
))}
|
||||
</select>
|
||||
</label>
|
||||
<label className="text-xs text-zinc-400">
|
||||
{t("alerts.inbox.filters.search")}
|
||||
<input
|
||||
className="mt-2 w-full rounded-lg border border-white/10 bg-black/30 px-3 py-2 text-sm text-white"
|
||||
value={search}
|
||||
onChange={(event) => setSearch(event.target.value)}
|
||||
placeholder={t("alerts.inbox.filters.searchPlaceholder")}
|
||||
/>
|
||||
</label>
|
||||
<label className="flex items-center gap-2 text-xs text-zinc-400">
|
||||
<input
|
||||
type="checkbox"
|
||||
checked={includeUpdates}
|
||||
onChange={(event) => setIncludeUpdates(event.target.checked)}
|
||||
className="h-4 w-4 rounded border border-white/20 bg-black/20"
|
||||
/>
|
||||
{t("alerts.inbox.filters.includeUpdates")}
|
||||
</label>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="rounded-2xl border border-white/10 bg-white/5 p-5">
|
||||
<div className="mb-4 flex flex-col gap-2 sm:flex-row sm:items-center sm:justify-between">
|
||||
<div className="text-sm font-semibold text-white">{t("alerts.inbox.title")}</div>
|
||||
{loadingEvents && <div className="text-xs text-zinc-500">{t("alerts.inbox.loading")}</div>}
|
||||
</div>
|
||||
|
||||
{error && (
|
||||
<div className="mb-4 rounded-xl border border-red-500/20 bg-red-500/10 p-3 text-sm text-red-200">
|
||||
{error}
|
||||
</div>
|
||||
)}
|
||||
|
||||
{!loadingEvents && !filteredEvents.length && (
|
||||
<div className="text-sm text-zinc-400">{t("alerts.inbox.empty")}</div>
|
||||
)}
|
||||
|
||||
{!!filteredEvents.length && (
|
||||
<div className="overflow-x-auto">
|
||||
<table className="w-full border-collapse text-sm text-zinc-300">
|
||||
<thead>
|
||||
<tr className="text-xs uppercase text-zinc-500">
|
||||
<th className="border-b border-white/10 px-3 py-2 text-left">{t("alerts.inbox.table.time")}</th>
|
||||
<th className="border-b border-white/10 px-3 py-2 text-left">{t("alerts.inbox.table.machine")}</th>
|
||||
<th className="border-b border-white/10 px-3 py-2 text-left">{t("alerts.inbox.table.site")}</th>
|
||||
<th className="border-b border-white/10 px-3 py-2 text-left">{t("alerts.inbox.table.shift")}</th>
|
||||
<th className="border-b border-white/10 px-3 py-2 text-left">{t("alerts.inbox.table.type")}</th>
|
||||
<th className="border-b border-white/10 px-3 py-2 text-left">{t("alerts.inbox.table.severity")}</th>
|
||||
<th className="border-b border-white/10 px-3 py-2 text-left">{t("alerts.inbox.table.status")}</th>
|
||||
<th className="border-b border-white/10 px-3 py-2 text-left">{t("alerts.inbox.table.duration")}</th>
|
||||
<th className="border-b border-white/10 px-3 py-2 text-left">{t("alerts.inbox.table.title")}</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{filteredEvents.map((ev) => (
|
||||
<tr key={ev.id} className="border-b border-white/5">
|
||||
<td className="px-3 py-3 text-xs text-zinc-400">
|
||||
{new Date(ev.ts).toLocaleString(locale)}
|
||||
</td>
|
||||
<td className="px-3 py-3">{ev.machineName || t("alerts.inbox.table.unknown")}</td>
|
||||
<td className="px-3 py-3">{ev.location || t("alerts.inbox.table.unknown")}</td>
|
||||
<td className="px-3 py-3">{ev.shift || t("alerts.inbox.table.unknown")}</td>
|
||||
<td className="px-3 py-3">{formatEventTypeLabel(ev.eventType)}</td>
|
||||
<td className="px-3 py-3">{ev.severity || t("alerts.inbox.table.unknown")}</td>
|
||||
<td className="px-3 py-3">{formatStatusLabel(ev.status)}</td>
|
||||
<td className="px-3 py-3">{formatDuration(ev.durationSec, t)}</td>
|
||||
<td className="px-3 py-3">
|
||||
<div className="text-sm text-white">{ev.title}</div>
|
||||
{ev.description && (
|
||||
<div className="mt-1 text-xs text-zinc-400">{ev.description}</div>
|
||||
)}
|
||||
{(ev.workOrderId || ev.sku) && (
|
||||
<div className="mt-1 text-[11px] text-zinc-500">
|
||||
{ev.workOrderId ? `${t("alerts.inbox.meta.workOrder")}: ${ev.workOrderId}` : null}
|
||||
{ev.workOrderId && ev.sku ? " • " : null}
|
||||
{ev.sku ? `${t("alerts.inbox.meta.sku")}: ${ev.sku}` : null}
|
||||
</div>
|
||||
)}
|
||||
</td>
|
||||
</tr>
|
||||
))}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
46
app/(app)/alerts/page.tsx
Normal file
46
app/(app)/alerts/page.tsx
Normal file
@@ -0,0 +1,46 @@
|
||||
import { redirect } from "next/navigation";
|
||||
import { prisma } from "@/lib/prisma";
|
||||
import { requireSession } from "@/lib/auth/requireSession";
|
||||
import { getAlertsInboxData } from "@/lib/alerts/getAlertsInboxData";
|
||||
import AlertsClient from "./AlertsClient";
|
||||
|
||||
export default async function AlertsPage() {
|
||||
const session = await requireSession();
|
||||
if (!session) redirect("/login?next=/alerts");
|
||||
|
||||
const [machines, shiftRows, inbox] = await Promise.all([
|
||||
prisma.machine.findMany({
|
||||
where: { orgId: session.orgId },
|
||||
orderBy: { createdAt: "desc" },
|
||||
select: { id: true, name: true, location: true },
|
||||
}),
|
||||
prisma.orgShift.findMany({
|
||||
where: { orgId: session.orgId },
|
||||
orderBy: { sortOrder: "asc" },
|
||||
select: { name: true, enabled: true },
|
||||
}),
|
||||
getAlertsInboxData({
|
||||
orgId: session.orgId,
|
||||
range: "24h",
|
||||
limit: 250,
|
||||
}),
|
||||
]);
|
||||
|
||||
const initialEvents = inbox.events.map((event) => ({
|
||||
...event,
|
||||
ts: event.ts ? event.ts.toISOString() : "",
|
||||
}));
|
||||
|
||||
const initialShifts = shiftRows.map((shift) => ({
|
||||
name: shift.name,
|
||||
enabled: shift.enabled !== false,
|
||||
}));
|
||||
|
||||
return (
|
||||
<AlertsClient
|
||||
initialMachines={machines}
|
||||
initialShifts={initialShifts}
|
||||
initialEvents={initialEvents}
|
||||
/>
|
||||
);
|
||||
}
|
||||
29
app/(app)/downtime/layout.tsx
Normal file
29
app/(app)/downtime/layout.tsx
Normal file
@@ -0,0 +1,29 @@
|
||||
import { redirect } from "next/navigation";
|
||||
import { prisma } from "@/lib/prisma";
|
||||
import { requireSession } from "@/lib/auth/requireSession";
|
||||
|
||||
function isPlainObject(value: unknown): value is Record<string, unknown> {
|
||||
return !!value && typeof value === "object" && !Array.isArray(value);
|
||||
}
|
||||
|
||||
function getScreenlessMode(defaultsJson: unknown) {
|
||||
const defaults = isPlainObject(defaultsJson) ? defaultsJson : {};
|
||||
const modules = isPlainObject(defaults.modules) ? defaults.modules : {};
|
||||
return modules.screenlessMode === true;
|
||||
}
|
||||
|
||||
export default async function DowntimeLayout({ children }: { children: React.ReactNode }) {
|
||||
const session = await requireSession();
|
||||
if (!session) redirect("/login?next=/downtime");
|
||||
|
||||
const settings = await prisma.orgSettings.findUnique({
|
||||
where: { orgId: session.orgId },
|
||||
select: { defaultsJson: true },
|
||||
});
|
||||
|
||||
if (getScreenlessMode(settings?.defaultsJson)) {
|
||||
redirect("/overview");
|
||||
}
|
||||
|
||||
return <>{children}</>;
|
||||
}
|
||||
5
app/(app)/downtime/page.tsx
Normal file
5
app/(app)/downtime/page.tsx
Normal file
@@ -0,0 +1,5 @@
|
||||
import DowntimePageClient from "@/components/downtime/DowntimePageClient";
|
||||
|
||||
export default function DowntimePage() {
|
||||
return <DowntimePageClient />;
|
||||
}
|
||||
31
app/(app)/error.tsx
Normal file
31
app/(app)/error.tsx
Normal file
@@ -0,0 +1,31 @@
|
||||
"use client";
|
||||
|
||||
import { useEffect } from "react";
|
||||
|
||||
export default function AppError({
|
||||
error,
|
||||
reset,
|
||||
}: {
|
||||
error: Error & { digest?: string };
|
||||
reset: () => void;
|
||||
}) {
|
||||
useEffect(() => {
|
||||
console.error("[App Error]", error);
|
||||
}, [error]);
|
||||
|
||||
return (
|
||||
<div className="flex min-h-[50vh] flex-col items-center justify-center gap-4 p-6">
|
||||
<h2 className="text-lg font-semibold text-white">Something went wrong</h2>
|
||||
<p className="max-w-md text-center text-sm text-zinc-400">
|
||||
An error occurred while loading this page. Please try again.
|
||||
</p>
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => reset()}
|
||||
className="rounded-xl border border-white/10 bg-white/5 px-4 py-2 text-sm text-white hover:bg-white/10"
|
||||
>
|
||||
Try again
|
||||
</button>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
405
app/(app)/financial/FinancialClient.tsx
Normal file
405
app/(app)/financial/FinancialClient.tsx
Normal file
@@ -0,0 +1,405 @@
|
||||
"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 forceRefreshRef = useRef(false);
|
||||
const [refreshSeed, setRefreshSeed] = useState(0);
|
||||
|
||||
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);
|
||||
const forceRefresh = forceRefreshRef.current;
|
||||
if (forceRefresh) params.set("refresh", "1");
|
||||
|
||||
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);
|
||||
} finally {
|
||||
if (forceRefresh) forceRefreshRef.current = false;
|
||||
}
|
||||
}
|
||||
|
||||
loadImpact();
|
||||
return () => {
|
||||
alive = false;
|
||||
controller.abort();
|
||||
};
|
||||
}, [currencyFilter, initialImpact, locationFilter, machineFilter, range, refreshSeed, 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}`;
|
||||
const handleRefresh = () => {
|
||||
forceRefreshRef.current = true;
|
||||
setRefreshSeed((prev) => prev + 1);
|
||||
};
|
||||
|
||||
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">
|
||||
<button
|
||||
type="button"
|
||||
onClick={handleRefresh}
|
||||
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"
|
||||
>
|
||||
{t("financial.refresh")}
|
||||
</button>
|
||||
<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%" minHeight={200}>
|
||||
<AreaChart data={chartData}>
|
||||
<defs>
|
||||
<linearGradient id="slowFill" x1="0" y1="0" x2="0" y2="1">
|
||||
<stop offset="5%" stopColor="#facc15" stopOpacity={0.5} />
|
||||
<stop offset="95%" stopColor="#facc15" stopOpacity={0.05} />
|
||||
</linearGradient>
|
||||
<linearGradient id="microFill" x1="0" y1="0" x2="0" y2="1">
|
||||
<stop offset="5%" stopColor="#fb7185" stopOpacity={0.5} />
|
||||
<stop offset="95%" stopColor="#fb7185" stopOpacity={0.05} />
|
||||
</linearGradient>
|
||||
<linearGradient id="macroFill" x1="0" y1="0" x2="0" y2="1">
|
||||
<stop offset="5%" stopColor="#f97316" stopOpacity={0.5} />
|
||||
<stop offset="95%" stopColor="#f97316" stopOpacity={0.05} />
|
||||
</linearGradient>
|
||||
<linearGradient id="scrapFill" x1="0" y1="0" x2="0" y2="1">
|
||||
<stop offset="5%" stopColor="#38bdf8" stopOpacity={0.5} />
|
||||
<stop offset="95%" stopColor="#38bdf8" stopOpacity={0.05} />
|
||||
</linearGradient>
|
||||
</defs>
|
||||
<CartesianGrid strokeDasharray="3 3" stroke="var(--app-chart-grid)" />
|
||||
<XAxis dataKey="day" tick={{ fill: "var(--app-chart-tick)", fontSize: 10 }} />
|
||||
<YAxis tick={{ fill: "var(--app-chart-tick)", fontSize: 10 }} />
|
||||
<Tooltip
|
||||
contentStyle={{
|
||||
background: "var(--app-chart-tooltip-bg)",
|
||||
border: "1px solid var(--app-chart-tooltip-border)",
|
||||
}}
|
||||
labelStyle={{ color: "var(--app-chart-label)" }}
|
||||
/>
|
||||
<Area type="monotone" dataKey="slowCycle" stackId="1" stroke="#facc15" fill="url(#slowFill)" />
|
||||
<Area type="monotone" dataKey="microstop" stackId="1" stroke="#fb7185" fill="url(#microFill)" />
|
||||
<Area type="monotone" dataKey="macrostop" stackId="1" stroke="#f97316" fill="url(#macroFill)" />
|
||||
<Area type="monotone" dataKey="scrap" stackId="1" stroke="#38bdf8" fill="url(#scrapFill)" />
|
||||
</AreaChart>
|
||||
</ResponsiveContainer>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="rounded-2xl border border-white/10 bg-black/40 p-4 space-y-4">
|
||||
<h2 className="text-lg font-semibold text-white">{t("financial.filters.title")}</h2>
|
||||
<div className="space-y-3 text-sm text-zinc-300">
|
||||
<div>
|
||||
<label className="text-xs uppercase text-zinc-500">{t("financial.filters.machine")}</label>
|
||||
<select
|
||||
className="mt-2 w-full rounded-lg border border-white/10 bg-black/30 px-3 py-2"
|
||||
value={machineFilter}
|
||||
onChange={(event) => setMachineFilter(event.target.value)}
|
||||
>
|
||||
<option value="">{t("financial.filters.allMachines")}</option>
|
||||
{machines.map((machine) => (
|
||||
<option key={machine.id} value={machine.id}>
|
||||
{machine.name}
|
||||
</option>
|
||||
))}
|
||||
</select>
|
||||
</div>
|
||||
<div>
|
||||
<label className="text-xs uppercase text-zinc-500">{t("financial.filters.location")}</label>
|
||||
<select
|
||||
className="mt-2 w-full rounded-lg border border-white/10 bg-black/30 px-3 py-2"
|
||||
value={locationFilter}
|
||||
onChange={(event) => setLocationFilter(event.target.value)}
|
||||
>
|
||||
<option value="">{t("financial.filters.allLocations")}</option>
|
||||
{locations.map((loc) => (
|
||||
<option key={loc} value={loc}>
|
||||
{loc}
|
||||
</option>
|
||||
))}
|
||||
</select>
|
||||
</div>
|
||||
<div>
|
||||
<label className="text-xs uppercase text-zinc-500">{t("financial.filters.sku")}</label>
|
||||
<input
|
||||
className="mt-2 w-full rounded-lg border border-white/10 bg-black/30 px-3 py-2"
|
||||
value={skuFilter}
|
||||
onChange={(event) => setSkuFilter(event.target.value)}
|
||||
placeholder={t("financial.filters.skuPlaceholder")}
|
||||
/>
|
||||
</div>
|
||||
<div>
|
||||
<label className="text-xs uppercase text-zinc-500">{t("financial.filters.currency")}</label>
|
||||
<input
|
||||
className="mt-2 w-full rounded-lg border border-white/10 bg-black/30 px-3 py-2"
|
||||
value={currencyFilter}
|
||||
onChange={(event) => setCurrencyFilter(event.target.value.toUpperCase())}
|
||||
placeholder={t("financial.filters.currencyPlaceholder")}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{loading && <div className="text-xs text-zinc-500">{t("financial.loadingMachines")}</div>}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
45
app/(app)/financial/page.tsx
Normal file
45
app/(app)/financial/page.tsx
Normal file
@@ -0,0 +1,45 @@
|
||||
import { redirect } from "next/navigation";
|
||||
import { prisma } from "@/lib/prisma";
|
||||
import { requireSession } from "@/lib/auth/requireSession";
|
||||
import { getFinancialImpactCached } from "@/lib/financial/cache";
|
||||
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 getFinancialImpactCached({
|
||||
orgId: session.orgId,
|
||||
start,
|
||||
end,
|
||||
includeEvents: false,
|
||||
});
|
||||
|
||||
return (
|
||||
<FinancialClient
|
||||
initialRole={role}
|
||||
initialMachines={machines}
|
||||
initialImpact={{ ok: true, currencySummaries: impact.currencySummaries }}
|
||||
/>
|
||||
);
|
||||
}
|
||||
@@ -1,33 +1,22 @@
|
||||
import { Sidebar } from "@/components/layout/Sidebar";
|
||||
import { AppShell } from "@/components/layout/AppShell";
|
||||
import { cookies } from "next/headers";
|
||||
import { redirect } from "next/navigation";
|
||||
import { prisma } from "@/lib/prisma";
|
||||
|
||||
const COOKIE_NAME = "mis_session";
|
||||
|
||||
export default async function AppLayout({ children }: { children: React.ReactNode }) {
|
||||
const sessionId = (await cookies()).get(COOKIE_NAME)?.value;
|
||||
export const dynamic = "force-dynamic";
|
||||
|
||||
if (!sessionId) redirect("/login?next=/machines");
|
||||
export default async function AppLayout({
|
||||
children,
|
||||
}: {
|
||||
children: React.ReactNode;
|
||||
}) {
|
||||
const cookieJar = await cookies();
|
||||
const sessionId = cookieJar.get(COOKIE_NAME)?.value;
|
||||
const themeCookie = cookieJar.get("mis_theme")?.value;
|
||||
const initialTheme = themeCookie === "light" ? "light" : "dark";
|
||||
|
||||
// validate session in DB (don’t trust cookie existence)
|
||||
const session = await prisma.session.findFirst({
|
||||
where: {
|
||||
id: sessionId,
|
||||
revokedAt: null,
|
||||
expiresAt: { gt: new Date() },
|
||||
},
|
||||
include: { user: true, org: true },
|
||||
});
|
||||
if (!sessionId) redirect("/login");
|
||||
|
||||
if (!session) redirect("/login?next=/machines");
|
||||
|
||||
return (
|
||||
<div className="min-h-screen bg-black text-white">
|
||||
<div className="flex">
|
||||
<Sidebar />
|
||||
<main className="flex-1 min-h-screen">{children}</main>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
return <AppShell initialTheme={initialTheme}>{children}</AppShell>;
|
||||
}
|
||||
|
||||
13
app/(app)/loading.tsx
Normal file
13
app/(app)/loading.tsx
Normal file
@@ -0,0 +1,13 @@
|
||||
export default function AppLoading() {
|
||||
return (
|
||||
<div className="p-4 sm:p-6 space-y-6 animate-pulse">
|
||||
<div className="h-7 w-48 rounded-lg bg-white/10" />
|
||||
<div className="grid gap-4 md:grid-cols-2 xl:grid-cols-3">
|
||||
{Array.from({ length: 3 }).map((_, idx) => (
|
||||
<div key={idx} className="h-28 rounded-2xl border border-white/10 bg-white/5" />
|
||||
))}
|
||||
</div>
|
||||
<div className="h-80 rounded-2xl border border-white/10 bg-white/5" />
|
||||
</div>
|
||||
);
|
||||
}
|
||||
349
app/(app)/machines/MachinesClient.tsx
Normal file
349
app/(app)/machines/MachinesClient.tsx
Normal file
@@ -0,0 +1,349 @@
|
||||
"use client";
|
||||
|
||||
import Link from "next/link";
|
||||
import { useRouter } from "next/navigation";
|
||||
import { useEffect, useState, type KeyboardEvent } from "react";
|
||||
import { useI18n } from "@/lib/i18n/useI18n";
|
||||
import { RECAP_HEARTBEAT_STALE_MS } from "@/lib/recap/recapUiConstants";
|
||||
|
||||
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;
|
||||
};
|
||||
};
|
||||
const LIVE_REFRESH_MS = 5000;
|
||||
const OFFLINE_MS = RECAP_HEARTBEAT_STALE_MS;
|
||||
|
||||
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() > OFFLINE_MS;
|
||||
}
|
||||
|
||||
function normalizeStatus(status?: string) {
|
||||
const s = (status ?? "").toUpperCase();
|
||||
if (s === "ONLINE") return "RUN";
|
||||
return s;
|
||||
}
|
||||
|
||||
function badgeClass(status?: string, offline?: boolean) {
|
||||
if (offline) return "bg-white/10 text-zinc-300";
|
||||
const s = (status ?? "").toUpperCase();
|
||||
if (s === "RUN") return "bg-emerald-500/15 text-emerald-300";
|
||||
if (s === "IDLE") return "bg-yellow-500/15 text-yellow-300";
|
||||
if (s === "STOP" || s === "DOWN") return "bg-red-500/15 text-red-300";
|
||||
return "bg-white/10 text-white";
|
||||
}
|
||||
|
||||
export default function MachinesClient({ initialMachines = [] }: { initialMachines?: MachineRow[] }) {
|
||||
const { t, locale } = useI18n();
|
||||
const router = useRouter();
|
||||
const [machines, setMachines] = useState<MachineRow[]>(() => initialMachines);
|
||||
const [loading, setLoading] = useState(() => initialMachines.length === 0);
|
||||
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;
|
||||
let timer: ReturnType<typeof setTimeout> | null = null;
|
||||
|
||||
async function load(initial: boolean) {
|
||||
try {
|
||||
if (!initial && typeof document !== "undefined" && document.hidden) {
|
||||
return;
|
||||
}
|
||||
|
||||
const res = await fetch("/api/machines", { cache: "no-store" });
|
||||
const json = await res.json();
|
||||
if (alive) {
|
||||
setMachines(json.machines ?? []);
|
||||
if (initial) setLoading(false);
|
||||
}
|
||||
} catch {
|
||||
if (alive && initial) setLoading(false);
|
||||
} finally {
|
||||
if (!alive) return;
|
||||
timer = setTimeout(() => {
|
||||
void load(false);
|
||||
}, LIVE_REFRESH_MS);
|
||||
}
|
||||
}
|
||||
|
||||
void load(initialMachines.length === 0);
|
||||
return () => {
|
||||
alive = false;
|
||||
if (timer) clearTimeout(timer);
|
||||
};
|
||||
}, [initialMachines.length]);
|
||||
|
||||
async function createMachine() {
|
||||
if (!createName.trim()) {
|
||||
setCreateError(t("machines.create.error.nameRequired"));
|
||||
return;
|
||||
}
|
||||
|
||||
setCreating(true);
|
||||
setCreateError(null);
|
||||
|
||||
try {
|
||||
const res = await fetch("/api/machines", {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({
|
||||
name: createName,
|
||||
code: createCode,
|
||||
location: createLocation,
|
||||
}),
|
||||
});
|
||||
const data = await res.json().catch(() => ({}));
|
||||
if (!res.ok || !data.ok) {
|
||||
throw new Error(data.error || t("machines.create.error.failed"));
|
||||
}
|
||||
|
||||
const nextMachine = {
|
||||
...data.machine,
|
||||
latestHeartbeat: null,
|
||||
};
|
||||
setMachines((prev) => [nextMachine, ...prev]);
|
||||
setCreatedMachine({
|
||||
id: data.machine.id,
|
||||
name: data.machine.name,
|
||||
pairingCode: data.machine.pairingCode,
|
||||
pairingExpiresAt: data.machine.pairingCodeExpiresAt,
|
||||
});
|
||||
setCreateName("");
|
||||
setCreateCode("");
|
||||
setCreateLocation("");
|
||||
setShowCreate(false);
|
||||
} catch (err: unknown) {
|
||||
const message = err instanceof Error ? err.message : null;
|
||||
setCreateError(message || t("machines.create.error.failed"));
|
||||
} finally {
|
||||
setCreating(false);
|
||||
}
|
||||
}
|
||||
|
||||
async function copyText(text: string) {
|
||||
try {
|
||||
if (navigator.clipboard?.writeText) {
|
||||
await navigator.clipboard.writeText(text);
|
||||
setCopyStatus(t("machines.pairing.copied"));
|
||||
} else {
|
||||
setCopyStatus(t("machines.pairing.copyUnsupported"));
|
||||
}
|
||||
} catch {
|
||||
setCopyStatus(t("machines.pairing.copyFailed"));
|
||||
}
|
||||
setTimeout(() => setCopyStatus(null), 2000);
|
||||
}
|
||||
|
||||
function handleCardKeyDown(event: KeyboardEvent<HTMLDivElement>, machineId: string) {
|
||||
if (event.key === "Enter" || event.key === " ") {
|
||||
event.preventDefault();
|
||||
router.push(`/machines/${machineId}`);
|
||||
}
|
||||
}
|
||||
|
||||
const showCreateCard = showCreate || (!loading && machines.length === 0);
|
||||
|
||||
return (
|
||||
<div className="p-4 sm:p-6">
|
||||
<div className="mb-6 flex flex-col gap-4 sm:flex-row sm:items-center sm:justify-between">
|
||||
<div>
|
||||
<h1 className="text-2xl font-semibold text-white">{t("machines.title")}</h1>
|
||||
<p className="text-sm text-zinc-400">{t("machines.subtitle")}</p>
|
||||
</div>
|
||||
|
||||
<div className="flex w-full flex-wrap items-center gap-2 sm:w-auto">
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => setShowCreate((prev) => !prev)}
|
||||
className="w-full rounded-xl border border-emerald-400/40 bg-emerald-500/20 px-4 py-2 text-sm text-emerald-100 hover:bg-emerald-500/30 sm:w-auto"
|
||||
>
|
||||
{showCreate ? t("machines.cancel") : t("machines.addMachine")}
|
||||
</button>
|
||||
<Link
|
||||
href="/overview"
|
||||
className="w-full rounded-xl border border-white/10 bg-white/5 px-4 py-2 text-center text-sm text-white hover:bg-white/10 sm:w-auto"
|
||||
>
|
||||
{t("machines.backOverview")}
|
||||
</Link>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{showCreateCard && (
|
||||
<div className="mb-6 rounded-2xl border border-white/10 bg-white/5 p-5">
|
||||
<div className="flex flex-col gap-2 sm:flex-row sm:items-center sm:justify-between">
|
||||
<div>
|
||||
<div className="text-sm font-semibold text-white">{t("machines.addCardTitle")}</div>
|
||||
<div className="text-xs text-zinc-400">{t("machines.addCardSubtitle")}</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="mt-4 grid grid-cols-1 gap-3 md:grid-cols-3">
|
||||
<label className="rounded-xl border border-white/10 bg-black/20 p-3 text-xs text-zinc-400">
|
||||
{t("machines.field.name")}
|
||||
<input
|
||||
value={createName}
|
||||
onChange={(event) => setCreateName(event.target.value)}
|
||||
className="mt-2 w-full rounded-lg border border-white/10 bg-black/30 px-3 py-2 text-sm text-white"
|
||||
/>
|
||||
</label>
|
||||
<label className="rounded-xl border border-white/10 bg-black/20 p-3 text-xs text-zinc-400">
|
||||
{t("machines.field.code")}
|
||||
<input
|
||||
value={createCode}
|
||||
onChange={(event) => setCreateCode(event.target.value)}
|
||||
className="mt-2 w-full rounded-lg border border-white/10 bg-black/30 px-3 py-2 text-sm text-white"
|
||||
/>
|
||||
</label>
|
||||
<label className="rounded-xl border border-white/10 bg-black/20 p-3 text-xs text-zinc-400">
|
||||
{t("machines.field.location")}
|
||||
<input
|
||||
value={createLocation}
|
||||
onChange={(event) => setCreateLocation(event.target.value)}
|
||||
className="mt-2 w-full rounded-lg border border-white/10 bg-black/30 px-3 py-2 text-sm text-white"
|
||||
/>
|
||||
</label>
|
||||
</div>
|
||||
|
||||
<div className="mt-4 flex flex-wrap items-center gap-3">
|
||||
<button
|
||||
type="button"
|
||||
onClick={createMachine}
|
||||
disabled={creating}
|
||||
className="rounded-xl border border-emerald-400/40 bg-emerald-500/20 px-4 py-2 text-sm text-emerald-100 hover:bg-emerald-500/30 disabled:opacity-60"
|
||||
>
|
||||
{creating ? t("machines.create.loading") : t("machines.create.default")}
|
||||
</button>
|
||||
{createError && <div className="text-xs text-red-200">{createError}</div>}
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{createdMachine && (
|
||||
<div className="mb-6 rounded-2xl border border-emerald-500/20 bg-emerald-500/10 p-5">
|
||||
<div className="text-sm font-semibold text-white">{t("machines.pairing.title")}</div>
|
||||
<div className="mt-2 text-xs text-zinc-300">
|
||||
{t("machines.pairing.machine")} <span className="text-white">{createdMachine.name}</span>
|
||||
</div>
|
||||
<div className="mt-3 rounded-xl border border-white/10 bg-black/30 p-4">
|
||||
<div className="text-xs uppercase tracking-wide text-zinc-400">{t("machines.pairing.codeLabel")}</div>
|
||||
<div className="mt-2 text-3xl font-semibold text-white">{createdMachine.pairingCode}</div>
|
||||
<div className="mt-2 text-xs text-zinc-400">
|
||||
{t("machines.pairing.expires")}{" "}
|
||||
{createdMachine.pairingExpiresAt
|
||||
? new Date(createdMachine.pairingExpiresAt).toLocaleString(locale)
|
||||
: t("machines.pairing.soon")}
|
||||
</div>
|
||||
</div>
|
||||
<div className="mt-3 text-xs text-zinc-300">
|
||||
{t("machines.pairing.instructions")}
|
||||
</div>
|
||||
<div className="mt-3 flex flex-wrap items-center gap-3">
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => copyText(createdMachine.pairingCode)}
|
||||
className="rounded-xl border border-white/10 bg-white/5 px-3 py-2 text-sm text-white hover:bg-white/10"
|
||||
>
|
||||
{t("machines.pairing.copy")}
|
||||
</button>
|
||||
{copyStatus && <div className="text-xs text-zinc-300">{copyStatus}</div>}
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{loading && <div className="mb-4 text-sm text-zinc-400">{t("machines.loading")}</div>}
|
||||
|
||||
{!loading && machines.length === 0 && (
|
||||
<div className="mb-4 text-sm text-zinc-400">{t("machines.empty")}</div>
|
||||
)}
|
||||
|
||||
<div className="grid grid-cols-1 gap-4 md:grid-cols-2 xl:grid-cols-3">
|
||||
{(!loading ? machines : []).map((m) => {
|
||||
const hb = m.latestHeartbeat;
|
||||
const hbTs = hb?.tsServer ?? hb?.ts;
|
||||
const offline = isOffline(hbTs);
|
||||
const normalizedStatus = normalizeStatus(hb?.status);
|
||||
const statusLabel = offline ? t("machines.status.offline") : (normalizedStatus || t("machines.status.unknown"));
|
||||
const lastSeen = secondsAgo(hbTs, locale, t("common.never"));
|
||||
|
||||
return (
|
||||
<div
|
||||
key={m.id}
|
||||
role="link"
|
||||
tabIndex={0}
|
||||
onClick={() => router.push(`/machines/${m.id}`)}
|
||||
onKeyDown={(event) => handleCardKeyDown(event, m.id)}
|
||||
className="cursor-pointer rounded-2xl border border-white/10 bg-white/5 p-5 hover:bg-white/10"
|
||||
>
|
||||
<div className="flex items-center justify-between gap-3">
|
||||
<div className="min-w-0">
|
||||
<div className="truncate text-lg font-semibold text-white">{m.name}</div>
|
||||
<div className="mt-1 text-xs text-zinc-400">
|
||||
{m.code ? m.code : t("common.na")} - {t("machines.lastSeen", { time: lastSeen })}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<span
|
||||
className={`shrink-0 rounded-full px-3 py-1 text-xs ${badgeClass(
|
||||
normalizedStatus,
|
||||
offline
|
||||
)}`}
|
||||
>
|
||||
{statusLabel}
|
||||
</span>
|
||||
</div>
|
||||
|
||||
<div className="mt-4 text-sm text-zinc-400">{t("machines.status")}</div>
|
||||
<div className="mt-1 flex items-center gap-2 text-sm font-semibold text-white">
|
||||
{offline ? (
|
||||
<>
|
||||
<span className="inline-flex h-2.5 w-2.5 rounded-full bg-zinc-500" aria-hidden="true" />
|
||||
<span>{t("machines.status.noHeartbeat")}</span>
|
||||
</>
|
||||
) : (
|
||||
<>
|
||||
<span className="relative flex h-2.5 w-2.5" aria-hidden="true">
|
||||
<span className="animate-ping absolute inline-flex h-full w-full rounded-full bg-emerald-400 opacity-75" />
|
||||
<span className="relative inline-flex h-2.5 w-2.5 rounded-full bg-emerald-400" />
|
||||
</span>
|
||||
<span>{t("machines.status.ok")}</span>
|
||||
</>
|
||||
)}
|
||||
</div>
|
||||
|
||||
</div>
|
||||
);
|
||||
})}
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
347
app/(app)/machines/MachinesClient.tsx.bak
Normal file
347
app/(app)/machines/MachinesClient.tsx.bak
Normal file
@@ -0,0 +1,347 @@
|
||||
"use client";
|
||||
|
||||
import Link from "next/link";
|
||||
import { useRouter } from "next/navigation";
|
||||
import { useEffect, useState, type KeyboardEvent } from "react";
|
||||
import { useI18n } from "@/lib/i18n/useI18n";
|
||||
|
||||
type MachineRow = {
|
||||
id: string;
|
||||
name: string;
|
||||
code?: string | null;
|
||||
location?: string | null;
|
||||
latestHeartbeat: null | {
|
||||
ts: string;
|
||||
tsServer?: string | null;
|
||||
status: string;
|
||||
message?: string | null;
|
||||
ip?: string | null;
|
||||
fwVersion?: string | null;
|
||||
};
|
||||
};
|
||||
const LIVE_REFRESH_MS = 5000;
|
||||
|
||||
function secondsAgo(ts: string | undefined, locale: string, fallback: string) {
|
||||
if (!ts) return fallback;
|
||||
const diff = Math.floor((Date.now() - new Date(ts).getTime()) / 1000);
|
||||
const rtf = new Intl.RelativeTimeFormat(locale, { numeric: "auto" });
|
||||
if (diff < 60) return rtf.format(-diff, "second");
|
||||
return rtf.format(-Math.floor(diff / 60), "minute");
|
||||
}
|
||||
|
||||
function isOffline(ts?: string) {
|
||||
if (!ts) return true;
|
||||
return Date.now() - new Date(ts).getTime() > 30000; // 30s threshold
|
||||
}
|
||||
|
||||
function normalizeStatus(status?: string) {
|
||||
const s = (status ?? "").toUpperCase();
|
||||
if (s === "ONLINE") return "RUN";
|
||||
return s;
|
||||
}
|
||||
|
||||
function badgeClass(status?: string, offline?: boolean) {
|
||||
if (offline) return "bg-white/10 text-zinc-300";
|
||||
const s = (status ?? "").toUpperCase();
|
||||
if (s === "RUN") return "bg-emerald-500/15 text-emerald-300";
|
||||
if (s === "IDLE") return "bg-yellow-500/15 text-yellow-300";
|
||||
if (s === "STOP" || s === "DOWN") return "bg-red-500/15 text-red-300";
|
||||
return "bg-white/10 text-white";
|
||||
}
|
||||
|
||||
export default function MachinesClient({ initialMachines = [] }: { initialMachines?: MachineRow[] }) {
|
||||
const { t, locale } = useI18n();
|
||||
const router = useRouter();
|
||||
const [machines, setMachines] = useState<MachineRow[]>(() => initialMachines);
|
||||
const [loading, setLoading] = useState(() => initialMachines.length === 0);
|
||||
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;
|
||||
let timer: ReturnType<typeof setTimeout> | null = null;
|
||||
|
||||
async function load(initial: boolean) {
|
||||
try {
|
||||
if (!initial && typeof document !== "undefined" && document.hidden) {
|
||||
return;
|
||||
}
|
||||
|
||||
const res = await fetch("/api/machines", { cache: "no-store" });
|
||||
const json = await res.json();
|
||||
if (alive) {
|
||||
setMachines(json.machines ?? []);
|
||||
if (initial) setLoading(false);
|
||||
}
|
||||
} catch {
|
||||
if (alive && initial) setLoading(false);
|
||||
} finally {
|
||||
if (!alive) return;
|
||||
timer = setTimeout(() => {
|
||||
void load(false);
|
||||
}, LIVE_REFRESH_MS);
|
||||
}
|
||||
}
|
||||
|
||||
void load(initialMachines.length === 0);
|
||||
return () => {
|
||||
alive = false;
|
||||
if (timer) clearTimeout(timer);
|
||||
};
|
||||
}, [initialMachines.length]);
|
||||
|
||||
async function createMachine() {
|
||||
if (!createName.trim()) {
|
||||
setCreateError(t("machines.create.error.nameRequired"));
|
||||
return;
|
||||
}
|
||||
|
||||
setCreating(true);
|
||||
setCreateError(null);
|
||||
|
||||
try {
|
||||
const res = await fetch("/api/machines", {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({
|
||||
name: createName,
|
||||
code: createCode,
|
||||
location: createLocation,
|
||||
}),
|
||||
});
|
||||
const data = await res.json().catch(() => ({}));
|
||||
if (!res.ok || !data.ok) {
|
||||
throw new Error(data.error || t("machines.create.error.failed"));
|
||||
}
|
||||
|
||||
const nextMachine = {
|
||||
...data.machine,
|
||||
latestHeartbeat: null,
|
||||
};
|
||||
setMachines((prev) => [nextMachine, ...prev]);
|
||||
setCreatedMachine({
|
||||
id: data.machine.id,
|
||||
name: data.machine.name,
|
||||
pairingCode: data.machine.pairingCode,
|
||||
pairingExpiresAt: data.machine.pairingCodeExpiresAt,
|
||||
});
|
||||
setCreateName("");
|
||||
setCreateCode("");
|
||||
setCreateLocation("");
|
||||
setShowCreate(false);
|
||||
} catch (err: unknown) {
|
||||
const message = err instanceof Error ? err.message : null;
|
||||
setCreateError(message || t("machines.create.error.failed"));
|
||||
} finally {
|
||||
setCreating(false);
|
||||
}
|
||||
}
|
||||
|
||||
async function copyText(text: string) {
|
||||
try {
|
||||
if (navigator.clipboard?.writeText) {
|
||||
await navigator.clipboard.writeText(text);
|
||||
setCopyStatus(t("machines.pairing.copied"));
|
||||
} else {
|
||||
setCopyStatus(t("machines.pairing.copyUnsupported"));
|
||||
}
|
||||
} catch {
|
||||
setCopyStatus(t("machines.pairing.copyFailed"));
|
||||
}
|
||||
setTimeout(() => setCopyStatus(null), 2000);
|
||||
}
|
||||
|
||||
function handleCardKeyDown(event: KeyboardEvent<HTMLDivElement>, machineId: string) {
|
||||
if (event.key === "Enter" || event.key === " ") {
|
||||
event.preventDefault();
|
||||
router.push(`/machines/${machineId}`);
|
||||
}
|
||||
}
|
||||
|
||||
const showCreateCard = showCreate || (!loading && machines.length === 0);
|
||||
|
||||
return (
|
||||
<div className="p-4 sm:p-6">
|
||||
<div className="mb-6 flex flex-col gap-4 sm:flex-row sm:items-center sm:justify-between">
|
||||
<div>
|
||||
<h1 className="text-2xl font-semibold text-white">{t("machines.title")}</h1>
|
||||
<p className="text-sm text-zinc-400">{t("machines.subtitle")}</p>
|
||||
</div>
|
||||
|
||||
<div className="flex w-full flex-wrap items-center gap-2 sm:w-auto">
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => setShowCreate((prev) => !prev)}
|
||||
className="w-full rounded-xl border border-emerald-400/40 bg-emerald-500/20 px-4 py-2 text-sm text-emerald-100 hover:bg-emerald-500/30 sm:w-auto"
|
||||
>
|
||||
{showCreate ? t("machines.cancel") : t("machines.addMachine")}
|
||||
</button>
|
||||
<Link
|
||||
href="/overview"
|
||||
className="w-full rounded-xl border border-white/10 bg-white/5 px-4 py-2 text-center text-sm text-white hover:bg-white/10 sm:w-auto"
|
||||
>
|
||||
{t("machines.backOverview")}
|
||||
</Link>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{showCreateCard && (
|
||||
<div className="mb-6 rounded-2xl border border-white/10 bg-white/5 p-5">
|
||||
<div className="flex flex-col gap-2 sm:flex-row sm:items-center sm:justify-between">
|
||||
<div>
|
||||
<div className="text-sm font-semibold text-white">{t("machines.addCardTitle")}</div>
|
||||
<div className="text-xs text-zinc-400">{t("machines.addCardSubtitle")}</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="mt-4 grid grid-cols-1 gap-3 md:grid-cols-3">
|
||||
<label className="rounded-xl border border-white/10 bg-black/20 p-3 text-xs text-zinc-400">
|
||||
{t("machines.field.name")}
|
||||
<input
|
||||
value={createName}
|
||||
onChange={(event) => setCreateName(event.target.value)}
|
||||
className="mt-2 w-full rounded-lg border border-white/10 bg-black/30 px-3 py-2 text-sm text-white"
|
||||
/>
|
||||
</label>
|
||||
<label className="rounded-xl border border-white/10 bg-black/20 p-3 text-xs text-zinc-400">
|
||||
{t("machines.field.code")}
|
||||
<input
|
||||
value={createCode}
|
||||
onChange={(event) => setCreateCode(event.target.value)}
|
||||
className="mt-2 w-full rounded-lg border border-white/10 bg-black/30 px-3 py-2 text-sm text-white"
|
||||
/>
|
||||
</label>
|
||||
<label className="rounded-xl border border-white/10 bg-black/20 p-3 text-xs text-zinc-400">
|
||||
{t("machines.field.location")}
|
||||
<input
|
||||
value={createLocation}
|
||||
onChange={(event) => setCreateLocation(event.target.value)}
|
||||
className="mt-2 w-full rounded-lg border border-white/10 bg-black/30 px-3 py-2 text-sm text-white"
|
||||
/>
|
||||
</label>
|
||||
</div>
|
||||
|
||||
<div className="mt-4 flex flex-wrap items-center gap-3">
|
||||
<button
|
||||
type="button"
|
||||
onClick={createMachine}
|
||||
disabled={creating}
|
||||
className="rounded-xl border border-emerald-400/40 bg-emerald-500/20 px-4 py-2 text-sm text-emerald-100 hover:bg-emerald-500/30 disabled:opacity-60"
|
||||
>
|
||||
{creating ? t("machines.create.loading") : t("machines.create.default")}
|
||||
</button>
|
||||
{createError && <div className="text-xs text-red-200">{createError}</div>}
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{createdMachine && (
|
||||
<div className="mb-6 rounded-2xl border border-emerald-500/20 bg-emerald-500/10 p-5">
|
||||
<div className="text-sm font-semibold text-white">{t("machines.pairing.title")}</div>
|
||||
<div className="mt-2 text-xs text-zinc-300">
|
||||
{t("machines.pairing.machine")} <span className="text-white">{createdMachine.name}</span>
|
||||
</div>
|
||||
<div className="mt-3 rounded-xl border border-white/10 bg-black/30 p-4">
|
||||
<div className="text-xs uppercase tracking-wide text-zinc-400">{t("machines.pairing.codeLabel")}</div>
|
||||
<div className="mt-2 text-3xl font-semibold text-white">{createdMachine.pairingCode}</div>
|
||||
<div className="mt-2 text-xs text-zinc-400">
|
||||
{t("machines.pairing.expires")}{" "}
|
||||
{createdMachine.pairingExpiresAt
|
||||
? new Date(createdMachine.pairingExpiresAt).toLocaleString(locale)
|
||||
: t("machines.pairing.soon")}
|
||||
</div>
|
||||
</div>
|
||||
<div className="mt-3 text-xs text-zinc-300">
|
||||
{t("machines.pairing.instructions")}
|
||||
</div>
|
||||
<div className="mt-3 flex flex-wrap items-center gap-3">
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => copyText(createdMachine.pairingCode)}
|
||||
className="rounded-xl border border-white/10 bg-white/5 px-3 py-2 text-sm text-white hover:bg-white/10"
|
||||
>
|
||||
{t("machines.pairing.copy")}
|
||||
</button>
|
||||
{copyStatus && <div className="text-xs text-zinc-300">{copyStatus}</div>}
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{loading && <div className="mb-4 text-sm text-zinc-400">{t("machines.loading")}</div>}
|
||||
|
||||
{!loading && machines.length === 0 && (
|
||||
<div className="mb-4 text-sm text-zinc-400">{t("machines.empty")}</div>
|
||||
)}
|
||||
|
||||
<div className="grid grid-cols-1 gap-4 md:grid-cols-2 xl:grid-cols-3">
|
||||
{(!loading ? machines : []).map((m) => {
|
||||
const hb = m.latestHeartbeat;
|
||||
const hbTs = hb?.tsServer ?? hb?.ts;
|
||||
const offline = isOffline(hbTs);
|
||||
const normalizedStatus = normalizeStatus(hb?.status);
|
||||
const statusLabel = offline ? t("machines.status.offline") : (normalizedStatus || t("machines.status.unknown"));
|
||||
const lastSeen = secondsAgo(hbTs, locale, t("common.never"));
|
||||
|
||||
return (
|
||||
<div
|
||||
key={m.id}
|
||||
role="link"
|
||||
tabIndex={0}
|
||||
onClick={() => router.push(`/machines/${m.id}`)}
|
||||
onKeyDown={(event) => handleCardKeyDown(event, m.id)}
|
||||
className="cursor-pointer rounded-2xl border border-white/10 bg-white/5 p-5 hover:bg-white/10"
|
||||
>
|
||||
<div className="flex items-center justify-between gap-3">
|
||||
<div className="min-w-0">
|
||||
<div className="truncate text-lg font-semibold text-white">{m.name}</div>
|
||||
<div className="mt-1 text-xs text-zinc-400">
|
||||
{m.code ? m.code : t("common.na")} - {t("machines.lastSeen", { time: lastSeen })}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<span
|
||||
className={`shrink-0 rounded-full px-3 py-1 text-xs ${badgeClass(
|
||||
normalizedStatus,
|
||||
offline
|
||||
)}`}
|
||||
>
|
||||
{statusLabel}
|
||||
</span>
|
||||
</div>
|
||||
|
||||
<div className="mt-4 text-sm text-zinc-400">{t("machines.status")}</div>
|
||||
<div className="mt-1 flex items-center gap-2 text-sm font-semibold text-white">
|
||||
{offline ? (
|
||||
<>
|
||||
<span className="inline-flex h-2.5 w-2.5 rounded-full bg-zinc-500" aria-hidden="true" />
|
||||
<span>{t("machines.status.noHeartbeat")}</span>
|
||||
</>
|
||||
) : (
|
||||
<>
|
||||
<span className="relative flex h-2.5 w-2.5" aria-hidden="true">
|
||||
<span className="animate-ping absolute inline-flex h-full w-full rounded-full bg-emerald-400 opacity-75" />
|
||||
<span className="relative inline-flex h-2.5 w-2.5 rounded-full bg-emerald-400" />
|
||||
</span>
|
||||
<span>{t("machines.status.ok")}</span>
|
||||
</>
|
||||
)}
|
||||
</div>
|
||||
|
||||
</div>
|
||||
);
|
||||
})}
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
22
app/(app)/machines/loading.tsx
Normal file
22
app/(app)/machines/loading.tsx
Normal file
@@ -0,0 +1,22 @@
|
||||
export default function MachinesLoading() {
|
||||
return (
|
||||
<div className="p-4 sm:p-6 space-y-6 animate-pulse">
|
||||
<div className="flex flex-col gap-3 sm:flex-row sm:items-center sm:justify-between">
|
||||
<div className="space-y-2">
|
||||
<div className="h-6 w-36 rounded-lg bg-white/10" />
|
||||
<div className="h-4 w-60 rounded-lg bg-white/5" />
|
||||
</div>
|
||||
<div className="flex w-full gap-2 sm:w-auto">
|
||||
<div className="h-9 w-full rounded-xl border border-emerald-400/40 bg-emerald-500/10 sm:w-36" />
|
||||
<div className="h-9 w-full rounded-xl border border-white/10 bg-white/5 sm:w-32" />
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="grid grid-cols-1 gap-4 md:grid-cols-2 xl:grid-cols-3">
|
||||
{Array.from({ length: 6 }).map((_, idx) => (
|
||||
<div key={idx} className="h-40 rounded-2xl border border-white/10 bg-white/5" />
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -1,133 +1,47 @@
|
||||
"use client";
|
||||
import { redirect } from "next/navigation";
|
||||
import { requireSession } from "@/lib/auth/requireSession";
|
||||
import {
|
||||
fetchLatestHeartbeats,
|
||||
fetchMachineBase,
|
||||
mergeMachineOverviewRows,
|
||||
} from "@/lib/machines/withLatest";
|
||||
import MachinesClient from "./MachinesClient";
|
||||
|
||||
import Link from "next/link";
|
||||
import { useEffect, useState } from "react";
|
||||
|
||||
type MachineRow = {
|
||||
id: string;
|
||||
name: string;
|
||||
code?: string | null;
|
||||
location?: string | null;
|
||||
latestHeartbeat: null | {
|
||||
ts: string;
|
||||
status: string;
|
||||
message?: string | null;
|
||||
ip?: string | null;
|
||||
fwVersion?: string | null;
|
||||
};
|
||||
};
|
||||
|
||||
function secondsAgo(ts?: string) {
|
||||
if (!ts) return "never";
|
||||
const diff = Math.floor((Date.now() - new Date(ts).getTime()) / 1000);
|
||||
if (diff < 60) return `${diff}s ago`;
|
||||
return `${Math.floor(diff / 60)}m ago`;
|
||||
function toIso(value?: Date | null) {
|
||||
return value ? value.toISOString() : null;
|
||||
}
|
||||
|
||||
function isOffline(ts?: string) {
|
||||
if (!ts) return true;
|
||||
return Date.now() - new Date(ts).getTime() > 15000; // 15s threshold
|
||||
}
|
||||
export default async function MachinesPage() {
|
||||
const session = await requireSession();
|
||||
if (!session) redirect("/login?next=/machines");
|
||||
|
||||
function badgeClass(status?: string, offline?: boolean) {
|
||||
if (offline) return "bg-white/10 text-zinc-300";
|
||||
const s = (status ?? "").toUpperCase();
|
||||
if (s === "RUN") return "bg-emerald-500/15 text-emerald-300";
|
||||
if (s === "IDLE") return "bg-yellow-500/15 text-yellow-300";
|
||||
if (s === "STOP" || s === "DOWN") return "bg-red-500/15 text-red-300";
|
||||
return "bg-white/10 text-white";
|
||||
}
|
||||
|
||||
export default function MachinesPage() {
|
||||
const [machines, setMachines] = useState<MachineRow[]>([]);
|
||||
const [loading, setLoading] = useState(true);
|
||||
|
||||
useEffect(() => {
|
||||
let alive = true;
|
||||
|
||||
async function load() {
|
||||
try {
|
||||
const res = await fetch("/api/machines", { cache: "no-store" });
|
||||
const json = await res.json();
|
||||
if (alive) {
|
||||
setMachines(json.machines ?? []);
|
||||
setLoading(false);
|
||||
}
|
||||
} catch {
|
||||
if (alive) setLoading(false);
|
||||
}
|
||||
}
|
||||
|
||||
load();
|
||||
const t = setInterval(load, 5000);
|
||||
|
||||
return () => {
|
||||
alive = false;
|
||||
clearInterval(t);
|
||||
};
|
||||
}, []);
|
||||
|
||||
return (
|
||||
<div className="p-6">
|
||||
<div className="mb-6 flex items-center justify-between">
|
||||
<div>
|
||||
<h1 className="text-2xl font-semibold text-white">Machines</h1>
|
||||
<p className="text-sm text-zinc-400">Select a machine to view live KPIs.</p>
|
||||
</div>
|
||||
|
||||
<Link
|
||||
href="/overview"
|
||||
className="rounded-xl border border-white/10 bg-white/5 px-4 py-2 text-sm text-white hover:bg-white/10"
|
||||
>
|
||||
Back to Overview
|
||||
</Link>
|
||||
</div>
|
||||
|
||||
{loading && <div className="mb-4 text-sm text-zinc-400">Loading machines…</div>}
|
||||
|
||||
{!loading && machines.length === 0 && (
|
||||
<div className="mb-4 text-sm text-zinc-400">No machines found for this org.</div>
|
||||
)}
|
||||
|
||||
<div className="grid grid-cols-1 gap-4 md:grid-cols-2 xl:grid-cols-3">
|
||||
{(!loading ? machines : []).map((m) => {
|
||||
const hb = m.latestHeartbeat;
|
||||
const offline = isOffline(hb?.ts);
|
||||
const statusLabel = offline ? "OFFLINE" : hb?.status ?? "UNKNOWN";
|
||||
const lastSeen = secondsAgo(hb?.ts);
|
||||
|
||||
return (
|
||||
<Link
|
||||
key={m.id}
|
||||
href={`/machines/${m.id}`}
|
||||
className="rounded-2xl border border-white/10 bg-white/5 p-5 hover:bg-white/10"
|
||||
>
|
||||
<div className="flex items-center justify-between gap-3">
|
||||
<div className="min-w-0">
|
||||
<div className="truncate text-lg font-semibold text-white">{m.name}</div>
|
||||
<div className="mt-1 text-xs text-zinc-400">
|
||||
{m.code ? m.code : "—"} • Last seen {lastSeen}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<span
|
||||
className={`shrink-0 rounded-full px-3 py-1 text-xs ${badgeClass(
|
||||
hb?.status,
|
||||
offline
|
||||
)}`}
|
||||
>
|
||||
{statusLabel}
|
||||
</span>
|
||||
</div>
|
||||
|
||||
<div className="mt-4 text-sm text-zinc-400">Status</div>
|
||||
<div className="text-xl font-semibold text-white">
|
||||
{offline ? "No heartbeat" : hb?.message ?? "OK"}
|
||||
</div>
|
||||
</Link>
|
||||
);
|
||||
})}
|
||||
</div>
|
||||
</div>
|
||||
const machines = await fetchMachineBase(session.orgId);
|
||||
const heartbeats = await fetchLatestHeartbeats(
|
||||
session.orgId,
|
||||
machines.map((machine) => machine.id)
|
||||
);
|
||||
const rows = mergeMachineOverviewRows({
|
||||
machines,
|
||||
heartbeats,
|
||||
includeKpi: false,
|
||||
});
|
||||
|
||||
const initialMachines = rows.map((machine) => ({
|
||||
id: machine.id,
|
||||
name: machine.name,
|
||||
code: machine.code ?? null,
|
||||
location: machine.location ?? null,
|
||||
latestHeartbeat: machine.latestHeartbeat
|
||||
? {
|
||||
ts: toIso(machine.latestHeartbeat.ts) ?? "",
|
||||
tsServer: toIso(machine.latestHeartbeat.tsServer),
|
||||
status: machine.latestHeartbeat.status,
|
||||
message: machine.latestHeartbeat.message ?? null,
|
||||
ip: machine.latestHeartbeat.ip ?? null,
|
||||
fwVersion: machine.latestHeartbeat.fwVersion ?? null,
|
||||
}
|
||||
: null,
|
||||
}));
|
||||
|
||||
return <MachinesClient initialMachines={initialMachines} />;
|
||||
}
|
||||
|
||||
445
app/(app)/overview/OverviewClient.tsx
Normal file
445
app/(app)/overview/OverviewClient.tsx
Normal file
@@ -0,0 +1,445 @@
|
||||
"use client";
|
||||
|
||||
import Link from "next/link";
|
||||
import { Suspense, lazy, useEffect, useMemo, useState } from "react";
|
||||
import { useI18n } from "@/lib/i18n/useI18n";
|
||||
import { RECAP_HEARTBEAT_STALE_MS } from "@/lib/recap/recapUiConstants";
|
||||
import type { EventRow, Heartbeat, MachineRow } from "./types";
|
||||
|
||||
const OFFLINE_MS = RECAP_HEARTBEAT_STALE_MS;
|
||||
const MAX_EVENT_MACHINES = 6;
|
||||
const OverviewTimeline = lazy(() => import("./OverviewTimeline"));
|
||||
|
||||
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 OverviewTimelineSkeleton() {
|
||||
return (
|
||||
<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="h-4 w-32 rounded bg-white/10" />
|
||||
<div className="h-3 w-20 rounded bg-white/5" />
|
||||
</div>
|
||||
<div className="space-y-3">
|
||||
{Array.from({ length: 4 }).map((_, idx) => (
|
||||
<div key={idx} className="h-20 rounded-xl border border-white/10 bg-black/20" />
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
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(() => initialEvents.length === 0);
|
||||
|
||||
useEffect(() => {
|
||||
let alive = true;
|
||||
|
||||
async function load() {
|
||||
try {
|
||||
setEventsLoading(true);
|
||||
const res = await fetch(
|
||||
`/api/overview?detail=1&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;
|
||||
let hasKpi = false;
|
||||
|
||||
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;
|
||||
hasKpi = true;
|
||||
}
|
||||
if (k?.availability != null) {
|
||||
availSum += Number(k.availability);
|
||||
availCount += 1;
|
||||
hasKpi = true;
|
||||
}
|
||||
if (k?.performance != null) {
|
||||
perfSum += Number(k.performance);
|
||||
perfCount += 1;
|
||||
hasKpi = true;
|
||||
}
|
||||
if (k?.quality != null) {
|
||||
qualSum += Number(k.quality);
|
||||
qualCount += 1;
|
||||
hasKpi = true;
|
||||
}
|
||||
if (k?.good != null) {
|
||||
goodSum += Number(k.good);
|
||||
hasKpi = true;
|
||||
}
|
||||
if (k?.scrap != null) {
|
||||
scrapSum += Number(k.scrap);
|
||||
hasKpi = true;
|
||||
}
|
||||
if (k?.target != null) {
|
||||
targetSum += Number(k.target);
|
||||
hasKpi = true;
|
||||
}
|
||||
}
|
||||
|
||||
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: hasKpi ? goodSum : null,
|
||||
scrapSum: hasKpi ? scrapSum : null,
|
||||
targetSum: hasKpi ? targetSum : null,
|
||||
};
|
||||
}, [machines]);
|
||||
|
||||
const attention = useMemo(() => {
|
||||
const list = machines
|
||||
.map((m) => {
|
||||
const hb = m.latestHeartbeat;
|
||||
const offline = isOffline(heartbeatTime(hb));
|
||||
const status = normalizeStatus(hb?.status);
|
||||
const k = m.latestKpi;
|
||||
const oee = k?.oee ?? null;
|
||||
const good = k?.good ?? null;
|
||||
const scrap = k?.scrap ?? null;
|
||||
const availability = k?.availability ?? null;
|
||||
|
||||
const reasons: string[] = [];
|
||||
let score = 0;
|
||||
|
||||
// Trigger 1: offline (highest priority — can't tell what's wrong)
|
||||
if (offline) {
|
||||
score += 100;
|
||||
reasons.push(t("overview.attention.offline"));
|
||||
}
|
||||
|
||||
// Trigger 2: stopped right now (and online — operator should act)
|
||||
if (!offline && (status === "STOP" || status === "DOWN")) {
|
||||
score += 60;
|
||||
reasons.push(t("overview.attention.stopped"));
|
||||
}
|
||||
|
||||
// Trigger 3: low OEE
|
||||
if (!offline && oee != null) {
|
||||
if (oee < 50) {
|
||||
score += 50;
|
||||
reasons.push(t("overview.attention.oeeCritical", { value: oee.toFixed(0) }));
|
||||
} else if (oee < 75) {
|
||||
score += 30;
|
||||
reasons.push(t("overview.attention.oeeLow", { value: oee.toFixed(0) }));
|
||||
}
|
||||
}
|
||||
|
||||
// Trigger 4: scrap rate >5% on active WO
|
||||
if (!offline && good != null && scrap != null && good + scrap > 0) {
|
||||
const scrapPct = (scrap / (good + scrap)) * 100;
|
||||
if (scrapPct > 10) {
|
||||
score += 40;
|
||||
reasons.push(t("overview.attention.scrapHigh", { value: scrapPct.toFixed(1) }));
|
||||
} else if (scrapPct > 5) {
|
||||
score += 20;
|
||||
reasons.push(t("overview.attention.scrapMod", { value: scrapPct.toFixed(1) }));
|
||||
}
|
||||
}
|
||||
|
||||
// Trigger 5: availability collapse (often means undeclared stops)
|
||||
if (!offline && availability != null && availability < 60) {
|
||||
score += 25;
|
||||
reasons.push(t("overview.attention.availLow", { value: availability.toFixed(0) }));
|
||||
}
|
||||
|
||||
return { machine: m, offline, oee, score, reasons };
|
||||
})
|
||||
.filter((x) => x.score > 0)
|
||||
.sort((a, b) => b.score - a.score)
|
||||
.slice(0, 6);
|
||||
|
||||
return list;
|
||||
}, [machines, t]);
|
||||
|
||||
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="mb-4 rounded-2xl border border-white/10 bg-black/40 p-4">
|
||||
<div className="flex flex-col gap-3 sm:flex-row sm:items-center sm:justify-between">
|
||||
<div>
|
||||
<div className="text-sm font-semibold text-white">{t("overview.recap.title")}</div>
|
||||
<div className="text-xs text-zinc-400">{t("overview.recap.subtitle")}</div>
|
||||
</div>
|
||||
<Link
|
||||
href="/recap"
|
||||
className="rounded-xl border border-emerald-500/30 bg-emerald-500/10 px-3 py-2 text-sm text-emerald-300 hover:bg-emerald-500/20"
|
||||
>
|
||||
{t("overview.recap.cta")}
|
||||
</Link>
|
||||
</div>
|
||||
</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, reasons }) => (
|
||||
<Link
|
||||
key={machine.id}
|
||||
href={`/recap/${machine.id}`}
|
||||
className="block rounded-xl border border-white/10 bg-black/20 p-3 hover:border-white/20 hover:bg-black/30 transition"
|
||||
>
|
||||
<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 flex-wrap items-center gap-1.5 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 && !offline && (
|
||||
<span className="rounded-full bg-yellow-500/15 px-2 py-0.5 text-yellow-300">
|
||||
OEE {fmtPct(oee)}
|
||||
</span>
|
||||
)}
|
||||
</div>
|
||||
{reasons.length > 0 && (
|
||||
<ul className="mt-2 space-y-0.5 text-[11px] text-zinc-400">
|
||||
{reasons.map((r, i) => (
|
||||
<li key={i}>· {r}</li>
|
||||
))}
|
||||
</ul>
|
||||
)}
|
||||
</Link>
|
||||
))}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
|
||||
<Suspense fallback={<OverviewTimelineSkeleton />}>
|
||||
<OverviewTimeline events={events} eventsLoading={eventsLoading} locale={locale} t={t} />
|
||||
</Suspense>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
388
app/(app)/overview/OverviewClient.tsx.bak
Normal file
388
app/(app)/overview/OverviewClient.tsx.bak
Normal file
@@ -0,0 +1,388 @@
|
||||
"use client";
|
||||
|
||||
import Link from "next/link";
|
||||
import { Suspense, lazy, useEffect, useMemo, useState } from "react";
|
||||
import { useI18n } from "@/lib/i18n/useI18n";
|
||||
import type { EventRow, Heartbeat, MachineRow } from "./types";
|
||||
|
||||
const OFFLINE_MS = 30000;
|
||||
const MAX_EVENT_MACHINES = 6;
|
||||
const OverviewTimeline = lazy(() => import("./OverviewTimeline"));
|
||||
|
||||
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 OverviewTimelineSkeleton() {
|
||||
return (
|
||||
<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="h-4 w-32 rounded bg-white/10" />
|
||||
<div className="h-3 w-20 rounded bg-white/5" />
|
||||
</div>
|
||||
<div className="space-y-3">
|
||||
{Array.from({ length: 4 }).map((_, idx) => (
|
||||
<div key={idx} className="h-20 rounded-xl border border-white/10 bg-black/20" />
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
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(() => initialEvents.length === 0);
|
||||
|
||||
useEffect(() => {
|
||||
let alive = true;
|
||||
|
||||
async function load() {
|
||||
try {
|
||||
setEventsLoading(true);
|
||||
const res = await fetch(
|
||||
`/api/overview?detail=1&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;
|
||||
let hasKpi = false;
|
||||
|
||||
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;
|
||||
hasKpi = true;
|
||||
}
|
||||
if (k?.availability != null) {
|
||||
availSum += Number(k.availability);
|
||||
availCount += 1;
|
||||
hasKpi = true;
|
||||
}
|
||||
if (k?.performance != null) {
|
||||
perfSum += Number(k.performance);
|
||||
perfCount += 1;
|
||||
hasKpi = true;
|
||||
}
|
||||
if (k?.quality != null) {
|
||||
qualSum += Number(k.quality);
|
||||
qualCount += 1;
|
||||
hasKpi = true;
|
||||
}
|
||||
if (k?.good != null) {
|
||||
goodSum += Number(k.good);
|
||||
hasKpi = true;
|
||||
}
|
||||
if (k?.scrap != null) {
|
||||
scrapSum += Number(k.scrap);
|
||||
hasKpi = true;
|
||||
}
|
||||
if (k?.target != null) {
|
||||
targetSum += Number(k.target);
|
||||
hasKpi = true;
|
||||
}
|
||||
}
|
||||
|
||||
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: hasKpi ? goodSum : null,
|
||||
scrapSum: hasKpi ? scrapSum : null,
|
||||
targetSum: hasKpi ? targetSum : null,
|
||||
};
|
||||
}, [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]);
|
||||
|
||||
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="mb-4 rounded-2xl border border-white/10 bg-black/40 p-4">
|
||||
<div className="flex flex-col gap-3 sm:flex-row sm:items-center sm:justify-between">
|
||||
<div>
|
||||
<div className="text-sm font-semibold text-white">{t("overview.recap.title")}</div>
|
||||
<div className="text-xs text-zinc-400">{t("overview.recap.subtitle")}</div>
|
||||
</div>
|
||||
<Link
|
||||
href="/recap"
|
||||
className="rounded-xl border border-emerald-500/30 bg-emerald-500/10 px-3 py-2 text-sm text-emerald-300 hover:bg-emerald-500/20"
|
||||
>
|
||||
{t("overview.recap.cta")}
|
||||
</Link>
|
||||
</div>
|
||||
</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>
|
||||
|
||||
<Suspense fallback={<OverviewTimelineSkeleton />}>
|
||||
<OverviewTimeline events={events} eventsLoading={eventsLoading} locale={locale} t={t} />
|
||||
</Suspense>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
129
app/(app)/overview/OverviewTimeline.tsx
Normal file
129
app/(app)/overview/OverviewTimeline.tsx
Normal file
@@ -0,0 +1,129 @@
|
||||
"use client";
|
||||
|
||||
import type { EventRow } from "./types";
|
||||
|
||||
type Translator = (key: string, vars?: Record<string, string | number>) => string;
|
||||
|
||||
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 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";
|
||||
}
|
||||
|
||||
function formatEventType(eventType: string | undefined, t: Translator) {
|
||||
if (!eventType) return "";
|
||||
const key = `overview.event.${eventType}`;
|
||||
const label = t(key);
|
||||
return label === key ? eventType : label;
|
||||
}
|
||||
|
||||
function formatSource(source: string | undefined, t: Translator) {
|
||||
if (!source) return "";
|
||||
const key = `overview.source.${source}`;
|
||||
const label = t(key);
|
||||
return label === key ? source : label;
|
||||
}
|
||||
|
||||
function formatSeverity(severity: string | undefined, t: Translator) {
|
||||
if (!severity) return "";
|
||||
const key = `overview.severity.${severity}`;
|
||||
const label = t(key);
|
||||
return label === key ? severity.toUpperCase() : label;
|
||||
}
|
||||
|
||||
export default function OverviewTimeline({
|
||||
events,
|
||||
eventsLoading,
|
||||
locale,
|
||||
t,
|
||||
}: {
|
||||
events: EventRow[];
|
||||
eventsLoading: boolean;
|
||||
locale: string;
|
||||
t: Translator;
|
||||
}) {
|
||||
if (eventsLoading && events.length === 0) {
|
||||
return (
|
||||
<div className="rounded-2xl border border-white/10 bg-white/5 p-5 xl:col-span-2 animate-pulse">
|
||||
<div className="mb-3 flex items-center justify-between">
|
||||
<div className="h-4 w-32 rounded bg-white/10" />
|
||||
<div className="h-3 w-20 rounded bg-white/5" />
|
||||
</div>
|
||||
<div className="space-y-3">
|
||||
{Array.from({ length: 4 }).map((_, idx) => (
|
||||
<div key={idx} className="h-20 rounded-xl border border-white/10 bg-black/20" />
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
return (
|
||||
<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, t)}
|
||||
</span>
|
||||
<span className="rounded-full bg-white/10 px-2 py-0.5 text-xs text-zinc-200">
|
||||
{formatEventType(e.eventType, t)}
|
||||
</span>
|
||||
<span className={`rounded-full px-2 py-0.5 text-xs ${sourceClass(e.source)}`}>
|
||||
{formatSource(e.source, t)}
|
||||
</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>
|
||||
);
|
||||
}
|
||||
30
app/(app)/overview/loading.tsx
Normal file
30
app/(app)/overview/loading.tsx
Normal file
@@ -0,0 +1,30 @@
|
||||
export default function OverviewLoading() {
|
||||
return (
|
||||
<div className="p-4 sm:p-6 space-y-6 animate-pulse">
|
||||
<div className="flex flex-col gap-3 sm:flex-row sm:items-center sm:justify-between">
|
||||
<div className="space-y-2">
|
||||
<div className="h-6 w-40 rounded-lg bg-white/10" />
|
||||
<div className="h-4 w-64 rounded-lg bg-white/5" />
|
||||
</div>
|
||||
<div className="h-9 w-40 rounded-xl border border-white/10 bg-white/5" />
|
||||
</div>
|
||||
|
||||
<div className="grid grid-cols-1 gap-4 xl:grid-cols-3">
|
||||
{Array.from({ length: 3 }).map((_, idx) => (
|
||||
<div key={idx} className="h-36 rounded-2xl border border-white/10 bg-white/5" />
|
||||
))}
|
||||
</div>
|
||||
|
||||
<div className="grid grid-cols-1 gap-4 md:grid-cols-2 xl: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 grid-cols-1 gap-4 xl:grid-cols-3">
|
||||
<div className="h-64 rounded-2xl border border-white/10 bg-white/5 xl:col-span-1" />
|
||||
<div className="h-64 rounded-2xl border border-white/10 bg-white/5 xl:col-span-2" />
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
55
app/(app)/overview/page.tsx
Normal file
55
app/(app)/overview/page.tsx
Normal file
@@ -0,0 +1,55 @@
|
||||
import { redirect } from "next/navigation";
|
||||
import { requireSession } from "@/lib/auth/requireSession";
|
||||
import { getOverviewSummary } from "@/lib/overview/getOverviewSummary";
|
||||
import type { getOverviewData } from "@/lib/overview/getOverviewData";
|
||||
import { logLine } from "@/lib/logger";
|
||||
import OverviewClient from "./OverviewClient";
|
||||
|
||||
function toIso(value?: Date | null) {
|
||||
return value ? value.toISOString() : null;
|
||||
}
|
||||
|
||||
export default async function OverviewPage() {
|
||||
const session = await requireSession();
|
||||
if (!session) redirect("/login?next=/overview");
|
||||
|
||||
let machines: Awaited<ReturnType<typeof getOverviewData>>["machines"];
|
||||
let events: Awaited<ReturnType<typeof getOverviewData>>["events"] = [];
|
||||
try {
|
||||
const data = await getOverviewSummary({ orgId: session.orgId });
|
||||
machines = data.machines;
|
||||
} catch (err) {
|
||||
const message = err instanceof Error ? err.message : String(err);
|
||||
const stack = err instanceof Error ? err.stack : undefined;
|
||||
logLine("OverviewPage.getOverviewSummary.error", { message, stack });
|
||||
console.error("[OverviewPage] getOverviewSummary:", err);
|
||||
machines = [];
|
||||
}
|
||||
|
||||
const initialMachines = machines.map((machine) => ({
|
||||
...machine,
|
||||
createdAt: toIso(machine.createdAt),
|
||||
updatedAt: toIso(machine.updatedAt),
|
||||
latestHeartbeat: machine.latestHeartbeat
|
||||
? {
|
||||
...machine.latestHeartbeat,
|
||||
ts: toIso(machine.latestHeartbeat.ts) ?? "",
|
||||
tsServer: toIso(machine.latestHeartbeat.tsServer),
|
||||
}
|
||||
: null,
|
||||
latestKpi: machine.latestKpi
|
||||
? {
|
||||
...machine.latestKpi,
|
||||
ts: toIso(machine.latestKpi.ts) ?? "",
|
||||
}
|
||||
: null,
|
||||
}));
|
||||
|
||||
const initialEvents = events.map((event) => ({
|
||||
...event,
|
||||
ts: event.ts ? event.ts.toISOString() : "",
|
||||
machineName: event.machineName ?? undefined,
|
||||
}));
|
||||
|
||||
return <OverviewClient initialMachines={initialMachines} initialEvents={initialEvents} />;
|
||||
}
|
||||
45
app/(app)/overview/types.ts
Normal file
45
app/(app)/overview/types.ts
Normal file
@@ -0,0 +1,45 @@
|
||||
export type Heartbeat = {
|
||||
ts: string;
|
||||
tsServer?: string | null;
|
||||
status: string;
|
||||
message?: string | null;
|
||||
ip?: string | null;
|
||||
fwVersion?: string | null;
|
||||
};
|
||||
|
||||
export 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;
|
||||
};
|
||||
|
||||
export type MachineRow = {
|
||||
id: string;
|
||||
name: string;
|
||||
code?: string | null;
|
||||
location?: string | null;
|
||||
latestHeartbeat: Heartbeat | null;
|
||||
latestKpi?: Kpi | null;
|
||||
};
|
||||
|
||||
export 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";
|
||||
};
|
||||
153
app/(app)/recap/RecapGridClient.tsx
Normal file
153
app/(app)/recap/RecapGridClient.tsx
Normal file
@@ -0,0 +1,153 @@
|
||||
"use client";
|
||||
|
||||
import { useEffect, useMemo, useState } from "react";
|
||||
import { useI18n } from "@/lib/i18n/useI18n";
|
||||
import type { RecapMachineStatus, RecapSummaryResponse } from "@/lib/recap/types";
|
||||
import RecapMachineCard from "@/components/recap/RecapMachineCard";
|
||||
|
||||
type Props = {
|
||||
initialData: RecapSummaryResponse;
|
||||
};
|
||||
|
||||
function statusLabel(status: RecapMachineStatus, t: (key: string) => string) {
|
||||
if (status === "running") return t("recap.status.running");
|
||||
if (status === "mold-change") return t("recap.status.moldChange");
|
||||
if (status === "stopped") return t("recap.status.stopped");
|
||||
return t("recap.status.offline");
|
||||
}
|
||||
|
||||
export default function RecapGridClient({ initialData }: Props) {
|
||||
const { t } = useI18n();
|
||||
|
||||
const [data, setData] = useState<RecapSummaryResponse>(initialData);
|
||||
const [loading, setLoading] = useState(false);
|
||||
const [locationFilter, setLocationFilter] = useState("all");
|
||||
const [statusFilter, setStatusFilter] = useState<"all" | RecapMachineStatus>("all");
|
||||
const [nowMs, setNowMs] = useState(() => Date.now());
|
||||
|
||||
useEffect(() => {
|
||||
const timer = window.setInterval(() => setNowMs(Date.now()), 1000);
|
||||
return () => window.clearInterval(timer);
|
||||
}, []);
|
||||
|
||||
useEffect(() => {
|
||||
let alive = true;
|
||||
|
||||
async function refresh() {
|
||||
setLoading(true);
|
||||
try {
|
||||
const res = await fetch(`/api/recap/summary?hours=${data.range.hours}`, { cache: "no-store" });
|
||||
const json = await res.json().catch(() => null);
|
||||
if (!alive || !json || !res.ok) return;
|
||||
setData(json as RecapSummaryResponse);
|
||||
} finally {
|
||||
if (alive) setLoading(false);
|
||||
}
|
||||
}
|
||||
|
||||
const onFocus = () => {
|
||||
void refresh();
|
||||
};
|
||||
|
||||
const interval = window.setInterval(onFocus, 60000);
|
||||
window.addEventListener("focus", onFocus);
|
||||
|
||||
return () => {
|
||||
alive = false;
|
||||
window.clearInterval(interval);
|
||||
window.removeEventListener("focus", onFocus);
|
||||
};
|
||||
}, [data.range.hours]);
|
||||
|
||||
const locationOptions = useMemo(() => {
|
||||
const set = new Set<string>();
|
||||
for (const machine of data.machines) {
|
||||
if (machine.location) set.add(machine.location);
|
||||
}
|
||||
return [...set].sort((a, b) => a.localeCompare(b));
|
||||
}, [data.machines]);
|
||||
|
||||
const filteredMachines = useMemo(() => {
|
||||
return data.machines.filter((machine) => {
|
||||
if (locationFilter !== "all" && machine.location !== locationFilter) return false;
|
||||
if (statusFilter !== "all" && machine.status !== statusFilter) return false;
|
||||
return true;
|
||||
});
|
||||
}, [data.machines, locationFilter, statusFilter]);
|
||||
|
||||
const generatedAtMs = new Date(data.generatedAt).getTime();
|
||||
const freshAgeSec = Number.isFinite(generatedAtMs) ? Math.max(0, Math.floor((nowMs - generatedAtMs) / 1000)) : null;
|
||||
|
||||
return (
|
||||
<div className="p-4 sm:p-6">
|
||||
<div className="mb-4 rounded-2xl border border-white/10 bg-black/40 p-4">
|
||||
<div className="flex flex-col gap-4 lg:flex-row lg:items-center lg:justify-between">
|
||||
<div>
|
||||
<h1 className="text-2xl font-semibold text-white">{t("recap.grid.title")}</h1>
|
||||
<p className="text-sm text-zinc-400">{t("recap.grid.subtitle")}</p>
|
||||
{freshAgeSec != null ? (
|
||||
<p className="mt-1 text-xs text-zinc-500">{t("recap.grid.updatedAgo", { sec: freshAgeSec })}</p>
|
||||
) : null}
|
||||
</div>
|
||||
|
||||
<div className="flex flex-wrap gap-2 text-sm">
|
||||
<select
|
||||
value={locationFilter}
|
||||
onChange={(event) => setLocationFilter(event.target.value)}
|
||||
className="rounded-xl border border-white/10 bg-black/40 px-3 py-2 text-zinc-200"
|
||||
>
|
||||
<option value="all">{t("recap.filter.allLocations")}</option>
|
||||
{locationOptions.map((location) => (
|
||||
<option key={location} value={location}>
|
||||
{location}
|
||||
</option>
|
||||
))}
|
||||
</select>
|
||||
|
||||
<select
|
||||
value={statusFilter}
|
||||
onChange={(event) => setStatusFilter(event.target.value as "all" | RecapMachineStatus)}
|
||||
className="rounded-xl border border-white/10 bg-black/40 px-3 py-2 text-zinc-200"
|
||||
>
|
||||
<option value="all">{t("recap.filter.allStatuses")}</option>
|
||||
{(["running", "mold-change", "stopped", "offline"] as const).map((status) => (
|
||||
<option key={status} value={status}>
|
||||
{statusLabel(status, t)}
|
||||
</option>
|
||||
))}
|
||||
</select>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{loading && data.machines.length === 0 ? (
|
||||
<div className="grid grid-cols-1 gap-4 sm:grid-cols-2 xl:grid-cols-3">
|
||||
{Array.from({ length: 6 }).map((_, idx) => (
|
||||
<div key={idx} className="h-[220px] animate-pulse rounded-2xl border border-white/10 bg-white/5" />
|
||||
))}
|
||||
</div>
|
||||
) : null}
|
||||
|
||||
{loading && data.machines.length > 0 ? (
|
||||
<div className="mb-3 text-xs text-zinc-500">{t("common.loading")}</div>
|
||||
) : null}
|
||||
|
||||
{filteredMachines.length === 0 ? (
|
||||
<div className="rounded-2xl border border-white/10 bg-black/30 p-4 text-sm text-zinc-400">
|
||||
{t("recap.grid.empty")}
|
||||
</div>
|
||||
) : (
|
||||
<div className="grid grid-cols-1 gap-4 sm:grid-cols-2 xl:grid-cols-3">
|
||||
{filteredMachines.map((machine) => (
|
||||
<RecapMachineCard
|
||||
key={machine.machineId}
|
||||
machine={machine}
|
||||
rangeStart={data.range.start}
|
||||
rangeEnd={data.range.end}
|
||||
/>
|
||||
))}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
30
app/(app)/recap/RecapPageSkeletons.tsx
Normal file
30
app/(app)/recap/RecapPageSkeletons.tsx
Normal file
@@ -0,0 +1,30 @@
|
||||
/**
|
||||
* Shared markup for loading states (used by `loading.tsx` and explicit `<Suspense>` in pages)
|
||||
* so the recap UI always shows the same skeleton while server data is pending.
|
||||
*/
|
||||
export function RecapGridPageSkeleton() {
|
||||
return (
|
||||
<div className="p-4 sm:p-6">
|
||||
<div className="mb-4 h-24 animate-pulse rounded-2xl border border-white/10 bg-black/40" />
|
||||
<div className="grid grid-cols-1 gap-4 sm:grid-cols-2 xl:grid-cols-3">
|
||||
{Array.from({ length: 6 }).map((_, index) => (
|
||||
<div key={index} className="h-[220px] animate-pulse rounded-2xl border border-white/10 bg-white/5" />
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
export function RecapDetailPageSkeleton() {
|
||||
return (
|
||||
<div className="p-4 sm:p-6">
|
||||
<div className="h-16 animate-pulse rounded-2xl border border-white/10 bg-black/40" />
|
||||
<div className="mt-4 grid grid-cols-1 gap-3 sm:grid-cols-2 xl:grid-cols-4">
|
||||
{Array.from({ length: 4 }).map((_, index) => (
|
||||
<div key={index} className="h-24 animate-pulse rounded-2xl border border-white/10 bg-black/30" />
|
||||
))}
|
||||
</div>
|
||||
<div className="mt-4 h-48 animate-pulse rounded-2xl border border-white/10 bg-black/30" />
|
||||
</div>
|
||||
);
|
||||
}
|
||||
240
app/(app)/recap/[machineId]/RecapDetailClient.tsx
Normal file
240
app/(app)/recap/[machineId]/RecapDetailClient.tsx
Normal file
@@ -0,0 +1,240 @@
|
||||
"use client";
|
||||
|
||||
import Link from "next/link";
|
||||
import { useEffect, useState, useTransition } from "react";
|
||||
import { usePathname, useRouter, useSearchParams } from "next/navigation";
|
||||
import { useI18n } from "@/lib/i18n/useI18n";
|
||||
import type { RecapDetailResponse, RecapRangeMode, RecapTimelineResponse } from "@/lib/recap/types";
|
||||
import RecapBanners from "@/components/recap/RecapBanners";
|
||||
import RecapKpiRow from "@/components/recap/RecapKpiRow";
|
||||
import RecapProductionBySku from "@/components/recap/RecapProductionBySku";
|
||||
import RecapDowntimeTop from "@/components/recap/RecapDowntimeTop";
|
||||
import RecapWorkOrders from "@/components/recap/RecapWorkOrders";
|
||||
import RecapMachineStatus from "@/components/recap/RecapMachineStatus";
|
||||
import RecapFullTimeline from "@/components/recap/RecapFullTimeline";
|
||||
|
||||
type Props = {
|
||||
machineId: string;
|
||||
initialData: RecapDetailResponse;
|
||||
};
|
||||
|
||||
function toInputDate(value: string) {
|
||||
const d = new Date(value);
|
||||
const pad = (n: number) => String(n).padStart(2, "0");
|
||||
return `${d.getFullYear()}-${pad(d.getMonth() + 1)}-${pad(d.getDate())}T${pad(d.getHours())}:${pad(d.getMinutes())}`;
|
||||
}
|
||||
|
||||
function normalizeInputDate(value: string) {
|
||||
const d = new Date(value);
|
||||
if (!Number.isFinite(d.getTime())) return null;
|
||||
return d.toISOString();
|
||||
}
|
||||
|
||||
export default function RecapDetailClient({ machineId, initialData }: Props) {
|
||||
const { t, locale } = useI18n();
|
||||
const router = useRouter();
|
||||
const pathname = usePathname();
|
||||
const searchParams = useSearchParams();
|
||||
const [isPending, startTransition] = useTransition();
|
||||
const [timeline, setTimeline] = useState<RecapTimelineResponse | null>(null);
|
||||
const [timelineLoading, setTimelineLoading] = useState(true);
|
||||
const [nowMs, setNowMs] = useState(() => Date.now());
|
||||
|
||||
const [customStart, setCustomStart] = useState(toInputDate(initialData.range.start));
|
||||
const [customEnd, setCustomEnd] = useState(toInputDate(initialData.range.end));
|
||||
|
||||
const requestedRange =
|
||||
(searchParams.get("range") as RecapRangeMode | null) ?? initialData.range.requestedMode ?? initialData.range.mode;
|
||||
const selectedRange = requestedRange;
|
||||
const shiftAvailable = initialData.range.shiftAvailable ?? true;
|
||||
const shiftFallbackReason = initialData.range.fallbackReason;
|
||||
const shiftFallbackActive = selectedRange === "shift" && initialData.range.mode !== "shift";
|
||||
|
||||
function pushRange(nextRange: RecapRangeMode, start?: string, end?: string) {
|
||||
const params = new URLSearchParams(searchParams.toString());
|
||||
params.set("range", nextRange);
|
||||
|
||||
if (nextRange === "custom" && start && end) {
|
||||
params.set("start", start);
|
||||
params.set("end", end);
|
||||
} else {
|
||||
params.delete("start");
|
||||
params.delete("end");
|
||||
}
|
||||
|
||||
startTransition(() => {
|
||||
router.push(`${pathname}?${params.toString()}`);
|
||||
});
|
||||
}
|
||||
|
||||
function applyCustomRange() {
|
||||
const start = normalizeInputDate(customStart);
|
||||
const end = normalizeInputDate(customEnd);
|
||||
if (!start || !end || end <= start) return;
|
||||
pushRange("custom", start, end);
|
||||
}
|
||||
|
||||
const machine = initialData.machine;
|
||||
const generatedAtMs = new Date(initialData.generatedAt).getTime();
|
||||
const freshAgeSec = Number.isFinite(generatedAtMs) ? Math.max(0, Math.floor((nowMs - generatedAtMs) / 1000)) : null;
|
||||
const timelineStart = timeline?.range.start ?? initialData.range.start;
|
||||
const timelineEnd = timeline?.range.end ?? initialData.range.end;
|
||||
const timelineSegments = timeline?.segments ?? [];
|
||||
const timelineHasData = timeline?.hasData ?? false;
|
||||
|
||||
useEffect(() => {
|
||||
let alive = true;
|
||||
setTimeline(null);
|
||||
setTimelineLoading(true);
|
||||
|
||||
async function loadTimeline() {
|
||||
try {
|
||||
const params = new URLSearchParams({
|
||||
start: initialData.range.start,
|
||||
end: initialData.range.end,
|
||||
});
|
||||
const res = await fetch(`/api/recap/${machineId}/timeline?${params.toString()}`, { cache: "no-store" });
|
||||
const json = await res.json().catch(() => null);
|
||||
if (!alive || !res.ok || !json) return;
|
||||
setTimeline(json as RecapTimelineResponse);
|
||||
} catch {
|
||||
} finally {
|
||||
if (alive) setTimelineLoading(false);
|
||||
}
|
||||
}
|
||||
|
||||
void loadTimeline();
|
||||
return () => {
|
||||
alive = false;
|
||||
};
|
||||
}, [initialData.range.end, initialData.range.start, machineId]);
|
||||
|
||||
useEffect(() => {
|
||||
const timer = window.setInterval(() => setNowMs(Date.now()), 1000);
|
||||
return () => window.clearInterval(timer);
|
||||
}, []);
|
||||
|
||||
return (
|
||||
<div className="p-4 sm:p-6">
|
||||
<div className="mb-4 flex flex-col gap-3 lg:flex-row lg:items-start lg:justify-between">
|
||||
<div>
|
||||
<Link href="/recap" className="text-sm text-zinc-400 hover:text-zinc-200">
|
||||
{`← ${t("recap.detail.back")}`}
|
||||
</Link>
|
||||
<h1 className="mt-1 text-2xl font-semibold text-white">{machine.name || machineId}</h1>
|
||||
<div className="text-sm text-zinc-400">{machine.location || t("common.na")}</div>
|
||||
{freshAgeSec != null ? (
|
||||
<div className="mt-1 text-xs text-zinc-500">{t("recap.grid.updatedAgo", { sec: freshAgeSec })}</div>
|
||||
) : null}
|
||||
</div>
|
||||
|
||||
<div className="flex flex-wrap gap-2 text-sm">
|
||||
{(["24h", "shift", "yesterday", "custom"] as const).map((range) => (
|
||||
<button
|
||||
key={range}
|
||||
type="button"
|
||||
disabled={range === "shift" && !shiftAvailable}
|
||||
onClick={() => {
|
||||
if (range === "shift" && !shiftAvailable) return;
|
||||
if (range === "custom") {
|
||||
pushRange("custom", normalizeInputDate(customStart) ?? undefined, normalizeInputDate(customEnd) ?? undefined);
|
||||
return;
|
||||
}
|
||||
pushRange(range);
|
||||
}}
|
||||
className={`rounded-xl border px-3 py-2 ${
|
||||
selectedRange === range
|
||||
? "border-emerald-300/60 bg-emerald-500/20 text-emerald-100"
|
||||
: "border-white/10 bg-black/40 text-zinc-200"
|
||||
} ${range === "shift" && !shiftAvailable ? "cursor-not-allowed opacity-60" : ""}`}
|
||||
>
|
||||
{range === "24h" ? t("recap.range.24h") : null}
|
||||
{range === "shift" ? t("recap.range.shiftCurrent") : null}
|
||||
{range === "yesterday" ? t("recap.range.yesterday") : null}
|
||||
{range === "custom" ? t("recap.range.custom") : null}
|
||||
</button>
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{!shiftAvailable ? (
|
||||
<div className="mb-4 rounded-xl border border-amber-400/40 bg-amber-400/10 px-3 py-2 text-xs text-amber-100">
|
||||
{t("recap.range.shiftUnavailable")}
|
||||
</div>
|
||||
) : null}
|
||||
|
||||
{shiftFallbackActive ? (
|
||||
<div className="mb-4 rounded-xl border border-amber-400/40 bg-amber-400/10 px-3 py-2 text-xs text-amber-100">
|
||||
{shiftFallbackReason === "shift-inactive" ? t("recap.range.shiftFallbackInactive") : t("recap.range.shiftFallbackUnavailable")}
|
||||
</div>
|
||||
) : null}
|
||||
|
||||
{selectedRange === "custom" ? (
|
||||
<div className="mb-4 flex flex-wrap gap-2 text-sm">
|
||||
<input
|
||||
type="datetime-local"
|
||||
value={customStart}
|
||||
onChange={(event) => setCustomStart(event.target.value)}
|
||||
className="rounded-xl border border-white/10 bg-black/40 px-3 py-2 text-zinc-200"
|
||||
/>
|
||||
<input
|
||||
type="datetime-local"
|
||||
value={customEnd}
|
||||
onChange={(event) => setCustomEnd(event.target.value)}
|
||||
className="rounded-xl border border-white/10 bg-black/40 px-3 py-2 text-zinc-200"
|
||||
/>
|
||||
<button
|
||||
type="button"
|
||||
onClick={applyCustomRange}
|
||||
className="rounded-xl border border-emerald-300/50 bg-emerald-500/20 px-3 py-2 text-emerald-100"
|
||||
>
|
||||
{t("recap.range.apply")}
|
||||
</button>
|
||||
</div>
|
||||
) : null}
|
||||
|
||||
{isPending ? <div className="mb-3 text-xs text-zinc-500">{t("common.loading")}</div> : null}
|
||||
|
||||
<div className="mb-4">
|
||||
<RecapBanners
|
||||
moldChangeStartMs={machine.moldChange?.active ? machine.moldChange.startMs : null}
|
||||
offlineForMin={machine.offlineForMin}
|
||||
ongoingStopMin={machine.ongoingStopMin}
|
||||
/>
|
||||
</div>
|
||||
|
||||
<RecapKpiRow
|
||||
oeeAvg={machine.oee}
|
||||
goodParts={machine.goodParts}
|
||||
totalStops={Math.round(machine.stopMinutes)}
|
||||
scrapParts={machine.scrap}
|
||||
rangeMode={initialData.range.mode}
|
||||
/>
|
||||
|
||||
<div className="mt-4">
|
||||
<RecapFullTimeline
|
||||
rangeStart={timelineStart}
|
||||
rangeEnd={timelineEnd}
|
||||
segments={timelineSegments}
|
||||
hasData={timelineHasData}
|
||||
loading={timelineLoading}
|
||||
locale={locale}
|
||||
rangeMode={initialData.range.mode}
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div className="mt-4 grid grid-cols-1 gap-4 xl:grid-cols-2">
|
||||
<RecapProductionBySku rows={machine.productionBySku} />
|
||||
<RecapDowntimeTop rows={machine.downtimeTop} />
|
||||
</div>
|
||||
|
||||
<div className="mt-4">
|
||||
<RecapWorkOrders workOrders={machine.workOrders} />
|
||||
</div>
|
||||
|
||||
<div className="mt-4">
|
||||
<RecapMachineStatus heartbeat={machine.heartbeat} />
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
5
app/(app)/recap/[machineId]/loading.tsx
Normal file
5
app/(app)/recap/[machineId]/loading.tsx
Normal file
@@ -0,0 +1,5 @@
|
||||
import { RecapDetailPageSkeleton } from "../RecapPageSkeletons";
|
||||
|
||||
export default function LoadingRecapDetail() {
|
||||
return <RecapDetailPageSkeleton />;
|
||||
}
|
||||
51
app/(app)/recap/[machineId]/page.tsx
Normal file
51
app/(app)/recap/[machineId]/page.tsx
Normal file
@@ -0,0 +1,51 @@
|
||||
import { Suspense } from "react";
|
||||
import { notFound, redirect } from "next/navigation";
|
||||
import { requireSession } from "@/lib/auth/requireSession";
|
||||
import { getRecapMachineDetailCached, parseRecapDetailRangeInput } from "@/lib/recap/redesign";
|
||||
import { RecapDetailPageSkeleton } from "../RecapPageSkeletons";
|
||||
import RecapDetailClient from "./RecapDetailClient";
|
||||
|
||||
async function RecapDetailData({
|
||||
params,
|
||||
searchParams,
|
||||
}: {
|
||||
params: Promise<{ machineId: string }>;
|
||||
searchParams?: Promise<Record<string, string | string[] | undefined>>;
|
||||
}) {
|
||||
const session = await requireSession();
|
||||
const { machineId } = await params;
|
||||
if (!session) redirect(`/login?next=/recap/${machineId}`);
|
||||
|
||||
const rawSearchParams = (await searchParams) ?? {};
|
||||
const input = parseRecapDetailRangeInput(rawSearchParams);
|
||||
|
||||
const initialData = await getRecapMachineDetailCached({
|
||||
orgId: session.orgId,
|
||||
machineId,
|
||||
input,
|
||||
});
|
||||
|
||||
if (!initialData) notFound();
|
||||
|
||||
return (
|
||||
<RecapDetailClient
|
||||
key={`${machineId}:${initialData.range.mode}:${initialData.range.start}:${initialData.range.end}`}
|
||||
machineId={machineId}
|
||||
initialData={initialData}
|
||||
/>
|
||||
);
|
||||
}
|
||||
|
||||
export default function RecapMachineDetailPage({
|
||||
params,
|
||||
searchParams,
|
||||
}: {
|
||||
params: Promise<{ machineId: string }>;
|
||||
searchParams?: Promise<Record<string, string | string[] | undefined>>;
|
||||
}) {
|
||||
return (
|
||||
<Suspense fallback={<RecapDetailPageSkeleton />}>
|
||||
<RecapDetailData params={params} searchParams={searchParams} />
|
||||
</Suspense>
|
||||
);
|
||||
}
|
||||
5
app/(app)/recap/loading.tsx
Normal file
5
app/(app)/recap/loading.tsx
Normal file
@@ -0,0 +1,5 @@
|
||||
import { RecapGridPageSkeleton } from "./RecapPageSkeletons";
|
||||
|
||||
export default function LoadingRecapGrid() {
|
||||
return <RecapGridPageSkeleton />;
|
||||
}
|
||||
26
app/(app)/recap/page.tsx
Normal file
26
app/(app)/recap/page.tsx
Normal file
@@ -0,0 +1,26 @@
|
||||
import { Suspense } from "react";
|
||||
import { redirect } from "next/navigation";
|
||||
import { requireSession } from "@/lib/auth/requireSession";
|
||||
import { getRecapSummaryCached } from "@/lib/recap/redesign";
|
||||
import RecapGridClient from "./RecapGridClient";
|
||||
import { RecapGridPageSkeleton } from "./RecapPageSkeletons";
|
||||
|
||||
async function RecapGridData() {
|
||||
const session = await requireSession();
|
||||
if (!session) redirect("/login?next=/recap");
|
||||
|
||||
const initialData = await getRecapSummaryCached({
|
||||
orgId: session.orgId,
|
||||
hours: 24,
|
||||
});
|
||||
|
||||
return <RecapGridClient initialData={initialData} />;
|
||||
}
|
||||
|
||||
export default function RecapPage() {
|
||||
return (
|
||||
<Suspense fallback={<RecapGridPageSkeleton />}>
|
||||
<RecapGridData />
|
||||
</Suspense>
|
||||
);
|
||||
}
|
||||
256
app/(app)/reports/ReportsCharts.tsx
Normal file
256
app/(app)/reports/ReportsCharts.tsx
Normal file
@@ -0,0 +1,256 @@
|
||||
"use client";
|
||||
|
||||
import {
|
||||
Bar,
|
||||
BarChart,
|
||||
CartesianGrid,
|
||||
Cell,
|
||||
Line,
|
||||
LineChart,
|
||||
ResponsiveContainer,
|
||||
Tooltip,
|
||||
XAxis,
|
||||
YAxis,
|
||||
} from "recharts";
|
||||
|
||||
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 ChartPoint = { ts: string; label: string; value: number | null };
|
||||
type CycleHistogramRow = {
|
||||
label: string;
|
||||
count: number;
|
||||
rangeStart?: number;
|
||||
rangeEnd?: number;
|
||||
overflow?: "low" | "high";
|
||||
minValue?: number;
|
||||
maxValue?: number;
|
||||
};
|
||||
|
||||
function CycleTooltip({ active, payload, t }: SimpleTooltipProps<CycleHistogramRow> & { t: Translator }) {
|
||||
if (!active || !payload?.length) return null;
|
||||
const p = payload[0]?.payload;
|
||||
if (!p) return null;
|
||||
|
||||
let detail = "";
|
||||
if (p.overflow === "low") {
|
||||
detail = `${t("reports.tooltip.below")} ${p.rangeEnd?.toFixed(1)}s`;
|
||||
} else if (p.overflow === "high") {
|
||||
detail = `${t("reports.tooltip.above")} ${p.rangeStart?.toFixed(1)}s`;
|
||||
} else if (p.rangeStart != null && p.rangeEnd != null) {
|
||||
detail = `${p.rangeStart.toFixed(1)}s - ${p.rangeEnd.toFixed(1)}s`;
|
||||
}
|
||||
|
||||
const extreme =
|
||||
p.overflow && (p.minValue != null || p.maxValue != null)
|
||||
? `${t("reports.tooltip.extremes")}: ${p.minValue?.toFixed(1) ?? "--"}s - ${p.maxValue?.toFixed(1) ?? "--"}s`
|
||||
: "";
|
||||
|
||||
return (
|
||||
<div className="rounded-xl border border-white/10 bg-zinc-950/95 px-4 py-3 shadow-lg">
|
||||
<div className="text-sm font-semibold text-white">{p.label}</div>
|
||||
<div className="mt-2 space-y-1 text-xs text-zinc-300">
|
||||
<div>
|
||||
{t("reports.tooltip.cycles")}: <span className="text-white">{p.count}</span>
|
||||
</div>
|
||||
{detail ? (
|
||||
<div>
|
||||
{t("reports.tooltip.range")}: <span className="text-white">{detail}</span>
|
||||
</div>
|
||||
) : null}
|
||||
{extreme ? <div className="text-zinc-400">{extreme}</div> : null}
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
function DowntimeTooltip({
|
||||
active,
|
||||
payload,
|
||||
t,
|
||||
}: SimpleTooltipProps<{ name?: string; value?: number }> & { t: Translator }) {
|
||||
if (!active || !payload?.length) return null;
|
||||
const row = payload[0]?.payload ?? {};
|
||||
const label = row.name ?? payload[0]?.name ?? "";
|
||||
const value = row.value ?? payload[0]?.value ?? 0;
|
||||
|
||||
return (
|
||||
<div className="rounded-xl border border-white/10 bg-zinc-950/95 px-4 py-3 shadow-lg">
|
||||
<div className="text-sm font-semibold text-white">{label}</div>
|
||||
<div className="mt-2 text-xs text-zinc-300">
|
||||
{t("reports.tooltip.downtime")}: <span className="text-white">{Number(value)} min</span>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
export default function ReportsCharts({
|
||||
oeeSeries,
|
||||
downtimeSeries,
|
||||
downtimeColors,
|
||||
cycleHistogram,
|
||||
scrapSeries,
|
||||
lossRows,
|
||||
locale,
|
||||
t,
|
||||
}: {
|
||||
oeeSeries: ChartPoint[];
|
||||
downtimeSeries: { name: string; value: number }[];
|
||||
downtimeColors: Record<string, string>;
|
||||
cycleHistogram: CycleHistogramRow[];
|
||||
scrapSeries: ChartPoint[];
|
||||
lossRows: Array<{ label: string; value: string }>;
|
||||
locale: string;
|
||||
t: Translator;
|
||||
}) {
|
||||
return (
|
||||
<>
|
||||
<div className="mt-6 grid grid-cols-1 gap-4 xl:grid-cols-2">
|
||||
<div className="rounded-2xl border border-white/10 bg-white/5 p-5">
|
||||
<div className="mb-2 text-sm font-semibold text-white">{t("reports.oeeTrend")}</div>
|
||||
<div className="h-[260px] rounded-2xl border border-white/10 bg-black/25 p-4">
|
||||
{oeeSeries.length ? (
|
||||
<ResponsiveContainer width="100%" height="100%" minHeight={200}>
|
||||
<LineChart data={oeeSeries}>
|
||||
<CartesianGrid strokeDasharray="3 3" stroke="var(--app-chart-grid)" />
|
||||
<XAxis dataKey="label" tick={{ fill: "var(--app-chart-tick)" }} />
|
||||
<YAxis domain={[0, 100]} tick={{ fill: "var(--app-chart-tick)" }} />
|
||||
<Tooltip
|
||||
contentStyle={{
|
||||
background: "var(--app-chart-tooltip-bg)",
|
||||
border: "1px solid var(--app-chart-tooltip-border)",
|
||||
}}
|
||||
labelStyle={{ color: "var(--app-chart-label)" }}
|
||||
labelFormatter={(_, payload) => {
|
||||
const row = payload?.[0]?.payload;
|
||||
return row?.ts ? new Date(row.ts).toLocaleString(locale) : "";
|
||||
}}
|
||||
formatter={(val: number | string | undefined) => [
|
||||
val == null ? "--" : `${Number(val).toFixed(1)}%`,
|
||||
"OEE",
|
||||
]}
|
||||
/>
|
||||
<Line
|
||||
type="linear"
|
||||
dataKey="value"
|
||||
stroke="#34d399"
|
||||
dot={false}
|
||||
strokeWidth={2}
|
||||
connectNulls={false}
|
||||
/>
|
||||
</LineChart>
|
||||
</ResponsiveContainer>
|
||||
) : (
|
||||
<div className="flex h-full items-center justify-center text-sm text-zinc-500">
|
||||
{t("reports.noTrend")}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="rounded-2xl border border-white/10 bg-white/5 p-5">
|
||||
<div className="mb-2 text-sm font-semibold text-white">{t("reports.downtimePareto")}</div>
|
||||
<div className="h-[260px] rounded-2xl border border-white/10 bg-black/25 p-4">
|
||||
{downtimeSeries.length ? (
|
||||
<ResponsiveContainer width="100%" height="100%" minHeight={200}>
|
||||
<BarChart data={downtimeSeries}>
|
||||
<CartesianGrid strokeDasharray="3 3" stroke="var(--app-chart-grid)" />
|
||||
<XAxis dataKey="name" tick={{ fill: "var(--app-chart-tick)" }} />
|
||||
<YAxis tick={{ fill: "var(--app-chart-tick)" }} />
|
||||
<Tooltip content={<DowntimeTooltip t={t} />} />
|
||||
<Bar dataKey="value" radius={[10, 10, 0, 0]} isAnimationActive={false}>
|
||||
{downtimeSeries.map((row, idx) => (
|
||||
<Cell key={`${row.name}-${idx}`} fill={downtimeColors[row.name] ?? "#94a3b8"} />
|
||||
))}
|
||||
</Bar>
|
||||
</BarChart>
|
||||
</ResponsiveContainer>
|
||||
) : (
|
||||
<div className="flex h-full items-center justify-center text-sm text-zinc-500">
|
||||
{t("reports.noTrend")}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="mt-6 grid grid-cols-1 gap-4 xl:grid-cols-3">
|
||||
<div className="rounded-2xl border border-white/10 bg-white/5 p-5">
|
||||
<div className="mb-2 text-sm font-semibold text-white">{t("reports.cycleDistribution")}</div>
|
||||
<div className="h-[220px] rounded-2xl border border-white/10 bg-black/25 p-4">
|
||||
{cycleHistogram.length ? (
|
||||
<ResponsiveContainer width="100%" height="100%" minHeight={200}>
|
||||
<BarChart data={cycleHistogram}>
|
||||
<CartesianGrid strokeDasharray="3 3" stroke="var(--app-chart-grid)" />
|
||||
<XAxis dataKey="label" tick={{ fill: "var(--app-chart-tick)", fontSize: 10 }} />
|
||||
<YAxis tick={{ fill: "var(--app-chart-tick)" }} />
|
||||
<Tooltip content={<CycleTooltip t={t} />} />
|
||||
<Bar dataKey="count" radius={[8, 8, 0, 0]} fill="#60a5fa" isAnimationActive={false} />
|
||||
</BarChart>
|
||||
</ResponsiveContainer>
|
||||
) : (
|
||||
<div className="flex h-full items-center justify-center text-sm text-zinc-500">
|
||||
{t("reports.noCycle")}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="rounded-2xl border border-white/10 bg-white/5 p-5">
|
||||
<div className="mb-2 text-sm font-semibold text-white">{t("reports.scrapTrend")}</div>
|
||||
<div className="h-[220px] rounded-2xl border border-white/10 bg-black/25 p-4">
|
||||
{scrapSeries.length ? (
|
||||
<ResponsiveContainer width="100%" height="100%" minHeight={200}>
|
||||
<LineChart data={scrapSeries}>
|
||||
<CartesianGrid strokeDasharray="3 3" stroke="var(--app-chart-grid)" />
|
||||
<XAxis dataKey="label" tick={{ fill: "var(--app-chart-tick)" }} />
|
||||
<YAxis domain={[0, 100]} tick={{ fill: "var(--app-chart-tick)" }} />
|
||||
<Tooltip
|
||||
contentStyle={{
|
||||
background: "var(--app-chart-tooltip-bg)",
|
||||
border: "1px solid var(--app-chart-tooltip-border)",
|
||||
}}
|
||||
labelStyle={{ color: "var(--app-chart-label)" }}
|
||||
labelFormatter={(_, payload) => {
|
||||
const row = payload?.[0]?.payload;
|
||||
return row?.ts ? new Date(row.ts).toLocaleString(locale) : "";
|
||||
}}
|
||||
formatter={(val: number | string | undefined) => [
|
||||
val == null ? "--" : `${Number(val).toFixed(1)}%`,
|
||||
t("reports.scrapRate"),
|
||||
]}
|
||||
/>
|
||||
<Line type="monotone" dataKey="value" stroke="#f97316" dot={false} strokeWidth={2} />
|
||||
</LineChart>
|
||||
</ResponsiveContainer>
|
||||
) : (
|
||||
<div className="flex h-full items-center justify-center text-sm text-zinc-500">
|
||||
{t("reports.noDowntime")}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="rounded-2xl border border-white/10 bg-white/5 p-5">
|
||||
<div className="mb-2 text-sm font-semibold text-white">{t("reports.topLossDrivers")}</div>
|
||||
<div className="space-y-3 text-sm text-zinc-300">
|
||||
{lossRows.map((row) => (
|
||||
<div
|
||||
key={row.label}
|
||||
className="flex items-center justify-between rounded-xl border border-white/10 bg-black/20 p-3"
|
||||
>
|
||||
<span>{row.label}</span>
|
||||
<span className="text-xs text-zinc-400">{row.value}</span>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</>
|
||||
);
|
||||
}
|
||||
706
app/(app)/reports/ReportsPageClient.tsx
Normal file
706
app/(app)/reports/ReportsPageClient.tsx
Normal file
@@ -0,0 +1,706 @@
|
||||
"use client";
|
||||
|
||||
import { Suspense, lazy, useEffect, useMemo, useState } from "react";
|
||||
import { useI18n } from "@/lib/i18n/useI18n";
|
||||
|
||||
const ReportsCharts = lazy(() => import("./ReportsCharts"));
|
||||
|
||||
type RangeKey = "24h" | "7d" | "30d" | "custom";
|
||||
|
||||
type ReportSummary = {
|
||||
oeeAvg: number | null;
|
||||
availabilityAvg: number | null;
|
||||
performanceAvg: number | null;
|
||||
qualityAvg: number | null;
|
||||
goodTotal: number | null;
|
||||
scrapTotal: number | null;
|
||||
targetTotal: number | null;
|
||||
scrapRate: number | null;
|
||||
topScrapSku?: string | null;
|
||||
topScrapWorkOrder?: string | null;
|
||||
};
|
||||
|
||||
type ReportDowntime = {
|
||||
macrostopSec: number;
|
||||
microstopSec: number;
|
||||
slowCycleCount: number;
|
||||
qualitySpikeCount: number;
|
||||
performanceDegradationCount: number;
|
||||
oeeDropCount: number;
|
||||
};
|
||||
|
||||
type ReportTrendPoint = { t: string; v: number | null };
|
||||
|
||||
type ReportPayload = {
|
||||
summary: ReportSummary;
|
||||
downtime: ReportDowntime;
|
||||
trend: {
|
||||
oee: ReportTrendPoint[];
|
||||
availability: ReportTrendPoint[];
|
||||
performance: ReportTrendPoint[];
|
||||
quality: ReportTrendPoint[];
|
||||
scrapRate: ReportTrendPoint[];
|
||||
};
|
||||
distribution: {
|
||||
cycleTime: {
|
||||
label: string;
|
||||
count: number;
|
||||
rangeStart?: number;
|
||||
rangeEnd?: number;
|
||||
overflow?: "low" | "high";
|
||||
minValue?: number;
|
||||
maxValue?: number;
|
||||
}[];
|
||||
};
|
||||
insights?: string[];
|
||||
};
|
||||
|
||||
type MachineOption = { id: string; name: string };
|
||||
type FilterOptions = { workOrders: string[]; skus: string[] };
|
||||
type Translator = (key: string, vars?: Record<string, string | number>) => string;
|
||||
|
||||
function fmtPct(v?: number | null) {
|
||||
if (v === null || v === undefined || Number.isNaN(v)) return "--";
|
||||
return `${v.toFixed(1)}%`;
|
||||
}
|
||||
|
||||
function fmtDuration(sec?: number | null) {
|
||||
if (!sec) return "--";
|
||||
const h = Math.floor(sec / 3600);
|
||||
const m = Math.floor((sec % 3600) / 60);
|
||||
if (h > 0) return `${h}h ${m}m`;
|
||||
return `${m}m`;
|
||||
}
|
||||
|
||||
function downsample<T>(rows: T[], max: number) {
|
||||
if (rows.length <= max) return rows;
|
||||
const step = Math.ceil(rows.length / max);
|
||||
return rows.filter((_, idx) => idx % step === 0);
|
||||
}
|
||||
|
||||
function downsampleTrendPreserveGaps(rows: ReportTrendPoint[], max: number) {
|
||||
if (rows.length <= max) return rows;
|
||||
const step = Math.ceil(rows.length / max);
|
||||
const picked = new Set<number>();
|
||||
|
||||
picked.add(0);
|
||||
picked.add(rows.length - 1);
|
||||
for (let idx = 0; idx < rows.length; idx += step) picked.add(idx);
|
||||
|
||||
// Keep both sides of null/non-null transitions so chart gaps remain visible.
|
||||
for (let idx = 1; idx < rows.length; idx += 1) {
|
||||
const prevIsNull = rows[idx - 1]?.v == null;
|
||||
const currIsNull = rows[idx]?.v == null;
|
||||
if (prevIsNull !== currIsNull) {
|
||||
picked.add(idx - 1);
|
||||
picked.add(idx);
|
||||
}
|
||||
}
|
||||
|
||||
return [...picked]
|
||||
.sort((a, b) => a - b)
|
||||
.map((idx) => rows[idx])
|
||||
.filter((row): row is ReportTrendPoint => row != null);
|
||||
}
|
||||
|
||||
function formatTickLabel(ts: string, range: RangeKey) {
|
||||
const d = new Date(ts);
|
||||
if (Number.isNaN(d.getTime())) return ts;
|
||||
const hh = String(d.getHours()).padStart(2, "0");
|
||||
const mm = String(d.getMinutes()).padStart(2, "0");
|
||||
const month = String(d.getMonth() + 1).padStart(2, "0");
|
||||
const day = String(d.getDate()).padStart(2, "0");
|
||||
if (range === "24h") return `${hh}:${mm}`;
|
||||
return `${month}-${day}`;
|
||||
}
|
||||
|
||||
function ReportsChartsSkeleton() {
|
||||
return (
|
||||
<>
|
||||
<div className="mt-6 grid grid-cols-1 gap-4 xl:grid-cols-2">
|
||||
{Array.from({ length: 2 }).map((_, idx) => (
|
||||
<div key={idx} className="h-[320px] rounded-2xl border border-white/10 bg-white/5" />
|
||||
))}
|
||||
</div>
|
||||
<div className="mt-6 grid grid-cols-1 gap-4 xl:grid-cols-3">
|
||||
{Array.from({ length: 3 }).map((_, idx) => (
|
||||
<div key={idx} className="h-[280px] rounded-2xl border border-white/10 bg-white/5" />
|
||||
))}
|
||||
</div>
|
||||
</>
|
||||
);
|
||||
}
|
||||
|
||||
function buildCsv(report: ReportPayload, t: Translator) {
|
||||
const rows = new Map<string, Record<string, string | number | null>>();
|
||||
const addSeries = (series: ReportTrendPoint[], key: string) => {
|
||||
for (const p of series) {
|
||||
const row = rows.get(p.t) ?? { timestamp: p.t };
|
||||
row[key] = p.v;
|
||||
rows.set(p.t, row);
|
||||
}
|
||||
};
|
||||
|
||||
addSeries(report.trend.oee, "oee");
|
||||
addSeries(report.trend.availability, "availability");
|
||||
addSeries(report.trend.performance, "performance");
|
||||
addSeries(report.trend.quality, "quality");
|
||||
addSeries(report.trend.scrapRate, "scrapRate");
|
||||
|
||||
const ordered = [...rows.values()].sort((a, b) => {
|
||||
const at = new Date(String(a.timestamp)).getTime();
|
||||
const bt = new Date(String(b.timestamp)).getTime();
|
||||
return at - bt;
|
||||
});
|
||||
|
||||
const header = ["timestamp", "oee", "availability", "performance", "quality", "scrapRate"].join(",");
|
||||
const lines = ordered.map((row) =>
|
||||
[
|
||||
row.timestamp,
|
||||
row.oee ?? "",
|
||||
row.availability ?? "",
|
||||
row.performance ?? "",
|
||||
row.quality ?? "",
|
||||
row.scrapRate ?? "",
|
||||
]
|
||||
.map((v) => (v == null ? "" : String(v)))
|
||||
.join(",")
|
||||
);
|
||||
|
||||
const summary = report.summary;
|
||||
const downtime = report.downtime;
|
||||
|
||||
const sectionLines: string[] = [];
|
||||
sectionLines.push(
|
||||
[t("reports.csv.section"), t("reports.csv.key"), t("reports.csv.value")].join(",")
|
||||
);
|
||||
const addRow = (section: string, key: string, value: string | number | null | undefined) => {
|
||||
sectionLines.push(
|
||||
[section, key, value == null ? "" : String(value)]
|
||||
.map((v) => (v.includes(",") ? `"${v.replace(/\"/g, '""')}"` : v))
|
||||
.join(",")
|
||||
);
|
||||
};
|
||||
|
||||
addRow("summary", "oeeAvg", summary.oeeAvg);
|
||||
addRow("summary", "availabilityAvg", summary.availabilityAvg);
|
||||
addRow("summary", "performanceAvg", summary.performanceAvg);
|
||||
addRow("summary", "qualityAvg", summary.qualityAvg);
|
||||
addRow("summary", "goodTotal", summary.goodTotal);
|
||||
addRow("summary", "scrapTotal", summary.scrapTotal);
|
||||
addRow("summary", "targetTotal", summary.targetTotal);
|
||||
addRow("summary", "scrapRate", summary.scrapRate);
|
||||
addRow("summary", "topScrapSku", summary.topScrapSku ?? "");
|
||||
addRow("summary", "topScrapWorkOrder", summary.topScrapWorkOrder ?? "");
|
||||
|
||||
addRow("loss_drivers", "macrostopSec", downtime.macrostopSec);
|
||||
addRow("loss_drivers", "microstopSec", downtime.microstopSec);
|
||||
addRow("loss_drivers", "slowCycleCount", downtime.slowCycleCount);
|
||||
addRow("loss_drivers", "qualitySpikeCount", downtime.qualitySpikeCount);
|
||||
addRow("loss_drivers", "performanceDegradationCount", downtime.performanceDegradationCount);
|
||||
addRow("loss_drivers", "oeeDropCount", downtime.oeeDropCount);
|
||||
|
||||
for (const bin of report.distribution.cycleTime) {
|
||||
addRow("cycle_distribution", bin.label, bin.count);
|
||||
}
|
||||
|
||||
if (report.insights?.length) {
|
||||
report.insights.forEach((note, idx) => addRow("insights", String(idx + 1), note));
|
||||
}
|
||||
|
||||
return [header, ...lines, "", ...sectionLines].join("\n");
|
||||
}
|
||||
|
||||
function downloadText(filename: string, content: string) {
|
||||
const blob = new Blob([content], { type: "text/csv;charset=utf-8;" });
|
||||
const url = URL.createObjectURL(blob);
|
||||
const link = document.createElement("a");
|
||||
link.href = url;
|
||||
link.setAttribute("download", filename);
|
||||
document.body.appendChild(link);
|
||||
link.click();
|
||||
document.body.removeChild(link);
|
||||
URL.revokeObjectURL(url);
|
||||
}
|
||||
|
||||
function buildPdfHtml(
|
||||
report: ReportPayload,
|
||||
rangeLabel: string,
|
||||
filters: { machine: string; workOrder: string; sku: string },
|
||||
t: Translator
|
||||
) {
|
||||
const summary = report.summary;
|
||||
const downtime = report.downtime;
|
||||
const cycleBins = report.distribution.cycleTime;
|
||||
const insights = report.insights ?? [];
|
||||
|
||||
return `
|
||||
<!doctype html>
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="utf-8" />
|
||||
<title>${t("reports.pdf.title")}</title>
|
||||
<style>
|
||||
body { font-family: Arial, sans-serif; color: #111; margin: 24px; }
|
||||
h1 { margin: 0 0 6px; }
|
||||
.meta { margin-bottom: 16px; color: #555; }
|
||||
.grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 16px; }
|
||||
.card { border: 1px solid #ddd; border-radius: 8px; padding: 12px; }
|
||||
.label { color: #666; font-size: 12px; text-transform: uppercase; letter-spacing: .03em; }
|
||||
.value { font-size: 18px; font-weight: 600; margin-top: 6px; }
|
||||
table { width: 100%; border-collapse: collapse; margin-top: 12px; }
|
||||
th, td { border: 1px solid #ddd; padding: 6px 8px; font-size: 12px; }
|
||||
th { background: #f5f5f5; text-align: left; }
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<h1>${t("reports.title")}</h1>
|
||||
<div class="meta">${t("reports.pdf.range")}: ${rangeLabel} | ${t("reports.pdf.machine")}: ${filters.machine} | ${t("reports.pdf.workOrder")}: ${filters.workOrder} | ${t("reports.pdf.sku")}: ${filters.sku}</div>
|
||||
|
||||
<div class="grid">
|
||||
<div class="card">
|
||||
<div class="label">OEE (avg)</div>
|
||||
<div class="value">${summary.oeeAvg != null ? summary.oeeAvg.toFixed(1) + "%" : "--"}</div>
|
||||
</div>
|
||||
<div class="card">
|
||||
<div class="label">Availability (avg)</div>
|
||||
<div class="value">${summary.availabilityAvg != null ? summary.availabilityAvg.toFixed(1) + "%" : "--"}</div>
|
||||
</div>
|
||||
<div class="card">
|
||||
<div class="label">Performance (avg)</div>
|
||||
<div class="value">${summary.performanceAvg != null ? summary.performanceAvg.toFixed(1) + "%" : "--"}</div>
|
||||
</div>
|
||||
<div class="card">
|
||||
<div class="label">Quality (avg)</div>
|
||||
<div class="value">${summary.qualityAvg != null ? summary.qualityAvg.toFixed(1) + "%" : "--"}</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="card" style="margin-top: 16px;">
|
||||
<div class="label">${t("reports.pdf.topLoss")}</div>
|
||||
<table>
|
||||
<thead>
|
||||
<tr><th>${t("reports.pdf.metric")}</th><th>${t("reports.pdf.value")}</th></tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<tr><td>${t("reports.loss.macrostop")} (sec)</td><td>${downtime.macrostopSec}</td></tr>
|
||||
<tr><td>${t("reports.loss.microstop")} (sec)</td><td>${downtime.microstopSec}</td></tr>
|
||||
<tr><td>${t("reports.loss.slowCycle")}</td><td>${downtime.slowCycleCount}</td></tr>
|
||||
<tr><td>${t("reports.loss.qualitySpike")}</td><td>${downtime.qualitySpikeCount}</td></tr>
|
||||
<tr><td>${t("reports.loss.perfDegradation")}</td><td>${downtime.performanceDegradationCount}</td></tr>
|
||||
<tr><td>${t("reports.loss.oeeDrop")}</td><td>${downtime.oeeDropCount}</td></tr>
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
|
||||
<div class="card" style="margin-top: 16px;">
|
||||
<div class="label">${t("reports.pdf.qualitySummary")}</div>
|
||||
<table>
|
||||
<thead>
|
||||
<tr><th>${t("reports.pdf.metric")}</th><th>${t("reports.pdf.value")}</th></tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<tr><td>${t("reports.scrapRate")}</td><td>${summary.scrapRate != null ? summary.scrapRate.toFixed(1) + "%" : "--"}</td></tr>
|
||||
<tr><td>${t("overview.good")}</td><td>${summary.goodTotal ?? "--"}</td></tr>
|
||||
<tr><td>${t("overview.scrap")}</td><td>${summary.scrapTotal ?? "--"}</td></tr>
|
||||
<tr><td>${t("overview.target")}</td><td>${summary.targetTotal ?? "--"}</td></tr>
|
||||
<tr><td>${t("reports.topScrapSku")}</td><td>${summary.topScrapSku ?? "--"}</td></tr>
|
||||
<tr><td>${t("reports.topScrapWorkOrder")}</td><td>${summary.topScrapWorkOrder ?? "--"}</td></tr>
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
|
||||
<div class="card" style="margin-top: 16px;">
|
||||
<div class="label">${t("reports.pdf.cycleDistribution")}</div>
|
||||
<table>
|
||||
<thead>
|
||||
<tr><th>${t("reports.tooltip.range")}</th><th>${t("reports.tooltip.cycles")}</th></tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
${cycleBins
|
||||
.map((bin) => `<tr><td>${bin.label}</td><td>${bin.count}</td></tr>`)
|
||||
.join("")}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
|
||||
<div class="card" style="margin-top: 16px;">
|
||||
<div class="label">${t("reports.pdf.notes")}</div>
|
||||
${insights.length ? `<ul>${insights.map((n) => `<li>${n}</li>`).join("")}</ul>` : `<div>${t("reports.pdf.none")}</div>`}
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
`.trim();
|
||||
}
|
||||
|
||||
export default function ReportsPageClient({
|
||||
initialMachines = [],
|
||||
}: {
|
||||
initialMachines?: MachineOption[];
|
||||
}) {
|
||||
const { t, locale } = useI18n();
|
||||
const [range, setRange] = useState<RangeKey>("24h");
|
||||
const [report, setReport] = useState<ReportPayload | null>(null);
|
||||
const [loading, setLoading] = useState(true);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
const [machines] = useState<MachineOption[]>(() => initialMachines);
|
||||
const [filterOptions, setFilterOptions] = useState<FilterOptions>({ workOrders: [], skus: [] });
|
||||
const [machineId, setMachineId] = useState("");
|
||||
const [workOrderId, setWorkOrderId] = useState("");
|
||||
const [sku, setSku] = useState("");
|
||||
|
||||
const rangeLabel = useMemo(() => {
|
||||
if (range === "24h") return t("reports.rangeLabel.last24");
|
||||
if (range === "7d") return t("reports.rangeLabel.last7");
|
||||
if (range === "30d") return t("reports.rangeLabel.last30");
|
||||
return t("reports.rangeLabel.custom");
|
||||
}, [range, t]);
|
||||
|
||||
useEffect(() => {
|
||||
let alive = true;
|
||||
const controller = new AbortController();
|
||||
|
||||
async function load() {
|
||||
setLoading(true);
|
||||
setError(null);
|
||||
try {
|
||||
const params = new URLSearchParams({ range });
|
||||
if (machineId) params.set("machineId", machineId);
|
||||
if (workOrderId) params.set("workOrderId", workOrderId);
|
||||
if (sku) params.set("sku", sku);
|
||||
|
||||
const res = await fetch(`/api/reports?${params.toString()}`, {
|
||||
cache: "no-cache",
|
||||
signal: controller.signal,
|
||||
});
|
||||
const json = await res.json();
|
||||
if (!alive) return;
|
||||
if (!res.ok || json?.ok === false) {
|
||||
setError(json?.error ?? t("reports.error.failed"));
|
||||
setReport(null);
|
||||
} else {
|
||||
setReport(json);
|
||||
}
|
||||
} catch {
|
||||
if (!alive) return;
|
||||
setError(t("reports.error.network"));
|
||||
setReport(null);
|
||||
} finally {
|
||||
if (alive) setLoading(false);
|
||||
}
|
||||
}
|
||||
|
||||
load();
|
||||
return () => {
|
||||
alive = false;
|
||||
controller.abort();
|
||||
};
|
||||
}, [range, machineId, workOrderId, sku, t]);
|
||||
|
||||
useEffect(() => {
|
||||
let alive = true;
|
||||
const controller = new AbortController();
|
||||
|
||||
async function loadFilters() {
|
||||
try {
|
||||
const params = new URLSearchParams({ range });
|
||||
if (machineId) params.set("machineId", machineId);
|
||||
const res = await fetch(`/api/reports/filters?${params.toString()}`, {
|
||||
cache: "no-cache",
|
||||
signal: controller.signal,
|
||||
});
|
||||
const json = await res.json();
|
||||
if (!alive) return;
|
||||
if (!res.ok || json?.ok === false) {
|
||||
setFilterOptions({ workOrders: [], skus: [] });
|
||||
} else {
|
||||
setFilterOptions({
|
||||
workOrders: json.workOrders ?? [],
|
||||
skus: json.skus ?? [],
|
||||
});
|
||||
}
|
||||
} catch {
|
||||
if (!alive) return;
|
||||
setFilterOptions({ workOrders: [], skus: [] });
|
||||
}
|
||||
}
|
||||
|
||||
loadFilters();
|
||||
return () => {
|
||||
alive = false;
|
||||
controller.abort();
|
||||
};
|
||||
}, [range, machineId]);
|
||||
|
||||
const summary = report?.summary;
|
||||
const downtime = report?.downtime;
|
||||
const trend = report?.trend;
|
||||
const distribution = report?.distribution;
|
||||
|
||||
const oeeSeries = useMemo(() => {
|
||||
const rows = trend?.oee ?? [];
|
||||
const trimmed = downsampleTrendPreserveGaps(rows, 600);
|
||||
return trimmed.map((p) => ({
|
||||
ts: p.t,
|
||||
label: formatTickLabel(p.t, range),
|
||||
value: p.v,
|
||||
}));
|
||||
}, [trend?.oee, range]);
|
||||
|
||||
const scrapSeries = useMemo(() => {
|
||||
const rows = trend?.scrapRate ?? [];
|
||||
const trimmed = downsample(rows, 600);
|
||||
return trimmed.map((p) => ({
|
||||
ts: p.t,
|
||||
label: formatTickLabel(p.t, range),
|
||||
value: p.v,
|
||||
}));
|
||||
}, [trend?.scrapRate, range]);
|
||||
|
||||
const cycleHistogram = useMemo(() => {
|
||||
return distribution?.cycleTime ?? [];
|
||||
}, [distribution?.cycleTime]);
|
||||
|
||||
const downtimeSeries = useMemo(() => {
|
||||
if (!downtime) return [];
|
||||
return [
|
||||
{ name: "Macrostop", value: Math.round(downtime.macrostopSec / 60) },
|
||||
{ name: "Microstop", value: Math.round(downtime.microstopSec / 60) },
|
||||
];
|
||||
}, [downtime]);
|
||||
|
||||
const downtimeColors: Record<string, string> = {
|
||||
Macrostop: "#FF3B5C",
|
||||
Microstop: "#FF7A00",
|
||||
};
|
||||
|
||||
const lossRows = useMemo(
|
||||
() => [
|
||||
{ label: t("reports.loss.macrostop"), value: fmtDuration(downtime?.macrostopSec) },
|
||||
{ label: t("reports.loss.microstop"), value: fmtDuration(downtime?.microstopSec) },
|
||||
{ label: t("reports.loss.slowCycle"), value: downtime ? `${downtime.slowCycleCount}` : "--" },
|
||||
{ label: t("reports.loss.qualitySpike"), value: downtime ? `${downtime.qualitySpikeCount}` : "--" },
|
||||
{ label: t("reports.loss.oeeDrop"), value: downtime ? `${downtime.oeeDropCount}` : "--" },
|
||||
{
|
||||
label: t("reports.loss.perfDegradation"),
|
||||
value: downtime ? `${downtime.performanceDegradationCount}` : "--",
|
||||
},
|
||||
],
|
||||
[downtime, t]
|
||||
);
|
||||
|
||||
const machineLabel = useMemo(() => {
|
||||
if (!machineId) return t("reports.filter.allMachines");
|
||||
return machines.find((m) => m.id === machineId)?.name ?? machineId;
|
||||
}, [machineId, machines, t]);
|
||||
|
||||
const workOrderLabel = workOrderId || t("reports.filter.allWorkOrders");
|
||||
const skuLabel = sku || t("reports.filter.allSkus");
|
||||
|
||||
const handleExportCsv = () => {
|
||||
if (!report) return;
|
||||
const csv = buildCsv(report, t);
|
||||
downloadText("reports.csv", csv);
|
||||
};
|
||||
|
||||
const handleExportPdf = () => {
|
||||
if (!report) return;
|
||||
const html = buildPdfHtml(
|
||||
report,
|
||||
rangeLabel,
|
||||
{
|
||||
machine: machineLabel,
|
||||
workOrder: workOrderLabel,
|
||||
sku: skuLabel,
|
||||
},
|
||||
t
|
||||
);
|
||||
|
||||
const win = window.open("", "_blank", "width=900,height=650");
|
||||
if (!win) return;
|
||||
win.document.open();
|
||||
win.document.write(html);
|
||||
win.document.close();
|
||||
win.focus();
|
||||
setTimeout(() => win.print(), 300);
|
||||
};
|
||||
|
||||
return (
|
||||
<div className="p-4 sm:p-6">
|
||||
<div className="mb-6 flex flex-col gap-4 sm:flex-row sm:items-start sm:justify-between">
|
||||
<div>
|
||||
<h1 className="text-2xl font-semibold text-white">{t("reports.title")}</h1>
|
||||
<p className="text-sm text-zinc-400">{t("reports.subtitle")}</p>
|
||||
</div>
|
||||
|
||||
<div className="flex w-full flex-wrap items-center gap-2 sm:w-auto">
|
||||
<button
|
||||
onClick={handleExportCsv}
|
||||
className="w-full rounded-xl border border-white/10 bg-white/5 px-4 py-2 text-sm text-white hover:bg-white/10 sm:w-auto"
|
||||
>
|
||||
{t("reports.exportCsv")}
|
||||
</button>
|
||||
<button
|
||||
onClick={handleExportPdf}
|
||||
className="w-full rounded-xl border border-white/10 bg-white/5 px-4 py-2 text-sm text-white hover:bg-white/10 sm:w-auto"
|
||||
>
|
||||
{t("reports.exportPdf")}
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="rounded-2xl border border-white/10 bg-white/5 p-5">
|
||||
<div className="flex flex-wrap items-center justify-between gap-4">
|
||||
<div className="text-sm font-semibold text-white">{t("reports.filters")}</div>
|
||||
<div className="text-xs text-zinc-400">{rangeLabel}</div>
|
||||
</div>
|
||||
|
||||
<div className="mt-4 grid grid-cols-1 gap-3 md:grid-cols-2 xl:grid-cols-4">
|
||||
<div className="rounded-xl border border-white/10 bg-black/20 p-3">
|
||||
<div className="text-[11px] text-zinc-400">{t("reports.filter.range")}</div>
|
||||
<div className="mt-2 flex flex-wrap gap-2">
|
||||
{(["24h", "7d", "30d", "custom"] as RangeKey[]).map((k) => (
|
||||
<button
|
||||
key={k}
|
||||
onClick={() => setRange(k)}
|
||||
className={`rounded-full border px-3 py-1 text-xs ${
|
||||
range === k
|
||||
? "border-emerald-500/30 bg-emerald-500/15 text-emerald-200"
|
||||
: "border-white/10 bg-white/5 text-zinc-300 hover:bg-white/10"
|
||||
}`}
|
||||
>
|
||||
{k.toUpperCase()}
|
||||
</button>
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="rounded-xl border border-white/10 bg-black/20 p-3">
|
||||
<div className="text-[11px] text-zinc-400">{t("reports.filter.machine")}</div>
|
||||
<select
|
||||
value={machineId}
|
||||
onChange={(e) => setMachineId(e.target.value)}
|
||||
className="mt-2 w-full rounded-lg border border-white/10 bg-white/5 px-3 py-2 text-sm text-zinc-300"
|
||||
>
|
||||
<option value="">{t("reports.filter.allMachines")}</option>
|
||||
{machines.map((m) => (
|
||||
<option key={m.id} value={m.id}>
|
||||
{m.name}
|
||||
</option>
|
||||
))}
|
||||
</select>
|
||||
</div>
|
||||
|
||||
<div className="rounded-xl border border-white/10 bg-black/20 p-3">
|
||||
<div className="text-[11px] text-zinc-400">{t("reports.filter.workOrder")}</div>
|
||||
<input
|
||||
list="work-order-list"
|
||||
value={workOrderId}
|
||||
onChange={(e) => setWorkOrderId(e.target.value)}
|
||||
placeholder={t("reports.filter.allWorkOrders")}
|
||||
className="mt-2 w-full rounded-lg border border-white/10 bg-white/5 px-3 py-2 text-sm text-zinc-300 placeholder:text-zinc-500"
|
||||
/>
|
||||
<datalist id="work-order-list">
|
||||
{filterOptions.workOrders.map((wo) => (
|
||||
<option key={wo} value={wo} />
|
||||
))}
|
||||
</datalist>
|
||||
</div>
|
||||
|
||||
<div className="rounded-xl border border-white/10 bg-black/20 p-3">
|
||||
<div className="text-[11px] text-zinc-400">{t("reports.filter.sku")}</div>
|
||||
<input
|
||||
list="sku-list"
|
||||
value={sku}
|
||||
onChange={(e) => setSku(e.target.value)}
|
||||
placeholder={t("reports.filter.allSkus")}
|
||||
className="mt-2 w-full rounded-lg border border-white/10 bg-white/5 px-3 py-2 text-sm text-zinc-300 placeholder:text-zinc-500"
|
||||
/>
|
||||
<datalist id="sku-list">
|
||||
{filterOptions.skus.map((s) => (
|
||||
<option key={s} value={s} />
|
||||
))}
|
||||
</datalist>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="mt-4">
|
||||
{loading && <div className="text-sm text-zinc-400">{t("reports.loading")}</div>}
|
||||
{error && !loading && (
|
||||
<div className="rounded-2xl border border-red-500/20 bg-red-500/10 p-4 text-sm text-red-200">
|
||||
{error}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
|
||||
<div className="mt-4 grid grid-cols-1 gap-4 md:grid-cols-2 xl:grid-cols-4">
|
||||
{[
|
||||
{ label: "OEE", value: fmtPct(summary?.oeeAvg), tone: "text-emerald-300" },
|
||||
{ label: "Availability", value: fmtPct(summary?.availabilityAvg), tone: "text-white" },
|
||||
{ label: "Performance", value: fmtPct(summary?.performanceAvg), tone: "text-white" },
|
||||
{ label: "Quality", value: fmtPct(summary?.qualityAvg), tone: "text-white" },
|
||||
].map((kpi) => (
|
||||
<div key={kpi.label} className="rounded-2xl border border-white/10 bg-white/5 p-5">
|
||||
<div className="text-xs text-zinc-400">{kpi.label} (avg)</div>
|
||||
<div className={`mt-2 text-3xl font-semibold ${kpi.tone}`}>{kpi.value}</div>
|
||||
<div className="mt-2 text-xs text-zinc-500">
|
||||
{summary ? t("reports.kpi.note.withData") : t("reports.kpi.note.noData")}
|
||||
</div>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
|
||||
<Suspense fallback={<ReportsChartsSkeleton />}>
|
||||
<ReportsCharts
|
||||
oeeSeries={oeeSeries}
|
||||
downtimeSeries={downtimeSeries}
|
||||
downtimeColors={downtimeColors}
|
||||
cycleHistogram={cycleHistogram}
|
||||
scrapSeries={scrapSeries}
|
||||
lossRows={lossRows}
|
||||
locale={locale}
|
||||
t={t}
|
||||
/>
|
||||
</Suspense>
|
||||
|
||||
<div className="mt-6 grid grid-cols-1 gap-4 xl:grid-cols-2">
|
||||
<div className="rounded-2xl border border-white/10 bg-white/5 p-5">
|
||||
<div className="mb-3 text-sm font-semibold text-white">{t("reports.qualitySummary")}</div>
|
||||
<div className="space-y-3 text-sm text-zinc-300">
|
||||
<div className="rounded-xl border border-white/10 bg-black/20 p-3">
|
||||
<div className="text-xs text-zinc-400">{t("reports.scrapRate")}</div>
|
||||
<div className="mt-1 text-lg font-semibold text-white">
|
||||
{summary?.scrapRate != null ? fmtPct(summary.scrapRate) : "--"}
|
||||
</div>
|
||||
</div>
|
||||
<div className="rounded-xl border border-white/10 bg-black/20 p-3">
|
||||
<div className="text-xs text-zinc-400">{t("reports.topScrapSku")}</div>
|
||||
<div className="mt-1 text-sm text-zinc-300">{summary?.topScrapSku ?? "--"}</div>
|
||||
</div>
|
||||
<div className="rounded-xl border border-white/10 bg-black/20 p-3">
|
||||
<div className="text-xs text-zinc-400">{t("reports.topScrapWorkOrder")}</div>
|
||||
<div className="mt-1 text-sm text-zinc-300">{summary?.topScrapWorkOrder ?? "--"}</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="rounded-2xl border border-white/10 bg-white/5 p-5">
|
||||
<div className="mb-3 text-sm font-semibold text-white">{t("reports.notes")}</div>
|
||||
<div className="rounded-xl border border-white/10 bg-black/20 p-4 text-sm text-zinc-300">
|
||||
<div className="mb-2 text-xs text-zinc-400">{t("reports.notes.suggested")}</div>
|
||||
{report?.insights && report.insights.length > 0 ? (
|
||||
<div className="space-y-2">
|
||||
{report.insights.map((note, idx) => (
|
||||
<div key={idx}>{note}</div>
|
||||
))}
|
||||
</div>
|
||||
) : (
|
||||
<div>{t("reports.notes.none")}</div>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
16
app/(app)/reports/downtime-pareto/page.tsx
Normal file
16
app/(app)/reports/downtime-pareto/page.tsx
Normal file
@@ -0,0 +1,16 @@
|
||||
import { redirect } from "next/navigation";
|
||||
|
||||
export default async function LegacyDowntimeParetoPage({
|
||||
searchParams,
|
||||
}: {
|
||||
searchParams: Promise<Record<string, string | string[] | undefined>>;
|
||||
}) {
|
||||
const params = await searchParams;
|
||||
const qs = new URLSearchParams();
|
||||
for (const [k, v] of Object.entries(params)) {
|
||||
if (typeof v === "string") qs.set(k, v);
|
||||
else if (Array.isArray(v)) v.forEach((vv) => qs.append(k, vv));
|
||||
}
|
||||
const q = qs.toString();
|
||||
redirect(q ? `/downtime?${q}` : "/downtime");
|
||||
}
|
||||
21
app/(app)/reports/loading.tsx
Normal file
21
app/(app)/reports/loading.tsx
Normal file
@@ -0,0 +1,21 @@
|
||||
export default function ReportsLoading() {
|
||||
return (
|
||||
<div className="p-4 sm:p-6 space-y-6">
|
||||
<div className="h-8 w-56 rounded-lg bg-white/5" />
|
||||
<div className="grid gap-4 lg:grid-cols-4">
|
||||
{Array.from({ length: 4 }).map((_, idx) => (
|
||||
<div key={idx} className="h-24 rounded-2xl border border-white/10 bg-white/5" />
|
||||
))}
|
||||
</div>
|
||||
<div className="grid gap-4 lg:grid-cols-[2fr_1fr]">
|
||||
<div className="h-80 rounded-2xl border border-white/10 bg-white/5" />
|
||||
<div className="h-80 rounded-2xl border border-white/10 bg-white/5" />
|
||||
</div>
|
||||
<div className="grid gap-4 lg:grid-cols-3">
|
||||
{Array.from({ length: 3 }).map((_, idx) => (
|
||||
<div key={idx} className="h-24 rounded-2xl border border-white/10 bg-white/5" />
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
17
app/(app)/reports/page.tsx
Normal file
17
app/(app)/reports/page.tsx
Normal file
@@ -0,0 +1,17 @@
|
||||
import { redirect } from "next/navigation";
|
||||
import { prisma } from "@/lib/prisma";
|
||||
import { requireSession } from "@/lib/auth/requireSession";
|
||||
import ReportsPageClient from "./ReportsPageClient";
|
||||
|
||||
export default async function ReportsPage() {
|
||||
const session = await requireSession();
|
||||
if (!session) redirect("/login?next=/reports");
|
||||
|
||||
const machines = await prisma.machine.findMany({
|
||||
where: { orgId: session.orgId },
|
||||
orderBy: { createdAt: "desc" },
|
||||
select: { id: true, name: true },
|
||||
});
|
||||
|
||||
return <ReportsPageClient initialMachines={machines} />;
|
||||
}
|
||||
1415
app/(app)/settings/page.tsx
Normal file
1415
app/(app)/settings/page.tsx
Normal file
File diff suppressed because it is too large
Load Diff
89
app/api/alerts/contacts/[id]/route.ts
Normal file
89
app/api/alerts/contacts/[id]/route.ts
Normal file
@@ -0,0 +1,89 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import { z } from "zod";
|
||||
import { Prisma } from "@prisma/client";
|
||||
import { prisma } from "@/lib/prisma";
|
||||
import { requireSession } from "@/lib/auth/requireSession";
|
||||
|
||||
const roleScopeSchema = z.preprocess(
|
||||
(value) => (typeof value === "string" ? value.trim().toUpperCase() : value),
|
||||
z.enum(["MEMBER", "ADMIN", "OWNER", "CUSTOM"])
|
||||
);
|
||||
|
||||
const contactPatchSchema = z.object({
|
||||
name: z.string().trim().min(1).max(120).optional(),
|
||||
roleScope: roleScopeSchema.optional(),
|
||||
email: z.string().trim().email().optional().nullable(),
|
||||
phone: z.string().trim().min(6).max(40).optional().nullable(),
|
||||
userId: z.string().uuid().optional().nullable(),
|
||||
eventTypes: z.array(z.string().trim().min(1)).optional().nullable(),
|
||||
isActive: z.boolean().optional(),
|
||||
});
|
||||
|
||||
function canManageAlerts(role?: string | null) {
|
||||
return role === "OWNER";
|
||||
}
|
||||
|
||||
export async function PATCH(req: Request, { params }: { params: Promise<{ id: string }> }) {
|
||||
const session = await requireSession();
|
||||
if (!session) return NextResponse.json({ ok: false, error: "Unauthorized" }, { status: 401 });
|
||||
|
||||
const membership = await prisma.orgUser.findUnique({
|
||||
where: { orgId_userId: { orgId: session.orgId, userId: session.userId } },
|
||||
select: { role: true },
|
||||
});
|
||||
if (!canManageAlerts(membership?.role)) {
|
||||
return NextResponse.json({ ok: false, error: "Forbidden" }, { status: 403 });
|
||||
}
|
||||
|
||||
const body = await req.json().catch(() => ({}));
|
||||
const parsed = contactPatchSchema.safeParse(body);
|
||||
if (!parsed.success) {
|
||||
return NextResponse.json({ ok: false, error: "Invalid contact payload" }, { status: 400 });
|
||||
}
|
||||
|
||||
const { id } = await params;
|
||||
const existing = await prisma.alertContact.findFirst({
|
||||
where: { id, orgId: session.orgId },
|
||||
});
|
||||
if (!existing) {
|
||||
return NextResponse.json({ ok: false, error: "Not found" }, { status: 404 });
|
||||
}
|
||||
|
||||
const { userId: _userId, eventTypes, ...updateData } = parsed.data;
|
||||
void _userId;
|
||||
const normalizedEventTypes =
|
||||
eventTypes === null ? Prisma.DbNull : eventTypes ?? undefined;
|
||||
const data = normalizedEventTypes === undefined
|
||||
? updateData
|
||||
: { ...updateData, eventTypes: normalizedEventTypes };
|
||||
const updated = await prisma.alertContact.update({
|
||||
where: { id },
|
||||
data,
|
||||
});
|
||||
|
||||
return NextResponse.json({ ok: true, contact: updated });
|
||||
}
|
||||
|
||||
export async function DELETE(_req: Request, { params }: { params: Promise<{ id: string }> }) {
|
||||
const session = await requireSession();
|
||||
if (!session) return NextResponse.json({ ok: false, error: "Unauthorized" }, { status: 401 });
|
||||
|
||||
const membership = await prisma.orgUser.findUnique({
|
||||
where: { orgId_userId: { orgId: session.orgId, userId: session.userId } },
|
||||
select: { role: true },
|
||||
});
|
||||
if (!canManageAlerts(membership?.role)) {
|
||||
return NextResponse.json({ ok: false, error: "Forbidden" }, { status: 403 });
|
||||
}
|
||||
|
||||
const { id } = await params;
|
||||
const existing = await prisma.alertContact.findFirst({
|
||||
where: { id, orgId: session.orgId },
|
||||
});
|
||||
if (!existing) {
|
||||
return NextResponse.json({ ok: false, error: "Not found" }, { status: 404 });
|
||||
}
|
||||
|
||||
await prisma.alertContact.delete({ where: { id } });
|
||||
return NextResponse.json({ ok: true });
|
||||
}
|
||||
77
app/api/alerts/contacts/route.ts
Normal file
77
app/api/alerts/contacts/route.ts
Normal file
@@ -0,0 +1,77 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import { z } from "zod";
|
||||
import { Prisma } from "@prisma/client";
|
||||
import { prisma } from "@/lib/prisma";
|
||||
import { requireSession } from "@/lib/auth/requireSession";
|
||||
|
||||
const roleScopeSchema = z.preprocess(
|
||||
(value) => (typeof value === "string" ? value.trim().toUpperCase() : value),
|
||||
z.enum(["MEMBER", "ADMIN", "OWNER", "CUSTOM"])
|
||||
);
|
||||
|
||||
const contactSchema = z.object({
|
||||
name: z.string().trim().min(1).max(120),
|
||||
roleScope: roleScopeSchema,
|
||||
email: z.string().trim().email().optional().nullable(),
|
||||
phone: z.string().trim().min(6).max(40).optional().nullable(),
|
||||
userId: z.string().uuid().optional().nullable(),
|
||||
eventTypes: z.array(z.string().trim().min(1)).optional().nullable(),
|
||||
});
|
||||
|
||||
function canManageAlerts(role?: string | null) {
|
||||
return role === "OWNER";
|
||||
}
|
||||
|
||||
export async function GET() {
|
||||
const session = await requireSession();
|
||||
if (!session) return NextResponse.json({ ok: false, error: "Unauthorized" }, { status: 401 });
|
||||
|
||||
const contacts = await prisma.alertContact.findMany({
|
||||
where: { orgId: session.orgId },
|
||||
orderBy: { createdAt: "asc" },
|
||||
});
|
||||
|
||||
return NextResponse.json({ ok: true, contacts });
|
||||
}
|
||||
|
||||
export async function POST(req: Request) {
|
||||
const session = await requireSession();
|
||||
if (!session) return NextResponse.json({ ok: false, error: "Unauthorized" }, { status: 401 });
|
||||
|
||||
const membership = await prisma.orgUser.findUnique({
|
||||
where: { orgId_userId: { orgId: session.orgId, userId: session.userId } },
|
||||
select: { role: true },
|
||||
});
|
||||
if (!canManageAlerts(membership?.role)) {
|
||||
return NextResponse.json({ ok: false, error: "Forbidden" }, { status: 403 });
|
||||
}
|
||||
|
||||
const body = await req.json().catch(() => ({}));
|
||||
const parsed = contactSchema.safeParse(body);
|
||||
if (!parsed.success) {
|
||||
return NextResponse.json({ ok: false, error: "Invalid contact payload" }, { status: 400 });
|
||||
}
|
||||
|
||||
const data = parsed.data;
|
||||
const hasChannel = !!(data.email || data.phone);
|
||||
if (!data.userId && !hasChannel) {
|
||||
return NextResponse.json({ ok: false, error: "email or phone required for external contact" }, { status: 400 });
|
||||
}
|
||||
|
||||
const eventTypes =
|
||||
data.eventTypes === null ? Prisma.DbNull : data.eventTypes ?? undefined;
|
||||
|
||||
const contact = await prisma.alertContact.create({
|
||||
data: {
|
||||
orgId: session.orgId,
|
||||
userId: data.userId ?? null,
|
||||
name: data.name,
|
||||
roleScope: data.roleScope,
|
||||
email: data.email ?? null,
|
||||
phone: data.phone ?? null,
|
||||
eventTypes,
|
||||
},
|
||||
});
|
||||
|
||||
return NextResponse.json({ ok: true, contact });
|
||||
}
|
||||
48
app/api/alerts/inbox/route.ts
Normal file
48
app/api/alerts/inbox/route.ts
Normal file
@@ -0,0 +1,48 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import type { NextRequest } from "next/server";
|
||||
import { requireSession } from "@/lib/auth/requireSession";
|
||||
import { getAlertsInboxData } from "@/lib/alerts/getAlertsInboxData";
|
||||
|
||||
function parseDate(input?: string | null) {
|
||||
if (!input) return null;
|
||||
const n = Number(input);
|
||||
if (!Number.isNaN(n)) return new Date(n);
|
||||
const d = new Date(input);
|
||||
return Number.isNaN(d.getTime()) ? null : d;
|
||||
}
|
||||
|
||||
export async function GET(req: NextRequest) {
|
||||
const session = await requireSession();
|
||||
if (!session) return NextResponse.json({ ok: false, error: "Unauthorized" }, { status: 401 });
|
||||
|
||||
const url = new URL(req.url);
|
||||
const range = url.searchParams.get("range") ?? "24h";
|
||||
const machineId = url.searchParams.get("machineId") ?? undefined;
|
||||
const location = url.searchParams.get("location") ?? undefined;
|
||||
const eventType = url.searchParams.get("eventType") ?? undefined;
|
||||
const severity = url.searchParams.get("severity") ?? undefined;
|
||||
const status = url.searchParams.get("status") ?? undefined;
|
||||
const shift = url.searchParams.get("shift") ?? undefined;
|
||||
const includeUpdates = url.searchParams.get("includeUpdates") === "1";
|
||||
const limitRaw = Number(url.searchParams.get("limit") ?? "200");
|
||||
const limit = Number.isFinite(limitRaw) ? Math.min(Math.max(limitRaw, 1), 500) : 200;
|
||||
const start = parseDate(url.searchParams.get("start"));
|
||||
const end = parseDate(url.searchParams.get("end"));
|
||||
|
||||
const result = await getAlertsInboxData({
|
||||
orgId: session.orgId,
|
||||
range,
|
||||
start,
|
||||
end,
|
||||
machineId,
|
||||
location,
|
||||
eventType,
|
||||
severity,
|
||||
status,
|
||||
shift,
|
||||
includeUpdates,
|
||||
limit,
|
||||
});
|
||||
|
||||
return NextResponse.json({ ok: true, range: result.range, events: result.events });
|
||||
}
|
||||
23
app/api/alerts/notifications/route.ts
Normal file
23
app/api/alerts/notifications/route.ts
Normal file
@@ -0,0 +1,23 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import { prisma } from "@/lib/prisma";
|
||||
import { requireSession } from "@/lib/auth/requireSession";
|
||||
|
||||
export async function GET(req: Request) {
|
||||
const session = await requireSession();
|
||||
if (!session) return NextResponse.json({ ok: false, error: "Unauthorized" }, { status: 401 });
|
||||
|
||||
const url = new URL(req.url);
|
||||
const machineId = url.searchParams.get("machineId") || undefined;
|
||||
const limit = Math.min(Number(url.searchParams.get("limit") ?? "50"), 200);
|
||||
|
||||
const notifications = await prisma.alertNotification.findMany({
|
||||
where: {
|
||||
orgId: session.orgId,
|
||||
...(machineId ? { machineId } : {}),
|
||||
},
|
||||
orderBy: { sentAt: "desc" },
|
||||
take: Number.isFinite(limit) ? limit : 50,
|
||||
});
|
||||
|
||||
return NextResponse.json({ ok: true, notifications });
|
||||
}
|
||||
55
app/api/alerts/policy/route.ts
Normal file
55
app/api/alerts/policy/route.ts
Normal file
@@ -0,0 +1,55 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import { prisma } from "@/lib/prisma";
|
||||
import { requireSession } from "@/lib/auth/requireSession";
|
||||
import { AlertPolicySchema, DEFAULT_POLICY } from "@/lib/alerts/policy";
|
||||
|
||||
function canManageAlerts(role?: string | null) {
|
||||
return role === "OWNER";
|
||||
}
|
||||
|
||||
export async function GET() {
|
||||
const session = await requireSession();
|
||||
if (!session) return NextResponse.json({ ok: false, error: "Unauthorized" }, { status: 401 });
|
||||
|
||||
let policy = await prisma.alertPolicy.findUnique({
|
||||
where: { orgId: session.orgId },
|
||||
select: { policyJson: true },
|
||||
});
|
||||
|
||||
if (!policy) {
|
||||
await prisma.alertPolicy.create({
|
||||
data: { orgId: session.orgId, policyJson: DEFAULT_POLICY },
|
||||
});
|
||||
policy = { policyJson: DEFAULT_POLICY };
|
||||
}
|
||||
|
||||
const parsed = AlertPolicySchema.safeParse(policy.policyJson);
|
||||
return NextResponse.json({ ok: true, policy: parsed.success ? parsed.data : DEFAULT_POLICY });
|
||||
}
|
||||
|
||||
export async function PUT(req: Request) {
|
||||
const session = await requireSession();
|
||||
if (!session) return NextResponse.json({ ok: false, error: "Unauthorized" }, { status: 401 });
|
||||
|
||||
const membership = await prisma.orgUser.findUnique({
|
||||
where: { orgId_userId: { orgId: session.orgId, userId: session.userId } },
|
||||
select: { role: true },
|
||||
});
|
||||
if (!canManageAlerts(membership?.role)) {
|
||||
return NextResponse.json({ ok: false, error: "Forbidden" }, { status: 403 });
|
||||
}
|
||||
|
||||
const body = await req.json().catch(() => ({}));
|
||||
const parsed = AlertPolicySchema.safeParse(body?.policy ?? body);
|
||||
if (!parsed.success) {
|
||||
return NextResponse.json({ ok: false, error: "Invalid policy payload" }, { status: 400 });
|
||||
}
|
||||
|
||||
await prisma.alertPolicy.upsert({
|
||||
where: { orgId: session.orgId },
|
||||
create: { orgId: session.orgId, policyJson: parsed.data, updatedBy: session.userId },
|
||||
update: { policyJson: parsed.data, updatedBy: session.userId },
|
||||
});
|
||||
|
||||
return NextResponse.json({ ok: true });
|
||||
}
|
||||
64
app/api/analytics/coverage/route.ts
Normal file
64
app/api/analytics/coverage/route.ts
Normal file
@@ -0,0 +1,64 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import { prisma } from "@/lib/prisma";
|
||||
import { requireSession } from "@/lib/auth/requireSession";
|
||||
import { coerceDowntimeRange, rangeToStart } from "@/lib/analytics/downtimeRange";
|
||||
|
||||
const bad = (status: number, error: string) =>
|
||||
NextResponse.json({ ok: false, error }, { status });
|
||||
|
||||
export async function GET(req: Request) {
|
||||
const session = await requireSession();
|
||||
if (!session) return bad(401, "Unauthorized");
|
||||
const orgId = session.orgId;
|
||||
|
||||
const url = new URL(req.url);
|
||||
|
||||
// ✅ Parse params INSIDE handler
|
||||
const range = coerceDowntimeRange(url.searchParams.get("range"));
|
||||
const start = rangeToStart(range);
|
||||
|
||||
const machineId = url.searchParams.get("machineId"); // optional
|
||||
const kind = (url.searchParams.get("kind") || "downtime").toLowerCase();
|
||||
|
||||
// coverage is only meaningful for downtime
|
||||
if (kind !== "downtime") return bad(400, "Invalid kind (downtime only)");
|
||||
|
||||
let resolvedMachineId: string | null = null;
|
||||
|
||||
// If machineId provided, validate ownership
|
||||
if (machineId) {
|
||||
const m = await prisma.machine.findFirst({
|
||||
where: { id: machineId, orgId },
|
||||
select: { id: true },
|
||||
});
|
||||
if (!m) return bad(404, "Machine not found");
|
||||
resolvedMachineId = m.id;
|
||||
}
|
||||
|
||||
const rows = await prisma.reasonEntry.findMany({
|
||||
where: {
|
||||
orgId,
|
||||
...(resolvedMachineId ? { machineId: resolvedMachineId } : {}),
|
||||
kind: "downtime",
|
||||
capturedAt: { gte: start },
|
||||
},
|
||||
select: { durationSeconds: true, episodeId: true },
|
||||
});
|
||||
|
||||
const receivedEpisodes = new Set(rows.map((r) => r.episodeId).filter(Boolean)).size;
|
||||
|
||||
const receivedMinutes =
|
||||
Math.round((rows.reduce((acc, r) => acc + (r.durationSeconds ?? 0), 0) / 60) * 10) / 10;
|
||||
|
||||
return NextResponse.json({
|
||||
ok: true,
|
||||
orgId,
|
||||
machineId: resolvedMachineId, // null => org-wide
|
||||
range,
|
||||
start,
|
||||
receivedEpisodes,
|
||||
receivedMinutes,
|
||||
note:
|
||||
"Control Tower received coverage (sync health). True coverage vs total downtime minutes can be added once CT has total downtime minutes per window.",
|
||||
});
|
||||
}
|
||||
159
app/api/analytics/downtime-events/route.ts
Normal file
159
app/api/analytics/downtime-events/route.ts
Normal file
@@ -0,0 +1,159 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import { prisma } from "@/lib/prisma";
|
||||
import { requireSession } from "@/lib/auth/requireSession";
|
||||
import { coerceDowntimeRange, rangeToStart } from "@/lib/analytics/downtimeRange";
|
||||
import type { Prisma } from "@prisma/client";
|
||||
import {
|
||||
applyDowntimeFilters,
|
||||
loadDowntimeShiftContext,
|
||||
normalizeMicrostopLtMin,
|
||||
normalizeShiftFilter,
|
||||
resolvePlannedFilter,
|
||||
} from "@/lib/analytics/downtimeFilters";
|
||||
|
||||
const bad = (status: number, error: string) =>
|
||||
NextResponse.json({ ok: false, error }, { status });
|
||||
|
||||
function toISO(d: Date | null | undefined) {
|
||||
return d ? d.toISOString() : null;
|
||||
}
|
||||
|
||||
export async function GET(req: Request) {
|
||||
// ✅ Session auth (cookie)
|
||||
const session = await requireSession();
|
||||
if (!session) return bad(401, "Unauthorized");
|
||||
const orgId = session.orgId;
|
||||
|
||||
const url = new URL(req.url);
|
||||
|
||||
// ✅ Params
|
||||
const range = coerceDowntimeRange(url.searchParams.get("range"));
|
||||
const start = rangeToStart(range);
|
||||
|
||||
const machineId = url.searchParams.get("machineId"); // optional
|
||||
const reasonCode = url.searchParams.get("reasonCode"); // optional
|
||||
const includeMoldChange = url.searchParams.get("includeMoldChange") === "true";
|
||||
const planned = resolvePlannedFilter(url.searchParams.get("planned"), includeMoldChange);
|
||||
const shift = normalizeShiftFilter(url.searchParams.get("shift"));
|
||||
const microstopLtMin = normalizeMicrostopLtMin(url.searchParams.get("microstopLtMin"));
|
||||
|
||||
const limitRaw = url.searchParams.get("limit");
|
||||
const limit = Math.min(Math.max(Number(limitRaw || 200), 1), 500);
|
||||
|
||||
// Optional pagination: return events before this timestamp (capturedAt)
|
||||
const before = url.searchParams.get("before"); // ISO string
|
||||
const beforeDate = before ? new Date(before) : null;
|
||||
if (before && isNaN(beforeDate!.getTime())) return bad(400, "Invalid before timestamp");
|
||||
|
||||
// ✅ If machineId provided, verify it belongs to this org
|
||||
if (machineId) {
|
||||
const m = await prisma.machine.findFirst({
|
||||
where: { id: machineId, orgId },
|
||||
select: { id: true },
|
||||
});
|
||||
if (!m) return bad(404, "Machine not found");
|
||||
}
|
||||
|
||||
// ✅ Query ReasonEntry as the "episode" table for downtime
|
||||
// We only return rows that have an episodeId (true downtime episodes)
|
||||
const where: Prisma.ReasonEntryWhereInput = {
|
||||
orgId,
|
||||
kind: "downtime",
|
||||
episodeId: { not: null },
|
||||
capturedAt: {
|
||||
gte: start,
|
||||
...(beforeDate ? { lt: beforeDate } : {}),
|
||||
},
|
||||
...(machineId ? { machineId } : {}),
|
||||
...(reasonCode ? { reasonCode } : {}),
|
||||
};
|
||||
|
||||
const scanTake = Math.min(Math.max(limit * 8, 1000), 5000);
|
||||
const rowsRaw = await prisma.reasonEntry.findMany({
|
||||
where,
|
||||
orderBy: { capturedAt: "desc" },
|
||||
take: scanTake,
|
||||
select: {
|
||||
id: true,
|
||||
episodeId: true,
|
||||
machineId: true,
|
||||
reasonCode: true,
|
||||
reasonLabel: true,
|
||||
reasonText: true,
|
||||
durationSeconds: true,
|
||||
capturedAt: true,
|
||||
episodeEndTs: true,
|
||||
workOrderId: true,
|
||||
meta: true,
|
||||
createdAt: true,
|
||||
machine: { select: { name: true } },
|
||||
},
|
||||
});
|
||||
|
||||
const shiftContext = shift === "all" ? null : await loadDowntimeShiftContext(orgId);
|
||||
const rows = applyDowntimeFilters(rowsRaw, {
|
||||
planned,
|
||||
shift,
|
||||
microstopLtMin,
|
||||
shiftContext,
|
||||
}).slice(0, limit);
|
||||
|
||||
const events = rows.map((r) => {
|
||||
const startAt = r.capturedAt;
|
||||
const endAt =
|
||||
r.episodeEndTs ??
|
||||
(r.durationSeconds != null
|
||||
? new Date(startAt.getTime() + r.durationSeconds * 1000)
|
||||
: null);
|
||||
|
||||
const durationSeconds = r.durationSeconds ?? null;
|
||||
const durationMinutes =
|
||||
durationSeconds != null ? Math.round((durationSeconds / 60) * 10) / 10 : null;
|
||||
|
||||
return {
|
||||
id: r.id,
|
||||
episodeId: r.episodeId,
|
||||
machineId: r.machineId,
|
||||
machineName: r.machine?.name ?? null,
|
||||
|
||||
reasonCode: r.reasonCode,
|
||||
reasonLabel: r.reasonLabel ?? r.reasonCode,
|
||||
reasonText: r.reasonText ?? null,
|
||||
|
||||
durationSeconds,
|
||||
durationMinutes,
|
||||
|
||||
startAt: toISO(startAt),
|
||||
endAt: toISO(endAt),
|
||||
capturedAt: toISO(r.capturedAt),
|
||||
|
||||
workOrderId: r.workOrderId ?? null,
|
||||
meta: r.meta ?? null,
|
||||
createdAt: toISO(r.createdAt),
|
||||
};
|
||||
});
|
||||
|
||||
const nextBefore =
|
||||
events.length > 0
|
||||
? events[events.length - 1]?.capturedAt ?? null
|
||||
: rowsRaw.length > 0
|
||||
? toISO(rowsRaw[rowsRaw.length - 1]?.capturedAt)
|
||||
: null;
|
||||
|
||||
return NextResponse.json({
|
||||
ok: true,
|
||||
orgId,
|
||||
range,
|
||||
start,
|
||||
machineId: machineId ?? null,
|
||||
reasonCode: reasonCode ?? null,
|
||||
planned,
|
||||
shift,
|
||||
microstopLtMin,
|
||||
includeMoldChange,
|
||||
limit,
|
||||
before: before ?? null,
|
||||
nextBefore, // pass this back for pagination
|
||||
events,
|
||||
});
|
||||
}
|
||||
183
app/api/analytics/pareto/route.ts
Normal file
183
app/api/analytics/pareto/route.ts
Normal file
@@ -0,0 +1,183 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import { prisma } from "@/lib/prisma";
|
||||
import { requireSession } from "@/lib/auth/requireSession";
|
||||
import { coerceDowntimeRange, rangeToStart } from "@/lib/analytics/downtimeRange";
|
||||
import {
|
||||
applyDowntimeFilters,
|
||||
loadDowntimeShiftContext,
|
||||
normalizeMicrostopLtMin,
|
||||
normalizeShiftFilter,
|
||||
resolvePlannedFilter,
|
||||
} from "@/lib/analytics/downtimeFilters";
|
||||
|
||||
const bad = (status: number, error: string) =>
|
||||
NextResponse.json({ ok: false, error }, { status });
|
||||
|
||||
export async function GET(req: Request) {
|
||||
// ✅ Session auth (cookie)
|
||||
const session = await requireSession();
|
||||
if (!session) return bad(401, "Unauthorized");
|
||||
const orgId = session.orgId;
|
||||
|
||||
const url = new URL(req.url);
|
||||
|
||||
// ✅ Parse params INSIDE handler
|
||||
const range = coerceDowntimeRange(url.searchParams.get("range"));
|
||||
const start = rangeToStart(range);
|
||||
|
||||
const machineId = url.searchParams.get("machineId"); // optional
|
||||
const kind = (url.searchParams.get("kind") || "downtime").toLowerCase();
|
||||
const includeMoldChange = url.searchParams.get("includeMoldChange") === "true";
|
||||
const planned = resolvePlannedFilter(url.searchParams.get("planned"), includeMoldChange);
|
||||
const shift = normalizeShiftFilter(url.searchParams.get("shift"));
|
||||
const microstopLtMin = normalizeMicrostopLtMin(url.searchParams.get("microstopLtMin"));
|
||||
|
||||
if (kind !== "downtime" && kind !== "scrap" && kind !== "planned-downtime") {
|
||||
return bad(400, "Invalid kind (downtime|scrap|planned-downtime)");
|
||||
}
|
||||
|
||||
// ✅ If machineId provided, verify it belongs to this org
|
||||
if (machineId) {
|
||||
const m = await prisma.machine.findFirst({
|
||||
where: { id: machineId, orgId },
|
||||
select: { id: true },
|
||||
});
|
||||
if (!m) return bad(404, "Machine not found");
|
||||
}
|
||||
|
||||
let itemsRaw: { reasonCode: string; reasonLabel: string; value: number; count: number }[] = [];
|
||||
|
||||
if (kind === "downtime" || kind === "planned-downtime") {
|
||||
const baseRows = await prisma.reasonEntry.findMany({
|
||||
where: {
|
||||
orgId,
|
||||
...(machineId ? { machineId } : {}),
|
||||
kind: "downtime",
|
||||
capturedAt: { gte: start },
|
||||
},
|
||||
select: {
|
||||
reasonCode: true,
|
||||
reasonLabel: true,
|
||||
durationSeconds: true,
|
||||
capturedAt: true,
|
||||
meta: true,
|
||||
episodeId: true,
|
||||
},
|
||||
});
|
||||
|
||||
const effectivePlanned = kind === "planned-downtime" ? "planned" : planned;
|
||||
const shiftContext = shift === "all" ? null : await loadDowntimeShiftContext(orgId);
|
||||
const filteredRows = applyDowntimeFilters(baseRows, {
|
||||
planned: effectivePlanned,
|
||||
shift,
|
||||
microstopLtMin,
|
||||
shiftContext,
|
||||
});
|
||||
|
||||
const grouped = new Map<string, { reasonCode: string; reasonLabel: string; durationSeconds: number; count: number }>();
|
||||
for (const row of filteredRows) {
|
||||
const key = `${row.reasonCode}:::${row.reasonLabel ?? row.reasonCode}`;
|
||||
const slot =
|
||||
grouped.get(key) ??
|
||||
{
|
||||
reasonCode: row.reasonCode,
|
||||
reasonLabel: row.reasonLabel ?? row.reasonCode,
|
||||
durationSeconds: 0,
|
||||
count: 0,
|
||||
};
|
||||
slot.durationSeconds += Math.max(0, row.durationSeconds ?? 0);
|
||||
slot.count += 1;
|
||||
grouped.set(key, slot);
|
||||
}
|
||||
|
||||
itemsRaw = [...grouped.values()]
|
||||
.map((g) => ({
|
||||
reasonCode: g.reasonCode,
|
||||
reasonLabel: g.reasonLabel,
|
||||
value: Math.round((g.durationSeconds / 60) * 10) / 10,
|
||||
count: g.count,
|
||||
}))
|
||||
.filter((x) => x.value > 0 || x.count > 0);
|
||||
} else {
|
||||
// Scrap path unchanged.
|
||||
const grouped = await prisma.reasonEntry.groupBy({
|
||||
by: ["reasonCode", "reasonLabel"],
|
||||
where: {
|
||||
orgId,
|
||||
...(machineId ? { machineId } : {}),
|
||||
kind,
|
||||
capturedAt: { gte: start },
|
||||
},
|
||||
_sum: { scrapQty: true },
|
||||
_count: { _all: true },
|
||||
});
|
||||
|
||||
itemsRaw = grouped
|
||||
.map((g) => ({
|
||||
reasonCode: g.reasonCode,
|
||||
reasonLabel: g.reasonLabel ?? g.reasonCode,
|
||||
value: g._sum.scrapQty ?? 0,
|
||||
count: g._count._all,
|
||||
}))
|
||||
.filter((x) => x.value > 0);
|
||||
}
|
||||
|
||||
itemsRaw.sort((a, b) => b.value - a.value);
|
||||
|
||||
const total = itemsRaw.reduce((acc, x) => acc + x.value, 0);
|
||||
|
||||
let cum = 0;
|
||||
let threshold80Index: number | null = null;
|
||||
|
||||
const rows = itemsRaw.map((x, idx) => {
|
||||
const pctOfTotal = total > 0 ? (x.value / total) * 100 : 0;
|
||||
cum += x.value;
|
||||
const cumulativePct = total > 0 ? (cum / total) * 100 : 0;
|
||||
|
||||
if (threshold80Index === null && cumulativePct >= 80) threshold80Index = idx;
|
||||
|
||||
return {
|
||||
reasonCode: x.reasonCode,
|
||||
reasonLabel: x.reasonLabel,
|
||||
minutesLost: kind === "downtime" || kind === "planned-downtime" ? x.value : undefined,
|
||||
scrapQty: kind === "scrap" ? x.value : undefined,
|
||||
pctOfTotal,
|
||||
cumulativePct,
|
||||
count: x.count,
|
||||
};
|
||||
});
|
||||
|
||||
const top3 = rows.slice(0, 3);
|
||||
const threshold80 =
|
||||
threshold80Index === null
|
||||
? null
|
||||
: {
|
||||
index: threshold80Index,
|
||||
reasonCode: rows[threshold80Index].reasonCode,
|
||||
reasonLabel: rows[threshold80Index].reasonLabel,
|
||||
};
|
||||
|
||||
return NextResponse.json({
|
||||
ok: true,
|
||||
orgId,
|
||||
machineId: machineId ?? null,
|
||||
kind,
|
||||
planned: kind === "downtime" ? planned : kind === "planned-downtime" ? "planned" : "all",
|
||||
shift,
|
||||
microstopLtMin,
|
||||
includeMoldChange,
|
||||
range, // ✅ now defined correctly
|
||||
start, // ✅ now defined correctly
|
||||
totalMinutesLost: kind === "downtime" || kind === "planned-downtime" ? total : undefined,
|
||||
totalScrap: kind === "scrap" ? total : undefined,
|
||||
rows,
|
||||
top3,
|
||||
threshold80,
|
||||
// (optional) keep old shape if anything else uses it:
|
||||
items: itemsRaw.map((x, i) => ({
|
||||
...x,
|
||||
cumPct: rows[i]?.cumulativePct ?? 0,
|
||||
})),
|
||||
total,
|
||||
});
|
||||
}
|
||||
45
app/api/debug/logs/route.ts
Normal file
45
app/api/debug/logs/route.ts
Normal file
@@ -0,0 +1,45 @@
|
||||
import type { NextRequest } from "next/server";
|
||||
import { NextResponse } from "next/server";
|
||||
import fs from "fs";
|
||||
import { getLogPath } from "@/lib/logger";
|
||||
|
||||
const MAX_LINES = 100;
|
||||
|
||||
/**
|
||||
* GET /api/debug/logs?key=YOUR_DEBUG_LOGS_KEY
|
||||
*
|
||||
* Returns the last MAX_LINES from the app log file. Set DEBUG_LOGS_KEY in .env
|
||||
* and call with ?key=... to view. If DEBUG_LOGS_KEY is unset, returns 401.
|
||||
*/
|
||||
export async function GET(req: NextRequest) {
|
||||
const key = req.nextUrl.searchParams.get("key");
|
||||
const secret = process.env.DEBUG_LOGS_KEY;
|
||||
|
||||
if (!secret || key !== secret) {
|
||||
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
|
||||
}
|
||||
|
||||
const logPath = getLogPath();
|
||||
try {
|
||||
const raw = fs.readFileSync(logPath, "utf8");
|
||||
const lines = raw.split("\n").filter(Boolean);
|
||||
const recent = lines.slice(-MAX_LINES);
|
||||
return NextResponse.json({
|
||||
logPath,
|
||||
lines: recent.length,
|
||||
entries: recent.map((line) => {
|
||||
try {
|
||||
return JSON.parse(line) as Record<string, unknown>;
|
||||
} catch {
|
||||
return { raw: line };
|
||||
}
|
||||
}),
|
||||
});
|
||||
} catch (err) {
|
||||
const message = err instanceof Error ? err.message : String(err);
|
||||
return NextResponse.json(
|
||||
{ error: "Failed to read log file", detail: message, logPath },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
31
app/api/debug/perf/route.ts
Normal file
31
app/api/debug/perf/route.ts
Normal file
@@ -0,0 +1,31 @@
|
||||
import type { NextRequest } from "next/server";
|
||||
import { NextResponse } from "next/server";
|
||||
import { logLine } from "@/lib/logger";
|
||||
|
||||
export const dynamic = "force-dynamic";
|
||||
|
||||
type PerfPayload = {
|
||||
event?: string;
|
||||
data?: Record<string, unknown>;
|
||||
};
|
||||
|
||||
export async function POST(req: NextRequest) {
|
||||
try {
|
||||
const body = (await req.json()) as PerfPayload;
|
||||
const type = typeof body?.event === "string" ? body.event : "nav";
|
||||
const data = body?.data && typeof body.data === "object" ? body.data : {};
|
||||
const userAgent = req.headers.get("user-agent") ?? "";
|
||||
|
||||
logLine("perf.client", {
|
||||
type,
|
||||
userAgent,
|
||||
...data,
|
||||
});
|
||||
|
||||
return NextResponse.json({ ok: true });
|
||||
} catch (err) {
|
||||
const message = err instanceof Error ? err.message : String(err);
|
||||
logLine("perf.client.error", { message });
|
||||
return NextResponse.json({ ok: false, error: "Bad payload" }, { status: 400 });
|
||||
}
|
||||
}
|
||||
226
app/api/downtime/actions/[id]/route.ts
Normal file
226
app/api/downtime/actions/[id]/route.ts
Normal file
@@ -0,0 +1,226 @@
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
import { z } from "zod";
|
||||
import { Prisma } from "@prisma/client";
|
||||
import { prisma } from "@/lib/prisma";
|
||||
import { requireSession } from "@/lib/auth/requireSession";
|
||||
import { buildDowntimeActionAssignedEmail, sendEmail } from "@/lib/email";
|
||||
import { getBaseUrl } from "@/lib/appUrl";
|
||||
|
||||
const STATUS = ["open", "in_progress", "blocked", "done"] as const;
|
||||
const PRIORITY = ["low", "medium", "high"] as const;
|
||||
const DATE_RE = /^\d{4}-\d{2}-\d{2}$/;
|
||||
|
||||
const updateSchema = z.object({
|
||||
machineId: z.string().trim().min(1).optional().nullable(),
|
||||
reasonCode: z.string().trim().min(1).max(64).optional().nullable(),
|
||||
hmDay: z.number().int().min(0).max(6).optional().nullable(),
|
||||
hmHour: z.number().int().min(0).max(23).optional().nullable(),
|
||||
title: z.string().trim().min(1).max(160).optional(),
|
||||
notes: z.string().trim().max(4000).optional().nullable(),
|
||||
ownerUserId: z.string().trim().min(1).optional().nullable(),
|
||||
dueDate: z.string().trim().regex(DATE_RE).optional().nullable(),
|
||||
status: z.enum(STATUS).optional(),
|
||||
priority: z.enum(PRIORITY).optional(),
|
||||
});
|
||||
|
||||
function parseDueDate(value?: string | null) {
|
||||
if (value === undefined) return undefined;
|
||||
if (!value) return null;
|
||||
return new Date(`${value}T00:00:00.000Z`);
|
||||
}
|
||||
|
||||
function formatDueDate(value?: Date | null) {
|
||||
if (!value) return null;
|
||||
return value.toISOString().slice(0, 10);
|
||||
}
|
||||
|
||||
function buildActionUrl(baseUrl: string, action: { machineId: string | null; reasonCode: string | null; hmDay: number | null; hmHour: number | null }) {
|
||||
const params = new URLSearchParams();
|
||||
if (action.machineId) params.set("machineId", action.machineId);
|
||||
if (action.reasonCode) params.set("reasonCode", action.reasonCode);
|
||||
if (action.hmDay != null && action.hmHour != null) {
|
||||
params.set("hmDay", String(action.hmDay));
|
||||
params.set("hmHour", String(action.hmHour));
|
||||
}
|
||||
const qs = params.toString();
|
||||
return qs ? `${baseUrl}/downtime?${qs}` : `${baseUrl}/downtime`;
|
||||
}
|
||||
|
||||
function serializeAction(action: {
|
||||
id: string;
|
||||
createdAt: Date;
|
||||
updatedAt: Date;
|
||||
machineId: string | null;
|
||||
reasonCode: string | null;
|
||||
hmDay: number | null;
|
||||
hmHour: number | null;
|
||||
title: string;
|
||||
notes: string | null;
|
||||
ownerUserId: string | null;
|
||||
dueDate: Date | null;
|
||||
status: string;
|
||||
priority: string;
|
||||
ownerUser?: { name: string | null; email: string } | null;
|
||||
}) {
|
||||
return {
|
||||
id: action.id,
|
||||
createdAt: action.createdAt.toISOString(),
|
||||
updatedAt: action.updatedAt.toISOString(),
|
||||
machineId: action.machineId,
|
||||
reasonCode: action.reasonCode,
|
||||
hmDay: action.hmDay,
|
||||
hmHour: action.hmHour,
|
||||
title: action.title,
|
||||
notes: action.notes ?? "",
|
||||
ownerUserId: action.ownerUserId,
|
||||
ownerName: action.ownerUser?.name ?? null,
|
||||
ownerEmail: action.ownerUser?.email ?? null,
|
||||
dueDate: formatDueDate(action.dueDate),
|
||||
status: action.status,
|
||||
priority: action.priority,
|
||||
};
|
||||
}
|
||||
|
||||
export async function PATCH(req: NextRequest, context: { params: Promise<{ id: string }> }) {
|
||||
const session = await requireSession();
|
||||
if (!session) return NextResponse.json({ ok: false, error: "Unauthorized" }, { status: 401 });
|
||||
|
||||
const { id } = await context.params;
|
||||
const body = await req.json().catch(() => ({}));
|
||||
const parsed = updateSchema.safeParse(body);
|
||||
if (!parsed.success) {
|
||||
return NextResponse.json({ ok: false, error: "Invalid action payload" }, { status: 400 });
|
||||
}
|
||||
|
||||
const data = parsed.data;
|
||||
if (("hmDay" in data || "hmHour" in data) && (data.hmDay == null) !== (data.hmHour == null)) {
|
||||
return NextResponse.json({ ok: false, error: "Heatmap requires hmDay and hmHour" }, { status: 400 });
|
||||
}
|
||||
|
||||
const existing = await prisma.downtimeAction.findFirst({
|
||||
where: { id, orgId: session.orgId },
|
||||
include: { ownerUser: { select: { name: true, email: true } } },
|
||||
});
|
||||
if (!existing) {
|
||||
return NextResponse.json({ ok: false, error: "Action not found" }, { status: 404 });
|
||||
}
|
||||
|
||||
if (data.machineId) {
|
||||
const machine = await prisma.machine.findFirst({
|
||||
where: { id: data.machineId, orgId: session.orgId },
|
||||
select: { id: true },
|
||||
});
|
||||
if (!machine) {
|
||||
return NextResponse.json({ ok: false, error: "Invalid machineId" }, { status: 400 });
|
||||
}
|
||||
}
|
||||
|
||||
let ownerMembership: { user: { name: string | null; email: string } } | null = null;
|
||||
if (data.ownerUserId) {
|
||||
ownerMembership = await prisma.orgUser.findUnique({
|
||||
where: { orgId_userId: { orgId: session.orgId, userId: data.ownerUserId } },
|
||||
include: { user: { select: { name: true, email: true } } },
|
||||
});
|
||||
if (!ownerMembership) {
|
||||
return NextResponse.json({ ok: false, error: "Invalid ownerUserId" }, { status: 400 });
|
||||
}
|
||||
}
|
||||
|
||||
let completedAt: Date | null | undefined = undefined;
|
||||
if ("status" in data) {
|
||||
completedAt = data.status === "done" ? existing.completedAt ?? new Date() : null;
|
||||
}
|
||||
|
||||
const updateData: Prisma.DowntimeActionUncheckedUpdateInput = {};
|
||||
let shouldResetReminder = false;
|
||||
if ("machineId" in data) updateData.machineId = data.machineId;
|
||||
if ("reasonCode" in data) updateData.reasonCode = data.reasonCode;
|
||||
if ("hmDay" in data) updateData.hmDay = data.hmDay;
|
||||
if ("hmHour" in data) updateData.hmHour = data.hmHour;
|
||||
if ("title" in data) updateData.title = data.title?.trim();
|
||||
if ("notes" in data) updateData.notes = data.notes == null ? null : data.notes.trim() || null;
|
||||
if ("ownerUserId" in data) updateData.ownerUserId = data.ownerUserId;
|
||||
if ("dueDate" in data) {
|
||||
const nextDue = parseDueDate(data.dueDate);
|
||||
const prev = formatDueDate(existing.dueDate);
|
||||
const next = formatDueDate(nextDue ?? null);
|
||||
updateData.dueDate = nextDue;
|
||||
if (prev !== next) {
|
||||
shouldResetReminder = true;
|
||||
}
|
||||
}
|
||||
if ("status" in data) updateData.status = data.status;
|
||||
if ("priority" in data) updateData.priority = data.priority;
|
||||
if (completedAt !== undefined) updateData.completedAt = completedAt;
|
||||
if (shouldResetReminder) {
|
||||
updateData.reminderStage = null;
|
||||
updateData.lastReminderAt = null;
|
||||
}
|
||||
|
||||
const updated = await prisma.downtimeAction.update({
|
||||
where: { id: existing.id },
|
||||
data: updateData,
|
||||
include: { ownerUser: { select: { name: true, email: true } } },
|
||||
});
|
||||
|
||||
const ownerChanged = "ownerUserId" in data && data.ownerUserId !== existing.ownerUserId;
|
||||
const dueChanged =
|
||||
"dueDate" in data && formatDueDate(existing.dueDate) !== formatDueDate(updated.dueDate);
|
||||
|
||||
let emailSent = false;
|
||||
let emailError: string | null = null;
|
||||
if ((ownerChanged || dueChanged) && updated.ownerUser?.email) {
|
||||
try {
|
||||
const org = await prisma.org.findUnique({
|
||||
where: { id: session.orgId },
|
||||
select: { name: true },
|
||||
});
|
||||
const baseUrl = getBaseUrl(req);
|
||||
const actionUrl = buildActionUrl(baseUrl, updated);
|
||||
const content = buildDowntimeActionAssignedEmail({
|
||||
appName: "MIS Control Tower",
|
||||
orgName: org?.name || "your organization",
|
||||
actionTitle: updated.title,
|
||||
assigneeName: updated.ownerUser.name ?? updated.ownerUser.email,
|
||||
dueDate: formatDueDate(updated.dueDate),
|
||||
actionUrl,
|
||||
priority: updated.priority,
|
||||
status: updated.status,
|
||||
});
|
||||
await sendEmail({
|
||||
to: updated.ownerUser.email,
|
||||
subject: content.subject,
|
||||
text: content.text,
|
||||
html: content.html,
|
||||
});
|
||||
emailSent = true;
|
||||
} catch (err: unknown) {
|
||||
emailError = err instanceof Error ? err.message : "Failed to send assignment email";
|
||||
}
|
||||
}
|
||||
|
||||
return NextResponse.json({
|
||||
ok: true,
|
||||
action: serializeAction(updated),
|
||||
emailSent,
|
||||
emailError,
|
||||
});
|
||||
}
|
||||
|
||||
export async function DELETE(req: NextRequest, context: { params: Promise<{ id: string }> }) {
|
||||
const session = await requireSession();
|
||||
if (!session) return NextResponse.json({ ok: false, error: "Unauthorized" }, { status: 401 });
|
||||
|
||||
const { id } = await context.params;
|
||||
const existing = await prisma.downtimeAction.findFirst({
|
||||
where: { id, orgId: session.orgId },
|
||||
select: { id: true },
|
||||
});
|
||||
if (!existing) {
|
||||
return NextResponse.json({ ok: false, error: "Action not found" }, { status: 404 });
|
||||
}
|
||||
|
||||
await prisma.downtimeAction.delete({ where: { id: existing.id } });
|
||||
|
||||
return NextResponse.json({ ok: true });
|
||||
}
|
||||
123
app/api/downtime/actions/reminders/route.ts
Normal file
123
app/api/downtime/actions/reminders/route.ts
Normal file
@@ -0,0 +1,123 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import { prisma } from "@/lib/prisma";
|
||||
import { requireSession } from "@/lib/auth/requireSession";
|
||||
import { buildDowntimeActionReminderEmail, sendEmail } from "@/lib/email";
|
||||
import { getBaseUrl } from "@/lib/appUrl";
|
||||
|
||||
const DEFAULT_DUE_DAYS = 7;
|
||||
const DEFAULT_LIMIT = 100;
|
||||
const MS_PER_HOUR = 60 * 60 * 1000;
|
||||
|
||||
type ReminderStage = "week" | "day" | "hour" | "overdue";
|
||||
|
||||
function formatDueDate(value?: Date | null) {
|
||||
if (!value) return null;
|
||||
return value.toISOString().slice(0, 10);
|
||||
}
|
||||
|
||||
function getReminderStage(dueDate: Date, now: Date): ReminderStage | null {
|
||||
const diffMs = dueDate.getTime() - now.getTime();
|
||||
if (diffMs <= 0) return "overdue";
|
||||
if (diffMs <= MS_PER_HOUR) return "hour";
|
||||
if (diffMs <= 24 * MS_PER_HOUR) return "day";
|
||||
if (diffMs <= 7 * 24 * MS_PER_HOUR) return "week";
|
||||
return null;
|
||||
}
|
||||
|
||||
function buildActionUrl(baseUrl: string, action: { machineId: string | null; reasonCode: string | null; hmDay: number | null; hmHour: number | null }) {
|
||||
const params = new URLSearchParams();
|
||||
if (action.machineId) params.set("machineId", action.machineId);
|
||||
if (action.reasonCode) params.set("reasonCode", action.reasonCode);
|
||||
if (action.hmDay != null && action.hmHour != null) {
|
||||
params.set("hmDay", String(action.hmDay));
|
||||
params.set("hmHour", String(action.hmHour));
|
||||
}
|
||||
const qs = params.toString();
|
||||
return qs ? `${baseUrl}/downtime?${qs}` : `${baseUrl}/downtime`;
|
||||
}
|
||||
|
||||
async function authorizeRequest(req: Request) {
|
||||
const secret = process.env.DOWNTIME_ACTION_REMINDER_SECRET;
|
||||
if (!secret) {
|
||||
const session = await requireSession();
|
||||
return { ok: !!session };
|
||||
}
|
||||
const authHeader = req.headers.get("authorization") || "";
|
||||
const token = authHeader.startsWith("Bearer ") ? authHeader.slice(7).trim() : null;
|
||||
const urlToken = new URL(req.url).searchParams.get("token");
|
||||
return { ok: token === secret || urlToken === secret };
|
||||
}
|
||||
|
||||
export async function POST(req: Request) {
|
||||
const auth = await authorizeRequest(req);
|
||||
if (!auth.ok) return NextResponse.json({ ok: false, error: "Unauthorized" }, { status: 401 });
|
||||
|
||||
const sp = new URL(req.url).searchParams;
|
||||
const dueInDays = Number(sp.get("dueInDays") || DEFAULT_DUE_DAYS);
|
||||
const limit = Number(sp.get("limit") || DEFAULT_LIMIT);
|
||||
|
||||
const now = new Date();
|
||||
const dueBy = new Date(now.getTime() + dueInDays * 24 * 60 * 60 * 1000);
|
||||
|
||||
const actions = await prisma.downtimeAction.findMany({
|
||||
where: {
|
||||
status: { not: "done" },
|
||||
ownerUserId: { not: null },
|
||||
dueDate: { not: null, lte: dueBy },
|
||||
},
|
||||
include: {
|
||||
ownerUser: { select: { name: true, email: true } },
|
||||
org: { select: { name: true } },
|
||||
},
|
||||
orderBy: { dueDate: "asc" },
|
||||
take: Number.isFinite(limit) ? Math.max(1, Math.min(500, limit)) : DEFAULT_LIMIT,
|
||||
});
|
||||
|
||||
const baseUrl = getBaseUrl(req);
|
||||
const sentIds: string[] = [];
|
||||
const failures: Array<{ id: string; error: string }> = [];
|
||||
|
||||
for (const action of actions) {
|
||||
const email = action.ownerUser?.email;
|
||||
if (!email) continue;
|
||||
if (!action.dueDate) continue;
|
||||
const stage = getReminderStage(action.dueDate, now);
|
||||
if (!stage) continue;
|
||||
if (action.reminderStage === stage) continue;
|
||||
try {
|
||||
const content = buildDowntimeActionReminderEmail({
|
||||
appName: "MIS Control Tower",
|
||||
orgName: action.org.name,
|
||||
actionTitle: action.title,
|
||||
assigneeName: action.ownerUser?.name ?? email,
|
||||
dueDate: formatDueDate(action.dueDate),
|
||||
actionUrl: buildActionUrl(baseUrl, action),
|
||||
priority: action.priority,
|
||||
status: action.status,
|
||||
});
|
||||
await sendEmail({
|
||||
to: email,
|
||||
subject: content.subject,
|
||||
text: content.text,
|
||||
html: content.html,
|
||||
});
|
||||
sentIds.push(action.id);
|
||||
await prisma.downtimeAction.update({
|
||||
where: { id: action.id },
|
||||
data: { reminderStage: stage, lastReminderAt: now },
|
||||
});
|
||||
} catch (err: unknown) {
|
||||
failures.push({
|
||||
id: action.id,
|
||||
error: err instanceof Error ? err.message : "Failed to send reminder email",
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
return NextResponse.json({
|
||||
ok: true,
|
||||
sent: sentIds.length,
|
||||
failed: failures.length,
|
||||
failures,
|
||||
});
|
||||
}
|
||||
226
app/api/downtime/actions/route.ts
Normal file
226
app/api/downtime/actions/route.ts
Normal file
@@ -0,0 +1,226 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import { z } from "zod";
|
||||
import { prisma } from "@/lib/prisma";
|
||||
import { requireSession } from "@/lib/auth/requireSession";
|
||||
import { buildDowntimeActionAssignedEmail, sendEmail } from "@/lib/email";
|
||||
import { getBaseUrl } from "@/lib/appUrl";
|
||||
|
||||
const STATUS = ["open", "in_progress", "blocked", "done"] as const;
|
||||
const PRIORITY = ["low", "medium", "high"] as const;
|
||||
const DATE_RE = /^\d{4}-\d{2}-\d{2}$/;
|
||||
|
||||
const createSchema = z.object({
|
||||
machineId: z.string().trim().min(1).optional().nullable(),
|
||||
reasonCode: z.string().trim().min(1).max(64).optional().nullable(),
|
||||
hmDay: z.number().int().min(0).max(6).optional().nullable(),
|
||||
hmHour: z.number().int().min(0).max(23).optional().nullable(),
|
||||
title: z.string().trim().min(1).max(160),
|
||||
notes: z.string().trim().max(4000).optional().nullable(),
|
||||
ownerUserId: z.string().trim().min(1).optional().nullable(),
|
||||
dueDate: z.string().trim().regex(DATE_RE).optional().nullable(),
|
||||
status: z.enum(STATUS).optional(),
|
||||
priority: z.enum(PRIORITY).optional(),
|
||||
});
|
||||
|
||||
function parseDueDate(value?: string | null) {
|
||||
if (!value) return null;
|
||||
return new Date(`${value}T00:00:00.000Z`);
|
||||
}
|
||||
|
||||
function formatDueDate(value?: Date | null) {
|
||||
if (!value) return null;
|
||||
return value.toISOString().slice(0, 10);
|
||||
}
|
||||
|
||||
function buildActionUrl(baseUrl: string, action: { machineId: string | null; reasonCode: string | null; hmDay: number | null; hmHour: number | null }) {
|
||||
const params = new URLSearchParams();
|
||||
if (action.machineId) params.set("machineId", action.machineId);
|
||||
if (action.reasonCode) params.set("reasonCode", action.reasonCode);
|
||||
if (action.hmDay != null && action.hmHour != null) {
|
||||
params.set("hmDay", String(action.hmDay));
|
||||
params.set("hmHour", String(action.hmHour));
|
||||
}
|
||||
const qs = params.toString();
|
||||
return qs ? `${baseUrl}/downtime?${qs}` : `${baseUrl}/downtime`;
|
||||
}
|
||||
|
||||
function serializeAction(action: {
|
||||
id: string;
|
||||
createdAt: Date;
|
||||
updatedAt: Date;
|
||||
machineId: string | null;
|
||||
reasonCode: string | null;
|
||||
hmDay: number | null;
|
||||
hmHour: number | null;
|
||||
title: string;
|
||||
notes: string | null;
|
||||
ownerUserId: string | null;
|
||||
dueDate: Date | null;
|
||||
status: string;
|
||||
priority: string;
|
||||
ownerUser?: { name: string | null; email: string } | null;
|
||||
}) {
|
||||
return {
|
||||
id: action.id,
|
||||
createdAt: action.createdAt.toISOString(),
|
||||
updatedAt: action.updatedAt.toISOString(),
|
||||
machineId: action.machineId,
|
||||
reasonCode: action.reasonCode,
|
||||
hmDay: action.hmDay,
|
||||
hmHour: action.hmHour,
|
||||
title: action.title,
|
||||
notes: action.notes ?? "",
|
||||
ownerUserId: action.ownerUserId,
|
||||
ownerName: action.ownerUser?.name ?? null,
|
||||
ownerEmail: action.ownerUser?.email ?? null,
|
||||
dueDate: formatDueDate(action.dueDate),
|
||||
status: action.status,
|
||||
priority: action.priority,
|
||||
};
|
||||
}
|
||||
|
||||
export async function GET(req: Request) {
|
||||
const session = await requireSession();
|
||||
if (!session) return NextResponse.json({ ok: false, error: "Unauthorized" }, { status: 401 });
|
||||
|
||||
const sp = new URL(req.url).searchParams;
|
||||
const machineId = sp.get("machineId");
|
||||
const reasonCode = sp.get("reasonCode");
|
||||
const hmDayStr = sp.get("hmDay");
|
||||
const hmHourStr = sp.get("hmHour");
|
||||
|
||||
const hmDay = hmDayStr != null ? Number(hmDayStr) : null;
|
||||
const hmHour = hmHourStr != null ? Number(hmHourStr) : null;
|
||||
if ((hmDayStr != null || hmHourStr != null) && (!Number.isFinite(hmDay) || !Number.isFinite(hmHour))) {
|
||||
return NextResponse.json({ ok: false, error: "Invalid heatmap selection" }, { status: 400 });
|
||||
}
|
||||
if ((hmDayStr != null || hmHourStr != null) && (hmDay == null || hmHour == null)) {
|
||||
return NextResponse.json({ ok: false, error: "Heatmap requires hmDay and hmHour" }, { status: 400 });
|
||||
}
|
||||
|
||||
const where: {
|
||||
orgId: string;
|
||||
AND?: Array<Record<string, unknown>>;
|
||||
} = { orgId: session.orgId };
|
||||
|
||||
if (machineId) {
|
||||
where.AND = [...(where.AND ?? []), { OR: [{ machineId }, { machineId: null }] }];
|
||||
}
|
||||
|
||||
if (reasonCode) {
|
||||
where.AND = [...(where.AND ?? []), { OR: [{ reasonCode }, { reasonCode: null }] }];
|
||||
}
|
||||
|
||||
if (hmDay != null && hmHour != null) {
|
||||
where.AND = [
|
||||
...(where.AND ?? []),
|
||||
{ OR: [{ hmDay, hmHour }, { hmDay: null, hmHour: null }] },
|
||||
];
|
||||
}
|
||||
|
||||
const actions = await prisma.downtimeAction.findMany({
|
||||
where,
|
||||
orderBy: { updatedAt: "desc" },
|
||||
include: { ownerUser: { select: { name: true, email: true } } },
|
||||
});
|
||||
|
||||
return NextResponse.json({
|
||||
ok: true,
|
||||
actions: actions.map(serializeAction),
|
||||
});
|
||||
}
|
||||
|
||||
export async function POST(req: Request) {
|
||||
const session = await requireSession();
|
||||
if (!session) return NextResponse.json({ ok: false, error: "Unauthorized" }, { status: 401 });
|
||||
|
||||
const body = await req.json().catch(() => ({}));
|
||||
const parsed = createSchema.safeParse(body);
|
||||
if (!parsed.success) {
|
||||
return NextResponse.json({ ok: false, error: "Invalid action payload" }, { status: 400 });
|
||||
}
|
||||
|
||||
const data = parsed.data;
|
||||
if ((data.hmDay == null) !== (data.hmHour == null)) {
|
||||
return NextResponse.json({ ok: false, error: "Heatmap requires hmDay and hmHour" }, { status: 400 });
|
||||
}
|
||||
|
||||
if (data.machineId) {
|
||||
const machine = await prisma.machine.findFirst({
|
||||
where: { id: data.machineId, orgId: session.orgId },
|
||||
select: { id: true },
|
||||
});
|
||||
if (!machine) {
|
||||
return NextResponse.json({ ok: false, error: "Invalid machineId" }, { status: 400 });
|
||||
}
|
||||
}
|
||||
|
||||
let ownerMembership: { user: { name: string | null; email: string } } | null = null;
|
||||
if (data.ownerUserId) {
|
||||
ownerMembership = await prisma.orgUser.findUnique({
|
||||
where: { orgId_userId: { orgId: session.orgId, userId: data.ownerUserId } },
|
||||
include: { user: { select: { name: true, email: true } } },
|
||||
});
|
||||
if (!ownerMembership) {
|
||||
return NextResponse.json({ ok: false, error: "Invalid ownerUserId" }, { status: 400 });
|
||||
}
|
||||
}
|
||||
|
||||
const created = await prisma.downtimeAction.create({
|
||||
data: {
|
||||
orgId: session.orgId,
|
||||
machineId: data.machineId ?? null,
|
||||
reasonCode: data.reasonCode ?? null,
|
||||
hmDay: data.hmDay ?? null,
|
||||
hmHour: data.hmHour ?? null,
|
||||
title: data.title.trim(),
|
||||
notes: data.notes?.trim() || null,
|
||||
ownerUserId: data.ownerUserId ?? null,
|
||||
dueDate: parseDueDate(data.dueDate),
|
||||
status: data.status ?? "open",
|
||||
priority: data.priority ?? "medium",
|
||||
completedAt: data.status === "done" ? new Date() : null,
|
||||
createdBy: session.userId,
|
||||
},
|
||||
include: { ownerUser: { select: { name: true, email: true } } },
|
||||
});
|
||||
|
||||
let emailSent = false;
|
||||
let emailError: string | null = null;
|
||||
if (ownerMembership?.user?.email) {
|
||||
try {
|
||||
const org = await prisma.org.findUnique({
|
||||
where: { id: session.orgId },
|
||||
select: { name: true },
|
||||
});
|
||||
const baseUrl = getBaseUrl(req);
|
||||
const actionUrl = buildActionUrl(baseUrl, created);
|
||||
const content = buildDowntimeActionAssignedEmail({
|
||||
appName: "MIS Control Tower",
|
||||
orgName: org?.name || "your organization",
|
||||
actionTitle: created.title,
|
||||
assigneeName: ownerMembership.user.name ?? ownerMembership.user.email,
|
||||
dueDate: formatDueDate(created.dueDate),
|
||||
actionUrl,
|
||||
priority: created.priority,
|
||||
status: created.status,
|
||||
});
|
||||
await sendEmail({
|
||||
to: ownerMembership.user.email,
|
||||
subject: content.subject,
|
||||
text: content.text,
|
||||
html: content.html,
|
||||
});
|
||||
emailSent = true;
|
||||
} catch (err: unknown) {
|
||||
emailError = err instanceof Error ? err.message : "Failed to send assignment email";
|
||||
}
|
||||
}
|
||||
|
||||
return NextResponse.json({
|
||||
ok: true,
|
||||
action: serializeAction(created),
|
||||
emailSent,
|
||||
emailError,
|
||||
});
|
||||
}
|
||||
309
app/api/financial/costs/route.ts
Normal file
309
app/api/financial/costs/route.ts
Normal file
@@ -0,0 +1,309 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import type { NextRequest } from "next/server";
|
||||
import { createHash } from "crypto";
|
||||
import { revalidateTag } from "next/cache";
|
||||
import { prisma } from "@/lib/prisma";
|
||||
import { requireSession } from "@/lib/auth/requireSession";
|
||||
import { Prisma } from "@prisma/client";
|
||||
import { z } from "zod";
|
||||
import {
|
||||
FINANCIAL_CONFIG_SWR_SEC,
|
||||
FINANCIAL_CONFIG_TTL_SEC,
|
||||
getFinancialConfig,
|
||||
type FinancialConfigPayload,
|
||||
} from "@/lib/financial/cache";
|
||||
|
||||
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,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
function toMs(value?: Date | string | null) {
|
||||
if (!value) return 0;
|
||||
const date = typeof value === "string" ? new Date(value) : value;
|
||||
const ms = date.getTime();
|
||||
return Number.isNaN(ms) ? 0 : ms;
|
||||
}
|
||||
|
||||
function maxUpdatedMs(rows: Array<{ updatedAt?: Date | string | null }>) {
|
||||
let max = 0;
|
||||
for (const row of rows) {
|
||||
const ms = toMs(row.updatedAt);
|
||||
if (ms > max) max = ms;
|
||||
}
|
||||
return max;
|
||||
}
|
||||
|
||||
function buildConfigEtag(orgId: string, payload: FinancialConfigPayload) {
|
||||
const parts = [
|
||||
orgId,
|
||||
toMs(payload.org?.updatedAt),
|
||||
maxUpdatedMs(payload.locations ?? []),
|
||||
maxUpdatedMs(payload.machines ?? []),
|
||||
maxUpdatedMs(payload.products ?? []),
|
||||
payload.locations?.length ?? 0,
|
||||
payload.machines?.length ?? 0,
|
||||
payload.products?.length ?? 0,
|
||||
];
|
||||
return `W/"${createHash("sha1").update(parts.join("|")).digest("hex")}"`;
|
||||
}
|
||||
|
||||
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 refresh = url.searchParams.get("refresh") === "1";
|
||||
|
||||
await prisma.$transaction((tx) => ensureOrgFinancialProfile(tx, session.orgId, session.userId));
|
||||
const payload = await getFinancialConfig(session.orgId, { refresh });
|
||||
|
||||
const etag = buildConfigEtag(session.orgId, payload);
|
||||
const responseHeaders = new Headers({
|
||||
"Cache-Control": `private, max-age=${FINANCIAL_CONFIG_TTL_SEC}, stale-while-revalidate=${FINANCIAL_CONFIG_SWR_SEC}`,
|
||||
ETag: etag,
|
||||
Vary: "Cookie",
|
||||
});
|
||||
|
||||
const ifNoneMatch = req.headers.get("if-none-match");
|
||||
if (!refresh && ifNoneMatch && ifNoneMatch === etag) {
|
||||
return new NextResponse(null, { status: 304, headers: responseHeaders });
|
||||
}
|
||||
|
||||
return NextResponse.json({ ok: true, ...payload }, { headers: responseHeaders });
|
||||
}
|
||||
|
||||
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,
|
||||
},
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
revalidateTag(`financial-config:${session.orgId}`, { expire: 0 });
|
||||
revalidateTag(`financial-impact:${session.orgId}`, { expire: 0 });
|
||||
|
||||
const payload = await getFinancialConfig(session.orgId, { refresh: true });
|
||||
return NextResponse.json({ ok: true, ...payload });
|
||||
}
|
||||
156
app/api/financial/export/excel/route.ts
Normal file
156
app/api/financial/export/excel/route.ts
Normal file
@@ -0,0 +1,156 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import type { NextRequest } from "next/server";
|
||||
import { requireSession } from "@/lib/auth/requireSession";
|
||||
import { prisma } from "@/lib/prisma";
|
||||
import { computeFinancialImpact } from "@/lib/financial/impact";
|
||||
|
||||
const RANGE_MS: Record<string, number> = {
|
||||
"24h": 24 * 60 * 60 * 1000,
|
||||
"7d": 7 * 24 * 60 * 60 * 1000,
|
||||
"30d": 30 * 24 * 60 * 60 * 1000,
|
||||
};
|
||||
|
||||
function canManageFinancials(role?: string | null) {
|
||||
return role === "OWNER";
|
||||
}
|
||||
|
||||
function parseDate(input?: string | null) {
|
||||
if (!input) return null;
|
||||
const n = Number(input);
|
||||
if (!Number.isNaN(n)) return new Date(n);
|
||||
const d = new Date(input);
|
||||
return Number.isNaN(d.getTime()) ? null : d;
|
||||
}
|
||||
|
||||
function pickRange(req: NextRequest) {
|
||||
const url = new URL(req.url);
|
||||
const range = url.searchParams.get("range") ?? "7d";
|
||||
const now = new Date();
|
||||
|
||||
if (range === "custom") {
|
||||
const start = parseDate(url.searchParams.get("start")) ?? new Date(now.getTime() - RANGE_MS["24h"]);
|
||||
const end = parseDate(url.searchParams.get("end")) ?? now;
|
||||
return { start, end };
|
||||
}
|
||||
|
||||
const ms = RANGE_MS[range] ?? RANGE_MS["24h"];
|
||||
return { start: new Date(now.getTime() - ms), end: now };
|
||||
}
|
||||
|
||||
function csvValue(value: string | number | null | undefined) {
|
||||
if (value === null || value === undefined) return "";
|
||||
const text = String(value);
|
||||
if (/[",\n]/.test(text)) {
|
||||
return `"${text.replace(/"/g, "\"\"")}"`;
|
||||
}
|
||||
return text;
|
||||
}
|
||||
|
||||
function formatNumber(value: number | null) {
|
||||
if (value == null || !Number.isFinite(value)) return "";
|
||||
return value.toFixed(4);
|
||||
}
|
||||
|
||||
function slugify(value: string) {
|
||||
return value
|
||||
.toLowerCase()
|
||||
.replace(/[^a-z0-9]+/g, "-")
|
||||
.replace(/^-+|-+$/g, "")
|
||||
.slice(0, 60) || "report";
|
||||
}
|
||||
|
||||
export async function GET(req: NextRequest) {
|
||||
const session = await requireSession();
|
||||
if (!session) return NextResponse.json({ ok: false, error: "Unauthorized" }, { status: 401 });
|
||||
|
||||
const membership = await prisma.orgUser.findUnique({
|
||||
where: { orgId_userId: { orgId: session.orgId, userId: session.userId } },
|
||||
select: { role: true },
|
||||
});
|
||||
if (!canManageFinancials(membership?.role)) {
|
||||
return NextResponse.json({ ok: false, error: "Forbidden" }, { status: 403 });
|
||||
}
|
||||
|
||||
const url = new URL(req.url);
|
||||
const { start, end } = pickRange(req);
|
||||
const machineId = url.searchParams.get("machineId") ?? undefined;
|
||||
const location = url.searchParams.get("location") ?? undefined;
|
||||
const sku = url.searchParams.get("sku") ?? undefined;
|
||||
const currency = url.searchParams.get("currency") ?? undefined;
|
||||
|
||||
const [org, impact] = await Promise.all([
|
||||
prisma.org.findUnique({ where: { id: session.orgId }, select: { name: true } }),
|
||||
computeFinancialImpact({
|
||||
orgId: session.orgId,
|
||||
start,
|
||||
end,
|
||||
machineId,
|
||||
location,
|
||||
sku,
|
||||
currency,
|
||||
includeEvents: true,
|
||||
}),
|
||||
]);
|
||||
|
||||
const orgName = org?.name ?? "Organization";
|
||||
const header = [
|
||||
"org_name",
|
||||
"range_start",
|
||||
"range_end",
|
||||
"event_id",
|
||||
"event_ts",
|
||||
"event_type",
|
||||
"status",
|
||||
"severity",
|
||||
"category",
|
||||
"machine_id",
|
||||
"machine_name",
|
||||
"location",
|
||||
"work_order_id",
|
||||
"sku",
|
||||
"duration_sec",
|
||||
"cost_machine",
|
||||
"cost_operator",
|
||||
"cost_energy",
|
||||
"cost_scrap",
|
||||
"cost_raw_material",
|
||||
"cost_total",
|
||||
"currency",
|
||||
];
|
||||
|
||||
const rows = impact.events.map((event) => [
|
||||
orgName,
|
||||
start.toISOString(),
|
||||
end.toISOString(),
|
||||
event.id,
|
||||
event.ts.toISOString(),
|
||||
event.eventType,
|
||||
event.status,
|
||||
event.severity,
|
||||
event.category,
|
||||
event.machineId,
|
||||
event.machineName ?? "",
|
||||
event.location ?? "",
|
||||
event.workOrderId ?? "",
|
||||
event.sku ?? "",
|
||||
formatNumber(event.durationSec),
|
||||
formatNumber(event.costMachine),
|
||||
formatNumber(event.costOperator),
|
||||
formatNumber(event.costEnergy),
|
||||
formatNumber(event.costScrap),
|
||||
formatNumber(event.costRawMaterial),
|
||||
formatNumber(event.costTotal),
|
||||
event.currency,
|
||||
]);
|
||||
|
||||
const lines = [header, ...rows].map((row) => row.map(csvValue).join(","));
|
||||
const csv = lines.join("\n");
|
||||
|
||||
const fileName = `financial_events_${slugify(orgName)}.csv`;
|
||||
return new NextResponse(csv, {
|
||||
headers: {
|
||||
"Content-Type": "text/csv; charset=utf-8",
|
||||
"Content-Disposition": `attachment; filename=\"${fileName}\"`,
|
||||
},
|
||||
});
|
||||
}
|
||||
246
app/api/financial/export/pdf/route.ts
Normal file
246
app/api/financial/export/pdf/route.ts
Normal file
@@ -0,0 +1,246 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import type { NextRequest } from "next/server";
|
||||
import { requireSession } from "@/lib/auth/requireSession";
|
||||
import { prisma } from "@/lib/prisma";
|
||||
import { computeFinancialImpact } from "@/lib/financial/impact";
|
||||
|
||||
const RANGE_MS: Record<string, number> = {
|
||||
"24h": 24 * 60 * 60 * 1000,
|
||||
"7d": 7 * 24 * 60 * 60 * 1000,
|
||||
"30d": 30 * 24 * 60 * 60 * 1000,
|
||||
};
|
||||
|
||||
function canManageFinancials(role?: string | null) {
|
||||
return role === "OWNER";
|
||||
}
|
||||
|
||||
function parseDate(input?: string | null) {
|
||||
if (!input) return null;
|
||||
const n = Number(input);
|
||||
if (!Number.isNaN(n)) return new Date(n);
|
||||
const d = new Date(input);
|
||||
return Number.isNaN(d.getTime()) ? null : d;
|
||||
}
|
||||
|
||||
function pickRange(req: NextRequest) {
|
||||
const url = new URL(req.url);
|
||||
const range = url.searchParams.get("range") ?? "7d";
|
||||
const now = new Date();
|
||||
|
||||
if (range === "custom") {
|
||||
const start = parseDate(url.searchParams.get("start")) ?? new Date(now.getTime() - RANGE_MS["24h"]);
|
||||
const end = parseDate(url.searchParams.get("end")) ?? now;
|
||||
return { start, end };
|
||||
}
|
||||
|
||||
const ms = RANGE_MS[range] ?? RANGE_MS["24h"];
|
||||
return { start: new Date(now.getTime() - ms), end: now };
|
||||
}
|
||||
|
||||
function escapeHtml(value: string) {
|
||||
return value
|
||||
.replace(/&/g, "&")
|
||||
.replace(/</g, "<")
|
||||
.replace(/>/g, ">")
|
||||
.replace(/\"/g, """)
|
||||
.replace(/'/g, "'");
|
||||
}
|
||||
|
||||
function formatMoney(value: number, currency: string) {
|
||||
if (!Number.isFinite(value)) return "--";
|
||||
try {
|
||||
return new Intl.NumberFormat("en-US", { style: "currency", currency, maximumFractionDigits: 2 }).format(value);
|
||||
} catch {
|
||||
return `${value.toFixed(2)} ${currency}`;
|
||||
}
|
||||
}
|
||||
|
||||
function formatNumber(value: number | null, digits = 2) {
|
||||
if (value == null || !Number.isFinite(value)) return "--";
|
||||
return value.toFixed(digits);
|
||||
}
|
||||
|
||||
function slugify(value: string) {
|
||||
return value
|
||||
.toLowerCase()
|
||||
.replace(/[^a-z0-9]+/g, "-")
|
||||
.replace(/^-+|-+$/g, "")
|
||||
.slice(0, 60) || "report";
|
||||
}
|
||||
|
||||
export async function GET(req: NextRequest) {
|
||||
const session = await requireSession();
|
||||
if (!session) return NextResponse.json({ ok: false, error: "Unauthorized" }, { status: 401 });
|
||||
|
||||
const membership = await prisma.orgUser.findUnique({
|
||||
where: { orgId_userId: { orgId: session.orgId, userId: session.userId } },
|
||||
select: { role: true },
|
||||
});
|
||||
if (!canManageFinancials(membership?.role)) {
|
||||
return NextResponse.json({ ok: false, error: "Forbidden" }, { status: 403 });
|
||||
}
|
||||
|
||||
const url = new URL(req.url);
|
||||
const { start, end } = pickRange(req);
|
||||
const machineId = url.searchParams.get("machineId") ?? undefined;
|
||||
const location = url.searchParams.get("location") ?? undefined;
|
||||
const sku = url.searchParams.get("sku") ?? undefined;
|
||||
const currency = url.searchParams.get("currency") ?? undefined;
|
||||
|
||||
const [org, impact] = await Promise.all([
|
||||
prisma.org.findUnique({ where: { id: session.orgId }, select: { name: true } }),
|
||||
computeFinancialImpact({
|
||||
orgId: session.orgId,
|
||||
start,
|
||||
end,
|
||||
machineId,
|
||||
location,
|
||||
sku,
|
||||
currency,
|
||||
includeEvents: true,
|
||||
}),
|
||||
]);
|
||||
|
||||
const orgName = org?.name ?? "Organization";
|
||||
const summaryBlocks = impact.currencySummaries
|
||||
.map(
|
||||
(summary) => `
|
||||
<div class="card">
|
||||
<div class="card-title">${escapeHtml(summary.currency)}</div>
|
||||
<div class="card-value">${escapeHtml(formatMoney(summary.totals.total, summary.currency))}</div>
|
||||
<div class="card-sub">Slow: ${escapeHtml(formatMoney(summary.totals.slowCycle, summary.currency))}</div>
|
||||
<div class="card-sub">Micro: ${escapeHtml(formatMoney(summary.totals.microstop, summary.currency))}</div>
|
||||
<div class="card-sub">Macro: ${escapeHtml(formatMoney(summary.totals.macrostop, summary.currency))}</div>
|
||||
<div class="card-sub">Scrap: ${escapeHtml(formatMoney(summary.totals.scrap, summary.currency))}</div>
|
||||
</div>
|
||||
`
|
||||
)
|
||||
.join("");
|
||||
|
||||
const dailyTables = impact.currencySummaries
|
||||
.map((summary) => {
|
||||
const rows = summary.byDay
|
||||
.map(
|
||||
(row) => `
|
||||
<tr>
|
||||
<td>${escapeHtml(row.day)}</td>
|
||||
<td>${escapeHtml(formatMoney(row.total, summary.currency))}</td>
|
||||
<td>${escapeHtml(formatMoney(row.slowCycle, summary.currency))}</td>
|
||||
<td>${escapeHtml(formatMoney(row.microstop, summary.currency))}</td>
|
||||
<td>${escapeHtml(formatMoney(row.macrostop, summary.currency))}</td>
|
||||
<td>${escapeHtml(formatMoney(row.scrap, summary.currency))}</td>
|
||||
</tr>
|
||||
`
|
||||
)
|
||||
.join("");
|
||||
|
||||
return `
|
||||
<section class="section">
|
||||
<h3>${escapeHtml(summary.currency)} Daily Breakdown</h3>
|
||||
<table>
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Day</th>
|
||||
<th>Total</th>
|
||||
<th>Slow</th>
|
||||
<th>Micro</th>
|
||||
<th>Macro</th>
|
||||
<th>Scrap</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
${rows || "<tr><td colspan=\"6\">No data</td></tr>"}
|
||||
</tbody>
|
||||
</table>
|
||||
</section>
|
||||
`;
|
||||
})
|
||||
.join("");
|
||||
|
||||
const eventRows = impact.events
|
||||
.map(
|
||||
(e) => `
|
||||
<tr>
|
||||
<td>${escapeHtml(e.ts.toISOString())}</td>
|
||||
<td>${escapeHtml(e.eventType)}</td>
|
||||
<td>${escapeHtml(e.category)}</td>
|
||||
<td>${escapeHtml(e.machineName ?? "-")}</td>
|
||||
<td>${escapeHtml(e.location ?? "-")}</td>
|
||||
<td>${escapeHtml(e.sku ?? "-")}</td>
|
||||
<td>${escapeHtml(e.workOrderId ?? "-")}</td>
|
||||
<td>${escapeHtml(formatNumber(e.durationSec))}</td>
|
||||
<td>${escapeHtml(formatMoney(e.costTotal, e.currency))}</td>
|
||||
<td>${escapeHtml(e.currency)}</td>
|
||||
</tr>
|
||||
`
|
||||
)
|
||||
.join("");
|
||||
|
||||
const html = `<!doctype html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="utf-8" />
|
||||
<title>Financial Impact Report</title>
|
||||
<style>
|
||||
body { font-family: Arial, sans-serif; color: #0f172a; margin: 32px; }
|
||||
h1 { margin: 0 0 6px; }
|
||||
.muted { color: #64748b; font-size: 12px; }
|
||||
.cards { display: grid; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); gap: 12px; margin: 20px 0; }
|
||||
.card { border: 1px solid #e2e8f0; border-radius: 12px; padding: 12px; }
|
||||
.card-title { font-size: 12px; text-transform: uppercase; color: #64748b; }
|
||||
.card-value { font-size: 20px; font-weight: 700; margin: 8px 0; }
|
||||
.card-sub { font-size: 12px; color: #475569; }
|
||||
.section { margin-top: 24px; }
|
||||
table { width: 100%; border-collapse: collapse; margin-top: 8px; }
|
||||
th, td { border: 1px solid #e2e8f0; padding: 8px; font-size: 12px; text-align: left; }
|
||||
th { background: #f8fafc; }
|
||||
footer { margin-top: 32px; text-align: right; font-size: 11px; color: #94a3b8; }
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<header>
|
||||
<h1>Financial Impact Report</h1>
|
||||
<div class="muted">${escapeHtml(orgName)} | ${escapeHtml(start.toISOString())} - ${escapeHtml(end.toISOString())}</div>
|
||||
</header>
|
||||
|
||||
<section class="cards">
|
||||
${summaryBlocks || "<div class=\"muted\">No totals yet.</div>"}
|
||||
</section>
|
||||
|
||||
${dailyTables}
|
||||
|
||||
<section class="section">
|
||||
<h3>Event Details</h3>
|
||||
<table>
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Timestamp</th>
|
||||
<th>Event</th>
|
||||
<th>Category</th>
|
||||
<th>Machine</th>
|
||||
<th>Location</th>
|
||||
<th>SKU</th>
|
||||
<th>Work Order</th>
|
||||
<th>Duration (sec)</th>
|
||||
<th>Cost</th>
|
||||
<th>Currency</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
${eventRows || "<tr><td colspan=\"10\">No events</td></tr>"}
|
||||
</tbody>
|
||||
</table>
|
||||
</section>
|
||||
|
||||
<footer>Power by MaliounTech</footer>
|
||||
</body>
|
||||
</html>`;
|
||||
|
||||
const fileName = `financial_report_${slugify(orgName)}.html`;
|
||||
return new NextResponse(html, {
|
||||
headers: {
|
||||
"Content-Type": "text/html; charset=utf-8",
|
||||
"Content-Disposition": `attachment; filename=\"${fileName}\"`,
|
||||
},
|
||||
});
|
||||
}
|
||||
84
app/api/financial/impact/route.ts
Normal file
84
app/api/financial/impact/route.ts
Normal file
@@ -0,0 +1,84 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import type { NextRequest } from "next/server";
|
||||
import { prisma } from "@/lib/prisma";
|
||||
import { requireSession } from "@/lib/auth/requireSession";
|
||||
import {
|
||||
FINANCIAL_IMPACT_SWR_SEC,
|
||||
FINANCIAL_IMPACT_TTL_SEC,
|
||||
getFinancialImpactCached,
|
||||
} from "@/lib/financial/cache";
|
||||
|
||||
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 refresh = url.searchParams.get("refresh") === "1";
|
||||
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 getFinancialImpactCached(
|
||||
{
|
||||
orgId: session.orgId,
|
||||
start,
|
||||
end,
|
||||
machineId,
|
||||
location,
|
||||
sku,
|
||||
currency,
|
||||
includeEvents: false,
|
||||
},
|
||||
{ refresh }
|
||||
);
|
||||
|
||||
const responseHeaders = new Headers({
|
||||
"Cache-Control": `private, max-age=${FINANCIAL_IMPACT_TTL_SEC}, stale-while-revalidate=${FINANCIAL_IMPACT_SWR_SEC}`,
|
||||
Vary: "Cookie",
|
||||
});
|
||||
|
||||
return NextResponse.json({ ok: true, ...result }, { headers: responseHeaders });
|
||||
}
|
||||
7
app/api/health/route.ts
Normal file
7
app/api/health/route.ts
Normal file
@@ -0,0 +1,7 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import { logLine } from "@/lib/logger";
|
||||
|
||||
export async function GET() {
|
||||
logLine("health.hit", { ok: true });
|
||||
return NextResponse.json({ ok: true });
|
||||
}
|
||||
@@ -1,45 +1,206 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import { prisma } from "@/lib/prisma";
|
||||
import { getMachineAuth } from "@/lib/machineAuthCache";
|
||||
import { z } from "zod";
|
||||
|
||||
function asRecord(value: unknown): Record<string, unknown> | null {
|
||||
if (!value || typeof value !== "object" || Array.isArray(value)) return null;
|
||||
return value as Record<string, unknown>;
|
||||
}
|
||||
|
||||
function unwrapEnvelope(raw: unknown) {
|
||||
const record = asRecord(raw);
|
||||
if (!record) return raw;
|
||||
const payload = asRecord(record.payload);
|
||||
if (!payload) return raw;
|
||||
|
||||
const hasMeta =
|
||||
record.schemaVersion !== undefined ||
|
||||
record.machineId !== undefined ||
|
||||
record.tsMs !== undefined ||
|
||||
record.tsDevice !== undefined ||
|
||||
record.seq !== undefined ||
|
||||
record.type !== undefined;
|
||||
if (!hasMeta) return raw;
|
||||
|
||||
return {
|
||||
...payload,
|
||||
machineId: record.machineId ?? payload.machineId,
|
||||
tsMs: record.tsMs ?? payload.tsMs,
|
||||
tsDevice: record.tsDevice ?? payload.tsDevice,
|
||||
schemaVersion: record.schemaVersion ?? payload.schemaVersion,
|
||||
seq: record.seq ?? payload.seq,
|
||||
};
|
||||
}
|
||||
|
||||
function asNumber(value: unknown) {
|
||||
if (typeof value === "number" && Number.isFinite(value)) return value;
|
||||
if (typeof value === "string" && value.trim() !== "") {
|
||||
const parsed = Number(value);
|
||||
return Number.isFinite(parsed) ? parsed : undefined;
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
|
||||
function normalizeCycleInput(raw: unknown): Record<string, unknown> | null {
|
||||
const row = asRecord(raw);
|
||||
if (!row) return null;
|
||||
const data = asRecord(row.data);
|
||||
|
||||
const fromRowOrData = (keys: string[]) => {
|
||||
for (const key of keys) {
|
||||
if (row[key] !== undefined) return row[key];
|
||||
if (data && data[key] !== undefined) return data[key];
|
||||
}
|
||||
return undefined;
|
||||
};
|
||||
|
||||
return {
|
||||
...row,
|
||||
actual_cycle_time: fromRowOrData(["actual_cycle_time", "actualCycleTime", "actual_cycle", "actual"]),
|
||||
theoretical_cycle_time: fromRowOrData([
|
||||
"theoretical_cycle_time",
|
||||
"theoreticalCycleTime",
|
||||
"cycleTime",
|
||||
"cycle_time",
|
||||
"ideal",
|
||||
]),
|
||||
cycle_count: fromRowOrData(["cycle_count", "cycleCount"]),
|
||||
work_order_id: fromRowOrData(["work_order_id", "workOrderId"]),
|
||||
good_delta: fromRowOrData(["good_delta", "goodDelta"]),
|
||||
// `scrap_total` is cumulative and should not be persisted as per-cycle delta.
|
||||
scrap_delta: fromRowOrData(["scrap_delta", "scrapDelta"]),
|
||||
timestamp: fromRowOrData(["timestamp", "tsMs"]),
|
||||
ts: fromRowOrData(["ts", "tsMs"]),
|
||||
event_timestamp: fromRowOrData(["event_timestamp", "eventTimestamp"]),
|
||||
};
|
||||
}
|
||||
|
||||
const numberFromAny = z.preprocess((value) => {
|
||||
if (typeof value === "number") return value;
|
||||
if (typeof value === "string" && value.trim() !== "") return Number(value);
|
||||
return value;
|
||||
}, z.number().finite());
|
||||
|
||||
const intFromAny = z.preprocess((value) => {
|
||||
if (typeof value === "number") return Math.trunc(value);
|
||||
if (typeof value === "string" && value.trim() !== "") return Math.trunc(Number(value));
|
||||
return value;
|
||||
}, z.number().int().finite());
|
||||
|
||||
const machineIdSchema = z.string().uuid();
|
||||
|
||||
const cycleSchema = z
|
||||
.object({
|
||||
actual_cycle_time: numberFromAny,
|
||||
theoretical_cycle_time: numberFromAny.optional(),
|
||||
cycle_count: intFromAny.optional(),
|
||||
work_order_id: z.string().trim().max(64).optional(),
|
||||
sku: z.string().trim().max(64).optional(),
|
||||
cavities: intFromAny.optional(),
|
||||
good_delta: intFromAny.optional(),
|
||||
scrap_delta: intFromAny.optional(),
|
||||
timestamp: numberFromAny.optional(),
|
||||
ts: numberFromAny.optional(),
|
||||
event_timestamp: numberFromAny.optional(),
|
||||
})
|
||||
.passthrough();
|
||||
|
||||
export async function POST(req: Request) {
|
||||
const apiKey = req.headers.get("x-api-key");
|
||||
if (!apiKey) return NextResponse.json({ ok: false, error: "Missing api key" }, { status: 401 });
|
||||
|
||||
const body = await req.json().catch(() => null);
|
||||
if (!body?.machineId || !body?.cycle) {
|
||||
let body: unknown = await req.json().catch(() => null);
|
||||
body = unwrapEnvelope(body);
|
||||
const bodyRecord = asRecord(body) ?? {};
|
||||
|
||||
const machineId =
|
||||
bodyRecord.machineId ??
|
||||
bodyRecord.machine_id ??
|
||||
(asRecord(bodyRecord.machine)?.id ?? null);
|
||||
if (!machineId || !machineIdSchema.safeParse(String(machineId)).success) {
|
||||
return NextResponse.json({ ok: false, error: "Invalid payload" }, { status: 400 });
|
||||
}
|
||||
|
||||
const machine = await prisma.machine.findFirst({
|
||||
where: { id: String(body.machineId), apiKey },
|
||||
select: { id: true, orgId: true },
|
||||
});
|
||||
const machine = await getMachineAuth(String(machineId), apiKey);
|
||||
if (!machine) return NextResponse.json({ ok: false, error: "Unauthorized" }, { status: 401 });
|
||||
|
||||
const c = body.cycle;
|
||||
const cyclesRaw = bodyRecord.cycles ?? bodyRecord.cycle;
|
||||
if (!cyclesRaw) {
|
||||
return NextResponse.json({ ok: false, error: "Invalid payload" }, { status: 400 });
|
||||
}
|
||||
|
||||
const tsMs =
|
||||
(typeof c.timestamp === "number" && c.timestamp) ||
|
||||
(typeof c.ts === "number" && c.ts) ||
|
||||
(typeof c.event_timestamp === "number" && c.event_timestamp) ||
|
||||
const cycleList = (Array.isArray(cyclesRaw) ? cyclesRaw : [cyclesRaw])
|
||||
.map((row) => normalizeCycleInput(row))
|
||||
.filter((row): row is Record<string, unknown> => !!row);
|
||||
|
||||
if (!cycleList.length) {
|
||||
return NextResponse.json({ ok: false, error: "Invalid payload" }, { status: 400 });
|
||||
}
|
||||
|
||||
const parsedCycles = z.array(cycleSchema).safeParse(cycleList);
|
||||
if (!parsedCycles.success) {
|
||||
return NextResponse.json({ ok: false, error: "Invalid payload" }, { status: 400 });
|
||||
}
|
||||
|
||||
const fallbackTsMs =
|
||||
asNumber(bodyRecord.tsMs) ||
|
||||
asNumber(bodyRecord.tsDevice) ||
|
||||
undefined;
|
||||
|
||||
const rows = parsedCycles.data.map((data) => {
|
||||
const tsMs =
|
||||
(typeof data.timestamp === "number" && data.timestamp) ||
|
||||
(typeof data.ts === "number" && data.ts) ||
|
||||
(typeof data.event_timestamp === "number" && data.event_timestamp) ||
|
||||
fallbackTsMs;
|
||||
|
||||
const ts = tsMs ? new Date(tsMs) : new Date();
|
||||
|
||||
const row = await prisma.machineCycle.create({
|
||||
data: {
|
||||
return {
|
||||
orgId: machine.orgId,
|
||||
machineId: machine.id,
|
||||
ts,
|
||||
cycleCount: typeof c.cycle_count === "number" ? c.cycle_count : null,
|
||||
actualCycleTime: Number(c.actual_cycle_time),
|
||||
theoreticalCycleTime: c.theoretical_cycle_time != null ? Number(c.theoretical_cycle_time) : null,
|
||||
workOrderId: c.work_order_id ? String(c.work_order_id) : null,
|
||||
sku: c.sku ? String(c.sku) : null,
|
||||
cavities: typeof c.cavities === "number" ? c.cavities : null,
|
||||
goodDelta: typeof c.good_delta === "number" ? c.good_delta : null,
|
||||
scrapDelta: typeof c.scrap_delta === "number" ? c.scrap_delta : null,
|
||||
},
|
||||
cycleCount: typeof data.cycle_count === "number" ? data.cycle_count : null,
|
||||
actualCycleTime: data.actual_cycle_time,
|
||||
theoreticalCycleTime: typeof data.theoretical_cycle_time === "number" ? data.theoretical_cycle_time : null,
|
||||
workOrderId: data.work_order_id ? String(data.work_order_id) : null,
|
||||
sku: data.sku ? String(data.sku) : null,
|
||||
cavities: typeof data.cavities === "number" ? data.cavities : null,
|
||||
goodDelta: typeof data.good_delta === "number" ? data.good_delta : null,
|
||||
scrapDelta: typeof data.scrap_delta === "number" ? data.scrap_delta : null,
|
||||
};
|
||||
});
|
||||
|
||||
const result = await prisma.machineCycle.createMany({
|
||||
data: rows,
|
||||
skipDuplicates: true,
|
||||
});
|
||||
|
||||
if (rows.length === 1) {
|
||||
const row = await prisma.machineCycle.findFirst({
|
||||
where: {
|
||||
orgId: machine.orgId,
|
||||
machineId: machine.id,
|
||||
ts: rows[0].ts,
|
||||
cycleCount: rows[0].cycleCount ?? null,
|
||||
},
|
||||
orderBy: { createdAt: "asc" },
|
||||
select: { id: true, ts: true },
|
||||
});
|
||||
return NextResponse.json({
|
||||
ok: true,
|
||||
id: row?.id,
|
||||
ts: row?.ts,
|
||||
inserted: result.count,
|
||||
duplicate: result.count === 0,
|
||||
});
|
||||
}
|
||||
|
||||
return NextResponse.json({
|
||||
ok: true,
|
||||
inserted: result.count,
|
||||
requested: rows.length,
|
||||
count: result.count,
|
||||
});
|
||||
return NextResponse.json({ ok: true, id: row.id, ts: row.ts });
|
||||
}
|
||||
|
||||
@@ -1,12 +1,29 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import { prisma } from "@/lib/prisma";
|
||||
import { getMachineAuth } from "@/lib/machineAuthCache";
|
||||
import { z } from "zod";
|
||||
import { evaluateAlertsForEvent } from "@/lib/alerts/engine";
|
||||
import { toJsonValue } from "@/lib/prismaJson";
|
||||
import {
|
||||
findCatalogReason,
|
||||
loadFallbackReasonCatalog,
|
||||
normalizeReasonCatalog,
|
||||
toReasonCode,
|
||||
type ReasonCatalog,
|
||||
type ReasonCatalogKind,
|
||||
} from "@/lib/reasonCatalog";
|
||||
|
||||
const normalizeType = (t: any) =>
|
||||
const normalizeType = (t: unknown) =>
|
||||
String(t ?? "")
|
||||
.trim()
|
||||
.toLowerCase()
|
||||
.replace(/_/g, "-");
|
||||
|
||||
function asRecord(value: unknown): Record<string, unknown> | null {
|
||||
if (!value || typeof value !== "object" || Array.isArray(value)) return null;
|
||||
return value as Record<string, unknown>;
|
||||
}
|
||||
|
||||
const CANON_TYPE: Record<string, string> = {
|
||||
// Node-RED
|
||||
"production-stopped": "stop",
|
||||
@@ -21,82 +38,329 @@ const CANON_TYPE: Record<string, string> = {
|
||||
"microparo": "microstop",
|
||||
"micro-paro": "microstop",
|
||||
"down": "stop",
|
||||
"downtime-acknowledged": "downtime-acknowledged",
|
||||
"scrap-manual-entry": "scrap-manual-entry",
|
||||
"mold-change": "mold-change",
|
||||
};
|
||||
|
||||
const ALLOWED_TYPES = new Set([
|
||||
"slow-cycle",
|
||||
"microstop",
|
||||
"macrostop",
|
||||
"offline",
|
||||
"error",
|
||||
"oee-drop",
|
||||
"quality-spike",
|
||||
"performance-degradation",
|
||||
"predictive-oee-decline",
|
||||
"downtime-acknowledged",
|
||||
"scrap-manual-entry",
|
||||
"mold-change",
|
||||
]);
|
||||
|
||||
// thresholds for stop classification (tune later / move to machine config)
|
||||
const MICROSTOP_SEC = 60;
|
||||
const MACROSTOP_SEC = 300;
|
||||
const machineIdSchema = z.string().uuid();
|
||||
const MAX_EVENTS = 100;
|
||||
|
||||
//when no cycle time is configed
|
||||
const DEFAULT_MACROSTOP_SEC = 300;
|
||||
const NON_AUTHORITATIVE_REASON_CODES = new Set(["PENDIENTE", "UNCLASSIFIED"]);
|
||||
|
||||
function isNonAuthoritativeReasonCode(code: unknown) {
|
||||
const normalized = clampText(code, 64)?.toUpperCase();
|
||||
return !!normalized && NON_AUTHORITATIVE_REASON_CODES.has(normalized);
|
||||
}
|
||||
|
||||
function clampText(value: unknown, maxLen: number) {
|
||||
if (value === null || value === undefined) return null;
|
||||
const text = String(value).trim().replace(/[\u0000-\u001f\u007f]/g, "");
|
||||
if (!text) return null;
|
||||
return text.length > maxLen ? text.slice(0, maxLen) : text;
|
||||
}
|
||||
|
||||
function numberFrom(value: unknown) {
|
||||
if (typeof value === "number" && Number.isFinite(value)) return value;
|
||||
if (typeof value === "string" && value.trim()) {
|
||||
const parsed = Number(value);
|
||||
if (Number.isFinite(parsed)) return parsed;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
function parseSeqToBigInt(value: unknown): bigint | null {
|
||||
if (value === null || value === undefined) return null;
|
||||
if (typeof value === "number") {
|
||||
if (!Number.isInteger(value) || value < 0) return null;
|
||||
return BigInt(value);
|
||||
}
|
||||
if (typeof value === "string" && /^\d+$/.test(value)) return BigInt(value);
|
||||
return null;
|
||||
}
|
||||
|
||||
function canonicalText(value: unknown) {
|
||||
return String(value ?? "")
|
||||
.normalize("NFD")
|
||||
.replace(/[\u0300-\u036f]/g, "")
|
||||
.toLowerCase()
|
||||
.replace(/[^a-z0-9]+/g, "-")
|
||||
.replace(/^-+|-+$/g, "");
|
||||
}
|
||||
|
||||
function parseReasonPath(rawPath: unknown) {
|
||||
let category: string | null = null;
|
||||
let detail: string | null = null;
|
||||
|
||||
if (Array.isArray(rawPath)) {
|
||||
const first = rawPath[0];
|
||||
const second = rawPath[1];
|
||||
if (typeof first === "string") category = first;
|
||||
if (typeof second === "string") detail = second;
|
||||
if (asRecord(first)) category = clampText(first.id ?? first.label ?? first.value, 120);
|
||||
if (asRecord(second)) detail = clampText(second.id ?? second.label ?? second.value, 120);
|
||||
} else if (typeof rawPath === "string") {
|
||||
const pieces = rawPath
|
||||
.split(/>|\/|\\|\|/g)
|
||||
.map((p) => p.trim())
|
||||
.filter(Boolean);
|
||||
category = pieces[0] ?? null;
|
||||
detail = pieces[1] ?? null;
|
||||
}
|
||||
|
||||
return {
|
||||
category: clampText(category, 120),
|
||||
detail: clampText(detail, 120),
|
||||
};
|
||||
}
|
||||
|
||||
function parseReasonTextPath(reasonText: unknown) {
|
||||
const text = clampText(reasonText, 240);
|
||||
if (!text) return { category: null as string | null, detail: null as string | null };
|
||||
const pieces = text
|
||||
.split(/>|\/|\\|\|/g)
|
||||
.map((p) => p.trim())
|
||||
.filter(Boolean);
|
||||
return {
|
||||
category: clampText(pieces[0] ?? null, 120),
|
||||
detail: clampText(pieces[1] ?? null, 120),
|
||||
};
|
||||
}
|
||||
|
||||
function findCatalogReasonFlexible(
|
||||
catalog: ReasonCatalog | null,
|
||||
kind: ReasonCatalogKind,
|
||||
categoryIdOrLabel: unknown,
|
||||
detailIdOrLabel: unknown
|
||||
) {
|
||||
const direct = findCatalogReason(catalog, kind, categoryIdOrLabel, detailIdOrLabel);
|
||||
if (direct) return direct;
|
||||
if (!catalog) return null;
|
||||
|
||||
const catNeedle = canonicalText(categoryIdOrLabel);
|
||||
const detNeedle = canonicalText(detailIdOrLabel);
|
||||
if (!catNeedle || !detNeedle) return null;
|
||||
|
||||
for (const category of catalog[kind] ?? []) {
|
||||
const catMatch =
|
||||
canonicalText(category.id) === catNeedle || canonicalText(category.label) === catNeedle;
|
||||
if (!catMatch) continue;
|
||||
for (const detail of category.details) {
|
||||
const detMatch = canonicalText(detail.id) === detNeedle || canonicalText(detail.label) === detNeedle;
|
||||
if (!detMatch) continue;
|
||||
return {
|
||||
categoryId: category.id,
|
||||
categoryLabel: category.label,
|
||||
detailId: detail.id,
|
||||
detailLabel: detail.label,
|
||||
reasonCode: toReasonCode(category.id, detail.id),
|
||||
reasonLabel: `${category.label} > ${detail.label}`,
|
||||
};
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
function getCatalogFromDefaults(defaultsJson: unknown) {
|
||||
const defaults = asRecord(defaultsJson);
|
||||
if (!defaults) return null;
|
||||
return normalizeReasonCatalog(defaults.reasonCatalog ?? defaults.reasonCatalogData);
|
||||
}
|
||||
|
||||
function resolveReason(
|
||||
raw: Record<string, unknown>,
|
||||
kind: ReasonCatalogKind,
|
||||
catalog: ReasonCatalog | null,
|
||||
fallbackVersion: number
|
||||
) {
|
||||
const reasonPath = parseReasonPath(raw.reasonPath);
|
||||
const reasonTextPath = parseReasonTextPath(raw.reasonText);
|
||||
const categoryIdRaw = clampText(raw.categoryId ?? reasonPath.category ?? reasonTextPath.category, 64);
|
||||
const detailIdRaw = clampText(raw.detailId ?? reasonPath.detail ?? reasonTextPath.detail, 64);
|
||||
const fromCatalog = findCatalogReasonFlexible(catalog, kind, categoryIdRaw, detailIdRaw);
|
||||
|
||||
const categoryLabelRaw = clampText(raw.categoryLabel ?? reasonPath.category ?? reasonTextPath.category, 120);
|
||||
const detailLabelRaw = clampText(raw.detailLabel ?? reasonPath.detail ?? reasonTextPath.detail, 120);
|
||||
|
||||
const reasonCode =
|
||||
clampText(raw.reasonCode, 64)?.toUpperCase() ??
|
||||
fromCatalog?.reasonCode ??
|
||||
toReasonCode(categoryIdRaw ?? categoryLabelRaw, detailIdRaw ?? detailLabelRaw) ??
|
||||
null;
|
||||
|
||||
const categoryId = fromCatalog?.categoryId ?? categoryIdRaw;
|
||||
const detailId = fromCatalog?.detailId ?? detailIdRaw;
|
||||
const categoryLabel = fromCatalog?.categoryLabel ?? categoryLabelRaw;
|
||||
const detailLabel = fromCatalog?.detailLabel ?? detailLabelRaw;
|
||||
|
||||
const pathLabel =
|
||||
clampText(raw.reasonText, 240) ??
|
||||
fromCatalog?.reasonLabel ??
|
||||
(categoryLabel && detailLabel ? `${categoryLabel} > ${detailLabel}` : null) ??
|
||||
detailLabel ??
|
||||
categoryLabel ??
|
||||
reasonCode;
|
||||
|
||||
const catalogVersionRaw = numberFrom(raw.catalogVersion);
|
||||
const catalogVersion = catalogVersionRaw != null ? Math.trunc(catalogVersionRaw) : fallbackVersion;
|
||||
|
||||
return {
|
||||
type: kind,
|
||||
categoryId,
|
||||
categoryLabel,
|
||||
detailId,
|
||||
detailLabel,
|
||||
reasonCode,
|
||||
reasonLabel: pathLabel,
|
||||
reasonText: pathLabel,
|
||||
catalogVersion,
|
||||
};
|
||||
}
|
||||
|
||||
export async function POST(req: Request) {
|
||||
const apiKey = req.headers.get("x-api-key");
|
||||
if (!apiKey) {
|
||||
return NextResponse.json({ ok: false, error: "Missing api key" }, { status: 401 });
|
||||
if (!apiKey) return NextResponse.json({ ok: false, error: "Missing api key" }, { status: 401 });
|
||||
|
||||
let body: unknown = await req.json().catch(() => null);
|
||||
|
||||
// ✅ if Node-RED sent an array as the whole body, unwrap it
|
||||
if (Array.isArray(body)) body = body[0];
|
||||
const bodyRecord = asRecord(body) ?? {};
|
||||
const payloadRecord = asRecord(bodyRecord.payload) ?? {};
|
||||
|
||||
// ✅ accept multiple common keys
|
||||
const machineId =
|
||||
bodyRecord.machineId ??
|
||||
bodyRecord.machine_id ??
|
||||
(asRecord(bodyRecord.machine)?.id ?? null);
|
||||
let rawEvent =
|
||||
bodyRecord.event ??
|
||||
bodyRecord.events ??
|
||||
bodyRecord.anomalies ??
|
||||
payloadRecord.event ??
|
||||
payloadRecord.events ??
|
||||
payloadRecord.anomalies ??
|
||||
payloadRecord ??
|
||||
bodyRecord.data; // sometimes "data"
|
||||
|
||||
const rawEventRecord = asRecord(rawEvent);
|
||||
if (rawEventRecord?.event && typeof rawEventRecord.event === "object") rawEvent = rawEventRecord.event;
|
||||
if (Array.isArray(rawEventRecord?.events)) rawEvent = rawEventRecord.events;
|
||||
|
||||
if (!machineId || !rawEvent) {
|
||||
return NextResponse.json(
|
||||
{ ok: false, error: "Invalid payload", got: { hasMachineId: !!machineId, keys: Object.keys(bodyRecord) } },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
const body = await req.json().catch(() => null);
|
||||
if (!body?.machineId || !body?.event) {
|
||||
return NextResponse.json({ ok: false, error: "Invalid payload" }, { status: 400 });
|
||||
if (!machineIdSchema.safeParse(String(machineId)).success) {
|
||||
return NextResponse.json({ ok: false, error: "Invalid machine id" }, { status: 400 });
|
||||
}
|
||||
|
||||
const machine = await getMachineAuth(String(machineId), apiKey);
|
||||
if (!machine) return NextResponse.json({ ok: false, error: "Unauthorized" }, { status: 401 });
|
||||
|
||||
const machine = await prisma.machine.findFirst({
|
||||
where: { id: String(body.machineId), apiKey },
|
||||
select: { id: true, orgId: true },
|
||||
const bodySeq = parseSeqToBigInt(bodyRecord.seq);
|
||||
const bodySchemaVersion = clampText(bodyRecord.schemaVersion, 16);
|
||||
|
||||
const orgSettings = await prisma.orgSettings.findUnique({
|
||||
where: { orgId: machine.orgId },
|
||||
select: { stoppageMultiplier: true, macroStoppageMultiplier: true, defaultsJson: true },
|
||||
});
|
||||
if (!machine) {
|
||||
return NextResponse.json({ ok: false, error: "Unauthorized" }, { status: 401 });
|
||||
}
|
||||
const fallbackCatalog = await loadFallbackReasonCatalog();
|
||||
const settingsCatalog = getCatalogFromDefaults(orgSettings?.defaultsJson);
|
||||
const reasonCatalog = settingsCatalog ?? fallbackCatalog;
|
||||
|
||||
// Normalize to array (Node-RED sends array of anomalies)
|
||||
const rawEvent = body.event;
|
||||
const defaultMicroMultiplier = Number(orgSettings?.stoppageMultiplier ?? 1.5);
|
||||
const defaultMacroMultiplier = Math.max(
|
||||
defaultMicroMultiplier,
|
||||
Number(orgSettings?.macroStoppageMultiplier ?? 5)
|
||||
);
|
||||
|
||||
|
||||
// ✅ normalize to array no matter what
|
||||
const events = Array.isArray(rawEvent) ? rawEvent : [rawEvent];
|
||||
if (events.length > MAX_EVENTS) {
|
||||
return NextResponse.json({ ok: false, error: "Too many events" }, { status: 400 });
|
||||
}
|
||||
|
||||
const created: { id: string; ts: Date; eventType: string }[] = [];
|
||||
const skipped: any[] = [];
|
||||
const skipped: Array<Record<string, unknown>> = [];
|
||||
|
||||
for (const ev of events) {
|
||||
if (!ev || typeof ev !== "object") {
|
||||
const evRecord = asRecord(ev);
|
||||
if (!evRecord) {
|
||||
skipped.push({ reason: "invalid_event_object" });
|
||||
continue;
|
||||
}
|
||||
const evData = asRecord(evRecord.data) ?? {};
|
||||
// We'll re-check reason again after parsing `data` (it may be a JSON string)
|
||||
let evReason = asRecord(evRecord.reason) ?? asRecord(evData.reason);
|
||||
const evDowntime = asRecord(evRecord.downtime) ?? asRecord(evData.downtime);
|
||||
// Some producers nest the reason under `downtime.reason`
|
||||
if (!evReason) evReason = asRecord(evDowntime?.reason);
|
||||
|
||||
const rawType = (ev as any).eventType ?? (ev as any).anomaly_type ?? (ev as any).topic ?? body.topic ?? "";
|
||||
const rawType = evRecord.eventType ?? evRecord.anomaly_type ?? evRecord.topic ?? bodyRecord.topic ?? "";
|
||||
const typ0 = normalizeType(rawType);
|
||||
const typ = CANON_TYPE[typ0] ?? typ0;
|
||||
|
||||
// Determine timestamp
|
||||
const tsMs =
|
||||
(typeof (ev as any)?.timestamp === "number" && (ev as any).timestamp) ||
|
||||
(typeof (ev as any)?.data?.timestamp === "number" && (ev as any).data.timestamp) ||
|
||||
(typeof (ev as any)?.data?.event_timestamp === "number" && (ev as any).data.event_timestamp) ||
|
||||
(typeof evRecord.timestamp === "number" && evRecord.timestamp) ||
|
||||
(typeof evData.timestamp === "number" && evData.timestamp) ||
|
||||
(typeof evData.event_timestamp === "number" && evData.event_timestamp) ||
|
||||
null;
|
||||
|
||||
const ts = tsMs ? new Date(tsMs) : new Date();
|
||||
|
||||
// Severity defaulting (do not skip on severity — store for audit)
|
||||
let sev = String((ev as any).severity ?? "").trim().toLowerCase();
|
||||
let sev = String(evRecord.severity ?? "").trim().toLowerCase();
|
||||
if (!sev) sev = "warning";
|
||||
|
||||
// Stop classification -> microstop/macrostop
|
||||
let finalType = typ;
|
||||
let stopSecForReason: number | null = null;
|
||||
if (typ === "stop") {
|
||||
const stopSec =
|
||||
(typeof (ev as any)?.data?.stoppage_duration_seconds === "number" && (ev as any).data.stoppage_duration_seconds) ||
|
||||
(typeof (ev as any)?.data?.stop_duration_seconds === "number" && (ev as any).data.stop_duration_seconds) ||
|
||||
(typeof evData.stoppage_duration_seconds === "number" && evData.stoppage_duration_seconds) ||
|
||||
(typeof evData.stop_duration_seconds === "number" && evData.stop_duration_seconds) ||
|
||||
null;
|
||||
stopSecForReason = stopSec != null ? Number(stopSec) : null;
|
||||
|
||||
if (stopSec != null) {
|
||||
finalType = stopSec >= MACROSTOP_SEC ? "macrostop" : "microstop";
|
||||
const theoretical = Number(evData.theoretical_cycle_time ?? evData.theoreticalCycleTime ?? 0) || 0;
|
||||
|
||||
const microMultiplier = Number(
|
||||
evData.micro_threshold_multiplier ?? evData.threshold_multiplier ?? defaultMicroMultiplier
|
||||
);
|
||||
const macroMultiplier = Math.max(
|
||||
microMultiplier,
|
||||
Number(evData.macro_threshold_multiplier ?? defaultMacroMultiplier)
|
||||
);
|
||||
|
||||
if (theoretical > 0) {
|
||||
const macroThresholdSec = theoretical * macroMultiplier;
|
||||
finalType = stopSec >= macroThresholdSec ? "macrostop" : "microstop";
|
||||
} else {
|
||||
finalType = stopSec >= DEFAULT_MACROSTOP_SEC ? "macrostop" : "microstop";
|
||||
}
|
||||
} else {
|
||||
// missing duration -> conservative
|
||||
finalType = "microstop";
|
||||
@@ -109,44 +373,378 @@ export async function POST(req: Request) {
|
||||
}
|
||||
|
||||
const title =
|
||||
String((ev as any).title ?? "").trim() ||
|
||||
clampText(evRecord.title, 160) ||
|
||||
(finalType === "slow-cycle" ? "Slow Cycle Detected" :
|
||||
finalType === "macrostop" ? "Macrostop Detected" :
|
||||
finalType === "microstop" ? "Microstop Detected" :
|
||||
"Event");
|
||||
|
||||
const description = (ev as any).description ? String((ev as any).description) : null;
|
||||
const description = clampText(evRecord.description, 1000);
|
||||
|
||||
// store full blob, ensure object
|
||||
const rawData = (ev as any).data ?? ev;
|
||||
const dataObj = typeof rawData === "string" ? (() => {
|
||||
try { return JSON.parse(rawData); } catch { return { raw: rawData }; }
|
||||
})() : rawData;
|
||||
const rawData = evRecord.data ?? evRecord;
|
||||
const parsedData = typeof rawData === "string"
|
||||
? (() => {
|
||||
try {
|
||||
return JSON.parse(rawData);
|
||||
} catch {
|
||||
return { raw: rawData };
|
||||
}
|
||||
})()
|
||||
: rawData;
|
||||
const dataObj: Record<string, unknown> =
|
||||
parsedData && typeof parsedData === "object" && !Array.isArray(parsedData)
|
||||
? { ...(parsedData as Record<string, unknown>) }
|
||||
: { raw: parsedData };
|
||||
if (evRecord.status != null && dataObj.status == null) dataObj.status = evRecord.status;
|
||||
if (evRecord.alert_id != null && dataObj.alert_id == null) dataObj.alert_id = evRecord.alert_id;
|
||||
if (evRecord.is_update != null && dataObj.is_update == null) dataObj.is_update = evRecord.is_update;
|
||||
if (evRecord.is_auto_ack != null && dataObj.is_auto_ack == null) dataObj.is_auto_ack = evRecord.is_auto_ack;
|
||||
if (evReason && dataObj.reason == null) dataObj.reason = evReason;
|
||||
if (evDowntime && dataObj.downtime == null) dataObj.downtime = evDowntime;
|
||||
|
||||
const row = await prisma.machineEvent.create({
|
||||
data: {
|
||||
// If `data` was a JSON string, the earlier evReason lookup would miss it.
|
||||
// Re-check here using the normalized object we will persist.
|
||||
if (!evReason) evReason = asRecord(dataObj.reason);
|
||||
if (!evReason) evReason = asRecord(asRecord(dataObj.downtime)?.reason);
|
||||
|
||||
// If we have a reasonText but missing ids, derive ids from the path-like string.
|
||||
if (evReason) {
|
||||
const hasCat = clampText((evReason as any).categoryId, 64) ?? clampText((evReason as any).categoryLabel, 120);
|
||||
const hasDet = clampText((evReason as any).detailId, 64) ?? clampText((evReason as any).detailLabel, 120);
|
||||
const rt = clampText((evReason as any).reasonText, 240);
|
||||
if ((!hasCat || !hasDet) && rt) {
|
||||
const parsed = parseReasonTextPath(rt);
|
||||
const next = { ...evReason } as Record<string, unknown>;
|
||||
// Preserve any explicit ids; only fill gaps.
|
||||
if ((next as any).categoryId == null && parsed.category) next.categoryId = canonicalText(parsed.category);
|
||||
if ((next as any).categoryLabel == null && parsed.category) next.categoryLabel = parsed.category;
|
||||
if ((next as any).detailId == null && parsed.detail) next.detailId = canonicalText(parsed.detail);
|
||||
if ((next as any).detailLabel == null && parsed.detail) next.detailLabel = parsed.detail;
|
||||
evReason = next;
|
||||
}
|
||||
}
|
||||
|
||||
const activeWorkOrder = asRecord(evRecord.activeWorkOrder);
|
||||
const dataActiveWorkOrder = asRecord(evData.activeWorkOrder);
|
||||
|
||||
// ✨ Cada evento puede traer su propio seq, o usar el del payload raíz
|
||||
const evSeq =
|
||||
parseSeqToBigInt(evRecord.seq) ??
|
||||
parseSeqToBigInt(evData.seq) ??
|
||||
bodySeq;
|
||||
|
||||
const evSchemaVersion =
|
||||
clampText(evRecord.schemaVersion, 16) ??
|
||||
bodySchemaVersion;
|
||||
|
||||
const eventData = {
|
||||
orgId: machine.orgId,
|
||||
machineId: machine.id,
|
||||
schemaVersion: evSchemaVersion,
|
||||
seq: evSeq,
|
||||
ts,
|
||||
topic: clampText(evRecord.topic ?? finalType, 64) ?? finalType,
|
||||
eventType: finalType,
|
||||
severity: sev,
|
||||
requiresAck: !!evRecord.requires_ack,
|
||||
title,
|
||||
description,
|
||||
data: toJsonValue(dataObj),
|
||||
workOrderId:
|
||||
clampText(evRecord.work_order_id, 64) ??
|
||||
clampText(evData.work_order_id, 64) ??
|
||||
clampText(activeWorkOrder?.id, 64) ??
|
||||
clampText(dataActiveWorkOrder?.id, 64) ??
|
||||
null,
|
||||
sku:
|
||||
clampText(evRecord.sku, 64) ??
|
||||
clampText(evData.sku, 64) ??
|
||||
clampText(activeWorkOrder?.sku, 64) ??
|
||||
clampText(dataActiveWorkOrder?.sku, 64) ??
|
||||
null,
|
||||
};
|
||||
|
||||
// ✨ Idempotente: si ya existe (mismo orgId+machineId+seq), no inserta
|
||||
const insertResult = await prisma.machineEvent.createMany({
|
||||
data: [eventData],
|
||||
skipDuplicates: true,
|
||||
});
|
||||
|
||||
// ✨ Buscar la fila (la recién creada o la duplicada existente)
|
||||
let row;
|
||||
if (evSeq != null) {
|
||||
row = await prisma.machineEvent.findFirst({
|
||||
where: {
|
||||
orgId: machine.orgId,
|
||||
machineId: machine.id,
|
||||
seq: evSeq,
|
||||
},
|
||||
orderBy: { ts: "asc" },
|
||||
});
|
||||
} else {
|
||||
// Sin seq, buscar por ts (fallback compatibilidad con eventos viejos)
|
||||
row = await prisma.machineEvent.findFirst({
|
||||
where: {
|
||||
orgId: machine.orgId,
|
||||
machineId: machine.id,
|
||||
ts,
|
||||
topic: String((ev as any).topic ?? finalType),
|
||||
eventType: finalType,
|
||||
severity: sev,
|
||||
requiresAck: !!(ev as any).requires_ack,
|
||||
title,
|
||||
description,
|
||||
data: dataObj,
|
||||
workOrderId:
|
||||
(ev as any)?.work_order_id ? String((ev as any).work_order_id)
|
||||
: (ev as any)?.data?.work_order_id ? String((ev as any).data.work_order_id)
|
||||
: null,
|
||||
sku:
|
||||
(ev as any)?.sku ? String((ev as any).sku)
|
||||
: (ev as any)?.data?.sku ? String((ev as any).data.sku)
|
||||
: null,
|
||||
},
|
||||
orderBy: { ts: "desc" },
|
||||
});
|
||||
}
|
||||
|
||||
if (!row) {
|
||||
skipped.push({ reason: "row_not_found_after_insert", seq: evSeq?.toString() });
|
||||
continue;
|
||||
}
|
||||
|
||||
const wasDuplicate = insertResult.count === 0;
|
||||
|
||||
// Si fue duplicado, no procesar reasonEntry ni alertas (ya se hicieron antes)
|
||||
if (wasDuplicate) {
|
||||
created.push({ id: row.id, ts: row.ts, eventType: row.eventType });
|
||||
continue; // ✨ saltar el resto del procesamiento
|
||||
}
|
||||
|
||||
created.push({ id: row.id, ts: row.ts, eventType: row.eventType });
|
||||
|
||||
|
||||
|
||||
// If the payload carries a `reason`, create the corresponding ReasonEntry.
|
||||
// If it doesn't, still create an "UNCLASSIFIED" downtime ReasonEntry for stop events so the dashboard can show coverage.
|
||||
if (evRecord.is_update || evRecord.is_auto_ack || dataObj.is_update || dataObj.is_auto_ack){
|
||||
// skip duplicate reasonEntry for refresh/ack
|
||||
} else if (evReason || finalType === "microstop" || finalType === "macrostop" || finalType === "downtime-acknowledged" || finalType === "mold-change"){
|
||||
const fallbackIncidentKey =
|
||||
clampText(
|
||||
evData.incidentKey ??
|
||||
dataObj.incidentKey ??
|
||||
evDowntime?.incidentKey ??
|
||||
evReason?.incidentKey,
|
||||
128
|
||||
) ?? null;
|
||||
const moldIncidentKey =
|
||||
clampText(evData.incidentKey ?? dataObj.incidentKey, 128) ??
|
||||
(numberFrom(evData.start_ms ?? dataObj.start_ms) != null
|
||||
? `mold-change:${Math.trunc(numberFrom(evData.start_ms ?? dataObj.start_ms) as number)}`
|
||||
: null);
|
||||
const reasonRaw: Record<string, unknown> =
|
||||
evReason ??
|
||||
(finalType === "mold-change"
|
||||
? ({
|
||||
type: "downtime",
|
||||
categoryId: "cambio-molde",
|
||||
detailId: "cambio-molde",
|
||||
categoryLabel: "Cambio molde",
|
||||
detailLabel: "Cambio molde",
|
||||
reasonCode: "MOLD_CHANGE",
|
||||
reasonText: "Cambio molde",
|
||||
incidentKey: moldIncidentKey ?? fallbackIncidentKey ?? row.id,
|
||||
} as Record<string, unknown>)
|
||||
:
|
||||
({
|
||||
type: "downtime",
|
||||
categoryId: "unclassified",
|
||||
detailId: "unclassified",
|
||||
categoryLabel: "Unclassified",
|
||||
detailLabel: "Unclassified",
|
||||
reasonCode: "UNCLASSIFIED",
|
||||
reasonText: "Unclassified",
|
||||
incidentKey: fallbackIncidentKey ?? row.id,
|
||||
} as Record<string, unknown>));
|
||||
|
||||
const inferredKind: ReasonCatalogKind =
|
||||
String(reasonRaw.type ?? "").toLowerCase() === "scrap" || finalType === "scrap-manual-entry"
|
||||
? "scrap"
|
||||
: "downtime";
|
||||
const resolved = resolveReason(reasonRaw, inferredKind, reasonCatalog, reasonCatalog.version);
|
||||
|
||||
if (resolved.reasonCode) {
|
||||
const continuityIncidentKey =
|
||||
inferredKind === "downtime"
|
||||
? clampText((reasonRaw as any).incidentKey ?? evDowntime?.incidentKey ?? fallbackIncidentKey, 128) ?? row.id
|
||||
: null;
|
||||
const reasonMetaIncidentKey =
|
||||
inferredKind === "downtime"
|
||||
? continuityIncidentKey
|
||||
: clampText((reasonRaw as any).incidentKey ?? evDowntime?.incidentKey, 128);
|
||||
const reasonId =
|
||||
clampText(reasonRaw.reasonId, 128) ??
|
||||
(inferredKind === "downtime"
|
||||
? `evt:${machine.id}:downtime:${continuityIncidentKey ?? row.id}`
|
||||
: `evt:${machine.id}:scrap:${clampText(reasonRaw.scrapEntryId, 128) ?? row.id}`);
|
||||
|
||||
const workOrderId =
|
||||
clampText(evRecord.work_order_id, 64) ??
|
||||
clampText(evData.work_order_id, 64) ??
|
||||
clampText(evRecord.workOrderId, 64) ??
|
||||
null;
|
||||
|
||||
const commonWrite = {
|
||||
reasonCode: resolved.reasonCode,
|
||||
reasonLabel: resolved.reasonLabel ?? resolved.reasonCode,
|
||||
reasonText: resolved.reasonText ?? null,
|
||||
capturedAt: row.ts,
|
||||
workOrderId,
|
||||
schemaVersion: Math.max(1, Math.trunc(resolved.catalogVersion)),
|
||||
meta: toJsonValue({
|
||||
source: "ingest:event",
|
||||
eventId: row.id,
|
||||
eventType: row.eventType,
|
||||
incidentKey: reasonMetaIncidentKey,
|
||||
anomalyType:
|
||||
clampText(evRecord.anomalyType, 64) ??
|
||||
clampText(evDowntime?.anomalyType, 64) ??
|
||||
clampText(evRecord.anomaly_type, 64),
|
||||
reason: {
|
||||
type: resolved.type,
|
||||
categoryId: resolved.categoryId,
|
||||
categoryLabel: resolved.categoryLabel,
|
||||
detailId: resolved.detailId,
|
||||
detailLabel: resolved.detailLabel,
|
||||
reasonText: resolved.reasonText,
|
||||
catalogVersion: resolved.catalogVersion,
|
||||
},
|
||||
}),
|
||||
};
|
||||
|
||||
if (inferredKind === "downtime") {
|
||||
const incidentKey = continuityIncidentKey ?? row.id;
|
||||
const durationSeconds =
|
||||
numberFrom(evDowntime?.durationSeconds) ??
|
||||
numberFrom(evData.duration_sec) ??
|
||||
numberFrom(evData.stoppage_duration_seconds) ??
|
||||
numberFrom(evData.stop_duration_seconds) ??
|
||||
(stopSecForReason != null ? stopSecForReason : null) ??
|
||||
null;
|
||||
const episodeEndTsMs =
|
||||
numberFrom(evData.end_ms) ??
|
||||
numberFrom(evDowntime?.episodeEndTsMs) ??
|
||||
numberFrom(evDowntime?.acknowledgedAtMs) ??
|
||||
null;
|
||||
|
||||
let guardedWrite = commonWrite;
|
||||
const incomingIsNonAuthoritative = isNonAuthoritativeReasonCode(resolved.reasonCode);
|
||||
const isManualAckEvent = finalType === "downtime-acknowledged";
|
||||
if (!isManualAckEvent && incomingIsNonAuthoritative) {
|
||||
const existingEpisode = await prisma.reasonEntry.findFirst({
|
||||
where: {
|
||||
orgId: machine.orgId,
|
||||
kind: "downtime",
|
||||
episodeId: incidentKey,
|
||||
},
|
||||
select: {
|
||||
reasonCode: true,
|
||||
reasonLabel: true,
|
||||
reasonText: true,
|
||||
meta: true,
|
||||
},
|
||||
});
|
||||
if (existingEpisode && !isNonAuthoritativeReasonCode(existingEpisode.reasonCode)) {
|
||||
const existingMeta = asRecord(existingEpisode.meta);
|
||||
const existingMetaReason = asRecord(existingMeta?.reason);
|
||||
guardedWrite = {
|
||||
...commonWrite,
|
||||
reasonCode: existingEpisode.reasonCode,
|
||||
reasonLabel: existingEpisode.reasonLabel ?? existingEpisode.reasonCode,
|
||||
reasonText:
|
||||
existingEpisode.reasonText ??
|
||||
existingEpisode.reasonLabel ??
|
||||
existingEpisode.reasonCode,
|
||||
meta: toJsonValue({
|
||||
source: "ingest:event",
|
||||
eventId: row.id,
|
||||
eventType: row.eventType,
|
||||
incidentKey: reasonMetaIncidentKey,
|
||||
anomalyType:
|
||||
clampText(evRecord.anomalyType, 64) ??
|
||||
clampText(evDowntime?.anomalyType, 64) ??
|
||||
clampText(evRecord.anomaly_type, 64),
|
||||
reason: existingMetaReason ?? {
|
||||
type: resolved.type,
|
||||
categoryId: resolved.categoryId,
|
||||
categoryLabel: resolved.categoryLabel,
|
||||
detailId: resolved.detailId,
|
||||
detailLabel: resolved.detailLabel,
|
||||
reasonText:
|
||||
existingEpisode.reasonText ??
|
||||
existingEpisode.reasonLabel ??
|
||||
existingEpisode.reasonCode,
|
||||
catalogVersion: resolved.catalogVersion,
|
||||
},
|
||||
reasonPreservedFromManual: true,
|
||||
incomingReasonCode: resolved.reasonCode,
|
||||
}),
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
await prisma.reasonEntry.upsert({
|
||||
where: { reasonId },
|
||||
create: {
|
||||
orgId: machine.orgId,
|
||||
machineId: machine.id,
|
||||
reasonId,
|
||||
kind: "downtime",
|
||||
episodeId: incidentKey,
|
||||
durationSeconds: durationSeconds != null ? Math.max(0, Math.trunc(durationSeconds)) : null,
|
||||
episodeEndTs: episodeEndTsMs != null ? new Date(episodeEndTsMs) : null,
|
||||
...guardedWrite,
|
||||
},
|
||||
update: {
|
||||
kind: "downtime",
|
||||
episodeId: incidentKey,
|
||||
durationSeconds: durationSeconds != null ? Math.max(0, Math.trunc(durationSeconds)) : null,
|
||||
episodeEndTs: episodeEndTsMs != null ? new Date(episodeEndTsMs) : null,
|
||||
...guardedWrite,
|
||||
},
|
||||
});
|
||||
} else {
|
||||
const scrapEntryId =
|
||||
clampText((reasonRaw as any).scrapEntryId, 128) ??
|
||||
clampText(evRecord.id, 128) ??
|
||||
clampText(evRecord.eventId, 128) ??
|
||||
row.id;
|
||||
const scrapQtyRaw =
|
||||
numberFrom(evRecord.scrapDelta) ??
|
||||
numberFrom(evData.scrapDelta) ??
|
||||
numberFrom(evData.scrap_delta) ??
|
||||
0;
|
||||
const scrapQty = Math.max(0, Math.trunc(scrapQtyRaw));
|
||||
|
||||
await prisma.reasonEntry.upsert({
|
||||
where: { reasonId },
|
||||
create: {
|
||||
orgId: machine.orgId,
|
||||
machineId: machine.id,
|
||||
reasonId,
|
||||
kind: "scrap",
|
||||
scrapEntryId,
|
||||
scrapQty,
|
||||
scrapUnit: clampText((reasonRaw as any).scrapUnit, 16) ?? null,
|
||||
...commonWrite,
|
||||
},
|
||||
update: {
|
||||
kind: "scrap",
|
||||
scrapEntryId,
|
||||
scrapQty,
|
||||
scrapUnit: clampText((reasonRaw as any).scrapUnit, 16) ?? null,
|
||||
...commonWrite,
|
||||
},
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
if (row.eventType !== "downtime-acknowledged" && row.eventType !== "scrap-manual-entry") {
|
||||
await evaluateAlertsForEvent(row.id);
|
||||
}
|
||||
} catch (err) {
|
||||
console.error("[alerts] evaluation failed", err);
|
||||
}
|
||||
}
|
||||
|
||||
return NextResponse.json({ ok: true, createdCount: created.length, created, skippedCount: skipped.length, skipped });
|
||||
|
||||
670
app/api/ingest/event/route.ts.bak
Normal file
670
app/api/ingest/event/route.ts.bak
Normal file
@@ -0,0 +1,670 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import { prisma } from "@/lib/prisma";
|
||||
import { getMachineAuth } from "@/lib/machineAuthCache";
|
||||
import { z } from "zod";
|
||||
import { evaluateAlertsForEvent } from "@/lib/alerts/engine";
|
||||
import { toJsonValue } from "@/lib/prismaJson";
|
||||
import {
|
||||
findCatalogReason,
|
||||
loadFallbackReasonCatalog,
|
||||
normalizeReasonCatalog,
|
||||
toReasonCode,
|
||||
type ReasonCatalog,
|
||||
type ReasonCatalogKind,
|
||||
} from "@/lib/reasonCatalog";
|
||||
|
||||
const normalizeType = (t: unknown) =>
|
||||
String(t ?? "")
|
||||
.trim()
|
||||
.toLowerCase()
|
||||
.replace(/_/g, "-");
|
||||
|
||||
function asRecord(value: unknown): Record<string, unknown> | null {
|
||||
if (!value || typeof value !== "object" || Array.isArray(value)) return null;
|
||||
return value as Record<string, unknown>;
|
||||
}
|
||||
|
||||
const CANON_TYPE: Record<string, string> = {
|
||||
// Node-RED
|
||||
"production-stopped": "stop",
|
||||
"oee-drop": "oee-drop",
|
||||
"quality-spike": "quality-spike",
|
||||
"predictive-oee-decline": "predictive-oee-decline",
|
||||
"performance-degradation": "performance-degradation",
|
||||
|
||||
// legacy / synonyms
|
||||
"macroparo": "macrostop",
|
||||
"macro-stop": "macrostop",
|
||||
"microparo": "microstop",
|
||||
"micro-paro": "microstop",
|
||||
"down": "stop",
|
||||
"downtime-acknowledged": "downtime-acknowledged",
|
||||
"scrap-manual-entry": "scrap-manual-entry",
|
||||
"mold-change": "mold-change",
|
||||
};
|
||||
|
||||
const ALLOWED_TYPES = new Set([
|
||||
"slow-cycle",
|
||||
"microstop",
|
||||
"macrostop",
|
||||
"offline",
|
||||
"error",
|
||||
"oee-drop",
|
||||
"quality-spike",
|
||||
"performance-degradation",
|
||||
"predictive-oee-decline",
|
||||
"downtime-acknowledged",
|
||||
"scrap-manual-entry",
|
||||
"mold-change",
|
||||
]);
|
||||
|
||||
const machineIdSchema = z.string().uuid();
|
||||
const MAX_EVENTS = 100;
|
||||
|
||||
//when no cycle time is configed
|
||||
const DEFAULT_MACROSTOP_SEC = 300;
|
||||
|
||||
|
||||
function clampText(value: unknown, maxLen: number) {
|
||||
if (value === null || value === undefined) return null;
|
||||
const text = String(value).trim().replace(/[\u0000-\u001f\u007f]/g, "");
|
||||
if (!text) return null;
|
||||
return text.length > maxLen ? text.slice(0, maxLen) : text;
|
||||
}
|
||||
|
||||
function numberFrom(value: unknown) {
|
||||
if (typeof value === "number" && Number.isFinite(value)) return value;
|
||||
if (typeof value === "string" && value.trim()) {
|
||||
const parsed = Number(value);
|
||||
if (Number.isFinite(parsed)) return parsed;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
function parseSeqToBigInt(value: unknown): bigint | null {
|
||||
if (value === null || value === undefined) return null;
|
||||
if (typeof value === "number") {
|
||||
if (!Number.isInteger(value) || value < 0) return null;
|
||||
return BigInt(value);
|
||||
}
|
||||
if (typeof value === "string" && /^\d+$/.test(value)) return BigInt(value);
|
||||
return null;
|
||||
}
|
||||
|
||||
function canonicalText(value: unknown) {
|
||||
return String(value ?? "")
|
||||
.normalize("NFD")
|
||||
.replace(/[\u0300-\u036f]/g, "")
|
||||
.toLowerCase()
|
||||
.replace(/[^a-z0-9]+/g, "-")
|
||||
.replace(/^-+|-+$/g, "");
|
||||
}
|
||||
|
||||
function parseReasonPath(rawPath: unknown) {
|
||||
let category: string | null = null;
|
||||
let detail: string | null = null;
|
||||
|
||||
if (Array.isArray(rawPath)) {
|
||||
const first = rawPath[0];
|
||||
const second = rawPath[1];
|
||||
if (typeof first === "string") category = first;
|
||||
if (typeof second === "string") detail = second;
|
||||
if (asRecord(first)) category = clampText(first.id ?? first.label ?? first.value, 120);
|
||||
if (asRecord(second)) detail = clampText(second.id ?? second.label ?? second.value, 120);
|
||||
} else if (typeof rawPath === "string") {
|
||||
const pieces = rawPath
|
||||
.split(/>|\/|\\|\|/g)
|
||||
.map((p) => p.trim())
|
||||
.filter(Boolean);
|
||||
category = pieces[0] ?? null;
|
||||
detail = pieces[1] ?? null;
|
||||
}
|
||||
|
||||
return {
|
||||
category: clampText(category, 120),
|
||||
detail: clampText(detail, 120),
|
||||
};
|
||||
}
|
||||
|
||||
function parseReasonTextPath(reasonText: unknown) {
|
||||
const text = clampText(reasonText, 240);
|
||||
if (!text) return { category: null as string | null, detail: null as string | null };
|
||||
const pieces = text
|
||||
.split(/>|\/|\\|\|/g)
|
||||
.map((p) => p.trim())
|
||||
.filter(Boolean);
|
||||
return {
|
||||
category: clampText(pieces[0] ?? null, 120),
|
||||
detail: clampText(pieces[1] ?? null, 120),
|
||||
};
|
||||
}
|
||||
|
||||
function findCatalogReasonFlexible(
|
||||
catalog: ReasonCatalog | null,
|
||||
kind: ReasonCatalogKind,
|
||||
categoryIdOrLabel: unknown,
|
||||
detailIdOrLabel: unknown
|
||||
) {
|
||||
const direct = findCatalogReason(catalog, kind, categoryIdOrLabel, detailIdOrLabel);
|
||||
if (direct) return direct;
|
||||
if (!catalog) return null;
|
||||
|
||||
const catNeedle = canonicalText(categoryIdOrLabel);
|
||||
const detNeedle = canonicalText(detailIdOrLabel);
|
||||
if (!catNeedle || !detNeedle) return null;
|
||||
|
||||
for (const category of catalog[kind] ?? []) {
|
||||
const catMatch =
|
||||
canonicalText(category.id) === catNeedle || canonicalText(category.label) === catNeedle;
|
||||
if (!catMatch) continue;
|
||||
for (const detail of category.details) {
|
||||
const detMatch = canonicalText(detail.id) === detNeedle || canonicalText(detail.label) === detNeedle;
|
||||
if (!detMatch) continue;
|
||||
return {
|
||||
categoryId: category.id,
|
||||
categoryLabel: category.label,
|
||||
detailId: detail.id,
|
||||
detailLabel: detail.label,
|
||||
reasonCode: toReasonCode(category.id, detail.id),
|
||||
reasonLabel: `${category.label} > ${detail.label}`,
|
||||
};
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
function getCatalogFromDefaults(defaultsJson: unknown) {
|
||||
const defaults = asRecord(defaultsJson);
|
||||
if (!defaults) return null;
|
||||
return normalizeReasonCatalog(defaults.reasonCatalog ?? defaults.reasonCatalogData);
|
||||
}
|
||||
|
||||
function resolveReason(
|
||||
raw: Record<string, unknown>,
|
||||
kind: ReasonCatalogKind,
|
||||
catalog: ReasonCatalog | null,
|
||||
fallbackVersion: number
|
||||
) {
|
||||
const reasonPath = parseReasonPath(raw.reasonPath);
|
||||
const reasonTextPath = parseReasonTextPath(raw.reasonText);
|
||||
const categoryIdRaw = clampText(raw.categoryId ?? reasonPath.category ?? reasonTextPath.category, 64);
|
||||
const detailIdRaw = clampText(raw.detailId ?? reasonPath.detail ?? reasonTextPath.detail, 64);
|
||||
const fromCatalog = findCatalogReasonFlexible(catalog, kind, categoryIdRaw, detailIdRaw);
|
||||
|
||||
const categoryLabelRaw = clampText(raw.categoryLabel ?? reasonPath.category ?? reasonTextPath.category, 120);
|
||||
const detailLabelRaw = clampText(raw.detailLabel ?? reasonPath.detail ?? reasonTextPath.detail, 120);
|
||||
|
||||
const reasonCode =
|
||||
clampText(raw.reasonCode, 64)?.toUpperCase() ??
|
||||
fromCatalog?.reasonCode ??
|
||||
toReasonCode(categoryIdRaw ?? categoryLabelRaw, detailIdRaw ?? detailLabelRaw) ??
|
||||
null;
|
||||
|
||||
const categoryId = fromCatalog?.categoryId ?? categoryIdRaw;
|
||||
const detailId = fromCatalog?.detailId ?? detailIdRaw;
|
||||
const categoryLabel = fromCatalog?.categoryLabel ?? categoryLabelRaw;
|
||||
const detailLabel = fromCatalog?.detailLabel ?? detailLabelRaw;
|
||||
|
||||
const pathLabel =
|
||||
clampText(raw.reasonText, 240) ??
|
||||
fromCatalog?.reasonLabel ??
|
||||
(categoryLabel && detailLabel ? `${categoryLabel} > ${detailLabel}` : null) ??
|
||||
detailLabel ??
|
||||
categoryLabel ??
|
||||
reasonCode;
|
||||
|
||||
const catalogVersionRaw = numberFrom(raw.catalogVersion);
|
||||
const catalogVersion = catalogVersionRaw != null ? Math.trunc(catalogVersionRaw) : fallbackVersion;
|
||||
|
||||
return {
|
||||
type: kind,
|
||||
categoryId,
|
||||
categoryLabel,
|
||||
detailId,
|
||||
detailLabel,
|
||||
reasonCode,
|
||||
reasonLabel: pathLabel,
|
||||
reasonText: pathLabel,
|
||||
catalogVersion,
|
||||
};
|
||||
}
|
||||
|
||||
export async function POST(req: Request) {
|
||||
const apiKey = req.headers.get("x-api-key");
|
||||
if (!apiKey) return NextResponse.json({ ok: false, error: "Missing api key" }, { status: 401 });
|
||||
|
||||
let body: unknown = await req.json().catch(() => null);
|
||||
|
||||
// ✅ if Node-RED sent an array as the whole body, unwrap it
|
||||
if (Array.isArray(body)) body = body[0];
|
||||
const bodyRecord = asRecord(body) ?? {};
|
||||
const payloadRecord = asRecord(bodyRecord.payload) ?? {};
|
||||
|
||||
// ✅ accept multiple common keys
|
||||
const machineId =
|
||||
bodyRecord.machineId ??
|
||||
bodyRecord.machine_id ??
|
||||
(asRecord(bodyRecord.machine)?.id ?? null);
|
||||
let rawEvent =
|
||||
bodyRecord.event ??
|
||||
bodyRecord.events ??
|
||||
bodyRecord.anomalies ??
|
||||
payloadRecord.event ??
|
||||
payloadRecord.events ??
|
||||
payloadRecord.anomalies ??
|
||||
payloadRecord ??
|
||||
bodyRecord.data; // sometimes "data"
|
||||
|
||||
const rawEventRecord = asRecord(rawEvent);
|
||||
if (rawEventRecord?.event && typeof rawEventRecord.event === "object") rawEvent = rawEventRecord.event;
|
||||
if (Array.isArray(rawEventRecord?.events)) rawEvent = rawEventRecord.events;
|
||||
|
||||
if (!machineId || !rawEvent) {
|
||||
return NextResponse.json(
|
||||
{ ok: false, error: "Invalid payload", got: { hasMachineId: !!machineId, keys: Object.keys(bodyRecord) } },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
if (!machineIdSchema.safeParse(String(machineId)).success) {
|
||||
return NextResponse.json({ ok: false, error: "Invalid machine id" }, { status: 400 });
|
||||
}
|
||||
|
||||
const machine = await getMachineAuth(String(machineId), apiKey);
|
||||
if (!machine) return NextResponse.json({ ok: false, error: "Unauthorized" }, { status: 401 });
|
||||
const orgSettings = await prisma.orgSettings.findUnique({
|
||||
where: { orgId: machine.orgId },
|
||||
select: { stoppageMultiplier: true, macroStoppageMultiplier: true, defaultsJson: true },
|
||||
});
|
||||
const fallbackCatalog = await loadFallbackReasonCatalog();
|
||||
const settingsCatalog = getCatalogFromDefaults(orgSettings?.defaultsJson);
|
||||
const reasonCatalog = settingsCatalog ?? fallbackCatalog;
|
||||
|
||||
const defaultMicroMultiplier = Number(orgSettings?.stoppageMultiplier ?? 1.5);
|
||||
const defaultMacroMultiplier = Math.max(
|
||||
defaultMicroMultiplier,
|
||||
Number(orgSettings?.macroStoppageMultiplier ?? 5)
|
||||
);
|
||||
|
||||
|
||||
// ✅ normalize to array no matter what
|
||||
const events = Array.isArray(rawEvent) ? rawEvent : [rawEvent];
|
||||
if (events.length > MAX_EVENTS) {
|
||||
return NextResponse.json({ ok: false, error: "Too many events" }, { status: 400 });
|
||||
}
|
||||
|
||||
const created: { id: string; ts: Date; eventType: string }[] = [];
|
||||
const skipped: Array<Record<string, unknown>> = [];
|
||||
|
||||
for (const ev of events) {
|
||||
const evRecord = asRecord(ev);
|
||||
if (!evRecord) {
|
||||
skipped.push({ reason: "invalid_event_object" });
|
||||
continue;
|
||||
}
|
||||
const evData = asRecord(evRecord.data) ?? {};
|
||||
// We'll re-check reason again after parsing `data` (it may be a JSON string)
|
||||
let evReason = asRecord(evRecord.reason) ?? asRecord(evData.reason);
|
||||
const evDowntime = asRecord(evRecord.downtime) ?? asRecord(evData.downtime);
|
||||
// Some producers nest the reason under `downtime.reason`
|
||||
if (!evReason) evReason = asRecord(evDowntime?.reason);
|
||||
|
||||
const rawType = evRecord.eventType ?? evRecord.anomaly_type ?? evRecord.topic ?? bodyRecord.topic ?? "";
|
||||
const typ0 = normalizeType(rawType);
|
||||
const typ = CANON_TYPE[typ0] ?? typ0;
|
||||
|
||||
// Determine timestamp
|
||||
const tsMs =
|
||||
(typeof evRecord.timestamp === "number" && evRecord.timestamp) ||
|
||||
(typeof evData.timestamp === "number" && evData.timestamp) ||
|
||||
(typeof evData.event_timestamp === "number" && evData.event_timestamp) ||
|
||||
null;
|
||||
|
||||
const ts = tsMs ? new Date(tsMs) : new Date();
|
||||
|
||||
// Severity defaulting (do not skip on severity — store for audit)
|
||||
let sev = String(evRecord.severity ?? "").trim().toLowerCase();
|
||||
if (!sev) sev = "warning";
|
||||
|
||||
// Stop classification -> microstop/macrostop
|
||||
let finalType = typ;
|
||||
let stopSecForReason: number | null = null;
|
||||
if (typ === "stop") {
|
||||
const stopSec =
|
||||
(typeof evData.stoppage_duration_seconds === "number" && evData.stoppage_duration_seconds) ||
|
||||
(typeof evData.stop_duration_seconds === "number" && evData.stop_duration_seconds) ||
|
||||
null;
|
||||
stopSecForReason = stopSec != null ? Number(stopSec) : null;
|
||||
|
||||
if (stopSec != null) {
|
||||
const theoretical = Number(evData.theoretical_cycle_time ?? evData.theoreticalCycleTime ?? 0) || 0;
|
||||
|
||||
const microMultiplier = Number(
|
||||
evData.micro_threshold_multiplier ?? evData.threshold_multiplier ?? defaultMicroMultiplier
|
||||
);
|
||||
const macroMultiplier = Math.max(
|
||||
microMultiplier,
|
||||
Number(evData.macro_threshold_multiplier ?? defaultMacroMultiplier)
|
||||
);
|
||||
|
||||
if (theoretical > 0) {
|
||||
const macroThresholdSec = theoretical * macroMultiplier;
|
||||
finalType = stopSec >= macroThresholdSec ? "macrostop" : "microstop";
|
||||
} else {
|
||||
finalType = stopSec >= DEFAULT_MACROSTOP_SEC ? "macrostop" : "microstop";
|
||||
}
|
||||
} else {
|
||||
// missing duration -> conservative
|
||||
finalType = "microstop";
|
||||
}
|
||||
}
|
||||
|
||||
if (!ALLOWED_TYPES.has(finalType)) {
|
||||
skipped.push({ reason: "type_not_allowed", typ: finalType, sev });
|
||||
continue;
|
||||
}
|
||||
|
||||
const title =
|
||||
clampText(evRecord.title, 160) ||
|
||||
(finalType === "slow-cycle" ? "Slow Cycle Detected" :
|
||||
finalType === "macrostop" ? "Macrostop Detected" :
|
||||
finalType === "microstop" ? "Microstop Detected" :
|
||||
"Event");
|
||||
|
||||
const description = clampText(evRecord.description, 1000);
|
||||
|
||||
// store full blob, ensure object
|
||||
const rawData = evRecord.data ?? evRecord;
|
||||
const parsedData = typeof rawData === "string"
|
||||
? (() => {
|
||||
try {
|
||||
return JSON.parse(rawData);
|
||||
} catch {
|
||||
return { raw: rawData };
|
||||
}
|
||||
})()
|
||||
: rawData;
|
||||
const dataObj: Record<string, unknown> =
|
||||
parsedData && typeof parsedData === "object" && !Array.isArray(parsedData)
|
||||
? { ...(parsedData as Record<string, unknown>) }
|
||||
: { raw: parsedData };
|
||||
if (evRecord.status != null && dataObj.status == null) dataObj.status = evRecord.status;
|
||||
if (evRecord.alert_id != null && dataObj.alert_id == null) dataObj.alert_id = evRecord.alert_id;
|
||||
if (evRecord.is_update != null && dataObj.is_update == null) dataObj.is_update = evRecord.is_update;
|
||||
if (evRecord.is_auto_ack != null && dataObj.is_auto_ack == null) dataObj.is_auto_ack = evRecord.is_auto_ack;
|
||||
if (evReason && dataObj.reason == null) dataObj.reason = evReason;
|
||||
if (evDowntime && dataObj.downtime == null) dataObj.downtime = evDowntime;
|
||||
|
||||
// If `data` was a JSON string, the earlier evReason lookup would miss it.
|
||||
// Re-check here using the normalized object we will persist.
|
||||
if (!evReason) evReason = asRecord(dataObj.reason);
|
||||
if (!evReason) evReason = asRecord(asRecord(dataObj.downtime)?.reason);
|
||||
|
||||
// If we have a reasonText but missing ids, derive ids from the path-like string.
|
||||
if (evReason) {
|
||||
const hasCat = clampText((evReason as any).categoryId, 64) ?? clampText((evReason as any).categoryLabel, 120);
|
||||
const hasDet = clampText((evReason as any).detailId, 64) ?? clampText((evReason as any).detailLabel, 120);
|
||||
const rt = clampText((evReason as any).reasonText, 240);
|
||||
if ((!hasCat || !hasDet) && rt) {
|
||||
const parsed = parseReasonTextPath(rt);
|
||||
const next = { ...evReason } as Record<string, unknown>;
|
||||
// Preserve any explicit ids; only fill gaps.
|
||||
if ((next as any).categoryId == null && parsed.category) next.categoryId = canonicalText(parsed.category);
|
||||
if ((next as any).categoryLabel == null && parsed.category) next.categoryLabel = parsed.category;
|
||||
if ((next as any).detailId == null && parsed.detail) next.detailId = canonicalText(parsed.detail);
|
||||
if ((next as any).detailLabel == null && parsed.detail) next.detailLabel = parsed.detail;
|
||||
evReason = next;
|
||||
}
|
||||
}
|
||||
|
||||
const activeWorkOrder = asRecord(evRecord.activeWorkOrder);
|
||||
const dataActiveWorkOrder = asRecord(evData.activeWorkOrder);
|
||||
|
||||
// ✨ Cada evento puede traer su propio seq, o usar el del payload raíz
|
||||
const evSeq =
|
||||
parseSeqToBigInt(evRecord.seq) ??
|
||||
parseSeqToBigInt(evData.seq) ??
|
||||
bodySeq;
|
||||
|
||||
const evSchemaVersion =
|
||||
clampText(evRecord.schemaVersion, 16) ??
|
||||
bodySchemaVersion;
|
||||
|
||||
const eventData = {
|
||||
orgId: machine.orgId,
|
||||
machineId: machine.id,
|
||||
schemaVersion: evSchemaVersion,
|
||||
seq: evSeq,
|
||||
ts,
|
||||
topic: clampText(evRecord.topic ?? finalType, 64) ?? finalType,
|
||||
eventType: finalType,
|
||||
severity: sev,
|
||||
requiresAck: !!evRecord.requires_ack,
|
||||
title,
|
||||
description,
|
||||
data: toJsonValue(dataObj),
|
||||
workOrderId:
|
||||
clampText(evRecord.work_order_id, 64) ??
|
||||
clampText(evData.work_order_id, 64) ??
|
||||
clampText(activeWorkOrder?.id, 64) ??
|
||||
clampText(dataActiveWorkOrder?.id, 64) ??
|
||||
null,
|
||||
sku:
|
||||
clampText(evRecord.sku, 64) ??
|
||||
clampText(evData.sku, 64) ??
|
||||
clampText(activeWorkOrder?.sku, 64) ??
|
||||
clampText(dataActiveWorkOrder?.sku, 64) ??
|
||||
null,
|
||||
};
|
||||
|
||||
// ✨ Idempotente: si ya existe (mismo orgId+machineId+seq), no inserta
|
||||
const insertResult = await prisma.machineEvent.createMany({
|
||||
data: [eventData],
|
||||
skipDuplicates: true,
|
||||
});
|
||||
|
||||
// ✨ Buscar la fila (la recién creada o la duplicada existente)
|
||||
let row;
|
||||
if (evSeq != null) {
|
||||
row = await prisma.machineEvent.findFirst({
|
||||
where: {
|
||||
orgId: machine.orgId,
|
||||
machineId: machine.id,
|
||||
seq: evSeq,
|
||||
},
|
||||
orderBy: { ts_server: "asc" },
|
||||
});
|
||||
} else {
|
||||
// Sin seq, buscar por ts (fallback compatibilidad con eventos viejos)
|
||||
row = await prisma.machineEvent.findFirst({
|
||||
where: {
|
||||
orgId: machine.orgId,
|
||||
machineId: machine.id,
|
||||
ts,
|
||||
eventType: finalType,
|
||||
},
|
||||
orderBy: { ts_server: "desc" },
|
||||
});
|
||||
}
|
||||
|
||||
if (!row) {
|
||||
skipped.push({ reason: "row_not_found_after_insert", seq: evSeq?.toString() });
|
||||
continue;
|
||||
}
|
||||
|
||||
const wasDuplicate = insertResult.count === 0;
|
||||
|
||||
// Si fue duplicado, no procesar reasonEntry ni alertas (ya se hicieron antes)
|
||||
if (wasDuplicate) {
|
||||
created.push({ id: row.id, ts: row.ts, eventType: row.eventType });
|
||||
continue; // ✨ saltar el resto del procesamiento
|
||||
}
|
||||
|
||||
created.push({ id: row.id, ts: row.ts, eventType: row.eventType });
|
||||
|
||||
|
||||
|
||||
// If the payload carries a `reason`, create the corresponding ReasonEntry.
|
||||
// If it doesn't, still create an "UNCLASSIFIED" downtime ReasonEntry for stop events so the dashboard can show coverage.
|
||||
if (evRecord.is_update || evRecord.is_auto_ack || dataObj.is_update || dataObj.is_auto_ack){
|
||||
// skip duplicate reasonEntry for refresh/ack
|
||||
} else if (evReason || finalType === "microstop" || finalType === "macrostop" || finalType === "downtime-acknowledged" || finalType === "mold-change"){
|
||||
const moldIncidentKey =
|
||||
clampText(evData.incidentKey ?? dataObj.incidentKey, 128) ??
|
||||
(numberFrom(evData.start_ms ?? dataObj.start_ms) != null
|
||||
? `mold-change:${Math.trunc(numberFrom(evData.start_ms ?? dataObj.start_ms) as number)}`
|
||||
: null);
|
||||
const reasonRaw: Record<string, unknown> =
|
||||
evReason ??
|
||||
(finalType === "mold-change"
|
||||
? ({
|
||||
type: "downtime",
|
||||
categoryId: "cambio-molde",
|
||||
detailId: "cambio-molde",
|
||||
categoryLabel: "Cambio molde",
|
||||
detailLabel: "Cambio molde",
|
||||
reasonCode: "MOLD_CHANGE",
|
||||
reasonText: "Cambio molde",
|
||||
incidentKey: moldIncidentKey ?? row.id,
|
||||
} as Record<string, unknown>)
|
||||
:
|
||||
({
|
||||
type: "downtime",
|
||||
categoryId: "unclassified",
|
||||
detailId: "unclassified",
|
||||
categoryLabel: "Unclassified",
|
||||
detailLabel: "Unclassified",
|
||||
reasonCode: "UNCLASSIFIED",
|
||||
reasonText: "Unclassified",
|
||||
incidentKey: row.id,
|
||||
} as Record<string, unknown>));
|
||||
|
||||
const inferredKind: ReasonCatalogKind =
|
||||
String(reasonRaw.type ?? "").toLowerCase() === "scrap" || finalType === "scrap-manual-entry"
|
||||
? "scrap"
|
||||
: "downtime";
|
||||
const resolved = resolveReason(reasonRaw, inferredKind, reasonCatalog, reasonCatalog.version);
|
||||
|
||||
if (resolved.reasonCode) {
|
||||
const reasonId =
|
||||
clampText(reasonRaw.reasonId, 128) ??
|
||||
(inferredKind === "downtime"
|
||||
? `evt:${machine.id}:downtime:${clampText((reasonRaw as any).incidentKey ?? evDowntime?.incidentKey, 128) ?? row.id}`
|
||||
: `evt:${machine.id}:scrap:${clampText(reasonRaw.scrapEntryId, 128) ?? row.id}`);
|
||||
|
||||
const workOrderId =
|
||||
clampText(evRecord.work_order_id, 64) ??
|
||||
clampText(evData.work_order_id, 64) ??
|
||||
clampText(evRecord.workOrderId, 64) ??
|
||||
null;
|
||||
|
||||
const commonWrite = {
|
||||
reasonCode: resolved.reasonCode,
|
||||
reasonLabel: resolved.reasonLabel ?? resolved.reasonCode,
|
||||
reasonText: resolved.reasonText ?? null,
|
||||
capturedAt: row.ts,
|
||||
workOrderId,
|
||||
schemaVersion: Math.max(1, Math.trunc(resolved.catalogVersion)),
|
||||
meta: toJsonValue({
|
||||
source: "ingest:event",
|
||||
eventId: row.id,
|
||||
eventType: row.eventType,
|
||||
incidentKey: clampText((reasonRaw as any).incidentKey ?? evDowntime?.incidentKey, 128),
|
||||
anomalyType:
|
||||
clampText(evRecord.anomalyType, 64) ??
|
||||
clampText(evDowntime?.anomalyType, 64) ??
|
||||
clampText(evRecord.anomaly_type, 64),
|
||||
reason: {
|
||||
type: resolved.type,
|
||||
categoryId: resolved.categoryId,
|
||||
categoryLabel: resolved.categoryLabel,
|
||||
detailId: resolved.detailId,
|
||||
detailLabel: resolved.detailLabel,
|
||||
reasonText: resolved.reasonText,
|
||||
catalogVersion: resolved.catalogVersion,
|
||||
},
|
||||
}),
|
||||
};
|
||||
|
||||
if (inferredKind === "downtime") {
|
||||
const incidentKey = clampText((reasonRaw as any).incidentKey ?? evDowntime?.incidentKey, 128) ?? row.id;
|
||||
const durationSeconds =
|
||||
numberFrom(evDowntime?.durationSeconds) ??
|
||||
numberFrom(evData.duration_sec) ??
|
||||
numberFrom(evData.stoppage_duration_seconds) ??
|
||||
numberFrom(evData.stop_duration_seconds) ??
|
||||
(stopSecForReason != null ? stopSecForReason : null) ??
|
||||
null;
|
||||
const episodeEndTsMs =
|
||||
numberFrom(evData.end_ms) ??
|
||||
numberFrom(evDowntime?.episodeEndTsMs) ??
|
||||
numberFrom(evDowntime?.acknowledgedAtMs) ??
|
||||
null;
|
||||
|
||||
await prisma.reasonEntry.upsert({
|
||||
where: { reasonId },
|
||||
create: {
|
||||
orgId: machine.orgId,
|
||||
machineId: machine.id,
|
||||
reasonId,
|
||||
kind: "downtime",
|
||||
episodeId: incidentKey,
|
||||
durationSeconds: durationSeconds != null ? Math.max(0, Math.trunc(durationSeconds)) : null,
|
||||
episodeEndTs: episodeEndTsMs != null ? new Date(episodeEndTsMs) : null,
|
||||
...commonWrite,
|
||||
},
|
||||
update: {
|
||||
kind: "downtime",
|
||||
episodeId: incidentKey,
|
||||
durationSeconds: durationSeconds != null ? Math.max(0, Math.trunc(durationSeconds)) : null,
|
||||
episodeEndTs: episodeEndTsMs != null ? new Date(episodeEndTsMs) : null,
|
||||
...commonWrite,
|
||||
},
|
||||
});
|
||||
} else {
|
||||
const scrapEntryId =
|
||||
clampText((reasonRaw as any).scrapEntryId, 128) ??
|
||||
clampText(evRecord.id, 128) ??
|
||||
clampText(evRecord.eventId, 128) ??
|
||||
row.id;
|
||||
const scrapQtyRaw =
|
||||
numberFrom(evRecord.scrapDelta) ??
|
||||
numberFrom(evData.scrapDelta) ??
|
||||
numberFrom(evData.scrap_delta) ??
|
||||
0;
|
||||
const scrapQty = Math.max(0, Math.trunc(scrapQtyRaw));
|
||||
|
||||
await prisma.reasonEntry.upsert({
|
||||
where: { reasonId },
|
||||
create: {
|
||||
orgId: machine.orgId,
|
||||
machineId: machine.id,
|
||||
reasonId,
|
||||
kind: "scrap",
|
||||
scrapEntryId,
|
||||
scrapQty,
|
||||
scrapUnit: clampText((reasonRaw as any).scrapUnit, 16) ?? null,
|
||||
...commonWrite,
|
||||
},
|
||||
update: {
|
||||
kind: "scrap",
|
||||
scrapEntryId,
|
||||
scrapQty,
|
||||
scrapUnit: clampText((reasonRaw as any).scrapUnit, 16) ?? null,
|
||||
...commonWrite,
|
||||
},
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
if (row.eventType !== "downtime-acknowledged" && row.eventType !== "scrap-manual-entry") {
|
||||
await evaluateAlertsForEvent(row.id);
|
||||
}
|
||||
} catch (err) {
|
||||
console.error("[alerts] evaluation failed", err);
|
||||
}
|
||||
}
|
||||
|
||||
return NextResponse.json({ ok: true, createdCount: created.length, created, skippedCount: skipped.length, skipped });
|
||||
}
|
||||
@@ -1,32 +1,174 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import { prisma } from "@/lib/prisma";
|
||||
import { getMachineAuth } from "@/lib/machineAuthCache";
|
||||
import { normalizeHeartbeatV1 } from "@/lib/contracts/v1";
|
||||
import { toJsonValue } from "@/lib/prismaJson";
|
||||
|
||||
function getClientIp(req: Request) {
|
||||
const xf = req.headers.get("x-forwarded-for");
|
||||
if (xf) return xf.split(",")[0]?.trim() || null;
|
||||
return req.headers.get("x-real-ip") || null;
|
||||
}
|
||||
|
||||
function parseSeqToBigInt(seq: unknown): bigint | null {
|
||||
if (seq === null || seq === undefined) return null;
|
||||
if (typeof seq === "number") {
|
||||
if (!Number.isInteger(seq) || seq < 0) return null;
|
||||
return BigInt(seq);
|
||||
}
|
||||
if (typeof seq === "string" && /^\d+$/.test(seq)) return BigInt(seq);
|
||||
return null;
|
||||
}
|
||||
|
||||
export async function POST(req: Request) {
|
||||
const apiKey = req.headers.get("x-api-key");
|
||||
if (!apiKey) return NextResponse.json({ ok: false, error: "Missing api key" }, { status: 401 });
|
||||
const endpoint = "/api/ingest/heartbeat";
|
||||
const ip = getClientIp(req);
|
||||
const userAgent = req.headers.get("user-agent");
|
||||
|
||||
const body = await req.json().catch(() => null);
|
||||
if (!body?.machineId || !body?.status) {
|
||||
return NextResponse.json({ ok: false, error: "Invalid payload" }, { status: 400 });
|
||||
let rawBody: unknown = null;
|
||||
let orgId: string | null = null;
|
||||
let machineId: string | null = null;
|
||||
let seq: bigint | null = null;
|
||||
let schemaVersion: string | null = null;
|
||||
let tsDeviceDate: Date | null = null;
|
||||
|
||||
try {
|
||||
// 1) Auth header exists
|
||||
const apiKey = req.headers.get("x-api-key");
|
||||
if (!apiKey) {
|
||||
await prisma.ingestLog.create({
|
||||
data: { endpoint, ok: false, status: 401, errorCode: "MISSING_API_KEY", errorMsg: "Missing api key", ip, userAgent },
|
||||
});
|
||||
return NextResponse.json({ ok: false, error: "Missing api key" }, { status: 401 });
|
||||
}
|
||||
|
||||
const machine = await prisma.machine.findFirst({
|
||||
where: { id: String(body.machineId), apiKey },
|
||||
select: { id: true, orgId: true },
|
||||
});
|
||||
// 2) Parse JSON
|
||||
rawBody = await req.json().catch(() => null);
|
||||
|
||||
if (!machine) return NextResponse.json({ ok: false, error: "Unauthorized" }, { status: 401 });
|
||||
|
||||
const hb = await prisma.machineHeartbeat.create({
|
||||
// 3) Normalize to v1 (legacy tolerated)
|
||||
const normalized = normalizeHeartbeatV1(rawBody);
|
||||
if (!normalized.ok) {
|
||||
await prisma.ingestLog.create({
|
||||
data: {
|
||||
orgId: machine.orgId,
|
||||
endpoint,
|
||||
ok: false,
|
||||
status: 400,
|
||||
errorCode: "INVALID_PAYLOAD",
|
||||
errorMsg: normalized.error,
|
||||
body: toJsonValue(rawBody),
|
||||
ip,
|
||||
userAgent,
|
||||
},
|
||||
});
|
||||
return NextResponse.json({ ok: false, error: "Invalid payload", detail: normalized.error }, { status: 400 });
|
||||
}
|
||||
|
||||
const body = normalized.value;
|
||||
schemaVersion = body.schemaVersion;
|
||||
machineId = body.machineId;
|
||||
seq = parseSeqToBigInt(body.seq);
|
||||
tsDeviceDate = new Date(body.tsDevice);
|
||||
|
||||
// 4) Authorize machineId + apiKey
|
||||
const machine = await getMachineAuth(machineId, apiKey);
|
||||
|
||||
if (!machine) {
|
||||
await prisma.ingestLog.create({
|
||||
data: {
|
||||
endpoint,
|
||||
ok: false,
|
||||
status: 401,
|
||||
errorCode: "UNAUTHORIZED",
|
||||
errorMsg: "Unauthorized (machineId/apiKey mismatch)",
|
||||
body: toJsonValue(rawBody),
|
||||
machineId,
|
||||
schemaVersion,
|
||||
seq,
|
||||
tsDevice: tsDeviceDate,
|
||||
ip,
|
||||
userAgent,
|
||||
},
|
||||
});
|
||||
return NextResponse.json({ ok: false, error: "Unauthorized" }, { status: 401 });
|
||||
}
|
||||
|
||||
orgId = machine.orgId;
|
||||
|
||||
// 5) Store heartbeat
|
||||
// Keep your legacy fields, but store meta fields too.
|
||||
const tsServerNow = new Date();
|
||||
const hbRow = {
|
||||
orgId,
|
||||
machineId: machine.id,
|
||||
status: String(body.status),
|
||||
schemaVersion,
|
||||
seq,
|
||||
ts: tsDeviceDate,
|
||||
tsServer: tsServerNow,
|
||||
status: body.status ? String(body.status) : (body.online ? "RUN" : "STOP"),
|
||||
message: body.message ? String(body.message) : null,
|
||||
ip: body.ip ? String(body.ip) : null,
|
||||
fwVersion: body.fwVersion ? String(body.fwVersion) : null,
|
||||
};
|
||||
|
||||
const insertHb = await prisma.machineHeartbeat.createMany({
|
||||
data: [hbRow],
|
||||
skipDuplicates: true,
|
||||
});
|
||||
|
||||
const hb = await prisma.machineHeartbeat.findFirst({
|
||||
where: {
|
||||
orgId,
|
||||
machineId: machine.id,
|
||||
ts: tsDeviceDate,
|
||||
},
|
||||
orderBy: { tsServer: "asc" },
|
||||
});
|
||||
|
||||
if (!hb) {
|
||||
return NextResponse.json({ ok: false, error: "Server error", detail: "Heartbeat row missing" }, { status: 500 });
|
||||
}
|
||||
|
||||
// Optional: update machine last seen (same as KPI) — also on duplicate HB so lastSeen is fresh
|
||||
await prisma.machine.update({
|
||||
where: { id: machine.id },
|
||||
data: {
|
||||
schemaVersion,
|
||||
seq,
|
||||
tsDevice: tsDeviceDate,
|
||||
tsServer: tsServerNow,
|
||||
},
|
||||
});
|
||||
|
||||
return NextResponse.json({ ok: true, id: hb.id, ts: hb.ts });
|
||||
return NextResponse.json({
|
||||
ok: true,
|
||||
id: hb.id,
|
||||
tsDevice: hb.ts,
|
||||
tsServer: hb.tsServer,
|
||||
duplicate: insertHb.count === 0,
|
||||
});
|
||||
} catch (err: unknown) {
|
||||
const msg = err instanceof Error ? err.message : "Unknown error";
|
||||
|
||||
try {
|
||||
await prisma.ingestLog.create({
|
||||
data: {
|
||||
orgId,
|
||||
machineId,
|
||||
endpoint,
|
||||
ok: false,
|
||||
status: 500,
|
||||
errorCode: "SERVER_ERROR",
|
||||
errorMsg: msg,
|
||||
schemaVersion,
|
||||
seq,
|
||||
tsDevice: tsDeviceDate ?? undefined,
|
||||
body: toJsonValue(rawBody),
|
||||
ip,
|
||||
userAgent,
|
||||
},
|
||||
});
|
||||
} catch {}
|
||||
|
||||
return NextResponse.json({ ok: false, error: "Server error", detail: msg }, { status: 500 });
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,50 +1,363 @@
|
||||
// mis-control-tower/app/api/ingest/kpi/route.ts
|
||||
import { NextResponse } from "next/server";
|
||||
import { prisma } from "@/lib/prisma";
|
||||
import { getMachineAuth } from "@/lib/machineAuthCache";
|
||||
import { normalizeSnapshotV1 } from "@/lib/contracts/v1";
|
||||
import { toJsonValue } from "@/lib/prismaJson";
|
||||
import { logLine } from "@/lib/logger";
|
||||
|
||||
export async function POST(req: Request) {
|
||||
const apiKey = req.headers.get("x-api-key");
|
||||
if (!apiKey) return NextResponse.json({ ok: false, error: "Missing api key" }, { status: 401 });
|
||||
function getClientIp(req: Request) {
|
||||
const xf = req.headers.get("x-forwarded-for");
|
||||
if (xf) return xf.split(",")[0]?.trim() || null;
|
||||
return req.headers.get("x-real-ip") || null;
|
||||
}
|
||||
|
||||
const body = await req.json().catch(() => null);
|
||||
if (!body?.machineId || !body?.kpis) {
|
||||
return NextResponse.json({ ok: false, error: "Invalid payload" }, { status: 400 });
|
||||
function parseSeqToBigInt(seq: unknown): bigint | null {
|
||||
if (seq === null || seq === undefined) return null;
|
||||
if (typeof seq === "number") {
|
||||
if (!Number.isInteger(seq) || seq < 0) return null;
|
||||
return BigInt(seq);
|
||||
}
|
||||
if (typeof seq === "string" && /^\d+$/.test(seq)) return BigInt(seq);
|
||||
return null;
|
||||
}
|
||||
|
||||
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 toFiniteNumber(value: unknown): number | null {
|
||||
if (typeof value === "number" && Number.isFinite(value)) return value;
|
||||
if (typeof value === "string") {
|
||||
const parsed = Number(value);
|
||||
if (Number.isFinite(parsed)) return parsed;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
function toFiniteInt(value: unknown): number | null {
|
||||
const parsed = toFiniteNumber(value);
|
||||
if (parsed == null) return null;
|
||||
return Math.trunc(parsed);
|
||||
}
|
||||
|
||||
function pickFirstNumber(...values: unknown[]) {
|
||||
for (const value of values) {
|
||||
const parsed = toFiniteNumber(value);
|
||||
if (parsed != null) return parsed;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
function readPath(root: unknown, path: string[]): unknown {
|
||||
let current = root;
|
||||
for (const key of path) {
|
||||
const record = asRecord(current);
|
||||
if (!record) return undefined;
|
||||
current = record[key];
|
||||
}
|
||||
return current;
|
||||
}
|
||||
|
||||
function collectQualityTrace(params: {
|
||||
rawBody: unknown;
|
||||
normalizedKpis: Record<string, unknown> | null;
|
||||
persistedQuality: number | null;
|
||||
machineId: string;
|
||||
rowId: string;
|
||||
}) {
|
||||
const { rawBody, normalizedKpis, persistedQuality, machineId, rowId } = params;
|
||||
const candidates = [
|
||||
"kpis.quality",
|
||||
"payload.kpis.quality",
|
||||
"kpi_snapshot.quality",
|
||||
"quality",
|
||||
"payload.quality",
|
||||
] as const;
|
||||
|
||||
const rawQualityCandidates: Record<string, { type: string; value: unknown }> = {};
|
||||
for (const path of candidates) {
|
||||
const value = readPath(rawBody, path.split("."));
|
||||
rawQualityCandidates[path] = {
|
||||
type: value === null ? "null" : typeof value,
|
||||
value,
|
||||
};
|
||||
}
|
||||
|
||||
const machine = await prisma.machine.findFirst({
|
||||
where: { id: String(body.machineId), apiKey },
|
||||
select: { id: true, orgId: true },
|
||||
});
|
||||
if (!machine) return NextResponse.json({ ok: false, error: "Unauthorized" }, { status: 401 });
|
||||
const normalizedQuality = normalizedKpis?.quality;
|
||||
return {
|
||||
machineId,
|
||||
rowId,
|
||||
rawQualityCandidates,
|
||||
normalizedQuality: {
|
||||
type: normalizedQuality === null ? "null" : typeof normalizedQuality,
|
||||
value: normalizedQuality ?? null,
|
||||
},
|
||||
persistedQuality: {
|
||||
type: persistedQuality === null ? "null" : typeof persistedQuality,
|
||||
value: persistedQuality,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
const wo = body.activeWorkOrder ?? {};
|
||||
const k = body.kpis ?? {};
|
||||
export async function POST(req: Request) {
|
||||
const endpoint = "/api/ingest/kpi";
|
||||
const startedAt = Date.now();
|
||||
const ip = getClientIp(req);
|
||||
const userAgent = req.headers.get("user-agent");
|
||||
const traceEnabled = process.env.TRACE_KPI_INGEST === "1" || req.headers.get("x-debug-ingest") === "1";
|
||||
|
||||
const row = await prisma.machineKpiSnapshot.create({
|
||||
let rawBody: unknown = null;
|
||||
let orgId: string | null = null;
|
||||
let machineId: string | null = null;
|
||||
let seq: bigint | null = null;
|
||||
let schemaVersion: string | null = null;
|
||||
let tsDeviceDate: Date | null = null;
|
||||
|
||||
try {
|
||||
const apiKey = req.headers.get("x-api-key");
|
||||
if (!apiKey) {
|
||||
await prisma.ingestLog.create({
|
||||
data: {
|
||||
orgId: machine.orgId,
|
||||
endpoint,
|
||||
ok: false,
|
||||
status: 401,
|
||||
errorCode: "MISSING_API_KEY",
|
||||
errorMsg: "Missing api key",
|
||||
ip,
|
||||
userAgent,
|
||||
},
|
||||
});
|
||||
return NextResponse.json({ ok: false, error: "Missing api key" }, { status: 401 });
|
||||
}
|
||||
|
||||
rawBody = await req.json().catch(() => null);
|
||||
const normalized = normalizeSnapshotV1(rawBody);
|
||||
if (!normalized.ok) {
|
||||
await prisma.ingestLog.create({
|
||||
data: {
|
||||
endpoint,
|
||||
ok: false,
|
||||
status: 400,
|
||||
errorCode: "INVALID_PAYLOAD",
|
||||
errorMsg: normalized.error,
|
||||
body: toJsonValue(rawBody),
|
||||
ip,
|
||||
userAgent,
|
||||
},
|
||||
});
|
||||
return NextResponse.json({ ok: false, error: "Invalid payload", detail: normalized.error }, { status: 400 });
|
||||
}
|
||||
|
||||
const body = normalized.value;
|
||||
|
||||
schemaVersion = body.schemaVersion;
|
||||
machineId = body.machineId;
|
||||
seq = parseSeqToBigInt(body.seq);
|
||||
tsDeviceDate = new Date(body.tsDevice);
|
||||
|
||||
// Auth: machineId + apiKey must match
|
||||
const machine = await getMachineAuth(machineId, apiKey);
|
||||
|
||||
if (!machine) {
|
||||
await prisma.ingestLog.create({
|
||||
data: {
|
||||
endpoint,
|
||||
ok: false,
|
||||
status: 401,
|
||||
errorCode: "UNAUTHORIZED",
|
||||
errorMsg: "Unauthorized (machineId/apiKey mismatch)",
|
||||
body: toJsonValue(rawBody),
|
||||
machineId,
|
||||
schemaVersion,
|
||||
seq,
|
||||
tsDevice: tsDeviceDate,
|
||||
ip,
|
||||
userAgent,
|
||||
},
|
||||
});
|
||||
return NextResponse.json({ ok: false, error: "Unauthorized" }, { status: 401 });
|
||||
}
|
||||
|
||||
orgId = machine.orgId;
|
||||
|
||||
const woRecord = (body.activeWorkOrder ?? {}) as Record<string, unknown>;
|
||||
const activeWorkOrderId = woRecord.id != null ? String(woRecord.id).trim() : "";
|
||||
const activeSku = woRecord.sku != null ? String(woRecord.sku).trim() : "";
|
||||
const activeStatus = woRecord.status != null ? String(woRecord.status).trim() : "";
|
||||
const activeTargetQty = toFiniteInt(woRecord.target);
|
||||
const activeCycleTime = toFiniteNumber(woRecord.cycleTime);
|
||||
const good = pickFirstNumber(woRecord.good, woRecord.goodParts, woRecord.good_parts);
|
||||
const scrap = pickFirstNumber(woRecord.scrap, woRecord.scrapParts, woRecord.scrap_parts);
|
||||
const activeGoodParts = Math.max(0, Math.trunc(good ?? 0));
|
||||
const activeScrapParts = Math.max(0, Math.trunc(scrap ?? 0));
|
||||
const activeCycleCount = Math.max(
|
||||
0,
|
||||
toFiniteInt(woRecord.cycleCount ?? woRecord.cycle_count ?? body.cycle_count) ?? 0
|
||||
);
|
||||
const snapshotCycleCount =
|
||||
toFiniteInt(body.cycle_count) ??
|
||||
toFiniteInt(woRecord.cycle_count) ??
|
||||
toFiniteInt(woRecord.cycleCount);
|
||||
const snapshotGoodParts =
|
||||
toFiniteInt(body.good_parts) ??
|
||||
toFiniteInt(woRecord.good_parts) ??
|
||||
toFiniteInt(woRecord.goodParts);
|
||||
const snapshotScrapParts =
|
||||
toFiniteInt(body.scrap_parts) ??
|
||||
toFiniteInt(woRecord.scrap_parts) ??
|
||||
toFiniteInt(woRecord.scrapParts);
|
||||
const k = body.kpis ?? {};
|
||||
const safeCycleTime =
|
||||
typeof body.cycleTime === "number" && body.cycleTime > 0
|
||||
? body.cycleTime
|
||||
: activeCycleTime != null && activeCycleTime > 0
|
||||
? activeCycleTime
|
||||
: null;
|
||||
|
||||
const safeCavities =
|
||||
typeof body.cavities === "number" && body.cavities > 0
|
||||
? body.cavities
|
||||
: typeof woRecord.cavities === "number" && woRecord.cavities > 0
|
||||
? woRecord.cavities
|
||||
: null;
|
||||
// Write snapshot (ts = tsDevice; tsServer auto). Idempotent on (org, machine, ts) to absorb retries.
|
||||
const kpiData = {
|
||||
orgId,
|
||||
machineId: machine.id,
|
||||
|
||||
workOrderId: wo.id ? String(wo.id) : null,
|
||||
sku: wo.sku ? String(wo.sku) : null,
|
||||
|
||||
target: typeof wo.target === "number" ? wo.target : null,
|
||||
good: typeof wo.good === "number" ? wo.good : null,
|
||||
scrap: typeof wo.scrap === "number" ? wo.scrap : null,
|
||||
|
||||
cycleCount: typeof body.cycle_count === "number" ? body.cycle_count : null,
|
||||
goodParts: typeof body.good_parts === "number" ? body.good_parts : null,
|
||||
|
||||
cycleTime: typeof body.cycleTime === "number" ? body.cycleTime : null,
|
||||
|
||||
schemaVersion,
|
||||
seq,
|
||||
ts: tsDeviceDate,
|
||||
workOrderId: activeWorkOrderId || null,
|
||||
sku: activeSku || null,
|
||||
target: activeTargetQty,
|
||||
good: good != null ? Math.trunc(good) : null,
|
||||
scrap: scrap != null ? Math.trunc(scrap) : null,
|
||||
cycleCount: snapshotCycleCount,
|
||||
goodParts: snapshotGoodParts,
|
||||
scrapParts: snapshotScrapParts,
|
||||
cavities: safeCavities,
|
||||
cycleTime: safeCycleTime,
|
||||
actualCycle: typeof body.actualCycleTime === "number" ? body.actualCycleTime : null,
|
||||
availability: typeof k.availability === "number" ? k.availability : null,
|
||||
performance: typeof k.performance === "number" ? k.performance : null,
|
||||
quality: typeof k.quality === "number" ? k.quality : null,
|
||||
oee: typeof k.oee === "number" ? k.oee : null,
|
||||
|
||||
trackingEnabled: typeof body.trackingEnabled === "boolean" ? body.trackingEnabled : null,
|
||||
productionStarted: typeof body.productionStarted === "boolean" ? body.productionStarted : null,
|
||||
};
|
||||
const insertKpi = await prisma.machineKpiSnapshot.createMany({
|
||||
data: [kpiData],
|
||||
skipDuplicates: true,
|
||||
});
|
||||
const row = await prisma.machineKpiSnapshot.findFirst({
|
||||
where: { orgId, machineId: machine.id, ts: tsDeviceDate },
|
||||
orderBy: { tsServer: "asc" },
|
||||
});
|
||||
if (!row) {
|
||||
return NextResponse.json({ ok: false, error: "Server error", detail: "KPI snapshot row missing" }, { status: 500 });
|
||||
}
|
||||
|
||||
if (activeWorkOrderId) {
|
||||
await prisma.machineWorkOrder.upsert({
|
||||
where: {
|
||||
machineId_workOrderId: {
|
||||
machineId: machine.id,
|
||||
workOrderId: activeWorkOrderId,
|
||||
},
|
||||
},
|
||||
create: {
|
||||
orgId: machine.orgId,
|
||||
machineId: machine.id,
|
||||
workOrderId: activeWorkOrderId,
|
||||
sku: activeSku || null,
|
||||
targetQty: activeTargetQty,
|
||||
cycleTime: activeCycleTime,
|
||||
status: activeStatus || "RUNNING",
|
||||
goodParts: activeGoodParts,
|
||||
scrapParts: activeScrapParts,
|
||||
cycleCount: activeCycleCount,
|
||||
},
|
||||
update: {
|
||||
sku: activeSku || undefined,
|
||||
targetQty: activeTargetQty ?? undefined,
|
||||
cycleTime: activeCycleTime ?? undefined,
|
||||
status: activeStatus || undefined,
|
||||
goodParts: activeGoodParts,
|
||||
scrapParts: activeScrapParts,
|
||||
cycleCount: activeCycleCount,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
// Optional but useful: update machine "last seen" meta fields
|
||||
await prisma.machine.update({
|
||||
where: { id: machine.id },
|
||||
data: {
|
||||
schemaVersion,
|
||||
seq,
|
||||
tsDevice: tsDeviceDate,
|
||||
tsServer: new Date(),
|
||||
},
|
||||
});
|
||||
|
||||
return NextResponse.json({ ok: true, id: row.id, ts: row.ts });
|
||||
const trace = collectQualityTrace({
|
||||
rawBody,
|
||||
normalizedKpis: asRecord(k),
|
||||
persistedQuality: row.quality ?? null,
|
||||
machineId: machine.id,
|
||||
rowId: row.id,
|
||||
});
|
||||
if (traceEnabled) {
|
||||
logLine("ingest.kpi.trace", {
|
||||
endpoint,
|
||||
machineId: machine.id,
|
||||
orgId,
|
||||
schemaVersion,
|
||||
seq: seq != null ? seq.toString() : null,
|
||||
ip,
|
||||
userAgent,
|
||||
trace,
|
||||
rawBody: toJsonValue(rawBody),
|
||||
});
|
||||
}
|
||||
|
||||
return NextResponse.json({
|
||||
ok: true,
|
||||
id: row.id,
|
||||
tsDevice: row.ts,
|
||||
tsServer: row.tsServer,
|
||||
duplicate: insertKpi.count === 0,
|
||||
trace: traceEnabled ? trace : undefined,
|
||||
});
|
||||
} catch (err: unknown) {
|
||||
const msg = err instanceof Error ? err.message : "Unknown error";
|
||||
|
||||
// Never fail the request because logging failed
|
||||
try {
|
||||
await prisma.ingestLog.create({
|
||||
data: {
|
||||
orgId,
|
||||
machineId,
|
||||
endpoint,
|
||||
ok: false,
|
||||
status: 500,
|
||||
errorCode: "SERVER_ERROR",
|
||||
errorMsg: msg,
|
||||
schemaVersion,
|
||||
seq,
|
||||
tsDevice: tsDeviceDate ?? undefined,
|
||||
body: toJsonValue(rawBody),
|
||||
ip,
|
||||
userAgent,
|
||||
},
|
||||
});
|
||||
} catch {}
|
||||
|
||||
return NextResponse.json({ ok: false, error: "Server error", detail: msg }, { status: 500 });
|
||||
} finally {
|
||||
// (If later you add latency_ms to IngestLog, you can store Date.now() - startedAt here.)
|
||||
void startedAt;
|
||||
}
|
||||
}
|
||||
|
||||
150
app/api/ingest/reason/route.ts
Normal file
150
app/api/ingest/reason/route.ts
Normal file
@@ -0,0 +1,150 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import { prisma } from "@/lib/prisma";
|
||||
|
||||
const bad = (status: number, error: string) =>
|
||||
NextResponse.json({ ok: false, error }, { status });
|
||||
|
||||
const asTrimmedString = (v: any) => {
|
||||
if (v == null) return "";
|
||||
return String(v).trim();
|
||||
};
|
||||
|
||||
export async function POST(req: Request) {
|
||||
const apiKey = req.headers.get("x-api-key");
|
||||
if (!apiKey) return bad(401, "Missing api key");
|
||||
|
||||
const body = await req.json().catch(() => null);
|
||||
if (!body?.machineId || !body?.reason) return bad(400, "Invalid payload");
|
||||
|
||||
const machine = await prisma.machine.findFirst({
|
||||
where: { id: String(body.machineId), apiKey },
|
||||
select: { id: true, orgId: true },
|
||||
});
|
||||
if (!machine) return bad(401, "Unauthorized");
|
||||
|
||||
const r = body.reason;
|
||||
|
||||
const reasonId = asTrimmedString(r.reasonId);
|
||||
if (!reasonId) return bad(400, "Missing reason.reasonId");
|
||||
|
||||
const kind = asTrimmedString(r.kind).toLowerCase();
|
||||
if (kind !== "downtime" && kind !== "scrap")
|
||||
return bad(400, "Invalid reason.kind");
|
||||
|
||||
const capturedAtMs = r.capturedAtMs;
|
||||
if (typeof capturedAtMs !== "number" || !Number.isFinite(capturedAtMs)) {
|
||||
return bad(400, "Invalid reason.capturedAtMs");
|
||||
}
|
||||
const capturedAt = new Date(capturedAtMs);
|
||||
|
||||
const reasonCodeRaw = asTrimmedString(r.reasonCode);
|
||||
if (!reasonCodeRaw) return bad(400, "Missing reason.reasonCode");
|
||||
const reasonCode = reasonCodeRaw.toUpperCase(); // normalize for grouping/pareto
|
||||
|
||||
const reasonLabel = r.reasonLabel != null ? String(r.reasonLabel) : null;
|
||||
|
||||
let reasonText = r.reasonText != null ? String(r.reasonText).trim() : null;
|
||||
if (reasonCode === "OTHER") {
|
||||
if (!reasonText || reasonText.length < 2)
|
||||
return bad(400, "reason.reasonText required when reasonCode=OTHER");
|
||||
} else {
|
||||
// Non-OTHER must not store free text
|
||||
reasonText = null;
|
||||
}
|
||||
|
||||
// Optional shared fields
|
||||
const workOrderId =
|
||||
r.workOrderId != null && String(r.workOrderId).trim()
|
||||
? String(r.workOrderId).trim()
|
||||
: null;
|
||||
|
||||
const schemaVersion =
|
||||
typeof r.schemaVersion === "number" && Number.isFinite(r.schemaVersion)
|
||||
? Math.trunc(r.schemaVersion)
|
||||
: 1;
|
||||
|
||||
const meta = r.meta != null ? r.meta : null;
|
||||
|
||||
// Kind-specific fields
|
||||
let episodeId: string | null = null;
|
||||
let durationSeconds: number | null = null;
|
||||
let episodeEndTs: Date | null = null;
|
||||
|
||||
let scrapEntryId: string | null = null;
|
||||
let scrapQty: number | null = null;
|
||||
let scrapUnit: string | null = null;
|
||||
|
||||
if (kind === "downtime") {
|
||||
episodeId = asTrimmedString(r.episodeId) || null;
|
||||
if (!episodeId) return bad(400, "Missing reason.episodeId for downtime");
|
||||
|
||||
if (typeof r.durationSeconds !== "number" || !Number.isFinite(r.durationSeconds)) {
|
||||
return bad(400, "Invalid reason.durationSeconds for downtime");
|
||||
}
|
||||
durationSeconds = Math.max(0, Math.trunc(r.durationSeconds));
|
||||
|
||||
const episodeEndTsMs = r.episodeEndTsMs;
|
||||
if (episodeEndTsMs != null) {
|
||||
if (typeof episodeEndTsMs !== "number" || !Number.isFinite(episodeEndTsMs)) {
|
||||
return bad(400, "Invalid reason.episodeEndTsMs");
|
||||
}
|
||||
episodeEndTs = new Date(episodeEndTsMs);
|
||||
}
|
||||
} else {
|
||||
scrapEntryId = asTrimmedString(r.scrapEntryId) || null;
|
||||
if (!scrapEntryId) return bad(400, "Missing reason.scrapEntryId for scrap");
|
||||
|
||||
if (typeof r.scrapQty !== "number" || !Number.isFinite(r.scrapQty)) {
|
||||
return bad(400, "Invalid reason.scrapQty for scrap");
|
||||
}
|
||||
scrapQty = Math.max(0, Math.trunc(r.scrapQty));
|
||||
|
||||
scrapUnit =
|
||||
r.scrapUnit != null && String(r.scrapUnit).trim()
|
||||
? String(r.scrapUnit).trim()
|
||||
: null;
|
||||
}
|
||||
|
||||
// Idempotent upsert keyed by reasonId
|
||||
const row = await prisma.reasonEntry.upsert({
|
||||
where: { reasonId },
|
||||
create: {
|
||||
orgId: machine.orgId,
|
||||
machineId: machine.id,
|
||||
reasonId,
|
||||
kind,
|
||||
episodeId,
|
||||
durationSeconds,
|
||||
episodeEndTs,
|
||||
scrapEntryId,
|
||||
scrapQty,
|
||||
scrapUnit,
|
||||
reasonCode,
|
||||
reasonLabel,
|
||||
reasonText,
|
||||
capturedAt,
|
||||
workOrderId,
|
||||
meta,
|
||||
schemaVersion,
|
||||
},
|
||||
update: {
|
||||
kind,
|
||||
episodeId,
|
||||
durationSeconds,
|
||||
episodeEndTs,
|
||||
scrapEntryId,
|
||||
scrapQty,
|
||||
scrapUnit,
|
||||
reasonCode,
|
||||
reasonLabel,
|
||||
reasonText,
|
||||
capturedAt,
|
||||
workOrderId,
|
||||
meta,
|
||||
schemaVersion,
|
||||
},
|
||||
select: { id: true, reasonId: true },
|
||||
});
|
||||
|
||||
return NextResponse.json({ ok: true, id: row.id, reasonId: row.reasonId });
|
||||
}
|
||||
153
app/api/invites/[token]/route.ts
Normal file
153
app/api/invites/[token]/route.ts
Normal file
@@ -0,0 +1,153 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import type { NextRequest } from "next/server";
|
||||
import bcrypt from "bcrypt";
|
||||
import { prisma } from "@/lib/prisma";
|
||||
import { buildSessionCookieOptions, COOKIE_NAME, SESSION_DAYS } from "@/lib/auth/sessionCookie";
|
||||
import { z } from "zod";
|
||||
|
||||
const tokenSchema = z.string().regex(/^[a-f0-9]{48}$/i);
|
||||
const acceptSchema = z.object({
|
||||
name: z.string().trim().min(1).max(80).optional(),
|
||||
password: z.string().min(8).max(256),
|
||||
});
|
||||
|
||||
async function loadInvite(token: string) {
|
||||
return prisma.orgInvite.findFirst({
|
||||
where: {
|
||||
token,
|
||||
revokedAt: null,
|
||||
acceptedAt: null,
|
||||
expiresAt: { gt: new Date() },
|
||||
},
|
||||
include: {
|
||||
org: { select: { id: true, name: true, slug: true } },
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
export async function GET(
|
||||
_req: NextRequest,
|
||||
{ params }: { params: Promise<{ token: string }> }
|
||||
) {
|
||||
const { token } = await params;
|
||||
if (!tokenSchema.safeParse(token).success) {
|
||||
return NextResponse.json({ ok: false, error: "Invalid invite token" }, { status: 400 });
|
||||
}
|
||||
const invite = await loadInvite(token);
|
||||
if (!invite) {
|
||||
return NextResponse.json({ ok: false, error: "Invite not found" }, { status: 404 });
|
||||
}
|
||||
|
||||
return NextResponse.json({
|
||||
ok: true,
|
||||
invite: {
|
||||
email: invite.email,
|
||||
role: invite.role,
|
||||
org: invite.org,
|
||||
expiresAt: invite.expiresAt,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
export async function POST(
|
||||
req: NextRequest,
|
||||
{ params }: { params: Promise<{ token: string }> }
|
||||
) {
|
||||
const { token } = await params;
|
||||
if (!tokenSchema.safeParse(token).success) {
|
||||
return NextResponse.json({ ok: false, error: "Invalid invite token" }, { status: 400 });
|
||||
}
|
||||
const invite = await loadInvite(token);
|
||||
if (!invite) {
|
||||
return NextResponse.json({ ok: false, error: "Invite not found" }, { status: 404 });
|
||||
}
|
||||
|
||||
const body = await req.json().catch(() => ({}));
|
||||
const parsed = acceptSchema.safeParse(body);
|
||||
if (!parsed.success) {
|
||||
return NextResponse.json({ ok: false, error: "Invalid invite payload" }, { status: 400 });
|
||||
}
|
||||
const name = String(parsed.data.name || "").trim();
|
||||
const password = parsed.data.password;
|
||||
|
||||
const existingUser = await prisma.user.findUnique({
|
||||
where: { email: invite.email },
|
||||
});
|
||||
|
||||
if (!existingUser && !name) {
|
||||
return NextResponse.json({ ok: false, error: "Name is required" }, { status: 400 });
|
||||
}
|
||||
|
||||
let userId = existingUser?.id ?? null;
|
||||
if (existingUser) {
|
||||
if (!existingUser.isActive) {
|
||||
return NextResponse.json({ ok: false, error: "User is inactive" }, { status: 403 });
|
||||
}
|
||||
const ok = await bcrypt.compare(password, existingUser.passwordHash);
|
||||
if (!ok) {
|
||||
return NextResponse.json({ ok: false, error: "Invalid credentials" }, { status: 401 });
|
||||
}
|
||||
userId = existingUser.id;
|
||||
} else {
|
||||
const passwordHash = await bcrypt.hash(password, 10);
|
||||
const created = await prisma.user.create({
|
||||
data: {
|
||||
email: invite.email,
|
||||
name,
|
||||
passwordHash,
|
||||
emailVerifiedAt: new Date(),
|
||||
},
|
||||
});
|
||||
userId = created.id;
|
||||
}
|
||||
|
||||
const expiresAt = new Date(Date.now() + SESSION_DAYS * 24 * 60 * 60 * 1000);
|
||||
|
||||
const session = await prisma.$transaction(async (tx) => {
|
||||
if (existingUser && !existingUser.emailVerifiedAt) {
|
||||
await tx.user.update({
|
||||
where: { id: existingUser.id },
|
||||
data: {
|
||||
emailVerifiedAt: new Date(),
|
||||
emailVerificationToken: null,
|
||||
emailVerificationExpiresAt: null,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
await tx.orgUser.upsert({
|
||||
where: {
|
||||
orgId_userId: {
|
||||
orgId: invite.orgId,
|
||||
userId,
|
||||
},
|
||||
},
|
||||
update: {
|
||||
role: invite.role,
|
||||
},
|
||||
create: {
|
||||
orgId: invite.orgId,
|
||||
userId,
|
||||
role: invite.role,
|
||||
},
|
||||
});
|
||||
|
||||
await tx.orgInvite.update({
|
||||
where: { id: invite.id },
|
||||
data: { acceptedAt: new Date() },
|
||||
});
|
||||
|
||||
return tx.session.create({
|
||||
data: {
|
||||
userId,
|
||||
orgId: invite.orgId,
|
||||
expiresAt,
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
const res = NextResponse.json({ ok: true, next: "/machines" });
|
||||
res.cookies.set(COOKIE_NAME, session.id, buildSessionCookieOptions(req));
|
||||
|
||||
return res;
|
||||
}
|
||||
74
app/api/login copy/route.ts
Normal file
74
app/api/login copy/route.ts
Normal file
@@ -0,0 +1,74 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import bcrypt from "bcrypt";
|
||||
import { prisma } from "@/lib/prisma";
|
||||
import { z } from "zod";
|
||||
|
||||
const COOKIE_NAME = "mis_session";
|
||||
const SESSION_DAYS = 7;
|
||||
|
||||
const loginSchema = z.object({
|
||||
email: z.string().trim().min(1).max(254).email(),
|
||||
password: z.string().min(1).max(256),
|
||||
next: z.string().optional(),
|
||||
});
|
||||
|
||||
function safeNextPath(value: unknown) {
|
||||
const raw = String(value ?? "").trim();
|
||||
if (!raw) return "/machines";
|
||||
if (!raw.startsWith("/") || raw.startsWith("//")) return "/machines";
|
||||
return raw;
|
||||
}
|
||||
|
||||
export async function POST(req: Request) {
|
||||
const body = await req.json().catch(() => ({}));
|
||||
const parsed = loginSchema.safeParse(body);
|
||||
if (!parsed.success) {
|
||||
return NextResponse.json({ ok: false, error: "Invalid login payload" }, { status: 400 });
|
||||
}
|
||||
const email = parsed.data.email.toLowerCase();
|
||||
const password = parsed.data.password;
|
||||
const next = safeNextPath(parsed.data.next);
|
||||
|
||||
const user = await prisma.user.findUnique({ where: { email } });
|
||||
if (!user || !user.isActive) {
|
||||
return NextResponse.json({ ok: false, error: "Invalid credentials" }, { status: 401 });
|
||||
}
|
||||
|
||||
const ok = await bcrypt.compare(password, user.passwordHash);
|
||||
if (!ok) {
|
||||
return NextResponse.json({ ok: false, error: "Invalid credentials" }, { status: 401 });
|
||||
}
|
||||
|
||||
// Multiple orgs per user: pick the oldest membership for now
|
||||
const membership = await prisma.orgUser.findFirst({
|
||||
where: { userId: user.id },
|
||||
orderBy: { createdAt: "asc" },
|
||||
});
|
||||
|
||||
if (!membership) {
|
||||
return NextResponse.json({ ok: false, error: "User has no organization" }, { status: 403 });
|
||||
}
|
||||
|
||||
const expiresAt = new Date(Date.now() + SESSION_DAYS * 24 * 60 * 60 * 1000);
|
||||
|
||||
const session = await prisma.session.create({
|
||||
data: {
|
||||
userId: user.id,
|
||||
orgId: membership.orgId,
|
||||
expiresAt,
|
||||
// optional fields you can add later: ip/userAgent
|
||||
},
|
||||
});
|
||||
|
||||
const res = NextResponse.json({ ok: true, next });
|
||||
|
||||
res.cookies.set(COOKIE_NAME, session.id, {
|
||||
httpOnly: true,
|
||||
sameSite: "lax",
|
||||
secure: false, // set true once HTTPS only
|
||||
path: "/",
|
||||
maxAge: SESSION_DAYS * 24 * 60 * 60,
|
||||
});
|
||||
|
||||
return res;
|
||||
}
|
||||
@@ -1,25 +1,41 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import bcrypt from "bcrypt";
|
||||
import { prisma } from "@/lib/prisma";
|
||||
import { buildSessionCookieOptions, COOKIE_NAME, SESSION_DAYS } from "@/lib/auth/sessionCookie";
|
||||
import { z } from "zod";
|
||||
|
||||
const COOKIE_NAME = "mis_session";
|
||||
const SESSION_DAYS = 7;
|
||||
const loginSchema = z.object({
|
||||
email: z.string().trim().min(1).max(254).email(),
|
||||
password: z.string().min(1).max(256),
|
||||
next: z.string().optional(),
|
||||
});
|
||||
|
||||
function safeNextPath(value: unknown) {
|
||||
const raw = String(value ?? "").trim();
|
||||
if (!raw) return "/machines";
|
||||
if (!raw.startsWith("/") || raw.startsWith("//")) return "/machines";
|
||||
return raw;
|
||||
}
|
||||
|
||||
export async function POST(req: Request) {
|
||||
const body = await req.json().catch(() => ({}));
|
||||
const email = String(body.email || "").trim().toLowerCase();
|
||||
const password = String(body.password || "");
|
||||
const next = String(body.next || "/machines");
|
||||
|
||||
if (!email || !password) {
|
||||
return NextResponse.json({ ok: false, error: "Missing email/password" }, { status: 400 });
|
||||
const parsed = loginSchema.safeParse(body);
|
||||
if (!parsed.success) {
|
||||
return NextResponse.json({ ok: false, error: "Invalid login payload" }, { status: 400 });
|
||||
}
|
||||
const email = parsed.data.email.toLowerCase();
|
||||
const password = parsed.data.password;
|
||||
const next = safeNextPath(parsed.data.next);
|
||||
|
||||
const user = await prisma.user.findUnique({ where: { email } });
|
||||
if (!user || !user.isActive) {
|
||||
return NextResponse.json({ ok: false, error: "Invalid credentials" }, { status: 401 });
|
||||
}
|
||||
|
||||
if (!user.emailVerifiedAt) {
|
||||
return NextResponse.json({ ok: false, error: "Email not verified" }, { status: 403 });
|
||||
}
|
||||
|
||||
const ok = await bcrypt.compare(password, user.passwordHash);
|
||||
if (!ok) {
|
||||
return NextResponse.json({ ok: false, error: "Invalid credentials" }, { status: 401 });
|
||||
@@ -47,14 +63,7 @@ export async function POST(req: Request) {
|
||||
});
|
||||
|
||||
const res = NextResponse.json({ ok: true, next });
|
||||
|
||||
res.cookies.set(COOKIE_NAME, session.id, {
|
||||
httpOnly: true,
|
||||
sameSite: "lax",
|
||||
secure: false, // set true once HTTPS only
|
||||
path: "/",
|
||||
maxAge: SESSION_DAYS * 24 * 60 * 60,
|
||||
});
|
||||
res.cookies.set(COOKIE_NAME, session.id, buildSessionCookieOptions(req));
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
21
app/api/logout copy/route.ts
Normal file
21
app/api/logout copy/route.ts
Normal file
@@ -0,0 +1,21 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import { cookies } from "next/headers";
|
||||
import { prisma } from "@/lib/prisma";
|
||||
|
||||
const COOKIE_NAME = "mis_session";
|
||||
|
||||
export async function POST() {
|
||||
const jar = await cookies();
|
||||
const sessionId = jar.get(COOKIE_NAME)?.value;
|
||||
|
||||
if (sessionId) {
|
||||
await prisma.session.updateMany({
|
||||
where: { id: sessionId, revokedAt: null },
|
||||
data: { revokedAt: new Date() },
|
||||
}).catch(() => {});
|
||||
}
|
||||
|
||||
const res = NextResponse.json({ ok: true });
|
||||
res.cookies.set(COOKIE_NAME, "", { path: "/", maxAge: 0 });
|
||||
return res;
|
||||
}
|
||||
@@ -1,183 +1,124 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import type { NextRequest } from "next/server";
|
||||
import { Prisma } from "@prisma/client";
|
||||
import { z } from "zod";
|
||||
import { prisma } from "@/lib/prisma";
|
||||
import { requireSession } from "@/lib/auth/requireSession";
|
||||
import { normalizeEvent } from "@/lib/events/normalizeEvent";
|
||||
import { invalidateMachineAuth } from "@/lib/machineAuthCache";
|
||||
|
||||
function normalizeEvent(row: any) {
|
||||
// -----------------------------
|
||||
// 1) Parse row.data safely
|
||||
// data may be:
|
||||
// - object
|
||||
// - array of objects
|
||||
// - JSON string of either
|
||||
// -----------------------------
|
||||
const raw = row.data;
|
||||
const machineIdSchema = z.string().uuid();
|
||||
|
||||
let parsed: any = raw;
|
||||
if (typeof raw === "string") {
|
||||
try {
|
||||
parsed = JSON.parse(raw);
|
||||
} catch {
|
||||
parsed = raw; // keep as string if not JSON
|
||||
}
|
||||
}
|
||||
const ALLOWED_EVENT_TYPES = new Set([
|
||||
"slow-cycle",
|
||||
"microstop",
|
||||
"macrostop",
|
||||
"offline",
|
||||
"error",
|
||||
"oee-drop",
|
||||
"quality-spike",
|
||||
"performance-degradation",
|
||||
"predictive-oee-decline",
|
||||
"alert-delivery-failed",
|
||||
]);
|
||||
|
||||
// data can be object OR [object]
|
||||
const blob = Array.isArray(parsed) ? parsed[0] : parsed;
|
||||
|
||||
// some payloads nest details under blob.data
|
||||
const inner = blob?.data ?? blob ?? {};
|
||||
|
||||
const normalizeType = (t: any) =>
|
||||
String(t ?? "")
|
||||
.trim()
|
||||
.toLowerCase()
|
||||
.replace(/_/g, "-");
|
||||
|
||||
// -----------------------------
|
||||
// 2) Alias mapping (canonical types)
|
||||
// -----------------------------
|
||||
const ALIAS: Record<string, string> = {
|
||||
// Spanish / synonyms
|
||||
macroparo: "macrostop",
|
||||
"macro-stop": "macrostop",
|
||||
macro_stop: "macrostop",
|
||||
|
||||
microparo: "microstop",
|
||||
"micro-paro": "microstop",
|
||||
micro_stop: "microstop",
|
||||
|
||||
// Node-RED types
|
||||
"production-stopped": "stop", // we'll classify to micro/macro below
|
||||
|
||||
// legacy / generic
|
||||
down: "stop",
|
||||
};
|
||||
|
||||
// -----------------------------
|
||||
// 3) Determine event type from DB or blob
|
||||
// -----------------------------
|
||||
const fromDbType =
|
||||
row.eventType && row.eventType !== "unknown" ? row.eventType : null;
|
||||
|
||||
const fromBlobType =
|
||||
blob?.anomaly_type ??
|
||||
blob?.eventType ??
|
||||
blob?.topic ??
|
||||
inner?.anomaly_type ??
|
||||
inner?.eventType ??
|
||||
null;
|
||||
|
||||
// infer slow-cycle if signature exists
|
||||
const inferredType =
|
||||
fromDbType ??
|
||||
fromBlobType ??
|
||||
((inner?.actual_cycle_time && inner?.theoretical_cycle_time) ||
|
||||
(blob?.actual_cycle_time && blob?.theoretical_cycle_time)
|
||||
? "slow-cycle"
|
||||
: "unknown");
|
||||
|
||||
const eventTypeRaw = normalizeType(inferredType);
|
||||
let eventType = ALIAS[eventTypeRaw] ?? eventTypeRaw;
|
||||
|
||||
// -----------------------------
|
||||
// 4) Optional: classify "stop" into micro/macro based on duration if present
|
||||
// (keeps old rows usable even if they stored production-stopped)
|
||||
// -----------------------------
|
||||
if (eventType === "stop") {
|
||||
const stopSec =
|
||||
(typeof inner?.stoppage_duration_seconds === "number" && inner.stoppage_duration_seconds) ||
|
||||
(typeof blob?.stoppage_duration_seconds === "number" && blob.stoppage_duration_seconds) ||
|
||||
(typeof inner?.stop_duration_seconds === "number" && inner.stop_duration_seconds) ||
|
||||
null;
|
||||
|
||||
// tune these thresholds to match your MES spec
|
||||
const MACROSTOP_SEC = 300; // 5 min
|
||||
eventType = stopSec != null && stopSec >= MACROSTOP_SEC ? "macrostop" : "microstop";
|
||||
}
|
||||
|
||||
// -----------------------------
|
||||
// 5) Severity, title, description, timestamp
|
||||
// -----------------------------
|
||||
const severity =
|
||||
String(
|
||||
(row.severity && row.severity !== "info" ? row.severity : null) ??
|
||||
blob?.severity ??
|
||||
inner?.severity ??
|
||||
"info"
|
||||
)
|
||||
.trim()
|
||||
.toLowerCase();
|
||||
|
||||
const title =
|
||||
String(
|
||||
(row.title && row.title !== "Event" ? row.title : null) ??
|
||||
blob?.title ??
|
||||
inner?.title ??
|
||||
(eventType === "slow-cycle" ? "Slow Cycle Detected" : "Event")
|
||||
).trim();
|
||||
|
||||
const description =
|
||||
row.description ??
|
||||
blob?.description ??
|
||||
inner?.description ??
|
||||
(eventType === "slow-cycle" &&
|
||||
(inner?.actual_cycle_time ?? blob?.actual_cycle_time) &&
|
||||
(inner?.theoretical_cycle_time ?? blob?.theoretical_cycle_time) &&
|
||||
(inner?.delta_percent ?? blob?.delta_percent) != null
|
||||
? `Cycle took ${Number(inner?.actual_cycle_time ?? blob?.actual_cycle_time).toFixed(1)}s (+${Number(inner?.delta_percent ?? blob?.delta_percent)}% vs ${Number(inner?.theoretical_cycle_time ?? blob?.theoretical_cycle_time).toFixed(1)}s objetivo)`
|
||||
: null);
|
||||
|
||||
const ts =
|
||||
row.ts ??
|
||||
(typeof blob?.timestamp === "number" ? new Date(blob.timestamp) : null) ??
|
||||
(typeof inner?.timestamp === "number" ? new Date(inner.timestamp) : null) ??
|
||||
null;
|
||||
|
||||
const workOrderId =
|
||||
row.workOrderId ??
|
||||
blob?.work_order_id ??
|
||||
inner?.work_order_id ??
|
||||
null;
|
||||
|
||||
return {
|
||||
id: row.id,
|
||||
ts,
|
||||
topic: String(row.topic ?? blob?.topic ?? eventType),
|
||||
eventType,
|
||||
severity,
|
||||
title,
|
||||
description,
|
||||
requiresAck: !!row.requiresAck,
|
||||
workOrderId,
|
||||
};
|
||||
function canManageMachines(role?: string | null) {
|
||||
return role === "OWNER" || role === "ADMIN";
|
||||
}
|
||||
|
||||
function isPlainObject(value: unknown): value is Record<string, unknown> {
|
||||
return !!value && typeof value === "object" && !Array.isArray(value);
|
||||
}
|
||||
|
||||
function parseNumber(value: string | null, fallback: number) {
|
||||
if (value == null || value === "") return fallback;
|
||||
const parsed = Number(value);
|
||||
return Number.isFinite(parsed) ? parsed : fallback;
|
||||
}
|
||||
|
||||
type MachineFkReference = {
|
||||
tableName: string;
|
||||
columnName: string;
|
||||
deleteRule: string;
|
||||
};
|
||||
|
||||
export async function GET(
|
||||
_req: NextRequest,
|
||||
{ params }: { params: Promise<{ machineId: string }> }
|
||||
) {
|
||||
function quoteIdent(identifier: string) {
|
||||
return `"${identifier.replace(/"/g, "\"\"")}"`;
|
||||
}
|
||||
|
||||
async function cleanupMachineReferences(machineId: string) {
|
||||
const refs = await prisma.$queryRaw<MachineFkReference[]>`
|
||||
SELECT DISTINCT
|
||||
tc.table_name AS "tableName",
|
||||
kcu.column_name AS "columnName",
|
||||
rc.delete_rule AS "deleteRule"
|
||||
FROM information_schema.table_constraints tc
|
||||
JOIN information_schema.key_column_usage kcu
|
||||
ON tc.constraint_name = kcu.constraint_name
|
||||
AND tc.table_schema = kcu.table_schema
|
||||
JOIN information_schema.referential_constraints rc
|
||||
ON tc.constraint_name = rc.constraint_name
|
||||
AND tc.table_schema = rc.constraint_schema
|
||||
WHERE tc.constraint_type = 'FOREIGN KEY'
|
||||
AND tc.table_schema = 'public'
|
||||
AND rc.unique_constraint_schema = 'public'
|
||||
AND rc.unique_constraint_name IN (
|
||||
SELECT constraint_name
|
||||
FROM information_schema.table_constraints
|
||||
WHERE table_schema = 'public'
|
||||
AND table_name = 'Machine'
|
||||
AND constraint_type IN ('PRIMARY KEY', 'UNIQUE')
|
||||
)
|
||||
`;
|
||||
|
||||
for (const ref of refs) {
|
||||
if (ref.tableName === "Machine") continue;
|
||||
const table = quoteIdent(ref.tableName);
|
||||
const column = quoteIdent(ref.columnName);
|
||||
const rule = String(ref.deleteRule ?? "").toUpperCase();
|
||||
|
||||
if (rule === "CASCADE") continue;
|
||||
|
||||
if (rule === "SET NULL") {
|
||||
await prisma.$executeRawUnsafe(`UPDATE ${table} SET ${column} = NULL WHERE ${column} = $1`, machineId);
|
||||
continue;
|
||||
}
|
||||
|
||||
await prisma.$executeRawUnsafe(`DELETE FROM ${table} WHERE ${column} = $1`, machineId);
|
||||
}
|
||||
}
|
||||
|
||||
export async function GET(req: NextRequest, { params }: { params: Promise<{ machineId: string }> }) {
|
||||
const session = await requireSession();
|
||||
if (!session) {
|
||||
return NextResponse.json({ ok: false, error: "Unauthorized" }, { status: 401 });
|
||||
}
|
||||
|
||||
const { machineId } = await params;
|
||||
if (!machineIdSchema.safeParse(machineId).success) {
|
||||
return NextResponse.json({ ok: false, error: "Invalid machine id" }, { status: 400 });
|
||||
}
|
||||
|
||||
const machine = await prisma.machine.findFirst({
|
||||
const url = new URL(req.url);
|
||||
const windowSec = Math.max(0, parseNumber(url.searchParams.get("windowSec"), 3600));
|
||||
const eventsWindowSec = Math.max(0, parseNumber(url.searchParams.get("eventsWindowSec"), 21600));
|
||||
const eventsMode = url.searchParams.get("events") ?? "critical";
|
||||
const eventsOnly = url.searchParams.get("eventsOnly") === "1";
|
||||
|
||||
const [machineRow, orgSettings, machineSettings] = await Promise.all([
|
||||
prisma.machine.findFirst({
|
||||
where: { id: machineId, orgId: session.orgId },
|
||||
select: {
|
||||
id: true,
|
||||
name: true,
|
||||
code: true,
|
||||
location: true,
|
||||
createdAt: true,
|
||||
updatedAt: true,
|
||||
heartbeats: {
|
||||
orderBy: { ts: "desc" },
|
||||
orderBy: { tsServer: "desc" },
|
||||
take: 1,
|
||||
select: { ts: true, status: true, message: true, ip: true, fwVersion: true },
|
||||
select: { ts: true, tsServer: true, status: true, message: true, ip: true, fwVersion: true },
|
||||
},
|
||||
kpiSnapshots: {
|
||||
orderBy: { ts: "desc" },
|
||||
@@ -197,19 +138,92 @@ export async function GET(
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
}),
|
||||
prisma.orgSettings.findUnique({
|
||||
where: { orgId: session.orgId },
|
||||
select: { stoppageMultiplier: true, macroStoppageMultiplier: true },
|
||||
}),
|
||||
prisma.machineSettings.findUnique({
|
||||
where: { machineId },
|
||||
select: { overridesJson: true },
|
||||
}),
|
||||
]);
|
||||
|
||||
if (!machine) {
|
||||
return NextResponse.json({ ok: false, error: "Not found" }, { status: 404 });
|
||||
if (!machineRow) {
|
||||
return NextResponse.json({ ok: false, error: "Machine not found" }, { status: 404 });
|
||||
}
|
||||
|
||||
const rawEvents = await prisma.machineEvent.findMany({
|
||||
const overrides = isPlainObject(machineSettings?.overridesJson) ? machineSettings?.overridesJson : {};
|
||||
const thresholdsOverride = isPlainObject(overrides.thresholds) ? overrides.thresholds : {};
|
||||
const stoppageMultiplier =
|
||||
typeof thresholdsOverride.stoppageMultiplier === "number"
|
||||
? thresholdsOverride.stoppageMultiplier
|
||||
: Number(orgSettings?.stoppageMultiplier ?? 1.5);
|
||||
const macroStoppageMultiplier =
|
||||
typeof thresholdsOverride.macroStoppageMultiplier === "number"
|
||||
? thresholdsOverride.macroStoppageMultiplier
|
||||
: Number(orgSettings?.macroStoppageMultiplier ?? 5);
|
||||
|
||||
const thresholds = {
|
||||
stoppageMultiplier,
|
||||
macroStoppageMultiplier,
|
||||
};
|
||||
|
||||
const machine = {
|
||||
...machineRow,
|
||||
effectiveCycleTime: null,
|
||||
latestHeartbeat: machineRow.heartbeats[0] ?? null,
|
||||
latestKpi: machineRow.kpiSnapshots[0] ?? null,
|
||||
heartbeats: undefined,
|
||||
kpiSnapshots: undefined,
|
||||
};
|
||||
|
||||
const cycles = eventsOnly
|
||||
? []
|
||||
: await prisma.machineCycle.findMany({
|
||||
where: {
|
||||
orgId: session.orgId,
|
||||
machineId,
|
||||
ts: { gte: new Date(Date.now() - windowSec * 1000) },
|
||||
},
|
||||
orderBy: { ts: "asc" },
|
||||
select: {
|
||||
ts: true,
|
||||
tsServer: true,
|
||||
cycleCount: true,
|
||||
actualCycleTime: true,
|
||||
theoreticalCycleTime: true,
|
||||
workOrderId: true,
|
||||
sku: true,
|
||||
},
|
||||
});
|
||||
|
||||
const cyclesOut = cycles.map((row) => {
|
||||
const ts = row.tsServer ?? row.ts;
|
||||
return {
|
||||
ts,
|
||||
t: ts.getTime(),
|
||||
cycleCount: row.cycleCount ?? null,
|
||||
actual: row.actualCycleTime,
|
||||
ideal: row.theoreticalCycleTime ?? null,
|
||||
workOrderId: row.workOrderId ?? null,
|
||||
sku: row.sku ?? null,
|
||||
};
|
||||
});
|
||||
|
||||
const eventWindowStart = new Date(Date.now() - eventsWindowSec * 1000);
|
||||
const criticalSeverities = ["critical", "error", "high"];
|
||||
const eventWhereBase = {
|
||||
orgId: session.orgId,
|
||||
machineId,
|
||||
ts: { gte: eventWindowStart },
|
||||
};
|
||||
|
||||
const [rawEvents, eventsCountAll] = await Promise.all([
|
||||
prisma.machineEvent.findMany({
|
||||
where: eventWhereBase,
|
||||
orderBy: { ts: "desc" },
|
||||
take: 100, // pull more, we'll filter after normalization
|
||||
take: eventsOnly ? 300 : 120,
|
||||
select: {
|
||||
id: true,
|
||||
ts: true,
|
||||
@@ -222,104 +236,257 @@ export async function GET(
|
||||
data: true,
|
||||
workOrderId: true,
|
||||
},
|
||||
}),
|
||||
prisma.machineEvent.count({ where: eventWhereBase }),
|
||||
]);
|
||||
|
||||
const normalized = rawEvents.map((row) =>
|
||||
normalizeEvent(row, { microMultiplier: stoppageMultiplier, macroMultiplier: macroStoppageMultiplier })
|
||||
);
|
||||
|
||||
const allowed = normalized.filter((event) => ALLOWED_EVENT_TYPES.has(event.eventType));
|
||||
const criticalEventTypes = new Set(["macrostop", "microstop", "slow-cycle", "offline", "error"]);
|
||||
const filtered =
|
||||
eventsMode === "critical"
|
||||
? allowed.filter((event) => {
|
||||
const severity = String(event.severity ?? "").toLowerCase();
|
||||
return (
|
||||
criticalEventTypes.has(event.eventType) ||
|
||||
event.requiresAck === true ||
|
||||
criticalSeverities.includes(severity)
|
||||
);
|
||||
})
|
||||
: allowed;
|
||||
|
||||
const seen = new Set<string>();
|
||||
const deduped = filtered.filter((event) => {
|
||||
const key = `${event.eventType}-${event.ts ?? ""}-${event.title}`;
|
||||
if (seen.has(key)) return false;
|
||||
seen.add(key);
|
||||
return true;
|
||||
});
|
||||
|
||||
const normalized = rawEvents.map(normalizeEvent);
|
||||
|
||||
const ALLOWED_TYPES = new Set([
|
||||
"slow-cycle",
|
||||
"microstop",
|
||||
"macrostop",
|
||||
"oee-drop",
|
||||
"quality-spike",
|
||||
"performance-degradation",
|
||||
"predictive-oee-decline",
|
||||
]);
|
||||
|
||||
const events = normalized
|
||||
.filter((e) => ALLOWED_TYPES.has(e.eventType))
|
||||
// keep slow-cycle even if severity is info, otherwise require warning/critical/error
|
||||
.filter((e) =>
|
||||
["slow-cycle", "microstop", "macrostop"].includes(e.eventType) ||
|
||||
["warning", "critical", "error"].includes(e.severity)
|
||||
)
|
||||
.slice(0, 30);
|
||||
|
||||
|
||||
// ---- cycles window ----
|
||||
const url = new URL(_req.url);
|
||||
const windowSec = Number(url.searchParams.get("windowSec") ?? "10800"); // default 3h
|
||||
|
||||
const latestKpi = machine.kpiSnapshots[0] ?? null;
|
||||
|
||||
// If KPI cycleTime missing, fallback to DB cycles (we fetch 1 first)
|
||||
const latestCycleForIdeal = await prisma.machineCycle.findFirst({
|
||||
where: { orgId: session.orgId, machineId },
|
||||
orderBy: { ts: "desc" },
|
||||
select: { theoreticalCycleTime: true },
|
||||
});
|
||||
|
||||
const effectiveCycleTime =
|
||||
latestKpi?.cycleTime ??
|
||||
latestCycleForIdeal?.theoreticalCycleTime ??
|
||||
null;
|
||||
|
||||
// Estimate how many cycles we need to cover the window.
|
||||
// Add buffer so the chart doesn’t look “tight”.
|
||||
const estCycleSec = Math.max(1, Number(effectiveCycleTime ?? 14));
|
||||
const needed = Math.ceil(windowSec / estCycleSec) + 50;
|
||||
|
||||
// Safety cap to avoid crazy payloads
|
||||
const takeCycles = Math.min(5000, Math.max(200, needed));
|
||||
|
||||
const rawCycles = await prisma.machineCycle.findMany({
|
||||
where: { orgId: session.orgId, machineId },
|
||||
orderBy: { ts: "desc" },
|
||||
take: takeCycles,
|
||||
select: {
|
||||
ts: true,
|
||||
cycleCount: true,
|
||||
actualCycleTime: true,
|
||||
theoreticalCycleTime: true,
|
||||
workOrderId: true,
|
||||
sku: true,
|
||||
},
|
||||
});
|
||||
|
||||
// chart-friendly: oldest -> newest + numeric timestamps
|
||||
const cycles = rawCycles
|
||||
.slice()
|
||||
.reverse()
|
||||
.map((c) => ({
|
||||
ts: c.ts,
|
||||
t: c.ts.getTime(),
|
||||
cycleCount: c.cycleCount ?? null,
|
||||
actual: c.actualCycleTime,
|
||||
ideal: c.theoreticalCycleTime ?? null,
|
||||
workOrderId: c.workOrderId ?? null,
|
||||
sku: c.sku ?? null,
|
||||
}));
|
||||
|
||||
|
||||
|
||||
deduped.sort((a, b) => {
|
||||
const at = a.ts ? a.ts.getTime() : 0;
|
||||
const bt = b.ts ? b.ts.getTime() : 0;
|
||||
return bt - at;
|
||||
});
|
||||
|
||||
return NextResponse.json({
|
||||
ok: true,
|
||||
machine: {
|
||||
id: machine.id,
|
||||
name: machine.name,
|
||||
code: machine.code,
|
||||
location: machine.location,
|
||||
latestHeartbeat: machine.heartbeats[0] ?? null,
|
||||
latestKpi: machine.kpiSnapshots[0] ?? null,
|
||||
effectiveCycleTime
|
||||
|
||||
},
|
||||
events,
|
||||
cycles
|
||||
machine,
|
||||
events: deduped,
|
||||
eventsCountAll,
|
||||
cycles: cyclesOut,
|
||||
thresholds,
|
||||
activeStoppage: null,
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
export async function DELETE(_req: Request, { params }: { params: Promise<{ machineId: string }> }) {
|
||||
const session = await requireSession();
|
||||
if (!session) {
|
||||
return NextResponse.json({ ok: false, error: "Unauthorized" }, { status: 401 });
|
||||
}
|
||||
|
||||
const { machineId } = await params;
|
||||
if (!machineIdSchema.safeParse(machineId).success) {
|
||||
return NextResponse.json({ ok: false, error: "Invalid machine id" }, { status: 400 });
|
||||
}
|
||||
|
||||
const membership = await prisma.orgUser.findUnique({
|
||||
where: {
|
||||
orgId_userId: {
|
||||
orgId: session.orgId,
|
||||
userId: session.userId,
|
||||
},
|
||||
},
|
||||
select: { role: true },
|
||||
});
|
||||
|
||||
if (!canManageMachines(membership?.role)) {
|
||||
return NextResponse.json({ ok: false, error: "Forbidden" }, { status: 403 });
|
||||
}
|
||||
|
||||
for (let attempt = 0; attempt < 3; attempt += 1) {
|
||||
try {
|
||||
if (attempt === 0) {
|
||||
// Revoke credentials first in a committed write so ingest auth fails immediately.
|
||||
const revoked = await prisma.machine.updateMany({
|
||||
where: {
|
||||
id: machineId,
|
||||
orgId: session.orgId,
|
||||
},
|
||||
data: {
|
||||
apiKey: null,
|
||||
},
|
||||
});
|
||||
|
||||
if (revoked.count === 0) {
|
||||
return NextResponse.json({ ok: false, error: "Machine not found" }, { status: 404 });
|
||||
}
|
||||
|
||||
invalidateMachineAuth(machineId);
|
||||
}
|
||||
|
||||
// Avoid long interactive transactions on very large history tables (P2028 timeout).
|
||||
// This sequence is idempotent and safe to retry because apiKey is revoked first.
|
||||
await prisma.machineCycle.deleteMany({
|
||||
where: {
|
||||
machineId,
|
||||
},
|
||||
});
|
||||
|
||||
await prisma.machineHeartbeat.deleteMany({
|
||||
where: {
|
||||
machineId,
|
||||
},
|
||||
});
|
||||
|
||||
await prisma.machineKpiSnapshot.deleteMany({
|
||||
where: {
|
||||
machineId,
|
||||
},
|
||||
});
|
||||
|
||||
await prisma.machineEvent.deleteMany({
|
||||
where: {
|
||||
machineId,
|
||||
},
|
||||
});
|
||||
|
||||
await prisma.machineWorkOrder.deleteMany({
|
||||
where: {
|
||||
machineId,
|
||||
},
|
||||
});
|
||||
|
||||
await prisma.machineSettings.deleteMany({
|
||||
where: {
|
||||
machineId,
|
||||
},
|
||||
});
|
||||
|
||||
await prisma.settingsAudit.deleteMany({
|
||||
where: {
|
||||
machineId,
|
||||
},
|
||||
});
|
||||
|
||||
await prisma.alertNotification.deleteMany({
|
||||
where: {
|
||||
machineId,
|
||||
},
|
||||
});
|
||||
|
||||
await prisma.machineFinancialOverride.deleteMany({
|
||||
where: {
|
||||
machineId,
|
||||
},
|
||||
});
|
||||
|
||||
await prisma.reasonEntry.deleteMany({
|
||||
where: {
|
||||
machineId,
|
||||
},
|
||||
});
|
||||
|
||||
await prisma.downtimeAction.updateMany({
|
||||
where: {
|
||||
machineId,
|
||||
},
|
||||
data: {
|
||||
machineId: null,
|
||||
},
|
||||
});
|
||||
|
||||
const result = await prisma.machine.deleteMany({
|
||||
where: {
|
||||
id: machineId,
|
||||
orgId: session.orgId,
|
||||
},
|
||||
});
|
||||
|
||||
if (result.count === 0) {
|
||||
return NextResponse.json({ ok: false, error: "Machine not found" }, { status: 404 });
|
||||
}
|
||||
|
||||
invalidateMachineAuth(machineId);
|
||||
return NextResponse.json({ ok: true });
|
||||
} catch (err: unknown) {
|
||||
const code = err instanceof Prisma.PrismaClientKnownRequestError ? err.code : undefined;
|
||||
const message = err instanceof Error ? err.message : String(err);
|
||||
console.error("DELETE /api/machines/[machineId] failed", {
|
||||
machineId,
|
||||
orgId: session.orgId,
|
||||
attempt,
|
||||
code,
|
||||
message,
|
||||
});
|
||||
|
||||
if (code === "P2003") {
|
||||
if (attempt < 2) {
|
||||
try {
|
||||
await cleanupMachineReferences(machineId);
|
||||
} catch (cleanupErr: unknown) {
|
||||
const cleanupMessage = cleanupErr instanceof Error ? cleanupErr.message : String(cleanupErr);
|
||||
console.error("DELETE /api/machines/[machineId] cleanup failed", {
|
||||
machineId,
|
||||
orgId: session.orgId,
|
||||
attempt,
|
||||
cleanupMessage,
|
||||
});
|
||||
}
|
||||
await new Promise((resolve) => setTimeout(resolve, (attempt + 1) * 150));
|
||||
continue;
|
||||
}
|
||||
|
||||
return NextResponse.json(
|
||||
{
|
||||
ok: false,
|
||||
error: "Machine has dependent records and could not be removed",
|
||||
code,
|
||||
},
|
||||
{ status: 409 }
|
||||
);
|
||||
}
|
||||
|
||||
if (code === "P2022") {
|
||||
return NextResponse.json(
|
||||
{
|
||||
ok: false,
|
||||
error: "Server schema is out of date for machine delete",
|
||||
code,
|
||||
},
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
|
||||
if (code === "P2028") {
|
||||
return NextResponse.json(
|
||||
{
|
||||
ok: false,
|
||||
error: "Delete timed out while removing machine history",
|
||||
code,
|
||||
},
|
||||
{ status: 503 }
|
||||
);
|
||||
}
|
||||
|
||||
if (code) {
|
||||
return NextResponse.json(
|
||||
{
|
||||
ok: false,
|
||||
error: "Delete failed due to database error",
|
||||
code,
|
||||
},
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
|
||||
return NextResponse.json({ ok: false, error: "Delete failed" }, { status: 500 });
|
||||
}
|
||||
}
|
||||
|
||||
return NextResponse.json({ ok: false, error: "Delete failed", code: "DELETE_RETRY_EXHAUSTED" }, { status: 500 });
|
||||
}
|
||||
|
||||
64
app/api/machines/pair/route.ts
Normal file
64
app/api/machines/pair/route.ts
Normal file
@@ -0,0 +1,64 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import { randomBytes } from "crypto";
|
||||
import { prisma } from "@/lib/prisma";
|
||||
import { getBaseUrl } from "@/lib/appUrl";
|
||||
import { normalizePairingCode } from "@/lib/pairingCode";
|
||||
import { z } from "zod";
|
||||
|
||||
const pairSchema = z.object({
|
||||
code: z.string().trim().max(16).optional(),
|
||||
pairingCode: z.string().trim().max(16).optional(),
|
||||
});
|
||||
|
||||
export async function POST(req: Request) {
|
||||
const body = await req.json().catch(() => ({}));
|
||||
const parsed = pairSchema.safeParse(body);
|
||||
if (!parsed.success) {
|
||||
return NextResponse.json({ ok: false, error: "Invalid pairing payload" }, { status: 400 });
|
||||
}
|
||||
const rawCode = String(parsed.data.code || parsed.data.pairingCode || "").trim();
|
||||
const code = normalizePairingCode(rawCode);
|
||||
|
||||
if (!code || code.length !== 5) {
|
||||
return NextResponse.json({ ok: false, error: "Invalid pairing code" }, { status: 400 });
|
||||
}
|
||||
|
||||
const now = new Date();
|
||||
|
||||
const machine = await prisma.machine.findFirst({
|
||||
where: {
|
||||
pairingCode: code,
|
||||
pairingCodeUsedAt: null,
|
||||
pairingCodeExpiresAt: { gt: now },
|
||||
},
|
||||
select: { id: true, orgId: true, apiKey: true },
|
||||
});
|
||||
|
||||
if (!machine) {
|
||||
return NextResponse.json({ ok: false, error: "Pairing code not found or expired" }, { status: 404 });
|
||||
}
|
||||
|
||||
let apiKey = machine.apiKey;
|
||||
if (!apiKey) {
|
||||
apiKey = randomBytes(24).toString("hex");
|
||||
}
|
||||
|
||||
await prisma.machine.update({
|
||||
where: { id: machine.id },
|
||||
data: {
|
||||
apiKey,
|
||||
pairingCode: null,
|
||||
pairingCodeExpiresAt: null,
|
||||
pairingCodeUsedAt: now,
|
||||
},
|
||||
});
|
||||
|
||||
return NextResponse.json({
|
||||
ok: true,
|
||||
config: {
|
||||
cloudBaseUrl: getBaseUrl(req),
|
||||
machineId: machine.id,
|
||||
apiKey,
|
||||
},
|
||||
});
|
||||
}
|
||||
@@ -1,47 +1,161 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import { randomBytes } from "crypto";
|
||||
import { prisma } from "@/lib/prisma";
|
||||
import { cookies } from "next/headers";
|
||||
import { generatePairingCode } from "@/lib/pairingCode";
|
||||
import { z } from "zod";
|
||||
import { logLine } from "@/lib/logger";
|
||||
import { elapsedMs, formatServerTiming, nowMs, PERF_LOGS_ENABLED } from "@/lib/perf/serverTiming";
|
||||
import { requireSession } from "@/lib/auth/requireSession";
|
||||
import {
|
||||
fetchLatestHeartbeats,
|
||||
fetchLatestKpis,
|
||||
fetchMachineBase,
|
||||
mergeMachineOverviewRows,
|
||||
} from "@/lib/machines/withLatest";
|
||||
|
||||
const COOKIE_NAME = "mis_session";
|
||||
let machinesColdStart = true;
|
||||
|
||||
async function requireSession() {
|
||||
const sessionId = (await cookies()).get(COOKIE_NAME)?.value;
|
||||
if (!sessionId) return null;
|
||||
|
||||
return prisma.session.findFirst({
|
||||
where: { id: sessionId, revokedAt: null, expiresAt: { gt: new Date() } },
|
||||
include: { org: true, user: true },
|
||||
});
|
||||
function getColdStartInfo() {
|
||||
const coldStart = machinesColdStart;
|
||||
machinesColdStart = false;
|
||||
return { coldStart, uptimeMs: Math.round(process.uptime() * 1000) };
|
||||
}
|
||||
|
||||
export async function GET() {
|
||||
const createMachineSchema = z.object({
|
||||
name: z.string().trim().min(1).max(80),
|
||||
code: z.string().trim().max(40).optional(),
|
||||
location: z.string().trim().max(80).optional(),
|
||||
});
|
||||
|
||||
export async function GET(req: Request) {
|
||||
const perfEnabled = PERF_LOGS_ENABLED;
|
||||
const totalStart = nowMs();
|
||||
const timings: Record<string, number> = {};
|
||||
const { coldStart, uptimeMs } = getColdStartInfo();
|
||||
const url = new URL(req.url);
|
||||
const includeKpi = url.searchParams.get("includeKpi") === "1";
|
||||
|
||||
const authStart = nowMs();
|
||||
const session = await requireSession();
|
||||
if (perfEnabled) timings.auth = elapsedMs(authStart);
|
||||
if (!session) return NextResponse.json({ ok: false, error: "Unauthorized" }, { status: 401 });
|
||||
|
||||
const preQueryStart = nowMs();
|
||||
const machinesStart = nowMs();
|
||||
if (perfEnabled) timings.preQuery = elapsedMs(preQueryStart);
|
||||
const machines = await fetchMachineBase(session.orgId);
|
||||
if (perfEnabled) timings.machinesQuery = elapsedMs(machinesStart);
|
||||
|
||||
const heartbeatStart = nowMs();
|
||||
const machineIds = machines.map((machine) => machine.id);
|
||||
const heartbeats = await fetchLatestHeartbeats(session.orgId, machineIds);
|
||||
if (perfEnabled) timings.heartbeatsQuery = elapsedMs(heartbeatStart);
|
||||
|
||||
let kpis: Awaited<ReturnType<typeof fetchLatestKpis>> = [];
|
||||
if (includeKpi) {
|
||||
const kpiStart = nowMs();
|
||||
kpis = await fetchLatestKpis(session.orgId, machineIds);
|
||||
if (perfEnabled) timings.kpiQuery = elapsedMs(kpiStart);
|
||||
}
|
||||
|
||||
const postQueryStart = nowMs();
|
||||
|
||||
// flatten latest heartbeat for UI convenience
|
||||
const out = mergeMachineOverviewRows({
|
||||
machines,
|
||||
heartbeats,
|
||||
kpis,
|
||||
includeKpi,
|
||||
});
|
||||
|
||||
const payload = { ok: true, machines: out };
|
||||
|
||||
const responseHeaders = new Headers();
|
||||
if (perfEnabled) {
|
||||
timings.postQuery = elapsedMs(postQueryStart);
|
||||
timings.total = elapsedMs(totalStart);
|
||||
responseHeaders.set("Server-Timing", formatServerTiming(timings));
|
||||
const payloadBytes = Buffer.byteLength(JSON.stringify(payload));
|
||||
logLine("perf.machines.api", {
|
||||
orgId: session.orgId,
|
||||
coldStart,
|
||||
uptimeMs,
|
||||
timings,
|
||||
counts: { machines: out.length },
|
||||
payloadBytes,
|
||||
});
|
||||
}
|
||||
|
||||
return NextResponse.json(payload, { headers: responseHeaders });
|
||||
}
|
||||
|
||||
export async function POST(req: Request) {
|
||||
const session = await requireSession();
|
||||
if (!session) return NextResponse.json({ ok: false, error: "Unauthorized" }, { status: 401 });
|
||||
|
||||
const machines = await prisma.machine.findMany({
|
||||
where: { orgId: session.orgId },
|
||||
orderBy: { createdAt: "desc" },
|
||||
const body = await req.json().catch(() => ({}));
|
||||
const parsed = createMachineSchema.safeParse(body);
|
||||
if (!parsed.success) {
|
||||
return NextResponse.json({ ok: false, error: "Invalid machine payload" }, { status: 400 });
|
||||
}
|
||||
|
||||
const name = parsed.data.name;
|
||||
const codeRaw = parsed.data.code ?? "";
|
||||
const locationRaw = parsed.data.location ?? "";
|
||||
|
||||
const existing = await prisma.machine.findFirst({
|
||||
where: { orgId: session.orgId, name },
|
||||
select: { id: true },
|
||||
});
|
||||
|
||||
if (existing) {
|
||||
return NextResponse.json({ ok: false, error: "Machine name already exists" }, { status: 409 });
|
||||
}
|
||||
|
||||
const apiKey = randomBytes(24).toString("hex");
|
||||
const pairingExpiresAt = new Date(Date.now() + 24 * 60 * 60 * 1000);
|
||||
|
||||
let machine = null as null | {
|
||||
id: string;
|
||||
name: string;
|
||||
code?: string | null;
|
||||
location?: string | null;
|
||||
pairingCode?: string | null;
|
||||
pairingCodeExpiresAt?: Date | null;
|
||||
};
|
||||
|
||||
for (let attempt = 0; attempt < 5; attempt += 1) {
|
||||
const pairingCode = generatePairingCode();
|
||||
try {
|
||||
machine = await prisma.machine.create({
|
||||
data: {
|
||||
orgId: session.orgId,
|
||||
name,
|
||||
code: codeRaw || null,
|
||||
location: locationRaw || null,
|
||||
apiKey,
|
||||
pairingCode,
|
||||
pairingCodeExpiresAt: pairingExpiresAt,
|
||||
},
|
||||
select: {
|
||||
id: true,
|
||||
name: true,
|
||||
code: true,
|
||||
location: true,
|
||||
createdAt: true,
|
||||
updatedAt: true,
|
||||
heartbeats: {
|
||||
orderBy: { ts: "desc" },
|
||||
take: 1,
|
||||
select: { ts: true, status: true, message: true, ip: true, fwVersion: true },
|
||||
},
|
||||
pairingCode: true,
|
||||
pairingCodeExpiresAt: true,
|
||||
},
|
||||
});
|
||||
break;
|
||||
} catch (err: unknown) {
|
||||
const code = typeof err === "object" && err !== null ? (err as { code?: string }).code : undefined;
|
||||
if (code !== "P2002") throw err;
|
||||
}
|
||||
}
|
||||
|
||||
// flatten latest heartbeat for UI convenience
|
||||
const out = machines.map((m) => ({
|
||||
...m,
|
||||
latestHeartbeat: m.heartbeats[0] ?? null,
|
||||
heartbeats: undefined,
|
||||
}));
|
||||
if (!machine?.pairingCode) {
|
||||
return NextResponse.json({ ok: false, error: "Failed to generate pairing code" }, { status: 500 });
|
||||
}
|
||||
|
||||
return NextResponse.json({ ok: true, machines: out });
|
||||
return NextResponse.json({ ok: true, machine });
|
||||
}
|
||||
|
||||
@@ -2,13 +2,19 @@ import { NextResponse } from "next/server";
|
||||
import { requireSession } from "@/lib/auth/requireSession";
|
||||
import { prisma } from "@/lib/prisma";
|
||||
|
||||
|
||||
|
||||
export async function GET() {
|
||||
try {
|
||||
const { userId, orgId } = await requireSession();
|
||||
const session = await requireSession();
|
||||
if (!session) {
|
||||
return NextResponse.json({ ok: false }, { status: 401 });
|
||||
}
|
||||
const { userId, orgId } = session;
|
||||
|
||||
const user = await prisma.user.findUnique({
|
||||
where: { id: userId },
|
||||
select: { id: true, email: true, name: true },
|
||||
select: { id: true, email: true, name: true, phone: true },
|
||||
});
|
||||
|
||||
const org = await prisma.org.findUnique({
|
||||
@@ -16,7 +22,12 @@ export async function GET() {
|
||||
select: { id: true, name: true, slug: true },
|
||||
});
|
||||
|
||||
return NextResponse.json({ ok: true, user, org });
|
||||
const membership = await prisma.orgUser.findUnique({
|
||||
where: { orgId_userId: { orgId, userId } },
|
||||
select: { role: true },
|
||||
});
|
||||
|
||||
return NextResponse.json({ ok: true, user, org, membership });
|
||||
} catch {
|
||||
return NextResponse.json({ ok: false }, { status: 401 });
|
||||
}
|
||||
|
||||
57
app/api/org/invites/[inviteId]/route.ts
Normal file
57
app/api/org/invites/[inviteId]/route.ts
Normal file
@@ -0,0 +1,57 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import type { NextRequest } from "next/server";
|
||||
import { prisma } from "@/lib/prisma";
|
||||
import { requireSession } from "@/lib/auth/requireSession";
|
||||
import { z } from "zod";
|
||||
|
||||
function canManageMembers(role?: string | null) {
|
||||
return role === "OWNER" || role === "ADMIN";
|
||||
}
|
||||
|
||||
const inviteIdSchema = z.string().uuid();
|
||||
|
||||
export async function DELETE(
|
||||
_req: NextRequest,
|
||||
{ params }: { params: Promise<{ inviteId: string }> }
|
||||
) {
|
||||
try {
|
||||
const session = await requireSession();
|
||||
if (!session) {
|
||||
return NextResponse.json({ ok: false, error: "Unauthorized" }, { status: 401 });
|
||||
}
|
||||
const { inviteId } = await params;
|
||||
if (!inviteIdSchema.safeParse(inviteId).success) {
|
||||
return NextResponse.json({ ok: false, error: "Invalid invite id" }, { status: 400 });
|
||||
}
|
||||
|
||||
const membership = await prisma.orgUser.findUnique({
|
||||
where: {
|
||||
orgId_userId: {
|
||||
orgId: session.orgId,
|
||||
userId: session.userId,
|
||||
},
|
||||
},
|
||||
select: { role: true },
|
||||
});
|
||||
|
||||
if (!canManageMembers(membership?.role)) {
|
||||
return NextResponse.json({ ok: false, error: "Forbidden" }, { status: 403 });
|
||||
}
|
||||
|
||||
await prisma.orgInvite.updateMany({
|
||||
where: {
|
||||
id: inviteId,
|
||||
orgId: session.orgId,
|
||||
acceptedAt: null,
|
||||
revokedAt: null,
|
||||
},
|
||||
data: {
|
||||
revokedAt: new Date(),
|
||||
},
|
||||
});
|
||||
|
||||
return NextResponse.json({ ok: true });
|
||||
} catch {
|
||||
return NextResponse.json({ ok: false, error: "Unauthorized" }, { status: 401 });
|
||||
}
|
||||
}
|
||||
197
app/api/org/members/route.ts
Normal file
197
app/api/org/members/route.ts
Normal file
@@ -0,0 +1,197 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import { randomBytes } from "crypto";
|
||||
import { prisma } from "@/lib/prisma";
|
||||
import { requireSession } from "@/lib/auth/requireSession";
|
||||
import { buildInviteEmail, sendEmail } from "@/lib/email";
|
||||
import { getBaseUrl } from "@/lib/appUrl";
|
||||
import { z } from "zod";
|
||||
|
||||
const INVITE_DAYS = 7;
|
||||
const ROLES = new Set(["OWNER", "ADMIN", "MEMBER"]);
|
||||
const inviteSchema = z.object({
|
||||
email: z.string().trim().min(1).max(254).email(),
|
||||
role: z.string().trim().toUpperCase().optional(),
|
||||
});
|
||||
|
||||
function canManageMembers(role?: string | null) {
|
||||
return role === "OWNER" || role === "ADMIN";
|
||||
}
|
||||
|
||||
export async function GET() {
|
||||
try {
|
||||
|
||||
const session = await requireSession();
|
||||
if (!session) {
|
||||
return NextResponse.json({ ok: false, error: "Unauthorized" }, { status: 401 });
|
||||
}
|
||||
|
||||
const [org, members, invites] = await prisma.$transaction([
|
||||
prisma.org.findUnique({
|
||||
where: { id: session.orgId },
|
||||
select: { id: true, name: true, slug: true },
|
||||
}),
|
||||
prisma.orgUser.findMany({
|
||||
where: { orgId: session.orgId },
|
||||
orderBy: { createdAt: "asc" },
|
||||
include: {
|
||||
user: { select: { id: true, email: true, name: true, isActive: true, createdAt: true } },
|
||||
},
|
||||
}),
|
||||
prisma.orgInvite.findMany({
|
||||
where: {
|
||||
orgId: session.orgId,
|
||||
revokedAt: null,
|
||||
acceptedAt: null,
|
||||
expiresAt: { gt: new Date() },
|
||||
},
|
||||
orderBy: { createdAt: "desc" },
|
||||
select: {
|
||||
id: true,
|
||||
email: true,
|
||||
role: true,
|
||||
token: true,
|
||||
createdAt: true,
|
||||
expiresAt: true,
|
||||
},
|
||||
}),
|
||||
]);
|
||||
|
||||
const mappedMembers = members.map((m) => ({
|
||||
id: m.user.id,
|
||||
membershipId: m.id,
|
||||
email: m.user.email,
|
||||
name: m.user.name,
|
||||
role: m.role,
|
||||
isActive: m.user.isActive,
|
||||
joinedAt: m.createdAt,
|
||||
}));
|
||||
|
||||
return NextResponse.json({
|
||||
ok: true,
|
||||
org,
|
||||
members: mappedMembers,
|
||||
invites,
|
||||
});
|
||||
} catch {
|
||||
return NextResponse.json({ ok: false, error: "Unauthorized" }, { status: 401 });
|
||||
}
|
||||
}
|
||||
|
||||
export async function POST(req: Request) {
|
||||
try {
|
||||
const session = await requireSession();
|
||||
if (!session) {
|
||||
return NextResponse.json({ ok: false, error: "Unauthorized" }, { status: 401 });
|
||||
}
|
||||
const membership = await prisma.orgUser.findUnique({
|
||||
where: {
|
||||
orgId_userId: {
|
||||
orgId: session.orgId,
|
||||
userId: session.userId,
|
||||
},
|
||||
},
|
||||
select: { role: true },
|
||||
});
|
||||
|
||||
if (!canManageMembers(membership?.role)) {
|
||||
return NextResponse.json({ ok: false, error: "Forbidden" }, { status: 403 });
|
||||
}
|
||||
|
||||
const body = await req.json().catch(() => ({}));
|
||||
const parsed = inviteSchema.safeParse(body);
|
||||
if (!parsed.success) {
|
||||
return NextResponse.json({ ok: false, error: "Invalid invite payload" }, { status: 400 });
|
||||
}
|
||||
const email = parsed.data.email.toLowerCase();
|
||||
const role = String(parsed.data.role || "MEMBER").toUpperCase();
|
||||
|
||||
if (!ROLES.has(role)) {
|
||||
return NextResponse.json({ ok: false, error: "Invalid role" }, { status: 400 });
|
||||
}
|
||||
|
||||
const existingUser = await prisma.user.findUnique({ where: { email } });
|
||||
if (existingUser) {
|
||||
const existingMembership = await prisma.orgUser.findUnique({
|
||||
where: {
|
||||
orgId_userId: {
|
||||
orgId: session.orgId,
|
||||
userId: existingUser.id,
|
||||
},
|
||||
},
|
||||
});
|
||||
if (existingMembership) {
|
||||
return NextResponse.json({ ok: false, error: "User already in org" }, { status: 409 });
|
||||
}
|
||||
}
|
||||
|
||||
const existingInvite = await prisma.orgInvite.findFirst({
|
||||
where: {
|
||||
orgId: session.orgId,
|
||||
email,
|
||||
acceptedAt: null,
|
||||
revokedAt: null,
|
||||
expiresAt: { gt: new Date() },
|
||||
},
|
||||
orderBy: { createdAt: "desc" },
|
||||
});
|
||||
|
||||
if (existingInvite) {
|
||||
return NextResponse.json({ ok: true, invite: existingInvite });
|
||||
}
|
||||
|
||||
let invite = null;
|
||||
for (let i = 0; i < 3; i += 1) {
|
||||
const token = randomBytes(24).toString("hex");
|
||||
try {
|
||||
invite = await prisma.orgInvite.create({
|
||||
data: {
|
||||
orgId: session.orgId,
|
||||
email,
|
||||
role,
|
||||
token,
|
||||
invitedBy: session.userId,
|
||||
expiresAt: new Date(Date.now() + INVITE_DAYS * 24 * 60 * 60 * 1000),
|
||||
},
|
||||
});
|
||||
break;
|
||||
} catch (err: unknown) {
|
||||
const code = typeof err === "object" && err !== null ? (err as { code?: string }).code : undefined;
|
||||
if (code !== "P2002") throw err;
|
||||
}
|
||||
}
|
||||
|
||||
if (!invite) {
|
||||
return NextResponse.json({ ok: false, error: "Failed to create invite" }, { status: 500 });
|
||||
}
|
||||
|
||||
let emailSent = true;
|
||||
let emailError: string | null = null;
|
||||
try {
|
||||
const org = await prisma.org.findUnique({
|
||||
where: { id: session.orgId },
|
||||
select: { name: true },
|
||||
});
|
||||
const baseUrl = getBaseUrl(req);
|
||||
const inviteUrl = `${baseUrl}/invite/${invite.token}`;
|
||||
const appName = "MIS Control Tower";
|
||||
const content = buildInviteEmail({
|
||||
appName,
|
||||
orgName: org?.name || "your organization",
|
||||
inviteUrl,
|
||||
});
|
||||
await sendEmail({
|
||||
to: invite.email,
|
||||
subject: content.subject,
|
||||
text: content.text,
|
||||
html: content.html,
|
||||
});
|
||||
} catch (err: unknown) {
|
||||
emailSent = false;
|
||||
emailError = err instanceof Error ? err.message : "Failed to send invite email";
|
||||
}
|
||||
|
||||
return NextResponse.json({ ok: true, invite, emailSent, emailError });
|
||||
} catch {
|
||||
return NextResponse.json({ ok: false, error: "Unauthorized" }, { status: 401 });
|
||||
}
|
||||
}
|
||||
172
app/api/overview/route.ts
Normal file
172
app/api/overview/route.ts
Normal file
@@ -0,0 +1,172 @@
|
||||
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";
|
||||
import { getOverviewSummary } from "@/lib/overview/getOverviewSummary";
|
||||
import { logLine } from "@/lib/logger";
|
||||
import { elapsedMs, formatServerTiming, nowMs, PERF_LOGS_ENABLED } from "@/lib/perf/serverTiming";
|
||||
|
||||
let overviewColdStart = true;
|
||||
|
||||
function getColdStartInfo() {
|
||||
const coldStart = overviewColdStart;
|
||||
overviewColdStart = false;
|
||||
return { coldStart, uptimeMs: Math.round(process.uptime() * 1000) };
|
||||
}
|
||||
|
||||
function toMs(value?: Date | null) {
|
||||
return value ? value.getTime() : 0;
|
||||
}
|
||||
|
||||
export async function GET(req: NextRequest) {
|
||||
const perfEnabled = PERF_LOGS_ENABLED;
|
||||
const totalStart = nowMs();
|
||||
const timings: Record<string, number> = {};
|
||||
const { coldStart, uptimeMs } = getColdStartInfo();
|
||||
|
||||
const authStart = nowMs();
|
||||
const session = await requireSession();
|
||||
if (perfEnabled) timings.auth = elapsedMs(authStart);
|
||||
if (!session) {
|
||||
return NextResponse.json({ ok: false, error: "Unauthorized" }, { status: 401 });
|
||||
}
|
||||
|
||||
const url = new URL(req.url);
|
||||
const detail = url.searchParams.get("detail") === "1";
|
||||
|
||||
if (!detail) {
|
||||
const summaryStart = nowMs();
|
||||
const { machines: machineRows } = await getOverviewSummary({ orgId: session.orgId });
|
||||
if (perfEnabled) timings.summary = elapsedMs(summaryStart);
|
||||
|
||||
const payload = { ok: true, machines: machineRows, events: [] };
|
||||
const responseHeaders = new Headers();
|
||||
if (perfEnabled) {
|
||||
timings.total = elapsedMs(totalStart);
|
||||
responseHeaders.set("Server-Timing", formatServerTiming(timings));
|
||||
const payloadBytes = Buffer.byteLength(JSON.stringify(payload));
|
||||
logLine("perf.overview.api", {
|
||||
orgId: session.orgId,
|
||||
detail: false,
|
||||
coldStart,
|
||||
uptimeMs,
|
||||
timings,
|
||||
counts: { machines: machineRows.length, events: 0 },
|
||||
payloadBytes,
|
||||
});
|
||||
}
|
||||
|
||||
return NextResponse.json(payload, { headers: responseHeaders });
|
||||
}
|
||||
|
||||
const preQueryStart = nowMs();
|
||||
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;
|
||||
if (perfEnabled) timings.preQuery = elapsedMs(preQueryStart);
|
||||
|
||||
const aggStart = nowMs();
|
||||
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 },
|
||||
}),
|
||||
]);
|
||||
if (perfEnabled) timings.agg = elapsedMs(aggStart);
|
||||
|
||||
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 dataStart = nowMs();
|
||||
const { machines: machineRows, events } = await getOverviewData({
|
||||
orgId: session.orgId,
|
||||
eventsMode,
|
||||
eventsWindowSec,
|
||||
eventMachines,
|
||||
orgSettings,
|
||||
});
|
||||
if (perfEnabled) timings.data = elapsedMs(dataStart);
|
||||
|
||||
const postQueryStart = nowMs();
|
||||
|
||||
const payload = { ok: true, machines: machineRows, events };
|
||||
if (perfEnabled) {
|
||||
timings.postQuery = elapsedMs(postQueryStart);
|
||||
timings.total = elapsedMs(totalStart);
|
||||
responseHeaders.set("Server-Timing", formatServerTiming(timings));
|
||||
const payloadBytes = Buffer.byteLength(JSON.stringify(payload));
|
||||
logLine("perf.overview.api", {
|
||||
orgId: session.orgId,
|
||||
detail: true,
|
||||
coldStart,
|
||||
uptimeMs,
|
||||
eventsMode,
|
||||
eventsWindowSec,
|
||||
eventMachines,
|
||||
timings,
|
||||
counts: { machines: machineRows.length, events: events.length },
|
||||
payloadBytes,
|
||||
});
|
||||
}
|
||||
|
||||
return NextResponse.json(payload, { headers: responseHeaders });
|
||||
}
|
||||
48
app/api/reasons/catalog/route.ts
Normal file
48
app/api/reasons/catalog/route.ts
Normal file
@@ -0,0 +1,48 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import { prisma } from "@/lib/prisma";
|
||||
import { requireSession } from "@/lib/auth/requireSession";
|
||||
import {
|
||||
flattenReasonCatalog,
|
||||
loadFallbackReasonCatalog,
|
||||
normalizeReasonCatalog,
|
||||
type ReasonCatalogKind,
|
||||
} from "@/lib/reasonCatalog";
|
||||
|
||||
function asKind(value: string | null): ReasonCatalogKind | null {
|
||||
const kind = String(value ?? "").toLowerCase();
|
||||
if (kind === "downtime" || kind === "scrap") return kind;
|
||||
return null;
|
||||
}
|
||||
|
||||
export async function GET(req: Request) {
|
||||
const session = await requireSession();
|
||||
if (!session) return NextResponse.json({ ok: false, error: "Unauthorized" }, { status: 401 });
|
||||
|
||||
const url = new URL(req.url);
|
||||
const kind = asKind(url.searchParams.get("kind"));
|
||||
if (!kind) {
|
||||
return NextResponse.json({ ok: false, error: "Invalid kind (downtime|scrap)" }, { status: 400 });
|
||||
}
|
||||
|
||||
const orgSettings = await prisma.orgSettings.findUnique({
|
||||
where: { orgId: session.orgId },
|
||||
select: { defaultsJson: true },
|
||||
});
|
||||
const defaultsJson =
|
||||
orgSettings?.defaultsJson && typeof orgSettings.defaultsJson === "object" && !Array.isArray(orgSettings.defaultsJson)
|
||||
? (orgSettings.defaultsJson as Record<string, unknown>)
|
||||
: {};
|
||||
const settingsCatalog = normalizeReasonCatalog(defaultsJson.reasonCatalog ?? defaultsJson.reasonCatalogData);
|
||||
const fallbackCatalog = await loadFallbackReasonCatalog();
|
||||
const catalog = settingsCatalog ?? fallbackCatalog;
|
||||
const rows = flattenReasonCatalog(catalog, kind);
|
||||
|
||||
return NextResponse.json({
|
||||
ok: true,
|
||||
source: settingsCatalog ? "settings" : "fallback",
|
||||
kind,
|
||||
catalogVersion: catalog.version,
|
||||
categories: catalog[kind],
|
||||
rows,
|
||||
});
|
||||
}
|
||||
37
app/api/recap/[machineId]/route.ts
Normal file
37
app/api/recap/[machineId]/route.ts
Normal file
@@ -0,0 +1,37 @@
|
||||
import type { NextRequest } from "next/server";
|
||||
import { NextResponse } from "next/server";
|
||||
import { requireSession } from "@/lib/auth/requireSession";
|
||||
import { getRecapMachineDetailCached, parseRecapDetailRangeInput } from "@/lib/recap/redesign";
|
||||
|
||||
export async function GET(
|
||||
req: NextRequest,
|
||||
{ params }: { params: Promise<{ machineId: string }> }
|
||||
) {
|
||||
const session = await requireSession();
|
||||
if (!session) {
|
||||
return NextResponse.json({ ok: false, error: "Unauthorized" }, { status: 401 });
|
||||
}
|
||||
|
||||
const { machineId } = await params;
|
||||
if (!machineId) {
|
||||
return NextResponse.json({ ok: false, error: "machineId is required" }, { status: 400 });
|
||||
}
|
||||
|
||||
const url = new URL(req.url);
|
||||
const input = parseRecapDetailRangeInput(url.searchParams);
|
||||
const detail = await getRecapMachineDetailCached({
|
||||
orgId: session.orgId,
|
||||
machineId,
|
||||
input,
|
||||
});
|
||||
|
||||
if (!detail) {
|
||||
return NextResponse.json({ ok: false, error: "Machine not found" }, { status: 404 });
|
||||
}
|
||||
|
||||
return NextResponse.json(detail, {
|
||||
headers: {
|
||||
"Cache-Control": "private, max-age=60, stale-while-revalidate=60",
|
||||
},
|
||||
});
|
||||
}
|
||||
42
app/api/recap/[machineId]/timeline/route.ts
Normal file
42
app/api/recap/[machineId]/timeline/route.ts
Normal file
@@ -0,0 +1,42 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import type { NextRequest } from "next/server";
|
||||
import { requireSession } from "@/lib/auth/requireSession";
|
||||
import { prisma } from "@/lib/prisma";
|
||||
import { getRecapTimelineForMachine, parseRecapTimelineRange } from "@/lib/recap/timelineApi";
|
||||
|
||||
function bad(status: number, error: string) {
|
||||
return NextResponse.json({ ok: false, error }, { status });
|
||||
}
|
||||
|
||||
export async function GET(
|
||||
req: NextRequest,
|
||||
{ params }: { params: Promise<{ machineId: string }> }
|
||||
) {
|
||||
const session = await requireSession();
|
||||
if (!session) return bad(401, "Unauthorized");
|
||||
|
||||
const { machineId } = await params;
|
||||
if (!machineId) return bad(400, "machineId is required");
|
||||
|
||||
const machine = await prisma.machine.findFirst({
|
||||
where: { id: machineId, orgId: session.orgId },
|
||||
select: { id: true },
|
||||
});
|
||||
if (!machine) return bad(404, "Machine not found");
|
||||
|
||||
const url = new URL(req.url);
|
||||
const { start, end, maxSegments } = parseRecapTimelineRange(url.searchParams);
|
||||
const response = await getRecapTimelineForMachine({
|
||||
orgId: session.orgId,
|
||||
machineId,
|
||||
start,
|
||||
end,
|
||||
maxSegments,
|
||||
});
|
||||
|
||||
return NextResponse.json(response, {
|
||||
headers: {
|
||||
"Cache-Control": "private, max-age=60, stale-while-revalidate=60",
|
||||
},
|
||||
});
|
||||
}
|
||||
29
app/api/recap/route.ts
Normal file
29
app/api/recap/route.ts
Normal file
@@ -0,0 +1,29 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import type { NextRequest } from "next/server";
|
||||
import { requireSession } from "@/lib/auth/requireSession";
|
||||
import { getRecapDataCached, parseRecapQuery } from "@/lib/recap/getRecapData";
|
||||
|
||||
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 query = parseRecapQuery({
|
||||
machineId: url.searchParams.get("machineId"),
|
||||
start: url.searchParams.get("start"),
|
||||
end: url.searchParams.get("end"),
|
||||
shift: url.searchParams.get("shift"),
|
||||
});
|
||||
|
||||
const recap = await getRecapDataCached({
|
||||
orgId: session.orgId,
|
||||
machineId: query.machineId,
|
||||
start: query.start ?? undefined,
|
||||
end: query.end ?? undefined,
|
||||
shift: query.shift ?? undefined,
|
||||
});
|
||||
|
||||
return NextResponse.json(recap);
|
||||
}
|
||||
21
app/api/recap/summary/route.ts
Normal file
21
app/api/recap/summary/route.ts
Normal file
@@ -0,0 +1,21 @@
|
||||
import type { NextRequest } from "next/server";
|
||||
import { NextResponse } from "next/server";
|
||||
import { requireSession } from "@/lib/auth/requireSession";
|
||||
import { getRecapSummaryCached, parseRecapSummaryHours } from "@/lib/recap/redesign";
|
||||
|
||||
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 hours = parseRecapSummaryHours(url.searchParams.get("hours"));
|
||||
const summary = await getRecapSummaryCached({ orgId: session.orgId, hours });
|
||||
|
||||
return NextResponse.json(summary, {
|
||||
headers: {
|
||||
"Cache-Control": "private, max-age=60, stale-while-revalidate=60",
|
||||
},
|
||||
});
|
||||
}
|
||||
39
app/api/recap/timeline/route.ts
Normal file
39
app/api/recap/timeline/route.ts
Normal file
@@ -0,0 +1,39 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import type { NextRequest } from "next/server";
|
||||
import { requireSession } from "@/lib/auth/requireSession";
|
||||
import { prisma } from "@/lib/prisma";
|
||||
import { getRecapTimelineForMachine, parseRecapTimelineRange } from "@/lib/recap/timelineApi";
|
||||
|
||||
function bad(status: number, error: string) {
|
||||
return NextResponse.json({ ok: false, error }, { status });
|
||||
}
|
||||
|
||||
export async function GET(req: NextRequest) {
|
||||
const session = await requireSession();
|
||||
if (!session) return bad(401, "Unauthorized");
|
||||
|
||||
const url = new URL(req.url);
|
||||
const machineId = url.searchParams.get("machineId");
|
||||
if (!machineId) return bad(400, "machineId is required");
|
||||
|
||||
const machine = await prisma.machine.findFirst({
|
||||
where: { id: machineId, orgId: session.orgId },
|
||||
select: { id: true },
|
||||
});
|
||||
if (!machine) return bad(404, "Machine not found");
|
||||
|
||||
const { start, end, maxSegments } = parseRecapTimelineRange(url.searchParams);
|
||||
const response = await getRecapTimelineForMachine({
|
||||
orgId: session.orgId,
|
||||
machineId,
|
||||
start,
|
||||
end,
|
||||
maxSegments,
|
||||
});
|
||||
|
||||
return NextResponse.json(response, {
|
||||
headers: {
|
||||
"Cache-Control": "private, max-age=60, stale-while-revalidate=60",
|
||||
},
|
||||
});
|
||||
}
|
||||
145
app/api/reports/filters/route.ts
Normal file
145
app/api/reports/filters/route.ts
Normal file
@@ -0,0 +1,145 @@
|
||||
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 { logLine } from "@/lib/logger";
|
||||
import { elapsedMs, formatServerTiming, nowMs, PERF_LOGS_ENABLED } from "@/lib/perf/serverTiming";
|
||||
|
||||
let reportsFiltersColdStart = true;
|
||||
|
||||
function getColdStartInfo() {
|
||||
const coldStart = reportsFiltersColdStart;
|
||||
reportsFiltersColdStart = false;
|
||||
return { coldStart, uptimeMs: Math.round(process.uptime() * 1000) };
|
||||
}
|
||||
|
||||
const RANGE_MS: Record<string, number> = {
|
||||
"24h": 24 * 60 * 60 * 1000,
|
||||
"7d": 7 * 24 * 60 * 60 * 1000,
|
||||
"30d": 30 * 24 * 60 * 60 * 1000,
|
||||
};
|
||||
|
||||
function parseDate(input?: string | null) {
|
||||
if (!input) return null;
|
||||
const n = Number(input);
|
||||
if (!Number.isNaN(n)) return new Date(n);
|
||||
const d = new Date(input);
|
||||
return Number.isNaN(d.getTime()) ? null : d;
|
||||
}
|
||||
|
||||
function pickRange(req: NextRequest) {
|
||||
const url = new URL(req.url);
|
||||
const range = url.searchParams.get("range") ?? "24h";
|
||||
const now = new Date();
|
||||
|
||||
if (range === "custom") {
|
||||
const start = parseDate(url.searchParams.get("start")) ?? new Date(now.getTime() - RANGE_MS["24h"]);
|
||||
const end = parseDate(url.searchParams.get("end")) ?? now;
|
||||
return { start, end };
|
||||
}
|
||||
|
||||
const ms = RANGE_MS[range] ?? RANGE_MS["24h"];
|
||||
return { start: new Date(now.getTime() - ms), end: now };
|
||||
}
|
||||
|
||||
function toMs(value?: Date | null) {
|
||||
return value ? value.getTime() : 0;
|
||||
}
|
||||
|
||||
export async function GET(req: NextRequest) {
|
||||
const perfEnabled = PERF_LOGS_ENABLED;
|
||||
const totalStart = nowMs();
|
||||
const timings: Record<string, number> = {};
|
||||
const { coldStart, uptimeMs } = getColdStartInfo();
|
||||
|
||||
const authStart = nowMs();
|
||||
const session = await requireSession();
|
||||
if (perfEnabled) timings.auth = elapsedMs(authStart);
|
||||
if (!session) return NextResponse.json({ ok: false, error: "Unauthorized" }, { status: 401 });
|
||||
|
||||
const preQueryStart = nowMs();
|
||||
const url = new URL(req.url);
|
||||
const range = url.searchParams.get("range") ?? "24h";
|
||||
const machineId = url.searchParams.get("machineId") ?? undefined;
|
||||
const { start, end } = pickRange(req);
|
||||
|
||||
const baseWhere = {
|
||||
orgId: session.orgId,
|
||||
...(machineId ? { machineId } : {}),
|
||||
ts: { gte: start, lte: end },
|
||||
};
|
||||
|
||||
if (perfEnabled) timings.preQuery = elapsedMs(preQueryStart);
|
||||
|
||||
const versionStart = nowMs();
|
||||
const cycleMax = await prisma.machineCycle.aggregate({
|
||||
where: baseWhere,
|
||||
_max: { tsServer: true },
|
||||
});
|
||||
if (perfEnabled) timings.version = elapsedMs(versionStart);
|
||||
|
||||
const versionParts = [
|
||||
session.orgId,
|
||||
range,
|
||||
machineId ?? "",
|
||||
toMs(cycleMax._max.tsServer),
|
||||
];
|
||||
const etag = `W/"${createHash("sha1").update(versionParts.join("|")).digest("hex")}"`;
|
||||
const responseHeaders = new Headers({
|
||||
"Cache-Control": "private, no-cache, max-age=0, must-revalidate",
|
||||
ETag: etag,
|
||||
"Last-Modified": new Date(toMs(cycleMax._max.tsServer) || 0).toUTCString(),
|
||||
Vary: "Cookie",
|
||||
});
|
||||
|
||||
const ifNoneMatch = req.headers.get("if-none-match");
|
||||
if (ifNoneMatch && ifNoneMatch === etag) {
|
||||
return new NextResponse(null, { status: 304, headers: responseHeaders });
|
||||
}
|
||||
|
||||
const workOrdersStart = nowMs();
|
||||
const workOrderRows = await prisma.machineCycle.findMany({
|
||||
where: { ...baseWhere, workOrderId: { not: null } },
|
||||
distinct: ["workOrderId"],
|
||||
select: { workOrderId: true },
|
||||
});
|
||||
if (perfEnabled) timings.workOrders = elapsedMs(workOrdersStart);
|
||||
|
||||
const skuStart = nowMs();
|
||||
const skuRows = await prisma.machineCycle.findMany({
|
||||
where: { ...baseWhere, sku: { not: null } },
|
||||
distinct: ["sku"],
|
||||
select: { sku: true },
|
||||
});
|
||||
if (perfEnabled) timings.skus = elapsedMs(skuStart);
|
||||
|
||||
const postQueryStart = nowMs();
|
||||
|
||||
const workOrders = workOrderRows.map((r) => r.workOrderId).filter(Boolean) as string[];
|
||||
const skus = skuRows.map((r) => r.sku).filter(Boolean) as string[];
|
||||
|
||||
const payload = { ok: true, workOrders, skus };
|
||||
|
||||
if (perfEnabled) {
|
||||
timings.postQuery = elapsedMs(postQueryStart);
|
||||
timings.total = elapsedMs(totalStart);
|
||||
responseHeaders.set("Server-Timing", formatServerTiming(timings));
|
||||
const payloadBytes = Buffer.byteLength(JSON.stringify(payload));
|
||||
logLine("perf.reports.filters", {
|
||||
orgId: session.orgId,
|
||||
coldStart,
|
||||
uptimeMs,
|
||||
range,
|
||||
machineId,
|
||||
timings,
|
||||
rowCounts: {
|
||||
workOrderRows: workOrderRows.length,
|
||||
skuRows: skuRows.length,
|
||||
},
|
||||
payloadBytes,
|
||||
});
|
||||
}
|
||||
|
||||
return NextResponse.json(payload, { headers: responseHeaders });
|
||||
}
|
||||
544
app/api/reports/route.ts
Normal file
544
app/api/reports/route.ts
Normal file
@@ -0,0 +1,544 @@
|
||||
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 { logLine } from "@/lib/logger";
|
||||
import { elapsedMs, formatServerTiming, nowMs, PERF_LOGS_ENABLED } from "@/lib/perf/serverTiming";
|
||||
|
||||
let reportsColdStart = true;
|
||||
|
||||
function getColdStartInfo() {
|
||||
const coldStart = reportsColdStart;
|
||||
reportsColdStart = false;
|
||||
return { coldStart, uptimeMs: Math.round(process.uptime() * 1000) };
|
||||
}
|
||||
|
||||
const RANGE_MS: Record<string, number> = {
|
||||
"24h": 24 * 60 * 60 * 1000,
|
||||
"7d": 7 * 24 * 60 * 60 * 1000,
|
||||
"30d": 30 * 24 * 60 * 60 * 1000,
|
||||
};
|
||||
|
||||
function parseDate(input?: string | null) {
|
||||
if (!input) return null;
|
||||
const n = Number(input);
|
||||
if (!Number.isNaN(n)) return new Date(n);
|
||||
const d = new Date(input);
|
||||
return Number.isNaN(d.getTime()) ? null : d;
|
||||
}
|
||||
|
||||
function pickRange(req: NextRequest) {
|
||||
const url = new URL(req.url);
|
||||
const range = url.searchParams.get("range") ?? "24h";
|
||||
const now = new Date();
|
||||
|
||||
if (range === "custom") {
|
||||
const start = parseDate(url.searchParams.get("start")) ?? new Date(now.getTime() - RANGE_MS["24h"]);
|
||||
const end = parseDate(url.searchParams.get("end")) ?? now;
|
||||
return { start, end };
|
||||
}
|
||||
|
||||
const ms = RANGE_MS[range] ?? RANGE_MS["24h"];
|
||||
return { start: new Date(now.getTime() - ms), end: now };
|
||||
}
|
||||
|
||||
function safeNum(v: unknown) {
|
||||
return typeof v === "number" && Number.isFinite(v) ? v : null;
|
||||
}
|
||||
|
||||
function isProductionSnapshot(trackingEnabled: unknown, productionStarted: unknown) {
|
||||
return trackingEnabled === true && productionStarted === true;
|
||||
}
|
||||
|
||||
function toMs(value?: Date | null) {
|
||||
return value ? value.getTime() : 0;
|
||||
}
|
||||
|
||||
export async function GET(req: NextRequest) {
|
||||
const perfEnabled = PERF_LOGS_ENABLED;
|
||||
const totalStart = nowMs();
|
||||
const timings: Record<string, number> = {};
|
||||
const { coldStart, uptimeMs } = getColdStartInfo();
|
||||
|
||||
const authStart = nowMs();
|
||||
const session = await requireSession();
|
||||
if (perfEnabled) timings.auth = elapsedMs(authStart);
|
||||
if (!session) return NextResponse.json({ ok: false, error: "Unauthorized" }, { status: 401 });
|
||||
|
||||
const preQueryStart = nowMs();
|
||||
const url = new URL(req.url);
|
||||
const range = url.searchParams.get("range") ?? "24h";
|
||||
const machineId = url.searchParams.get("machineId") ?? undefined;
|
||||
const { start, end } = pickRange(req);
|
||||
const workOrderId = url.searchParams.get("workOrderId") ?? undefined;
|
||||
const sku = url.searchParams.get("sku") ?? undefined;
|
||||
const baseWhere = {
|
||||
orgId: session.orgId,
|
||||
...(machineId ? { machineId } : {}),
|
||||
...(workOrderId ? { workOrderId } : {}),
|
||||
...(sku ? { sku } : {}),
|
||||
};
|
||||
|
||||
if (perfEnabled) timings.preQuery = elapsedMs(preQueryStart);
|
||||
|
||||
const versionStart = nowMs();
|
||||
const [kpiMax, cycleMax, eventMax] = await Promise.all([
|
||||
prisma.machineKpiSnapshot.aggregate({
|
||||
where: { ...baseWhere, ts: { gte: start, lte: end } },
|
||||
_max: { tsServer: true },
|
||||
}),
|
||||
prisma.machineCycle.aggregate({
|
||||
where: { ...baseWhere, ts: { gte: start, lte: end } },
|
||||
_max: { tsServer: true },
|
||||
}),
|
||||
prisma.machineEvent.aggregate({
|
||||
where: { ...baseWhere, ts: { gte: start, lte: end } },
|
||||
_max: { tsServer: true },
|
||||
}),
|
||||
]);
|
||||
if (perfEnabled) timings.version = elapsedMs(versionStart);
|
||||
|
||||
const lastModifiedMs = Math.max(
|
||||
toMs(kpiMax._max.tsServer),
|
||||
toMs(cycleMax._max.tsServer),
|
||||
toMs(eventMax._max.tsServer)
|
||||
);
|
||||
|
||||
const versionParts = [
|
||||
session.orgId,
|
||||
range,
|
||||
machineId ?? "",
|
||||
workOrderId ?? "",
|
||||
sku ?? "",
|
||||
toMs(kpiMax._max.tsServer),
|
||||
toMs(cycleMax._max.tsServer),
|
||||
toMs(eventMax._max.tsServer),
|
||||
];
|
||||
const etag = `W/"${createHash("sha1").update(versionParts.join("|")).digest("hex")}"`;
|
||||
const responseHeaders = new Headers({
|
||||
"Cache-Control": "private, no-cache, max-age=0, must-revalidate",
|
||||
ETag: etag,
|
||||
"Last-Modified": new Date(lastModifiedMs || 0).toUTCString(),
|
||||
Vary: "Cookie",
|
||||
});
|
||||
|
||||
const ifNoneMatch = req.headers.get("if-none-match");
|
||||
if (ifNoneMatch && ifNoneMatch === etag) {
|
||||
return new NextResponse(null, { status: 304, headers: responseHeaders });
|
||||
}
|
||||
|
||||
const kpiStart = nowMs();
|
||||
const kpiRows = await prisma.machineKpiSnapshot.findMany({
|
||||
where: { ...baseWhere, ts: { gte: start, lte: end } },
|
||||
orderBy: { ts: "asc" },
|
||||
select: {
|
||||
ts: true,
|
||||
oee: true,
|
||||
availability: true,
|
||||
performance: true,
|
||||
quality: true,
|
||||
good: true,
|
||||
scrap: true,
|
||||
target: true,
|
||||
trackingEnabled: true,
|
||||
productionStarted: true,
|
||||
machineId: true,
|
||||
},
|
||||
});
|
||||
if (perfEnabled) timings.kpiRows = elapsedMs(kpiStart);
|
||||
|
||||
let oeeSum = 0;
|
||||
let oeeCount = 0;
|
||||
let availSum = 0;
|
||||
let availCount = 0;
|
||||
let perfSum = 0;
|
||||
let perfCount = 0;
|
||||
let qualSum = 0;
|
||||
let qualCount = 0;
|
||||
|
||||
// OEE-family summaries are production-only to avoid mixing downtime/off windows.
|
||||
for (const k of kpiRows) {
|
||||
if (!isProductionSnapshot(k.trackingEnabled, k.productionStarted)) continue;
|
||||
if (safeNum(k.oee) != null) {
|
||||
oeeSum += Number(k.oee);
|
||||
oeeCount += 1;
|
||||
}
|
||||
if (safeNum(k.availability) != null) {
|
||||
availSum += Number(k.availability);
|
||||
availCount += 1;
|
||||
}
|
||||
if (safeNum(k.performance) != null) {
|
||||
perfSum += Number(k.performance);
|
||||
perfCount += 1;
|
||||
}
|
||||
if (safeNum(k.quality) != null) {
|
||||
qualSum += Number(k.quality);
|
||||
qualCount += 1;
|
||||
}
|
||||
}
|
||||
|
||||
const cyclesStart = nowMs();
|
||||
const cycles = await prisma.machineCycle.findMany({
|
||||
where: { ...baseWhere, ts: { gte: start, lte: end } },
|
||||
select: { goodDelta: true, scrapDelta: true },
|
||||
});
|
||||
if (perfEnabled) timings.cycles = elapsedMs(cyclesStart);
|
||||
|
||||
let goodTotal = 0;
|
||||
let scrapTotal = 0;
|
||||
|
||||
for (const c of cycles) {
|
||||
if (safeNum(c.goodDelta) != null) goodTotal += Number(c.goodDelta);
|
||||
if (safeNum(c.scrapDelta) != null) scrapTotal += Number(c.scrapDelta);
|
||||
}
|
||||
|
||||
const kpiAggStart = nowMs();
|
||||
const kpiAgg = await prisma.machineKpiSnapshot.groupBy({
|
||||
by: ["machineId"],
|
||||
where: { ...baseWhere, ts: { gte: start, lte: end } },
|
||||
_max: { good: true, scrap: true, target: true },
|
||||
_min: { good: true, scrap: true },
|
||||
_count: { _all: true },
|
||||
});
|
||||
if (perfEnabled) timings.kpiAgg = elapsedMs(kpiAggStart);
|
||||
|
||||
let targetTotal = 0;
|
||||
if (goodTotal === 0 && scrapTotal === 0) {
|
||||
let goodFallback = 0;
|
||||
let scrapFallback = 0;
|
||||
|
||||
for (const row of kpiAgg) {
|
||||
const count = row._count._all ?? 0;
|
||||
const maxGood = safeNum(row._max.good);
|
||||
const minGood = safeNum(row._min.good);
|
||||
const maxScrap = safeNum(row._max.scrap);
|
||||
const minScrap = safeNum(row._min.scrap);
|
||||
|
||||
if (count > 1 && maxGood != null && minGood != null) {
|
||||
goodFallback += Math.max(0, maxGood - minGood);
|
||||
} else if (maxGood != null) {
|
||||
goodFallback += maxGood;
|
||||
}
|
||||
|
||||
if (count > 1 && maxScrap != null && minScrap != null) {
|
||||
scrapFallback += Math.max(0, maxScrap - minScrap);
|
||||
} else if (maxScrap != null) {
|
||||
scrapFallback += maxScrap;
|
||||
}
|
||||
}
|
||||
|
||||
goodTotal = goodFallback;
|
||||
scrapTotal = scrapFallback;
|
||||
}
|
||||
|
||||
for (const row of kpiAgg) {
|
||||
const maxTarget = safeNum(row._max.target);
|
||||
if (maxTarget != null) targetTotal += maxTarget;
|
||||
}
|
||||
|
||||
const eventsStart = nowMs();
|
||||
const events = await prisma.machineEvent.findMany({
|
||||
where: { ...baseWhere, ts: { gte: start, lte: end } },
|
||||
select: { eventType: true, data: true },
|
||||
});
|
||||
if (perfEnabled) timings.events = elapsedMs(eventsStart);
|
||||
|
||||
let macrostopSec = 0;
|
||||
let microstopSec = 0;
|
||||
let slowCycleCount = 0;
|
||||
let qualitySpikeCount = 0;
|
||||
let performanceDegradationCount = 0;
|
||||
let oeeDropCount = 0;
|
||||
|
||||
for (const e of events) {
|
||||
const type = String(e.eventType ?? "").toLowerCase();
|
||||
let blob: unknown = e.data;
|
||||
|
||||
if (typeof blob === "string") {
|
||||
try {
|
||||
blob = JSON.parse(blob);
|
||||
} catch {
|
||||
blob = null;
|
||||
}
|
||||
}
|
||||
|
||||
const blobRecord = typeof blob === "object" && blob !== null ? (blob as Record<string, unknown>) : null;
|
||||
const innerCandidate = blobRecord?.data ?? blobRecord ?? {};
|
||||
const inner =
|
||||
typeof innerCandidate === "object" && innerCandidate !== null
|
||||
? (innerCandidate as Record<string, unknown>)
|
||||
: {};
|
||||
const stopSec =
|
||||
(typeof inner?.stoppage_duration_seconds === "number" && inner.stoppage_duration_seconds) ||
|
||||
(typeof inner?.stop_duration_seconds === "number" && inner.stop_duration_seconds) ||
|
||||
0;
|
||||
|
||||
if (type === "macrostop") macrostopSec += Number(stopSec) || 0;
|
||||
else if (type === "microstop") microstopSec += Number(stopSec) || 0;
|
||||
else if (type === "slow-cycle") slowCycleCount += 1;
|
||||
else if (type === "quality-spike") qualitySpikeCount += 1;
|
||||
else if (type === "performance-degradation") performanceDegradationCount += 1;
|
||||
else if (type === "oee-drop") oeeDropCount += 1;
|
||||
}
|
||||
|
||||
type TrendPoint = { t: string; v: number | null };
|
||||
|
||||
const trend: {
|
||||
oee: TrendPoint[];
|
||||
availability: TrendPoint[];
|
||||
performance: TrendPoint[];
|
||||
quality: TrendPoint[];
|
||||
scrapRate: TrendPoint[];
|
||||
} = {
|
||||
oee: [],
|
||||
availability: [],
|
||||
performance: [],
|
||||
quality: [],
|
||||
scrapRate: [],
|
||||
};
|
||||
|
||||
type TsBucket = {
|
||||
oeeSum: number; oeeCount: number;
|
||||
availSum: number; availCount: number;
|
||||
perfSum: number; perfCount: number;
|
||||
qualSum: number; qualCount: number;
|
||||
goodSum: number; scrapSum: number;
|
||||
anyProduction: boolean;
|
||||
};
|
||||
const tsBuckets = new Map<string, TsBucket>();
|
||||
|
||||
for (const k of kpiRows) {
|
||||
const t = k.ts.toISOString();
|
||||
let b = tsBuckets.get(t);
|
||||
if (!b) {
|
||||
b = {
|
||||
oeeSum: 0, oeeCount: 0,
|
||||
availSum: 0, availCount: 0,
|
||||
perfSum: 0, perfCount: 0,
|
||||
qualSum: 0, qualCount: 0,
|
||||
goodSum: 0, scrapSum: 0,
|
||||
anyProduction: false,
|
||||
};
|
||||
tsBuckets.set(t, b);
|
||||
}
|
||||
|
||||
const isProd = isProductionSnapshot(k.trackingEnabled, k.productionStarted);
|
||||
if (isProd) {
|
||||
b.anyProduction = true;
|
||||
const oee = safeNum(k.oee);
|
||||
if (oee != null) { b.oeeSum += Number(oee); b.oeeCount += 1; }
|
||||
const avail = safeNum(k.availability);
|
||||
if (avail != null) { b.availSum += Number(avail); b.availCount += 1; }
|
||||
const perf = safeNum(k.performance);
|
||||
if (perf != null) { b.perfSum += Number(perf); b.perfCount += 1; }
|
||||
const qual = safeNum(k.quality);
|
||||
if (qual != null) { b.qualSum += Number(qual); b.qualCount += 1; }
|
||||
}
|
||||
|
||||
const good = safeNum(k.good);
|
||||
const scrap = safeNum(k.scrap);
|
||||
if (good != null) b.goodSum += Number(good);
|
||||
if (scrap != null) b.scrapSum += Number(scrap);
|
||||
}
|
||||
|
||||
// Iterate sorted ts. kpiRows already orderBy ts asc, but Map insertion
|
||||
// order matches that, so spreading keys preserves order.
|
||||
for (const [t, b] of tsBuckets) {
|
||||
if (!b.anyProduction) {
|
||||
// No machine producing at this ts -> gap, same as before.
|
||||
trend.oee.push({ t, v: null });
|
||||
trend.availability.push({ t, v: null });
|
||||
trend.performance.push({ t, v: null });
|
||||
trend.quality.push({ t, v: null });
|
||||
} else {
|
||||
trend.oee.push({ t, v: b.oeeCount ? b.oeeSum / b.oeeCount : null });
|
||||
trend.availability.push({ t, v: b.availCount ? b.availSum / b.availCount : null });
|
||||
trend.performance.push({ t, v: b.perfCount ? b.perfSum / b.perfCount : null });
|
||||
trend.quality.push({ t, v: b.qualCount ? b.qualSum / b.qualCount : null });
|
||||
}
|
||||
const total = b.goodSum + b.scrapSum;
|
||||
if (total > 0) {
|
||||
trend.scrapRate.push({ t, v: (b.scrapSum / total) * 100 });
|
||||
}
|
||||
}
|
||||
const cycleRowsStart = nowMs();
|
||||
const cycleRows = await prisma.machineCycle.findMany({
|
||||
where: { ...baseWhere, ts: { gte: start, lte: end } },
|
||||
select: { actualCycleTime: true },
|
||||
});
|
||||
if (perfEnabled) timings.cycleRows = elapsedMs(cycleRowsStart);
|
||||
|
||||
const values = cycleRows
|
||||
.map((c) => Number(c.actualCycleTime))
|
||||
.filter((v) => Number.isFinite(v) && v > 0)
|
||||
.sort((a, b) => a - b);
|
||||
|
||||
let cycleTimeBins: {
|
||||
label: string;
|
||||
count: number;
|
||||
rangeStart?: number;
|
||||
rangeEnd?: number;
|
||||
overflow?: "low" | "high";
|
||||
minValue?: number;
|
||||
maxValue?: number;
|
||||
}[] = [];
|
||||
|
||||
if (values.length) {
|
||||
const pct = (p: number) => {
|
||||
const idx = Math.max(0, Math.min(values.length - 1, Math.floor(p * (values.length - 1))));
|
||||
return values[idx];
|
||||
};
|
||||
|
||||
const p5 = pct(0.05);
|
||||
const p95 = pct(0.95);
|
||||
|
||||
const inRange = values.filter((v) => v >= p5 && v <= p95);
|
||||
const low = values.filter((v) => v < p5);
|
||||
const high = values.filter((v) => v > p95);
|
||||
|
||||
const binCount = 10;
|
||||
const span = Math.max(0.1, p95 - p5);
|
||||
const step = span / binCount;
|
||||
|
||||
const counts = new Array(binCount).fill(0);
|
||||
for (const v of inRange) {
|
||||
const idx = Math.min(binCount - 1, Math.floor((v - p5) / step));
|
||||
counts[idx] += 1;
|
||||
}
|
||||
const decimals = step < 0.1 ? 2 : step < 1 ? 1 : 0;
|
||||
|
||||
cycleTimeBins = counts.map((count, i) => {
|
||||
const a = p5 + step * i;
|
||||
const b = p5 + step * (i + 1);
|
||||
return {
|
||||
label: `${a.toFixed(decimals)}-${b.toFixed(decimals)}s`,
|
||||
count,
|
||||
rangeStart: a,
|
||||
rangeEnd: b,
|
||||
};
|
||||
});
|
||||
|
||||
|
||||
if (low.length) {
|
||||
cycleTimeBins.unshift({
|
||||
label: `< ${p5.toFixed(1)}s`,
|
||||
count: low.length,
|
||||
rangeEnd: p5,
|
||||
overflow: "low",
|
||||
minValue: low[0],
|
||||
maxValue: low[low.length - 1],
|
||||
});
|
||||
}
|
||||
|
||||
if (high.length) {
|
||||
cycleTimeBins.push({
|
||||
label: `> ${p95.toFixed(1)}s`,
|
||||
count: high.length,
|
||||
rangeStart: p95,
|
||||
overflow: "high",
|
||||
minValue: high[0],
|
||||
maxValue: high[high.length - 1],
|
||||
});
|
||||
}
|
||||
}
|
||||
const scrapRate =
|
||||
goodTotal + scrapTotal > 0 ? (scrapTotal / (goodTotal + scrapTotal)) * 100 : null;
|
||||
|
||||
|
||||
|
||||
// top scrap SKU / work order (from cycles)
|
||||
const scrapBySku = new Map<string, number>();
|
||||
const scrapByWo = new Map<string, number>();
|
||||
|
||||
const scrapRowsStart = nowMs();
|
||||
const scrapRows = await prisma.machineCycle.findMany({
|
||||
where: { ...baseWhere, ts: { gte: start, lte: end } },
|
||||
select: { sku: true, workOrderId: true, scrapDelta: true },
|
||||
});
|
||||
if (perfEnabled) timings.scrapRows = elapsedMs(scrapRowsStart);
|
||||
|
||||
const postQueryStart = nowMs();
|
||||
|
||||
for (const row of scrapRows) {
|
||||
const scrap = safeNum(row.scrapDelta);
|
||||
if (scrap == null || scrap <= 0) continue;
|
||||
if (row.sku) scrapBySku.set(row.sku, (scrapBySku.get(row.sku) ?? 0) + scrap);
|
||||
if (row.workOrderId) scrapByWo.set(row.workOrderId, (scrapByWo.get(row.workOrderId) ?? 0) + scrap);
|
||||
}
|
||||
|
||||
const topScrapSku = [...scrapBySku.entries()].sort((a, b) => b[1] - a[1])[0]?.[0] ?? null;
|
||||
const topScrapWorkOrder = [...scrapByWo.entries()].sort((a, b) => b[1] - a[1])[0]?.[0] ?? null;
|
||||
|
||||
const oeeAvg = oeeCount ? oeeSum / oeeCount : null;
|
||||
const availabilityAvg = availCount ? availSum / availCount : null;
|
||||
const performanceAvg = perfCount ? perfSum / perfCount : null;
|
||||
const qualityAvg = qualCount ? qualSum / qualCount : null;
|
||||
|
||||
// insights
|
||||
const insights: string[] = [];
|
||||
if (scrapRate != null && scrapRate > 5) insights.push(`Scrap rate is ${scrapRate.toFixed(1)}% (above 5%).`);
|
||||
if (performanceAvg != null && performanceAvg < 85) insights.push("Performance below 85%.");
|
||||
if (availabilityAvg != null && availabilityAvg < 85) insights.push("Availability below 85%.");
|
||||
if (oeeAvg != null && oeeAvg < 85) insights.push("OEE below 85%.");
|
||||
if (macrostopSec > 1800) insights.push("Macrostop time exceeds 30 minutes in this range.");
|
||||
|
||||
|
||||
|
||||
const payload = {
|
||||
ok: true,
|
||||
summary: {
|
||||
oeeAvg,
|
||||
availabilityAvg,
|
||||
performanceAvg,
|
||||
qualityAvg,
|
||||
goodTotal,
|
||||
scrapTotal,
|
||||
targetTotal,
|
||||
scrapRate,
|
||||
topScrapSku,
|
||||
topScrapWorkOrder,
|
||||
},
|
||||
|
||||
downtime: {
|
||||
macrostopSec,
|
||||
microstopSec,
|
||||
slowCycleCount,
|
||||
qualitySpikeCount,
|
||||
performanceDegradationCount,
|
||||
oeeDropCount,
|
||||
},
|
||||
trend,
|
||||
insights,
|
||||
distribution: {
|
||||
cycleTime: cycleTimeBins,
|
||||
},
|
||||
};
|
||||
|
||||
if (perfEnabled) {
|
||||
timings.postQuery = elapsedMs(postQueryStart);
|
||||
timings.total = elapsedMs(totalStart);
|
||||
responseHeaders.set("Server-Timing", formatServerTiming(timings));
|
||||
const payloadBytes = Buffer.byteLength(JSON.stringify(payload));
|
||||
logLine("perf.reports.api", {
|
||||
orgId: session.orgId,
|
||||
coldStart,
|
||||
uptimeMs,
|
||||
range,
|
||||
machineId,
|
||||
workOrderId,
|
||||
sku,
|
||||
timings,
|
||||
rowCounts: {
|
||||
kpiRows: kpiRows.length,
|
||||
cycles: cycles.length,
|
||||
events: events.length,
|
||||
cycleRows: cycleRows.length,
|
||||
scrapRows: scrapRows.length,
|
||||
},
|
||||
payloadBytes,
|
||||
});
|
||||
}
|
||||
|
||||
return NextResponse.json(payload, { headers: responseHeaders });
|
||||
}
|
||||
458
app/api/settings/machines/[machineId]/route.ts
Normal file
458
app/api/settings/machines/[machineId]/route.ts
Normal file
@@ -0,0 +1,458 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import type { NextRequest } from "next/server";
|
||||
import { Prisma } from "@prisma/client";
|
||||
import { prisma } from "@/lib/prisma";
|
||||
import { requireSession } from "@/lib/auth/requireSession";
|
||||
import { toJsonValue } from "@/lib/prismaJson";
|
||||
import {
|
||||
DEFAULT_ALERTS,
|
||||
DEFAULT_DEFAULTS,
|
||||
DEFAULT_SHIFT,
|
||||
applyOverridePatch,
|
||||
buildSettingsPayload,
|
||||
deepMerge,
|
||||
validateDefaults,
|
||||
validateShiftFields,
|
||||
validateShiftSchedule,
|
||||
validateShiftOverrides,
|
||||
validateThresholds,
|
||||
} from "@/lib/settings";
|
||||
import { loadFallbackReasonCatalog, normalizeReasonCatalog, type ReasonCatalog } from "@/lib/reasonCatalog";
|
||||
import { publishSettingsUpdate } from "@/lib/mqtt";
|
||||
import { z } from "zod";
|
||||
|
||||
function isPlainObject(value: unknown): value is Record<string, unknown> {
|
||||
return !!value && typeof value === "object" && !Array.isArray(value);
|
||||
}
|
||||
|
||||
function canManageSettings(role?: string | null) {
|
||||
return role === "OWNER" || role === "ADMIN";
|
||||
}
|
||||
|
||||
const machineIdSchema = z.string().uuid();
|
||||
const machineSettingsSchema = z
|
||||
.object({
|
||||
source: z.string().trim().max(40).optional(),
|
||||
overrides: z.any().optional(),
|
||||
})
|
||||
.passthrough();
|
||||
|
||||
function pickAllowedOverrides(raw: unknown) {
|
||||
if (!isPlainObject(raw)) return {};
|
||||
const out: Record<string, unknown> = {};
|
||||
for (const key of ["shiftSchedule", "thresholds", "alerts", "defaults"]) {
|
||||
if (raw[key] !== undefined) out[key] = raw[key];
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
function withReasonCatalog<T extends Record<string, unknown>>(payload: T, fallbackCatalog: ReasonCatalog) {
|
||||
const base = (isPlainObject(payload) ? { ...payload } : {}) as T;
|
||||
const defaults = isPlainObject(base.defaults) ? base.defaults : {};
|
||||
const parsed =
|
||||
normalizeReasonCatalog(base.reasonCatalog) ??
|
||||
normalizeReasonCatalog(base.reasonCatalogData) ??
|
||||
normalizeReasonCatalog(defaults.reasonCatalog) ??
|
||||
normalizeReasonCatalog(defaults.reasonCatalogData) ??
|
||||
fallbackCatalog;
|
||||
|
||||
return {
|
||||
...base,
|
||||
reasonCatalog: parsed,
|
||||
reasonCatalogData: parsed,
|
||||
reasonCatalogVersion: Number(parsed.version || 1),
|
||||
};
|
||||
}
|
||||
|
||||
async function ensureOrgSettings(
|
||||
tx: Prisma.TransactionClient,
|
||||
orgId: string,
|
||||
userId?: string | null
|
||||
) {
|
||||
let settings = await tx.orgSettings.findUnique({
|
||||
where: { orgId },
|
||||
});
|
||||
|
||||
if (settings) {
|
||||
let shifts = await tx.orgShift.findMany({
|
||||
where: { orgId },
|
||||
orderBy: { sortOrder: "asc" },
|
||||
});
|
||||
if (!shifts.length) {
|
||||
await tx.orgShift.create({
|
||||
data: {
|
||||
orgId,
|
||||
name: DEFAULT_SHIFT.name,
|
||||
startTime: DEFAULT_SHIFT.start,
|
||||
endTime: DEFAULT_SHIFT.end,
|
||||
sortOrder: 1,
|
||||
enabled: true,
|
||||
},
|
||||
});
|
||||
shifts = await tx.orgShift.findMany({
|
||||
where: { orgId },
|
||||
orderBy: { sortOrder: "asc" },
|
||||
});
|
||||
}
|
||||
return { settings, shifts };
|
||||
}
|
||||
|
||||
settings = await tx.orgSettings.create({
|
||||
data: {
|
||||
orgId,
|
||||
timezone: "UTC",
|
||||
shiftChangeCompMin: 10,
|
||||
lunchBreakMin: 30,
|
||||
stoppageMultiplier: 1.5,
|
||||
macroStoppageMultiplier: 5,
|
||||
oeeAlertThresholdPct: 90,
|
||||
performanceThresholdPct: 85,
|
||||
qualitySpikeDeltaPct: 5,
|
||||
alertsJson: DEFAULT_ALERTS,
|
||||
defaultsJson: DEFAULT_DEFAULTS,
|
||||
updatedBy: userId ?? null,
|
||||
},
|
||||
});
|
||||
|
||||
await tx.orgShift.create({
|
||||
data: {
|
||||
orgId,
|
||||
name: DEFAULT_SHIFT.name,
|
||||
startTime: DEFAULT_SHIFT.start,
|
||||
endTime: DEFAULT_SHIFT.end,
|
||||
sortOrder: 1,
|
||||
enabled: true,
|
||||
},
|
||||
});
|
||||
|
||||
const shifts = await tx.orgShift.findMany({
|
||||
where: { orgId },
|
||||
orderBy: { sortOrder: "asc" },
|
||||
});
|
||||
return { settings, shifts };
|
||||
}
|
||||
|
||||
export async function GET(
|
||||
req: NextRequest,
|
||||
{ params }: { params: Promise<{ machineId: string }> }
|
||||
) {
|
||||
const { machineId } = await params;
|
||||
if (!machineIdSchema.safeParse(machineId).success) {
|
||||
return NextResponse.json({ ok: false, error: "Invalid machine id" }, { status: 400 });
|
||||
}
|
||||
|
||||
const session = await requireSession();
|
||||
let orgId: string | null = null;
|
||||
let userId: string | null = null;
|
||||
let machine: { id: string; orgId: string } | null = null;
|
||||
|
||||
if (session) {
|
||||
machine = await prisma.machine.findFirst({
|
||||
where: { id: machineId, orgId: session.orgId },
|
||||
select: { id: true, orgId: true },
|
||||
});
|
||||
if (!machine) return NextResponse.json({ ok: false, error: "Not found" }, { status: 404 });
|
||||
orgId = machine.orgId;
|
||||
userId = session.userId;
|
||||
} else {
|
||||
const apiKey = req.headers.get("x-api-key");
|
||||
if (!apiKey) return NextResponse.json({ ok: false, error: "Unauthorized" }, { status: 401 });
|
||||
machine = await prisma.machine.findFirst({
|
||||
where: { id: machineId, apiKey },
|
||||
select: { id: true, orgId: true },
|
||||
});
|
||||
if (!machine) return NextResponse.json({ ok: false, error: "Unauthorized" }, { status: 401 });
|
||||
orgId = machine.orgId;
|
||||
}
|
||||
const fallbackCatalog = await loadFallbackReasonCatalog();
|
||||
|
||||
const { settings, overrides } = await prisma.$transaction(async (tx) => {
|
||||
const orgSettings = await ensureOrgSettings(tx, orgId as string, userId);
|
||||
if (!orgSettings?.settings) throw new Error("SETTINGS_NOT_FOUND");
|
||||
|
||||
const machineSettings = await tx.machineSettings.findUnique({
|
||||
where: { machineId },
|
||||
select: { overridesJson: true },
|
||||
});
|
||||
|
||||
const orgPayload = withReasonCatalog(
|
||||
buildSettingsPayload(orgSettings.settings, orgSettings.shifts ?? []),
|
||||
fallbackCatalog
|
||||
);
|
||||
const rawOverrides = pickAllowedOverrides(machineSettings?.overridesJson ?? {});
|
||||
const effective = withReasonCatalog(
|
||||
deepMerge(orgPayload, rawOverrides) as Record<string, unknown>,
|
||||
fallbackCatalog
|
||||
);
|
||||
|
||||
return { settings: { org: orgPayload, effective }, overrides: rawOverrides };
|
||||
});
|
||||
|
||||
return NextResponse.json({
|
||||
ok: true,
|
||||
machineId,
|
||||
orgSettings: settings.org,
|
||||
effectiveSettings: settings.effective,
|
||||
overrides,
|
||||
});
|
||||
}
|
||||
|
||||
export async function PUT(
|
||||
req: NextRequest,
|
||||
{ params }: { params: Promise<{ machineId: string }> }
|
||||
) {
|
||||
const session = await requireSession();
|
||||
if (!session) return NextResponse.json({ ok: false, error: "Unauthorized" }, { status: 401 });
|
||||
|
||||
const membership = await prisma.orgUser.findUnique({
|
||||
where: { orgId_userId: { orgId: session.orgId, userId: session.userId } },
|
||||
select: { role: true },
|
||||
});
|
||||
if (!canManageSettings(membership?.role)) {
|
||||
return NextResponse.json({ ok: false, error: "Forbidden" }, { status: 403 });
|
||||
}
|
||||
|
||||
const { machineId } = await params;
|
||||
if (!machineIdSchema.safeParse(machineId).success) {
|
||||
return NextResponse.json({ ok: false, error: "Invalid machine id" }, { status: 400 });
|
||||
}
|
||||
|
||||
const machine = await prisma.machine.findFirst({
|
||||
where: { id: machineId, orgId: session.orgId },
|
||||
select: { id: true },
|
||||
});
|
||||
|
||||
if (!machine) return NextResponse.json({ ok: false, error: "Not found" }, { status: 404 });
|
||||
|
||||
const body = await req.json().catch(() => ({}));
|
||||
const parsed = machineSettingsSchema.safeParse(body);
|
||||
if (!parsed.success) {
|
||||
return NextResponse.json({ ok: false, error: "Invalid settings payload" }, { status: 400 });
|
||||
}
|
||||
const source = String(parsed.data.source ?? "control_tower");
|
||||
|
||||
let patch = parsed.data.overrides ?? parsed.data;
|
||||
if (patch === null) {
|
||||
patch = null;
|
||||
}
|
||||
|
||||
if (patch && !isPlainObject(patch)) {
|
||||
return NextResponse.json({ ok: false, error: "overrides must be an object or null" }, { status: 400 });
|
||||
}
|
||||
|
||||
if (patch && Object.keys(patch).length === 0) {
|
||||
return NextResponse.json({ ok: false, error: "No overrides provided" }, { status: 400 });
|
||||
}
|
||||
|
||||
if (patch && Object.keys(pickAllowedOverrides(patch)).length !== Object.keys(patch).length) {
|
||||
return NextResponse.json({ ok: false, error: "overrides contain unsupported keys" }, { status: 400 });
|
||||
}
|
||||
|
||||
if (patch?.shiftSchedule && !isPlainObject(patch.shiftSchedule)) {
|
||||
return NextResponse.json({ ok: false, error: "shiftSchedule must be an object" }, { status: 400 });
|
||||
}
|
||||
if (patch?.thresholds !== undefined && patch.thresholds !== null && !isPlainObject(patch.thresholds)) {
|
||||
return NextResponse.json({ ok: false, error: "thresholds must be an object" }, { status: 400 });
|
||||
}
|
||||
if (patch?.alerts !== undefined && patch.alerts !== null && !isPlainObject(patch.alerts)) {
|
||||
return NextResponse.json({ ok: false, error: "alerts must be an object" }, { status: 400 });
|
||||
}
|
||||
if (patch?.defaults !== undefined && patch.defaults !== null && !isPlainObject(patch.defaults)) {
|
||||
return NextResponse.json({ ok: false, error: "defaults must be an object" }, { status: 400 });
|
||||
}
|
||||
|
||||
const shiftValidation = validateShiftFields(
|
||||
patch?.shiftSchedule?.shiftChangeCompensationMin,
|
||||
patch?.shiftSchedule?.lunchBreakMin
|
||||
);
|
||||
if (!shiftValidation.ok) {
|
||||
return NextResponse.json({ ok: false, error: shiftValidation.error }, { status: 400 });
|
||||
}
|
||||
|
||||
const overridesResult =
|
||||
patch?.shiftSchedule?.overrides !== undefined
|
||||
? validateShiftOverrides(patch.shiftSchedule.overrides)
|
||||
: ({ ok: true, overrides: undefined } as const);
|
||||
if (!overridesResult.ok) {
|
||||
return NextResponse.json({ ok: false, error: overridesResult.error }, { status: 400 });
|
||||
}
|
||||
|
||||
const thresholdsValidation = validateThresholds(patch?.thresholds);
|
||||
if (!thresholdsValidation.ok) {
|
||||
return NextResponse.json({ ok: false, error: thresholdsValidation.error }, { status: 400 });
|
||||
}
|
||||
|
||||
const defaultsValidation = validateDefaults(patch?.defaults);
|
||||
if (!defaultsValidation.ok) {
|
||||
return NextResponse.json({ ok: false, error: defaultsValidation.error }, { status: 400 });
|
||||
}
|
||||
|
||||
if (patch?.shiftSchedule?.shifts !== undefined) {
|
||||
const shiftResult = validateShiftSchedule(patch.shiftSchedule.shifts);
|
||||
if (!shiftResult.ok) {
|
||||
return NextResponse.json({ ok: false, error: shiftResult.error }, { status: 400 });
|
||||
}
|
||||
patch = {
|
||||
...patch,
|
||||
shiftSchedule: {
|
||||
...patch.shiftSchedule,
|
||||
shifts: shiftResult.shifts?.map((s) => ({
|
||||
name: s.name,
|
||||
start: s.startTime,
|
||||
end: s.endTime,
|
||||
enabled: s.enabled !== false,
|
||||
})),
|
||||
},
|
||||
};
|
||||
}
|
||||
if (patch?.shiftSchedule) {
|
||||
patch = {
|
||||
...patch,
|
||||
shiftSchedule: {
|
||||
...patch.shiftSchedule,
|
||||
overrides:
|
||||
patch.shiftSchedule.overrides !== undefined
|
||||
? overridesResult.overrides === null
|
||||
? null
|
||||
: overridesResult.overrides
|
||||
: patch.shiftSchedule.overrides,
|
||||
shiftChangeCompensationMin:
|
||||
patch.shiftSchedule.shiftChangeCompensationMin !== undefined
|
||||
? Number(patch.shiftSchedule.shiftChangeCompensationMin)
|
||||
: patch.shiftSchedule.shiftChangeCompensationMin,
|
||||
lunchBreakMin:
|
||||
patch.shiftSchedule.lunchBreakMin !== undefined
|
||||
? Number(patch.shiftSchedule.lunchBreakMin)
|
||||
: patch.shiftSchedule.lunchBreakMin,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
if (patch?.thresholds) {
|
||||
patch = {
|
||||
...patch,
|
||||
thresholds: {
|
||||
...patch.thresholds,
|
||||
stoppageMultiplier:
|
||||
patch.thresholds.stoppageMultiplier !== undefined
|
||||
? Number(patch.thresholds.stoppageMultiplier)
|
||||
: patch.thresholds.stoppageMultiplier,
|
||||
macroStoppageMultiplier:
|
||||
patch.thresholds.macroStoppageMultiplier !== undefined
|
||||
? Number(patch.thresholds.macroStoppageMultiplier)
|
||||
: patch.thresholds.macroStoppageMultiplier,
|
||||
oeeAlertThresholdPct:
|
||||
patch.thresholds.oeeAlertThresholdPct !== undefined
|
||||
? Number(patch.thresholds.oeeAlertThresholdPct)
|
||||
: patch.thresholds.oeeAlertThresholdPct,
|
||||
performanceThresholdPct:
|
||||
patch.thresholds.performanceThresholdPct !== undefined
|
||||
? Number(patch.thresholds.performanceThresholdPct)
|
||||
: patch.thresholds.performanceThresholdPct,
|
||||
qualitySpikeDeltaPct:
|
||||
patch.thresholds.qualitySpikeDeltaPct !== undefined
|
||||
? Number(patch.thresholds.qualitySpikeDeltaPct)
|
||||
: patch.thresholds.qualitySpikeDeltaPct,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
if (patch?.defaults) {
|
||||
patch = {
|
||||
...patch,
|
||||
defaults: {
|
||||
...patch.defaults,
|
||||
moldTotal:
|
||||
patch.defaults.moldTotal !== undefined ? Number(patch.defaults.moldTotal) : patch.defaults.moldTotal,
|
||||
moldActive:
|
||||
patch.defaults.moldActive !== undefined ? Number(patch.defaults.moldActive) : patch.defaults.moldActive,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
const result = await prisma.$transaction(async (tx) => {
|
||||
const orgSettings = await ensureOrgSettings(tx, session.orgId, session.userId);
|
||||
if (!orgSettings?.settings) throw new Error("SETTINGS_NOT_FOUND");
|
||||
|
||||
const existing = await tx.machineSettings.findUnique({
|
||||
where: { machineId },
|
||||
select: { overridesJson: true },
|
||||
});
|
||||
|
||||
let nextOverrides: Record<string, unknown> | null = null;
|
||||
if (patch === null) {
|
||||
nextOverrides = null;
|
||||
} else {
|
||||
const merged = applyOverridePatch(existing?.overridesJson ?? {}, patch);
|
||||
nextOverrides = Object.keys(merged).length ? merged : null;
|
||||
}
|
||||
const nextOverridesJson =
|
||||
nextOverrides === null ? Prisma.DbNull : toJsonValue(nextOverrides);
|
||||
|
||||
const saved = await tx.machineSettings.upsert({
|
||||
where: { machineId },
|
||||
update: {
|
||||
overridesJson: nextOverridesJson,
|
||||
updatedBy: session.userId,
|
||||
},
|
||||
create: {
|
||||
machineId,
|
||||
orgId: session.orgId,
|
||||
overridesJson: nextOverridesJson,
|
||||
updatedBy: session.userId,
|
||||
},
|
||||
});
|
||||
|
||||
await tx.settingsAudit.create({
|
||||
data: {
|
||||
orgId: session.orgId,
|
||||
machineId,
|
||||
actorId: session.userId,
|
||||
source,
|
||||
payloadJson: body,
|
||||
},
|
||||
});
|
||||
|
||||
const fallbackCatalog = await loadFallbackReasonCatalog();
|
||||
const orgPayload = withReasonCatalog(
|
||||
buildSettingsPayload(orgSettings.settings, orgSettings.shifts ?? []),
|
||||
fallbackCatalog
|
||||
);
|
||||
const overrides = pickAllowedOverrides(saved.overridesJson ?? {});
|
||||
const effective = withReasonCatalog(
|
||||
deepMerge(orgPayload, overrides) as Record<string, unknown>,
|
||||
fallbackCatalog
|
||||
);
|
||||
|
||||
return {
|
||||
orgPayload,
|
||||
overrides,
|
||||
effective,
|
||||
overridesUpdatedAt: saved.updatedAt,
|
||||
};
|
||||
});
|
||||
|
||||
const overridesUpdatedAt =
|
||||
result.overridesUpdatedAt && result.overridesUpdatedAt instanceof Date
|
||||
? result.overridesUpdatedAt.toISOString()
|
||||
: undefined;
|
||||
try {
|
||||
await publishSettingsUpdate({
|
||||
orgId: session.orgId,
|
||||
machineId,
|
||||
version: Number(result.orgPayload.version ?? 0),
|
||||
source,
|
||||
overridesUpdatedAt,
|
||||
});
|
||||
} catch (err) {
|
||||
console.warn("[settings machine PUT] MQTT publish failed", err);
|
||||
}
|
||||
|
||||
return NextResponse.json({
|
||||
ok: true,
|
||||
machineId,
|
||||
orgSettings: result.orgPayload,
|
||||
effectiveSettings: result.effective,
|
||||
overrides: result.overrides,
|
||||
});
|
||||
}
|
||||
477
app/api/settings/route.ts
Normal file
477
app/api/settings/route.ts
Normal file
@@ -0,0 +1,477 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import type { NextRequest } from "next/server";
|
||||
import { createHash } from "crypto";
|
||||
import { revalidateTag, unstable_cache } from "next/cache";
|
||||
import { Prisma } from "@prisma/client";
|
||||
import { prisma } from "@/lib/prisma";
|
||||
import { requireSession } from "@/lib/auth/requireSession";
|
||||
import {
|
||||
DEFAULT_ALERTS,
|
||||
DEFAULT_DEFAULTS,
|
||||
DEFAULT_SHIFT,
|
||||
buildSettingsPayload,
|
||||
normalizeAlerts,
|
||||
normalizeDefaults,
|
||||
stripUndefined,
|
||||
validateDefaults,
|
||||
validateShiftFields,
|
||||
validateShiftSchedule,
|
||||
validateShiftOverrides,
|
||||
validateThresholds,
|
||||
} from "@/lib/settings";
|
||||
import { loadFallbackReasonCatalog, normalizeReasonCatalog, type ReasonCatalog } from "@/lib/reasonCatalog";
|
||||
import { publishSettingsUpdate } from "@/lib/mqtt";
|
||||
import { z } from "zod";
|
||||
|
||||
type ValidShift = {
|
||||
name: string;
|
||||
startTime: string;
|
||||
endTime: string;
|
||||
sortOrder: number;
|
||||
enabled: boolean;
|
||||
};
|
||||
|
||||
function isPlainObject(value: unknown): value is Record<string, unknown> {
|
||||
return !!value && typeof value === "object" && !Array.isArray(value);
|
||||
}
|
||||
|
||||
function canManageSettings(role?: string | null) {
|
||||
return role === "OWNER" || role === "ADMIN";
|
||||
}
|
||||
|
||||
function withReasonCatalog<T extends Record<string, unknown>>(payload: T, fallbackCatalog: ReasonCatalog) {
|
||||
const base = (isPlainObject(payload) ? { ...payload } : {}) as T;
|
||||
const defaults = isPlainObject(base.defaults) ? base.defaults : {};
|
||||
const parsed =
|
||||
normalizeReasonCatalog(base.reasonCatalog) ??
|
||||
normalizeReasonCatalog(base.reasonCatalogData) ??
|
||||
normalizeReasonCatalog(defaults.reasonCatalog) ??
|
||||
normalizeReasonCatalog(defaults.reasonCatalogData) ??
|
||||
fallbackCatalog;
|
||||
|
||||
return {
|
||||
...base,
|
||||
reasonCatalog: parsed,
|
||||
reasonCatalogData: parsed,
|
||||
reasonCatalogVersion: Number(parsed.version || 1),
|
||||
};
|
||||
}
|
||||
|
||||
const settingsPayloadSchema = z
|
||||
.object({
|
||||
source: z.string().trim().max(40).optional(),
|
||||
modules: z.any().optional(),
|
||||
timezone: z.string().trim().max(64).optional(),
|
||||
shiftSchedule: z.any().optional(),
|
||||
thresholds: z.any().optional(),
|
||||
alerts: z.any().optional(),
|
||||
defaults: z.any().optional(),
|
||||
reasonCatalog: z.any().optional(),
|
||||
version: z.union([z.number(), z.string()]).optional(),
|
||||
})
|
||||
.passthrough();
|
||||
|
||||
const SETTINGS_TTL_SEC = 10;
|
||||
const SETTINGS_SWR_SEC = 30;
|
||||
|
||||
async function ensureOrgSettings(tx: Prisma.TransactionClient, orgId: string, userId: string) {
|
||||
let settings = await tx.orgSettings.findUnique({
|
||||
where: { orgId },
|
||||
});
|
||||
|
||||
if (settings) {
|
||||
let shifts = await tx.orgShift.findMany({
|
||||
where: { orgId },
|
||||
orderBy: { sortOrder: "asc" },
|
||||
});
|
||||
if (!shifts.length) {
|
||||
await tx.orgShift.create({
|
||||
data: {
|
||||
orgId,
|
||||
name: DEFAULT_SHIFT.name,
|
||||
startTime: DEFAULT_SHIFT.start,
|
||||
endTime: DEFAULT_SHIFT.end,
|
||||
sortOrder: 1,
|
||||
enabled: true,
|
||||
},
|
||||
});
|
||||
shifts = await tx.orgShift.findMany({
|
||||
where: { orgId },
|
||||
orderBy: { sortOrder: "asc" },
|
||||
});
|
||||
}
|
||||
return { settings, shifts };
|
||||
}
|
||||
|
||||
settings = await tx.orgSettings.create({
|
||||
data: {
|
||||
orgId,
|
||||
timezone: "UTC",
|
||||
shiftChangeCompMin: 10,
|
||||
lunchBreakMin: 30,
|
||||
stoppageMultiplier: 1.5,
|
||||
macroStoppageMultiplier: 5,
|
||||
oeeAlertThresholdPct: 90,
|
||||
performanceThresholdPct: 85,
|
||||
qualitySpikeDeltaPct: 5,
|
||||
alertsJson: DEFAULT_ALERTS,
|
||||
defaultsJson: { ...(DEFAULT_DEFAULTS as any), modules: { screenlessMode: false } },
|
||||
updatedBy: userId,
|
||||
},
|
||||
});
|
||||
|
||||
await tx.orgShift.create({
|
||||
data: {
|
||||
orgId,
|
||||
name: DEFAULT_SHIFT.name,
|
||||
startTime: DEFAULT_SHIFT.start,
|
||||
endTime: DEFAULT_SHIFT.end,
|
||||
sortOrder: 1,
|
||||
enabled: true,
|
||||
},
|
||||
});
|
||||
|
||||
const shifts = await tx.orgShift.findMany({
|
||||
where: { orgId },
|
||||
orderBy: { sortOrder: "asc" },
|
||||
});
|
||||
return { settings, shifts };
|
||||
}
|
||||
|
||||
async function loadSettingsPayload(orgId: string, userId: string) {
|
||||
const loaded = await prisma.$transaction(async (tx) => {
|
||||
const found = await ensureOrgSettings(tx, orgId, userId);
|
||||
if (!found?.settings) throw new Error("SETTINGS_NOT_FOUND");
|
||||
return found;
|
||||
});
|
||||
|
||||
const fallbackCatalog = await loadFallbackReasonCatalog();
|
||||
const payload = withReasonCatalog(buildSettingsPayload(loaded.settings, loaded.shifts ?? []), fallbackCatalog);
|
||||
const defaultsRaw = isPlainObject(loaded.settings.defaultsJson) ? (loaded.settings.defaultsJson as any) : {};
|
||||
const modulesRaw = isPlainObject(defaultsRaw.modules) ? defaultsRaw.modules : {};
|
||||
const modules = { screenlessMode: modulesRaw.screenlessMode === true };
|
||||
|
||||
return { payload, modules };
|
||||
}
|
||||
|
||||
async function loadSettingsCached(orgId: string, userId: string) {
|
||||
const cached = unstable_cache(
|
||||
() => loadSettingsPayload(orgId, userId),
|
||||
["settings", orgId],
|
||||
{ revalidate: SETTINGS_TTL_SEC, tags: [`settings:${orgId}`] }
|
||||
);
|
||||
return cached();
|
||||
}
|
||||
|
||||
export async function GET(req: NextRequest) {
|
||||
const session = await requireSession();
|
||||
if (!session) return NextResponse.json({ ok: false, error: "Unauthorized" }, { status: 401 });
|
||||
|
||||
try {
|
||||
const url = new URL(req.url);
|
||||
const refresh = url.searchParams.get("refresh") === "1";
|
||||
const { payload, modules } = refresh
|
||||
? await loadSettingsPayload(session.orgId, session.userId)
|
||||
: await loadSettingsCached(session.orgId, session.userId);
|
||||
|
||||
const version = payload.version ?? 0;
|
||||
const etag = `W/"${createHash("sha1").update(`${session.orgId}:${version}`).digest("hex")}"`;
|
||||
const responseHeaders = new Headers({
|
||||
"Cache-Control": `private, max-age=${SETTINGS_TTL_SEC}, stale-while-revalidate=${SETTINGS_SWR_SEC}`,
|
||||
ETag: etag,
|
||||
Vary: "Cookie",
|
||||
});
|
||||
|
||||
const ifNoneMatch = req.headers.get("if-none-match");
|
||||
if (!refresh && ifNoneMatch && ifNoneMatch === etag) {
|
||||
return new NextResponse(null, { status: 304, headers: responseHeaders });
|
||||
}
|
||||
|
||||
return NextResponse.json({ ok: true, settings: { ...payload, modules } }, { headers: responseHeaders });
|
||||
|
||||
} catch (err) {
|
||||
console.error("[settings GET] failed", err);
|
||||
const message = err instanceof Error ? err.message : "Internal error";
|
||||
return NextResponse.json({ ok: false, error: message }, { status: 500 });
|
||||
}
|
||||
}
|
||||
|
||||
export async function PUT(req: Request) {
|
||||
const session = await requireSession();
|
||||
if (!session) return NextResponse.json({ ok: false, error: "Unauthorized" }, { status: 401 });
|
||||
|
||||
const membership = await prisma.orgUser.findUnique({
|
||||
where: { orgId_userId: { orgId: session.orgId, userId: session.userId } },
|
||||
select: { role: true },
|
||||
});
|
||||
if (!canManageSettings(membership?.role)) {
|
||||
return NextResponse.json({ ok: false, error: "Forbidden" }, { status: 403 });
|
||||
}
|
||||
|
||||
try {
|
||||
const body = await req.json().catch(() => ({}));
|
||||
const parsed = settingsPayloadSchema.safeParse(body);
|
||||
if (!parsed.success) {
|
||||
return NextResponse.json({ ok: false, error: "Invalid settings payload" }, { status: 400 });
|
||||
}
|
||||
|
||||
const source = String(parsed.data.source ?? "control_tower");
|
||||
const timezone = parsed.data.timezone;
|
||||
const shiftSchedule = parsed.data.shiftSchedule;
|
||||
const thresholds = parsed.data.thresholds;
|
||||
const alerts = parsed.data.alerts;
|
||||
const defaults = parsed.data.defaults;
|
||||
const reasonCatalogRaw = parsed.data.reasonCatalog;
|
||||
const expectedVersion = parsed.data.version;
|
||||
const modules = parsed.data.modules;
|
||||
|
||||
|
||||
|
||||
if (
|
||||
timezone === undefined &&
|
||||
shiftSchedule === undefined &&
|
||||
thresholds === undefined &&
|
||||
alerts === undefined &&
|
||||
defaults === undefined &&
|
||||
reasonCatalogRaw === undefined &&
|
||||
modules === undefined
|
||||
|
||||
) {
|
||||
return NextResponse.json({ ok: false, error: "No settings provided" }, { status: 400 });
|
||||
}
|
||||
|
||||
if (shiftSchedule && !isPlainObject(shiftSchedule)) {
|
||||
return NextResponse.json({ ok: false, error: "shiftSchedule must be an object" }, { status: 400 });
|
||||
}
|
||||
if (thresholds !== undefined && !isPlainObject(thresholds)) {
|
||||
return NextResponse.json({ ok: false, error: "thresholds must be an object" }, { status: 400 });
|
||||
}
|
||||
if (alerts !== undefined && !isPlainObject(alerts)) {
|
||||
return NextResponse.json({ ok: false, error: "alerts must be an object" }, { status: 400 });
|
||||
}
|
||||
if (defaults !== undefined && !isPlainObject(defaults)) {
|
||||
return NextResponse.json({ ok: false, error: "defaults must be an object" }, { status: 400 });
|
||||
}
|
||||
const nextReasonCatalog =
|
||||
reasonCatalogRaw === undefined || reasonCatalogRaw === null
|
||||
? reasonCatalogRaw
|
||||
: normalizeReasonCatalog(reasonCatalogRaw);
|
||||
if (reasonCatalogRaw !== undefined && reasonCatalogRaw !== null && !nextReasonCatalog) {
|
||||
return NextResponse.json({ ok: false, error: "reasonCatalog must be a valid catalog payload" }, { status: 400 });
|
||||
}
|
||||
if (modules !== undefined && !isPlainObject(modules)) {
|
||||
return NextResponse.json({ ok: false, error: "Invalid modules payload" }, { status: 400 });
|
||||
}
|
||||
|
||||
const screenlessMode =
|
||||
modules && typeof (modules as any).screenlessMode === "boolean"
|
||||
? (modules as any).screenlessMode
|
||||
: undefined;
|
||||
|
||||
|
||||
|
||||
const shiftValidation = validateShiftFields(
|
||||
shiftSchedule?.shiftChangeCompensationMin,
|
||||
shiftSchedule?.lunchBreakMin
|
||||
);
|
||||
if (!shiftValidation.ok) {
|
||||
return NextResponse.json({ ok: false, error: shiftValidation.error }, { status: 400 });
|
||||
}
|
||||
|
||||
const overridesResult =
|
||||
shiftSchedule?.overrides !== undefined
|
||||
? validateShiftOverrides(shiftSchedule.overrides)
|
||||
: ({ ok: true, overrides: undefined } as const);
|
||||
if (!overridesResult.ok) {
|
||||
return NextResponse.json({ ok: false, error: overridesResult.error }, { status: 400 });
|
||||
}
|
||||
|
||||
const thresholdsValidation = validateThresholds(thresholds);
|
||||
if (!thresholdsValidation.ok) {
|
||||
return NextResponse.json({ ok: false, error: thresholdsValidation.error }, { status: 400 });
|
||||
}
|
||||
|
||||
let shiftRows: ValidShift[] | null = null;
|
||||
if (shiftSchedule?.shifts !== undefined) {
|
||||
const shiftResult = validateShiftSchedule(shiftSchedule.shifts);
|
||||
if (!shiftResult.ok) {
|
||||
return NextResponse.json({ ok: false, error: shiftResult.error }, { status: 400 });
|
||||
}
|
||||
shiftRows = shiftResult.shifts ?? [];
|
||||
}
|
||||
const shiftRowsSafe = shiftRows ?? [];
|
||||
|
||||
const updated = await prisma.$transaction(async (tx) => {
|
||||
const current = await ensureOrgSettings(tx, session.orgId, session.userId);
|
||||
if (!current?.settings) throw new Error("SETTINGS_NOT_FOUND");
|
||||
|
||||
if (expectedVersion != null && Number(expectedVersion) !== Number(current.settings.version)) {
|
||||
return { error: "VERSION_MISMATCH", currentVersion: current.settings.version } as const;
|
||||
}
|
||||
|
||||
const nextAlerts =
|
||||
alerts !== undefined ? { ...normalizeAlerts(current.settings.alertsJson), ...alerts } : undefined;
|
||||
const currentDefaultsRaw = isPlainObject(current.settings.defaultsJson)
|
||||
? (current.settings.defaultsJson as any)
|
||||
: {};
|
||||
const currentModulesRaw = isPlainObject(currentDefaultsRaw.modules) ? currentDefaultsRaw.modules : {};
|
||||
|
||||
// Merge defaults core (moldTotal, etc.)
|
||||
const nextDefaultsCore =
|
||||
defaults !== undefined ? { ...normalizeDefaults(currentDefaultsRaw), ...defaults } : undefined;
|
||||
|
||||
// Validate merged defaults
|
||||
if (nextDefaultsCore) {
|
||||
const dv = validateDefaults(nextDefaultsCore);
|
||||
if (!dv.ok) return { error: dv.error } as const;
|
||||
}
|
||||
|
||||
// Merge modules
|
||||
const nextModules =
|
||||
screenlessMode === undefined
|
||||
? currentModulesRaw
|
||||
: { ...currentModulesRaw, screenlessMode };
|
||||
|
||||
// Write defaultsJson if either defaults changed OR modules changed
|
||||
const shouldWriteDefaultsJson =
|
||||
!!nextDefaultsCore || screenlessMode !== undefined || reasonCatalogRaw !== undefined;
|
||||
|
||||
const nextDefaultsJson = shouldWriteDefaultsJson
|
||||
? { ...(nextDefaultsCore ?? normalizeDefaults(currentDefaultsRaw)), modules: nextModules }
|
||||
: undefined;
|
||||
|
||||
if (nextDefaultsJson && reasonCatalogRaw !== undefined) {
|
||||
const defaultsTarget = nextDefaultsJson as Record<string, unknown>;
|
||||
if (nextReasonCatalog === null) {
|
||||
delete defaultsTarget.reasonCatalog;
|
||||
} else if (nextReasonCatalog) {
|
||||
defaultsTarget.reasonCatalog = nextReasonCatalog;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
const updateData = stripUndefined({
|
||||
timezone: timezone !== undefined ? String(timezone) : undefined,
|
||||
shiftChangeCompMin:
|
||||
shiftSchedule?.shiftChangeCompensationMin !== undefined
|
||||
? Number(shiftSchedule.shiftChangeCompensationMin)
|
||||
: undefined,
|
||||
lunchBreakMin:
|
||||
shiftSchedule?.lunchBreakMin !== undefined ? Number(shiftSchedule.lunchBreakMin) : undefined,
|
||||
shiftScheduleOverridesJson:
|
||||
shiftSchedule?.overrides !== undefined
|
||||
? overridesResult.overrides === null
|
||||
? null
|
||||
: overridesResult.overrides
|
||||
: undefined,
|
||||
stoppageMultiplier:
|
||||
thresholds?.stoppageMultiplier !== undefined ? Number(thresholds.stoppageMultiplier) : undefined,
|
||||
macroStoppageMultiplier:
|
||||
thresholds?.macroStoppageMultiplier !== undefined
|
||||
? Number(thresholds.macroStoppageMultiplier)
|
||||
: undefined,
|
||||
oeeAlertThresholdPct:
|
||||
thresholds?.oeeAlertThresholdPct !== undefined ? Number(thresholds.oeeAlertThresholdPct) : undefined,
|
||||
performanceThresholdPct:
|
||||
thresholds?.performanceThresholdPct !== undefined
|
||||
? Number(thresholds.performanceThresholdPct)
|
||||
: undefined,
|
||||
qualitySpikeDeltaPct:
|
||||
thresholds?.qualitySpikeDeltaPct !== undefined ? Number(thresholds.qualitySpikeDeltaPct) : undefined,
|
||||
alertsJson: nextAlerts,
|
||||
defaultsJson: nextDefaultsJson,
|
||||
});
|
||||
|
||||
const hasShiftUpdate = shiftRows !== null;
|
||||
const hasSettingsUpdate = Object.keys(updateData).length > 0;
|
||||
|
||||
if (!hasShiftUpdate && !hasSettingsUpdate) {
|
||||
return { error: "No settings provided" } as const;
|
||||
}
|
||||
|
||||
const updateWithMeta = {
|
||||
...updateData,
|
||||
version: current.settings.version + 1,
|
||||
updatedBy: session.userId,
|
||||
};
|
||||
|
||||
await tx.orgSettings.update({
|
||||
where: { orgId: session.orgId },
|
||||
data: updateWithMeta,
|
||||
});
|
||||
|
||||
if (hasShiftUpdate) {
|
||||
await tx.orgShift.deleteMany({ where: { orgId: session.orgId } });
|
||||
if (shiftRowsSafe.length) {
|
||||
await tx.orgShift.createMany({
|
||||
data: shiftRowsSafe.map((s) => ({
|
||||
...s,
|
||||
orgId: session.orgId,
|
||||
})),
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
const refreshed = await tx.orgSettings.findUnique({
|
||||
where: { orgId: session.orgId },
|
||||
});
|
||||
if (!refreshed) throw new Error("SETTINGS_NOT_FOUND");
|
||||
const refreshedShifts = await tx.orgShift.findMany({
|
||||
where: { orgId: session.orgId },
|
||||
orderBy: { sortOrder: "asc" },
|
||||
});
|
||||
|
||||
await tx.settingsAudit.create({
|
||||
data: {
|
||||
orgId: session.orgId,
|
||||
actorId: session.userId,
|
||||
source,
|
||||
payloadJson: body,
|
||||
},
|
||||
});
|
||||
|
||||
return { settings: refreshed, shifts: refreshedShifts };
|
||||
});
|
||||
|
||||
if ("error" in updated && updated.error === "VERSION_MISMATCH") {
|
||||
return NextResponse.json(
|
||||
{ ok: false, error: "Version mismatch", currentVersion: updated.currentVersion },
|
||||
{ status: 409 }
|
||||
);
|
||||
}
|
||||
|
||||
if ("error" in updated) {
|
||||
return NextResponse.json({ ok: false, error: updated.error }, { status: 400 });
|
||||
}
|
||||
|
||||
const payload = buildSettingsPayload(updated.settings, updated.shifts ?? []);
|
||||
const updatedAt =
|
||||
typeof payload.updatedAt === "string"
|
||||
? payload.updatedAt
|
||||
: payload.updatedAt
|
||||
? payload.updatedAt.toISOString()
|
||||
: undefined;
|
||||
try {
|
||||
await publishSettingsUpdate({
|
||||
orgId: session.orgId,
|
||||
version: Number(payload.version ?? 0),
|
||||
source,
|
||||
updatedAt,
|
||||
});
|
||||
} catch (err) {
|
||||
console.warn("[settings PUT] MQTT publish failed", err);
|
||||
}
|
||||
const defaultsRaw = isPlainObject(updated.settings.defaultsJson) ? (updated.settings.defaultsJson as any) : {};
|
||||
const modulesRaw = isPlainObject(defaultsRaw.modules) ? defaultsRaw.modules : {};
|
||||
const modulesOut = { screenlessMode: modulesRaw.screenlessMode === true };
|
||||
|
||||
revalidateTag(`settings:${session.orgId}`, { expire: 0 });
|
||||
|
||||
return NextResponse.json({ ok: true, settings: { ...payload, modules: modulesOut } });
|
||||
|
||||
} catch (err) {
|
||||
console.error("[settings PUT] failed", err);
|
||||
const message = err instanceof Error ? err.message : "Internal error";
|
||||
return NextResponse.json({ ok: false, error: message }, { status: 500 });
|
||||
}
|
||||
}
|
||||
137
app/api/signup/route.ts
Normal file
137
app/api/signup/route.ts
Normal file
@@ -0,0 +1,137 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import bcrypt from "bcrypt";
|
||||
import { randomBytes } from "crypto";
|
||||
import { prisma } from "@/lib/prisma";
|
||||
import { DEFAULT_ALERTS, DEFAULT_DEFAULTS, DEFAULT_SHIFT } from "@/lib/settings";
|
||||
import { buildVerifyEmail, sendEmail } from "@/lib/email";
|
||||
import { getBaseUrl } from "@/lib/appUrl";
|
||||
import { logLine } from "@/lib/logger";
|
||||
import { z } from "zod";
|
||||
|
||||
const signupSchema = z.object({
|
||||
orgName: z.string().trim().min(1).max(120),
|
||||
name: z.string().trim().min(1).max(80),
|
||||
email: z.string().trim().min(1).max(254).email(),
|
||||
password: z.string().min(8).max(256),
|
||||
});
|
||||
|
||||
function slugify(input: string) {
|
||||
const trimmed = input.trim().toLowerCase();
|
||||
const slug = trimmed
|
||||
.replace(/[^a-z0-9]+/g, "-")
|
||||
.replace(/^-+|-+$/g, "");
|
||||
return slug || "org";
|
||||
}
|
||||
|
||||
export async function POST(req: Request) {
|
||||
const body = await req.json().catch(() => ({}));
|
||||
const parsed = signupSchema.safeParse(body);
|
||||
if (!parsed.success) {
|
||||
return NextResponse.json({ ok: false, error: "Invalid signup payload" }, { status: 400 });
|
||||
}
|
||||
const orgName = parsed.data.orgName;
|
||||
const name = parsed.data.name;
|
||||
const email = parsed.data.email.toLowerCase();
|
||||
const password = parsed.data.password;
|
||||
|
||||
const existing = await prisma.user.findUnique({ where: { email } });
|
||||
if (existing) {
|
||||
return NextResponse.json({ ok: false, error: "Email already in use" }, { status: 409 });
|
||||
}
|
||||
|
||||
const baseSlug = slugify(orgName);
|
||||
let slug = baseSlug;
|
||||
let counter = 1;
|
||||
while (await prisma.org.findUnique({ where: { slug } })) {
|
||||
counter += 1;
|
||||
slug = `${baseSlug}-${counter}`;
|
||||
}
|
||||
|
||||
const passwordHash = await bcrypt.hash(password, 10);
|
||||
const verificationToken = randomBytes(24).toString("hex");
|
||||
const verificationExpiresAt = new Date(Date.now() + 24 * 60 * 60 * 1000);
|
||||
|
||||
await prisma.$transaction(async (tx) => {
|
||||
const org = await tx.org.create({
|
||||
data: { name: orgName, slug },
|
||||
});
|
||||
|
||||
const user = await tx.user.create({
|
||||
data: {
|
||||
email,
|
||||
name,
|
||||
passwordHash,
|
||||
emailVerificationToken: verificationToken,
|
||||
emailVerificationExpiresAt: verificationExpiresAt,
|
||||
},
|
||||
});
|
||||
|
||||
await tx.orgUser.create({
|
||||
data: {
|
||||
orgId: org.id,
|
||||
userId: user.id,
|
||||
role: "OWNER",
|
||||
},
|
||||
});
|
||||
|
||||
await tx.orgSettings.create({
|
||||
data: {
|
||||
orgId: org.id,
|
||||
timezone: "UTC",
|
||||
shiftChangeCompMin: 10,
|
||||
lunchBreakMin: 30,
|
||||
stoppageMultiplier: 1.5,
|
||||
macroStoppageMultiplier: 5,
|
||||
oeeAlertThresholdPct: 90,
|
||||
performanceThresholdPct: 85,
|
||||
qualitySpikeDeltaPct: 5,
|
||||
alertsJson: DEFAULT_ALERTS,
|
||||
defaultsJson: DEFAULT_DEFAULTS,
|
||||
updatedBy: user.id,
|
||||
},
|
||||
});
|
||||
|
||||
await tx.orgShift.create({
|
||||
data: {
|
||||
orgId: org.id,
|
||||
name: DEFAULT_SHIFT.name,
|
||||
startTime: DEFAULT_SHIFT.start,
|
||||
endTime: DEFAULT_SHIFT.end,
|
||||
sortOrder: 1,
|
||||
enabled: true,
|
||||
},
|
||||
});
|
||||
|
||||
return { org, user };
|
||||
});
|
||||
|
||||
const baseUrl = getBaseUrl(req);
|
||||
const verifyUrl = `${baseUrl}/api/verify-email?token=${verificationToken}`;
|
||||
const appName = "MIS Control Tower";
|
||||
const emailContent = buildVerifyEmail({ appName, verifyUrl });
|
||||
|
||||
let emailSent = true;
|
||||
try {
|
||||
await sendEmail({
|
||||
to: email,
|
||||
subject: emailContent.subject,
|
||||
text: emailContent.text,
|
||||
html: emailContent.html,
|
||||
});
|
||||
} catch (err: unknown) {
|
||||
emailSent = false;
|
||||
const error = err as { message?: string; code?: string; response?: unknown; responseCode?: number };
|
||||
logLine("signup.verify_email.failed", {
|
||||
email,
|
||||
message: error?.message,
|
||||
code: error?.code,
|
||||
response: error?.response,
|
||||
responseCode: error?.responseCode,
|
||||
});
|
||||
}
|
||||
return NextResponse.json({
|
||||
ok: true,
|
||||
verificationRequired: true,
|
||||
emailSent,
|
||||
});
|
||||
}
|
||||
62
app/api/verify-email/route.ts
Normal file
62
app/api/verify-email/route.ts
Normal file
@@ -0,0 +1,62 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import { prisma } from "@/lib/prisma";
|
||||
import { buildSessionCookieOptions, COOKIE_NAME, SESSION_DAYS } from "@/lib/auth/sessionCookie";
|
||||
import { getBaseUrl } from "@/lib/appUrl";
|
||||
|
||||
export async function GET(req: Request) {
|
||||
const url = new URL(req.url);
|
||||
const token = url.searchParams.get("token");
|
||||
const wantsJson = req.headers.get("accept")?.includes("application/json");
|
||||
|
||||
if (!token) {
|
||||
return NextResponse.json({ ok: false, error: "Missing token" }, { status: 400 });
|
||||
}
|
||||
|
||||
const user = await prisma.user.findFirst({
|
||||
where: {
|
||||
emailVerificationToken: token,
|
||||
emailVerificationExpiresAt: { gt: new Date() },
|
||||
},
|
||||
});
|
||||
|
||||
if (!user) {
|
||||
return NextResponse.json({ ok: false, error: "Invalid or expired token" }, { status: 404 });
|
||||
}
|
||||
|
||||
await prisma.user.update({
|
||||
where: { id: user.id },
|
||||
data: {
|
||||
emailVerifiedAt: new Date(),
|
||||
emailVerificationToken: null,
|
||||
emailVerificationExpiresAt: null,
|
||||
},
|
||||
});
|
||||
|
||||
const membership = await prisma.orgUser.findFirst({
|
||||
where: { userId: user.id },
|
||||
orderBy: { createdAt: "asc" },
|
||||
});
|
||||
|
||||
if (!membership) {
|
||||
return NextResponse.json({ ok: false, error: "No organization found" }, { status: 403 });
|
||||
}
|
||||
|
||||
const expiresAt = new Date(Date.now() + SESSION_DAYS * 24 * 60 * 60 * 1000);
|
||||
const session = await prisma.session.create({
|
||||
data: {
|
||||
userId: user.id,
|
||||
orgId: membership.orgId,
|
||||
expiresAt,
|
||||
},
|
||||
});
|
||||
|
||||
if (wantsJson) {
|
||||
const res = NextResponse.json({ ok: true, next: "/machines" });
|
||||
res.cookies.set(COOKIE_NAME, session.id, buildSessionCookieOptions(req));
|
||||
return res;
|
||||
}
|
||||
|
||||
const res = NextResponse.redirect(new URL("/machines", getBaseUrl(req)));
|
||||
res.cookies.set(COOKIE_NAME, session.id, buildSessionCookieOptions(req));
|
||||
return res;
|
||||
}
|
||||
52
app/api/work-orders/machines/[machineId]/route.ts
Normal file
52
app/api/work-orders/machines/[machineId]/route.ts
Normal file
@@ -0,0 +1,52 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import type { NextRequest } from "next/server";
|
||||
import { prisma } from "@/lib/prisma";
|
||||
import { requireSession } from "@/lib/auth/requireSession";
|
||||
|
||||
export async function GET(
|
||||
req: NextRequest,
|
||||
{ params }: { params: Promise<{ machineId: string }> }
|
||||
) {
|
||||
const { machineId } = await params;
|
||||
|
||||
const session = await requireSession();
|
||||
let orgId: string | null = null;
|
||||
|
||||
if (session) {
|
||||
const machine = await prisma.machine.findFirst({
|
||||
where: { id: machineId, orgId: session.orgId },
|
||||
select: { id: true, orgId: true },
|
||||
});
|
||||
if (!machine) return NextResponse.json({ ok: false, error: "Not found" }, { status: 404 });
|
||||
orgId = machine.orgId;
|
||||
} else {
|
||||
const apiKey = req.headers.get("x-api-key");
|
||||
if (!apiKey) return NextResponse.json({ ok: false, error: "Unauthorized" }, { status: 401 });
|
||||
const machine = await prisma.machine.findFirst({
|
||||
where: { id: machineId, apiKey },
|
||||
select: { id: true, orgId: true },
|
||||
});
|
||||
if (!machine) return NextResponse.json({ ok: false, error: "Unauthorized" }, { status: 401 });
|
||||
orgId = machine.orgId;
|
||||
}
|
||||
|
||||
const rows = await prisma.machineWorkOrder.findMany({
|
||||
where: { machineId, orgId: orgId as string, status: { not: "DONE" } },
|
||||
orderBy: { createdAt: "desc" },
|
||||
});
|
||||
|
||||
return NextResponse.json({
|
||||
ok: true,
|
||||
machineId,
|
||||
workOrders: rows.map((row) => ({
|
||||
workOrderId: row.workOrderId,
|
||||
sku: row.sku,
|
||||
targetQty: row.targetQty,
|
||||
cycleTime: row.cycleTime,
|
||||
mold: row.mold,
|
||||
cavitiesTotal: row.cavitiesTotal,
|
||||
cavitiesActive: row.cavitiesActive,
|
||||
status: row.status,
|
||||
})),
|
||||
});
|
||||
}
|
||||
280
app/api/work-orders/route.ts
Normal file
280
app/api/work-orders/route.ts
Normal file
@@ -0,0 +1,280 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import type { NextRequest } from "next/server";
|
||||
import { prisma } from "@/lib/prisma";
|
||||
import { requireSession } from "@/lib/auth/requireSession";
|
||||
import { publishWorkOrdersUpdate } from "@/lib/mqtt";
|
||||
import { z } from "zod";
|
||||
|
||||
function canManage(role?: string | null) {
|
||||
return role === "OWNER" || role === "ADMIN";
|
||||
}
|
||||
|
||||
const MAX_WORK_ORDERS = 2000;
|
||||
const MAX_WORK_ORDER_ID_LENGTH = 64;
|
||||
const MAX_SKU_LENGTH = 64;
|
||||
const MAX_MOLD_LENGTH = 256;
|
||||
const MAX_TARGET_QTY = 2_000_000_000;
|
||||
const MAX_CYCLE_TIME = 86_400;
|
||||
const MAX_CAVITIES = 100_000;
|
||||
const WORK_ORDER_ID_RE = /^[A-Za-z0-9._-]+$/;
|
||||
|
||||
const uploadBodySchema = z.object({
|
||||
machineId: z.string().trim().min(1),
|
||||
workOrders: z.array(z.any()).optional(),
|
||||
orders: z.array(z.any()).optional(),
|
||||
workOrder: z.any().optional(),
|
||||
});
|
||||
|
||||
function cleanText(value: unknown, maxLen: number) {
|
||||
if (value === null || value === undefined) return null;
|
||||
const text = String(value).trim();
|
||||
if (!text) return null;
|
||||
const sanitized = text.replace(/[\u0000-\u001f\u007f]/g, "");
|
||||
if (!sanitized) return null;
|
||||
return sanitized.length > maxLen ? sanitized.slice(0, maxLen) : sanitized;
|
||||
}
|
||||
|
||||
function toIntOrNull(value: unknown) {
|
||||
if (value === null || value === undefined || value === "") return null;
|
||||
const n = Number(value);
|
||||
if (!Number.isFinite(n)) return null;
|
||||
return Math.trunc(n);
|
||||
}
|
||||
|
||||
function toFloatOrNull(value: unknown) {
|
||||
if (value === null || value === undefined || value === "") return null;
|
||||
const n = Number(value);
|
||||
if (!Number.isFinite(n)) return null;
|
||||
return n;
|
||||
}
|
||||
|
||||
type WorkOrderInput = {
|
||||
workOrderId: string;
|
||||
sku?: string | null;
|
||||
targetQty?: number | null;
|
||||
cycleTime?: number | null;
|
||||
mold?: string | null;
|
||||
cavitiesTotal?: number | null;
|
||||
cavitiesActive?: number | null;
|
||||
};
|
||||
|
||||
type RowIssue = {
|
||||
row: number;
|
||||
workOrderId: string | null;
|
||||
errors: string[];
|
||||
};
|
||||
|
||||
function normalizeWorkOrders(raw: unknown[]) {
|
||||
const seen = new Set<string>();
|
||||
const cleaned: WorkOrderInput[] = [];
|
||||
|
||||
for (const item of raw) {
|
||||
const record = item && typeof item === "object" ? (item as Record<string, unknown>) : {};
|
||||
const idRaw = cleanText(
|
||||
record.workOrderId ?? record.id ?? record.work_order_id,
|
||||
MAX_WORK_ORDER_ID_LENGTH
|
||||
);
|
||||
if (!idRaw || !WORK_ORDER_ID_RE.test(idRaw) || seen.has(idRaw)) continue;
|
||||
seen.add(idRaw);
|
||||
|
||||
const sku = cleanText(record.sku ?? record.SKU ?? null, MAX_SKU_LENGTH);
|
||||
const targetQtyRaw = toIntOrNull(
|
||||
record.targetQty ?? record.target_qty ?? record.target ?? record.targetQuantity
|
||||
);
|
||||
const cycleTimeRaw = toFloatOrNull(
|
||||
record.cycleTime ?? record.theoreticalCycleTime ?? record.theoretical_cycle_time ?? record.cycle_time
|
||||
);
|
||||
const targetQty =
|
||||
targetQtyRaw == null ? null : Math.min(Math.max(targetQtyRaw, 0), MAX_TARGET_QTY);
|
||||
const cycleTime =
|
||||
cycleTimeRaw == null ? null : Math.min(Math.max(cycleTimeRaw, 0), MAX_CYCLE_TIME);
|
||||
|
||||
const mold = cleanText(
|
||||
record.mold ?? record.moldId ?? record.mold_id ?? null,
|
||||
MAX_MOLD_LENGTH
|
||||
);
|
||||
const cavitiesTotalRaw = toIntOrNull(
|
||||
record.cavitiesTotal ??
|
||||
record.cavities_total ??
|
||||
record.totalCavities ??
|
||||
record.total_cavities
|
||||
);
|
||||
const cavitiesActiveRaw = toIntOrNull(
|
||||
record.cavitiesActive ??
|
||||
record.cavities_active ??
|
||||
record.activeCavities ??
|
||||
record.active_cavities
|
||||
);
|
||||
const cavitiesTotal =
|
||||
cavitiesTotalRaw == null
|
||||
? null
|
||||
: Math.min(Math.max(cavitiesTotalRaw, 0), MAX_CAVITIES);
|
||||
const cavitiesActive =
|
||||
cavitiesActiveRaw == null
|
||||
? null
|
||||
: Math.min(Math.max(cavitiesActiveRaw, 0), MAX_CAVITIES);
|
||||
|
||||
cleaned.push({
|
||||
workOrderId: idRaw,
|
||||
sku: sku ?? null,
|
||||
targetQty: targetQty ?? null,
|
||||
cycleTime: cycleTime ?? null,
|
||||
mold: mold ?? null,
|
||||
cavitiesTotal: cavitiesTotal ?? null,
|
||||
cavitiesActive: cavitiesActive ?? null,
|
||||
});
|
||||
}
|
||||
|
||||
return cleaned;
|
||||
}
|
||||
|
||||
// ✨ NUEVO: validación estricta del Excel
|
||||
// Cada fila debe tener mold (no vacío), cavitiesTotal (>=1), cavitiesActive (>=1, <=cavitiesTotal)
|
||||
// Si UNA SOLA fila falla, se rechaza el archivo completo (Opción A)
|
||||
function validateRows(rows: WorkOrderInput[], rawList: unknown[]): RowIssue[] {
|
||||
const issues: RowIssue[] = [];
|
||||
|
||||
// Validar lista cruda primero (si hay duplicados o IDs inválidos no llegaron a `cleaned`)
|
||||
// Pero aquí enfocamos en la validación de mold/cavidades sobre filas ya normalizadas.
|
||||
rows.forEach((row, idx) => {
|
||||
const errors: string[] = [];
|
||||
|
||||
// Mold requerido
|
||||
if (!row.mold || row.mold.length === 0) {
|
||||
errors.push("Mold is required");
|
||||
}
|
||||
|
||||
// Cavities Total requerido y >= 1
|
||||
if (row.cavitiesTotal == null) {
|
||||
errors.push("Total Cavities is required");
|
||||
} else if (row.cavitiesTotal < 1) {
|
||||
errors.push("Total Cavities must be at least 1");
|
||||
}
|
||||
|
||||
// Cavities Active requerido y >= 1
|
||||
if (row.cavitiesActive == null) {
|
||||
errors.push("Active Cavities is required");
|
||||
} else if (row.cavitiesActive < 1) {
|
||||
errors.push("Active Cavities must be at least 1");
|
||||
}
|
||||
|
||||
// Active <= Total
|
||||
if (
|
||||
row.cavitiesActive != null &&
|
||||
row.cavitiesTotal != null &&
|
||||
row.cavitiesActive > row.cavitiesTotal
|
||||
) {
|
||||
errors.push(
|
||||
`Active Cavities (${row.cavitiesActive}) cannot exceed Total Cavities (${row.cavitiesTotal})`
|
||||
);
|
||||
}
|
||||
|
||||
if (errors.length > 0) {
|
||||
issues.push({
|
||||
row: idx + 1, // 1-indexed para el operador
|
||||
workOrderId: row.workOrderId,
|
||||
errors,
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
return issues;
|
||||
}
|
||||
|
||||
export async function POST(req: NextRequest) {
|
||||
const session = await requireSession();
|
||||
if (!session) return NextResponse.json({ ok: false, error: "Unauthorized" }, { status: 401 });
|
||||
|
||||
const membership = await prisma.orgUser.findUnique({
|
||||
where: { orgId_userId: { orgId: session.orgId, userId: session.userId } },
|
||||
select: { role: true },
|
||||
});
|
||||
if (!canManage(membership?.role)) {
|
||||
return NextResponse.json({ ok: false, error: "Forbidden" }, { status: 403 });
|
||||
}
|
||||
|
||||
const body = await req.json().catch(() => ({}));
|
||||
const parsedBody = uploadBodySchema.safeParse(body);
|
||||
if (!parsedBody.success) {
|
||||
return NextResponse.json({ ok: false, error: "Invalid payload" }, { status: 400 });
|
||||
}
|
||||
|
||||
const machineId = String(parsedBody.data.machineId ?? "").trim();
|
||||
if (!machineId) {
|
||||
return NextResponse.json({ ok: false, error: "machineId is required" }, { status: 400 });
|
||||
}
|
||||
|
||||
const machine = await prisma.machine.findFirst({
|
||||
where: { id: machineId, orgId: session.orgId },
|
||||
select: { id: true },
|
||||
});
|
||||
if (!machine) return NextResponse.json({ ok: false, error: "Not found" }, { status: 404 });
|
||||
|
||||
const listRaw = Array.isArray(parsedBody.data.workOrders)
|
||||
? parsedBody.data.workOrders
|
||||
: Array.isArray(parsedBody.data.orders)
|
||||
? parsedBody.data.orders
|
||||
: parsedBody.data.workOrder
|
||||
? [parsedBody.data.workOrder]
|
||||
: [];
|
||||
|
||||
if (listRaw.length > MAX_WORK_ORDERS) {
|
||||
return NextResponse.json(
|
||||
{ ok: false, error: `Too many work orders (max ${MAX_WORK_ORDERS})` },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
const cleaned = normalizeWorkOrders(listRaw);
|
||||
if (!cleaned.length) {
|
||||
return NextResponse.json({ ok: false, error: "No valid work orders provided" }, { status: 400 });
|
||||
}
|
||||
|
||||
// ✨ NUEVO: validación estricta de mold/cavidades
|
||||
// Si una sola fila falla, rechazamos el archivo completo
|
||||
const issues = validateRows(cleaned, listRaw);
|
||||
if (issues.length > 0) {
|
||||
return NextResponse.json(
|
||||
{
|
||||
ok: false,
|
||||
error: "Validation failed",
|
||||
summary: `Excel rejected: ${issues.length} of ${cleaned.length} work order(s) have errors. All work orders must include mold name, total cavities, and active cavities. Fix and re-upload.`,
|
||||
issues,
|
||||
},
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
const created = await prisma.machineWorkOrder.createMany({
|
||||
data: cleaned.map((row) => ({
|
||||
orgId: session.orgId,
|
||||
machineId,
|
||||
workOrderId: row.workOrderId,
|
||||
sku: row.sku ?? null,
|
||||
targetQty: row.targetQty ?? null,
|
||||
cycleTime: row.cycleTime ?? null,
|
||||
mold: row.mold ?? null,
|
||||
cavitiesTotal: row.cavitiesTotal ?? null,
|
||||
cavitiesActive: row.cavitiesActive ?? null,
|
||||
status: "PENDING",
|
||||
})),
|
||||
skipDuplicates: true,
|
||||
});
|
||||
|
||||
try {
|
||||
await publishWorkOrdersUpdate({
|
||||
orgId: session.orgId,
|
||||
machineId,
|
||||
count: created.count,
|
||||
});
|
||||
} catch (err) {
|
||||
console.warn("[work orders POST] MQTT publish failed", err);
|
||||
}
|
||||
|
||||
return NextResponse.json({
|
||||
ok: true,
|
||||
machineId,
|
||||
inserted: created.count,
|
||||
total: cleaned.length,
|
||||
});
|
||||
}
|
||||
37
app/global-error.tsx
Normal file
37
app/global-error.tsx
Normal file
@@ -0,0 +1,37 @@
|
||||
"use client";
|
||||
|
||||
export default function GlobalError({
|
||||
error,
|
||||
reset,
|
||||
}: {
|
||||
error: Error & { digest?: string };
|
||||
reset: () => void;
|
||||
}) {
|
||||
return (
|
||||
<html lang="en">
|
||||
<body style={{ margin: 0, fontFamily: "system-ui, sans-serif", background: "#0a0a0a", color: "#e5e5e5", minHeight: "100vh", display: "flex", alignItems: "center", justifyContent: "center" }}>
|
||||
<div style={{ textAlign: "center", padding: "2rem", maxWidth: "28rem" }}>
|
||||
<h1 style={{ fontSize: "1.25rem", fontWeight: 600, marginBottom: "0.5rem" }}>Something went wrong</h1>
|
||||
<p style={{ fontSize: "0.875rem", color: "#a3a3a3", marginBottom: "1.5rem" }}>
|
||||
An unexpected error occurred. Please try again.
|
||||
</p>
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => reset()}
|
||||
style={{
|
||||
padding: "0.5rem 1rem",
|
||||
fontSize: "0.875rem",
|
||||
borderRadius: "0.5rem",
|
||||
border: "1px solid rgba(255,255,255,0.2)",
|
||||
background: "rgba(255,255,255,0.05)",
|
||||
color: "#e5e5e5",
|
||||
cursor: "pointer",
|
||||
}}
|
||||
>
|
||||
Try again
|
||||
</button>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
);
|
||||
}
|
||||
161
app/globals.css
161
app/globals.css
@@ -1,31 +1,168 @@
|
||||
@import "tailwindcss";
|
||||
|
||||
:root {
|
||||
--background: #ffffff;
|
||||
--foreground: #171717;
|
||||
color-scheme: dark;
|
||||
--font-geist-sans: "Segoe UI", system-ui, sans-serif;
|
||||
--font-geist-mono: ui-monospace, SFMono-Regular, Menlo, monospace;
|
||||
--app-bg: #0b0f14;
|
||||
--app-surface: rgba(255, 255, 255, 0.05);
|
||||
--app-surface-2: rgba(255, 255, 255, 0.08);
|
||||
--app-surface-3: rgba(0, 0, 0, 0.28);
|
||||
--app-surface-4: rgba(0, 0, 0, 0.42);
|
||||
--app-border: rgba(148, 163, 184, 0.18);
|
||||
--app-text: #e5e7eb;
|
||||
--app-text-strong: #f8fafc;
|
||||
--app-text-muted: #94a3b8;
|
||||
--app-text-subtle: #6b7280;
|
||||
--app-text-faint: #475569;
|
||||
--app-text-on-accent: #0b0f14;
|
||||
|
||||
--app-good-text: #6ee7b7;
|
||||
--app-good-bg: rgba(34, 197, 94, 0.18);
|
||||
--app-good-border: rgba(34, 197, 94, 0.28);
|
||||
--app-good-solid: #34d399;
|
||||
|
||||
--app-warn-text: #facc15;
|
||||
--app-warn-bg: rgba(250, 204, 21, 0.18);
|
||||
--app-warn-border: rgba(250, 204, 21, 0.32);
|
||||
|
||||
--app-bad-text: #f87171;
|
||||
--app-bad-bg: rgba(248, 113, 113, 0.18);
|
||||
--app-bad-border: rgba(248, 113, 113, 0.32);
|
||||
|
||||
--app-info-text: #7ab8ff;
|
||||
--app-info-bg: rgba(59, 130, 246, 0.18);
|
||||
--app-info-border: rgba(59, 130, 246, 0.3);
|
||||
|
||||
--app-overlay: rgba(3, 6, 12, 0.65);
|
||||
--app-modal-bg: rgba(9, 13, 19, 0.92);
|
||||
|
||||
--app-chart-grid: rgba(148, 163, 184, 0.2);
|
||||
--app-chart-tick: #9ca3af;
|
||||
--app-chart-tooltip-bg: rgba(2, 6, 23, 0.88);
|
||||
--app-chart-tooltip-border: rgba(148, 163, 184, 0.25);
|
||||
--app-chart-label: #f8fafc;
|
||||
--app-chart-shadow: 0 0 30px rgba(2, 6, 23, 0.6);
|
||||
}
|
||||
|
||||
@theme inline {
|
||||
--color-background: var(--background);
|
||||
--color-foreground: var(--foreground);
|
||||
--color-background: var(--app-bg);
|
||||
--color-foreground: var(--app-text);
|
||||
--font-sans: var(--font-geist-sans);
|
||||
--font-mono: var(--font-geist-mono);
|
||||
}
|
||||
|
||||
@media (prefers-color-scheme: dark) {
|
||||
:root {
|
||||
--background: #0a0a0a;
|
||||
--foreground: #ededed;
|
||||
}
|
||||
:root[data-theme="light"] {
|
||||
color-scheme: light;
|
||||
--app-bg: #f4f6f9;
|
||||
--app-surface: #ffffff;
|
||||
--app-surface-2: #eef2f6;
|
||||
--app-surface-3: #e7ecf2;
|
||||
--app-surface-4: #dde3ea;
|
||||
--app-border: rgba(15, 23, 42, 0.12);
|
||||
--app-text: #1f2937;
|
||||
--app-text-strong: #0f172a;
|
||||
--app-text-muted: #4b5563;
|
||||
--app-text-subtle: #6b7280;
|
||||
--app-text-faint: #8b95a3;
|
||||
--app-text-on-accent: #0f172a;
|
||||
|
||||
--app-good-text: #0f7a3e;
|
||||
--app-good-bg: rgba(34, 197, 94, 0.16);
|
||||
--app-good-border: rgba(34, 197, 94, 0.3);
|
||||
--app-good-solid: #22c55e;
|
||||
|
||||
--app-warn-text: #a16207;
|
||||
--app-warn-bg: rgba(234, 179, 8, 0.18);
|
||||
--app-warn-border: rgba(234, 179, 8, 0.36);
|
||||
|
||||
--app-bad-text: #b91c1c;
|
||||
--app-bad-bg: rgba(239, 68, 68, 0.16);
|
||||
--app-bad-border: rgba(239, 68, 68, 0.34);
|
||||
|
||||
--app-info-text: #1d4ed8;
|
||||
--app-info-bg: rgba(59, 130, 246, 0.16);
|
||||
--app-info-border: rgba(59, 130, 246, 0.3);
|
||||
|
||||
--app-overlay: rgba(15, 23, 42, 0.45);
|
||||
--app-modal-bg: rgba(255, 255, 255, 0.92);
|
||||
|
||||
--app-chart-grid: rgba(15, 23, 42, 0.12);
|
||||
--app-chart-tick: #6b7280;
|
||||
--app-chart-tooltip-bg: #ffffff;
|
||||
--app-chart-tooltip-border: rgba(15, 23, 42, 0.16);
|
||||
--app-chart-label: #0f172a;
|
||||
--app-chart-shadow: 0 0 24px rgba(15, 23, 42, 0.12);
|
||||
}
|
||||
|
||||
body {
|
||||
background: var(--background);
|
||||
color: var(--foreground);
|
||||
font-family: Arial, Helvetica, sans-serif;
|
||||
background: var(--app-bg);
|
||||
color: var(--app-text);
|
||||
font-family: var(--font-geist-sans), "Segoe UI", system-ui, sans-serif;
|
||||
}
|
||||
|
||||
/* Hide scrollbar but keep scrolling */
|
||||
.no-scrollbar::-webkit-scrollbar { display: none; }
|
||||
.no-scrollbar { -ms-overflow-style: none; scrollbar-width: none; }
|
||||
|
||||
/* Theme-friendly overrides for common utility classes */
|
||||
.text-white { color: var(--app-text-strong) !important; }
|
||||
.text-black { color: var(--app-text-on-accent) !important; }
|
||||
.text-zinc-200 { color: var(--app-text) !important; }
|
||||
.text-zinc-300 { color: var(--app-text-muted) !important; }
|
||||
.text-zinc-400 { color: var(--app-text-subtle) !important; }
|
||||
.text-zinc-500 { color: var(--app-text-faint) !important; }
|
||||
.text-emerald-100,
|
||||
.text-emerald-200,
|
||||
.text-emerald-300 { color: var(--app-good-text) !important; }
|
||||
.text-yellow-300 { color: var(--app-warn-text) !important; }
|
||||
.text-red-200,
|
||||
.text-red-300,
|
||||
.text-red-400 { color: var(--app-bad-text) !important; }
|
||||
.text-blue-300 { color: var(--app-info-text) !important; }
|
||||
.text-orange-300 { color: var(--app-warn-text) !important; }
|
||||
.text-rose-300 { color: var(--app-bad-text) !important; }
|
||||
|
||||
.bg-black { background-color: var(--app-bg) !important; }
|
||||
.bg-black\/20 { background-color: var(--app-surface-2) !important; }
|
||||
.bg-black\/25 { background-color: var(--app-surface-3) !important; }
|
||||
.bg-black\/30 { background-color: var(--app-surface-3) !important; }
|
||||
.bg-black\/40 { background-color: var(--app-surface-4) !important; }
|
||||
.bg-black\/70 { background-color: var(--app-overlay) !important; }
|
||||
.bg-zinc-950\/80,
|
||||
.bg-zinc-950\/95 { background-color: var(--app-modal-bg) !important; }
|
||||
.bg-white\/5 { background-color: var(--app-surface) !important; }
|
||||
.bg-white\/10 { background-color: var(--app-surface-2) !important; }
|
||||
|
||||
.border-white\/10,
|
||||
.border-white\/5 { border-color: var(--app-border) !important; }
|
||||
.border-emerald-500\/20,
|
||||
.border-emerald-400\/40,
|
||||
.border-emerald-500\/30 { border-color: var(--app-good-border) !important; }
|
||||
.border-red-500\/20,
|
||||
.border-red-500\/30 { border-color: var(--app-bad-border) !important; }
|
||||
.border-yellow-500\/20,
|
||||
.border-orange-500\/20 { border-color: var(--app-warn-border) !important; }
|
||||
.border-rose-500\/20 { border-color: var(--app-bad-border) !important; }
|
||||
.border-blue-500\/20 { border-color: var(--app-info-border) !important; }
|
||||
|
||||
.bg-emerald-500\/10,
|
||||
.bg-emerald-500\/15,
|
||||
.bg-emerald-500\/20,
|
||||
.bg-emerald-500\/30 { background-color: var(--app-good-bg) !important; }
|
||||
.bg-emerald-400 { background-color: var(--app-good-solid) !important; }
|
||||
.bg-yellow-500\/15 { background-color: var(--app-warn-bg) !important; }
|
||||
.bg-red-500\/10,
|
||||
.bg-red-500\/15,
|
||||
.bg-red-500\/20 { background-color: var(--app-bad-bg) !important; }
|
||||
.bg-blue-500\/15 { background-color: var(--app-info-bg) !important; }
|
||||
.bg-orange-500\/15 { background-color: var(--app-warn-bg) !important; }
|
||||
.bg-rose-500\/15 { background-color: var(--app-bad-bg) !important; }
|
||||
|
||||
.placeholder\:text-zinc-500::placeholder { color: var(--app-text-faint) !important; }
|
||||
|
||||
.hover\:bg-white\/5:hover { background-color: var(--app-surface) !important; }
|
||||
.hover\:bg-white\/10:hover { background-color: var(--app-surface-2) !important; }
|
||||
.hover\:bg-emerald-500\/30:hover { background-color: var(--app-good-bg) !important; }
|
||||
.hover\:bg-red-500\/20:hover { background-color: var(--app-bad-bg) !important; }
|
||||
.hover\:text-white:hover { color: var(--app-text-strong) !important; }
|
||||
|
||||
155
app/invite/[token]/InviteAcceptForm.tsx
Normal file
155
app/invite/[token]/InviteAcceptForm.tsx
Normal file
@@ -0,0 +1,155 @@
|
||||
"use client";
|
||||
|
||||
import { useEffect, useState } from "react";
|
||||
import { useRouter } from "next/navigation";
|
||||
import { useI18n } from "@/lib/i18n/useI18n";
|
||||
|
||||
type InviteInfo = {
|
||||
email: string;
|
||||
role: string;
|
||||
org: { id: string; name: string; slug: string };
|
||||
expiresAt: string;
|
||||
};
|
||||
|
||||
type InviteAcceptFormProps = {
|
||||
token: string;
|
||||
initialInvite?: InviteInfo | null;
|
||||
initialError?: string | null;
|
||||
};
|
||||
|
||||
export default function InviteAcceptForm({
|
||||
token,
|
||||
initialInvite = null,
|
||||
initialError = null,
|
||||
}: InviteAcceptFormProps) {
|
||||
const router = useRouter();
|
||||
const { t } = useI18n();
|
||||
const cleanedToken = token.trim();
|
||||
const [invite, setInvite] = useState<InviteInfo | null>(initialInvite);
|
||||
const [loading, setLoading] = useState(!initialInvite && !initialError);
|
||||
const [submitting, setSubmitting] = useState(false);
|
||||
const [error, setError] = useState<string | null>(initialError);
|
||||
const [name, setName] = useState("");
|
||||
const [password, setPassword] = useState("");
|
||||
|
||||
useEffect(() => {
|
||||
if (initialInvite || initialError) {
|
||||
setLoading(false);
|
||||
return;
|
||||
}
|
||||
|
||||
let alive = true;
|
||||
async function loadInvite() {
|
||||
setLoading(true);
|
||||
setError(null);
|
||||
try {
|
||||
const res = await fetch(`/api/invites/${encodeURIComponent(cleanedToken)}`, {
|
||||
cache: "no-store",
|
||||
});
|
||||
const data = await res.json().catch(() => ({}));
|
||||
if (!res.ok || !data.ok) {
|
||||
throw new Error(data.error || t("invite.error.notFound"));
|
||||
}
|
||||
if (alive) setInvite(data.invite);
|
||||
} catch (err: unknown) {
|
||||
const message = err instanceof Error ? err.message : null;
|
||||
if (alive) setError(message || t("invite.error.notFound"));
|
||||
} finally {
|
||||
if (alive) setLoading(false);
|
||||
}
|
||||
}
|
||||
|
||||
loadInvite();
|
||||
return () => {
|
||||
alive = false;
|
||||
};
|
||||
}, [cleanedToken, initialInvite, initialError, t]);
|
||||
|
||||
async function onSubmit(e: React.FormEvent) {
|
||||
e.preventDefault();
|
||||
setError(null);
|
||||
setSubmitting(true);
|
||||
try {
|
||||
const res = await fetch(`/api/invites/${encodeURIComponent(cleanedToken)}`, {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ name, password }),
|
||||
});
|
||||
const data = await res.json().catch(() => ({}));
|
||||
if (!res.ok || !data.ok) {
|
||||
throw new Error(data.error || t("invite.error.acceptFailed"));
|
||||
}
|
||||
router.push("/machines");
|
||||
router.refresh();
|
||||
} catch (err: unknown) {
|
||||
const message = err instanceof Error ? err.message : null;
|
||||
setError(message || t("invite.error.acceptFailed"));
|
||||
} finally {
|
||||
setSubmitting(false);
|
||||
}
|
||||
}
|
||||
|
||||
if (loading) {
|
||||
return (
|
||||
<div className="min-h-screen bg-black flex items-center justify-center p-6 text-zinc-300">
|
||||
{t("invite.loading")}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
if (!invite) {
|
||||
return (
|
||||
<div className="min-h-screen bg-black flex items-center justify-center p-6">
|
||||
<div className="max-w-md rounded-2xl border border-red-500/30 bg-red-500/10 p-6 text-sm text-red-200">
|
||||
{error || t("invite.notFound")}
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="min-h-screen bg-black flex items-center justify-center p-6">
|
||||
<form onSubmit={onSubmit} className="w-full max-w-lg rounded-2xl border border-white/10 bg-white/5 p-8">
|
||||
<h1 className="text-2xl font-semibold text-white">
|
||||
{t("invite.joinTitle", { org: invite.org.name })}
|
||||
</h1>
|
||||
<p className="mt-1 text-sm text-zinc-400">
|
||||
{t("invite.acceptCopy", { email: invite.email, role: invite.role })}
|
||||
</p>
|
||||
|
||||
<div className="mt-6 space-y-4">
|
||||
<div>
|
||||
<label className="text-sm text-zinc-300">{t("invite.yourName")}</label>
|
||||
<input
|
||||
className="mt-1 w-full rounded-xl border border-white/10 bg-black/40 px-4 py-3 text-white outline-none"
|
||||
value={name}
|
||||
onChange={(e) => setName(e.target.value)}
|
||||
autoComplete="name"
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<label className="text-sm text-zinc-300">{t("invite.password")}</label>
|
||||
<input
|
||||
type="password"
|
||||
className="mt-1 w-full rounded-xl border border-white/10 bg-black/40 px-4 py-3 text-white outline-none"
|
||||
value={password}
|
||||
onChange={(e) => setPassword(e.target.value)}
|
||||
autoComplete="new-password"
|
||||
/>
|
||||
</div>
|
||||
|
||||
{error && <div className="text-sm text-red-400">{error}</div>}
|
||||
|
||||
<button
|
||||
type="submit"
|
||||
disabled={submitting}
|
||||
className="mt-2 w-full rounded-xl bg-emerald-400 py-3 font-semibold text-black disabled:opacity-70"
|
||||
>
|
||||
{submitting ? t("invite.submit.loading") : t("invite.submit.default")}
|
||||
</button>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
52
app/invite/[token]/page.tsx
Normal file
52
app/invite/[token]/page.tsx
Normal file
@@ -0,0 +1,52 @@
|
||||
import { cookies } from "next/headers";
|
||||
import { redirect } from "next/navigation";
|
||||
import { prisma } from "@/lib/prisma";
|
||||
import InviteAcceptForm from "./InviteAcceptForm";
|
||||
|
||||
export default async function InvitePage({ params }: { params: Promise<{ token: string }> }) {
|
||||
const session = (await cookies()).get("mis_session")?.value;
|
||||
if (session) {
|
||||
redirect("/machines");
|
||||
}
|
||||
|
||||
const { token: rawToken } = await params;
|
||||
const token = String(rawToken || "").trim().toLowerCase();
|
||||
let invite = null;
|
||||
let error: string | null = null;
|
||||
|
||||
if (!token) {
|
||||
error = "Invite not found";
|
||||
} else {
|
||||
invite = await prisma.orgInvite.findFirst({
|
||||
where: {
|
||||
token,
|
||||
revokedAt: null,
|
||||
acceptedAt: null,
|
||||
expiresAt: { gt: new Date() },
|
||||
},
|
||||
include: {
|
||||
org: { select: { id: true, name: true, slug: true } },
|
||||
},
|
||||
});
|
||||
if (!invite) {
|
||||
error = "Invite not found";
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<InviteAcceptForm
|
||||
token={token}
|
||||
initialInvite={
|
||||
invite
|
||||
? {
|
||||
email: invite.email,
|
||||
role: invite.role,
|
||||
org: invite.org,
|
||||
expiresAt: invite.expiresAt.toISOString(),
|
||||
}
|
||||
: null
|
||||
}
|
||||
initialError={error}
|
||||
/>
|
||||
);
|
||||
}
|
||||
@@ -1,19 +1,25 @@
|
||||
import type { Metadata } from "next";
|
||||
import { Geist, Geist_Mono } from "next/font/google";
|
||||
import { cookies } from "next/headers";
|
||||
import "./globals.css";
|
||||
import { prisma } from "@/lib/prisma";
|
||||
|
||||
const geistSans = Geist({ variable: "--font-geist-sans", subsets: ["latin"] });
|
||||
const geistMono = Geist_Mono({ variable: "--font-geist-mono", subsets: ["latin"] });
|
||||
|
||||
export const metadata: Metadata = {
|
||||
export async function generateMetadata(): Promise<Metadata> {
|
||||
return {
|
||||
title: "MIS Control Tower",
|
||||
description: "MaliounTech Industrial Suite",
|
||||
};
|
||||
};
|
||||
}
|
||||
|
||||
export default async function RootLayout({ children }: { children: React.ReactNode }) {
|
||||
const cookieJar = await cookies();
|
||||
const themeCookie = cookieJar.get("mis_theme")?.value;
|
||||
const localeCookie = cookieJar.get("mis_locale")?.value;
|
||||
const theme = themeCookie === "light" ? "light" : "dark";
|
||||
const locale = localeCookie === "es-MX" ? "es-MX" : "en";
|
||||
|
||||
export default function RootLayout({ children }: { children: React.ReactNode }) {
|
||||
return (
|
||||
<html lang="en">
|
||||
<body className={`${geistSans.variable} ${geistMono.variable} antialiased`}>
|
||||
<html lang={locale} data-theme={theme}>
|
||||
<body className="antialiased">
|
||||
{children}
|
||||
</body>
|
||||
</html>
|
||||
|
||||
@@ -2,11 +2,13 @@
|
||||
|
||||
import { useSearchParams, useRouter } from "next/navigation";
|
||||
import { useState } from "react";
|
||||
import { useI18n } from "@/lib/i18n/useI18n";
|
||||
|
||||
export default function LoginForm() {
|
||||
const router = useRouter();
|
||||
const searchParams = useSearchParams();
|
||||
const next = searchParams.get("next") || "/machines";
|
||||
const { t } = useI18n();
|
||||
|
||||
const [email, setEmail] = useState("");
|
||||
const [password, setPassword] = useState("");
|
||||
@@ -27,14 +29,15 @@ export default function LoginForm() {
|
||||
|
||||
const data = await res.json().catch(() => ({}));
|
||||
if (!res.ok || !data.ok) {
|
||||
setErr(data.error || "Login failed");
|
||||
setErr(data.error || t("login.error.default"));
|
||||
return;
|
||||
}
|
||||
|
||||
router.push(next);
|
||||
router.refresh();
|
||||
} catch (e: any) {
|
||||
setErr(e?.message || "Network error");
|
||||
} catch (e: unknown) {
|
||||
const message = e instanceof Error ? e.message : null;
|
||||
setErr(message || t("login.error.network"));
|
||||
} finally {
|
||||
setLoading(false);
|
||||
}
|
||||
@@ -43,12 +46,12 @@ export default function LoginForm() {
|
||||
return (
|
||||
<div className="min-h-screen bg-black flex items-center justify-center p-6">
|
||||
<form onSubmit={onSubmit} className="w-full max-w-md rounded-2xl border border-white/10 bg-white/5 p-8">
|
||||
<h1 className="text-2xl font-semibold text-white">Control Tower</h1>
|
||||
<p className="mt-1 text-sm text-zinc-400">Sign in to your organization</p>
|
||||
<h1 className="text-2xl font-semibold text-white">{t("login.title")}</h1>
|
||||
<p className="mt-1 text-sm text-zinc-400">{t("login.subtitle")}</p>
|
||||
|
||||
<div className="mt-6 space-y-4">
|
||||
<div>
|
||||
<label className="text-sm text-zinc-300">Email</label>
|
||||
<label className="text-sm text-zinc-300">{t("login.email")}</label>
|
||||
<input
|
||||
className="mt-1 w-full rounded-xl border border-white/10 bg-black/40 px-4 py-3 text-white outline-none"
|
||||
value={email}
|
||||
@@ -58,7 +61,7 @@ export default function LoginForm() {
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<label className="text-sm text-zinc-300">Password</label>
|
||||
<label className="text-sm text-zinc-300">{t("login.password")}</label>
|
||||
<input
|
||||
type="password"
|
||||
className="mt-1 w-full rounded-xl border border-white/10 bg-black/40 px-4 py-3 text-white outline-none"
|
||||
@@ -75,10 +78,15 @@ export default function LoginForm() {
|
||||
disabled={loading}
|
||||
className="mt-2 w-full rounded-xl bg-emerald-400 py-3 font-semibold text-black disabled:opacity-70"
|
||||
>
|
||||
{loading ? "Signing in..." : "Login"}
|
||||
{loading ? t("login.submit.loading") : t("login.submit.default")}
|
||||
</button>
|
||||
|
||||
<div className="text-xs text-zinc-500">(Dev mode) This will be replaced with JWT auth later.</div>
|
||||
<div className="text-xs text-zinc-500">
|
||||
{t("login.newHere")}{" "}
|
||||
<a href="/signup" className="text-emerald-300 hover:text-emerald-200">
|
||||
{t("login.createAccount")}
|
||||
</a>
|
||||
</div>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
|
||||
@@ -6,13 +6,14 @@ import LoginForm from "./LoginForm"; // adjust path if needed
|
||||
export default async function LoginPage({
|
||||
searchParams,
|
||||
}: {
|
||||
searchParams?: { next?: string };
|
||||
searchParams?: Promise<{ next?: string }>;
|
||||
}) {
|
||||
const session = (await cookies()).get("mis_session")?.value;
|
||||
|
||||
// If already logged in, send to next or machines
|
||||
if (session) {
|
||||
const next = searchParams?.next || "/machines";
|
||||
const params = searchParams ? await searchParams : {};
|
||||
const next = params?.next || "/machines";
|
||||
redirect(next);
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { redirect } from "next/navigation";
|
||||
|
||||
export default function Home() {
|
||||
redirect("/machines");
|
||||
redirect("/recap");
|
||||
}
|
||||
|
||||
141
app/signup/SignupForm.tsx
Normal file
141
app/signup/SignupForm.tsx
Normal file
@@ -0,0 +1,141 @@
|
||||
"use client";
|
||||
|
||||
import { useState } from "react";
|
||||
import { useI18n } from "@/lib/i18n/useI18n";
|
||||
|
||||
export default function SignupForm() {
|
||||
const { t } = useI18n();
|
||||
const [orgName, setOrgName] = useState("");
|
||||
const [name, setName] = useState("");
|
||||
const [email, setEmail] = useState("");
|
||||
const [password, setPassword] = useState("");
|
||||
const [loading, setLoading] = useState(false);
|
||||
const [err, setErr] = useState<string | null>(null);
|
||||
const [verificationSent, setVerificationSent] = useState(false);
|
||||
const [emailSent, setEmailSent] = useState(true);
|
||||
|
||||
async function onSubmit(e: React.FormEvent) {
|
||||
e.preventDefault();
|
||||
setErr(null);
|
||||
setLoading(true);
|
||||
|
||||
try {
|
||||
const res = await fetch("/api/signup", {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ orgName, name, email, password }),
|
||||
});
|
||||
|
||||
const data = await res.json().catch(() => ({}));
|
||||
if (!res.ok || !data.ok) {
|
||||
setErr(data.error || t("signup.error.default"));
|
||||
return;
|
||||
}
|
||||
|
||||
setVerificationSent(true);
|
||||
setEmailSent(data.emailSent !== false);
|
||||
} catch (e: unknown) {
|
||||
const message = e instanceof Error ? e.message : null;
|
||||
setErr(message || t("signup.error.network"));
|
||||
} finally {
|
||||
setLoading(false);
|
||||
}
|
||||
}
|
||||
|
||||
if (verificationSent) {
|
||||
return (
|
||||
<div className="min-h-screen bg-black flex items-center justify-center p-6">
|
||||
<div className="w-full max-w-lg rounded-2xl border border-white/10 bg-white/5 p-8">
|
||||
<h1 className="text-2xl font-semibold text-white">{t("signup.verify.title")}</h1>
|
||||
<p className="mt-2 text-sm text-zinc-300">
|
||||
{t("signup.verify.sent", { email: email || t("common.na") })}
|
||||
</p>
|
||||
{!emailSent && (
|
||||
<div className="mt-3 rounded-xl border border-red-500/30 bg-red-500/10 p-3 text-xs text-red-200">
|
||||
{t("signup.verify.failed")}
|
||||
</div>
|
||||
)}
|
||||
<div className="mt-4 text-xs text-zinc-500">{t("signup.verify.notice")}</div>
|
||||
<div className="mt-6">
|
||||
<a
|
||||
href="/login"
|
||||
className="inline-flex rounded-xl border border-white/10 bg-white/5 px-4 py-2 text-sm text-white hover:bg-white/10"
|
||||
>
|
||||
{t("signup.verify.back")}
|
||||
</a>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="min-h-screen bg-black flex items-center justify-center p-6">
|
||||
<form onSubmit={onSubmit} className="w-full max-w-lg rounded-2xl border border-white/10 bg-white/5 p-8">
|
||||
<h1 className="text-2xl font-semibold text-white">{t("signup.title")}</h1>
|
||||
<p className="mt-1 text-sm text-zinc-400">{t("signup.subtitle")}</p>
|
||||
|
||||
<div className="mt-6 space-y-4">
|
||||
<div>
|
||||
<label className="text-sm text-zinc-300">{t("signup.orgName")}</label>
|
||||
<input
|
||||
className="mt-1 w-full rounded-xl border border-white/10 bg-black/40 px-4 py-3 text-white outline-none"
|
||||
value={orgName}
|
||||
onChange={(e) => setOrgName(e.target.value)}
|
||||
autoComplete="organization"
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div className="grid grid-cols-1 gap-4 md:grid-cols-2">
|
||||
<div>
|
||||
<label className="text-sm text-zinc-300">{t("signup.yourName")}</label>
|
||||
<input
|
||||
className="mt-1 w-full rounded-xl border border-white/10 bg-black/40 px-4 py-3 text-white outline-none"
|
||||
value={name}
|
||||
onChange={(e) => setName(e.target.value)}
|
||||
autoComplete="name"
|
||||
/>
|
||||
</div>
|
||||
<div>
|
||||
<label className="text-sm text-zinc-300">{t("signup.email")}</label>
|
||||
<input
|
||||
className="mt-1 w-full rounded-xl border border-white/10 bg-black/40 px-4 py-3 text-white outline-none"
|
||||
value={email}
|
||||
onChange={(e) => setEmail(e.target.value)}
|
||||
autoComplete="email"
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<label className="text-sm text-zinc-300">{t("signup.password")}</label>
|
||||
<input
|
||||
type="password"
|
||||
className="mt-1 w-full rounded-xl border border-white/10 bg-black/40 px-4 py-3 text-white outline-none"
|
||||
value={password}
|
||||
onChange={(e) => setPassword(e.target.value)}
|
||||
autoComplete="new-password"
|
||||
/>
|
||||
</div>
|
||||
|
||||
{err && <div className="text-sm text-red-400">{err}</div>}
|
||||
|
||||
<button
|
||||
type="submit"
|
||||
disabled={loading}
|
||||
className="mt-2 w-full rounded-xl bg-emerald-400 py-3 font-semibold text-black disabled:opacity-70"
|
||||
>
|
||||
{loading ? t("signup.submit.loading") : t("signup.submit.default")}
|
||||
</button>
|
||||
|
||||
<div className="text-xs text-zinc-500">
|
||||
{t("signup.alreadyHave")}{" "}
|
||||
<a href="/login" className="text-emerald-300 hover:text-emerald-200">
|
||||
{t("signup.signIn")}
|
||||
</a>
|
||||
</div>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
12
app/signup/page.tsx
Normal file
12
app/signup/page.tsx
Normal file
@@ -0,0 +1,12 @@
|
||||
import { cookies } from "next/headers";
|
||||
import { redirect } from "next/navigation";
|
||||
import SignupForm from "./SignupForm";
|
||||
|
||||
export default async function SignupPage() {
|
||||
const session = (await cookies()).get("mis_session")?.value;
|
||||
if (session) {
|
||||
redirect("/machines");
|
||||
}
|
||||
|
||||
return <SignupForm />;
|
||||
}
|
||||
329
components/analytics/DowntimeParetoCard.tsx
Normal file
329
components/analytics/DowntimeParetoCard.tsx
Normal file
@@ -0,0 +1,329 @@
|
||||
"use client";
|
||||
|
||||
import { useEffect, useMemo, useState } from "react";
|
||||
import { DOWNTIME_RANGES, type DowntimeRange } from "@/lib/analytics/downtimeRange";
|
||||
import Link from "next/link";
|
||||
import {
|
||||
Bar,
|
||||
CartesianGrid,
|
||||
ComposedChart,
|
||||
Line,
|
||||
ReferenceLine,
|
||||
ResponsiveContainer,
|
||||
Tooltip,
|
||||
XAxis,
|
||||
YAxis,
|
||||
} from "recharts";
|
||||
import { useI18n } from "@/lib/i18n/useI18n";
|
||||
|
||||
|
||||
type ParetoRow = {
|
||||
reasonCode: string;
|
||||
reasonLabel: string;
|
||||
minutesLost?: number; // downtime
|
||||
scrapQty?: number; // scrap (future)
|
||||
pctOfTotal: number; // 0..100
|
||||
cumulativePct: number; // 0..100
|
||||
};
|
||||
|
||||
type ParetoResponse = {
|
||||
ok?: boolean;
|
||||
rows?: ParetoRow[];
|
||||
top3?: ParetoRow[];
|
||||
totalMinutesLost?: number;
|
||||
threshold80?: { reasonCode: string; reasonLabel: string; index: number } | null;
|
||||
error?: string;
|
||||
};
|
||||
|
||||
type CoverageResponse = {
|
||||
ok?: boolean;
|
||||
totalDowntimeMinutes?: number;
|
||||
receivedMinutes?: number;
|
||||
receivedCoveragePct?: number; // could be 0..1 or 0..100 depending on your impl
|
||||
pendingEpisodesCount?: number;
|
||||
};
|
||||
|
||||
function clampLabel(s: string, max = 18) {
|
||||
if (!s) return "";
|
||||
return s.length > max ? `${s.slice(0, max - 1)}…` : s;
|
||||
}
|
||||
|
||||
function normalizePct(v?: number | null) {
|
||||
if (v == null || Number.isNaN(v)) return null;
|
||||
// If API returns 0..1, convert to 0..100
|
||||
return v <= 1 ? v * 100 : v;
|
||||
}
|
||||
|
||||
export default function DowntimeParetoCard({
|
||||
machineId,
|
||||
range = "7d",
|
||||
showCoverage = true,
|
||||
showOpenFullReport = true,
|
||||
variant = "summary",
|
||||
maxBars,
|
||||
}: {
|
||||
machineId?: string;
|
||||
range?: DowntimeRange;
|
||||
showCoverage?: boolean;
|
||||
showOpenFullReport?: boolean;
|
||||
variant?: "summary" | "full";
|
||||
maxBars?: number; // optional override
|
||||
}) {
|
||||
const { t } = useI18n();
|
||||
const isSummary = variant === "summary";
|
||||
const barsLimit = maxBars ?? (isSummary ? 5 : 12);
|
||||
const chartHeightClass = isSummary ? "h-[240px]" : "h-[360px]";
|
||||
const containerPad = isSummary ? "p-4" : "p-5";
|
||||
const [loading, setLoading] = useState(true);
|
||||
const [err, setErr] = useState<string | null>(null);
|
||||
const [pareto, setPareto] = useState<ParetoResponse | null>(null);
|
||||
const [coverage, setCoverage] = useState<CoverageResponse | null>(null);
|
||||
|
||||
useEffect(() => {
|
||||
const controller = new AbortController();
|
||||
|
||||
async function load() {
|
||||
setLoading(true);
|
||||
setErr(null);
|
||||
|
||||
try {
|
||||
const qs = new URLSearchParams();
|
||||
qs.set("kind", "downtime");
|
||||
qs.set("range", range);
|
||||
if (machineId) qs.set("machineId", machineId);
|
||||
|
||||
const res = await fetch(`/api/analytics/pareto?${qs.toString()}`, {
|
||||
cache: "no-cache",
|
||||
credentials: "include",
|
||||
signal: controller.signal,
|
||||
});
|
||||
|
||||
const json = (await res.json().catch(() => ({}))) as ParetoResponse;
|
||||
|
||||
if (!res.ok || json?.ok === false) {
|
||||
setPareto(null);
|
||||
setErr(json?.error ?? "Failed to load pareto.");
|
||||
setLoading(false);
|
||||
return;
|
||||
}
|
||||
|
||||
setPareto(json);
|
||||
|
||||
// Optional coverage (fail silently if endpoint not ready)
|
||||
if (showCoverage) {
|
||||
const cqs = new URLSearchParams();
|
||||
cqs.set("kind", "downtime");
|
||||
cqs.set("range", range);
|
||||
if (machineId) cqs.set("machineId", machineId);
|
||||
|
||||
fetch(`/api/analytics/coverage?${cqs.toString()}`, {
|
||||
cache: "no-cache",
|
||||
credentials: "include",
|
||||
signal: controller.signal,
|
||||
})
|
||||
.then((r) => (r.ok ? r.json() : null))
|
||||
.then((cj) => (cj ? (cj as CoverageResponse) : null))
|
||||
.then((cj) => {
|
||||
if (cj) setCoverage(cj);
|
||||
})
|
||||
.catch(() => {
|
||||
// ignore
|
||||
});
|
||||
}
|
||||
|
||||
setLoading(false);
|
||||
} catch (e: any) {
|
||||
if (e?.name === "AbortError") return;
|
||||
setErr("Network error.");
|
||||
setLoading(false);
|
||||
}
|
||||
}
|
||||
|
||||
load();
|
||||
return () => controller.abort();
|
||||
}, [machineId, range, showCoverage]);
|
||||
|
||||
const rows = pareto?.rows ?? [];
|
||||
|
||||
const chartData = useMemo(() => {
|
||||
return rows.slice(0, barsLimit).map((r, idx) => ({
|
||||
i: idx,
|
||||
reasonCode: r.reasonCode,
|
||||
reasonLabel: r.reasonLabel,
|
||||
label: clampLabel(r.reasonLabel || r.reasonCode, isSummary ? 16 : 22),
|
||||
minutes: Number(r.minutesLost ?? 0),
|
||||
pctOfTotal: Number(r.pctOfTotal ?? 0),
|
||||
cumulativePct: Number(r.cumulativePct ?? 0),
|
||||
}));
|
||||
}, [rows, barsLimit, isSummary]);
|
||||
|
||||
|
||||
const top3 = useMemo(() => {
|
||||
if (pareto?.top3?.length) return pareto.top3.slice(0, 3);
|
||||
return [...rows]
|
||||
.sort((a, b) => Number(b.minutesLost ?? 0) - Number(a.minutesLost ?? 0))
|
||||
.slice(0, 3);
|
||||
}, [pareto?.top3, rows]);
|
||||
|
||||
const totalMinutes = Number(pareto?.totalMinutesLost ?? 0);
|
||||
|
||||
const covPct = normalizePct(coverage?.receivedCoveragePct ?? null);
|
||||
const pending = coverage?.pendingEpisodesCount ?? null;
|
||||
|
||||
const title =
|
||||
range === "24h"
|
||||
? "Downtime Pareto (24h)"
|
||||
: range === "30d"
|
||||
? "Downtime Pareto (30d)"
|
||||
: range === "mtd"
|
||||
? "Downtime Pareto (MTD)"
|
||||
: "Downtime Pareto (7d)";
|
||||
|
||||
|
||||
const reportHref = machineId
|
||||
? `/downtime?machineId=${encodeURIComponent(machineId)}&range=${encodeURIComponent(range)}`
|
||||
: `/downtime?range=${encodeURIComponent(range)}`;
|
||||
|
||||
return (
|
||||
<div className={`rounded-2xl border border-white/10 bg-white/5 ${containerPad}`}>
|
||||
<div className="flex flex-wrap items-start justify-between gap-3">
|
||||
<div>
|
||||
<div className="text-sm font-semibold text-white">{title}</div>
|
||||
<div className="mt-1 text-xs text-zinc-400">
|
||||
Total: <span className="text-white">{totalMinutes.toFixed(0)} min</span>
|
||||
{covPct != null ? (
|
||||
<>
|
||||
<span className="mx-2 text-zinc-600">•</span>
|
||||
Coverage: <span className="text-white">{covPct.toFixed(0)}%</span>
|
||||
{pending != null ? (
|
||||
<>
|
||||
<span className="mx-2 text-zinc-600">•</span>
|
||||
Pending: <span className="text-white">{pending}</span>
|
||||
</>
|
||||
) : null}
|
||||
</>
|
||||
) : null}
|
||||
</div>
|
||||
</div>
|
||||
{showOpenFullReport ? (
|
||||
<Link
|
||||
href={reportHref}
|
||||
className="rounded-xl border border-white/10 bg-white/5 px-3 py-1.5 text-xs text-white hover:bg-white/10"
|
||||
>
|
||||
View full report →
|
||||
</Link>
|
||||
) : null}
|
||||
</div>
|
||||
|
||||
{loading ? (
|
||||
<div className="mt-4 text-sm text-zinc-400">{t("machine.detail.loading")}</div>
|
||||
) : err ? (
|
||||
<div className="mt-4 rounded-2xl border border-red-500/20 bg-red-500/10 p-4 text-sm text-red-200">
|
||||
{err}
|
||||
</div>
|
||||
) : rows.length === 0 ? (
|
||||
<div className="mt-4 text-sm text-zinc-400">No downtime reasons found for this range.</div>
|
||||
) : (
|
||||
<div className="mt-4 grid grid-cols-1 gap-4 lg:grid-cols-3">
|
||||
<div
|
||||
className={`${chartHeightClass} rounded-3xl border border-white/10 bg-black/30 p-4 backdrop-blur lg:col-span-2`}
|
||||
style={{ boxShadow: "var(--app-chart-shadow)" }}
|
||||
>
|
||||
<ResponsiveContainer width="100%" height="100%">
|
||||
<ComposedChart data={chartData} margin={{ top: 10, right: 24, left: 0, bottom: 10 }}>
|
||||
<CartesianGrid strokeDasharray="3 3" stroke="var(--app-chart-grid)" />
|
||||
<XAxis
|
||||
dataKey="label"
|
||||
interval={0}
|
||||
tick={{ fill: "var(--app-chart-tick)", fontSize: 11 }}
|
||||
/>
|
||||
<YAxis
|
||||
yAxisId="left"
|
||||
tick={{ fill: "var(--app-chart-tick)" }}
|
||||
width={40}
|
||||
/>
|
||||
<YAxis
|
||||
yAxisId="right"
|
||||
orientation="right"
|
||||
domain={[0, 100]}
|
||||
tick={{ fill: "var(--app-chart-tick)" }}
|
||||
tickFormatter={(v) => `${v}%`}
|
||||
width={44}
|
||||
/>
|
||||
<Tooltip
|
||||
cursor={{ stroke: "var(--app-chart-grid)" }}
|
||||
contentStyle={{
|
||||
background: "var(--app-chart-tooltip-bg)",
|
||||
border: "1px solid var(--app-chart-tooltip-border)",
|
||||
}}
|
||||
labelStyle={{ color: "var(--app-chart-label)" }}
|
||||
formatter={(val: any, name: any, ctx: any) => {
|
||||
if (name === "minutes") return [`${Number(val).toFixed(1)} min`, "Minutes"];
|
||||
if (name === "cumulativePct") return [`${Number(val).toFixed(1)}%`, "Cumulative"];
|
||||
return [val, name];
|
||||
}}
|
||||
/>
|
||||
|
||||
<ReferenceLine
|
||||
yAxisId="right"
|
||||
y={80}
|
||||
stroke="rgba(255,255,255,0.25)"
|
||||
strokeDasharray="6 6"
|
||||
/>
|
||||
|
||||
<Bar
|
||||
yAxisId="left"
|
||||
dataKey="minutes"
|
||||
radius={[10, 10, 0, 0]}
|
||||
isAnimationActive={false}
|
||||
fill="#FF7A00"
|
||||
/>
|
||||
<Line
|
||||
yAxisId="right"
|
||||
dataKey="cumulativePct"
|
||||
dot={false}
|
||||
strokeWidth={2}
|
||||
isAnimationActive={false}
|
||||
stroke="#12D18E"
|
||||
/>
|
||||
</ComposedChart>
|
||||
</ResponsiveContainer>
|
||||
</div>
|
||||
|
||||
<div className={`rounded-2xl border border-white/10 bg-black/20 ${isSummary ? "p-3" : "p-4"}`}>
|
||||
<div className="text-xs font-semibold text-white">Top 3 reasons</div>
|
||||
<div className="mt-3 space-y-3">
|
||||
{top3.map((r) => (
|
||||
<div key={r.reasonCode} className="rounded-xl border border-white/10 bg-white/5 p-3">
|
||||
<div className="flex items-start justify-between gap-3">
|
||||
<div className="min-w-0">
|
||||
<div className="truncate text-sm font-semibold text-white">
|
||||
{r.reasonLabel || r.reasonCode}
|
||||
</div>
|
||||
<div className="mt-1 text-xs text-zinc-400">{r.reasonCode}</div>
|
||||
</div>
|
||||
<div className="shrink-0 text-right">
|
||||
<div className="text-sm font-semibold text-white">
|
||||
{(r.minutesLost ?? 0).toFixed(0)}m
|
||||
</div>
|
||||
<div className="text-xs text-zinc-400">{(r.pctOfTotal ?? 0).toFixed(1)}%</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
|
||||
{!isSummary && pareto?.threshold80 ? (
|
||||
<div className="mt-4 rounded-xl border border-white/10 bg-white/5 p-3 text-xs text-zinc-300">
|
||||
80% cutoff:{" "}
|
||||
<span className="text-white">
|
||||
{pareto.threshold80.reasonLabel} ({pareto.threshold80.reasonCode})
|
||||
</span>
|
||||
</div>
|
||||
) : null}
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -1,25 +1,34 @@
|
||||
"use client";
|
||||
|
||||
import { usePathname, useRouter } from "next/navigation";
|
||||
import { useEffect, useState } from "react";
|
||||
import { useEffect, useSyncExternalStore } from "react";
|
||||
|
||||
function subscribe(callback: () => void) {
|
||||
if (typeof window === "undefined") return () => {};
|
||||
window.addEventListener("storage", callback);
|
||||
return () => window.removeEventListener("storage", callback);
|
||||
}
|
||||
|
||||
function getSnapshot() {
|
||||
if (typeof window === "undefined") return null;
|
||||
return localStorage.getItem("ct_token");
|
||||
}
|
||||
|
||||
export function RequireAuth({ children }: { children: React.ReactNode }) {
|
||||
const router = useRouter();
|
||||
const pathname = usePathname();
|
||||
const [ready, setReady] = useState(false);
|
||||
const token = useSyncExternalStore(subscribe, getSnapshot, () => null);
|
||||
const hasToken = Boolean(token);
|
||||
|
||||
useEffect(() => {
|
||||
const token = localStorage.getItem("ct_token");
|
||||
if (!token) {
|
||||
if (!hasToken) {
|
||||
router.replace("/login");
|
||||
return;
|
||||
}
|
||||
setReady(true);
|
||||
}, [router, pathname]);
|
||||
}, [router, pathname, hasToken]);
|
||||
|
||||
if (!ready) {
|
||||
if (!hasToken) {
|
||||
return (
|
||||
<div className="min-h-screen bg-[#070A0C] text-zinc-200 flex items-center justify-center">
|
||||
<div className="min-h-screen bg-black text-zinc-200 flex items-center justify-center">
|
||||
Loading…
|
||||
</div>
|
||||
);
|
||||
|
||||
2497
components/downtime/DowntimePageClient.tsx
Normal file
2497
components/downtime/DowntimePageClient.tsx
Normal file
File diff suppressed because it is too large
Load Diff
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user