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.'
          });
        }

        for (const user of users) {
          await fastify.prisma.user.update({
            where: { id: user.id },
            data: {
              sendQuincyEmail: false
            }
          });
        }

        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) {

Review comment (suggestion):
For a one-to-one, we probably want:

```suggestion
        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);
```

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,