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

Code before:
        if (!user || !user.isClassroomAccount) {
          return reply.code(404).send({ error: 'User not found' });
        }

        return reply.send({
          userId: user.id
        });
      } catch (error) {
        fastify.log.error(error);
        return reply.code(500).send({ error: 'Failed to retrieve user id' });
      }
    }
  );

  // Endpoint to retrieve user(s) data from a list of user ids
  fastify.post(
    '/api/protected/classroom/get-user-data',
    {
      schema: {
        body: Type.Object({
          userIds: Type.Array(Type.String())
        }),
        response: {
          200: Type.Object({
            data: Type.Record(
              Type.String(), // Id as key
              Type.Array(
                Type.Object({
                  id: Type.String(),
                  completedDate: Type.Number(),
                  challengeName: Type.Optional(Type.String()),
                  files: Type.Optional(Type.Array(Type.Object({}))),
                  githubLink: Type.Optional(Type.String()),
                  solution: Type.Optional(Type.String())
                })
              )
            )
          }),
          400: Type.Object({
            error: Type.String()
          }),
          500: Type.Object({
            error: Type.String()
          })
        }
      }
    },
    async (request, reply) => {
      const { userIds = [] } = request.body as { userIds: string[] };

      // Limit number of users per request for performance

Review comment (suggestion):
If you want to limit the limit the size of the request, I would put that in the schema (e.g. `{ maxItems: 50 }`) since the schema is already validating the shape of the data.

Code after:
        return reply.send({
          userId: user.id
        });
      } catch (error) {
        fastify.log.error(error);
        return reply.code(500).send({ error: 'Failed to retrieve user id' });
      }
    }
  );

  // Endpoint to retrieve user(s) data from a list of user ids
  fastify.post(
    '/api/protected/classroom/get-user-data',
    {
      schema: schemas.classroomGetUserDataSchema
    },
    async (request, reply) => {
      const { userIds = [] } = request.body;

      // Limit number of users per request for performance
      // Send custom error message if this is exceeded
      if (userIds.length > 50) {
        return reply.code(400).send({
          error: 'Too many users requested. Maximum 50 allowed.'
        });
      }

      try {
        // Find all the requested users by user id
        const users = await fastify.prisma.user.findMany({
          where: {
            id: { in: userIds },
            isClassroomAccount: true
          },
          select: {
            id: true,
            completedChallenges: true
          }
        });

        // Map to transform user data into the required format
        const userData: Record<string, NormalizedChallenge[]> = {};

        users.forEach(user => {
          // Normalize challenges
          const normalizedChallenges = normalizeChallenges(
            user.completedChallenges
          );

          userData[user.id] = normalizedChallenges;
        });