Repository: freeCodeCamp/freeCodeCamp
File: api/src/routes/challenge.ts (TypeScript)

Code before:
        void reply.code(400);
        return { msg: `'tutorialId' not found in request body` };
      }

      if (!encodedUserToken) {
        void reply.code(400);
        return { msg: `'coderoad-user-token' not found in request headers` };
      }

      try {
        if (typeof encodedUserToken === 'string' && jwtSecret) {
          const payload = jwt.verify(encodedUserToken, jwtSecret) as JwtPayload;
          userToken = payload.userToken;
        }
      } catch {
        void reply.code(400);
        return { msg: `invalid user token` };
      }

      const tutorialRepo = tutorialId?.split(':')[0];
      const tutorialOrg = tutorialRepo?.split('/')?.[0];

      if (tutorialOrg !== 'freeCodeCamp') {
        void reply.code(400);
        return { msg: `Tutorial not hosted on freeCodeCamp GitHub account` };
      }

      console.log('tutorialRepo', tutorialRepo);

      const codeRoadChallenges = challenges.filter(
        ({ challengeType }) => challengeType === 12 || challengeType === 13
      );

      const challenge = codeRoadChallenges.find(challenge => {
        if (!tutorialRepo) {
          return false;
        }
        return challenge.url?.endsWith(tutorialRepo);
      });

      if (!challenge) {
        void reply.code(400);
        return { msg: 'Tutorial name is not valid' };
      }

      const { id: challengeId, challengeType } = challenge;
      try {
        const tokenInfo = await fastify.prisma.userToken.findUnique({
          where: { id: userToken }
        });


Review comment (suggestion):
@moT01 I'm pretty sure you said something like this to me the other day, but just to confirm: can we get rid of the `if (deploymentEnv !== 'staging' && environment !== 'development') {` conditional in api-server? It looks like this is a leftover from early testing, but I wanted to confirm.


Code after:
      const { 'coderoad-user-token': encodedUserToken } = req.headers;
      const { tutorialId } = req.body;

      if (!tutorialId) {
        void reply.code(400);
        return {
          type: 'error',
          msg: `'tutorialId' not found in request body`
        } as const;
      }

      if (!encodedUserToken) {
        void reply.code(400);
        return {
          type: 'error',
          msg: `'Coderoad-User-Token' not found in request headers`
        } as const;
      }

      try {
        if (typeof encodedUserToken === 'string' && jwtSecret) {
          const payload = jwt.verify(encodedUserToken, jwtSecret) as JwtPayload;
          userToken = payload.userToken;
        }
      } catch {
        void reply.code(400);
        return { type: 'error', msg: `invalid user token` } as const;
      }

      const tutorialRepo = tutorialId?.split(':')[0];
      const tutorialOrg = tutorialRepo?.split('/')?.[0];

      if (tutorialOrg !== 'freeCodeCamp') {
        void reply.code(400);
        return {
          type: 'error',
          msg: `Tutorial not hosted on freeCodeCamp GitHub account`
        } as const;
      }

      const codeRoadChallenges = challenges.filter(
        ({ challengeType }) => challengeType === 12 || challengeType === 13
      );

      const challenge = codeRoadChallenges.find(challenge => {
        return tutorialRepo && challenge.url?.endsWith(tutorialRepo);
      });

      if (!challenge) {
        void reply.code(400);
        return { type: 'error', msg: 'Tutorial name is not valid' } as const;