lrj
16 小时以前 7ad9c3c93f0cc103347ae2e2429e0122fb512e24
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# Fix employee role codes
$API_URL = "http://localhost:8080/api/graphql"
$TOKEN = "eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiIyIiwicGhvbmUiOiIxMzk4MTk3MDgxNiIsImlhdCI6MTc1OTMyMzk3MSwiZXhwIjoxNzU5NDEwMzcxfQ.z8n5QuCpALluVJD9JShNsjQSaPqm91HcKV-5PB3jLN4"
 
$headers = @{
    "Content-Type" = "application/json"
    "Authorization" = "Bearer $TOKEN"
}
 
Write-Host "Getting employees..."
$employeesQuery = '{"query":"query { employees { id name phone roleId description } }"}'
$employeesResponse = Invoke-RestMethod -Uri $API_URL -Method POST -Headers $headers -Body $employeesQuery
$employees = $employeesResponse.data.employees
 
Write-Host "Found $($employees.Count) employees"
 
Write-Host "Getting valid roles..."
$rolesQuery = '{"query":"query { activeRoles { id code name description } }"}'
$rolesResponse = Invoke-RestMethod -Uri $API_URL -Method POST -Headers $headers -Body $rolesQuery
$validRoles = $rolesResponse.data.activeRoles
 
$validRoleCodes = $validRoles | ForEach-Object { $_.code }
$invalidEmployees = $employees | Where-Object { $_.roleId -notin $validRoleCodes }
 
if ($invalidEmployees.Count -eq 0) {
    Write-Host "All employee role codes are valid!"
    exit 0
}
 
Write-Host "Found $($invalidEmployees.Count) employees with invalid role codes"
 
$roleMapping = @{
    "MANAGER" = "SUPER_ADMIN"
    "EMPLOYEE" = "AUDITOR"  
    "ADMIN" = "SUPER_ADMIN"
}
 
$fixedCount = 0
$errorCount = 0
 
foreach ($emp in $invalidEmployees) {
    $newRoleId = $roleMapping[$emp.roleId]
    if (-not $newRoleId) {
        $newRoleId = "AUDITOR"
    }
    
    Write-Host "Fixing employee $($emp.name): $($emp.roleId) -> $newRoleId"
    
    $mutationBody = @{
        query = 'mutation SaveEmployee($input: EmployeeInput!) { saveEmployee(input: $input) { id name roleId } }'
        variables = @{
            input = @{
                id = [int]$emp.id
                name = $emp.name
                phone = $emp.phone
                roleId = $newRoleId
                description = $emp.description
            }
        }
    }
    
    $mutation = $mutationBody | ConvertTo-Json -Depth 10
    
    try {
        $result = Invoke-RestMethod -Uri $API_URL -Method POST -Headers $headers -Body $mutation
        if ($result.errors) {
            Write-Host "  Error: $($result.errors[0].message)" -ForegroundColor Red
            $errorCount++
        } else {
            Write-Host "  Success!" -ForegroundColor Green
            $fixedCount++
        }
    } catch {
        Write-Host "  Exception: $($_.Exception.Message)" -ForegroundColor Red
        $errorCount++
    }
}
 
Write-Host "Fix completed!"
Write-Host "Successfully fixed: $fixedCount employees"
Write-Host "Failed to fix: $errorCount employees"