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

Code before:
            type: 'info'
          } as const;
        }
        return { userToken: null };
      } catch (err) {
        fastify.log.error(err);
        void reply.code(500);
        return {
          type: 'danger',
          message:
            'Oops! Something went wrong. Please try again in a moment or contact support@freecodecamp.org if the error persists.'
        } as const;
      }
    }
  );

  fastify.post(
    '/user/report-user',
    {
      schema: schemas.reportUser,
      preValidation: (req, reply, done) => {
        // NOTE: Is this the right hook?
        req.body = {
          ...req.body,
          reportDescription: trimTags(req.body.reportDescription)
        };
        done();
      }
    },
    async (req, reply) => {
      try {
        const user = await fastify.prisma.user.findUniqueOrThrow({
          where: { id: req.session.user.id }
        });
        const { username, reportDescription: report } = req.body;

        if (!username || !report || report === '') {
          // NOTE: Do we want to log these instances?
          void reply.code(400);
          return {
            type: 'danger',
            message: 'flash.provide-username'
          } as const;
        }

        await fastify.sendEmail({
          from: 'team@freecodecamp.org',
          to: 'support@freecodecamp.org',
          cc: user.email,
          subject: `Abuse Report: Reporting ${username}'s profile`,
          text: generateReportEmail(user, username, report)

Review comment (suggestion):
```suggestion
        req.body.reportDescription = trimTags(req.body.reportDescription);
```
Since we have no use for the old body, I don't see the harm in mutating it here.

Code after:
            keyboardShortcuts: true,
            linkedin: true,
            location: true,
            name: true,
            partiallyCompletedChallenges: true,
            picture: true,
            portfolio: true,
            profileUI: true,
            progressTimestamps: true,
            savedChallenges: true,
            sendQuincyEmail: true,
            theme: true,
            twitter: true,
            username: true,
            usernameDisplay: true,
            website: true,
            yearsTopContributor: true
          }
        });

        const [userToken, user] = await Promise.all([userTokenP, userP]);

        if (!user?.username) {
          void res.code(500);
          return { user: {}, result: '' };
        }

        const encodedToken = userToken
          ? encodeUserToken(userToken.id)
          : undefined;

        const {
          username,
          usernameDisplay,
          completedChallenges,
          progressTimestamps,
          twitter,
          profileUI,
          savedChallenges,
          partiallyCompletedChallenges,
          ...publicUser
        } = user;

        return {
          user: {
            [username]: {
              ...removeNulls(publicUser),
              completedChallenges: normalizeChallenges(completedChallenges),
              completedChallengeCount: completedChallenges.length,
              // This assertion is necessary until the database is normalized.
              calendar: getCalendar(