Repository: freeCodeCamp/freeCodeCamp
File: api/src/routes/email-subscription.ts (TypeScript)

Code before:
    async (req, reply) => {
      try {
        const { origin } = getRedirectParams(req);
        const { unsubscribeId } = req.params;
        const users = await fastify.prisma.user.findMany({
          where: { unsubscribeId }
        });

        if (!users.length) {
          void reply.code(302);
          return reply.redirectWithMessage(origin, {
            type: 'info',
            content: 'We could not find an account to unsubscribe.'
          });
        }

        const userUpdatePromises = [];
        for (const user of users) {
          const userUpdatePromise = fastify.prisma.user.update({
            where: { id: user.id },
            data: {
              sendQuincyEmail: false
            }
          });
          userUpdatePromises.push(userUpdatePromise);
        }

        await Promise.all(userUpdatePromises);

        return reply.redirectWithMessage(
          `${origin}/unsubscribed/${unsubscribeId}`,
          {
            type: 'success',
            content: "We've successfully updated your email preferences."
          }
        );
      } catch (error) {
        fastify.log.error(error);
        void reply.code(302);
        return reply.redirectWithMessage(origin, {
          type: 'danger',
          content: 'Something went wrong.'
        });
      }
    }
  );

  fastify.get(
    '/resubscribe/:unsubscribeId',
    {
      schema: schemas.resubscribe,

Review comment (suggestion):
```suggestion
        const userUpdatePromises = users.map(user =>
          fastify.prisma.user.update({
            where: { id: user.id },
            data: {
              sendQuincyEmail: false
            }
          })
        );
```
A little tidier, imo.

Code after:
    async (req, reply) => {
      try {
        const { origin } = getRedirectParams(req);
        const { unsubscribeId } = req.params;
        const users = await fastify.prisma.user.findMany({
          where: { unsubscribeId }
        });

        if (!users.length) {
          void reply.code(302);
          return reply.redirectWithMessage(origin, {
            type: 'info',
            content: 'We could not find an account to unsubscribe.'
          });
        }

        const userUpdatePromises = users.map(user =>
          fastify.prisma.user.update({
            where: { id: user.id },
            data: {
              sendQuincyEmail: false
            }
          })
        );

        await Promise.all(userUpdatePromises);

        return reply.redirectWithMessage(
          `${origin}/unsubscribed/${unsubscribeId}`,
          {
            type: 'success',
            content: "We've successfully updated your email preferences."
          }
        );
      } catch (error) {
        fastify.log.error(error);
        void reply.code(302);
        return reply.redirectWithMessage(origin, {
          type: 'danger',
          content: 'Something went wrong.'
        });
      }
    }
  );

  fastify.get(
    '/resubscribe/:unsubscribeId',
    {
      schema: schemas.resubscribe,
      errorHandler(error, request, reply) {
        if (error.validation) {