Repository: freeCodeCamp/freeCodeCamp
File: api-server/src/server/boot/donate.js (JavaScript)

Code before:
        err.type === 'UserActionRequired' ||
        err.type === 'PaymentMethodRequired'
      ) {
        return res.status(402).send({ error: err });
      }
      if (err.type === 'InvalidRequest')
        return res.status(400).send({ error: err });
      return res.status(500).send({
        error: 'Donation failed due to a server error.'
      });
    });
  }

  async function createStripeDonation(req, res) {
    const { body } = req;
    const { amount, duration, email, subscriptionId } = body;
    try {
      const subscription = await stripe.subscriptions.retrieve(subscriptionId);
      const isSubscriptionActive = subscription.status === 'active';
      const productId = subscription.items.data[0].plan.product;
      const isSubscribedInMinutes = inLastFiveMinutes(
        subscription.current_period_start
      );
      const isProductIdValid = allStripeProductIdsArray.includes(productId);

      if (isSubscriptionActive && isProductIdValid && isSubscribedInMinutes) {
        const [donatingUser] = await User.findOrCreate(
          { where: { email } },
          { email }
        );
        const donation = {
          email,
          amount,
          duration,
          provider: 'stripe',
          subscriptionId,
          customerId: subscription.customer,
          startDate: new Date().toISOString()
        };
        await donatingUser.createDonation(donation);
        return res.status(200).send({ isDonating: true });
      } else {
        throw new Error('Donation failed due to a server error.');
      }
    } catch (err) {
      return res
        .status(500)
        .send({ error: 'Donation failed due to a server error.' });
    }
  }


Review comment (suggestion):
```suggestion
      if (isSubscriptionActive && isProductIdValid && isStartedRecently) {
```

Code after:
        err.type === 'UserActionRequired' ||
        err.type === 'PaymentMethodRequired'
      ) {
        return res.status(402).send({ error: err });
      }
      if (err.type === 'InvalidRequest')
        return res.status(400).send({ error: err });
      return res.status(500).send({
        error: 'Donation failed due to a server error.'
      });
    });
  }

  async function createStripeDonation(req, res) {
    const { body } = req;
    const { amount, duration, email, subscriptionId } = body;
    try {
      const subscription = await stripe.subscriptions.retrieve(subscriptionId);
      const isSubscriptionActive = subscription.status === 'active';
      const productId = subscription.items.data[0].plan.product;
      const isStartedRecently = inLastFiveMinutes(
        subscription.current_period_start
      );
      const isProductIdValid = allStripeProductIdsArray.includes(productId);

      if (isSubscriptionActive && isProductIdValid && isStartedRecently) {
        const [donatingUser] = await User.findOrCreate(
          { where: { email } },
          { email }
        );
        const donation = {
          email,
          amount,
          duration,
          provider: 'stripe',
          subscriptionId,
          customerId: subscription.customer,
          startDate: new Date().toISOString()
        };
        await donatingUser.createDonation(donation);
        return res.status(200).send({ isDonating: true });
      } else {
        throw new Error('Donation failed due to a server error.');
      }
    } catch (err) {
      return res
        .status(500)
        .send({ error: 'Donation failed due to a server error.' });
    }
  }