feat(05-01): add Dashboard API route and integration tests

- GET /api/dashboard with read Report permission (ADMIN, OFFICE_STAFF)
- Optional startDate/endDate query params for cash flow date range
- 6 integration tests verifying all metric methods against seeded data
- Tests cover revenue, overdue, subscriber status, cash flow, collectors
- Comprehensive cleanup order for dashboard test data

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
kevin-asprec
2026-03-05 17:23:16 +08:00
parent 539564dbd4
commit 5b1b4ee0e1
2 changed files with 494 additions and 0 deletions

View File

@@ -0,0 +1,49 @@
/**
* GET /api/dashboard
*
* Returns all dashboard metrics for the ISP owner's business health overview.
* Aggregates revenue, overdue, subscriber status, cash flow, and collector metrics.
*
* Query params:
* startDate? - ISO date string for cash flow start (default: start of current month)
* endDate? - ISO date string for cash flow end (default: end of current month)
*
* Access: ADMIN and OFFICE_STAFF (read Report permission).
*/
import { NextRequest, NextResponse } from "next/server";
import { withPermission } from "@/lib/middleware/authorize";
import { withTenantContext } from "@/lib/prisma-tenant";
import { DashboardService } from "@/lib/services/dashboard-service";
export const GET = withPermission("read", "Report")(
async (req: NextRequest, { user }) => {
if (!user.tenantId) {
return NextResponse.json(
{ error: "No tenant context -- super-admins must use the admin API" },
{ status: 400 }
);
}
const { searchParams } = new URL(req.url);
const startDateParam = searchParams.get("startDate");
const endDateParam = searchParams.get("endDate");
const tenantPrisma = withTenantContext(user.tenantId);
try {
const summary = await DashboardService.getDashboardSummary(
tenantPrisma,
user.tenantId,
{
startDate: startDateParam ? new Date(startDateParam) : undefined,
endDate: endDateParam ? new Date(endDateParam) : undefined,
}
);
return NextResponse.json(summary);
} catch (err) {
const message =
err instanceof Error ? err.message : "Failed to load dashboard";
return NextResponse.json({ error: message }, { status: 500 });
}
}
);