Downtime catalog
This commit is contained in:
@@ -17,7 +17,7 @@ import {
|
||||
validateShiftOverrides,
|
||||
validateThresholds,
|
||||
} from "@/lib/settings";
|
||||
import { loadFallbackReasonCatalog, normalizeReasonCatalog, type ReasonCatalog } from "@/lib/reasonCatalog";
|
||||
import { effectiveReasonCatalogForOrg } from "@/lib/reasonCatalogDb";
|
||||
import { publishSettingsUpdate } from "@/lib/mqtt";
|
||||
import { z } from "zod";
|
||||
|
||||
@@ -46,21 +46,18 @@ function pickAllowedOverrides(raw: unknown) {
|
||||
return out;
|
||||
}
|
||||
|
||||
function withReasonCatalog<T extends Record<string, unknown>>(payload: T, fallbackCatalog: ReasonCatalog) {
|
||||
const base = (isPlainObject(payload) ? { ...payload } : {}) as T;
|
||||
const defaults = isPlainObject(base.defaults) ? base.defaults : {};
|
||||
const parsed =
|
||||
normalizeReasonCatalog(base.reasonCatalog) ??
|
||||
normalizeReasonCatalog(base.reasonCatalogData) ??
|
||||
normalizeReasonCatalog(defaults.reasonCatalog) ??
|
||||
normalizeReasonCatalog(defaults.reasonCatalogData) ??
|
||||
fallbackCatalog;
|
||||
|
||||
async function attachReasonCatalog(
|
||||
orgId: string,
|
||||
defaultsJson: unknown,
|
||||
settingsVersion: number,
|
||||
base: Record<string, unknown>
|
||||
): Promise<Record<string, unknown>> {
|
||||
const catalog = await effectiveReasonCatalogForOrg(orgId, defaultsJson, settingsVersion);
|
||||
return {
|
||||
...base,
|
||||
reasonCatalog: parsed,
|
||||
reasonCatalogData: parsed,
|
||||
reasonCatalogVersion: Number(parsed.version || 1),
|
||||
reasonCatalog: catalog,
|
||||
reasonCatalogData: catalog,
|
||||
reasonCatalogVersion: Number(catalog.version || 1),
|
||||
};
|
||||
}
|
||||
|
||||
@@ -164,9 +161,7 @@ export async function GET(
|
||||
if (!machine) return NextResponse.json({ ok: false, error: "Unauthorized" }, { status: 401 });
|
||||
orgId = machine.orgId;
|
||||
}
|
||||
const fallbackCatalog = await loadFallbackReasonCatalog();
|
||||
|
||||
const { settings, overrides } = await prisma.$transaction(async (tx) => {
|
||||
const { orgRow, shifts, rawOverrides } = await prisma.$transaction(async (tx) => {
|
||||
const orgSettings = await ensureOrgSettings(tx, orgId as string, userId);
|
||||
if (!orgSettings?.settings) throw new Error("SETTINGS_NOT_FOUND");
|
||||
|
||||
@@ -175,25 +170,24 @@ export async function GET(
|
||||
select: { overridesJson: true },
|
||||
});
|
||||
|
||||
const orgPayload = withReasonCatalog(
|
||||
buildSettingsPayload(orgSettings.settings, orgSettings.shifts ?? []),
|
||||
fallbackCatalog
|
||||
);
|
||||
const rawOverrides = pickAllowedOverrides(machineSettings?.overridesJson ?? {});
|
||||
const effective = withReasonCatalog(
|
||||
deepMerge(orgPayload, rawOverrides) as Record<string, unknown>,
|
||||
fallbackCatalog
|
||||
);
|
||||
|
||||
return { settings: { org: orgPayload, effective }, overrides: rawOverrides };
|
||||
return {
|
||||
orgRow: orgSettings.settings,
|
||||
shifts: orgSettings.shifts ?? [],
|
||||
rawOverrides,
|
||||
};
|
||||
});
|
||||
|
||||
const baseOrg = buildSettingsPayload(orgRow, shifts) as Record<string, unknown>;
|
||||
const orgPayload = await attachReasonCatalog(orgId as string, orgRow.defaultsJson, orgRow.version, baseOrg);
|
||||
const effective = deepMerge(orgPayload, rawOverrides) as Record<string, unknown>;
|
||||
|
||||
return NextResponse.json({
|
||||
ok: true,
|
||||
machineId,
|
||||
orgSettings: settings.org,
|
||||
effectiveSettings: settings.effective,
|
||||
overrides,
|
||||
orgSettings: orgPayload,
|
||||
effectiveSettings: effective,
|
||||
overrides: rawOverrides,
|
||||
});
|
||||
}
|
||||
|
||||
@@ -413,25 +407,23 @@ export async function PUT(
|
||||
},
|
||||
});
|
||||
|
||||
const fallbackCatalog = await loadFallbackReasonCatalog();
|
||||
const orgPayload = withReasonCatalog(
|
||||
buildSettingsPayload(orgSettings.settings, orgSettings.shifts ?? []),
|
||||
fallbackCatalog
|
||||
);
|
||||
const overrides = pickAllowedOverrides(saved.overridesJson ?? {});
|
||||
const effective = withReasonCatalog(
|
||||
deepMerge(orgPayload, overrides) as Record<string, unknown>,
|
||||
fallbackCatalog
|
||||
);
|
||||
|
||||
return {
|
||||
orgPayload,
|
||||
overrides,
|
||||
effective,
|
||||
orgSettingsRow: orgSettings.settings,
|
||||
shifts: orgSettings.shifts ?? [],
|
||||
overrides: pickAllowedOverrides(saved.overridesJson ?? {}),
|
||||
overridesUpdatedAt: saved.updatedAt,
|
||||
};
|
||||
});
|
||||
|
||||
const baseOrg = buildSettingsPayload(result.orgSettingsRow, result.shifts) as Record<string, unknown>;
|
||||
const orgPayload = await attachReasonCatalog(
|
||||
session.orgId,
|
||||
result.orgSettingsRow.defaultsJson,
|
||||
result.orgSettingsRow.version,
|
||||
baseOrg
|
||||
);
|
||||
const effective = deepMerge(orgPayload, result.overrides) as Record<string, unknown>;
|
||||
|
||||
const overridesUpdatedAt =
|
||||
result.overridesUpdatedAt && result.overridesUpdatedAt instanceof Date
|
||||
? result.overridesUpdatedAt.toISOString()
|
||||
@@ -440,7 +432,7 @@ export async function PUT(
|
||||
await publishSettingsUpdate({
|
||||
orgId: session.orgId,
|
||||
machineId,
|
||||
version: Number(result.orgPayload.version ?? 0),
|
||||
version: Number(result.orgSettingsRow.version ?? 0),
|
||||
source,
|
||||
overridesUpdatedAt,
|
||||
});
|
||||
@@ -451,8 +443,8 @@ export async function PUT(
|
||||
return NextResponse.json({
|
||||
ok: true,
|
||||
machineId,
|
||||
orgSettings: result.orgPayload,
|
||||
effectiveSettings: result.effective,
|
||||
orgSettings: orgPayload,
|
||||
effectiveSettings: effective,
|
||||
overrides: result.overrides,
|
||||
});
|
||||
}
|
||||
|
||||
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,
|
||||
})),
|
||||
})),
|
||||
});
|
||||
}
|
||||
@@ -19,7 +19,7 @@ import {
|
||||
validateShiftOverrides,
|
||||
validateThresholds,
|
||||
} from "@/lib/settings";
|
||||
import { loadFallbackReasonCatalog, normalizeReasonCatalog, type ReasonCatalog } from "@/lib/reasonCatalog";
|
||||
import { effectiveReasonCatalogForOrg } from "@/lib/reasonCatalogDb";
|
||||
import { publishSettingsUpdate } from "@/lib/mqtt";
|
||||
import { z } from "zod";
|
||||
|
||||
@@ -39,21 +39,18 @@ function canManageSettings(role?: string | null) {
|
||||
return role === "OWNER" || role === "ADMIN";
|
||||
}
|
||||
|
||||
function withReasonCatalog<T extends Record<string, unknown>>(payload: T, fallbackCatalog: ReasonCatalog) {
|
||||
const base = (isPlainObject(payload) ? { ...payload } : {}) as T;
|
||||
const defaults = isPlainObject(base.defaults) ? base.defaults : {};
|
||||
const parsed =
|
||||
normalizeReasonCatalog(base.reasonCatalog) ??
|
||||
normalizeReasonCatalog(base.reasonCatalogData) ??
|
||||
normalizeReasonCatalog(defaults.reasonCatalog) ??
|
||||
normalizeReasonCatalog(defaults.reasonCatalogData) ??
|
||||
fallbackCatalog;
|
||||
|
||||
async function attachReasonCatalog(
|
||||
orgId: string,
|
||||
defaultsJson: unknown,
|
||||
settingsVersion: number,
|
||||
base: Record<string, unknown>
|
||||
): Promise<Record<string, unknown>> {
|
||||
const catalog = await effectiveReasonCatalogForOrg(orgId, defaultsJson, settingsVersion);
|
||||
return {
|
||||
...base,
|
||||
reasonCatalog: parsed,
|
||||
reasonCatalogData: parsed,
|
||||
reasonCatalogVersion: Number(parsed.version || 1),
|
||||
reasonCatalog: catalog,
|
||||
reasonCatalogData: catalog,
|
||||
reasonCatalogVersion: Number(catalog.version || 1),
|
||||
};
|
||||
}
|
||||
|
||||
@@ -66,7 +63,6 @@ const settingsPayloadSchema = z
|
||||
thresholds: z.any().optional(),
|
||||
alerts: z.any().optional(),
|
||||
defaults: z.any().optional(),
|
||||
reasonCatalog: z.any().optional(),
|
||||
version: z.union([z.number(), z.string()]).optional(),
|
||||
})
|
||||
.passthrough();
|
||||
@@ -145,8 +141,13 @@ async function loadSettingsPayload(orgId: string, userId: string) {
|
||||
return found;
|
||||
});
|
||||
|
||||
const fallbackCatalog = await loadFallbackReasonCatalog();
|
||||
const payload = withReasonCatalog(buildSettingsPayload(loaded.settings, loaded.shifts ?? []), fallbackCatalog);
|
||||
const base = buildSettingsPayload(loaded.settings, loaded.shifts ?? []) as Record<string, unknown>;
|
||||
const payload = await attachReasonCatalog(
|
||||
orgId,
|
||||
loaded.settings.defaultsJson,
|
||||
loaded.settings.version,
|
||||
base
|
||||
);
|
||||
const defaultsRaw = isPlainObject(loaded.settings.defaultsJson) ? (loaded.settings.defaultsJson as any) : {};
|
||||
const modulesRaw = isPlainObject(defaultsRaw.modules) ? defaultsRaw.modules : {};
|
||||
const modules = { screenlessMode: modulesRaw.screenlessMode === true };
|
||||
@@ -221,7 +222,6 @@ export async function PUT(req: Request) {
|
||||
const thresholds = parsed.data.thresholds;
|
||||
const alerts = parsed.data.alerts;
|
||||
const defaults = parsed.data.defaults;
|
||||
const reasonCatalogRaw = parsed.data.reasonCatalog;
|
||||
const expectedVersion = parsed.data.version;
|
||||
const modules = parsed.data.modules;
|
||||
|
||||
@@ -233,7 +233,6 @@ export async function PUT(req: Request) {
|
||||
thresholds === undefined &&
|
||||
alerts === undefined &&
|
||||
defaults === undefined &&
|
||||
reasonCatalogRaw === undefined &&
|
||||
modules === undefined
|
||||
|
||||
) {
|
||||
@@ -252,13 +251,6 @@ export async function PUT(req: Request) {
|
||||
if (defaults !== undefined && !isPlainObject(defaults)) {
|
||||
return NextResponse.json({ ok: false, error: "defaults must be an object" }, { status: 400 });
|
||||
}
|
||||
const nextReasonCatalog =
|
||||
reasonCatalogRaw === undefined || reasonCatalogRaw === null
|
||||
? reasonCatalogRaw
|
||||
: normalizeReasonCatalog(reasonCatalogRaw);
|
||||
if (reasonCatalogRaw !== undefined && reasonCatalogRaw !== null && !nextReasonCatalog) {
|
||||
return NextResponse.json({ ok: false, error: "reasonCatalog must be a valid catalog payload" }, { status: 400 });
|
||||
}
|
||||
if (modules !== undefined && !isPlainObject(modules)) {
|
||||
return NextResponse.json({ ok: false, error: "Invalid modules payload" }, { status: 400 });
|
||||
}
|
||||
@@ -333,20 +325,16 @@ export async function PUT(req: Request) {
|
||||
: { ...currentModulesRaw, screenlessMode };
|
||||
|
||||
// Write defaultsJson if either defaults changed OR modules changed
|
||||
const shouldWriteDefaultsJson =
|
||||
!!nextDefaultsCore || screenlessMode !== undefined || reasonCatalogRaw !== undefined;
|
||||
const shouldWriteDefaultsJson = !!nextDefaultsCore || screenlessMode !== undefined;
|
||||
|
||||
const nextDefaultsJson = shouldWriteDefaultsJson
|
||||
? { ...(nextDefaultsCore ?? normalizeDefaults(currentDefaultsRaw)), modules: nextModules }
|
||||
: undefined;
|
||||
|
||||
if (nextDefaultsJson && reasonCatalogRaw !== undefined) {
|
||||
if (nextDefaultsJson) {
|
||||
const defaultsTarget = nextDefaultsJson as Record<string, unknown>;
|
||||
if (nextReasonCatalog === null) {
|
||||
delete defaultsTarget.reasonCatalog;
|
||||
} else if (nextReasonCatalog) {
|
||||
defaultsTarget.reasonCatalog = nextReasonCatalog;
|
||||
}
|
||||
delete defaultsTarget.reasonCatalog;
|
||||
delete defaultsTarget.reasonCatalogData;
|
||||
}
|
||||
|
||||
|
||||
@@ -444,12 +432,18 @@ export async function PUT(req: Request) {
|
||||
return NextResponse.json({ ok: false, error: updated.error }, { status: 400 });
|
||||
}
|
||||
|
||||
const payload = buildSettingsPayload(updated.settings, updated.shifts ?? []);
|
||||
const baseOut = buildSettingsPayload(updated.settings, updated.shifts ?? []) as Record<string, unknown>;
|
||||
const payload = await attachReasonCatalog(
|
||||
session.orgId,
|
||||
updated.settings.defaultsJson,
|
||||
updated.settings.version,
|
||||
baseOut
|
||||
);
|
||||
const updatedAt =
|
||||
typeof payload.updatedAt === "string"
|
||||
? payload.updatedAt
|
||||
: payload.updatedAt
|
||||
? payload.updatedAt.toISOString()
|
||||
? (payload.updatedAt as Date).toISOString()
|
||||
: undefined;
|
||||
try {
|
||||
await publishSettingsUpdate({
|
||||
|
||||
Reference in New Issue
Block a user