add: 新增ci diamanté
This commit is contained in:
@@ -50,15 +50,39 @@ func (l *PreCreateOrderLogic) PreCreateOrder(req *types.PurchaseOrderRequest) (r
|
||||
req.Quantity = 1
|
||||
}
|
||||
|
||||
targetSubscribeID := req.SubscribeId
|
||||
isSingleModeRenewal := false
|
||||
if l.svcCtx.Config.Subscribe.SingleModel {
|
||||
anchorSub, anchorErr := l.svcCtx.UserModel.FindSingleModeAnchorSubscribe(l.ctx, u.Id)
|
||||
switch {
|
||||
case anchorErr == nil && anchorSub != nil:
|
||||
targetSubscribeID = anchorSub.SubscribeId
|
||||
isSingleModeRenewal = true
|
||||
if req.SubscribeId != targetSubscribeID {
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SingleSubscribePlanMismatch), "single subscribe mode plan mismatch")
|
||||
}
|
||||
l.Infow("[PreCreateOrder] single mode purchase routed to renewal preview",
|
||||
logger.Field("mode", "single"),
|
||||
logger.Field("route", "purchase_to_renewal"),
|
||||
logger.Field("anchor_user_subscribe_id", anchorSub.Id),
|
||||
logger.Field("user_id", u.Id),
|
||||
)
|
||||
case errors.Is(anchorErr, gorm.ErrRecordNotFound):
|
||||
case anchorErr != nil:
|
||||
l.Errorw("[PreCreateOrder] Database query error", logger.Field("error", anchorErr.Error()), logger.Field("user_id", u.Id))
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DatabaseQueryError), "find single mode anchor subscribe error: %v", anchorErr.Error())
|
||||
}
|
||||
}
|
||||
|
||||
// find subscribe plan
|
||||
sub, err := l.svcCtx.SubscribeModel.FindOne(l.ctx, req.SubscribeId)
|
||||
sub, err := l.svcCtx.SubscribeModel.FindOne(l.ctx, targetSubscribeID)
|
||||
if err != nil {
|
||||
l.Errorw("[PreCreateOrder] Database query error", logger.Field("error", err.Error()), logger.Field("subscribe_id", req.SubscribeId))
|
||||
l.Errorw("[PreCreateOrder] Database query error", logger.Field("error", err.Error()), logger.Field("subscribe_id", targetSubscribeID))
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DatabaseQueryError), "find subscribe error: %v", err.Error())
|
||||
}
|
||||
|
||||
// check subscribe plan quota limit
|
||||
if sub.Quota > 0 {
|
||||
// check subscribe plan quota limit for new purchase flow only
|
||||
if !isSingleModeRenewal && sub.Quota > 0 {
|
||||
userSub, err := l.svcCtx.UserModel.QueryUserSubscribe(l.ctx, u.Id)
|
||||
if err != nil {
|
||||
l.Errorw("[PreCreateOrder] Database query error", logger.Field("error", err.Error()), logger.Field("user_id", u.Id))
|
||||
@@ -66,7 +90,7 @@ func (l *PreCreateOrderLogic) PreCreateOrder(req *types.PurchaseOrderRequest) (r
|
||||
}
|
||||
var count int64
|
||||
for _, v := range userSub {
|
||||
if v.SubscribeId == req.SubscribeId {
|
||||
if v.SubscribeId == targetSubscribeID {
|
||||
count++
|
||||
}
|
||||
}
|
||||
@@ -112,7 +136,7 @@ func (l *PreCreateOrderLogic) PreCreateOrder(req *types.PurchaseOrderRequest) (r
|
||||
}
|
||||
|
||||
couponSub := tool.StringToInt64Slice(couponInfo.Subscribe)
|
||||
if len(couponSub) > 0 && !tool.Contains(couponSub, req.SubscribeId) {
|
||||
if len(couponSub) > 0 && !tool.Contains(couponSub, targetSubscribeID) {
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.CouponNotApplicable), "coupon not match")
|
||||
}
|
||||
couponAmount = calculateCoupon(amount, couponInfo)
|
||||
|
||||
@@ -65,34 +65,44 @@ func (l *PurchaseLogic) Purchase(req *types.PurchaseOrderRequest) (resp *types.P
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.InvalidParams), "quantity exceeds maximum limit of %d", MaxQuantity)
|
||||
}
|
||||
|
||||
// find user subscription
|
||||
userSub, err := l.svcCtx.UserModel.QueryUserSubscribe(l.ctx, u.Id)
|
||||
if err != nil {
|
||||
l.Errorw("[Purchase] Database query error", logger.Field("error", err.Error()), logger.Field("user_id", u.Id))
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DatabaseQueryError), "find user subscription error: %v", err.Error())
|
||||
}
|
||||
targetSubscribeID := req.SubscribeId
|
||||
orderType := uint8(1)
|
||||
parentOrderID := int64(0)
|
||||
subscribeToken := ""
|
||||
anchorUserSubscribeID := int64(0)
|
||||
isSingleModeRenewal := false
|
||||
if l.svcCtx.Config.Subscribe.SingleModel {
|
||||
if len(userSub) > 0 {
|
||||
// Only block if user has a paid subscription (OrderId > 0)
|
||||
// Allow purchase if user only has gift subscriptions
|
||||
hasPaidSubscription := false
|
||||
for _, s := range userSub {
|
||||
if s.OrderId > 0 {
|
||||
hasPaidSubscription = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if hasPaidSubscription {
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.UserSubscribeExist), "user has subscription")
|
||||
anchorSub, anchorErr := l.svcCtx.UserModel.FindSingleModeAnchorSubscribe(l.ctx, u.Id)
|
||||
switch {
|
||||
case anchorErr == nil && anchorSub != nil:
|
||||
if req.SubscribeId != anchorSub.SubscribeId {
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SingleSubscribePlanMismatch), "single subscribe mode plan mismatch")
|
||||
}
|
||||
targetSubscribeID = anchorSub.SubscribeId
|
||||
orderType = 2
|
||||
parentOrderID = anchorSub.OrderId
|
||||
subscribeToken = anchorSub.Token
|
||||
anchorUserSubscribeID = anchorSub.Id
|
||||
isSingleModeRenewal = true
|
||||
l.Infow("[Purchase] single mode purchase routed to renewal",
|
||||
logger.Field("mode", "single"),
|
||||
logger.Field("route", "purchase_to_renewal"),
|
||||
logger.Field("anchor_user_subscribe_id", anchorSub.Id),
|
||||
logger.Field("order_no", "pending"),
|
||||
logger.Field("user_id", u.Id),
|
||||
)
|
||||
case errors.Is(anchorErr, gorm.ErrRecordNotFound):
|
||||
case anchorErr != nil:
|
||||
l.Errorw("[Purchase] Database query error", logger.Field("error", anchorErr.Error()), logger.Field("user_id", u.Id))
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DatabaseQueryError), "find single mode anchor subscribe error: %v", anchorErr.Error())
|
||||
}
|
||||
}
|
||||
|
||||
// find subscribe plan
|
||||
sub, err := l.svcCtx.SubscribeModel.FindOne(l.ctx, req.SubscribeId)
|
||||
sub, err := l.svcCtx.SubscribeModel.FindOne(l.ctx, targetSubscribeID)
|
||||
|
||||
if err != nil {
|
||||
l.Errorw("[Purchase] Database query error", logger.Field("error", err.Error()), logger.Field("subscribe_id", req.SubscribeId))
|
||||
l.Errorw("[Purchase] Database query error", logger.Field("error", err.Error()), logger.Field("subscribe_id", targetSubscribeID))
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DatabaseQueryError), "find subscribe error: %v", err.Error())
|
||||
}
|
||||
// check subscribe plan status
|
||||
@@ -100,8 +110,8 @@ func (l *PurchaseLogic) Purchase(req *types.PurchaseOrderRequest) (resp *types.P
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.ERROR), "subscribe not sell")
|
||||
}
|
||||
|
||||
// check subscribe plan inventory
|
||||
if sub.Inventory == 0 {
|
||||
// check subscribe plan inventory for new purchase flow only
|
||||
if orderType == 1 && sub.Inventory == 0 {
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SubscribeOutOfStock), "subscribe out of stock")
|
||||
}
|
||||
|
||||
@@ -140,7 +150,7 @@ func (l *PurchaseLogic) Purchase(req *types.PurchaseOrderRequest) (resp *types.P
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.CouponInsufficientUsage), "coupon used")
|
||||
}
|
||||
couponSub := tool.StringToInt64Slice(couponInfo.Subscribe)
|
||||
if len(couponSub) > 0 && !tool.Contains(couponSub, req.SubscribeId) {
|
||||
if len(couponSub) > 0 && !tool.Contains(couponSub, targetSubscribeID) {
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.CouponNotApplicable), "coupon not match")
|
||||
}
|
||||
var count int64
|
||||
@@ -192,16 +202,20 @@ func (l *PurchaseLogic) Purchase(req *types.PurchaseOrderRequest) (resp *types.P
|
||||
}
|
||||
}
|
||||
// query user is new purchase or renewal
|
||||
isNew, err := l.svcCtx.OrderModel.IsUserEligibleForNewOrder(l.ctx, u.Id)
|
||||
if err != nil {
|
||||
l.Errorw("[Purchase] Database query error", logger.Field("error", err.Error()), logger.Field("user_id", u.Id))
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DatabaseQueryError), "find user order error: %v", err.Error())
|
||||
isNew := false
|
||||
if orderType == 1 {
|
||||
isNew, err = l.svcCtx.OrderModel.IsUserEligibleForNewOrder(l.ctx, u.Id)
|
||||
if err != nil {
|
||||
l.Errorw("[Purchase] Database query error", logger.Field("error", err.Error()), logger.Field("user_id", u.Id))
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DatabaseQueryError), "find user order error: %v", err.Error())
|
||||
}
|
||||
}
|
||||
// create order
|
||||
orderInfo := &order.Order{
|
||||
UserId: u.Id,
|
||||
ParentId: parentOrderID,
|
||||
OrderNo: tool.GenerateTradeNo(),
|
||||
Type: 1,
|
||||
Type: orderType,
|
||||
Quantity: req.Quantity,
|
||||
Price: price,
|
||||
Amount: amount,
|
||||
@@ -214,12 +228,23 @@ func (l *PurchaseLogic) Purchase(req *types.PurchaseOrderRequest) (resp *types.P
|
||||
FeeAmount: feeAmount,
|
||||
Status: 1,
|
||||
IsNew: isNew,
|
||||
SubscribeId: req.SubscribeId,
|
||||
SubscribeId: targetSubscribeID,
|
||||
SubscribeToken: subscribeToken,
|
||||
}
|
||||
if isSingleModeRenewal {
|
||||
l.Infow("[Purchase] single mode purchase order created as renewal",
|
||||
logger.Field("mode", "single"),
|
||||
logger.Field("route", "purchase_to_renewal"),
|
||||
logger.Field("anchor_user_subscribe_id", anchorUserSubscribeID),
|
||||
logger.Field("order_no", orderInfo.OrderNo),
|
||||
logger.Field("parent_id", orderInfo.ParentId),
|
||||
logger.Field("user_id", u.Id),
|
||||
)
|
||||
}
|
||||
// Database transaction
|
||||
err = l.svcCtx.DB.Transaction(func(db *gorm.DB) error {
|
||||
// check subscribe plan quota limit inside transaction to prevent race condition
|
||||
if sub.Quota > 0 {
|
||||
if orderInfo.Type == 1 && sub.Quota > 0 {
|
||||
var currentUserSub []user.Subscribe
|
||||
if e := db.Model(&user.Subscribe{}).Where("user_id = ?", u.Id).Find(¤tUserSub).Error; e != nil {
|
||||
l.Errorw("[Purchase] Database query error", logger.Field("error", e.Error()), logger.Field("user_id", u.Id))
|
||||
@@ -227,7 +252,7 @@ func (l *PurchaseLogic) Purchase(req *types.PurchaseOrderRequest) (resp *types.P
|
||||
}
|
||||
var count int64
|
||||
for _, v := range currentUserSub {
|
||||
if v.SubscribeId == req.SubscribeId {
|
||||
if v.SubscribeId == targetSubscribeID {
|
||||
count++
|
||||
}
|
||||
}
|
||||
@@ -270,7 +295,7 @@ func (l *PurchaseLogic) Purchase(req *types.PurchaseOrderRequest) (resp *types.P
|
||||
}
|
||||
}
|
||||
|
||||
if sub.Inventory != -1 {
|
||||
if orderInfo.Type == 1 && sub.Inventory != -1 {
|
||||
// decrease subscribe plan stock
|
||||
sub.Inventory -= 1
|
||||
// update subscribe plan stock
|
||||
|
||||
Reference in New Issue
Block a user