This commit is contained in:
Bright 2025-01-22 17:42:23 +07:00
parent dcc53befb2
commit 9f5f1af6e2
3 changed files with 123 additions and 82 deletions

View file

@ -53,7 +53,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, deleteUser, enableStatus } from "../keycloak";
import { addUserRoles, createUser, getRoles, deleteUser, enableStatus, getUserByUsername, getRoleMappings } from "../keycloak";
import { ProfileEducation, CreateProfileEducation } from "../entities/ProfileEducation";
import { ProfileEducationHistory } from "../entities/ProfileEducationHistory";
import { CreateProfileCertificate, ProfileCertificate } from "../entities/ProfileCertificate";
@ -2564,47 +2564,37 @@ export class CommandController extends Controller {
//คำสั่งบรรจุกลับเข้ารับราชการ หรือ ผู้ออกไปรับราชการทหารกลับเข้ารับราชการ solutionเดิม ให้ enable user เปลี่ยนเป็นสร้าง user ใหม่เลยเพราะยังไงตอนถูกพักก็ถูกลบ user
if (returnWork && item.isGovernment) {
let userKeycloakId;
userKeycloakId = await createUser(profile.citizenId, profile.citizenId, {
firstName: profile.firstName,
lastName: profile.lastName,
});
// กรณี Keycloak ไม่ถูกลบ ให้ลบซ้ำอีกรอบแล้วสร้างใหม่ และหากยังไม่สามารถลบได้ให้แสดง Error
if (
profile.keycloak != null &&
userKeycloakId &&
userKeycloakId.errorMessage === "User exists with same username"
) {
const delUserKeycloak = await deleteUser(profile.keycloak);
if (delUserKeycloak) {
userKeycloakId = await createUser(profile.citizenId, profile.citizenId, {
firstName: profile.firstName,
lastName: profile.lastName,
});
} else {
throw new HttpError(
HttpStatus.BAD_REQUEST,
"พบข้อผิดพลาด ไม่สามารถจัดการผู้ใช้งานได้",
const checkUser = await getUserByUsername(profile.citizenId)
//ถ้ายังไม่มี user keycloak ให้สร้างใหม่
if (checkUser.length == 0) {
userKeycloakId = await createUser(profile.citizenId, profile.citizenId, {
firstName: profile.firstName,
lastName: profile.lastName,
});
const list = await getRoles();
let result = false;
if (Array.isArray(list) && userKeycloakId) {
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 : "";;
}
const list = await getRoles();
let result = false;
if (Array.isArray(list) && userKeycloakId) {
result = await addUserRoles(
userKeycloakId,
list
.filter((v) => v.name === "USER")
.map((x) => ({
id: x.id,
name: x.name,
})),
);
//ถ้ามีอยู่แล้วให้ใช้อันเดิม
else {
// const _roleKeycloaks = await getRoleMappings(checkUser[0].id);
profile.keycloak = checkUser[0].id;
}
profile.amount = item.amount ?? _null;
profile.amountSpecial = item.amountSpecial ?? _null;
profile.isActive = true;
profile.keycloak = typeof userKeycloakId === "string" ? userKeycloakId : "";
profile.roleKeycloaks = result && roleKeycloak ? [roleKeycloak] : [];
}
await this.profileRepository.save(profile);
}),
@ -3929,26 +3919,32 @@ export class CommandController extends Controller {
item.bodyProfile.birthDate == null
? _null
: calculateRetireLaw(item.bodyProfile.birthDate);
const userKeycloakId = await createUser(profile.citizenId, profile.citizenId, {
firstName: profile.firstName,
lastName: profile.lastName,
});
// if (typeof userKeycloakId !== "string") {
// throw new Error(userKeycloakId.errorMessage);
// }
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,
})),
);
// if (!result) throw new Error("Failed. Cannot set user's role.");
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 {
profile.keycloak = checkUser[0].id;
}
let registrationProvinceId = await this.provinceRepo.findOneBy({
id: profile.registrationProvinceId,
});
@ -3980,8 +3976,6 @@ export class CommandController extends Controller {
id: profile.currentSubDistrictId,
});
profile.currentSubDistrictId = currentSubDistrictId ? currentSubDistrictId.id : null_;
profile.keycloak = typeof userKeycloakId === "string" ? userKeycloakId : "";
profile.roleKeycloaks = result && roleKeycloak ? [roleKeycloak] : [];
profile.email = item.bodyProfile.email;
profile.dateStart = item.bodyProfile.dateStart;
profile.amount = item.bodyProfile.amount ?? null;
@ -4317,30 +4311,36 @@ export class CommandController extends Controller {
if (positionNew != null) {
// Create Keycloak
const userKeycloakId = await createUser(profile.citizenId, profile.citizenId, {
firstName: profile.firstName,
lastName: profile.lastName,
// email: profile.email,
});
// if (typeof userKeycloakId !== "string") {
// throw new Error(userKeycloakId.errorMessage);
// }
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,
})),
);
// if (!result) throw new Error("Failed. Cannot set user's role.");
profile.keycloak = typeof userKeycloakId == "string" ? userKeycloakId : "";
profile.roleKeycloaks = result && roleKeycloak ? [roleKeycloak] : [];
// End Create Keycloak
const checkUser = await getUserByUsername(profile.citizenId)
if(checkUser.length == 0) {
const userKeycloakId = await createUser(profile.citizenId, profile.citizenId, {
firstName: profile.firstName,
lastName: profile.lastName,
// email: profile.email,
});
// if (typeof userKeycloakId !== "string") {
// throw new Error(userKeycloakId.errorMessage);
// }
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,
})),
);
// if (!result) throw new Error("Failed. Cannot set user's role.");
profile.keycloak = userKeycloakId && typeof userKeycloakId == "string" ? userKeycloakId : "";
profile.roleKeycloaks = result && roleKeycloak ? [roleKeycloak] : [];
// End Create Keycloak
}
else {
profile.keycloak = checkUser[0].id;
}
positionNew.positionIsSelected = true;
profile.posLevelId = positionNew.posLevelId;