# 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"
|