Repository: freeCodeCamp/freeCodeCamp
File: api-server/src/server/boot/certificate.js (JavaScript)

Code before:
} = certIds;

const log = debug('fcc:certification');

export default function bootCertificate(app) {
  const api = app.loopback.Router();
  // TODO: rather than getting all the challenges, then grabbing the certs,
  // consider just getting the certs.
  const certTypeIds = createCertTypeIds(getChallenges());
  const showCert = createShowCert(app);
  const verifyCert = createVerifyCert(certTypeIds, app);

  api.put('/certificate/verify', ifNoUser401, ifNoSuperBlock404, verifyCert);
  api.get('/certificate/showCert/:username/:certSlug', showCert);
  api.get('/certificate/verify-can-claim-cert', deprecatedEndpoint);
  app.use(api);
}

export function getFallbackFullStackDate(completedChallenges, completedDate) {
  var chalIds = [
    certIds.respWebDesignId,
    certIds.jsAlgoDataStructId,
    certIds.frontEndDevLibsId,
    certIds.dataVis2018Id,
    certIds.apisMicroservicesId,
    certIds.legacyInfosecQaId
  ];

  const latestCertDate = completedChallenges
    .filter(chal => chalIds.includes(chal.id))
    .sort((a, b) => b.completedDate - a.completedDate)[0]?.completedDate;

  return latestCertDate ? latestCertDate : completedDate;
}

const certSlugs = Object.keys(certSlugTypeMap);

function ifNoSuperBlock404(req, res, next) {
  const { certSlug } = req.body;
  if (certSlug && certSlugs.includes(certSlug)) {
    return next();
  }
  return res.status(404).end();
}

const renderCertifiedEmail = loopback.template(
  path.join(__dirname, '..', 'views', 'emails', 'certified.ejs')
);

function createCertTypeIds(allChallenges) {
  return {

Review comment (suggestion):
We already destructure these further up. Shall we remove an extraneous lookup with:

```suggestion
    respWebDesignId,
    jsAlgoDataStructId,
    frontEndDevLibsId,
    dataVis2018Id,
    apisMicroservicesId,
    legacyInfosecQaId
```

Code after:
} = certIds;

const log = debug('fcc:certification');

export default function bootCertificate(app) {
  const api = app.loopback.Router();
  // TODO: rather than getting all the challenges, then grabbing the certs,
  // consider just getting the certs.
  const certTypeIds = createCertTypeIds(getChallenges());
  const showCert = createShowCert(app);
  const verifyCert = createVerifyCert(certTypeIds, app);

  api.put('/certificate/verify', ifNoUser401, ifNoSuperBlock404, verifyCert);
  api.get('/certificate/showCert/:username/:certSlug', showCert);
  api.get('/certificate/verify-can-claim-cert', deprecatedEndpoint);
  app.use(api);
}

export function getFallbackFullStackDate(completedChallenges, completedDate) {
  var chalIds = [
    respWebDesignId,
    jsAlgoDataStructId,
    frontEndDevLibsId,
    dataVis2018Id,
    apisMicroservicesId,
    legacyInfosecQaId
  ];

  const latestCertDate = completedChallenges
    .filter(chal => chalIds.includes(chal.id))
    .sort((a, b) => b.completedDate - a.completedDate)[0]?.completedDate;

  return latestCertDate ? latestCertDate : completedDate;
}

const certSlugs = Object.keys(certSlugTypeMap);

function ifNoSuperBlock404(req, res, next) {
  const { certSlug } = req.body;
  if (certSlug && certSlugs.includes(certSlug)) {
    return next();
  }
  return res.status(404).end();
}

const renderCertifiedEmail = loopback.template(
  path.join(__dirname, '..', 'views', 'emails', 'certified.ejs')
);

function createCertTypeIds(allChallenges) {
  return {