fix(coupon): add expiration check and error handling for expired coupons

This commit is contained in:
Chang lue Tsen 2025-10-16 09:31:33 -04:00
parent 8b48286365
commit 1b76794251
3 changed files with 8 additions and 0 deletions

View File

@ -83,6 +83,12 @@ func (l *PurchaseLogic) Purchase(req *types.PortalPurchaseRequest) (resp *types.
if couponInfo.Count != 0 && couponInfo.Count <= couponInfo.UsedCount { if couponInfo.Count != 0 && couponInfo.Count <= couponInfo.UsedCount {
return nil, errors.Wrapf(xerr.NewErrCode(xerr.CouponInsufficientUsage), "coupon used") return nil, errors.Wrapf(xerr.NewErrCode(xerr.CouponInsufficientUsage), "coupon used")
} }
// Check expiration time
expireTime := time.Unix(couponInfo.ExpireTime, 0)
if time.Now().After(expireTime) {
return nil, errors.Wrapf(xerr.NewErrCode(xerr.CouponExpired), "coupon expired")
}
couponSub := tool.StringToInt64Slice(couponInfo.Subscribe) couponSub := tool.StringToInt64Slice(couponInfo.Subscribe)
if len(couponSub) > 0 && !tool.Contains(couponSub, req.SubscribeId) { if len(couponSub) > 0 && !tool.Contains(couponSub, req.SubscribeId) {
return nil, errors.Wrapf(xerr.NewErrCode(xerr.CouponNotApplicable), "coupon not match") return nil, errors.Wrapf(xerr.NewErrCode(xerr.CouponNotApplicable), "coupon not match")

View File

@ -57,6 +57,7 @@ const (
CouponAlreadyUsed uint32 = 50002 // Coupon has already been used CouponAlreadyUsed uint32 = 50002 // Coupon has already been used
CouponNotApplicable uint32 = 50003 // Coupon does not match the order or conditions CouponNotApplicable uint32 = 50003 // Coupon does not match the order or conditions
CouponInsufficientUsage uint32 = 50004 // Coupon has insufficient remaining uses CouponInsufficientUsage uint32 = 50004 // Coupon has insufficient remaining uses
CouponExpired uint32 = 50005 // Coupon is expired
) )
// Subscribe // Subscribe

View File

@ -46,6 +46,7 @@ func init() {
CouponAlreadyUsed: "Coupon has already been used", CouponAlreadyUsed: "Coupon has already been used",
CouponNotApplicable: "Coupon does not match the order or conditions", CouponNotApplicable: "Coupon does not match the order or conditions",
CouponInsufficientUsage: "Coupon has insufficient remaining uses", CouponInsufficientUsage: "Coupon has insufficient remaining uses",
CouponExpired: "Coupon is expired",
// Subscribe // Subscribe
SubscribeExpired: "Subscribe is expired", SubscribeExpired: "Subscribe is expired",