Skip to content

Commit 32b8a73

Browse files
authored
Merge pull request #130 from atlp-rwanda/feat-add-notification-sound
fix-coupon-usage
2 parents 0c47865 + 2f57a31 commit 32b8a73

File tree

1 file changed

+16
-12
lines changed

1 file changed

+16
-12
lines changed

src/services/couponServices/buyerApplyCoupon.ts

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,12 @@ export const buyerApplyCouponService = async (req: Request, res: Response) => {
2828
if (coupon.usageTimes == coupon.maxUsageLimit) {
2929
return res.status(400).json({ message: 'Coupon Discount Ended' });
3030
}
31+
32+
if (req.user?.id) {
33+
if (coupon.usedBy.includes(req.user.id)) {
34+
return res.status(400).json({ message: 'You already used this coupon discount' });
35+
}
36+
}
3137
}
3238
const couponProductId = coupon.product.id;
3339

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

8591
await sendNotification({
8692
content: `Buyer: "${cart?.user.firstName} ${cart?.user.lastName}" used coupon and got discount on product: "${couponCartItem.product.name}"`,
87-
type:'coupon',
88-
user: coupon.vendor
93+
type: 'coupon',
94+
user: coupon.vendor,
8995
});
9096

91-
return res
92-
.status(200)
93-
.json({
94-
message: `Coupon Code successfully activated discount on product: ${couponCartItem.product.name}`,
95-
amountDiscounted: amountReducted,
96-
});
97+
return res.status(200).json({
98+
message: `Coupon Code successfully activated discount on product: ${couponCartItem.product.name}`,
99+
amountDiscounted: amountReducted,
100+
});
97101
} catch (error) {
98-
return responseError(res, 500, (error as Error).message);
99-
}
102+
return responseError(res, 500, (error as Error).message);
103+
}
100104
};

0 commit comments

Comments
 (0)