feat: implement coupon management and receive flow
This commit is contained in:
@@ -58,7 +58,7 @@ func (s *CouponTestSuite) Test_CouponFlow() {
|
||||
cp := &models.Coupon{
|
||||
TenantID: tenantID,
|
||||
Title: "Save 5",
|
||||
Type: "fix_amount",
|
||||
Type: consts.CouponTypeFixAmount,
|
||||
Value: 500,
|
||||
MinOrderAmount: 1000,
|
||||
}
|
||||
@@ -68,7 +68,7 @@ func (s *CouponTestSuite) Test_CouponFlow() {
|
||||
uc := &models.UserCoupon{
|
||||
UserID: user.ID,
|
||||
CouponID: cp.ID,
|
||||
Status: "unused",
|
||||
Status: consts.UserCouponStatusUnused,
|
||||
}
|
||||
models.UserCouponQuery.WithContext(ctx).Create(uc)
|
||||
|
||||
@@ -91,7 +91,7 @@ func (s *CouponTestSuite) Test_CouponFlow() {
|
||||
TenantID: tenantID,
|
||||
ContentID: c.ID,
|
||||
PriceAmount: 2000, // 20.00 CNY
|
||||
Currency: "CNY",
|
||||
Currency: consts.CurrencyCNY,
|
||||
})
|
||||
|
||||
form := &order_dto.OrderCreateForm{
|
||||
@@ -112,8 +112,87 @@ func (s *CouponTestSuite) Test_CouponFlow() {
|
||||
|
||||
// Verify Coupon Status
|
||||
ucReload, _ := models.UserCouponQuery.WithContext(ctx).Where(models.UserCouponQuery.ID.Eq(uc.ID)).First()
|
||||
So(ucReload.Status, ShouldEqual, "used")
|
||||
So(ucReload.Status, ShouldEqual, consts.UserCouponStatusUsed)
|
||||
So(ucReload.OrderID, ShouldEqual, oid)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
func (s *CouponTestSuite) Test_Receive() {
|
||||
Convey("Receive", s.T(), func() {
|
||||
ctx := s.T().Context()
|
||||
tenantID := int64(2)
|
||||
database.Truncate(
|
||||
ctx,
|
||||
s.DB,
|
||||
models.TableNameCoupon,
|
||||
models.TableNameUserCoupon,
|
||||
models.TableNameUser,
|
||||
)
|
||||
|
||||
user := &models.User{Username: "coupon_receive", Phone: "13800000002"}
|
||||
So(models.UserQuery.WithContext(ctx).Create(user), ShouldBeNil)
|
||||
|
||||
cp := &models.Coupon{
|
||||
TenantID: tenantID,
|
||||
Title: "Receive Test",
|
||||
Type: consts.CouponTypeFixAmount,
|
||||
Value: 300,
|
||||
MinOrderAmount: 0,
|
||||
TotalQuantity: 1,
|
||||
}
|
||||
So(models.CouponQuery.WithContext(ctx).Create(cp), ShouldBeNil)
|
||||
|
||||
item, err := Coupon.Receive(ctx, tenantID, user.ID, cp.ID)
|
||||
So(err, ShouldBeNil)
|
||||
So(item, ShouldNotBeNil)
|
||||
So(item.CouponID, ShouldEqual, cp.ID)
|
||||
|
||||
// second receive should return existing
|
||||
item2, err := Coupon.Receive(ctx, tenantID, user.ID, cp.ID)
|
||||
So(err, ShouldBeNil)
|
||||
So(item2.CouponID, ShouldEqual, cp.ID)
|
||||
})
|
||||
}
|
||||
|
||||
func (s *CouponTestSuite) Test_ListAvailable() {
|
||||
Convey("ListAvailable", s.T(), func() {
|
||||
ctx := s.T().Context()
|
||||
tenantID := int64(3)
|
||||
database.Truncate(
|
||||
ctx,
|
||||
s.DB,
|
||||
models.TableNameCoupon,
|
||||
models.TableNameUserCoupon,
|
||||
models.TableNameUser,
|
||||
)
|
||||
|
||||
user := &models.User{Username: "coupon_available", Phone: "13800000003"}
|
||||
So(models.UserQuery.WithContext(ctx).Create(user), ShouldBeNil)
|
||||
|
||||
cp := &models.Coupon{
|
||||
TenantID: tenantID,
|
||||
Title: "Available Test",
|
||||
Type: consts.CouponTypeFixAmount,
|
||||
Value: 500,
|
||||
MinOrderAmount: 1000,
|
||||
}
|
||||
So(models.CouponQuery.WithContext(ctx).Create(cp), ShouldBeNil)
|
||||
|
||||
uc := &models.UserCoupon{
|
||||
UserID: user.ID,
|
||||
CouponID: cp.ID,
|
||||
Status: consts.UserCouponStatusUnused,
|
||||
}
|
||||
So(models.UserCouponQuery.WithContext(ctx).Create(uc), ShouldBeNil)
|
||||
|
||||
list, err := Coupon.ListAvailable(ctx, tenantID, user.ID, 500)
|
||||
So(err, ShouldBeNil)
|
||||
So(len(list), ShouldEqual, 0)
|
||||
|
||||
list, err = Coupon.ListAvailable(ctx, tenantID, user.ID, 1200)
|
||||
So(err, ShouldBeNil)
|
||||
So(len(list), ShouldEqual, 1)
|
||||
So(list[0].CouponID, ShouldEqual, cp.ID)
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user