Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
18 changes: 10 additions & 8 deletions src/controllers/hgnFormResponseController.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,18 +85,20 @@
? allSkills.reduce((a, b) => a + b.score, 0) / allSkills.length
: 0;

// Decide which section to use for topSkills
let sectionToUse = null;
// When a skills filter is active, the displayed "Top Skills" must include
// the skills that actually matched the filter (that's why this user is in
// the results), ranked first; remaining slots fill with the user's other
// highest-scoring skills so the list still shows up to 4.
let matchedSkills = [];
if (skills) {
const skillList = skills.split(',').map((s) => s.trim().toLowerCase());
const match = allSkills.find((s) => skillList.includes(s.skill.toLowerCase()));
if (match) sectionToUse = match.section;
matchedSkills = allSkills.filter((s) => skillList.includes(s.skill.toLowerCase()));
}
const remainingSkills = allSkills
.filter((s) => !matchedSkills.includes(s))
.sort((a, b) => b.score - a.score);

// Pick top 4 from chosen section, or global top 4
const topSkills = allSkills
.filter((s) => (sectionToUse ? s.section === sectionToUse : true))
.sort((a, b) => b.score - a.score)
const topSkills = [...matchedSkills.sort((a, b) => b.score - a.score), ...remainingSkills]

Check warning on line 101 in src/controllers/hgnFormResponseController.js

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Move this array "sort" operation to a separate statement or replace it with "toSorted".

See more on https://sonarcloud.io/project/issues?id=OneCommunityGlobal_HGNRest&issues=AaDeHEhHs4mYgcx9JqGY&open=AaDeHEhHs4mYgcx9JqGY&pullRequest=2370
.slice(0, 4)
.map((s) => s.skill);

Expand Down
32 changes: 32 additions & 0 deletions src/controllers/hgnFormResponseController.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,38 @@ describe('HgnFormResponseController', () => {
expect(result[1]._id).toBe('1');
});

it('topSkills should include every selected skill the matched user has, even if it is not their highest-scoring skill', async () => {
// EnvironmentSetup is the user's lowest-scoring backend skill, so the old
// "top 4 of the matched section" logic dropped it from topSkills even
// though it was one of the filters that matched this user.
mockReq.query = { skills: 'EnvironmentSetup,AdvancedCoding,AgileDevelopment' };
UserProfile.find = jest.fn().mockResolvedValue([{ _id: '123', isActive: true }]);

FormResponse.find = jest.fn().mockResolvedValue([
{
_id: '1',
user_id: '123',
userInfo: { name: 'John' },
frontend: {},
backend: {
EnvironmentSetup: '3',
AdvancedCoding: '9',
AgileDevelopment: '8',
MongoDB: '9',
Database: '9',
},
general: {},
},
]);

await controller.getRankedResponses(mockReq, mockRes);

const result = mockRes.json.mock.calls[0][0];
expect(result[0].topSkills).toEqual(
expect.arrayContaining(['EnvironmentSetup', 'AdvancedCoding', 'AgileDevelopment']),
);
});

it('should return all users if no query params are provided', async () => {
UserProfile.find = jest.fn().mockResolvedValue([
{ _id: '123', isActive: true },
Expand Down
Loading