Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion app/[locale]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { redirect } from "next/navigation";
import { connection } from "next/server";
import { Suspense } from "react";
import { AppSidebar } from "@/components/app-sidebar";
import { Dashboard } from "@/components/dashboard";
import { getAuthOptions, isAuthConfigured } from "@/lib/auth";
import { getAuthConfiguration } from "@/lib/auth-configuration";

Expand Down Expand Up @@ -41,6 +42,8 @@ async function AuthenticatedHome({ locale }: { locale: string }) {
authConfigured={authConfigured}
authMode={mode}
providerName={providerName}
/>
>
<Dashboard />
</AppSidebar>
);
}
109 changes: 109 additions & 0 deletions app/api/dashboard/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
import { getToken } from "next-auth/jwt";
import { NextResponse, type NextRequest } from "next/server";
import { ApiError, fetchJson } from "@/lib/api";

type BackendDashboard = {
licenses_count?: unknown;
obligations_count?: unknown;
users_count?: unknown;
monthly_license_changes_count?: unknown;
risk_license_frequency?: unknown;
};

type BackendAudit = {
timestamp?: unknown;
type?: unknown;
user?: { display_name?: unknown; user_name?: unknown };
};

function asCount(value: unknown) {
return typeof value === "number" && Number.isFinite(value) && value >= 0
? value
: 0;
}

function asRiskFrequency(value: unknown) {
if (!Array.isArray(value)) return [];

return value.flatMap((entry) => {
if (!entry || typeof entry !== "object") return [];
const { risk, count } = entry as Record<string, unknown>;
if (typeof risk !== "number" || !Number.isFinite(risk)) return [];
return [{ risk, count: asCount(count) }];
});
}

function asRecentChanges(value: unknown) {
if (!Array.isArray(value)) return [];

return value.map((entry) => {
const audit = entry as BackendAudit;
const user = audit.user;
return {
actor:
typeof user?.display_name === "string" && user.display_name.trim()
? user.display_name
: typeof user?.user_name === "string" && user.user_name.trim()
? user.user_name
: null,
type: typeof audit.type === "string" ? audit.type : null,
timestamp: typeof audit.timestamp === "string" ? audit.timestamp : null,
};
});
}

export async function GET(request: NextRequest) {
const token = await getToken({
req: request,
secret: process.env.NEXTAUTH_SECRET,
});
const accessToken = token?.backendTokens?.accessToken;
const baseUrl = process.env.LICENSEDB_API_URL;

if (!accessToken) {
return NextResponse.json({ error: "Unauthenticated" }, { status: 401 });
}
if (!baseUrl) {
return NextResponse.json({ error: "Backend unavailable" }, { status: 503 });
}

const headers = { Authorization: `Bearer ${accessToken}` };
const apiUrl = baseUrl.replace(/\/+$/, "");

try {
const [dashboard, audits] = await Promise.all([
fetchJson<{ data?: BackendDashboard }>(`${apiUrl}/dashboard`, {
headers,
cache: "no-store",
}),
fetchJson<{ data?: BackendAudit[] }>(`${apiUrl}/audits?limit=7&page=1`, {
headers,
cache: "no-store",
}),
]);

return NextResponse.json(
{
data: {
licensesCount: asCount(dashboard.data?.licenses_count),
obligationsCount: asCount(dashboard.data?.obligations_count),
usersCount: asCount(dashboard.data?.users_count),
monthlyLicenseChangesCount: asCount(
dashboard.data?.monthly_license_changes_count,
),
riskLicenseFrequency: asRiskFrequency(
dashboard.data?.risk_license_frequency,
),
recentChanges: asRecentChanges(audits.data),
},
},
{ headers: { "Cache-Control": "private, no-store" } },
);
} catch (error) {
const status = error instanceof ApiError ? error.status : 502;
return NextResponse.json(
{ error: "Unable to load dashboard data" },
{ status },
);
}
}
4 changes: 4 additions & 0 deletions components/app-sidebar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import Link from "next/link";
import { useSession } from "next-auth/react";
import { useTranslations } from "next-intl";
import type { ReactNode } from "react";
import {
ChevronDown,
ChevronRight,
Expand Down Expand Up @@ -64,10 +65,12 @@ export function AppSidebar({
authConfigured,
authMode,
providerName,
children,
}: {
authConfigured: boolean;
authMode: AuthMode | null;
providerName: string;
children: ReactNode;
}) {
const t = useTranslations("Sidebar");
const { data: session } = useSession();
Expand Down Expand Up @@ -215,6 +218,7 @@ export function AppSidebar({
title={t("openSidebar")}
className="fixed top-4 left-4 z-20 md:hidden"
/>
{children}
</main>
</SidebarProvider>
</TooltipProvider>
Expand Down
Loading