diff --git a/api-server/server/boot/certificate.js b/api-server/server/boot/certificate.js index d5ca128c5c..26aa352a98 100644 --- a/api-server/server/boot/certificate.js +++ b/api-server/server/boot/certificate.js @@ -41,6 +41,16 @@ export default function bootCertificate(app) { app.use(api); } +export function getFallbackFrontEndDate(completedChallenges, completedDate) { + var chalIds = [...Object.values(certIds), oldDataVizId]; + + const latestCertDate = completedChallenges + .filter(chal => chalIds.includes(chal.id)) + .sort((a, b) => b.completedDate - a.completedDate)[0].completedDate; + + return latestCertDate ? latestCertDate : completedDate; +} + const noNameMessage = dedent` We need your name so we can put it on your certification. Add your name to your account settings and click the save button. @@ -451,13 +461,10 @@ function createShowCert(app) { // if fullcert is not found, return the latest completedDate if (certType === 'isFullStackCert' && !certChallenge) { - var chalIds = [...Object.values(certIds), oldDataVizId]; - - const latestCertDate = completedChallenges - .filter(chal => chalIds.includes(chal.id)) - .sort((a, b) => a.completedDate < b.completedDate)[0].completedDate; - - completedDate = latestCertDate ? latestCertDate : completedDate; + completedDate = getFallbackFrontEndDate( + completedChallenges, + completedDate + ); } const { username, name } = user; diff --git a/api-server/server/boot_tests/certificate.test.js b/api-server/server/boot_tests/certificate.test.js new file mode 100644 index 0000000000..9d909e821b --- /dev/null +++ b/api-server/server/boot_tests/certificate.test.js @@ -0,0 +1,12 @@ +/* global it expect */ + +import { getFallbackFrontEndDate } from '../boot/certificate'; +import { fullStackChallenges } from './fixtures'; + +describe('boot/certificate', () => { + describe('getFallbackFrontEndDate', () => { + it('should return the date of the latest completed challenge', () => { + expect(getFallbackFrontEndDate(fullStackChallenges)).toBe(1685210952511); + }); + }); +}); diff --git a/api-server/server/boot_tests/fixtures.js b/api-server/server/boot_tests/fixtures.js index 2e60053bfa..5c1b3d939c 100644 --- a/api-server/server/boot_tests/fixtures.js +++ b/api-server/server/boot_tests/fixtures.js @@ -178,3 +178,42 @@ export const mockPathMigrationMap = { 'challenge-one': '/learn/superblock/block/challenge-one', 'challenge-two': '/learn/superblock/block/challenge-two' }; + +export const fullStackChallenges = [ + { + completedDate: 1585210952511, + id: '5a553ca864b52e1d8bceea14', + challengeType: 7, + files: [] + }, + { + completedDate: 1585210952511, + id: '561add10cb82ac38a17513bc', + challengeType: 7, + files: [] + }, + { + completedDate: 1588665778679, + id: '561acd10cb82ac38a17513bc', + challengeType: 7, + files: [] + }, + { + completedDate: 1685210952511, + id: '561abd10cb81ac38a17513bc', + challengeType: 7, + files: [] + }, + { + completedDate: 1585210952511, + id: '561add10cb82ac38a17523bc', + challengeType: 7, + files: [] + }, + { + completedDate: 1588665778679, + id: '561add10cb82ac38a17213bc', + challengeType: 7, + files: [] + } +];