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

Code before:
    }
  );

  fastify.get(
    '/resubscribe/:unsubscribeId',
    {
      schema: schemas.resubscribe,
      errorHandler(error, request, reply) {
        if (error.validation) {
          const { origin } = getRedirectParams(request);
          void reply.code(302);
          void reply.redirectWithMessage(origin, {
            type: 'info',
            content:
              'We were unable to process this request, please check and try again.'
          });
        } else {
          fastify.errorHandler(error, request, reply);
        }
      }
    },
    async (req, reply) => {
      try {
        const { origin } = getRedirectParams(req);
        const { unsubscribeId } = req.params;
        const [user] = await fastify.prisma.user.findMany({
          where: { unsubscribeId }
        });

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

        await fastify.prisma.user.update({
          where: { id: user.id },
          data: {
            sendQuincyEmail: true
          }
        });

        return reply.redirectWithMessage(origin, {
          type: 'success',
          content:
            "We've successfully updated your email preferences. Thank you for resubscribing."
        });
      } catch (error) {
        fastify.log.error(error);

Review comment (suggestion):
```suggestion
        const user = await fastify.prisma.user.findFirst({
```
Same functionality, but more direct.

Code after:

  fastify.get(
    '/resubscribe/:unsubscribeId',
    {
      schema: schemas.resubscribe,
      errorHandler(error, request, reply) {
        if (error.validation) {
          const { origin } = getRedirectParams(request);
          void reply.code(302);
          void reply.redirectWithMessage(origin, {
            type: 'info',
            content:
              'We were unable to process this request, please check and try again.'
          });
        } else {
          fastify.errorHandler(error, request, reply);
        }
      }
    },
    async (req, reply) => {
      try {
        const { origin } = getRedirectParams(req);
        const { unsubscribeId } = req.params;
        const user = await fastify.prisma.user.findFirst({
          where: { unsubscribeId }
        });

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

        await fastify.prisma.user.update({
          where: { id: user.id },
          data: {
            sendQuincyEmail: true
          }
        });

        return reply.redirectWithMessage(origin, {
          type: 'success',
          content:
            "We've successfully updated your email preferences. Thank you for resubscribing."
        });
      } catch (error) {
        fastify.log.error(error);
        void reply.code(302);
        return reply.redirectWithMessage(origin, {