fix ออกคำสั่งบรรจุแต่งตั้ง ขรก.เดิมหรือขรก.สถานะพ้นราชการไม่อัพเดพสถานะทดลองงาน

This commit is contained in:
Bright 2025-01-31 14:10:16 +07:00
parent 87a91b035c
commit 618ccb5cff

View file

@ -61,6 +61,7 @@ import {
enableStatus,
getUserByUsername,
getRoleMappings,
removeUserRoles
} from "../keycloak";
import { ProfileEducation, CreateProfileEducation } from "../entities/ProfileEducation";
import { ProfileEducationHistory } from "../entities/ProfileEducationHistory";
@ -4214,6 +4215,9 @@ export class CommandController extends Controller {
const roleKeycloak = await this.roleKeycloakRepo.findOne({
where: { name: Like("USER") },
});
const list = await getRoles();
if (!Array.isArray(list))
throw new Error("Failed. Cannot get role(s) data from the server.");
const _null: any = null;
await Promise.all(
body.data.map(async (item) => {
@ -4242,85 +4246,90 @@ export class CommandController extends Controller {
) {
throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลประเภทตำแหน่งนี้");
}
let registrationProvinceId = await this.provinceRepo.findOneBy({
id: item.bodyProfile.registrationProvinceId ?? "",
});
let registrationDistrictId = await this.districtRepo.findOneBy({
id: item.bodyProfile.registrationDistrictId ?? "",
});
let registrationSubDistrictId = await this.subDistrictRepo.findOneBy({
id: item.bodyProfile.registrationSubDistrictId ?? "",
});
let currentProvinceId = await this.provinceRepo.findOneBy({
id: item.bodyProfile.currentProvinceId ?? "",
});
let currentDistrictId = await this.districtRepo.findOneBy({
id: item.bodyProfile.currentDistrictId ?? "",
});
let currentSubDistrictId = await this.subDistrictRepo.findOneBy({
id: item.bodyProfile.currentSubDistrictId ?? "",
});
let _dateRetire = item.bodyProfile.birthDate == null
? _null
: calculateRetireDate(item.bodyProfile.birthDate);
let _dateRetireLaw = item.bodyProfile.birthDate == null
? _null
: calculateRetireLaw(item.bodyProfile.birthDate);
let userKeycloakId:any
let result:any
const checkUser = await getUserByUsername(item.bodyProfile.citizenId);
if (checkUser.length == 0) {
userKeycloakId = await createUser(item.bodyProfile.citizenId, item.bodyProfile.citizenId, {
firstName: item.bodyProfile.firstName,
lastName: item.bodyProfile.lastName,
});
result = await addUserRoles(
userKeycloakId,
list
.filter((v) => v.name === "USER")
.map((x) => ({
id: x.id,
name: x.name,
})),
);
} else {
userKeycloakId = checkUser[0].id;
const rolesData = await getRoleMappings(userKeycloakId);
if (rolesData) {
const _delRole = rolesData.map((x:any) => ({
id : x.id,
name: x.name
}));
await removeUserRoles(
userKeycloakId,
_delRole
);
}
result = await addUserRoles(
userKeycloakId,
list
.filter((v) => v.name === "USER")
.map((x) => ({
id: x.id,
name: x.name,
})),
);
}
let profile: any = await this.profileRepository.findOne({
where: { citizenId: item.bodyProfile.citizenId, isActive: true },
where: { citizenId: item.bodyProfile.citizenId/*, isActive: true */},
relations: ["roleKeycloaks"],
});
if (!profile) {
profile = Object.assign({ ...item.bodyProfile, ...meta });
profile.dateRetire =
item.bodyProfile.birthDate == null
? _null
: calculateRetireDate(item.bodyProfile.birthDate);
profile.dateRetireLaw =
item.bodyProfile.birthDate == null
? _null
: calculateRetireLaw(item.bodyProfile.birthDate);
const checkUser = await getUserByUsername(profile.citizenId);
if (checkUser.length == 0) {
const userKeycloakId = await createUser(profile.citizenId, profile.citizenId, {
firstName: profile.firstName,
lastName: profile.lastName,
});
const list = await getRoles();
if (!Array.isArray(list))
throw new Error("Failed. Cannot get role(s) data from the server.");
const result = await addUserRoles(
userKeycloakId,
list
.filter((v) => v.name === "USER")
.map((x) => ({
id: x.id,
name: x.name,
})),
);
profile.roleKeycloaks = result && roleKeycloak ? [roleKeycloak] : [];
profile.keycloak =
userKeycloakId && typeof userKeycloakId === "string" ? userKeycloakId : "";
} else {
const rolesData = await getRoleMappings(checkUser[0].id);
if (rolesData) {
const _roleKeycloak = await this.roleKeycloakRepo.find({
where: { name: In(rolesData.map((x: any) => x.name)) },
});
profile.roleKeycloaks =
_roleKeycloak && _roleKeycloak.length > 0 ? _roleKeycloak : [];
}
profile.keycloak = checkUser[0].id;
}
let registrationProvinceId = await this.provinceRepo.findOneBy({
id: profile.registrationProvinceId,
});
profile.registrationProvinceId = registrationProvinceId
? registrationProvinceId.id
: _null;
let registrationDistrictId = await this.districtRepo.findOneBy({
id: profile.registrationDistrictId,
});
profile.registrationDistrictId = registrationDistrictId
? registrationDistrictId.id
: _null;
let registrationSubDistrictId = await this.subDistrictRepo.findOneBy({
id: profile.registrationSubDistrictId,
});
profile.registrationSubDistrictId = registrationSubDistrictId
? registrationSubDistrictId.id
: _null;
let currentProvinceId = await this.provinceRepo.findOneBy({
id: profile.currentProvinceId,
});
profile.dateRetire = _dateRetire;
profile.dateRetireLaw = _dateRetireLaw
profile.roleKeycloaks = result && roleKeycloak ? [roleKeycloak] : [];
profile.keycloak = userKeycloakId && typeof userKeycloakId === "string" ? userKeycloakId : ""
profile.registrationProvinceId = registrationProvinceId ? registrationProvinceId.id : _null;
profile.registrationDistrictId = registrationDistrictId ? registrationDistrictId.id : _null;
profile.registrationSubDistrictId = registrationSubDistrictId ? registrationSubDistrictId.id : _null;
profile.currentProvinceId = currentProvinceId ? currentProvinceId.id : _null;
let currentDistrictId = await this.districtRepo.findOneBy({
id: profile.currentDistrictId,
});
profile.currentDistrictId = currentDistrictId ? currentDistrictId.id : _null;
let currentSubDistrictId = await this.subDistrictRepo.findOneBy({
id: profile.currentSubDistrictId,
});
profile.currentSubDistrictId = currentSubDistrictId ? currentSubDistrictId.id : _null;
profile.email = item.bodyProfile.email;
profile.dateStart = item.bodyProfile.dateStart;
@ -4330,6 +4339,41 @@ export class CommandController extends Controller {
await this.profileRepository.save(profile);
setLogDataDiff(req, { before, after: profile });
}
//ขรก.ในระบบ หรือ ขรก.ในระบบที่สถานะพ้นจากราชการ
else {
console.log("have keycloak..............")
profile.roleKeycloaks = result && roleKeycloak ? [roleKeycloak] : [];
profile.keycloak = userKeycloakId && typeof userKeycloakId === "string" ? userKeycloakId : "";
profile.isProbation = item.bodyProfile.isProbation;
profile.isLeave = item.bodyProfile.isLeave;
profile.isRetirement = false;
profile.isActive = true;
profile.dateLeave = _null;
profile.dateRetire = _dateRetire;
profile.dateRetireLaw = _dateRetireLaw
profile.registrationProvinceId = registrationProvinceId ? registrationProvinceId.id : _null;
profile.registrationDistrictId = registrationDistrictId ? registrationDistrictId.id : _null;
profile.registrationSubDistrictId = registrationSubDistrictId ? registrationSubDistrictId.id : _null;
profile.currentProvinceId = currentProvinceId ? currentProvinceId.id : _null;
profile.currentDistrictId = currentDistrictId ? currentDistrictId.id : _null;
profile.currentSubDistrictId = currentSubDistrictId ? currentSubDistrictId.id : _null;
profile.email = item.bodyProfile.email;
profile.dateStart = item.bodyProfile.dateStart;
profile.amount = item.bodyProfile.amount ?? null;
profile.amountSpecial = item.bodyProfile.amountSpecial ?? null;
profile.leaveCommandId = _null;
profile.leaveCommandNo = _null;
profile.leaveRemark = _null;
profile.leaveDate = _null;
profile.leaveType = _null;
profile.lastUpdateUserId = req.user.sub;
profile.lastUpdateFullName = req.user.name;
profile.lastUpdatedAt = new Date();
profile.refCommandDate = new Date();
await this.profileRepository.save(profile);
setLogDataDiff(req, { before, after: profile });
}
if (profile && profile.id) {
//Educations
if (item.bodyEducations && item.bodyEducations.length > 0) {