All checks were successful
Build docker and publish / build (20.15.1) (push) Successful in 7m36s
59 lines
1.5 KiB
Go
59 lines
1.5 KiB
Go
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
|
|
}
|