This commit is contained in:
Tom Chien (IQ-AS)
2026-09-08 17:46:28 +08:00
commit e25c995560
55 changed files with 6928 additions and 0 deletions
+3
View File
@@ -0,0 +1,3 @@
import { handlers } from "@/auth";
export const { GET, POST } = handlers;
+32
View File
@@ -0,0 +1,32 @@
import { NextRequest, NextResponse } from "next/server";
import { prisma } from "@/lib/prisma";
import { REGIONS, ALL_REGION, isRegion } from "@/lib/regions";
// POST /api/cost-items/delete { name, region, year }
// Deletes the cost item(s) matching name+year. When region = "All", remove every
// existing copy across regions (no warning if fewer than 5 exist). Cascades sub-costs.
export async function POST(req: NextRequest) {
const body = await req.json().catch(() => null);
const name = (body?.name ?? "").trim();
const region = body?.region ?? "";
const year = parseInt(String(body?.year), 10);
if (!name) {
return NextResponse.json({ error: "name is required" }, { status: 400 });
}
if (Number.isNaN(year)) {
return NextResponse.json({ error: "year is required" }, { status: 400 });
}
const targets =
region === ALL_REGION ? [...REGIONS] : isRegion(region) ? [region] : [];
if (targets.length === 0) {
return NextResponse.json({ error: "invalid region" }, { status: 400 });
}
const result = await prisma.costItem.deleteMany({
where: { name, year, region: { in: targets } },
});
return NextResponse.json({ deleted: result.count });
}
+64
View File
@@ -0,0 +1,64 @@
import { NextRequest, NextResponse } from "next/server";
import { prisma } from "@/lib/prisma";
import { REGIONS, ALL_REGION, isRegion } from "@/lib/regions";
// GET /api/cost-items?region&year -> list of cost items (with derived cost).
export async function GET(req: NextRequest) {
const { searchParams } = new URL(req.url);
const region = searchParams.get("region") ?? ALL_REGION;
const yearParam = searchParams.get("year");
const year = yearParam ? parseInt(yearParam, 10) : NaN;
if (Number.isNaN(year)) {
return NextResponse.json({ error: "year is required" }, { status: 400 });
}
const regions = region === ALL_REGION ? REGIONS : isRegion(region) ? [region] : [];
if (regions.length === 0) return NextResponse.json({ items: [] });
const items = await prisma.costItem.findMany({
where: { year, region: { in: [...regions] } },
select: { id: true, name: true, region: true, year: true },
orderBy: [{ name: "asc" }, { region: "asc" }],
});
return NextResponse.json({ items });
}
// POST /api/cost-items { name, region, year } -> create instantly.
// When region = "All", create in all 5 regions, skipping any that already have it.
export async function POST(req: NextRequest) {
const body = await req.json().catch(() => null);
const name = (body?.name ?? "").trim();
const region = body?.region ?? "";
const year = parseInt(String(body?.year), 10);
if (!name) {
return NextResponse.json({ error: "name is required" }, { status: 400 });
}
if (Number.isNaN(year)) {
return NextResponse.json({ error: "year is required" }, { status: 400 });
}
const targets =
region === ALL_REGION ? [...REGIONS] : isRegion(region) ? [region] : [];
if (targets.length === 0) {
return NextResponse.json({ error: "invalid region" }, { status: 400 });
}
let created = 0;
let skipped = 0;
for (const r of targets) {
const existing = await prisma.costItem.findFirst({
where: { name, region: r, year },
});
if (existing) {
skipped++;
continue;
}
await prisma.costItem.create({ data: { name, region: r, year } });
created++;
}
return NextResponse.json({ created, skipped });
}
+45
View File
@@ -0,0 +1,45 @@
import { NextRequest, NextResponse } from "next/server";
import { prisma } from "@/lib/prisma";
import { isRegion } from "@/lib/regions";
// GET /api/cost-registration?region&year -> cost items + subItems + pct for a
// specific region+year (registration requires a specific region).
export async function GET(req: NextRequest) {
const { searchParams } = new URL(req.url);
const region = searchParams.get("region") ?? "";
const yearParam = searchParams.get("year");
const year = yearParam ? parseInt(yearParam, 10) : NaN;
if (!isRegion(region)) {
return NextResponse.json(
{ error: "a specific region is required" },
{ status: 400 }
);
}
if (Number.isNaN(year)) {
return NextResponse.json({ error: "year is required" }, { status: 400 });
}
const items = await prisma.costItem.findMany({
where: { region, year },
include: { subItems: { orderBy: { id: "asc" } } },
orderBy: { name: "asc" },
});
const pctRow = await prisma.trendAiPercentage.findUnique({
where: { region_year: { region, year } },
});
return NextResponse.json({
items: items.map((it) => ({
id: it.id,
name: it.name,
subItems: it.subItems.map((s) => ({
id: s.id,
name: s.name,
subCost: s.subCost,
})),
})),
percentage: pctRow?.percentage ?? 0,
});
}
+18
View File
@@ -0,0 +1,18 @@
import { NextRequest, NextResponse } from "next/server";
import { getDashboardRows } from "@/lib/calc";
// GET /api/dashboard?region&costItem&year -> aggregated table rows.
export async function GET(req: NextRequest) {
const { searchParams } = new URL(req.url);
const region = searchParams.get("region") ?? "All";
const costItem = searchParams.get("costItem") ?? "All";
const yearParam = searchParams.get("year");
const year = yearParam ? parseInt(yearParam, 10) : NaN;
if (Number.isNaN(year)) {
return NextResponse.json({ error: "year is required" }, { status: 400 });
}
const rows = await getDashboardRows({ region, costItem, year });
return NextResponse.json({ rows });
}
+36
View File
@@ -0,0 +1,36 @@
import { NextRequest, NextResponse } from "next/server";
import { prisma } from "@/lib/prisma";
// PATCH /api/sub-items/[id] { name?, subCost? } -> update a sub-item.
export async function PATCH(
req: NextRequest,
{ params }: { params: Promise<{ id: string }> }
) {
const { id: idStr } = await params;
const id = parseInt(idStr, 10);
if (Number.isNaN(id)) {
return NextResponse.json({ error: "invalid id" }, { status: 400 });
}
const body = await req.json().catch(() => null);
const data: { name?: string; subCost?: number } = {};
if (typeof body?.name === "string") data.name = body.name;
if (body?.subCost !== undefined) data.subCost = Number(body.subCost) || 0;
const updated = await prisma.subCostItem.update({ where: { id }, data });
return NextResponse.json({ subItem: updated });
}
// DELETE /api/sub-items/[id] -> remove a sub-item.
export async function DELETE(
_req: NextRequest,
{ params }: { params: Promise<{ id: string }> }
) {
const { id: idStr } = await params;
const id = parseInt(idStr, 10);
if (Number.isNaN(id)) {
return NextResponse.json({ error: "invalid id" }, { status: 400 });
}
await prisma.subCostItem.delete({ where: { id } });
return NextResponse.json({ ok: true });
}
+25
View File
@@ -0,0 +1,25 @@
import { NextRequest, NextResponse } from "next/server";
import { prisma } from "@/lib/prisma";
// POST /api/sub-items { costItemId, name?, subCost? } -> append a sub-item.
export async function POST(req: NextRequest) {
const body = await req.json().catch(() => null);
const costItemId = parseInt(String(body?.costItemId), 10);
const name = (body?.name ?? "New Sub Cost").trim() || "New Sub Cost";
const subCost = Number(body?.subCost ?? 0) || 0;
if (Number.isNaN(costItemId)) {
return NextResponse.json({ error: "costItemId is required" }, { status: 400 });
}
const parent = await prisma.costItem.findUnique({ where: { id: costItemId } });
if (!parent) {
return NextResponse.json({ error: "cost item not found" }, { status: 404 });
}
const sub = await prisma.subCostItem.create({
data: { costItemId, name, subCost },
});
return NextResponse.json({ subItem: sub });
}
+50
View File
@@ -0,0 +1,50 @@
import { NextRequest, NextResponse } from "next/server";
import { prisma } from "@/lib/prisma";
import { isRegion } from "@/lib/regions";
// GET /api/trend-percentage?region&year -> current TrendAI percentage.
export async function GET(req: NextRequest) {
const { searchParams } = new URL(req.url);
const region = searchParams.get("region") ?? "";
const year = parseInt(searchParams.get("year") ?? "", 10);
if (!isRegion(region) || Number.isNaN(year)) {
return NextResponse.json(
{ error: "region and year are required" },
{ status: 400 }
);
}
const row = await prisma.trendAiPercentage.findUnique({
where: { region_year: { region, year } },
});
return NextResponse.json({ percentage: row?.percentage ?? 0 });
}
// PUT /api/trend-percentage { region, year, percentage } -> upsert.
export async function PUT(req: NextRequest) {
const body = await req.json().catch(() => null);
const region = body?.region ?? "";
const year = parseInt(String(body?.year), 10);
const percentage = Number(body?.percentage);
if (!isRegion(region) || Number.isNaN(year)) {
return NextResponse.json(
{ error: "region and year are required" },
{ status: 400 }
);
}
if (Number.isNaN(percentage) || percentage < 0 || percentage > 100) {
return NextResponse.json(
{ error: "percentage must be between 0 and 100" },
{ status: 400 }
);
}
const row = await prisma.trendAiPercentage.upsert({
where: { region_year: { region, year } },
create: { region, year, percentage },
update: { percentage },
});
return NextResponse.json({ percentage: row.percentage });
}
+10
View File
@@ -0,0 +1,10 @@
import { NextRequest, NextResponse } from "next/server";
import { getTrendSeries } from "@/lib/calc";
// GET /api/trend?region -> series data across all years.
export async function GET(req: NextRequest) {
const { searchParams } = new URL(req.url);
const region = searchParams.get("region") ?? "All";
const series = await getTrendSeries({ region });
return NextResponse.json(series);
}
+24
View File
@@ -0,0 +1,24 @@
import { NextRequest, NextResponse } from "next/server";
import { prisma } from "@/lib/prisma";
// POST /api/years/delete { year } -> remove a year and ALL cost data for it
// (cost items + their sub-costs via cascade, plus TrendAI percentages).
export async function POST(req: NextRequest) {
const body = await req.json().catch(() => null);
const year = parseInt(String(body?.year), 10);
if (Number.isNaN(year)) {
return NextResponse.json({ error: "invalid year" }, { status: 400 });
}
// Sub-costs cascade when their cost items are deleted.
const items = await prisma.costItem.deleteMany({ where: { year } });
const pcts = await prisma.trendAiPercentage.deleteMany({ where: { year } });
await prisma.year.deleteMany({ where: { year } });
return NextResponse.json({
year,
deletedItems: items.count,
deletedPercentages: pcts.count,
});
}
+39
View File
@@ -0,0 +1,39 @@
import { NextRequest, NextResponse } from "next/server";
import { prisma } from "@/lib/prisma";
// GET /api/years -> all years (desc) for filter dropdowns + the Year Creation
// table. Union of the managed Year table and any years already used by cost
// items (so pre-existing data always shows even if not in the Year table).
export async function GET() {
const [yearRows, itemRows] = await Promise.all([
prisma.year.findMany({ select: { year: true } }),
prisma.costItem.findMany({
distinct: ["year"],
select: { year: true },
}),
]);
const set = new Set<number>([
...yearRows.map((r) => r.year),
...itemRows.map((r) => r.year),
]);
const years = [...set].sort((a, b) => b - a);
return NextResponse.json({ years });
}
// POST /api/years { year } -> add a year. Reports whether it was created or
// already existed.
export async function POST(req: NextRequest) {
const body = await req.json().catch(() => null);
const year = parseInt(String(body?.year), 10);
if (Number.isNaN(year) || year < 1900 || year > 3000) {
return NextResponse.json({ error: "invalid year" }, { status: 400 });
}
const existing = await prisma.year.findUnique({ where: { year } });
if (existing) {
return NextResponse.json({ created: false, exists: true, year });
}
await prisma.year.create({ data: { year } });
return NextResponse.json({ created: true, exists: false, year });
}