Untitled
unknown
plain_text
2 years ago
4.4 kB
17
Indexable
const getUserFeeOrProfitMargins = asyncHandler(async (req, res) => {
if (req.user.type !== "ADMIN") {
res.status(400);
throw new Error("Invalid User type");
}
const id = req.params.id;
if (typeof id !== "string" || !id.trim()) {
res.status(400);
throw new Error("Invalid id parameter");
}
const user = await User.findById(id);
if (!user) {
res.status(404);
throw new Error("User not found");
}
if (user.type === "CCA") {
const info = await Profite.findById(user.profits_fees);
res.status(200).json({
reservation: info.newLead_reservation_FlatProfit,
confermation: info.newLead_confermation_FlatProfit,
sold: info.newLead_selling_PercentProfit,
aftersail: info.afterSail_FlatProfit,
followUp: info.followUp_FlatProfit,
});
} else {
const info = await Fee.findById(user.profits_fees);
const responseData = {
reservation: info.newLead_reservation_FlatFee,
confermation: info.newLead_confermation_FlatFee,
sold: info.newLead_selling_PercentFee,
aftersail: info.afterSail_FlatFee,
followUp: info.followUp_FlatFee,
leadCost: info.leadCost,
};
if (user.type === "BO") {
responseData.maxCCAs = user.maxCCAs;
}
res.status(200).json(responseData);
}
});
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") {
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 {
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();
}
res.status(200).json({
message: "updated successfully",
});
});Editor is loading...
Leave a Comment