Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix-coupon-usage #130

Merged
merged 1 commit into from
Jul 26, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 16 additions & 12 deletions src/services/couponServices/buyerApplyCoupon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ export const buyerApplyCouponService = async (req: Request, res: Response) => {
if (coupon.usageTimes == coupon.maxUsageLimit) {
return res.status(400).json({ message: 'Coupon Discount Ended' });
}

if (req.user?.id) {
if (coupon.usedBy.includes(req.user.id)) {
return res.status(400).json({ message: 'You already used this coupon discount' });
}
}
}
const couponProductId = coupon.product.id;

Expand Down Expand Up @@ -79,22 +85,20 @@ export const buyerApplyCouponService = async (req: Request, res: Response) => {
await sendNotification({
content: `Coupon Code successfully activated discount on product: ${couponCartItem.product.name}`,
type: 'coupon',
user: cart.user
})
user: cart.user,
});

await sendNotification({
content: `Buyer: "${cart?.user.firstName} ${cart?.user.lastName}" used coupon and got discount on product: "${couponCartItem.product.name}"`,
type:'coupon',
user: coupon.vendor
type: 'coupon',
user: coupon.vendor,
});

return res
.status(200)
.json({
message: `Coupon Code successfully activated discount on product: ${couponCartItem.product.name}`,
amountDiscounted: amountReducted,
});
return res.status(200).json({
message: `Coupon Code successfully activated discount on product: ${couponCartItem.product.name}`,
amountDiscounted: amountReducted,
});
} catch (error) {
return responseError(res, 500, (error as Error).message);
}
return responseError(res, 500, (error as Error).message);
}
};
Loading
You are viewing a condensed version of this merge commit. You can view the full changes here.