fix(order): reconcile subscriptions and grant device trials
All checks were successful
Build docker and publish / build (20.15.1) (push) Successful in 5m30s
All checks were successful
Build docker and publish / build (20.15.1) (push) Successful in 5m30s
This commit is contained in:
parent
79427c9f4c
commit
41e665d957
@ -12,6 +12,7 @@ import (
|
|||||||
"github.com/perfect-panel/server/internal/types"
|
"github.com/perfect-panel/server/internal/types"
|
||||||
"github.com/perfect-panel/server/pkg/jwt"
|
"github.com/perfect-panel/server/pkg/jwt"
|
||||||
"github.com/perfect-panel/server/pkg/logger"
|
"github.com/perfect-panel/server/pkg/logger"
|
||||||
|
"github.com/perfect-panel/server/pkg/tool"
|
||||||
"github.com/perfect-panel/server/pkg/uuidx"
|
"github.com/perfect-panel/server/pkg/uuidx"
|
||||||
"github.com/perfect-panel/server/pkg/xerr"
|
"github.com/perfect-panel/server/pkg/xerr"
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
@ -266,6 +267,33 @@ func (l *DeviceLoginLogic) registerUserAndDevice(req *types.DeviceLoginRequest)
|
|||||||
logger.Field("refer_code", userInfo.ReferCode),
|
logger.Field("refer_code", userInfo.ReferCode),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
if IsTrialConfigReady(l.svcCtx.Config.Register) {
|
||||||
|
trialSubscribe, trialErr := l.activeTrial(userInfo.Id)
|
||||||
|
if trialErr != nil {
|
||||||
|
l.Errorw("failed to activate trial subscription for device registration",
|
||||||
|
logger.Field("user_id", userInfo.Id),
|
||||||
|
logger.Field("identifier", req.Identifier),
|
||||||
|
logger.Field("error", trialErr.Error()),
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
if clearErr := l.svcCtx.UserModel.ClearSubscribeCache(l.ctx, trialSubscribe); clearErr != nil {
|
||||||
|
l.Errorw("ClearSubscribeCache failed",
|
||||||
|
logger.Field("error", clearErr.Error()),
|
||||||
|
logger.Field("userSubscribeId", trialSubscribe.Id),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
if clearErr := l.svcCtx.SubscribeModel.ClearCache(l.ctx, trialSubscribe.SubscribeId); clearErr != nil {
|
||||||
|
l.Errorw("ClearSubscribeCache failed",
|
||||||
|
logger.Field("error", clearErr.Error()),
|
||||||
|
logger.Field("subscribeId", trialSubscribe.SubscribeId),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
if clearErr := l.svcCtx.NodeModel.ClearServerAllCache(l.ctx); clearErr != nil {
|
||||||
|
l.Errorf("ClearServerAllCache error: %v", clearErr.Error())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Register log
|
// Register log
|
||||||
registerLog := log.Register{
|
registerLog := log.Register{
|
||||||
AuthMethod: "device",
|
AuthMethod: "device",
|
||||||
@ -291,3 +319,28 @@ func (l *DeviceLoginLogic) registerUserAndDevice(req *types.DeviceLoginRequest)
|
|||||||
|
|
||||||
return userInfo, nil
|
return userInfo, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (l *DeviceLoginLogic) activeTrial(uid int64) (*user.Subscribe, error) {
|
||||||
|
sub, err := l.svcCtx.SubscribeModel.FindOne(l.ctx, l.svcCtx.Config.Register.TrialSubscribe)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
startTime := time.Now()
|
||||||
|
userSub := &user.Subscribe{
|
||||||
|
UserId: uid,
|
||||||
|
OrderId: 0,
|
||||||
|
SubscribeId: sub.Id,
|
||||||
|
StartTime: startTime,
|
||||||
|
ExpireTime: tool.AddTime(l.svcCtx.Config.Register.TrialTimeUnit, l.svcCtx.Config.Register.TrialTime, startTime),
|
||||||
|
Traffic: sub.Traffic,
|
||||||
|
Download: 0,
|
||||||
|
Upload: 0,
|
||||||
|
Token: uuidx.NewUUID().String(),
|
||||||
|
UUID: uuidx.NewUUID().String(),
|
||||||
|
Status: 1,
|
||||||
|
}
|
||||||
|
if err = l.svcCtx.UserModel.InsertSubscribe(l.ctx, userSub); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return userSub, nil
|
||||||
|
}
|
||||||
|
|||||||
@ -54,12 +54,19 @@ func ShouldGrantTrialForEmail(register config.RegisterConfig, email string) bool
|
|||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// IsTrialConfigReady verifies that trial auto-grant has all required config.
|
||||||
|
func IsTrialConfigReady(register config.RegisterConfig) bool {
|
||||||
|
return register.EnableTrial &&
|
||||||
|
register.TrialSubscribe > 0 &&
|
||||||
|
register.TrialTime > 0 &&
|
||||||
|
strings.TrimSpace(register.TrialTimeUnit) != ""
|
||||||
|
}
|
||||||
|
|
||||||
// ShouldAutoGrantTrialOnPublicEmailFlows defines whether browser/email-originated
|
// ShouldAutoGrantTrialOnPublicEmailFlows defines whether browser/email-originated
|
||||||
// flows may auto-create a trial subscription. The current policy disables trial
|
// flows may auto-create a trial subscription. Email-specific abuse protection
|
||||||
// creation for email registration, email login auto-register, OAuth-with-email,
|
// is still handled by ShouldGrantTrialForEmail and NormalizedEmailHasTrial.
|
||||||
// and email binding/verification to avoid abuse through public email channels.
|
|
||||||
func ShouldAutoGrantTrialOnPublicEmailFlows(register config.RegisterConfig) bool {
|
func ShouldAutoGrantTrialOnPublicEmailFlows(register config.RegisterConfig) bool {
|
||||||
return false
|
return IsTrialConfigReady(register)
|
||||||
}
|
}
|
||||||
|
|
||||||
// IsDisposableAlias detects Gmail dot trick and + alias abuse.
|
// IsDisposableAlias detects Gmail dot trick and + alias abuse.
|
||||||
|
|||||||
@ -304,20 +304,17 @@ func (l *ActivateOrderLogic) reconcilePostOrderSubscriptions(ctx context.Context
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
maxExpire := survivor.ExpireTime
|
now := time.Now()
|
||||||
|
accumulatedExpire := now
|
||||||
for i := range ownerSubs {
|
for i := range ownerSubs {
|
||||||
item := ownerSubs[i]
|
item := ownerSubs[i]
|
||||||
if item.Id == survivor.Id {
|
if (item.Id == survivor.Id || orderMergeRemainingTimeStatus(item.Status)) && item.ExpireTime.After(now) {
|
||||||
if item.ExpireTime.After(maxExpire) {
|
accumulatedExpire = accumulatedExpire.Add(item.ExpireTime.Sub(now))
|
||||||
maxExpire = item.ExpireTime
|
|
||||||
}
|
|
||||||
continue
|
|
||||||
}
|
}
|
||||||
|
|
||||||
losers = append(losers, item)
|
if item.Id != survivor.Id {
|
||||||
mergedIDs = append(mergedIDs, item.Id)
|
losers = append(losers, item)
|
||||||
if item.ExpireTime.After(maxExpire) {
|
mergedIDs = append(mergedIDs, item.Id)
|
||||||
maxExpire = item.ExpireTime
|
|
||||||
}
|
}
|
||||||
if item.SubscribeId > 0 {
|
if item.SubscribeId > 0 {
|
||||||
subscribeIDsToClear[item.SubscribeId] = struct{}{}
|
subscribeIDsToClear[item.SubscribeId] = struct{}{}
|
||||||
@ -341,9 +338,9 @@ func (l *ActivateOrderLogic) reconcilePostOrderSubscriptions(ctx context.Context
|
|||||||
"status": 1,
|
"status": 1,
|
||||||
"finished_at": nil,
|
"finished_at": nil,
|
||||||
}
|
}
|
||||||
if maxExpire.After(survivor.ExpireTime) {
|
if accumulatedExpire.After(survivor.ExpireTime) {
|
||||||
survivor.ExpireTime = maxExpire
|
survivor.ExpireTime = accumulatedExpire
|
||||||
updateFields["expire_time"] = maxExpire
|
updateFields["expire_time"] = accumulatedExpire
|
||||||
}
|
}
|
||||||
if identitySource != nil {
|
if identitySource != nil {
|
||||||
if identitySource.Token != "" {
|
if identitySource.Token != "" {
|
||||||
@ -441,6 +438,15 @@ func shouldReconcilePostOrderSubscriptions(orderInfo *order.Order) bool {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func orderMergeRemainingTimeStatus(status uint8) bool {
|
||||||
|
switch status {
|
||||||
|
case 0, 1, 2:
|
||||||
|
return true
|
||||||
|
default:
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func pickSubscriptionIdentitySource(candidates []user.Subscribe) *user.Subscribe {
|
func pickSubscriptionIdentitySource(candidates []user.Subscribe) *user.Subscribe {
|
||||||
if len(candidates) == 0 {
|
if len(candidates) == 0 {
|
||||||
return nil
|
return nil
|
||||||
@ -1434,6 +1440,7 @@ func (l *ActivateOrderLogic) updateSubscriptionWithIAPExpire(ctx context.Context
|
|||||||
userSub.FinishedAt = nil
|
userSub.FinishedAt = nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
userSub.OrderId = orderInfo.Id
|
||||||
userSub.ExpireTime = newExpire
|
userSub.ExpireTime = newExpire
|
||||||
userSub.Status = 1
|
userSub.Status = 1
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user