Mobile friendly, lint correction, typescript error clear
This commit is contained in:
@@ -50,6 +50,7 @@ export async function PATCH(req: Request, { params }: { params: Promise<{ id: st
|
||||
}
|
||||
|
||||
const { userId: _userId, eventTypes, ...updateData } = parsed.data;
|
||||
void _userId;
|
||||
const normalizedEventTypes =
|
||||
eventTypes === null ? Prisma.DbNull : eventTypes ?? undefined;
|
||||
const data = normalizedEventTypes === undefined
|
||||
|
||||
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 });
|
||||
}
|
||||
262
app/api/financial/costs/route.ts
Normal file
262
app/api/financial/costs/route.ts
Normal file
@@ -0,0 +1,262 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import { prisma } from "@/lib/prisma";
|
||||
import { requireSession } from "@/lib/auth/requireSession";
|
||||
import { Prisma } from "@prisma/client";
|
||||
import { z } from "zod";
|
||||
|
||||
function canManageFinancials(role?: string | null) {
|
||||
return role === "OWNER";
|
||||
}
|
||||
|
||||
function stripUndefined<T extends Record<string, unknown>>(input: T) {
|
||||
const out: Record<string, unknown> = {};
|
||||
for (const [key, value] of Object.entries(input)) {
|
||||
if (value !== undefined) out[key] = value;
|
||||
}
|
||||
return out as T;
|
||||
}
|
||||
|
||||
function normalizeCurrency(value?: string | null) {
|
||||
if (!value) return null;
|
||||
const trimmed = value.trim();
|
||||
if (!trimmed) return null;
|
||||
return trimmed.toUpperCase();
|
||||
}
|
||||
|
||||
const numberField = z.preprocess(
|
||||
(value) => {
|
||||
if (value === "" || value === null || value === undefined) return null;
|
||||
const n = Number(value);
|
||||
return Number.isFinite(n) ? n : value;
|
||||
},
|
||||
z.number().finite().nullable()
|
||||
);
|
||||
|
||||
const numericFields = {
|
||||
machineCostPerMin: numberField.optional(),
|
||||
operatorCostPerMin: numberField.optional(),
|
||||
ratedRunningKw: numberField.optional(),
|
||||
idleKw: numberField.optional(),
|
||||
kwhRate: numberField.optional(),
|
||||
energyMultiplier: numberField.optional(),
|
||||
energyCostPerMin: numberField.optional(),
|
||||
scrapCostPerUnit: numberField.optional(),
|
||||
rawMaterialCostPerUnit: numberField.optional(),
|
||||
};
|
||||
|
||||
const orgSchema = z
|
||||
.object({
|
||||
defaultCurrency: z.string().trim().min(1).max(8).optional(),
|
||||
...numericFields,
|
||||
})
|
||||
.strict();
|
||||
|
||||
const locationSchema = z
|
||||
.object({
|
||||
location: z.string().trim().min(1).max(80),
|
||||
currency: z.string().trim().min(1).max(8).optional().nullable(),
|
||||
...numericFields,
|
||||
})
|
||||
.strict();
|
||||
|
||||
const machineSchema = z
|
||||
.object({
|
||||
machineId: z.string().uuid(),
|
||||
currency: z.string().trim().min(1).max(8).optional().nullable(),
|
||||
...numericFields,
|
||||
})
|
||||
.strict();
|
||||
|
||||
const productSchema = z
|
||||
.object({
|
||||
sku: z.string().trim().min(1).max(64),
|
||||
currency: z.string().trim().min(1).max(8).optional().nullable(),
|
||||
rawMaterialCostPerUnit: numberField.optional(),
|
||||
})
|
||||
.strict();
|
||||
|
||||
const payloadSchema = z
|
||||
.object({
|
||||
org: orgSchema.optional(),
|
||||
locations: z.array(locationSchema).optional(),
|
||||
machines: z.array(machineSchema).optional(),
|
||||
products: z.array(productSchema).optional(),
|
||||
})
|
||||
.strict();
|
||||
|
||||
async function ensureOrgFinancialProfile(
|
||||
tx: Prisma.TransactionClient,
|
||||
orgId: string,
|
||||
userId: string
|
||||
) {
|
||||
const existing = await tx.orgFinancialProfile.findUnique({ where: { orgId } });
|
||||
if (existing) return existing;
|
||||
return tx.orgFinancialProfile.create({
|
||||
data: {
|
||||
orgId,
|
||||
defaultCurrency: "USD",
|
||||
energyMultiplier: 1.0,
|
||||
updatedBy: userId,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
async function loadFinancialConfig(orgId: string) {
|
||||
const [org, locations, machines, products] = await Promise.all([
|
||||
prisma.orgFinancialProfile.findUnique({ where: { orgId } }),
|
||||
prisma.locationFinancialOverride.findMany({ where: { orgId }, orderBy: { location: "asc" } }),
|
||||
prisma.machineFinancialOverride.findMany({ where: { orgId }, orderBy: { createdAt: "desc" } }),
|
||||
prisma.productCostOverride.findMany({ where: { orgId }, orderBy: { sku: "asc" } }),
|
||||
]);
|
||||
|
||||
return { org, locations, machines, products };
|
||||
}
|
||||
|
||||
export async function GET() {
|
||||
const session = await requireSession();
|
||||
if (!session) return NextResponse.json({ ok: false, error: "Unauthorized" }, { status: 401 });
|
||||
|
||||
const membership = await prisma.orgUser.findUnique({
|
||||
where: { orgId_userId: { orgId: session.orgId, userId: session.userId } },
|
||||
select: { role: true },
|
||||
});
|
||||
if (!canManageFinancials(membership?.role)) {
|
||||
return NextResponse.json({ ok: false, error: "Forbidden" }, { status: 403 });
|
||||
}
|
||||
|
||||
await prisma.$transaction((tx) => ensureOrgFinancialProfile(tx, session.orgId, session.userId));
|
||||
const payload = await loadFinancialConfig(session.orgId);
|
||||
return NextResponse.json({ ok: true, ...payload });
|
||||
}
|
||||
|
||||
export async function POST(req: Request) {
|
||||
const session = await requireSession();
|
||||
if (!session) return NextResponse.json({ ok: false, error: "Unauthorized" }, { status: 401 });
|
||||
|
||||
const membership = await prisma.orgUser.findUnique({
|
||||
where: { orgId_userId: { orgId: session.orgId, userId: session.userId } },
|
||||
select: { role: true },
|
||||
});
|
||||
if (!canManageFinancials(membership?.role)) {
|
||||
return NextResponse.json({ ok: false, error: "Forbidden" }, { status: 403 });
|
||||
}
|
||||
|
||||
const body = await req.json().catch(() => ({}));
|
||||
const parsed = payloadSchema.safeParse(body);
|
||||
if (!parsed.success) {
|
||||
return NextResponse.json({ ok: false, error: "Invalid payload" }, { status: 400 });
|
||||
}
|
||||
|
||||
const data = parsed.data;
|
||||
|
||||
await prisma.$transaction(async (tx) => {
|
||||
await ensureOrgFinancialProfile(tx, session.orgId, session.userId);
|
||||
|
||||
if (data.org) {
|
||||
const updateData = stripUndefined({
|
||||
defaultCurrency: data.org.defaultCurrency?.trim().toUpperCase(),
|
||||
machineCostPerMin: data.org.machineCostPerMin,
|
||||
operatorCostPerMin: data.org.operatorCostPerMin,
|
||||
ratedRunningKw: data.org.ratedRunningKw,
|
||||
idleKw: data.org.idleKw,
|
||||
kwhRate: data.org.kwhRate,
|
||||
energyMultiplier: data.org.energyMultiplier == null ? undefined : data.org.energyMultiplier,
|
||||
energyCostPerMin: data.org.energyCostPerMin,
|
||||
scrapCostPerUnit: data.org.scrapCostPerUnit,
|
||||
rawMaterialCostPerUnit: data.org.rawMaterialCostPerUnit,
|
||||
updatedBy: session.userId,
|
||||
});
|
||||
|
||||
if (Object.keys(updateData).length > 0) {
|
||||
await tx.orgFinancialProfile.update({
|
||||
where: { orgId: session.orgId },
|
||||
data: updateData,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
const machineIds = new Set((data.machines ?? []).map((m) => m.machineId));
|
||||
const validMachineIds = new Set<string>();
|
||||
if (machineIds.size > 0) {
|
||||
const rows = await tx.machine.findMany({
|
||||
where: { orgId: session.orgId, id: { in: Array.from(machineIds) } },
|
||||
select: { id: true },
|
||||
});
|
||||
rows.forEach((m) => validMachineIds.add(m.id));
|
||||
}
|
||||
|
||||
for (const loc of data.locations ?? []) {
|
||||
const updateData = stripUndefined({
|
||||
currency: normalizeCurrency(loc.currency),
|
||||
machineCostPerMin: loc.machineCostPerMin,
|
||||
operatorCostPerMin: loc.operatorCostPerMin,
|
||||
ratedRunningKw: loc.ratedRunningKw,
|
||||
idleKw: loc.idleKw,
|
||||
kwhRate: loc.kwhRate,
|
||||
energyMultiplier: loc.energyMultiplier,
|
||||
energyCostPerMin: loc.energyCostPerMin,
|
||||
scrapCostPerUnit: loc.scrapCostPerUnit,
|
||||
rawMaterialCostPerUnit: loc.rawMaterialCostPerUnit,
|
||||
updatedBy: session.userId,
|
||||
});
|
||||
|
||||
await tx.locationFinancialOverride.upsert({
|
||||
where: { orgId_location: { orgId: session.orgId, location: loc.location } },
|
||||
update: updateData,
|
||||
create: {
|
||||
orgId: session.orgId,
|
||||
location: loc.location,
|
||||
...updateData,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
for (const machine of data.machines ?? []) {
|
||||
if (!validMachineIds.has(machine.machineId)) continue;
|
||||
const updateData = stripUndefined({
|
||||
currency: normalizeCurrency(machine.currency),
|
||||
machineCostPerMin: machine.machineCostPerMin,
|
||||
operatorCostPerMin: machine.operatorCostPerMin,
|
||||
ratedRunningKw: machine.ratedRunningKw,
|
||||
idleKw: machine.idleKw,
|
||||
kwhRate: machine.kwhRate,
|
||||
energyMultiplier: machine.energyMultiplier,
|
||||
energyCostPerMin: machine.energyCostPerMin,
|
||||
scrapCostPerUnit: machine.scrapCostPerUnit,
|
||||
rawMaterialCostPerUnit: machine.rawMaterialCostPerUnit,
|
||||
updatedBy: session.userId,
|
||||
});
|
||||
|
||||
await tx.machineFinancialOverride.upsert({
|
||||
where: { orgId_machineId: { orgId: session.orgId, machineId: machine.machineId } },
|
||||
update: updateData,
|
||||
create: {
|
||||
orgId: session.orgId,
|
||||
machineId: machine.machineId,
|
||||
...updateData,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
for (const product of data.products ?? []) {
|
||||
const updateData = stripUndefined({
|
||||
currency: normalizeCurrency(product.currency),
|
||||
rawMaterialCostPerUnit: product.rawMaterialCostPerUnit,
|
||||
updatedBy: session.userId,
|
||||
});
|
||||
|
||||
await tx.productCostOverride.upsert({
|
||||
where: { orgId_sku: { orgId: session.orgId, sku: product.sku } },
|
||||
update: updateData,
|
||||
create: {
|
||||
orgId: session.orgId,
|
||||
sku: product.sku,
|
||||
...updateData,
|
||||
},
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
const payload = await loadFinancialConfig(session.orgId);
|
||||
return NextResponse.json({ ok: true, ...payload });
|
||||
}
|
||||
156
app/api/financial/export/excel/route.ts
Normal file
156
app/api/financial/export/excel/route.ts
Normal file
@@ -0,0 +1,156 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import type { NextRequest } from "next/server";
|
||||
import { requireSession } from "@/lib/auth/requireSession";
|
||||
import { prisma } from "@/lib/prisma";
|
||||
import { computeFinancialImpact } from "@/lib/financial/impact";
|
||||
|
||||
const RANGE_MS: Record<string, number> = {
|
||||
"24h": 24 * 60 * 60 * 1000,
|
||||
"7d": 7 * 24 * 60 * 60 * 1000,
|
||||
"30d": 30 * 24 * 60 * 60 * 1000,
|
||||
};
|
||||
|
||||
function canManageFinancials(role?: string | null) {
|
||||
return role === "OWNER";
|
||||
}
|
||||
|
||||
function parseDate(input?: string | null) {
|
||||
if (!input) return null;
|
||||
const n = Number(input);
|
||||
if (!Number.isNaN(n)) return new Date(n);
|
||||
const d = new Date(input);
|
||||
return Number.isNaN(d.getTime()) ? null : d;
|
||||
}
|
||||
|
||||
function pickRange(req: NextRequest) {
|
||||
const url = new URL(req.url);
|
||||
const range = url.searchParams.get("range") ?? "7d";
|
||||
const now = new Date();
|
||||
|
||||
if (range === "custom") {
|
||||
const start = parseDate(url.searchParams.get("start")) ?? new Date(now.getTime() - RANGE_MS["24h"]);
|
||||
const end = parseDate(url.searchParams.get("end")) ?? now;
|
||||
return { start, end };
|
||||
}
|
||||
|
||||
const ms = RANGE_MS[range] ?? RANGE_MS["24h"];
|
||||
return { start: new Date(now.getTime() - ms), end: now };
|
||||
}
|
||||
|
||||
function csvValue(value: string | number | null | undefined) {
|
||||
if (value === null || value === undefined) return "";
|
||||
const text = String(value);
|
||||
if (/[",\n]/.test(text)) {
|
||||
return `"${text.replace(/"/g, "\"\"")}"`;
|
||||
}
|
||||
return text;
|
||||
}
|
||||
|
||||
function formatNumber(value: number | null) {
|
||||
if (value == null || !Number.isFinite(value)) return "";
|
||||
return value.toFixed(4);
|
||||
}
|
||||
|
||||
function slugify(value: string) {
|
||||
return value
|
||||
.toLowerCase()
|
||||
.replace(/[^a-z0-9]+/g, "-")
|
||||
.replace(/^-+|-+$/g, "")
|
||||
.slice(0, 60) || "report";
|
||||
}
|
||||
|
||||
export async function GET(req: NextRequest) {
|
||||
const session = await requireSession();
|
||||
if (!session) return NextResponse.json({ ok: false, error: "Unauthorized" }, { status: 401 });
|
||||
|
||||
const membership = await prisma.orgUser.findUnique({
|
||||
where: { orgId_userId: { orgId: session.orgId, userId: session.userId } },
|
||||
select: { role: true },
|
||||
});
|
||||
if (!canManageFinancials(membership?.role)) {
|
||||
return NextResponse.json({ ok: false, error: "Forbidden" }, { status: 403 });
|
||||
}
|
||||
|
||||
const url = new URL(req.url);
|
||||
const { start, end } = pickRange(req);
|
||||
const machineId = url.searchParams.get("machineId") ?? undefined;
|
||||
const location = url.searchParams.get("location") ?? undefined;
|
||||
const sku = url.searchParams.get("sku") ?? undefined;
|
||||
const currency = url.searchParams.get("currency") ?? undefined;
|
||||
|
||||
const [org, impact] = await Promise.all([
|
||||
prisma.org.findUnique({ where: { id: session.orgId }, select: { name: true } }),
|
||||
computeFinancialImpact({
|
||||
orgId: session.orgId,
|
||||
start,
|
||||
end,
|
||||
machineId,
|
||||
location,
|
||||
sku,
|
||||
currency,
|
||||
includeEvents: true,
|
||||
}),
|
||||
]);
|
||||
|
||||
const orgName = org?.name ?? "Organization";
|
||||
const header = [
|
||||
"org_name",
|
||||
"range_start",
|
||||
"range_end",
|
||||
"event_id",
|
||||
"event_ts",
|
||||
"event_type",
|
||||
"status",
|
||||
"severity",
|
||||
"category",
|
||||
"machine_id",
|
||||
"machine_name",
|
||||
"location",
|
||||
"work_order_id",
|
||||
"sku",
|
||||
"duration_sec",
|
||||
"cost_machine",
|
||||
"cost_operator",
|
||||
"cost_energy",
|
||||
"cost_scrap",
|
||||
"cost_raw_material",
|
||||
"cost_total",
|
||||
"currency",
|
||||
];
|
||||
|
||||
const rows = impact.events.map((event) => [
|
||||
orgName,
|
||||
start.toISOString(),
|
||||
end.toISOString(),
|
||||
event.id,
|
||||
event.ts.toISOString(),
|
||||
event.eventType,
|
||||
event.status,
|
||||
event.severity,
|
||||
event.category,
|
||||
event.machineId,
|
||||
event.machineName ?? "",
|
||||
event.location ?? "",
|
||||
event.workOrderId ?? "",
|
||||
event.sku ?? "",
|
||||
formatNumber(event.durationSec),
|
||||
formatNumber(event.costMachine),
|
||||
formatNumber(event.costOperator),
|
||||
formatNumber(event.costEnergy),
|
||||
formatNumber(event.costScrap),
|
||||
formatNumber(event.costRawMaterial),
|
||||
formatNumber(event.costTotal),
|
||||
event.currency,
|
||||
]);
|
||||
|
||||
const lines = [header, ...rows].map((row) => row.map(csvValue).join(","));
|
||||
const csv = lines.join("\n");
|
||||
|
||||
const fileName = `financial_events_${slugify(orgName)}.csv`;
|
||||
return new NextResponse(csv, {
|
||||
headers: {
|
||||
"Content-Type": "text/csv; charset=utf-8",
|
||||
"Content-Disposition": `attachment; filename=\"${fileName}\"`,
|
||||
},
|
||||
});
|
||||
}
|
||||
246
app/api/financial/export/pdf/route.ts
Normal file
246
app/api/financial/export/pdf/route.ts
Normal file
@@ -0,0 +1,246 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import type { NextRequest } from "next/server";
|
||||
import { requireSession } from "@/lib/auth/requireSession";
|
||||
import { prisma } from "@/lib/prisma";
|
||||
import { computeFinancialImpact } from "@/lib/financial/impact";
|
||||
|
||||
const RANGE_MS: Record<string, number> = {
|
||||
"24h": 24 * 60 * 60 * 1000,
|
||||
"7d": 7 * 24 * 60 * 60 * 1000,
|
||||
"30d": 30 * 24 * 60 * 60 * 1000,
|
||||
};
|
||||
|
||||
function canManageFinancials(role?: string | null) {
|
||||
return role === "OWNER";
|
||||
}
|
||||
|
||||
function parseDate(input?: string | null) {
|
||||
if (!input) return null;
|
||||
const n = Number(input);
|
||||
if (!Number.isNaN(n)) return new Date(n);
|
||||
const d = new Date(input);
|
||||
return Number.isNaN(d.getTime()) ? null : d;
|
||||
}
|
||||
|
||||
function pickRange(req: NextRequest) {
|
||||
const url = new URL(req.url);
|
||||
const range = url.searchParams.get("range") ?? "7d";
|
||||
const now = new Date();
|
||||
|
||||
if (range === "custom") {
|
||||
const start = parseDate(url.searchParams.get("start")) ?? new Date(now.getTime() - RANGE_MS["24h"]);
|
||||
const end = parseDate(url.searchParams.get("end")) ?? now;
|
||||
return { start, end };
|
||||
}
|
||||
|
||||
const ms = RANGE_MS[range] ?? RANGE_MS["24h"];
|
||||
return { start: new Date(now.getTime() - ms), end: now };
|
||||
}
|
||||
|
||||
function escapeHtml(value: string) {
|
||||
return value
|
||||
.replace(/&/g, "&")
|
||||
.replace(/</g, "<")
|
||||
.replace(/>/g, ">")
|
||||
.replace(/\"/g, """)
|
||||
.replace(/'/g, "'");
|
||||
}
|
||||
|
||||
function formatMoney(value: number, currency: string) {
|
||||
if (!Number.isFinite(value)) return "--";
|
||||
try {
|
||||
return new Intl.NumberFormat("en-US", { style: "currency", currency, maximumFractionDigits: 2 }).format(value);
|
||||
} catch {
|
||||
return `${value.toFixed(2)} ${currency}`;
|
||||
}
|
||||
}
|
||||
|
||||
function formatNumber(value: number | null, digits = 2) {
|
||||
if (value == null || !Number.isFinite(value)) return "--";
|
||||
return value.toFixed(digits);
|
||||
}
|
||||
|
||||
function slugify(value: string) {
|
||||
return value
|
||||
.toLowerCase()
|
||||
.replace(/[^a-z0-9]+/g, "-")
|
||||
.replace(/^-+|-+$/g, "")
|
||||
.slice(0, 60) || "report";
|
||||
}
|
||||
|
||||
export async function GET(req: NextRequest) {
|
||||
const session = await requireSession();
|
||||
if (!session) return NextResponse.json({ ok: false, error: "Unauthorized" }, { status: 401 });
|
||||
|
||||
const membership = await prisma.orgUser.findUnique({
|
||||
where: { orgId_userId: { orgId: session.orgId, userId: session.userId } },
|
||||
select: { role: true },
|
||||
});
|
||||
if (!canManageFinancials(membership?.role)) {
|
||||
return NextResponse.json({ ok: false, error: "Forbidden" }, { status: 403 });
|
||||
}
|
||||
|
||||
const url = new URL(req.url);
|
||||
const { start, end } = pickRange(req);
|
||||
const machineId = url.searchParams.get("machineId") ?? undefined;
|
||||
const location = url.searchParams.get("location") ?? undefined;
|
||||
const sku = url.searchParams.get("sku") ?? undefined;
|
||||
const currency = url.searchParams.get("currency") ?? undefined;
|
||||
|
||||
const [org, impact] = await Promise.all([
|
||||
prisma.org.findUnique({ where: { id: session.orgId }, select: { name: true } }),
|
||||
computeFinancialImpact({
|
||||
orgId: session.orgId,
|
||||
start,
|
||||
end,
|
||||
machineId,
|
||||
location,
|
||||
sku,
|
||||
currency,
|
||||
includeEvents: true,
|
||||
}),
|
||||
]);
|
||||
|
||||
const orgName = org?.name ?? "Organization";
|
||||
const summaryBlocks = impact.currencySummaries
|
||||
.map(
|
||||
(summary) => `
|
||||
<div class="card">
|
||||
<div class="card-title">${escapeHtml(summary.currency)}</div>
|
||||
<div class="card-value">${escapeHtml(formatMoney(summary.totals.total, summary.currency))}</div>
|
||||
<div class="card-sub">Slow: ${escapeHtml(formatMoney(summary.totals.slowCycle, summary.currency))}</div>
|
||||
<div class="card-sub">Micro: ${escapeHtml(formatMoney(summary.totals.microstop, summary.currency))}</div>
|
||||
<div class="card-sub">Macro: ${escapeHtml(formatMoney(summary.totals.macrostop, summary.currency))}</div>
|
||||
<div class="card-sub">Scrap: ${escapeHtml(formatMoney(summary.totals.scrap, summary.currency))}</div>
|
||||
</div>
|
||||
`
|
||||
)
|
||||
.join("");
|
||||
|
||||
const dailyTables = impact.currencySummaries
|
||||
.map((summary) => {
|
||||
const rows = summary.byDay
|
||||
.map(
|
||||
(row) => `
|
||||
<tr>
|
||||
<td>${escapeHtml(row.day)}</td>
|
||||
<td>${escapeHtml(formatMoney(row.total, summary.currency))}</td>
|
||||
<td>${escapeHtml(formatMoney(row.slowCycle, summary.currency))}</td>
|
||||
<td>${escapeHtml(formatMoney(row.microstop, summary.currency))}</td>
|
||||
<td>${escapeHtml(formatMoney(row.macrostop, summary.currency))}</td>
|
||||
<td>${escapeHtml(formatMoney(row.scrap, summary.currency))}</td>
|
||||
</tr>
|
||||
`
|
||||
)
|
||||
.join("");
|
||||
|
||||
return `
|
||||
<section class="section">
|
||||
<h3>${escapeHtml(summary.currency)} Daily Breakdown</h3>
|
||||
<table>
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Day</th>
|
||||
<th>Total</th>
|
||||
<th>Slow</th>
|
||||
<th>Micro</th>
|
||||
<th>Macro</th>
|
||||
<th>Scrap</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
${rows || "<tr><td colspan=\"6\">No data</td></tr>"}
|
||||
</tbody>
|
||||
</table>
|
||||
</section>
|
||||
`;
|
||||
})
|
||||
.join("");
|
||||
|
||||
const eventRows = impact.events
|
||||
.map(
|
||||
(e) => `
|
||||
<tr>
|
||||
<td>${escapeHtml(e.ts.toISOString())}</td>
|
||||
<td>${escapeHtml(e.eventType)}</td>
|
||||
<td>${escapeHtml(e.category)}</td>
|
||||
<td>${escapeHtml(e.machineName ?? "-")}</td>
|
||||
<td>${escapeHtml(e.location ?? "-")}</td>
|
||||
<td>${escapeHtml(e.sku ?? "-")}</td>
|
||||
<td>${escapeHtml(e.workOrderId ?? "-")}</td>
|
||||
<td>${escapeHtml(formatNumber(e.durationSec))}</td>
|
||||
<td>${escapeHtml(formatMoney(e.costTotal, e.currency))}</td>
|
||||
<td>${escapeHtml(e.currency)}</td>
|
||||
</tr>
|
||||
`
|
||||
)
|
||||
.join("");
|
||||
|
||||
const html = `<!doctype html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="utf-8" />
|
||||
<title>Financial Impact Report</title>
|
||||
<style>
|
||||
body { font-family: Arial, sans-serif; color: #0f172a; margin: 32px; }
|
||||
h1 { margin: 0 0 6px; }
|
||||
.muted { color: #64748b; font-size: 12px; }
|
||||
.cards { display: grid; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); gap: 12px; margin: 20px 0; }
|
||||
.card { border: 1px solid #e2e8f0; border-radius: 12px; padding: 12px; }
|
||||
.card-title { font-size: 12px; text-transform: uppercase; color: #64748b; }
|
||||
.card-value { font-size: 20px; font-weight: 700; margin: 8px 0; }
|
||||
.card-sub { font-size: 12px; color: #475569; }
|
||||
.section { margin-top: 24px; }
|
||||
table { width: 100%; border-collapse: collapse; margin-top: 8px; }
|
||||
th, td { border: 1px solid #e2e8f0; padding: 8px; font-size: 12px; text-align: left; }
|
||||
th { background: #f8fafc; }
|
||||
footer { margin-top: 32px; text-align: right; font-size: 11px; color: #94a3b8; }
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<header>
|
||||
<h1>Financial Impact Report</h1>
|
||||
<div class="muted">${escapeHtml(orgName)} | ${escapeHtml(start.toISOString())} - ${escapeHtml(end.toISOString())}</div>
|
||||
</header>
|
||||
|
||||
<section class="cards">
|
||||
${summaryBlocks || "<div class=\"muted\">No totals yet.</div>"}
|
||||
</section>
|
||||
|
||||
${dailyTables}
|
||||
|
||||
<section class="section">
|
||||
<h3>Event Details</h3>
|
||||
<table>
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Timestamp</th>
|
||||
<th>Event</th>
|
||||
<th>Category</th>
|
||||
<th>Machine</th>
|
||||
<th>Location</th>
|
||||
<th>SKU</th>
|
||||
<th>Work Order</th>
|
||||
<th>Duration (sec)</th>
|
||||
<th>Cost</th>
|
||||
<th>Currency</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
${eventRows || "<tr><td colspan=\"10\">No events</td></tr>"}
|
||||
</tbody>
|
||||
</table>
|
||||
</section>
|
||||
|
||||
<footer>Power by MaliounTech</footer>
|
||||
</body>
|
||||
</html>`;
|
||||
|
||||
const fileName = `financial_report_${slugify(orgName)}.html`;
|
||||
return new NextResponse(html, {
|
||||
headers: {
|
||||
"Content-Type": "text/html; charset=utf-8",
|
||||
"Content-Disposition": `attachment; filename=\"${fileName}\"`,
|
||||
},
|
||||
});
|
||||
}
|
||||
71
app/api/financial/impact/route.ts
Normal file
71
app/api/financial/impact/route.ts
Normal file
@@ -0,0 +1,71 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import type { NextRequest } from "next/server";
|
||||
import { prisma } from "@/lib/prisma";
|
||||
import { requireSession } from "@/lib/auth/requireSession";
|
||||
import { computeFinancialImpact } from "@/lib/financial/impact";
|
||||
|
||||
const RANGE_MS: Record<string, number> = {
|
||||
"24h": 24 * 60 * 60 * 1000,
|
||||
"7d": 7 * 24 * 60 * 60 * 1000,
|
||||
"30d": 30 * 24 * 60 * 60 * 1000,
|
||||
};
|
||||
|
||||
function canManageFinancials(role?: string | null) {
|
||||
return role === "OWNER";
|
||||
}
|
||||
|
||||
function parseDate(input?: string | null) {
|
||||
if (!input) return null;
|
||||
const n = Number(input);
|
||||
if (!Number.isNaN(n)) return new Date(n);
|
||||
const d = new Date(input);
|
||||
return Number.isNaN(d.getTime()) ? null : d;
|
||||
}
|
||||
|
||||
function pickRange(req: NextRequest) {
|
||||
const url = new URL(req.url);
|
||||
const range = url.searchParams.get("range") ?? "7d";
|
||||
const now = new Date();
|
||||
|
||||
if (range === "custom") {
|
||||
const start = parseDate(url.searchParams.get("start")) ?? new Date(now.getTime() - RANGE_MS["24h"]);
|
||||
const end = parseDate(url.searchParams.get("end")) ?? now;
|
||||
return { start, end };
|
||||
}
|
||||
|
||||
const ms = RANGE_MS[range] ?? RANGE_MS["24h"];
|
||||
return { start: new Date(now.getTime() - ms), end: now };
|
||||
}
|
||||
|
||||
export async function GET(req: NextRequest) {
|
||||
const session = await requireSession();
|
||||
if (!session) return NextResponse.json({ ok: false, error: "Unauthorized" }, { status: 401 });
|
||||
|
||||
const membership = await prisma.orgUser.findUnique({
|
||||
where: { orgId_userId: { orgId: session.orgId, userId: session.userId } },
|
||||
select: { role: true },
|
||||
});
|
||||
if (!canManageFinancials(membership?.role)) {
|
||||
return NextResponse.json({ ok: false, error: "Forbidden" }, { status: 403 });
|
||||
}
|
||||
|
||||
const url = new URL(req.url);
|
||||
const { start, end } = pickRange(req);
|
||||
const machineId = url.searchParams.get("machineId") ?? undefined;
|
||||
const location = url.searchParams.get("location") ?? undefined;
|
||||
const sku = url.searchParams.get("sku") ?? undefined;
|
||||
const currency = url.searchParams.get("currency") ?? undefined;
|
||||
|
||||
const result = await computeFinancialImpact({
|
||||
orgId: session.orgId,
|
||||
start,
|
||||
end,
|
||||
machineId,
|
||||
location,
|
||||
sku,
|
||||
currency,
|
||||
includeEvents: false,
|
||||
});
|
||||
|
||||
return NextResponse.json({ ok: true, ...result });
|
||||
}
|
||||
@@ -3,27 +3,33 @@ import { prisma } from "@/lib/prisma";
|
||||
import { getMachineAuth } from "@/lib/machineAuthCache";
|
||||
import { z } from "zod";
|
||||
|
||||
function unwrapEnvelope(raw: any) {
|
||||
if (!raw || typeof raw !== "object") return raw;
|
||||
const payload = raw.payload;
|
||||
if (!payload || typeof payload !== "object") return raw;
|
||||
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 =
|
||||
raw.schemaVersion !== undefined ||
|
||||
raw.machineId !== undefined ||
|
||||
raw.tsMs !== undefined ||
|
||||
raw.tsDevice !== undefined ||
|
||||
raw.seq !== undefined ||
|
||||
raw.type !== undefined;
|
||||
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: raw.machineId ?? payload.machineId,
|
||||
tsMs: raw.tsMs ?? payload.tsMs,
|
||||
tsDevice: raw.tsDevice ?? payload.tsDevice,
|
||||
schemaVersion: raw.schemaVersion ?? payload.schemaVersion,
|
||||
seq: raw.seq ?? payload.seq,
|
||||
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,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -61,10 +67,14 @@ 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 = await req.json().catch(() => null);
|
||||
let body: unknown = await req.json().catch(() => null);
|
||||
body = unwrapEnvelope(body);
|
||||
const bodyRecord = asRecord(body) ?? {};
|
||||
|
||||
const machineId = body?.machineId ?? body?.machine_id ?? body?.machine?.id;
|
||||
const machineId =
|
||||
bodyRecord.machineId ??
|
||||
bodyRecord.machine_id ??
|
||||
(asRecord(bodyRecord.machine)?.id ?? null);
|
||||
if (!machineId || !machineIdSchema.safeParse(String(machineId)).success) {
|
||||
return NextResponse.json({ ok: false, error: "Invalid payload" }, { status: 400 });
|
||||
}
|
||||
@@ -72,8 +82,7 @@ export async function POST(req: Request) {
|
||||
const machine = await getMachineAuth(String(machineId), apiKey);
|
||||
if (!machine) return NextResponse.json({ ok: false, error: "Unauthorized" }, { status: 401 });
|
||||
|
||||
const raw = body as any;
|
||||
const cyclesRaw = raw?.cycles ?? raw?.cycle;
|
||||
const cyclesRaw = bodyRecord.cycles ?? bodyRecord.cycle;
|
||||
if (!cyclesRaw) {
|
||||
return NextResponse.json({ ok: false, error: "Invalid payload" }, { status: 400 });
|
||||
}
|
||||
@@ -85,8 +94,8 @@ export async function POST(req: Request) {
|
||||
}
|
||||
|
||||
const fallbackTsMs =
|
||||
(typeof raw?.tsMs === "number" && raw.tsMs) ||
|
||||
(typeof raw?.tsDevice === "number" && raw.tsDevice) ||
|
||||
(typeof bodyRecord.tsMs === "number" && bodyRecord.tsMs) ||
|
||||
(typeof bodyRecord.tsDevice === "number" && bodyRecord.tsDevice) ||
|
||||
undefined;
|
||||
|
||||
const rows = parsedCycles.data.map((data) => {
|
||||
|
||||
@@ -3,13 +3,19 @@ import { prisma } from "@/lib/prisma";
|
||||
import { getMachineAuth } from "@/lib/machineAuthCache";
|
||||
import { z } from "zod";
|
||||
import { evaluateAlertsForEvent } from "@/lib/alerts/engine";
|
||||
import { toJsonValue } from "@/lib/prismaJson";
|
||||
|
||||
const normalizeType = (t: any) =>
|
||||
const normalizeType = (t: unknown) =>
|
||||
String(t ?? "")
|
||||
.trim()
|
||||
.toLowerCase()
|
||||
.replace(/_/g, "-");
|
||||
|
||||
function asRecord(value: unknown): Record<string, unknown> | null {
|
||||
if (!value || typeof value !== "object" || Array.isArray(value)) return null;
|
||||
return value as Record<string, unknown>;
|
||||
}
|
||||
|
||||
const CANON_TYPE: Record<string, string> = {
|
||||
// Node-RED
|
||||
"production-stopped": "stop",
|
||||
@@ -56,29 +62,35 @@ 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: any = await req.json().catch(() => null);
|
||||
let body: unknown = await req.json().catch(() => null);
|
||||
|
||||
// ✅ if Node-RED sent an array as the whole body, unwrap it
|
||||
if (Array.isArray(body)) body = body[0];
|
||||
const bodyRecord = asRecord(body) ?? {};
|
||||
const payloadRecord = asRecord(bodyRecord.payload) ?? {};
|
||||
|
||||
// ✅ accept multiple common keys
|
||||
const machineId = body?.machineId ?? body?.machine_id ?? body?.machine?.id;
|
||||
const machineId =
|
||||
bodyRecord.machineId ??
|
||||
bodyRecord.machine_id ??
|
||||
(asRecord(bodyRecord.machine)?.id ?? null);
|
||||
let rawEvent =
|
||||
body?.event ??
|
||||
body?.events ??
|
||||
body?.anomalies ??
|
||||
body?.payload?.event ??
|
||||
body?.payload?.events ??
|
||||
body?.payload?.anomalies ??
|
||||
body?.payload ??
|
||||
body?.data; // sometimes "data"
|
||||
bodyRecord.event ??
|
||||
bodyRecord.events ??
|
||||
bodyRecord.anomalies ??
|
||||
payloadRecord.event ??
|
||||
payloadRecord.events ??
|
||||
payloadRecord.anomalies ??
|
||||
payloadRecord ??
|
||||
bodyRecord.data; // sometimes "data"
|
||||
|
||||
if (rawEvent?.event && typeof rawEvent.event === "object") rawEvent = rawEvent.event;
|
||||
if (Array.isArray(rawEvent?.events)) rawEvent = rawEvent.events;
|
||||
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(body ?? {}) } },
|
||||
{ ok: false, error: "Invalid payload", got: { hasMachineId: !!machineId, keys: Object.keys(bodyRecord) } },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
@@ -108,55 +120,50 @@ export async function POST(req: Request) {
|
||||
}
|
||||
|
||||
const created: { id: string; ts: Date; eventType: string }[] = [];
|
||||
const skipped: any[] = [];
|
||||
const skipped: Array<Record<string, unknown>> = [];
|
||||
|
||||
for (const ev of events) {
|
||||
if (!ev || typeof ev !== "object") {
|
||||
const evRecord = asRecord(ev);
|
||||
if (!evRecord) {
|
||||
skipped.push({ reason: "invalid_event_object" });
|
||||
continue;
|
||||
}
|
||||
const evData = asRecord(evRecord.data) ?? {};
|
||||
|
||||
const rawType = (ev as any).eventType ?? (ev as any).anomaly_type ?? (ev as any).topic ?? body.topic ?? "";
|
||||
const rawType = evRecord.eventType ?? evRecord.anomaly_type ?? evRecord.topic ?? bodyRecord.topic ?? "";
|
||||
const typ0 = normalizeType(rawType);
|
||||
const typ = CANON_TYPE[typ0] ?? typ0;
|
||||
|
||||
// Determine timestamp
|
||||
const tsMs =
|
||||
(typeof (ev as any)?.timestamp === "number" && (ev as any).timestamp) ||
|
||||
(typeof (ev as any)?.data?.timestamp === "number" && (ev as any).data.timestamp) ||
|
||||
(typeof (ev as any)?.data?.event_timestamp === "number" && (ev as any).data.event_timestamp) ||
|
||||
(typeof evRecord.timestamp === "number" && evRecord.timestamp) ||
|
||||
(typeof evData.timestamp === "number" && evData.timestamp) ||
|
||||
(typeof evData.event_timestamp === "number" && evData.event_timestamp) ||
|
||||
null;
|
||||
|
||||
const ts = tsMs ? new Date(tsMs) : new Date();
|
||||
|
||||
// Severity defaulting (do not skip on severity — store for audit)
|
||||
let sev = String((ev as any).severity ?? "").trim().toLowerCase();
|
||||
let sev = String(evRecord.severity ?? "").trim().toLowerCase();
|
||||
if (!sev) sev = "warning";
|
||||
|
||||
// Stop classification -> microstop/macrostop
|
||||
let finalType = typ;
|
||||
if (typ === "stop") {
|
||||
const stopSec =
|
||||
(typeof (ev as any)?.data?.stoppage_duration_seconds === "number" && (ev as any).data.stoppage_duration_seconds) ||
|
||||
(typeof (ev as any)?.data?.stop_duration_seconds === "number" && (ev as any).data.stop_duration_seconds) ||
|
||||
(typeof evData.stoppage_duration_seconds === "number" && evData.stoppage_duration_seconds) ||
|
||||
(typeof evData.stop_duration_seconds === "number" && evData.stop_duration_seconds) ||
|
||||
null;
|
||||
|
||||
if (stopSec != null) {
|
||||
const theoretical =
|
||||
Number(
|
||||
(ev as any)?.data?.theoretical_cycle_time ??
|
||||
(ev as any)?.data?.theoreticalCycleTime ??
|
||||
0
|
||||
) || 0;
|
||||
const theoretical = Number(evData.theoretical_cycle_time ?? evData.theoreticalCycleTime ?? 0) || 0;
|
||||
|
||||
const microMultiplier = Number(
|
||||
(ev as any)?.data?.micro_threshold_multiplier ??
|
||||
(ev as any)?.data?.threshold_multiplier ??
|
||||
defaultMicroMultiplier
|
||||
evData.micro_threshold_multiplier ?? evData.threshold_multiplier ?? defaultMicroMultiplier
|
||||
);
|
||||
const macroMultiplier = Math.max(
|
||||
microMultiplier,
|
||||
Number((ev as any)?.data?.macro_threshold_multiplier ?? defaultMacroMultiplier)
|
||||
Number(evData.macro_threshold_multiplier ?? defaultMacroMultiplier)
|
||||
);
|
||||
|
||||
if (theoretical > 0) {
|
||||
@@ -177,39 +184,60 @@ export async function POST(req: Request) {
|
||||
}
|
||||
|
||||
const title =
|
||||
clampText((ev as any).title, 160) ||
|
||||
clampText(evRecord.title, 160) ||
|
||||
(finalType === "slow-cycle" ? "Slow Cycle Detected" :
|
||||
finalType === "macrostop" ? "Macrostop Detected" :
|
||||
finalType === "microstop" ? "Microstop Detected" :
|
||||
"Event");
|
||||
|
||||
const description = clampText((ev as any).description, 1000);
|
||||
const description = clampText(evRecord.description, 1000);
|
||||
|
||||
// store full blob, ensure object
|
||||
const rawData = (ev as any).data ?? ev;
|
||||
const dataObj = typeof rawData === "string" ? (() => {
|
||||
try { return JSON.parse(rawData); } catch { return { raw: rawData }; }
|
||||
})() : rawData;
|
||||
const rawData = evRecord.data ?? evRecord;
|
||||
const parsedData = typeof rawData === "string"
|
||||
? (() => {
|
||||
try {
|
||||
return JSON.parse(rawData);
|
||||
} catch {
|
||||
return { raw: rawData };
|
||||
}
|
||||
})()
|
||||
: rawData;
|
||||
const dataObj: Record<string, unknown> =
|
||||
parsedData && typeof parsedData === "object" && !Array.isArray(parsedData)
|
||||
? { ...(parsedData as Record<string, unknown>) }
|
||||
: { raw: parsedData };
|
||||
if (evRecord.status != null && dataObj.status == null) dataObj.status = evRecord.status;
|
||||
if (evRecord.alert_id != null && dataObj.alert_id == null) dataObj.alert_id = evRecord.alert_id;
|
||||
if (evRecord.is_update != null && dataObj.is_update == null) dataObj.is_update = evRecord.is_update;
|
||||
if (evRecord.is_auto_ack != null && dataObj.is_auto_ack == null) dataObj.is_auto_ack = evRecord.is_auto_ack;
|
||||
|
||||
const activeWorkOrder = asRecord(evRecord.activeWorkOrder);
|
||||
const dataActiveWorkOrder = asRecord(evData.activeWorkOrder);
|
||||
|
||||
const row = await prisma.machineEvent.create({
|
||||
data: {
|
||||
orgId: machine.orgId,
|
||||
machineId: machine.id,
|
||||
ts,
|
||||
topic: clampText((ev as any).topic ?? finalType, 64) ?? finalType,
|
||||
topic: clampText(evRecord.topic ?? finalType, 64) ?? finalType,
|
||||
eventType: finalType,
|
||||
severity: sev,
|
||||
requiresAck: !!(ev as any).requires_ack,
|
||||
requiresAck: !!evRecord.requires_ack,
|
||||
title,
|
||||
description,
|
||||
data: dataObj,
|
||||
data: toJsonValue(dataObj),
|
||||
workOrderId:
|
||||
clampText((ev as any)?.work_order_id, 64) ??
|
||||
clampText((ev as any)?.data?.work_order_id, 64) ??
|
||||
clampText(evRecord.work_order_id, 64) ??
|
||||
clampText(evData.work_order_id, 64) ??
|
||||
clampText(activeWorkOrder?.id, 64) ??
|
||||
clampText(dataActiveWorkOrder?.id, 64) ??
|
||||
null,
|
||||
sku:
|
||||
clampText((ev as any)?.sku, 64) ??
|
||||
clampText((ev as any)?.data?.sku, 64) ??
|
||||
clampText(evRecord.sku, 64) ??
|
||||
clampText(evData.sku, 64) ??
|
||||
clampText(activeWorkOrder?.sku, 64) ??
|
||||
clampText(dataActiveWorkOrder?.sku, 64) ??
|
||||
null,
|
||||
},
|
||||
});
|
||||
|
||||
@@ -2,6 +2,7 @@ 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");
|
||||
@@ -24,7 +25,7 @@ export async function POST(req: Request) {
|
||||
const ip = getClientIp(req);
|
||||
const userAgent = req.headers.get("user-agent");
|
||||
|
||||
let rawBody: any = null;
|
||||
let rawBody: unknown = null;
|
||||
let orgId: string | null = null;
|
||||
let machineId: string | null = null;
|
||||
let seq: bigint | null = null;
|
||||
@@ -48,7 +49,16 @@ export async function POST(req: Request) {
|
||||
const normalized = normalizeHeartbeatV1(rawBody);
|
||||
if (!normalized.ok) {
|
||||
await prisma.ingestLog.create({
|
||||
data: { endpoint, ok: false, status: 400, errorCode: "INVALID_PAYLOAD", errorMsg: normalized.error, body: rawBody, ip, userAgent },
|
||||
data: {
|
||||
endpoint,
|
||||
ok: false,
|
||||
status: 400,
|
||||
errorCode: "INVALID_PAYLOAD",
|
||||
errorMsg: normalized.error,
|
||||
body: toJsonValue(rawBody),
|
||||
ip,
|
||||
userAgent,
|
||||
},
|
||||
});
|
||||
return NextResponse.json({ ok: false, error: "Invalid payload", detail: normalized.error }, { status: 400 });
|
||||
}
|
||||
@@ -70,7 +80,7 @@ export async function POST(req: Request) {
|
||||
status: 401,
|
||||
errorCode: "UNAUTHORIZED",
|
||||
errorMsg: "Unauthorized (machineId/apiKey mismatch)",
|
||||
body: rawBody,
|
||||
body: toJsonValue(rawBody),
|
||||
machineId,
|
||||
schemaVersion,
|
||||
seq,
|
||||
@@ -123,8 +133,8 @@ export async function POST(req: Request) {
|
||||
tsDevice: hb.ts,
|
||||
tsServer: hb.tsServer,
|
||||
});
|
||||
} catch (err: any) {
|
||||
const msg = err?.message ? String(err.message) : "Unknown error";
|
||||
} catch (err: unknown) {
|
||||
const msg = err instanceof Error ? err.message : "Unknown error";
|
||||
|
||||
try {
|
||||
await prisma.ingestLog.create({
|
||||
@@ -139,7 +149,7 @@ export async function POST(req: Request) {
|
||||
schemaVersion,
|
||||
seq,
|
||||
tsDevice: tsDeviceDate ?? undefined,
|
||||
body: rawBody,
|
||||
body: toJsonValue(rawBody),
|
||||
ip,
|
||||
userAgent,
|
||||
},
|
||||
|
||||
@@ -3,6 +3,7 @@ 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";
|
||||
|
||||
function getClientIp(req: Request) {
|
||||
const xf = req.headers.get("x-forwarded-for");
|
||||
@@ -26,7 +27,7 @@ export async function POST(req: Request) {
|
||||
const ip = getClientIp(req);
|
||||
const userAgent = req.headers.get("user-agent");
|
||||
|
||||
let rawBody: any = null;
|
||||
let rawBody: unknown = null;
|
||||
let orgId: string | null = null;
|
||||
let machineId: string | null = null;
|
||||
let seq: bigint | null = null;
|
||||
@@ -60,7 +61,7 @@ export async function POST(req: Request) {
|
||||
status: 400,
|
||||
errorCode: "INVALID_PAYLOAD",
|
||||
errorMsg: normalized.error,
|
||||
body: rawBody,
|
||||
body: toJsonValue(rawBody),
|
||||
ip,
|
||||
userAgent,
|
||||
},
|
||||
@@ -86,7 +87,7 @@ export async function POST(req: Request) {
|
||||
status: 401,
|
||||
errorCode: "UNAUTHORIZED",
|
||||
errorMsg: "Unauthorized (machineId/apiKey mismatch)",
|
||||
body: rawBody,
|
||||
body: toJsonValue(rawBody),
|
||||
machineId,
|
||||
schemaVersion,
|
||||
seq,
|
||||
@@ -100,19 +101,37 @@ export async function POST(req: Request) {
|
||||
|
||||
orgId = machine.orgId;
|
||||
|
||||
const wo = body.activeWorkOrder ?? {};
|
||||
const good = typeof wo.good === "number" ? wo.good : (typeof wo.goodParts === "number" ? wo.goodParts : null);
|
||||
const scrap = typeof wo.scrap === "number" ? wo.scrap : (typeof wo.scrapParts === "number" ? wo.scrapParts : null)
|
||||
const woRecord = (body.activeWorkOrder ?? {}) as Record<string, unknown>;
|
||||
const good =
|
||||
typeof woRecord.good === "number"
|
||||
? woRecord.good
|
||||
: typeof woRecord.goodParts === "number"
|
||||
? woRecord.goodParts
|
||||
: typeof woRecord.good_parts === "number"
|
||||
? woRecord.good_parts
|
||||
: null;
|
||||
const scrap =
|
||||
typeof woRecord.scrap === "number"
|
||||
? woRecord.scrap
|
||||
: typeof woRecord.scrapParts === "number"
|
||||
? woRecord.scrapParts
|
||||
: typeof woRecord.scrap_parts === "number"
|
||||
? woRecord.scrap_parts
|
||||
: null;
|
||||
const k = body.kpis ?? {};
|
||||
const safeCycleTime =
|
||||
typeof body.cycleTime === "number" && body.cycleTime > 0
|
||||
? body.cycleTime
|
||||
: (typeof (wo as any).cycleTime === "number" && (wo as any).cycleTime > 0 ? (wo as any).cycleTime : null);
|
||||
typeof body.cycleTime === "number" && body.cycleTime > 0
|
||||
? body.cycleTime
|
||||
: typeof woRecord.cycleTime === "number" && woRecord.cycleTime > 0
|
||||
? woRecord.cycleTime
|
||||
: null;
|
||||
|
||||
const safeCavities =
|
||||
typeof body.cavities === "number" && body.cavities > 0
|
||||
? body.cavities
|
||||
: (typeof (wo as any).cavities === "number" && (wo as any).cavities > 0 ? (wo as any).cavities : null);
|
||||
const safeCavities =
|
||||
typeof body.cavities === "number" && body.cavities > 0
|
||||
? body.cavities
|
||||
: typeof woRecord.cavities === "number" && woRecord.cavities > 0
|
||||
? woRecord.cavities
|
||||
: null;
|
||||
// Write snapshot (ts = tsDevice; tsServer auto)
|
||||
const row = await prisma.machineKpiSnapshot.create({
|
||||
data: {
|
||||
@@ -125,9 +144,9 @@ export async function POST(req: Request) {
|
||||
ts: tsDeviceDate, // store device-time in ts; server-time goes to ts_server
|
||||
|
||||
// Work order fields
|
||||
workOrderId: wo.id ? String(wo.id) : null,
|
||||
sku: wo.sku ? String(wo.sku) : null,
|
||||
target: typeof wo.target === "number" ? Math.trunc(wo.target) : null,
|
||||
workOrderId: woRecord.id != null ? String(woRecord.id) : null,
|
||||
sku: woRecord.sku != null ? String(woRecord.sku) : null,
|
||||
target: typeof woRecord.target === "number" ? Math.trunc(woRecord.target) : null,
|
||||
good: good != null ? Math.trunc(good) : null,
|
||||
scrap: scrap != null ? Math.trunc(scrap) : null,
|
||||
|
||||
@@ -169,8 +188,8 @@ export async function POST(req: Request) {
|
||||
tsDevice: row.ts,
|
||||
tsServer: row.tsServer,
|
||||
});
|
||||
} catch (err: any) {
|
||||
const msg = err?.message ? String(err.message) : "Unknown error";
|
||||
} catch (err: unknown) {
|
||||
const msg = err instanceof Error ? err.message : "Unknown error";
|
||||
|
||||
// Never fail the request because logging failed
|
||||
try {
|
||||
@@ -186,7 +205,7 @@ export async function POST(req: Request) {
|
||||
schemaVersion,
|
||||
seq,
|
||||
tsDevice: tsDeviceDate ?? undefined,
|
||||
body: rawBody,
|
||||
body: toJsonValue(rawBody),
|
||||
ip,
|
||||
userAgent,
|
||||
},
|
||||
|
||||
@@ -1,177 +1,9 @@
|
||||
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";
|
||||
|
||||
function normalizeEvent(
|
||||
row: any,
|
||||
thresholds: { microMultiplier: number; macroMultiplier: number }
|
||||
) {
|
||||
// -----------------------------
|
||||
// 1) Parse row.data safely
|
||||
// data may be:
|
||||
// - object
|
||||
// - array of objects
|
||||
// - JSON string of either
|
||||
// -----------------------------
|
||||
const raw = row.data;
|
||||
|
||||
let parsed: any = raw;
|
||||
if (typeof raw === "string") {
|
||||
try {
|
||||
parsed = JSON.parse(raw);
|
||||
} catch {
|
||||
parsed = raw; // keep as string if not JSON
|
||||
}
|
||||
}
|
||||
|
||||
// data can be object OR [object]
|
||||
const blob = Array.isArray(parsed) ? parsed[0] : parsed;
|
||||
|
||||
// some payloads nest details under blob.data
|
||||
const inner = blob?.data ?? blob ?? {};
|
||||
|
||||
const normalizeType = (t: any) =>
|
||||
String(t ?? "")
|
||||
.trim()
|
||||
.toLowerCase()
|
||||
.replace(/_/g, "-");
|
||||
|
||||
// -----------------------------
|
||||
// 2) Alias mapping (canonical types)
|
||||
// -----------------------------
|
||||
const ALIAS: Record<string, string> = {
|
||||
// Spanish / synonyms
|
||||
macroparo: "macrostop",
|
||||
"macro-stop": "macrostop",
|
||||
macro_stop: "macrostop",
|
||||
|
||||
microparo: "microstop",
|
||||
"micro-paro": "microstop",
|
||||
micro_stop: "microstop",
|
||||
|
||||
// Node-RED types
|
||||
"production-stopped": "stop", // we'll classify to micro/macro below
|
||||
|
||||
// legacy / generic
|
||||
down: "stop",
|
||||
};
|
||||
|
||||
// -----------------------------
|
||||
// 3) Determine event type from DB or blob
|
||||
// -----------------------------
|
||||
const fromDbType =
|
||||
row.eventType && row.eventType !== "unknown" ? row.eventType : null;
|
||||
|
||||
const fromBlobType =
|
||||
blob?.anomaly_type ??
|
||||
blob?.eventType ??
|
||||
blob?.topic ??
|
||||
inner?.anomaly_type ??
|
||||
inner?.eventType ??
|
||||
null;
|
||||
|
||||
// infer slow-cycle if signature exists
|
||||
const inferredType =
|
||||
fromDbType ??
|
||||
fromBlobType ??
|
||||
((inner?.actual_cycle_time && inner?.theoretical_cycle_time) ||
|
||||
(blob?.actual_cycle_time && blob?.theoretical_cycle_time)
|
||||
? "slow-cycle"
|
||||
: "unknown");
|
||||
|
||||
const eventTypeRaw = normalizeType(inferredType);
|
||||
let eventType = ALIAS[eventTypeRaw] ?? eventTypeRaw;
|
||||
|
||||
// -----------------------------
|
||||
// 4) Optional: classify "stop" into micro/macro based on duration if present
|
||||
// (keeps old rows usable even if they stored production-stopped)
|
||||
// -----------------------------
|
||||
if (eventType === "stop") {
|
||||
const stopSec =
|
||||
(typeof inner?.stoppage_duration_seconds === "number" && inner.stoppage_duration_seconds) ||
|
||||
(typeof blob?.stoppage_duration_seconds === "number" && blob.stoppage_duration_seconds) ||
|
||||
(typeof inner?.stop_duration_seconds === "number" && inner.stop_duration_seconds) ||
|
||||
null;
|
||||
|
||||
const microMultiplier = Number(thresholds?.microMultiplier ?? 1.5);
|
||||
const macroMultiplier = Math.max(
|
||||
microMultiplier,
|
||||
Number(thresholds?.macroMultiplier ?? 5)
|
||||
);
|
||||
|
||||
const theoreticalCycle =
|
||||
Number(inner?.theoretical_cycle_time ?? blob?.theoretical_cycle_time) || 0;
|
||||
|
||||
if (stopSec != null) {
|
||||
if (theoreticalCycle > 0) {
|
||||
const macroThresholdSec = theoreticalCycle * macroMultiplier;
|
||||
eventType = stopSec >= macroThresholdSec ? "macrostop" : "microstop";
|
||||
} else {
|
||||
const fallbackMacroSec = 300;
|
||||
eventType = stopSec >= fallbackMacroSec ? "macrostop" : "microstop";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// -----------------------------
|
||||
// 5) Severity, title, description, timestamp
|
||||
// -----------------------------
|
||||
const severity =
|
||||
String(
|
||||
(row.severity && row.severity !== "info" ? row.severity : null) ??
|
||||
blob?.severity ??
|
||||
inner?.severity ??
|
||||
"info"
|
||||
)
|
||||
.trim()
|
||||
.toLowerCase();
|
||||
|
||||
const title =
|
||||
String(
|
||||
(row.title && row.title !== "Event" ? row.title : null) ??
|
||||
blob?.title ??
|
||||
inner?.title ??
|
||||
(eventType === "slow-cycle" ? "Slow Cycle Detected" : "Event")
|
||||
).trim();
|
||||
|
||||
const description =
|
||||
row.description ??
|
||||
blob?.description ??
|
||||
inner?.description ??
|
||||
(eventType === "slow-cycle" &&
|
||||
(inner?.actual_cycle_time ?? blob?.actual_cycle_time) &&
|
||||
(inner?.theoretical_cycle_time ?? blob?.theoretical_cycle_time) &&
|
||||
(inner?.delta_percent ?? blob?.delta_percent) != null
|
||||
? `Cycle took ${Number(inner?.actual_cycle_time ?? blob?.actual_cycle_time).toFixed(1)}s (+${Number(inner?.delta_percent ?? blob?.delta_percent)}% vs ${Number(inner?.theoretical_cycle_time ?? blob?.theoretical_cycle_time).toFixed(1)}s objetivo)`
|
||||
: null);
|
||||
|
||||
const ts =
|
||||
row.ts ??
|
||||
(typeof blob?.timestamp === "number" ? new Date(blob.timestamp) : null) ??
|
||||
(typeof inner?.timestamp === "number" ? new Date(inner.timestamp) : null) ??
|
||||
null;
|
||||
|
||||
const workOrderId =
|
||||
row.workOrderId ??
|
||||
blob?.work_order_id ??
|
||||
inner?.work_order_id ??
|
||||
null;
|
||||
|
||||
return {
|
||||
id: row.id,
|
||||
ts,
|
||||
topic: String(row.topic ?? blob?.topic ?? eventType),
|
||||
eventType,
|
||||
severity,
|
||||
title,
|
||||
description,
|
||||
requiresAck: !!row.requiresAck,
|
||||
workOrderId,
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
import { normalizeEvent } from "@/lib/events/normalizeEvent";
|
||||
|
||||
|
||||
export async function GET(
|
||||
@@ -188,9 +20,89 @@ export async function GET(
|
||||
const eventsOnly = url.searchParams.get("eventsOnly") === "1";
|
||||
const eventsWindowSec = Number(url.searchParams.get("eventsWindowSec") ?? "21600"); // default 6h
|
||||
const eventsWindowStart = new Date(Date.now() - Math.max(0, eventsWindowSec) * 1000);
|
||||
const windowSec = Number(url.searchParams.get("windowSec") ?? "3600"); // default 1h
|
||||
|
||||
const { machineId } = await params;
|
||||
|
||||
const machineBase = await prisma.machine.findFirst({
|
||||
where: { id: machineId, orgId: session.orgId },
|
||||
select: { id: true, updatedAt: true },
|
||||
});
|
||||
|
||||
if (!machineBase) {
|
||||
return NextResponse.json({ ok: false, error: "Not found" }, { status: 404 });
|
||||
}
|
||||
|
||||
const [heartbeatAgg, kpiAgg, eventAgg, cycleAgg, orgSettingsAgg] = await Promise.all([
|
||||
prisma.machineHeartbeat.aggregate({
|
||||
where: { orgId: session.orgId, machineId },
|
||||
_max: { tsServer: true },
|
||||
}),
|
||||
prisma.machineKpiSnapshot.aggregate({
|
||||
where: { orgId: session.orgId, machineId },
|
||||
_max: { tsServer: true },
|
||||
}),
|
||||
prisma.machineEvent.aggregate({
|
||||
where: { orgId: session.orgId, machineId, ts: { gte: eventsWindowStart } },
|
||||
_max: { tsServer: true },
|
||||
}),
|
||||
prisma.machineCycle.aggregate({
|
||||
where: { orgId: session.orgId, machineId },
|
||||
_max: { ts: true },
|
||||
}),
|
||||
prisma.orgSettings.findUnique({
|
||||
where: { orgId: session.orgId },
|
||||
select: { updatedAt: true, stoppageMultiplier: true, macroStoppageMultiplier: true },
|
||||
}),
|
||||
]);
|
||||
|
||||
const toMs = (value?: Date | null) => (value ? value.getTime() : 0);
|
||||
const lastModifiedMs = Math.max(
|
||||
toMs(machineBase.updatedAt),
|
||||
toMs(heartbeatAgg._max.tsServer),
|
||||
toMs(kpiAgg._max.tsServer),
|
||||
toMs(eventAgg._max.tsServer),
|
||||
toMs(cycleAgg._max.ts),
|
||||
toMs(orgSettingsAgg?.updatedAt)
|
||||
);
|
||||
|
||||
const versionParts = [
|
||||
session.orgId,
|
||||
machineId,
|
||||
eventsMode,
|
||||
eventsOnly ? "1" : "0",
|
||||
eventsWindowSec,
|
||||
windowSec,
|
||||
toMs(machineBase.updatedAt),
|
||||
toMs(heartbeatAgg._max.tsServer),
|
||||
toMs(kpiAgg._max.tsServer),
|
||||
toMs(eventAgg._max.tsServer),
|
||||
toMs(cycleAgg._max.ts),
|
||||
toMs(orgSettingsAgg?.updatedAt),
|
||||
];
|
||||
|
||||
const etag = `W/"${createHash("sha1").update(versionParts.join("|")).digest("hex")}"`;
|
||||
const lastModified = new Date(lastModifiedMs || 0).toUTCString();
|
||||
const responseHeaders = new Headers({
|
||||
"Cache-Control": "private, no-cache, max-age=0, must-revalidate",
|
||||
ETag: etag,
|
||||
"Last-Modified": lastModified,
|
||||
Vary: "Cookie",
|
||||
});
|
||||
|
||||
const ifNoneMatch = _req.headers.get("if-none-match");
|
||||
if (ifNoneMatch && ifNoneMatch === etag) {
|
||||
return new NextResponse(null, { status: 304, headers: responseHeaders });
|
||||
}
|
||||
|
||||
const ifModifiedSince = _req.headers.get("if-modified-since");
|
||||
if (!ifNoneMatch && ifModifiedSince) {
|
||||
const since = Date.parse(ifModifiedSince);
|
||||
if (!Number.isNaN(since) && lastModifiedMs <= since) {
|
||||
return new NextResponse(null, { status: 304, headers: responseHeaders });
|
||||
}
|
||||
}
|
||||
|
||||
const machine = await prisma.machine.findFirst({
|
||||
where: { id: machineId, orgId: session.orgId },
|
||||
select: {
|
||||
@@ -227,15 +139,10 @@ export async function GET(
|
||||
return NextResponse.json({ ok: false, error: "Not found" }, { status: 404 });
|
||||
}
|
||||
|
||||
const orgSettings = await prisma.orgSettings.findUnique({
|
||||
where: { orgId: session.orgId },
|
||||
select: { stoppageMultiplier: true, macroStoppageMultiplier: true },
|
||||
});
|
||||
|
||||
const microMultiplier = Number(orgSettings?.stoppageMultiplier ?? 1.5);
|
||||
const microMultiplier = Number(orgSettingsAgg?.stoppageMultiplier ?? 1.5);
|
||||
const macroMultiplier = Math.max(
|
||||
microMultiplier,
|
||||
Number(orgSettings?.macroStoppageMultiplier ?? 5)
|
||||
Number(orgSettingsAgg?.macroStoppageMultiplier ?? 5)
|
||||
);
|
||||
|
||||
const rawEvents = await prisma.machineEvent.findMany({
|
||||
@@ -296,12 +203,14 @@ export async function GET(
|
||||
const eventsCountCritical = allEvents.filter(isCritical).length;
|
||||
|
||||
if (eventsOnly) {
|
||||
return NextResponse.json({ ok: true, events, eventsCountAll, eventsCountCritical });
|
||||
return NextResponse.json(
|
||||
{ ok: true, events, eventsCountAll, eventsCountCritical },
|
||||
{ headers: responseHeaders }
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
// ---- cycles window ----
|
||||
const windowSec = Number(url.searchParams.get("windowSec") ?? "3600"); // default 1h
|
||||
|
||||
const latestKpi = machine.kpiSnapshots[0] ?? null;
|
||||
|
||||
@@ -380,30 +289,28 @@ const cycles = rawCycles
|
||||
workOrderId: c.workOrderId ?? null,
|
||||
sku: c.sku ?? null,
|
||||
}));
|
||||
|
||||
|
||||
|
||||
|
||||
return NextResponse.json({
|
||||
ok: true,
|
||||
machine: {
|
||||
id: machine.id,
|
||||
name: machine.name,
|
||||
code: machine.code,
|
||||
location: machine.location,
|
||||
latestHeartbeat: machine.heartbeats[0] ?? null,
|
||||
latestKpi: machine.kpiSnapshots[0] ?? null,
|
||||
effectiveCycleTime,
|
||||
},
|
||||
thresholds: {
|
||||
stoppageMultiplier: microMultiplier,
|
||||
macroStoppageMultiplier: macroMultiplier,
|
||||
},
|
||||
activeStoppage,
|
||||
events,
|
||||
eventsCountAll,
|
||||
eventsCountCritical,
|
||||
cycles,
|
||||
});
|
||||
|
||||
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,
|
||||
},
|
||||
thresholds: {
|
||||
stoppageMultiplier: microMultiplier,
|
||||
macroStoppageMultiplier: macroMultiplier,
|
||||
},
|
||||
activeStoppage,
|
||||
events,
|
||||
eventsCountAll,
|
||||
eventsCountCritical,
|
||||
cycles,
|
||||
},
|
||||
{ headers: responseHeaders }
|
||||
);
|
||||
}
|
||||
|
||||
@@ -139,10 +139,9 @@ export async function POST(req: Request) {
|
||||
},
|
||||
});
|
||||
break;
|
||||
} catch (err: any) {
|
||||
if (err?.code !== "P2002") {
|
||||
throw err;
|
||||
}
|
||||
} catch (err: unknown) {
|
||||
const code = typeof err === "object" && err !== null ? (err as { code?: string }).code : undefined;
|
||||
if (code !== "P2002") throw err;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -154,8 +154,9 @@ export async function POST(req: Request) {
|
||||
},
|
||||
});
|
||||
break;
|
||||
} catch (err: any) {
|
||||
if (err?.code !== "P2002") throw err;
|
||||
} catch (err: unknown) {
|
||||
const code = typeof err === "object" && err !== null ? (err as { code?: string }).code : undefined;
|
||||
if (code !== "P2002") throw err;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -184,9 +185,9 @@ export async function POST(req: Request) {
|
||||
text: content.text,
|
||||
html: content.html,
|
||||
});
|
||||
} catch (err: any) {
|
||||
} catch (err: unknown) {
|
||||
emailSent = false;
|
||||
emailError = err?.message || "Failed to send invite email";
|
||||
emailError = err instanceof Error ? err.message : "Failed to send invite email";
|
||||
}
|
||||
|
||||
return NextResponse.json({ ok: true, invite, emailSent, emailError });
|
||||
|
||||
101
app/api/overview/route.ts
Normal file
101
app/api/overview/route.ts
Normal file
@@ -0,0 +1,101 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import type { NextRequest } from "next/server";
|
||||
import { createHash } from "crypto";
|
||||
import { prisma } from "@/lib/prisma";
|
||||
import { requireSession } from "@/lib/auth/requireSession";
|
||||
import { getOverviewData } from "@/lib/overview/getOverviewData";
|
||||
|
||||
function toMs(value?: Date | null) {
|
||||
return value ? value.getTime() : 0;
|
||||
}
|
||||
|
||||
export async function GET(req: NextRequest) {
|
||||
const session = await requireSession();
|
||||
if (!session) {
|
||||
return NextResponse.json({ ok: false, error: "Unauthorized" }, { status: 401 });
|
||||
}
|
||||
|
||||
const url = new URL(req.url);
|
||||
const eventsMode = url.searchParams.get("events") ?? "critical";
|
||||
const eventsWindowSecRaw = Number(url.searchParams.get("eventsWindowSec") ?? "21600");
|
||||
const eventsWindowSec = Number.isFinite(eventsWindowSecRaw) ? eventsWindowSecRaw : 21600;
|
||||
const eventMachinesRaw = Number(url.searchParams.get("eventMachines") ?? "6");
|
||||
const eventMachines = Number.isFinite(eventMachinesRaw) ? Math.max(1, eventMachinesRaw) : 6;
|
||||
const [machineAgg, heartbeatAgg, kpiAgg, eventAgg, orgSettings] = await Promise.all([
|
||||
prisma.machine.aggregate({
|
||||
where: { orgId: session.orgId },
|
||||
_max: { updatedAt: true },
|
||||
}),
|
||||
prisma.machineHeartbeat.aggregate({
|
||||
where: { orgId: session.orgId },
|
||||
_max: { tsServer: true },
|
||||
}),
|
||||
prisma.machineKpiSnapshot.aggregate({
|
||||
where: { orgId: session.orgId },
|
||||
_max: { tsServer: true },
|
||||
}),
|
||||
prisma.machineEvent.aggregate({
|
||||
where: { orgId: session.orgId },
|
||||
_max: { tsServer: true },
|
||||
}),
|
||||
prisma.orgSettings.findUnique({
|
||||
where: { orgId: session.orgId },
|
||||
select: { updatedAt: true, stoppageMultiplier: true, macroStoppageMultiplier: true },
|
||||
}),
|
||||
]);
|
||||
|
||||
const lastModifiedMs = Math.max(
|
||||
toMs(machineAgg._max.updatedAt),
|
||||
toMs(heartbeatAgg._max.tsServer),
|
||||
toMs(kpiAgg._max.tsServer),
|
||||
toMs(eventAgg._max.tsServer),
|
||||
toMs(orgSettings?.updatedAt)
|
||||
);
|
||||
|
||||
const versionParts = [
|
||||
session.orgId,
|
||||
eventsMode,
|
||||
eventsWindowSec,
|
||||
eventMachines,
|
||||
toMs(machineAgg._max.updatedAt),
|
||||
toMs(heartbeatAgg._max.tsServer),
|
||||
toMs(kpiAgg._max.tsServer),
|
||||
toMs(eventAgg._max.tsServer),
|
||||
toMs(orgSettings?.updatedAt),
|
||||
];
|
||||
|
||||
const etag = `W/"${createHash("sha1").update(versionParts.join("|")).digest("hex")}"`;
|
||||
const lastModified = new Date(lastModifiedMs || 0).toUTCString();
|
||||
const responseHeaders = new Headers({
|
||||
"Cache-Control": "private, no-cache, max-age=0, must-revalidate",
|
||||
ETag: etag,
|
||||
"Last-Modified": lastModified,
|
||||
Vary: "Cookie",
|
||||
});
|
||||
|
||||
const ifNoneMatch = req.headers.get("if-none-match");
|
||||
if (ifNoneMatch && ifNoneMatch === etag) {
|
||||
return new NextResponse(null, { status: 304, headers: responseHeaders });
|
||||
}
|
||||
|
||||
const ifModifiedSince = req.headers.get("if-modified-since");
|
||||
if (!ifNoneMatch && ifModifiedSince) {
|
||||
const since = Date.parse(ifModifiedSince);
|
||||
if (!Number.isNaN(since) && lastModifiedMs <= since) {
|
||||
return new NextResponse(null, { status: 304, headers: responseHeaders });
|
||||
}
|
||||
}
|
||||
|
||||
const { machines: machineRows, events } = await getOverviewData({
|
||||
orgId: session.orgId,
|
||||
eventsMode,
|
||||
eventsWindowSec,
|
||||
eventMachines,
|
||||
orgSettings,
|
||||
});
|
||||
|
||||
return NextResponse.json(
|
||||
{ ok: true, machines: machineRows, events },
|
||||
{ headers: responseHeaders }
|
||||
);
|
||||
}
|
||||
@@ -165,7 +165,7 @@ export async function GET(req: NextRequest) {
|
||||
|
||||
for (const e of events) {
|
||||
const type = String(e.eventType ?? "").toLowerCase();
|
||||
let blob: any = e.data;
|
||||
let blob: unknown = e.data;
|
||||
|
||||
if (typeof blob === "string") {
|
||||
try {
|
||||
@@ -175,7 +175,12 @@ export async function GET(req: NextRequest) {
|
||||
}
|
||||
}
|
||||
|
||||
const inner = blob?.data ?? blob ?? {};
|
||||
const blobRecord = typeof blob === "object" && blob !== null ? (blob as Record<string, unknown>) : null;
|
||||
const innerCandidate = blobRecord?.data ?? blobRecord ?? {};
|
||||
const inner =
|
||||
typeof innerCandidate === "object" && innerCandidate !== null
|
||||
? (innerCandidate as Record<string, unknown>)
|
||||
: {};
|
||||
const stopSec =
|
||||
(typeof inner?.stoppage_duration_seconds === "number" && inner.stoppage_duration_seconds) ||
|
||||
(typeof inner?.stop_duration_seconds === "number" && inner.stop_duration_seconds) ||
|
||||
|
||||
@@ -3,6 +3,7 @@ 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,
|
||||
@@ -18,7 +19,7 @@ import {
|
||||
import { publishSettingsUpdate } from "@/lib/mqtt";
|
||||
import { z } from "zod";
|
||||
|
||||
function isPlainObject(value: any): value is Record<string, any> {
|
||||
function isPlainObject(value: unknown): value is Record<string, unknown> {
|
||||
return !!value && typeof value === "object" && !Array.isArray(value);
|
||||
}
|
||||
|
||||
@@ -34,9 +35,9 @@ const machineSettingsSchema = z
|
||||
})
|
||||
.passthrough();
|
||||
|
||||
function pickAllowedOverrides(raw: any) {
|
||||
function pickAllowedOverrides(raw: unknown) {
|
||||
if (!isPlainObject(raw)) return {};
|
||||
const out: Record<string, any> = {};
|
||||
const out: Record<string, unknown> = {};
|
||||
for (const key of ["shiftSchedule", "thresholds", "alerts", "defaults"]) {
|
||||
if (raw[key] !== undefined) out[key] = raw[key];
|
||||
}
|
||||
@@ -337,24 +338,26 @@ export async function PUT(
|
||||
select: { overridesJson: true },
|
||||
});
|
||||
|
||||
let nextOverrides: any = null;
|
||||
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: nextOverrides,
|
||||
overridesJson: nextOverridesJson,
|
||||
updatedBy: session.userId,
|
||||
},
|
||||
create: {
|
||||
machineId,
|
||||
orgId: session.orgId,
|
||||
overridesJson: nextOverrides,
|
||||
overridesJson: nextOverridesJson,
|
||||
updatedBy: session.userId,
|
||||
},
|
||||
});
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
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";
|
||||
@@ -19,7 +18,15 @@ import {
|
||||
import { publishSettingsUpdate } from "@/lib/mqtt";
|
||||
import { z } from "zod";
|
||||
|
||||
function isPlainObject(value: any): value is Record<string, any> {
|
||||
type ValidShift = {
|
||||
name: string;
|
||||
startTime: string;
|
||||
endTime: string;
|
||||
sortOrder: number;
|
||||
enabled: boolean;
|
||||
};
|
||||
|
||||
function isPlainObject(value: unknown): value is Record<string, unknown> {
|
||||
return !!value && typeof value === "object" && !Array.isArray(value);
|
||||
}
|
||||
|
||||
@@ -191,7 +198,7 @@ export async function PUT(req: Request) {
|
||||
return NextResponse.json({ ok: false, error: defaultsValidation.error }, { status: 400 });
|
||||
}
|
||||
|
||||
let shiftRows: any[] | null = null;
|
||||
let shiftRows: ValidShift[] | null = null;
|
||||
if (shiftSchedule?.shifts !== undefined) {
|
||||
const shiftResult = validateShiftSchedule(shiftSchedule.shifts);
|
||||
if (!shiftResult.ok) {
|
||||
@@ -291,15 +298,15 @@ export async function PUT(req: Request) {
|
||||
return { settings: refreshed, shifts: refreshedShifts };
|
||||
});
|
||||
|
||||
if ((updated as any)?.error === "VERSION_MISMATCH") {
|
||||
if ("error" in updated && updated.error === "VERSION_MISMATCH") {
|
||||
return NextResponse.json(
|
||||
{ ok: false, error: "Version mismatch", currentVersion: (updated as any).currentVersion },
|
||||
{ ok: false, error: "Version mismatch", currentVersion: updated.currentVersion },
|
||||
{ status: 409 }
|
||||
);
|
||||
}
|
||||
|
||||
if ((updated as any)?.error) {
|
||||
return NextResponse.json({ ok: false, error: (updated as any).error }, { status: 400 });
|
||||
if ("error" in updated) {
|
||||
return NextResponse.json({ ok: false, error: updated.error }, { status: 400 });
|
||||
}
|
||||
|
||||
const payload = buildSettingsPayload(updated.settings, updated.shifts ?? []);
|
||||
|
||||
@@ -51,7 +51,7 @@ export async function POST(req: Request) {
|
||||
const verificationToken = randomBytes(24).toString("hex");
|
||||
const verificationExpiresAt = new Date(Date.now() + 24 * 60 * 60 * 1000);
|
||||
|
||||
const result = await prisma.$transaction(async (tx) => {
|
||||
await prisma.$transaction(async (tx) => {
|
||||
const org = await tx.org.create({
|
||||
data: { name: orgName, slug },
|
||||
});
|
||||
@@ -118,14 +118,15 @@ export async function POST(req: Request) {
|
||||
text: emailContent.text,
|
||||
html: emailContent.html,
|
||||
});
|
||||
} catch (err: any) {
|
||||
} catch (err: unknown) {
|
||||
emailSent = false;
|
||||
const error = err as { message?: string; code?: string; response?: unknown; responseCode?: number };
|
||||
logLine("signup.verify_email.failed", {
|
||||
email,
|
||||
message: err?.message,
|
||||
code: err?.code,
|
||||
response: err?.response,
|
||||
responseCode: err?.responseCode,
|
||||
message: error?.message,
|
||||
code: error?.code,
|
||||
response: error?.response,
|
||||
responseCode: error?.responseCode,
|
||||
});
|
||||
}
|
||||
return NextResponse.json({
|
||||
|
||||
@@ -53,19 +53,25 @@ type WorkOrderInput = {
|
||||
cycleTime?: number | null;
|
||||
};
|
||||
|
||||
function normalizeWorkOrders(raw: any[]) {
|
||||
function normalizeWorkOrders(raw: unknown[]) {
|
||||
const seen = new Set<string>();
|
||||
const cleaned: WorkOrderInput[] = [];
|
||||
|
||||
for (const item of raw) {
|
||||
const idRaw = cleanText(item?.workOrderId ?? item?.id ?? item?.work_order_id, MAX_WORK_ORDER_ID_LENGTH);
|
||||
const record = item && typeof item === "object" ? (item as Record<string, unknown>) : {};
|
||||
const idRaw = cleanText(
|
||||
record.workOrderId ?? record.id ?? record.work_order_id,
|
||||
MAX_WORK_ORDER_ID_LENGTH
|
||||
);
|
||||
if (!idRaw || !WORK_ORDER_ID_RE.test(idRaw) || seen.has(idRaw)) continue;
|
||||
seen.add(idRaw);
|
||||
|
||||
const sku = cleanText(item?.sku ?? item?.SKU ?? null, MAX_SKU_LENGTH);
|
||||
const targetQtyRaw = toIntOrNull(item?.targetQty ?? item?.target_qty ?? item?.target ?? item?.targetQuantity);
|
||||
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(
|
||||
item?.cycleTime ?? item?.theoreticalCycleTime ?? item?.theoretical_cycle_time ?? item?.cycle_time
|
||||
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);
|
||||
|
||||
Reference in New Issue
Block a user