Downtime catalog
This commit is contained in:
106
app/api/settings/reason-catalog/categories/[categoryId]/route.ts
Normal file
106
app/api/settings/reason-catalog/categories/[categoryId]/route.ts
Normal file
@@ -0,0 +1,106 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import { prisma } from "@/lib/prisma";
|
||||
import { requireOrgAdminSession } from "@/lib/auth/requireOrgAdminSession";
|
||||
import { bumpOrgSettingsVersion, composeReasonCode } from "@/lib/reasonCatalogDb";
|
||||
import { z } from "zod";
|
||||
|
||||
const PREFIX_RE = /^[A-Za-z][A-Za-z0-9-]*$/;
|
||||
|
||||
const patchSchema = z.object({
|
||||
name: z.string().trim().min(1).max(200).optional(),
|
||||
codePrefix: z
|
||||
.string()
|
||||
.trim()
|
||||
.min(1)
|
||||
.max(32)
|
||||
.transform((s) => s.toUpperCase())
|
||||
.optional(),
|
||||
sortOrder: z.number().int().optional(),
|
||||
active: z.boolean().optional(),
|
||||
});
|
||||
|
||||
export async function PATCH(
|
||||
req: Request,
|
||||
{ params }: { params: Promise<{ categoryId: string }> }
|
||||
) {
|
||||
const auth = await requireOrgAdminSession();
|
||||
if (!auth.ok) return auth.response;
|
||||
|
||||
const { categoryId } = await params;
|
||||
const parsed = patchSchema.safeParse(await req.json().catch(() => null));
|
||||
if (!parsed.success) {
|
||||
return NextResponse.json({ ok: false, error: "Invalid body", issues: parsed.error.flatten() }, { status: 400 });
|
||||
}
|
||||
|
||||
const existing = await prisma.reasonCatalogCategory.findFirst({
|
||||
where: { id: categoryId, orgId: auth.session.orgId },
|
||||
include: { items: true },
|
||||
});
|
||||
if (!existing) return NextResponse.json({ ok: false, error: "Not found" }, { status: 404 });
|
||||
|
||||
const nextPrefix = parsed.data.codePrefix ?? existing.codePrefix;
|
||||
if (parsed.data.codePrefix !== undefined && !PREFIX_RE.test(nextPrefix)) {
|
||||
return NextResponse.json(
|
||||
{ ok: false, error: "codePrefix must start with a letter; letters, digits, hyphen allowed." },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
if (parsed.data.codePrefix !== undefined && parsed.data.codePrefix !== existing.codePrefix) {
|
||||
const proposed = new Set<string>();
|
||||
for (const it of existing.items) {
|
||||
proposed.add(composeReasonCode(nextPrefix, it.codeSuffix));
|
||||
}
|
||||
const codes = [...proposed];
|
||||
const conflicts = await prisma.reasonCatalogItem.findMany({
|
||||
where: {
|
||||
orgId: auth.session.orgId,
|
||||
reasonCode: { in: codes },
|
||||
NOT: { categoryId: existing.id },
|
||||
},
|
||||
select: { reasonCode: true },
|
||||
});
|
||||
if (conflicts.length) {
|
||||
return NextResponse.json(
|
||||
{ ok: false, error: "Prefix change would duplicate codes", conflicts: conflicts.map((c) => c.reasonCode) },
|
||||
{ status: 409 }
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
await prisma.$transaction(async (tx) => {
|
||||
await tx.reasonCatalogCategory.update({
|
||||
where: { id: categoryId },
|
||||
data: {
|
||||
...(parsed.data.name !== undefined ? { name: parsed.data.name } : {}),
|
||||
...(parsed.data.codePrefix !== undefined ? { codePrefix: parsed.data.codePrefix } : {}),
|
||||
...(parsed.data.sortOrder !== undefined ? { sortOrder: parsed.data.sortOrder } : {}),
|
||||
...(parsed.data.active !== undefined ? { active: parsed.data.active } : {}),
|
||||
},
|
||||
});
|
||||
|
||||
if (parsed.data.codePrefix !== undefined && parsed.data.codePrefix !== existing.codePrefix) {
|
||||
for (const it of existing.items) {
|
||||
const reasonCode = composeReasonCode(nextPrefix, it.codeSuffix);
|
||||
await tx.reasonCatalogItem.update({
|
||||
where: { id: it.id },
|
||||
data: { reasonCode },
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
await bumpOrgSettingsVersion(tx, auth.session.orgId, auth.session.userId);
|
||||
});
|
||||
|
||||
const updated = await prisma.reasonCatalogCategory.findUnique({
|
||||
where: { id: categoryId },
|
||||
include: { items: { orderBy: [{ sortOrder: "asc" }, { reasonCode: "asc" }] } },
|
||||
});
|
||||
|
||||
return NextResponse.json({ ok: true, category: updated });
|
||||
} catch (e) {
|
||||
console.error("[reason-catalog category PATCH]", e);
|
||||
return NextResponse.json({ ok: false, error: "Update failed" }, { status: 500 });
|
||||
}
|
||||
}
|
||||
64
app/api/settings/reason-catalog/categories/route.ts
Normal file
64
app/api/settings/reason-catalog/categories/route.ts
Normal file
@@ -0,0 +1,64 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import { prisma } from "@/lib/prisma";
|
||||
import { requireOrgAdminSession } from "@/lib/auth/requireOrgAdminSession";
|
||||
import { bumpOrgSettingsVersion } from "@/lib/reasonCatalogDb";
|
||||
import { z } from "zod";
|
||||
|
||||
const PREFIX_RE = /^[A-Za-z][A-Za-z0-9-]*$/;
|
||||
|
||||
const bodySchema = z.object({
|
||||
kind: z.enum(["downtime", "scrap"]),
|
||||
name: z.string().trim().min(1).max(200),
|
||||
codePrefix: z
|
||||
.string()
|
||||
.trim()
|
||||
.min(1)
|
||||
.max(32)
|
||||
.transform((s) => s.toUpperCase()),
|
||||
});
|
||||
|
||||
export async function POST(req: Request) {
|
||||
const auth = await requireOrgAdminSession();
|
||||
if (!auth.ok) return auth.response;
|
||||
|
||||
const parsed = bodySchema.safeParse(await req.json().catch(() => null));
|
||||
if (!parsed.success) {
|
||||
return NextResponse.json({ ok: false, error: "Invalid body", issues: parsed.error.flatten() }, { status: 400 });
|
||||
}
|
||||
const { kind, name, codePrefix } = parsed.data;
|
||||
if (!PREFIX_RE.test(codePrefix)) {
|
||||
return NextResponse.json(
|
||||
{ ok: false, error: "codePrefix must start with a letter; letters, digits, hyphen allowed." },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
try {
|
||||
const row = await prisma.$transaction(async (tx) => {
|
||||
const last = await tx.reasonCatalogCategory.findFirst({
|
||||
where: { orgId: auth.session.orgId, kind },
|
||||
orderBy: { sortOrder: "desc" },
|
||||
select: { sortOrder: true },
|
||||
});
|
||||
const sortOrder = (last?.sortOrder ?? -1) + 1;
|
||||
|
||||
const created = await tx.reasonCatalogCategory.create({
|
||||
data: {
|
||||
orgId: auth.session.orgId,
|
||||
kind,
|
||||
name,
|
||||
codePrefix,
|
||||
sortOrder,
|
||||
active: true,
|
||||
},
|
||||
});
|
||||
await bumpOrgSettingsVersion(tx, auth.session.orgId, auth.session.userId);
|
||||
return created;
|
||||
});
|
||||
|
||||
return NextResponse.json({ ok: true, category: row });
|
||||
} catch (e) {
|
||||
console.error("[reason-catalog categories POST]", e);
|
||||
return NextResponse.json({ ok: false, error: "Create failed" }, { status: 500 });
|
||||
}
|
||||
}
|
||||
69
app/api/settings/reason-catalog/items/[itemId]/route.ts
Normal file
69
app/api/settings/reason-catalog/items/[itemId]/route.ts
Normal file
@@ -0,0 +1,69 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import { prisma } from "@/lib/prisma";
|
||||
import { requireOrgAdminSession } from "@/lib/auth/requireOrgAdminSession";
|
||||
import { bumpOrgSettingsVersion, composeReasonCode, isNumericSuffix } from "@/lib/reasonCatalogDb";
|
||||
import { z } from "zod";
|
||||
|
||||
const patchSchema = z.object({
|
||||
name: z.string().trim().min(1).max(500).optional(),
|
||||
codeSuffix: z.string().trim().min(1).max(32).optional(),
|
||||
sortOrder: z.number().int().optional(),
|
||||
active: z.boolean().optional(),
|
||||
});
|
||||
|
||||
export async function PATCH(
|
||||
req: Request,
|
||||
{ params }: { params: Promise<{ itemId: string }> }
|
||||
) {
|
||||
const auth = await requireOrgAdminSession();
|
||||
if (!auth.ok) return auth.response;
|
||||
|
||||
const { itemId } = await params;
|
||||
const parsed = patchSchema.safeParse(await req.json().catch(() => null));
|
||||
if (!parsed.success) {
|
||||
return NextResponse.json({ ok: false, error: "Invalid body", issues: parsed.error.flatten() }, { status: 400 });
|
||||
}
|
||||
|
||||
const existing = await prisma.reasonCatalogItem.findFirst({
|
||||
where: { id: itemId, orgId: auth.session.orgId },
|
||||
include: { category: true },
|
||||
});
|
||||
if (!existing) return NextResponse.json({ ok: false, error: "Not found" }, { status: 404 });
|
||||
|
||||
const nextSuffix = parsed.data.codeSuffix ?? existing.codeSuffix;
|
||||
if (parsed.data.codeSuffix !== undefined && !isNumericSuffix(nextSuffix)) {
|
||||
return NextResponse.json({ ok: false, error: "codeSuffix must be digits only" }, { status: 400 });
|
||||
}
|
||||
|
||||
const reasonCode = composeReasonCode(existing.category.codePrefix, nextSuffix);
|
||||
if (reasonCode !== existing.reasonCode) {
|
||||
const conflict = await prisma.reasonCatalogItem.findFirst({
|
||||
where: { orgId: auth.session.orgId, reasonCode, NOT: { id: itemId } },
|
||||
select: { id: true },
|
||||
});
|
||||
if (conflict) {
|
||||
return NextResponse.json({ ok: false, error: "Duplicate reasonCode for this organization" }, { status: 409 });
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
await prisma.$transaction(async (tx) => {
|
||||
await tx.reasonCatalogItem.update({
|
||||
where: { id: itemId },
|
||||
data: {
|
||||
...(parsed.data.name !== undefined ? { name: parsed.data.name } : {}),
|
||||
...(parsed.data.codeSuffix !== undefined ? { codeSuffix: nextSuffix, reasonCode } : {}),
|
||||
...(parsed.data.sortOrder !== undefined ? { sortOrder: parsed.data.sortOrder } : {}),
|
||||
...(parsed.data.active !== undefined ? { active: parsed.data.active } : {}),
|
||||
},
|
||||
});
|
||||
await bumpOrgSettingsVersion(tx, auth.session.orgId, auth.session.userId);
|
||||
});
|
||||
|
||||
const updated = await prisma.reasonCatalogItem.findUnique({ where: { id: itemId } });
|
||||
return NextResponse.json({ ok: true, item: updated });
|
||||
} catch (e) {
|
||||
console.error("[reason-catalog item PATCH]", e);
|
||||
return NextResponse.json({ ok: false, error: "Update failed" }, { status: 500 });
|
||||
}
|
||||
}
|
||||
71
app/api/settings/reason-catalog/items/route.ts
Normal file
71
app/api/settings/reason-catalog/items/route.ts
Normal file
@@ -0,0 +1,71 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import { prisma } from "@/lib/prisma";
|
||||
import { requireOrgAdminSession } from "@/lib/auth/requireOrgAdminSession";
|
||||
import { bumpOrgSettingsVersion, composeReasonCode, isNumericSuffix } from "@/lib/reasonCatalogDb";
|
||||
import { z } from "zod";
|
||||
|
||||
const bodySchema = z.object({
|
||||
categoryId: z.string().uuid(),
|
||||
codeSuffix: z.string().trim().min(1).max(32),
|
||||
name: z.string().trim().min(1).max(500),
|
||||
sortOrder: z.number().int().optional(),
|
||||
});
|
||||
|
||||
export async function POST(req: Request) {
|
||||
const auth = await requireOrgAdminSession();
|
||||
if (!auth.ok) return auth.response;
|
||||
|
||||
const parsed = bodySchema.safeParse(await req.json().catch(() => null));
|
||||
if (!parsed.success) {
|
||||
return NextResponse.json({ ok: false, error: "Invalid body", issues: parsed.error.flatten() }, { status: 400 });
|
||||
}
|
||||
|
||||
const { categoryId, codeSuffix, name, sortOrder } = parsed.data;
|
||||
if (!isNumericSuffix(codeSuffix)) {
|
||||
return NextResponse.json({ ok: false, error: "codeSuffix must be digits only" }, { status: 400 });
|
||||
}
|
||||
|
||||
const category = await prisma.reasonCatalogCategory.findFirst({
|
||||
where: { id: categoryId, orgId: auth.session.orgId },
|
||||
});
|
||||
if (!category) return NextResponse.json({ ok: false, error: "Category not found" }, { status: 404 });
|
||||
|
||||
const reasonCode = composeReasonCode(category.codePrefix, codeSuffix);
|
||||
|
||||
try {
|
||||
const row = await prisma.$transaction(async (tx) => {
|
||||
let nextOrder = sortOrder;
|
||||
if (nextOrder === undefined) {
|
||||
const last = await tx.reasonCatalogItem.findFirst({
|
||||
where: { categoryId },
|
||||
orderBy: { sortOrder: "desc" },
|
||||
select: { sortOrder: true },
|
||||
});
|
||||
nextOrder = (last?.sortOrder ?? -1) + 1;
|
||||
}
|
||||
|
||||
const created = await tx.reasonCatalogItem.create({
|
||||
data: {
|
||||
orgId: auth.session.orgId,
|
||||
categoryId,
|
||||
name,
|
||||
codeSuffix,
|
||||
reasonCode,
|
||||
sortOrder: nextOrder,
|
||||
active: true,
|
||||
},
|
||||
});
|
||||
await bumpOrgSettingsVersion(tx, auth.session.orgId, auth.session.userId);
|
||||
return created;
|
||||
});
|
||||
|
||||
return NextResponse.json({ ok: true, item: row });
|
||||
} catch (e: unknown) {
|
||||
const code = typeof e === "object" && e && "code" in e ? (e as { code: string }).code : "";
|
||||
if (code === "P2002") {
|
||||
return NextResponse.json({ ok: false, error: "Duplicate reasonCode for this organization" }, { status: 409 });
|
||||
}
|
||||
console.error("[reason-catalog items POST]", e);
|
||||
return NextResponse.json({ ok: false, error: "Create failed" }, { status: 500 });
|
||||
}
|
||||
}
|
||||
43
app/api/settings/reason-catalog/route.ts
Normal file
43
app/api/settings/reason-catalog/route.ts
Normal file
@@ -0,0 +1,43 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import { prisma } from "@/lib/prisma";
|
||||
import { requireOrgAdminSession } from "@/lib/auth/requireOrgAdminSession";
|
||||
|
||||
/** Full tree for Control Tower (includes inactive rows). */
|
||||
export async function GET() {
|
||||
const auth = await requireOrgAdminSession();
|
||||
if (!auth.ok) return auth.response;
|
||||
|
||||
const orgSettings = await prisma.orgSettings.findUnique({
|
||||
where: { orgId: auth.session.orgId },
|
||||
select: { version: true },
|
||||
});
|
||||
|
||||
const categories = await prisma.reasonCatalogCategory.findMany({
|
||||
where: { orgId: auth.session.orgId },
|
||||
include: {
|
||||
items: { orderBy: [{ sortOrder: "asc" }, { reasonCode: "asc" }] },
|
||||
},
|
||||
orderBy: [{ kind: "asc" }, { sortOrder: "asc" }, { name: "asc" }],
|
||||
});
|
||||
|
||||
return NextResponse.json({
|
||||
ok: true,
|
||||
catalogVersion: orgSettings?.version ?? 1,
|
||||
categories: categories.map((c) => ({
|
||||
id: c.id,
|
||||
kind: c.kind,
|
||||
name: c.name,
|
||||
codePrefix: c.codePrefix,
|
||||
sortOrder: c.sortOrder,
|
||||
active: c.active,
|
||||
items: c.items.map((it) => ({
|
||||
id: it.id,
|
||||
name: it.name,
|
||||
codeSuffix: it.codeSuffix,
|
||||
reasonCode: it.reasonCode,
|
||||
sortOrder: it.sortOrder,
|
||||
active: it.active,
|
||||
})),
|
||||
})),
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user