Unique OrderId Validation for Coupon Use
This commit is contained in:
parent
f85a516808
commit
f4313fcef5
@ -286,21 +286,52 @@ export const usedCoupon = async (req, res) => {
|
|||||||
const couponData = await AffiliateModel.findOne({
|
const couponData = await AffiliateModel.findOne({
|
||||||
coupon_code: coupon_code,
|
coupon_code: coupon_code,
|
||||||
});
|
});
|
||||||
|
//order exists or not
|
||||||
|
|
||||||
// Check if the coupon exists
|
|
||||||
if (!couponData) {
|
if (!couponData) {
|
||||||
|
// Check if the coupon exists
|
||||||
return res.status(404).json({
|
return res.status(404).json({
|
||||||
success: false,
|
success: false,
|
||||||
message: "Coupon not found",
|
message: "Coupon not found",
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
// Check if orderId is unique
|
||||||
|
try {
|
||||||
|
const isOrderIdUnique = await AffiliateModel.find(
|
||||||
|
{},
|
||||||
|
{
|
||||||
|
coupon_used_history: 1,
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
let orderIdFound = false;
|
||||||
|
|
||||||
|
isOrderIdUnique.forEach((data) => {
|
||||||
|
data.coupon_used_history.forEach((subItem) => {
|
||||||
|
if (subItem.orderId == orderId) {
|
||||||
|
orderIdFound = true;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
if (orderIdFound) {
|
||||||
|
return res.status(400).json({
|
||||||
|
success: false,
|
||||||
|
message: "Error: OrderId already used",
|
||||||
|
});
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.error(error);
|
||||||
|
return res.status(500).json({
|
||||||
|
success: false,
|
||||||
|
message: "Internal server error",
|
||||||
|
});
|
||||||
|
}
|
||||||
|
//If not unique then create
|
||||||
const {
|
const {
|
||||||
valid_till,
|
valid_till,
|
||||||
is_coupon_active,
|
is_coupon_active,
|
||||||
total_earning,
|
|
||||||
is_affiliate_active,
|
is_affiliate_active,
|
||||||
coupon_claimed,
|
|
||||||
affiliate_discount_amount,
|
affiliate_discount_amount,
|
||||||
_id,
|
_id,
|
||||||
} = couponData;
|
} = couponData;
|
||||||
|
Loading…
Reference in New Issue
Block a user