Skip to content
Merged
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
24 changes: 24 additions & 0 deletions src/reports/report-directory.data.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@ describe("getAccessibleReportsDirectory", () => {
expect(directory.identity).toBeUndefined();
expect(directory.sfdc).toBeUndefined();
expect(directory.topcoder).toBeUndefined();
expect(
directory.statistics?.reports.map((report) => report.path),
).not.toContain("/statistics/expert-skills/categories");
expect(
directory.statistics?.reports.map((report) => report.path),
).not.toContain("/statistics/expert-skills/category-members");
});

it("returns challenge, member, and role-mapped identity reports for talent managers", () => {
Expand All @@ -74,6 +80,12 @@ describe("getAccessibleReportsDirectory", () => {
"/member/recent-member-data",
"/member/search",
]);
expect(directory.statistics?.reports.map((report) => report.path)).toEqual(
expect.arrayContaining([
"/statistics/expert-skills/categories",
"/statistics/expert-skills/category-members",
]),
);
});

it("returns bulk member lookup for topcoder project managers", () => {
Expand All @@ -98,6 +110,12 @@ describe("getAccessibleReportsDirectory", () => {
expect(directory.identity).toBeUndefined();
expect(directory.sfdc).toBeUndefined();
expect(directory.statistics?.reports.length).toBeGreaterThan(0);
expect(
directory.statistics?.reports.map((report) => report.path),
).not.toContain("/statistics/expert-skills/categories");
expect(
directory.statistics?.reports.map((report) => report.path),
).not.toContain("/statistics/expert-skills/category-members");
expect(directory.topcoder).toBeUndefined();
});

Expand All @@ -119,6 +137,12 @@ describe("getAccessibleReportsDirectory", () => {
"/member/recent-member-data",
"/member/search",
]);
expect(
directory.statistics?.reports.map((report) => report.path),
).not.toContain("/statistics/expert-skills/categories");
expect(
directory.statistics?.reports.map((report) => report.path),
).not.toContain("/statistics/expert-skills/category-members");
});

