93 lines
2.7 KiB
TypeScript
93 lines
2.7 KiB
TypeScript
import "dotenv/config";
|
|
import { AppDataSource } from "../src/database/data-source";
|
|
import { KeycloakAttributeService } from "../src/services/KeycloakAttributeService";
|
|
import * as keycloak from "../src/keycloak/index";
|
|
|
|
/**
|
|
* Main function
|
|
*/
|
|
async function main() {
|
|
const args = process.argv.slice(2);
|
|
const dryRun = args.includes("--dry-run");
|
|
const limitArg = args.find((arg) => arg.startsWith("--limit="));
|
|
const limit = limitArg ? parseInt(limitArg.split("=")[1], 10) : undefined;
|
|
const concurrencyArg = args.find((arg) => arg.startsWith("--concurrency="));
|
|
const concurrency = concurrencyArg ? parseInt(concurrencyArg.split("=")[1], 10) : undefined;
|
|
|
|
console.log("=".repeat(60));
|
|
console.log("Sync All Attributes to Keycloak Script");
|
|
console.log("=".repeat(60));
|
|
console.log(`Mode: ${dryRun ? "DRY-RUN (no changes)" : "LIVE"}`);
|
|
if (limit !== undefined) {
|
|
console.log(`Limit: ${limit} profiles per table (for testing)`);
|
|
}
|
|
if (concurrency !== undefined) {
|
|
console.log(`Concurrency: ${concurrency}`);
|
|
}
|
|
console.log("");
|
|
|
|
console.log("Attributes to sync:");
|
|
console.log(" - profileId");
|
|
console.log(" - orgRootDnaId");
|
|
console.log(" - orgChild1DnaId");
|
|
console.log(" - orgChild2DnaId");
|
|
console.log(" - orgChild3DnaId");
|
|
console.log(" - orgChild4DnaId");
|
|
console.log(" - empType");
|
|
console.log(" - prefix");
|
|
console.log("");
|
|
|
|
// Initialize database
|
|
try {
|
|
await AppDataSource.initialize();
|
|
console.log("Database connected");
|
|
} catch (error) {
|
|
console.error("Failed to connect to database:", error);
|
|
process.exit(1);
|
|
}
|
|
|
|
// Validate Keycloak connection
|
|
try {
|
|
await keycloak.getToken();
|
|
console.log("Keycloak connected");
|
|
} catch (error) {
|
|
console.error("Failed to connect to Keycloak:", error);
|
|
await AppDataSource.destroy();
|
|
process.exit(1);
|
|
}
|
|
|
|
console.log("");
|
|
|
|
// Run the sync operation
|
|
const service = new KeycloakAttributeService();
|
|
console.log("Syncing attributes for all profiles with Keycloak IDs...");
|
|
console.log("");
|
|
|
|
const result = await service.batchSyncUsers({ limit, concurrency });
|
|
|
|
// Summary
|
|
console.log("");
|
|
console.log("=".repeat(60));
|
|
console.log("Summary:");
|
|
console.log(` Total profiles: ${result.total}`);
|
|
console.log(` Success: ${result.success}`);
|
|
console.log(` Failed: ${result.failed}`);
|
|
console.log("=".repeat(60));
|
|
|
|
if (result.failed > 0) {
|
|
console.log("");
|
|
console.log("Failed Details:");
|
|
for (const detail of result.details.filter(
|
|
(d) => d.status === "failed" || d.status === "error",
|
|
)) {
|
|
console.log(
|
|
` ${detail.profileId} (${detail.keycloakUserId}): ${detail.error || "Sync failed"}`,
|
|
);
|
|
}
|
|
}
|
|
|
|
// Cleanup
|
|
await AppDataSource.destroy();
|
|
}
|
|
|
|
main().catch(console.error);
|