refactor: remove certIds and the associated map (#64299)

This commit is contained in:
Oliver Eyton-Williams
2025-12-04 14:01:27 +01:00
committed by GitHub
parent ff66ae89df
commit a38caeca39
8 changed files with 111 additions and 194 deletions
+8 -16
View File
@@ -1,26 +1,18 @@
import { Prisma } from '@prisma/client';
import {
certSlugTypeMap,
certIds
certToIdMap,
Certification
} from '../../../../shared/config/certification-settings.js';
import { normalizeDate } from '../../utils/normalize.js';
const {
legacyInfosecQaId,
respWebDesignId,
frontEndDevLibsId,
jsAlgoDataStructId,
dataVis2018Id,
apisMicroservicesId
} = certIds;
const fullStackCertificateIds = [
respWebDesignId,
jsAlgoDataStructId,
frontEndDevLibsId,
dataVis2018Id,
apisMicroservicesId,
legacyInfosecQaId
certToIdMap[Certification.RespWebDesign],
certToIdMap[Certification.JsAlgoDataStruct],
certToIdMap[Certification.FrontEndDevLibs],
certToIdMap[Certification.DataVis],
certToIdMap[Certification.BackEndDevApis],
certToIdMap[Certification.LegacyInfoSecQa]
];
/**
@@ -16,6 +16,8 @@ import {
setupServer,
superRequest
} from '../../../vitest.utils.js';
import { getChallenges } from '../../utils/get-challenges.js';
import { createCertLookup } from './certificate.js';
describe('certificate routes', () => {
setupServer();
@@ -461,3 +463,32 @@ describe('certificate routes', () => {
});
});
});
describe('createCertLookup', () => {
let challenges: ReturnType<typeof getChallenges>;
beforeAll(() => {
// TODO: create a mock challenges array specific to these tests.
challenges = getChallenges();
});
test('should create a lookup for all certifications', () => {
const certLookup = createCertLookup(challenges);
for (const cert of Object.values(Certification)) {
const certData = certLookup[cert];
expect(certData).toHaveProperty('id');
expect(certData).toHaveProperty('tests');
expect(certData).toHaveProperty('challengeType');
}
});
test('each certification should have a unique challenge id', () => {
const certLookup = createCertLookup(challenges);
const ids = Object.values(certLookup)
.map(({ id }) => id)
.sort();
const uniqueIds = Array.from(new Set(ids)).sort();
expect(uniqueIds).toEqual(ids);
});
});
+22 -105
View File
@@ -4,9 +4,9 @@ import type { FastifyPluginCallbackTypebox } from '@fastify/type-provider-typebo
import { getChallenges } from '../../utils/get-challenges.js';
import {
certIds,
Certification,
certSlugTypeMap,
certToIdMap,
certToTitleMap,
currentCertifications,
legacyCertifications,
@@ -83,10 +83,11 @@ function assertTestsExist(
}
}
function getCertById(
challengeId: string,
function getCertBySlug(
cert: Certification,
challenges: ReturnType<typeof getChallenges>
): { id: string; tests: { id: string }[]; challengeType: number } {
const challengeId = certToIdMap[cert];
const challengeById = challenges.filter(({ id }) => id === challengeId)[0];
if (!challengeById) {
throw new Error(`Challenge with id '${challengeId}' not found`);
@@ -96,112 +97,28 @@ function getCertById(
return { id, tests, challengeType };
}
function createCertLookup(
challenges: ReturnType<typeof getChallenges>
): Record<
type CertLookup = Record<
Certification,
{ id: string; tests: { id: string }[]; challengeType: number }
> {
return {
// legacy
[Certification.LegacyFrontEnd]: getCertById(
certIds.legacyFrontEndChallengeId,
challenges
),
[Certification.JsAlgoDataStruct]: getCertById(
certIds.jsAlgoDataStructId,
challenges
),
>;
[Certification.LegacyBackEnd]: getCertById(
certIds.legacyBackEndChallengeId,
challenges
),
[Certification.LegacyDataVis]: getCertById(
certIds.legacyDataVisId,
challenges
),
[Certification.LegacyInfoSecQa]: getCertById(
certIds.legacyInfosecQaId,
challenges
),
[Certification.LegacyFullStack]: getCertById(
certIds.legacyFullStackId,
challenges
),
/**
* Create a lookup from Certification enum values to their corresponding
* challenge metadata (id, tests and challengeType) using the provided
* challenges array.
*
* @param challenges - The array returned by getChallenges().
* @returns A record mapping each Certification to an object with id, tests and challengeType.
*/
export function createCertLookup(
challenges: ReturnType<typeof getChallenges>
): CertLookup {
const certLookup = {} as CertLookup;
// modern
[Certification.RespWebDesign]: getCertById(
certIds.respWebDesignId,
challenges
),
[Certification.FrontEndDevLibs]: getCertById(
certIds.frontEndDevLibsId,
challenges
),
[Certification.DataVis]: getCertById(certIds.dataVis2018Id, challenges),
[Certification.JsAlgoDataStructNew]: getCertById(
certIds.jsAlgoDataStructV8Id,
challenges
),
[Certification.BackEndDevApis]: getCertById(
certIds.apisMicroservicesId,
challenges
),
[Certification.QualityAssurance]: getCertById(certIds.qaV7Id, challenges),
[Certification.InfoSec]: getCertById(certIds.infosecV7Id, challenges),
[Certification.SciCompPy]: getCertById(certIds.sciCompPyV7Id, challenges),
[Certification.DataAnalysisPy]: getCertById(
certIds.dataAnalysisPyV7Id,
challenges
),
[Certification.MachineLearningPy]: getCertById(
certIds.machineLearningPyV7Id,
challenges
),
[Certification.RelationalDb]: getCertById(
certIds.relationalDatabaseV8Id,
challenges
),
[Certification.CollegeAlgebraPy]: getCertById(
certIds.collegeAlgebraPyV8Id,
challenges
),
[Certification.FoundationalCSharp]: getCertById(
certIds.foundationalCSharpV8Id,
challenges
),
[Certification.JsV9]: getCertById(certIds.javascriptV9Id, challenges),
[Certification.RespWebDesignV9]: getCertById(
certIds.respWebDesignV9Id,
challenges
),
[Certification.A2English]: getCertById(certIds.a2EnglishId, challenges),
// upcoming
[Certification.FrontEndDevLibsV9]: getCertById(
certIds.frontEndLibsV9Id,
challenges
),
[Certification.PythonV9]: getCertById(certIds.pythonV9Id, challenges),
[Certification.RelationalDbV9]: getCertById(
certIds.relationalDbV9Id,
challenges
),
[Certification.BackEndDevApisV9]: getCertById(
certIds.backEndDevApisV9Id,
challenges
),
[Certification.FullStackDeveloperV9]: getCertById(
certIds.fullStackDeveloperV9Id,
challenges
),
[Certification.B1English]: getCertById(certIds.b1EnglishId, challenges),
[Certification.A2Spanish]: getCertById(certIds.a2SpanishId, challenges),
[Certification.A1Chinese]: getCertById(certIds.a1ChineseId, challenges),
[Certification.A2Chinese]: getCertById(certIds.a2ChineseId, challenges)
};
for (const cert of Object.values(Certification)) {
certLookup[cert] = getCertBySlug(cert, challenges);
}
return certLookup;
}
interface CertI {
+2 -2
View File
@@ -5,7 +5,7 @@ import * as schemas from '../../schemas.js';
import {
certSlugTypeMap,
certToTitleMap,
certTypeIdMap,
certToIdMap,
completionHours,
oldDataVizId
} from '../../../../shared/config/certification-settings.js';
@@ -52,7 +52,7 @@ export const unprotectedCertificateRoutes: FastifyPluginCallbackTypebox = (
}
const certType = certSlugTypeMap[certSlug];
const certId = certTypeIdMap[certType];
const certId = certToIdMap[certSlug];
const certTitle = certToTitleMap[certSlug];
const completionTime = completionHours[certType] || 300;
const user = await fastify.prisma.user.findFirst({