it("returns an empty directory when no JWT user is present", () => {
Expand Down
33 changes: 28 additions & 5 deletions src/reports/report-directory.data.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Scopes as AppScopes } from "../app-constants";
import { Scopes as AppScopes, UserRoles } from "../app-constants";
import {
AuthUserLike,
getNormalizedRoles,
Expand Down Expand Up @@ -58,6 +58,7 @@ export type ReportsDirectory = Partial<Record<ReportGroupKey, ReportGroup>>;
type RegisteredReport = AvailableReport & {
requiredScopes: readonly string[];
adminOnly?: boolean;
allowedRoles?: readonly string[];
};

type RegisteredReportGroup = Omit<ReportGroup, "reports"> & {
Expand Down Expand Up @@ -169,6 +170,16 @@ const publicReport = (
parameters: ReportParameter[] = [],
): RegisteredReport => report(name, path, description, [], parameters);

const talentManagerReport = (
name: string,
path: string,
description: string,
parameters: ReportParameter[] = [],
): RegisteredReport => ({
...publicReport(name, path, description, parameters),
allowedRoles: [UserRoles.TalentManager],
});

const challengeStatusParam: ReportParameter = {
name: "challengeStatus",
type: "enum[]",
Expand Down Expand Up @@ -684,15 +695,15 @@ const REGISTERED_REPORTS_DIRECTORY: RegisteredReportsDirectory = {
"/statistics/qa/wins",
"Quality Assurance challenge wins by member (desc)",
),
publicReport(
talentManagerReport(
"Expert Skill Categories",
"/statistics/expert-skills/categories",
"Skill categories from skills.skill_category with win-normalized bubble sizes",
"Skill categories from skills.skill_category with win-normalized bubble sizes. Accessible by Administrator and Talent Manager roles only.",
),
publicReport(
talentManagerReport(
"Expert Skill Category Members",
"/statistics/expert-skills/category-members",
"Top 100 members in a skill category, sorted by wins",
"Top 100 members in a skill category, sorted by wins. Accessible by Administrator and Talent Manager roles only.",
[
{
name: "selectedcategory",
Expand Down Expand Up @@ -980,6 +991,18 @@ export function getAccessibleReportsDirectory(
return false;
}

if (reportDefinition.allowedRoles?.length) {
if (isAdmin) {
return true;
}

const allowedRoles = new Set(
reportDefinition.allowedRoles.map((role) => role.toLowerCase()),
);

return normalizedRoles.some((role) => allowedRoles.has(role));
}

return hasAccessToScopes(authUser, reportDefinition.requiredScopes);
});

Expand Down
32 changes: 30 additions & 2 deletions src/statistics/expert-skills-statistics.data.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,36 @@ describe("expert-skills-statistics.data", () => {
);
});

it("assigns a stable color and icon from the category id", () => {
const first = getCategoryAppearance("481b5ebc-2fe6-45ed-a90c-736936d458d7");
it("maps known category names to Figma icons regardless of id", () => {
expect(
getCategoryAppearance(
"481b5ebc-2fe6-45ed-a90c-736936d458d7",
"Programming & Development",
),
).toEqual({
color: "#1B4F72",
icon: "TerminalIcon",
});
expect(getCategoryAppearance("unused-id", "Web Development")).toEqual({
color: "#7EB8C4",
icon: "GlobeAltIcon",
});
expect(
getCategoryAppearance(
"unused-id",
"Software Development Lifecycle (SDLC)",
).icon,
).toBe("RefreshIcon");
expect(
getCategoryAppearance("unused-id", "UX Design and Multimedia").icon,
).toBe("PencilAltIcon");
});

it("hashes unknown categories by id so new catalog entries still render", () => {
const first = getCategoryAppearance(
"481b5ebc-2fe6-45ed-a90c-736936d458d7",
"Test Cat QA 1",
);
const second = getCategoryAppearance(
"481b5ebc-2fe6-45ed-a90c-736936d458d7",
);
Expand Down
90 changes: 79 additions & 11 deletions src/statistics/expert-skills-statistics.data.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* Visual tokens for Skill Statistics bubbles.
* Category names/ids come from standardized-skills; these palettes are
* assigned by hashing the category id so new catalog entries still render.
* Category names/ids come from standardized-skills. Known catalog names
* map to Figma icons/colors; unknown entries still hash by id.
*/
const CATEGORY_COLORS = [
"#1B4F72",
Expand Down Expand Up @@ -59,15 +59,6 @@ export type CategoryAppearance = {
icon: string;
};

export function getCategoryAppearance(categoryId: string): CategoryAppearance {
const hash = hashString(categoryId);

return {
color: CATEGORY_COLORS[hash % CATEGORY_COLORS.length],
icon: CATEGORY_ICONS[hash % CATEGORY_ICONS.length],
};
}

export function normalizeCategoryName(value: string): string {
return value
.toLowerCase()
Expand All @@ -76,6 +67,83 @@ export function normalizeCategoryName(value: string): string {
.trim();
}

const NAMED_CATEGORY_APPEARANCE: Array<[string[], CategoryAppearance]> = [
[["Programming and Development"], { color: "#1B4F72", icon: "TerminalIcon" }],
[["Web Development"], { color: "#7EB8C4", icon: "GlobeAltIcon" }],
[
["Networking and Telecommunications"],
{ color: "#5EB3C4", icon: "RssIcon" },
],
[["Cybersecurity"], { color: "#5B9BD5", icon: "ShieldCheckIcon" }],
[
["Software Development Lifecycle (SDLC)", "Software Development Lifecycle"],
{ color: "#1A3D3D", icon: "RefreshIcon" },
],
[["Cloud Computing"], { color: "#2C4A6E", icon: "CloudIcon" }],
[["Operating Systems"], { color: "#3D7EA6", icon: "ChipIcon" }],
[["DevOps and Automation"], { color: "#2D4A3E", icon: "RefreshIcon" }],
[["Data Analysis and Big Data"], { color: "#2C5F8A", icon: "ChartBarIcon" }],
[["Virtualization"], { color: "#3D7EA6", icon: "DuplicateIcon" }],
[
["Databases and Data Warehousing"],
{ color: "#1B4F72", icon: "ServerIcon" },
],
[
["Mathematics and Statistics"],
{ color: "#6B7C4A", icon: "CalculatorIcon" },
],
[["Database Management"], { color: "#7EB8C4", icon: "DatabaseIcon" }],
[
["Geospatial Information Systems (GIS)", "Geospatial Information Systems"],
{ color: "#3D6A8A", icon: "MapIcon" },
],
[["Machine Learning and AI"], { color: "#5EB8B0", icon: "SparklesIcon" }],
[
["User Experience Design and Multimedia", "UX Design and Multimedia"],
{ color: "#3D5C5C", icon: "PencilAltIcon" },
],
[
["Hardware and Systems Administration"],
{ color: "#4EC4C4", icon: "DesktopComputerIcon" },
],
[["Mobile App Development"], { color: "#5A8A8A", icon: "DeviceMobileIcon" }],
[
["Software Testing and Quality Assurance", "Software Testing and QA"],
{ color: "#2C5F8A", icon: "ClipboardCheckIcon" },
],
[["Blockchain"], { color: "#2C4A6E", icon: "ShareIcon" }],
[["IoT (Internet of Things)"], { color: "#3D8B8F", icon: "WifiIcon" }],
[["Project Management"], { color: "#3D7EA6", icon: "ClipboardListIcon" }],
[["Scripting and Automation"], { color: "#5B9BD5", icon: "CodeIcon" }],
];

const CATEGORY_APPEARANCE_BY_NAME: Record<string, CategoryAppearance> =
Object.fromEntries(
NAMED_CATEGORY_APPEARANCE.flatMap(([names, appearance]) =>
names.map((name) => [normalizeCategoryName(name), appearance]),
),
);

export function getCategoryAppearance(
categoryId: string,
categoryName?: string,
): CategoryAppearance {
const mapped = categoryName
? CATEGORY_APPEARANCE_BY_NAME[normalizeCategoryName(categoryName)]
: undefined;

if (mapped) {
return mapped;
}

const hash = hashString(categoryId);

return {
color: CATEGORY_COLORS[hash % CATEGORY_COLORS.length],
icon: CATEGORY_ICONS[hash % CATEGORY_ICONS.length],
};
}

export const MIN_CATEGORY_SIZE = 3;
export const MAX_CATEGORY_SIZE = 10;

Expand Down
4 changes: 3 additions & 1 deletion src/statistics/expert-skills-statistics.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,10 @@ describe("ExpertSkillsStatisticsService", () => {
totalSkills: 50,
}),
);
expect(result[0].color).toMatch(/^#[0-9A-F]{6}$/i);
expect(result[0].color).toBe("#1B4F72");
expect(result[0].icon).toBe("TerminalIcon");
expect(result[1].name).toBe("Scripting and Automation");
expect(result[1].icon).toBe("CodeIcon");
expect(result[1].size).toBe(3);
});

Expand Down
2 changes: 1 addition & 1 deletion src/statistics/expert-skills-statistics.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ export class ExpertSkillsStatisticsService {

return categories.map((category, index) => {
const stats = statsById.get(category.id);
const appearance = getCategoryAppearance(category.id);
const appearance = getCategoryAppearance(category.id, category.name);

return {
id: category.id,
Expand Down
85 changes: 85 additions & 0 deletions src/statistics/guards/expert-skills.guard.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import {
ExecutionContext,
ForbiddenException,
UnauthorizedException,
} from "@nestjs/common";
import { UserRoles } from "src/app-constants";
import { ExpertSkillsGuard } from "./expert-skills.guard";

type AuthUserFixture = {
isMachine?: boolean;
roles?: string[];
role?: string | string[];
scopes?: string[];
};

function createExecutionContext(authUser?: AuthUserFixture): ExecutionContext {
return {
switchToHttp: () => ({
getRequest: () => ({
authUser,
}),
}),
} as unknown as ExecutionContext;
}

describe("ExpertSkillsGuard", () => {
const guard = new ExpertSkillsGuard();

it("throws when no auth user is present", () => {
expect(() => guard.canActivate(createExecutionContext())).toThrow(
UnauthorizedException,
);
});

it("allows talent manager role access", () => {
expect(
guard.canActivate(
createExecutionContext({
roles: [UserRoles.TalentManager],
}),
),
).toBe(true);
});

it("allows admin role access", () => {
expect(
guard.canActivate(
createExecutionContext({
roles: ["Administrator"],
}),
),
).toBe(true);
});

it("allows role claim with topcoder talent manager prefix", () => {
expect(
guard.canActivate(
createExecutionContext({
role: "Topcoder Talent Manager",
}),
),
).toBe(true);
});

it("denies machine clients even with report scopes", () => {
expect(() =>
guard.canActivate(
createExecutionContext({
isMachine: true,
scopes: ["reports:all"],
}),
),
).toThrow(ForbiddenException);
});

it("denies non-admin users without required role", () => {
expect(() =>
guard.canActivate(
createExecutionContext({
roles: [UserRoles.ProjectManager],
}),
),
).toThrow(ForbiddenException);
});
});
Loading
Loading