This commit is contained in:
@@ -17,12 +17,12 @@ import (
|
||||
"github.com/google/uuid"
|
||||
"github.com/hibiken/asynq"
|
||||
"github.com/perfect-panel/server/internal/model/order"
|
||||
internaltypes "github.com/perfect-panel/server/internal/types"
|
||||
"github.com/perfect-panel/server/internal/model/redemption"
|
||||
"github.com/perfect-panel/server/internal/model/subscribe"
|
||||
"github.com/perfect-panel/server/internal/model/user"
|
||||
"github.com/perfect-panel/server/internal/svc"
|
||||
"github.com/perfect-panel/server/internal/types"
|
||||
internaltypes "github.com/perfect-panel/server/internal/types"
|
||||
"github.com/perfect-panel/server/pkg/tool"
|
||||
"github.com/perfect-panel/server/pkg/uuidx"
|
||||
queueTypes "github.com/perfect-panel/server/queue/types"
|
||||
@@ -223,27 +223,8 @@ func (l *ActivateOrderLogic) NewPurchase(ctx context.Context, orderInfo *order.O
|
||||
return err
|
||||
}
|
||||
|
||||
// check new user only restriction at activation to prevent concurrent bypass
|
||||
if orderInfo.Type == OrderTypeSubscribe && sub.Discount != "" {
|
||||
var dis []internaltypes.SubscribeDiscount
|
||||
if jsonErr := json.Unmarshal([]byte(sub.Discount), &dis); jsonErr == nil {
|
||||
newUserOnly := isNewUserOnlyForQuantity(dis, orderInfo.Quantity)
|
||||
if newUserOnly {
|
||||
if time.Since(userInfo.CreatedAt) > 24*time.Hour {
|
||||
return fmt.Errorf("new user only: user %d is not a new user", userInfo.Id)
|
||||
}
|
||||
var historyCount int64
|
||||
if e := l.svc.DB.Model(&order.Order{}).
|
||||
Where("user_id = ? AND subscribe_id = ? AND type = 1 AND status = ? AND order_no != ?",
|
||||
orderInfo.UserId, orderInfo.SubscribeId, OrderStatusFinished, orderInfo.OrderNo).
|
||||
Count(&historyCount).Error; e != nil {
|
||||
return fmt.Errorf("new user only: check history error: %w", e)
|
||||
}
|
||||
if historyCount >= 1 {
|
||||
return fmt.Errorf("new user only: user %d already activated subscribe %d", userInfo.Id, orderInfo.SubscribeId)
|
||||
}
|
||||
}
|
||||
}
|
||||
if err = validateNewUserOnlyEligibilityAtActivation(ctx, l.svc.DB, orderInfo, sub); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var userSub *user.Subscribe
|
||||
|
||||
@@ -0,0 +1,58 @@
|
||||
package orderLogic
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
commonLogic "github.com/perfect-panel/server/internal/logic/common"
|
||||
"github.com/perfect-panel/server/internal/model/order"
|
||||
"github.com/perfect-panel/server/internal/model/subscribe"
|
||||
internaltypes "github.com/perfect-panel/server/internal/types"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
func validateNewUserOnlyEligibilityAtActivation(
|
||||
ctx context.Context,
|
||||
db *gorm.DB,
|
||||
orderInfo *order.Order,
|
||||
sub *subscribe.Subscribe,
|
||||
) error {
|
||||
if orderInfo == nil || sub == nil || orderInfo.Type != OrderTypeSubscribe || sub.Discount == "" {
|
||||
return nil
|
||||
}
|
||||
|
||||
var discounts []internaltypes.SubscribeDiscount
|
||||
if err := json.Unmarshal([]byte(sub.Discount), &discounts); err != nil {
|
||||
return nil
|
||||
}
|
||||
if !isNewUserOnlyForQuantity(discounts, orderInfo.Quantity) {
|
||||
return nil
|
||||
}
|
||||
|
||||
eligibility, err := commonLogic.ResolveNewUserEligibility(ctx, db, orderInfo.UserId)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !eligibility.IsNewUserAt(time.Now()) {
|
||||
return fmt.Errorf("new user only: user %d is not a new user", orderInfo.UserId)
|
||||
}
|
||||
|
||||
historyCount, err := commonLogic.CountScopedSubscribePurchaseOrders(
|
||||
ctx,
|
||||
db,
|
||||
eligibility.ScopeUserIDs,
|
||||
orderInfo.SubscribeId,
|
||||
[]int64{OrderStatusFinished},
|
||||
orderInfo.OrderNo,
|
||||
)
|
||||
if err != nil {
|
||||
return fmt.Errorf("new user only: check history error: %w", err)
|
||||
}
|
||||
if historyCount >= 1 {
|
||||
return fmt.Errorf("new user only: user %d already activated subscribe %d", orderInfo.UserId, orderInfo.SubscribeId)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user