fix(tenant): 收紧双站运营提号与录单边界
This commit is contained in:
@@ -98,33 +98,29 @@ func (r *UserRepository) FindOrCreateByPhone(ctx context.Context, phone string,
|
||||
now := time.Now()
|
||||
code := sanitizeTrackCode(trackCode)
|
||||
tenantID := tenancy.IDFromContext(ctx)
|
||||
if tenantID > 0 {
|
||||
var existing model.User
|
||||
query := r.db.WithContext(ctx).
|
||||
Where("tenant_id = ? AND phone = ? AND distribution_site_id = 0", tenantID, phone)
|
||||
if err := query.First(&existing).Error; err == nil {
|
||||
_ = r.db.WithContext(ctx).Model(&existing).Updates(map[string]any{
|
||||
"last_login_at": now,
|
||||
"updated_at": now,
|
||||
}).Error
|
||||
return &existing, nil
|
||||
} else if !errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return nil, err
|
||||
}
|
||||
var legacyCount int64
|
||||
if err := r.db.WithContext(ctx).Model(&model.User{}).
|
||||
Where("tenant_id = ? AND phone = ? AND distribution_site_id > 0", tenantID, phone).
|
||||
Count(&legacyCount).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if legacyCount > 0 {
|
||||
return nil, ErrLegacyDistributionUser
|
||||
}
|
||||
}
|
||||
var siteID uint64
|
||||
if len(siteCode) > 0 {
|
||||
siteID = r.resolveSiteID(ctx, siteCode[0])
|
||||
}
|
||||
if tenantID > 0 {
|
||||
siteID = 0
|
||||
}
|
||||
var existing model.User
|
||||
query := r.db.WithContext(ctx).Where("phone = ?", phone)
|
||||
if siteID > 0 {
|
||||
query = query.Where("distribution_site_id = ?", siteID)
|
||||
} else {
|
||||
query = query.Where("tenant_id = ? AND distribution_site_id = 0", tenantID)
|
||||
}
|
||||
if err := query.First(&existing).Error; err == nil {
|
||||
_ = r.db.WithContext(ctx).Model(&existing).Updates(map[string]any{
|
||||
"last_login_at": now,
|
||||
"updated_at": now,
|
||||
}).Error
|
||||
return &existing, nil
|
||||
} else if !errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return nil, err
|
||||
}
|
||||
user := model.User{
|
||||
TenantID: tenantID,
|
||||
Phone: phone,
|
||||
@@ -140,11 +136,11 @@ func (r *UserRepository) FindOrCreateByPhone(ctx context.Context, phone string,
|
||||
|
||||
onConflict := clause.OnConflict{
|
||||
DoUpdates: clause.AssignmentColumns([]string{"last_login_at", "updated_at"}),
|
||||
}
|
||||
if tenantID > 0 {
|
||||
onConflict.Columns = []clause.Column{{Name: "tenant_id"}, {Name: "phone"}}
|
||||
} else {
|
||||
onConflict.Columns = []clause.Column{{Name: "phone"}}
|
||||
Columns: []clause.Column{
|
||||
{Name: "tenant_id"},
|
||||
{Name: "distribution_site_id"},
|
||||
{Name: "phone"},
|
||||
},
|
||||
}
|
||||
err := r.db.WithContext(ctx).Clauses(onConflict).Create(&user).Error
|
||||
if err != nil {
|
||||
@@ -152,8 +148,10 @@ func (r *UserRepository) FindOrCreateByPhone(ctx context.Context, phone string,
|
||||
}
|
||||
|
||||
var found model.User
|
||||
query := r.db.WithContext(ctx).Where("phone = ?", phone)
|
||||
if tenantID > 0 {
|
||||
query = r.db.WithContext(ctx).Where("phone = ?", phone)
|
||||
if siteID > 0 {
|
||||
query = query.Where("distribution_site_id = ?", siteID)
|
||||
} else {
|
||||
query = query.Where("tenant_id = ? AND distribution_site_id = 0", tenantID)
|
||||
}
|
||||
if err := query.First(&found).Error; err != nil {
|
||||
@@ -162,24 +160,23 @@ func (r *UserRepository) FindOrCreateByPhone(ctx context.Context, phone string,
|
||||
return &found, nil
|
||||
}
|
||||
|
||||
func (r *UserRepository) FindByPhone(ctx context.Context, phone string) (*model.User, error) {
|
||||
func (r *UserRepository) FindByPhone(ctx context.Context, phone string, siteCode ...string) (*model.User, error) {
|
||||
tenantID := tenancy.IDFromContext(ctx)
|
||||
var siteID uint64
|
||||
if len(siteCode) > 0 {
|
||||
siteID = r.resolveSiteID(ctx, siteCode[0])
|
||||
}
|
||||
if tenantID > 0 {
|
||||
siteID = 0
|
||||
}
|
||||
var user model.User
|
||||
query := r.db.WithContext(ctx).Where("phone = ?", phone)
|
||||
if tenantID := tenancy.IDFromContext(ctx); tenantID > 0 {
|
||||
if siteID > 0 {
|
||||
query = query.Where("distribution_site_id = ?", siteID)
|
||||
} else {
|
||||
query = query.Where("tenant_id = ? AND distribution_site_id = 0", tenantID)
|
||||
}
|
||||
if err := query.First(&user).Error; err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) && tenancy.IDFromContext(ctx) > 0 {
|
||||
var legacyCount int64
|
||||
if countErr := r.db.WithContext(ctx).Model(&model.User{}).
|
||||
Where("tenant_id = ? AND phone = ? AND distribution_site_id > 0", tenancy.IDFromContext(ctx), phone).
|
||||
Count(&legacyCount).Error; countErr != nil {
|
||||
return nil, countErr
|
||||
}
|
||||
if legacyCount > 0 {
|
||||
return nil, ErrLegacyDistributionUser
|
||||
}
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
return &user, nil
|
||||
@@ -209,21 +206,13 @@ func (r *UserRepository) RegisterWithPassword(ctx context.Context, phone string,
|
||||
now := time.Now()
|
||||
code := sanitizeTrackCode(trackCode)
|
||||
tenantID := tenancy.IDFromContext(ctx)
|
||||
if tenantID > 0 {
|
||||
var legacyCount int64
|
||||
if err := r.db.WithContext(ctx).Model(&model.User{}).
|
||||
Where("tenant_id = ? AND phone = ? AND distribution_site_id > 0", tenantID, phone).
|
||||
Count(&legacyCount).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if legacyCount > 0 {
|
||||
return nil, ErrLegacyDistributionUser
|
||||
}
|
||||
}
|
||||
var siteID uint64
|
||||
if len(siteCode) > 0 {
|
||||
siteID = r.resolveSiteID(ctx, siteCode[0])
|
||||
}
|
||||
if tenantID > 0 {
|
||||
siteID = 0
|
||||
}
|
||||
user := model.User{
|
||||
TenantID: tenantID,
|
||||
Phone: phone,
|
||||
|
||||
Reference in New Issue
Block a user