fix(application): normalize dashboard top value score breakdown

This commit is contained in:
2026-03-31 22:35:02 +02:00
parent 78d19c59b6
commit f2bcf4b7f0
2 changed files with 221 additions and 5 deletions
@@ -1,14 +1,49 @@
import type { PrismaClient } from "@capakraken/db";
import type { ValueScoreBreakdown } from "@capakraken/shared";
export interface GetDashboardTopValueResourcesInput {
limit: number;
userRole: string;
}
export interface DashboardTopValueResourceRow {
id: string;
eid: string;
displayName: string;
chapter: string | null;
valueScore: number | null;
valueScoreBreakdown: ValueScoreBreakdown | null;
valueScoreUpdatedAt: Date | null;
lcrCents: number;
countryCode: string | null;
countryName: string | null;
federalState: string | null;
metroCityName: string | null;
}
function isValueScoreBreakdown(value: unknown): value is ValueScoreBreakdown {
if (typeof value !== "object" || value === null) {
return false;
}
return [
"skillDepth",
"skillBreadth",
"costEfficiency",
"chargeability",
"experience",
"total",
].every((key) => typeof (value as Record<string, unknown>)[key] === "number");
}
function normalizeValueScoreBreakdown(value: unknown): ValueScoreBreakdown | null {
return isValueScoreBreakdown(value) ? value : null;
}
export async function getDashboardTopValueResources(
db: PrismaClient,
input: GetDashboardTopValueResourcesInput,
) {
): Promise<DashboardTopValueResourceRow[]> {
const settings = await db.systemSettings.findUnique({
where: { id: "singleton" },
});
@@ -28,9 +63,36 @@ export async function getDashboardTopValueResources(
displayName: true,
chapter: true,
valueScore: true,
valueScoreBreakdown: true,
valueScoreUpdatedAt: true,
lcrCents: true,
country: {
select: {
code: true,
name: true,
},
},
federalState: true,
metroCity: {
select: {
name: true,
},
},
},
orderBy: { valueScore: "desc" },
take: input.limit,
});
}).then((resources) => resources.map((resource) => ({
id: resource.id,
eid: resource.eid,
displayName: resource.displayName,
chapter: resource.chapter,
valueScore: resource.valueScore,
valueScoreBreakdown: normalizeValueScoreBreakdown(resource.valueScoreBreakdown),
valueScoreUpdatedAt: resource.valueScoreUpdatedAt,
lcrCents: resource.lcrCents,
countryCode: resource.country?.code ?? null,
countryName: resource.country?.name ?? null,
federalState: resource.federalState ?? null,
metroCityName: resource.metroCity?.name ?? null,
})));
}