This commit is contained in:
@@ -8,14 +8,12 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/perfect-panel/server/internal/config"
|
||||
"github.com/perfect-panel/server/internal/logic/auth"
|
||||
"github.com/perfect-panel/server/internal/model/user"
|
||||
"github.com/perfect-panel/server/internal/svc"
|
||||
"github.com/perfect-panel/server/internal/types"
|
||||
"github.com/perfect-panel/server/pkg/constant"
|
||||
"github.com/perfect-panel/server/pkg/jwt"
|
||||
"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/xerr"
|
||||
"github.com/pkg/errors"
|
||||
@@ -129,8 +127,7 @@ func (l *BindEmailWithVerificationLogic) BindEmailWithVerification(req *types.Bi
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// Grant trial subscription if email domain is whitelisted
|
||||
l.tryGrantTrialOnEmailBind(emailUser.Id, req.Email)
|
||||
tryGrantTrialOnEmailBind(l.ctx, l.svcCtx, l.Logger, emailUser.Id, req.Email)
|
||||
return &types.BindEmailWithVerificationResponse{
|
||||
Success: true,
|
||||
Message: "email user created and joined family",
|
||||
@@ -158,8 +155,7 @@ func (l *BindEmailWithVerificationLogic) BindEmailWithVerification(req *types.Bi
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Grant trial subscription if email domain is whitelisted
|
||||
l.tryGrantTrialOnEmailBind(existingMethod.UserId, req.Email)
|
||||
tryGrantTrialOnEmailBind(l.ctx, l.svcCtx, l.Logger, existingMethod.UserId, req.Email)
|
||||
|
||||
return &types.BindEmailWithVerificationResponse{
|
||||
Success: true,
|
||||
@@ -207,74 +203,3 @@ func (l *BindEmailWithVerificationLogic) refreshBindSessionToken(userId int64) (
|
||||
|
||||
return token, nil
|
||||
}
|
||||
|
||||
// tryGrantTrialOnEmailBind grants trial subscription to the email user (family owner)
|
||||
// if email domain is in the configured whitelist (or if whitelist is empty, no trial is granted).
|
||||
func (l *BindEmailWithVerificationLogic) tryGrantTrialOnEmailBind(ownerUserId int64, email string) {
|
||||
rc := l.svcCtx.Config.Register
|
||||
if !rc.EnableTrial || rc.TrialEmailDomainWhitelist == "" {
|
||||
return
|
||||
}
|
||||
if !auth.IsEmailDomainWhitelisted(email, rc.TrialEmailDomainWhitelist) {
|
||||
l.Infow("email domain not in trial whitelist, skip",
|
||||
logger.Field("email", email),
|
||||
logger.Field("owner_user_id", ownerUserId),
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
// Anti-duplicate: check if owner already has trial subscription
|
||||
var count int64
|
||||
if err := l.svcCtx.DB.WithContext(l.ctx).
|
||||
Model(&user.Subscribe{}).
|
||||
Where("user_id = ? AND subscribe_id = ?", ownerUserId, rc.TrialSubscribe).
|
||||
Count(&count).Error; err != nil {
|
||||
l.Errorw("failed to check existing trial", logger.Field("error", err.Error()))
|
||||
return
|
||||
}
|
||||
if count > 0 {
|
||||
l.Infow("trial already granted, skip",
|
||||
logger.Field("owner_user_id", ownerUserId),
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
sub, err := l.svcCtx.SubscribeModel.FindOne(l.ctx, rc.TrialSubscribe)
|
||||
if err != nil {
|
||||
l.Errorw("failed to find trial subscribe template", logger.Field("error", err.Error()))
|
||||
return
|
||||
}
|
||||
|
||||
userSub := &user.Subscribe{
|
||||
UserId: ownerUserId,
|
||||
OrderId: 0,
|
||||
SubscribeId: sub.Id,
|
||||
StartTime: time.Now(),
|
||||
ExpireTime: tool.AddTime(rc.TrialTimeUnit, rc.TrialTime, time.Now()),
|
||||
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 {
|
||||
l.Errorw("failed to insert trial subscribe",
|
||||
logger.Field("error", err.Error()),
|
||||
logger.Field("owner_user_id", ownerUserId),
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
// InsertSubscribe auto-clears user subscribe cache via execSubscribeMutation.
|
||||
// Clear server cache so nodes pick up the new subscription.
|
||||
if err = l.svcCtx.NodeModel.ClearServerAllCache(l.ctx); err != nil {
|
||||
l.Errorw("ClearServerAllCache error", logger.Field("error", err.Error()))
|
||||
}
|
||||
|
||||
l.Infow("trial granted on email bind",
|
||||
logger.Field("owner_user_id", ownerUserId),
|
||||
logger.Field("email", email),
|
||||
logger.Field("subscribe_id", sub.Id),
|
||||
)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user