Untitled

 avatar
unknown
plain_text
2 years ago
3.1 kB
2
Indexable
const editUserFeeOrProfitMargin = asyncHandler(async (req, res) => {
  if (req.user.type !== "ADMIN") {
    res.status(400);
    throw new Error("Invalid User type");
  }
  const {
    id,
    confermation,
    reservation,
    sold,
    followUp,
    aftersail,
    leadCost,
    maxCCAs,
  } = req.body;

  if (typeof id !== "string" || !id.trim()) {
    res.status(400);
    throw new Error("Invalid id parameter");
  }
  if (!confermation || Number.isNaN(confermation) || confermation < 0) {
    res.status(400);
    throw new Error("Invalid confermation ammount");
  }
  if (!reservation || Number.isNaN(reservation) || reservation < 0) {
    res.status(400);
    throw new Error("Invalid reservation ammount");
  }
  if (!sold || Number.isNaN(sold) || sold < 0) {
    res.status(400);
    throw new Error("Invalid sold ammount");
  }
  if (!followUp || Number.isNaN(followUp) || followUp < 0) {
    res.status(400);
    throw new Error("Invalid followUp ammount");
  }
  if (!aftersail || Number.isNaN(aftersail) || aftersail < 0) {
    res.status(400);
    throw new Error("Invalid aftersail ammount");
  }

  const user = await User.findById(id);
  if (!user) {
    res.status(404);
    throw new Error("User not found");
  }
  if (user.type === "BO") {
    if (!leadCost || Number.isNaN(leadCost) || leadCost < 0) {
      res.status(400);
      throw new Error("Invalid leadCost amount");
    }
  }
  if (user.type === "BO") {
    const info = await Fee.findById(user.profits_fees);
    if (info.type === "custom") {
      await Fee.deleteOne({ _id: info._id });
    }
    const paymentInfo = await Fee.create({
      newLead_reservation_FlatFee: reservation,
      newLead_confermation_FlatFee: confermation,
      newLead_selling_PercentFee: sold,
      afterSail_FlatFee: aftersail,
      followUp_FlatFee: followUp,
      updateDate: new Date().toDateString(),
      leadCost: leadCost,
      type: "custom",
      boId: id,
    });
    user.profits_fees = paymentInfo._id;
    await user.save();

    if (typeof maxCCAs !== "undefined") {
      if (Number.isNaN(maxCCAs) || maxCCAs < 0) {
        res.status(400);
        throw new Error("Invalid maxCCAs value");
      }
      user.maxCCAs = maxCCAs;
      user.maxCCAsIsExplicit = true;
      await user.save();
    }
  } else if (user.type === "CCA") {
    const info = await Profite.findById(user.profits_fees);
    if (info.type === "custom") {
      await Profite.deleteOne({ _id: info._id });
    }
    const paymentInfo = await Profite.create({
      newLead_reservation_FlatProfit: reservation,
      newLead_confermation_FlatProfit: confermation,
      newLead_selling_PercentProfit: sold,
      afterSail_FlatProfit: aftersail,
      followUp_FlatProfit: followUp,
      updateDate: new Date().toDateString(),
      type: "custom",
      ccaId: id,
    });
    user.profits_fees = paymentInfo._id;
    await user.save();
  } else {
    console.log(err);
  }
  res.status(200).json({
    message: "updated successfully",
  });
});
Editor is loading...
Leave a Comment