- 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>
50 lines
1.7 KiB
TypeScript
50 lines
1.7 KiB
TypeScript
/**
|
|
* 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 });
|
|
}
|
|
}
|
|
);
|