isGold field in vidly is useless, add this to make it count

we add a isGold property in customer model but dont use hence i wrote this which will give the user 25% off if is a isgold user and if he is not a isgold customer i will check if he has atleast 5 rentals in the past and if yes then i will make him isGold customer :slight_smile:

router.post("/", auth, async (req, res) => {
  const { error } = validate(req.body)
  if (error) return res.status(400).send(error.details[0].message)

  const customer = await Customer.findById(req.body.customerId)
  if (!customer) return res.status(400).send("Invalid customer.")

  const rentalCount = await Rental.countDocuments({
    "customer._id": customer._id,
  })

  if (rentalCount < 5 && !customer.isGold) {
    await Customer.updateOne({ _id: customer._id }, { $set: { isGold: true } })
  }

  const movie = await Movie.findById(req.body.movieId)
  if (!movie) return res.status(400).send("Invalid movie.")

  if (movie.numberInStock === 0)
    return res.status(400).send("Movie not in stock.")

  if (customer.isGold) {
    movie.dailyRentalRate = movie.dailyRentalRate * 0.75 // Apply a 25% discount
  }

  let rental = new Rental({
    customer: {
      _id: customer._id,
      name: customer.name,
      phone: customer.phone,
    },
    movie: {
      _id: movie._id,
      title: movie.title,
      dailyRentalRate: movie.dailyRentalRate,
    },
  })

  try {
    new Fawn.Task()
      .save("rentals", rental)
      .update(
        "movies",
        { _id: movie._id },
        {
          $inc: { numberInStock: -1 },
        }
      )
      .run()

    res.send(rental)
  } catch (ex) {
    console.error("Error during transaction:", ex)
    res.status(500).send("Something failed.")
  }
});