fix ปลดตำแหน่ง/ลบkeycloak คำสั่ง 12, 13, 17, 18, 19, 20, 23,

This commit is contained in:
Bright 2024-10-25 14:36:32 +07:00
parent 038eb37842
commit eecc6f22c9
3 changed files with 73 additions and 3 deletions

View file

@ -39,6 +39,7 @@ import {
commandTypePath,
removeProfileInOrganize,
setLogDataDiff,
checkCommandType
} from "../interfaces/utils";
import { Position } from "../entities/Position";
import { PosMaster } from "../entities/PosMaster";
@ -50,7 +51,7 @@ import { PosMasterAct } from "../entities/PosMasterAct";
import { sendToQueue } from "../services/rabbitmq";
import { PosLevel } from "../entities/PosLevel";
import { PosType } from "../entities/PosType";
import { addUserRoles, createUser, getRoles } from "../keycloak";
import { addUserRoles, createUser, getRoles, deleteUser } from "../keycloak";
import { ProfileEducation, CreateProfileEducation } from "../entities/ProfileEducation";
import { ProfileEducationHistory } from "../entities/ProfileEducationHistory";
import { CreateProfileCertificate, ProfileCertificate } from "../entities/ProfileCertificate";
@ -1826,6 +1827,16 @@ export class CommandController extends Controller {
if (item.isLeave == true) {
await removeProfileInOrganize(profile.id, "OFFICER");
}
const clearProfile = await checkCommandType(String(item.commandId));
if(clearProfile) {
if(profile.keycloak != null) {
const delUserKeycloak = await deleteUser(profile.keycloak);
if (delUserKeycloak) profile.keycloak = _null;
}
profile.position = _null;
profile.posTypeId = _null;
profile.posLevelId = _null;
}
await this.profileRepository.save(profile);
}),
);
@ -1854,6 +1865,7 @@ export class CommandController extends Controller {
leaveReason?: string | null;
dateLeave?: Date | null;
isGovernment?: boolean | null;
commandId?: string | null;
}[];
},
) {
@ -1905,6 +1917,16 @@ export class CommandController extends Controller {
if (item.isLeave == true) {
await removeProfileInOrganize(profile.id, "EMPLOYEE");
}
const clearProfile = await checkCommandType(String(item.commandId));
if(clearProfile) {
if(profile.keycloak != null) {
const delUserKeycloak = await deleteUser(profile.keycloak);
if (delUserKeycloak) profile.keycloak = _null;
}
profile.position = _null;
profile.posTypeId = _null;
profile.posLevelId = _null;
}
await this.profileEmployeeRepository.save(profile);
}),
);
@ -1959,7 +1981,18 @@ export class CommandController extends Controller {
createdAt: new Date(),
lastUpdatedAt: new Date(),
};
const clearProfile = await checkCommandType(String(item.commandId));
const _null: any = null;
if(clearProfile) {
if(profile.keycloak != null) {
const delUserKeycloak = await deleteUser(profile.keycloak);
if (delUserKeycloak) profile.keycloak = _null;
}
profile.position = _null;
profile.posTypeId = _null;
profile.posLevelId = _null;
await this.profileRepository.save(profile, {data: req});
}
Object.assign(data, { ...item, ...meta });
const history = new ProfileSalaryHistory();
Object.assign(history, { ...data, id: undefined });
@ -2155,6 +2188,16 @@ export class CommandController extends Controller {
if (item.isLeave == true) {
await removeProfileInOrganize(profile.id, "OFFICER");
}
const clearProfile = await checkCommandType(String(item.commandId));
if(clearProfile) {
if(profile.keycloak != null) {
const delUserKeycloak = await deleteUser(profile.keycloak);
if (delUserKeycloak) profile.keycloak = _null;
}
profile.position = _null;
profile.posTypeId = _null;
profile.posLevelId = _null;
}
await this.profileRepository.save(profile);
}
}),
@ -2327,6 +2370,17 @@ export class CommandController extends Controller {
dateGovernment: new Date(),
isGovernment: item.isGovernment,
});
const clearProfile = await checkCommandType(String(item.commandId));
const _null: any = null;
if(clearProfile) {
if(_profile.keycloak != null) {
const delUserKeycloak = await deleteUser(_profile.keycloak);
if (delUserKeycloak) _profile.keycloak = _null;
}
_profile.position = _null;
_profile.posTypeId = _null;
_profile.posLevelId = _null;
}
await Promise.all([
this.profileRepository.save(_profile),
this.salaryRepo.save(profileSalary),

View file

@ -187,7 +187,7 @@ export class KeycloakController extends Controller {
const profile = await this.profileRepo.findOne({
where: {
id: userId,
keycloak: userId,
},
});

View file

@ -6,6 +6,7 @@ import { EmployeePosMaster } from "../entities/EmployeePosMaster";
import { EmployeePosition } from "../entities/EmployeePosition";
import { In } from "typeorm";
import { RequestWithUser } from "../middlewares/user";
import { Command } from "../entities/Command";
export function calculateAge(start: Date, end = new Date()) {
if (start.getTime() > end.getTime()) return null;
@ -173,6 +174,21 @@ export async function removeProfileInOrganize(profileId: string, type: string) {
.execute();
}
}
export async function checkCommandType(commandId: string) {
const commandRepository = AppDataSource.getRepository(Command);
const _type = await commandRepository.findOne({
where: {
id: commandId
},
relations: ["commandType"],
});
if (!["C-PM-12", "C-PM-13", "C-PM-17", "C-PM-18", "C-PM-23", "C-PM-19", "C-PM-20"].includes(String(_type?.commandType.code))) {
return false;
}
return true;
}
//logs
export type DataDiff = {
before: any;