add api sync-missing-emptype
All checks were successful
Build & Deploy on Dev / build (push) Successful in 1m4s
All checks were successful
Build & Deploy on Dev / build (push) Successful in 1m4s
This commit is contained in:
parent
b5fb2346ab
commit
2417c90dc2
2 changed files with 294 additions and 0 deletions
|
|
@ -442,6 +442,223 @@ export class KeycloakAttributeService {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if Keycloak user has empty/null empType attribute
|
||||
* @param keycloakUserId - Keycloak user ID
|
||||
* @returns Object with isEmpty flag and currentEmpType value
|
||||
*/
|
||||
async checkEmpTypeEmpty(keycloakUserId: string): Promise<{
|
||||
isEmpty: boolean;
|
||||
currentEmpType?: string;
|
||||
}> {
|
||||
try {
|
||||
const user = await getUser(keycloakUserId);
|
||||
|
||||
if (!user || !user.attributes) {
|
||||
return { isEmpty: true };
|
||||
}
|
||||
|
||||
const empType = user.attributes.empType?.[0];
|
||||
|
||||
return {
|
||||
isEmpty: !empType || empType.trim() === "",
|
||||
currentEmpType: empType || "",
|
||||
};
|
||||
} catch (error) {
|
||||
console.error(`[checkEmpTypeEmpty] Error for user ${keycloakUserId}:`, error);
|
||||
return { isEmpty: true }; // Assume empty on error
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sync profiles with missing empType for a specific month
|
||||
* @param options - Sync configuration
|
||||
* @returns Sync results summary
|
||||
*/
|
||||
async syncMissingEmpTypeByMonth(options: {
|
||||
month: string; // "YYYY-MM" format
|
||||
profileType?: "PROFILE" | "PROFILE_EMPLOYEE";
|
||||
dryRun?: boolean;
|
||||
concurrency?: number;
|
||||
rateLimit?: number;
|
||||
}): Promise<{
|
||||
month: string;
|
||||
profileType: string;
|
||||
totalProfiles: number;
|
||||
profilesChecked: number;
|
||||
missingEmpType: number;
|
||||
syncSuccess: number;
|
||||
syncFailed: number;
|
||||
skipped: number;
|
||||
executionTime: string;
|
||||
dryRun: boolean;
|
||||
}> {
|
||||
const startTime = Date.now();
|
||||
const {
|
||||
month,
|
||||
profileType = "PROFILE",
|
||||
dryRun = false,
|
||||
concurrency = 5,
|
||||
rateLimit = 10,
|
||||
} = options;
|
||||
|
||||
const result = {
|
||||
month,
|
||||
profileType,
|
||||
totalProfiles: 0,
|
||||
profilesChecked: 0,
|
||||
missingEmpType: 0,
|
||||
syncSuccess: 0,
|
||||
syncFailed: 0,
|
||||
skipped: 0,
|
||||
executionTime: "",
|
||||
dryRun,
|
||||
};
|
||||
|
||||
let rateLimiter: RateLimiter | null = null;
|
||||
|
||||
try {
|
||||
// Parse month (YYYY-MM) to date range
|
||||
const [year, monthNum] = month.split("-").map(Number);
|
||||
const startDate = new Date(Date.UTC(year, monthNum - 1, 1, 0, 0, 0));
|
||||
const endDate = new Date(Date.UTC(year, monthNum, 0, 23, 59, 59, 999));
|
||||
|
||||
console.log(
|
||||
`[syncMissingEmpTypeByMonth] Processing ${profileType} for ${month} (${startDate.toISOString()} to ${endDate.toISOString()})`,
|
||||
);
|
||||
|
||||
// Initialize rate limiter if rate limiting is enabled
|
||||
if (rateLimit && rateLimit > 0) {
|
||||
rateLimiter = new RateLimiter(rateLimit);
|
||||
console.log(`[syncMissingEmpTypeByMonth] Rate limiting enabled: ${rateLimit} requests/second`);
|
||||
}
|
||||
|
||||
// Select repository based on profile type
|
||||
const repo =
|
||||
profileType === "PROFILE" ? this.profileRepo : this.profileEmployeeRepo;
|
||||
|
||||
// Query profiles updated within the month
|
||||
const profiles = await repo
|
||||
.createQueryBuilder("p")
|
||||
.where("p.keycloak IS NOT NULL")
|
||||
.andWhere("p.keycloak != :empty", { empty: "" })
|
||||
.andWhere("p.lastUpdatedAt BETWEEN :start AND :end", {
|
||||
start: startDate,
|
||||
end: endDate,
|
||||
})
|
||||
.orderBy("p.lastUpdatedAt", "ASC")
|
||||
.getMany();
|
||||
|
||||
result.totalProfiles = profiles.length;
|
||||
console.log(`[syncMissingEmpTypeByMonth] Found ${profiles.length} profiles to check`);
|
||||
|
||||
if (profiles.length === 0) {
|
||||
result.executionTime = `${((Date.now() - startTime) / 1000).toFixed(2)}s`;
|
||||
return result;
|
||||
}
|
||||
|
||||
// Process profiles in parallel with concurrency limit
|
||||
for (let i = 0; i < profiles.length; i += concurrency) {
|
||||
const batch = profiles.slice(i, i + concurrency);
|
||||
|
||||
await Promise.all(
|
||||
batch.map(async (profile) => {
|
||||
// Apply rate limiting if enabled
|
||||
if (rateLimiter) {
|
||||
await rateLimiter.throttle();
|
||||
}
|
||||
|
||||
const keycloakUserId = profile.keycloak;
|
||||
if (!keycloakUserId) {
|
||||
return {
|
||||
profileId: profile.id,
|
||||
status: "skipped" as const,
|
||||
reason: "No keycloak ID",
|
||||
};
|
||||
}
|
||||
|
||||
try {
|
||||
// Check if empType is empty in Keycloak
|
||||
const { isEmpty, currentEmpType } =
|
||||
await this.checkEmpTypeEmpty(keycloakUserId);
|
||||
|
||||
result.profilesChecked++;
|
||||
|
||||
if (!isEmpty) {
|
||||
result.skipped++;
|
||||
return {
|
||||
profileId: profile.id,
|
||||
status: "skipped" as const,
|
||||
reason: "empType already exists",
|
||||
empType: currentEmpType,
|
||||
};
|
||||
}
|
||||
|
||||
result.missingEmpType++;
|
||||
|
||||
if (dryRun) {
|
||||
return {
|
||||
profileId: profile.id,
|
||||
status: "skipped" as const,
|
||||
reason: "dry run",
|
||||
wouldSync: true,
|
||||
};
|
||||
}
|
||||
|
||||
// Sync the profile
|
||||
const success = await withRetry(
|
||||
async () =>
|
||||
this.syncOnOrganizationChange(profile.id, profileType),
|
||||
3, // maxRetries
|
||||
1000, // baseDelay
|
||||
);
|
||||
|
||||
if (success) {
|
||||
result.syncSuccess++;
|
||||
return {
|
||||
profileId: profile.id,
|
||||
status: "synced" as const,
|
||||
};
|
||||
} else {
|
||||
result.syncFailed++;
|
||||
return {
|
||||
profileId: profile.id,
|
||||
status: "failed" as const,
|
||||
reason: "Sync returned false",
|
||||
};
|
||||
}
|
||||
} catch (error: any) {
|
||||
result.syncFailed++;
|
||||
return {
|
||||
profileId: profile.id,
|
||||
status: "failed" as const,
|
||||
reason: error.message || "Unknown error",
|
||||
};
|
||||
}
|
||||
}),
|
||||
);
|
||||
|
||||
// Log progress every 50 profiles
|
||||
const completed = Math.min(i + concurrency, profiles.length);
|
||||
if (completed % 50 === 0 || completed === profiles.length) {
|
||||
console.log(
|
||||
`[syncMissingEmpTypeByMonth] Progress: ${completed}/${profiles.length} profiles processed`,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
result.executionTime = `${((Date.now() - startTime) / 1000).toFixed(2)}s`;
|
||||
console.log(
|
||||
`[syncMissingEmpTypeByMonth] Completed: total=${result.totalProfiles}, checked=${result.profilesChecked}, missing=${result.missingEmpType}, synced=${result.syncSuccess}, failed=${result.syncFailed}, skipped=${result.skipped}, elapsed=${result.executionTime}`,
|
||||
);
|
||||
} catch (error) {
|
||||
console.error("[syncMissingEmpTypeByMonth] Error:", error);
|
||||
throw error;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Clear org DNA attributes in Keycloak for given profiles
|
||||
* Sets all org DNA fields to empty strings
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue