Downtime catalog
This commit is contained in:
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 });
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user