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

Code before:
          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 challenge.url?.endsWith(tutorialRepo ?? '');
      });

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

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

        if (!tokenInfo)
          return { type: 'error', msg: 'User token not found' } as const;

        const { userId } = tokenInfo;

        const user = await fastify.prisma.user.findFirstOrThrow({
          where: { id: userId }
        });

        if (!user)
          return {
            type: 'error',
            msg: 'User for user token not found'
          } as const;

        const completedDate = Date.now();
        const { completedChallenges = [], partiallyCompletedChallenges = [] } =
          user;

        const isCompleted = completedChallenges.some(
          challenge => challenge.id === challengeId
        );

        if (challengeType === 13 && !isCompleted) {
          const finalChallenge = {
            id: challengeId,
            completedDate

Review comment (suggestion):
```suggestion
        if (!tokenInfo) {
          void reply.code(400);
          return { type: 'error', msg: 'User token not found' } as const;
        }
```

I got a 200 success response when I used a valid token, but didn't have a match in the database:

![Screenshot from 2023-07-26 14-58-31](https://github.com/freeCodeCamp/freeCodeCamp/assets/20648924/930437b6-ed8c-4cd9-bdc0-7d80368b3d2c)

I recall coming across this before. Fastify defaults to what's in the schema or something I think - so we need to make sure we're setting status codes correctly, so it returns the expected things. If a client is relying on the code or the string type (`success`|`error`|whatever), it would cause some issues. I will try to remember to bring this up at the next meeting - remind me if you can @Sembauke.

Code after:
      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;
      }

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

        if (!tokenInfo) {
          void reply.code(400);
          return { type: 'error', msg: 'User token not found' } as const;
        }

        const { userId } = tokenInfo;

        const user = await fastify.prisma.user.findFirstOrThrow({
          where: { id: userId }
        });

        if (!user) {
          void reply.code(400);
          return {
            type: 'error',
            msg: 'User for user token not found'
          } as const;
        }

        const completedDate = Date.now();
        const { completedChallenges = [], partiallyCompletedChallenges = [] } =