fix emp temp and add script clear org dna at keycloak
This commit is contained in:
parent
b714dfe239
commit
49a8494a8d
3 changed files with 363 additions and 57 deletions
|
|
@ -9,6 +9,7 @@ import { PosMaster } from "./../entities/PosMaster";
|
|||
import axios from "axios";
|
||||
import { KeycloakSyncController } from "./KeycloakSyncController";
|
||||
import { EmployeePosMaster } from "./../entities/EmployeePosMaster";
|
||||
import { EmployeeTempPosMaster } from "./../entities/EmployeeTempPosMaster";
|
||||
|
||||
interface OrgUpdatePayload {
|
||||
profileId: string;
|
||||
|
|
@ -26,6 +27,7 @@ interface OrgUpdatePayload {
|
|||
export class ScriptProfileOrgController extends Controller {
|
||||
private posMasterRepo = AppDataSource.getRepository(PosMaster);
|
||||
private employeePosMasterRepo = AppDataSource.getRepository(EmployeePosMaster);
|
||||
private employeeTempPosMasterRepo = AppDataSource.getRepository(EmployeeTempPosMaster);
|
||||
|
||||
// Idempotency flag to prevent concurrent runs
|
||||
private isRunning = false;
|
||||
|
|
@ -37,6 +39,11 @@ export class ScriptProfileOrgController extends Controller {
|
|||
10,
|
||||
);
|
||||
|
||||
/**
|
||||
* Script to update profile's organizational structure in leave service and sync to Keycloak
|
||||
*
|
||||
* @summary Update org structure for profiles updated within a certain time window and sync to Keycloak
|
||||
*/
|
||||
@Post("update-org")
|
||||
public async cronjobUpdateOrg(@Request() request: RequestWithUser) {
|
||||
// Idempotency check - prevent concurrent runs
|
||||
|
|
@ -61,7 +68,7 @@ export class ScriptProfileOrgController extends Controller {
|
|||
});
|
||||
|
||||
// Query with optimized select - only fetch required fields
|
||||
const [posMasters, posMasterEmployee] = await Promise.all([
|
||||
const [posMasters, posMasterEmployee, posMasterEmployeeTemp] = await Promise.all([
|
||||
this.posMasterRepo.find({
|
||||
where: {
|
||||
lastUpdatedAt: MoreThanOrEqual(windowStart),
|
||||
|
|
@ -120,16 +127,46 @@ export class ScriptProfileOrgController extends Controller {
|
|||
current_holder: { id: true },
|
||||
},
|
||||
}),
|
||||
this.employeeTempPosMasterRepo.find({
|
||||
where: {
|
||||
lastUpdatedAt: MoreThanOrEqual(windowStart),
|
||||
orgRevision: {
|
||||
orgRevisionIsCurrent: true,
|
||||
},
|
||||
},
|
||||
relations: [
|
||||
"orgRevision",
|
||||
"orgRoot",
|
||||
"orgChild1",
|
||||
"orgChild2",
|
||||
"orgChild3",
|
||||
"orgChild4",
|
||||
"current_holder",
|
||||
],
|
||||
select: {
|
||||
id: true,
|
||||
current_holderId: true,
|
||||
lastUpdatedAt: true,
|
||||
orgRevision: { id: true },
|
||||
orgRoot: { ancestorDNA: true },
|
||||
orgChild1: { ancestorDNA: true },
|
||||
orgChild2: { ancestorDNA: true },
|
||||
orgChild3: { ancestorDNA: true },
|
||||
orgChild4: { ancestorDNA: true },
|
||||
current_holder: { id: true },
|
||||
},
|
||||
}),
|
||||
]);
|
||||
|
||||
console.log("cronjobUpdateOrg: Database query completed", {
|
||||
posMastersCount: posMasters.length,
|
||||
employeePosCount: posMasterEmployee.length,
|
||||
totalRecords: posMasters.length + posMasterEmployee.length,
|
||||
employeeTempPosCount: posMasterEmployeeTemp.length,
|
||||
totalRecords: posMasters.length + posMasterEmployee.length + posMasterEmployeeTemp.length,
|
||||
});
|
||||
|
||||
// Build payloads with proper profile type tracking
|
||||
const payloads = this.buildPayloads(posMasters, posMasterEmployee);
|
||||
const payloads = this.buildPayloads(posMasters, posMasterEmployee, posMasterEmployeeTemp);
|
||||
|
||||
if (payloads.length === 0) {
|
||||
console.log("cronjobUpdateOrg: No records to process");
|
||||
|
|
@ -246,12 +283,13 @@ export class ScriptProfileOrgController extends Controller {
|
|||
}
|
||||
|
||||
/**
|
||||
* Build payloads from PosMaster and EmployeePosMaster records
|
||||
* Build payloads from PosMaster, EmployeePosMaster, and EmployeeTempPosMaster records
|
||||
* Includes proper profile type tracking for accurate Keycloak sync
|
||||
*/
|
||||
private buildPayloads(
|
||||
posMasters: PosMaster[],
|
||||
posMasterEmployee: EmployeePosMaster[],
|
||||
posMasterEmployeeTemp: EmployeeTempPosMaster[],
|
||||
): OrgUpdatePayload[] {
|
||||
const payloads: OrgUpdatePayload[] = [];
|
||||
|
||||
|
|
@ -285,6 +323,21 @@ export class ScriptProfileOrgController extends Controller {
|
|||
}
|
||||
}
|
||||
|
||||
// Process EmployeeTempPosMaster records (PROFILE_EMPLOYEE type)
|
||||
for (const employeeTempPos of posMasterEmployeeTemp) {
|
||||
if (employeeTempPos.current_holder && employeeTempPos.current_holderId) {
|
||||
payloads.push({
|
||||
profileId: employeeTempPos.current_holderId,
|
||||
rootDnaId: employeeTempPos.orgRoot?.ancestorDNA || null,
|
||||
child1DnaId: employeeTempPos.orgChild1?.ancestorDNA || null,
|
||||
child2DnaId: employeeTempPos.orgChild2?.ancestorDNA || null,
|
||||
child3DnaId: employeeTempPos.orgChild3?.ancestorDNA || null,
|
||||
child4DnaId: employeeTempPos.orgChild4?.ancestorDNA || null,
|
||||
profileType: "PROFILE_EMPLOYEE",
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
return payloads;
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue