This commit is contained in:
@@ -0,0 +1,99 @@
|
||||
package user
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/perfect-panel/server/internal/model/user"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
func mapFamilyStatus(status uint8) string {
|
||||
if status == user.FamilyStatusActive {
|
||||
return "active"
|
||||
}
|
||||
return "disabled"
|
||||
}
|
||||
|
||||
func mapFamilyRoleName(role uint8) string {
|
||||
switch role {
|
||||
case user.FamilyRoleOwner:
|
||||
return "owner"
|
||||
case user.FamilyRoleMember:
|
||||
return "member"
|
||||
default:
|
||||
return fmt.Sprintf("role_%d", role)
|
||||
}
|
||||
}
|
||||
|
||||
func mapFamilyMemberStatusName(status uint8) string {
|
||||
switch status {
|
||||
case user.FamilyMemberActive:
|
||||
return "active"
|
||||
case user.FamilyMemberLeft:
|
||||
return "left"
|
||||
case user.FamilyMemberRemoved:
|
||||
return "removed"
|
||||
default:
|
||||
return fmt.Sprintf("status_%d", status)
|
||||
}
|
||||
}
|
||||
|
||||
func normalizeFamilyStatusInput(status string) (uint8, bool) {
|
||||
switch strings.ToLower(strings.TrimSpace(status)) {
|
||||
case "", "all":
|
||||
return 0, false
|
||||
case "active", "1":
|
||||
return user.FamilyStatusActive, true
|
||||
case "disabled", "0":
|
||||
return 0, true
|
||||
default:
|
||||
return 0, false
|
||||
}
|
||||
}
|
||||
|
||||
func findUserIdentifiers(ctx context.Context, db *gorm.DB, userIDs []int64) (map[int64]string, error) {
|
||||
identifierMap := make(map[int64]string)
|
||||
if len(userIDs) == 0 {
|
||||
return identifierMap, nil
|
||||
}
|
||||
|
||||
type authRow struct {
|
||||
UserId int64
|
||||
AuthType string
|
||||
AuthIdentifier string
|
||||
}
|
||||
|
||||
var rows []authRow
|
||||
err := db.WithContext(ctx).
|
||||
Table("user_auth_methods").
|
||||
Select("user_id, auth_type, auth_identifier").
|
||||
Where("user_id IN ? AND deleted_at IS NULL", userIDs).
|
||||
Scan(&rows).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
priority := map[string]int{
|
||||
"email": 1,
|
||||
"mobile": 2,
|
||||
"telegram": 3,
|
||||
"device": 4,
|
||||
}
|
||||
selectedPriority := make(map[int64]int, len(userIDs))
|
||||
|
||||
for _, row := range rows {
|
||||
currentPriority := priority[row.AuthType]
|
||||
if currentPriority == 0 {
|
||||
currentPriority = 100
|
||||
}
|
||||
if previous, exists := selectedPriority[row.UserId]; exists && previous <= currentPriority {
|
||||
continue
|
||||
}
|
||||
selectedPriority[row.UserId] = currentPriority
|
||||
identifierMap[row.UserId] = row.AuthIdentifier
|
||||
}
|
||||
|
||||
return identifierMap, nil
|
||||
}
|
||||
Reference in New Issue
Block a user