เช็ค update role keycloak #802

This commit is contained in:
Bright 2024-11-22 11:34:11 +07:00
parent 71d5ad0ab9
commit 102f1f6646

View file

@ -2172,26 +2172,30 @@ export class CommandController extends Controller {
if (clearProfile) {
if (profile.keycloak != null) {
const delUserKeycloak = await deleteUser(profile.keycloak);
if (delUserKeycloak) profile.keycloak = _null;
profile.roleKeycloaks = [];
if (delUserKeycloak) {
profile.keycloak = _null;
profile.roleKeycloaks = [];
profile.isActive = false;
}
}
profile.position = _null;
profile.posTypeId = _null;
profile.posLevelId = _null;
}
const returnWork = await checkReturnCommandType(String(item.commandId));
//คำสั่งบรรจุกลับเข้ารับราชการ หรือ ผู้ออกไปรับราชการทหารกลับเข้ารับราชการ solutionเดิม ให้ enable user เปลี่ยนเป็นสร้าง user ใหม่เลยเพราะยังไงตอนถูกพักก็ถูกลบ user
if (returnWork && item.isGovernment) {
if (profile.keycloak != null) {
/*if (profile.keycloak != null) {
const enableActive = await enableStatus(profile.keycloak, true);
if (!enableActive) throw new Error("Failed. Cannot change enable status.");
} else {
} else {*/
const userKeycloakId = await createUser(profile.citizenId, profile.citizenId, {
firstName: profile.firstName,
lastName: profile.lastName,
});
if (typeof userKeycloakId !== "string") {
throw new Error(userKeycloakId.errorMessage);
}
// 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.");
@ -2204,11 +2208,11 @@ export class CommandController extends Controller {
name: x.name,
})),
);
if (!result) throw new Error("Failed. Cannot set user's role.");
profile.keycloak = userKeycloakId;
}
// if (!result) throw new Error("Failed. Cannot set user's role.");
profile.keycloak = typeof userKeycloakId === "string" ? userKeycloakId : "";
/*}*/
profile.isActive = true;
profile.roleKeycloaks = profile.roleKeycloaks ?? [roleKeycloak];
profile.roleKeycloaks = result && roleKeycloak ? [roleKeycloak] : [];
}
await this.profileRepository.save(profile);
}),
@ -2297,9 +2301,11 @@ export class CommandController extends Controller {
if (clearProfile) {
if (profile.keycloak != null) {
const delUserKeycloak = await deleteUser(profile.keycloak);
if (delUserKeycloak) profile.keycloak = _null;
profile.roleKeycloaks = [];
profile.isActive = false;
if (delUserKeycloak) {
profile.keycloak = _null;
profile.roleKeycloaks = [];
profile.isActive = false;
}
}
profile.position = _null;
profile.posTypeId = _null;
@ -2373,8 +2379,11 @@ export class CommandController extends Controller {
if (clearProfile) {
if (profile.keycloak != null) {
const delUserKeycloak = await deleteUser(profile.keycloak);
if (delUserKeycloak) profile.keycloak = _null;
profile.roleKeycloaks = [];
if (delUserKeycloak) {
profile.keycloak = _null;
profile.roleKeycloaks = [];
profile.isActive = false;
}
}
profile.position = _null;
profile.posTypeId = _null;
@ -2615,17 +2624,27 @@ export class CommandController extends Controller {
const exceptClear = await checkExceptCommandType(String(item.commandId));
if (item.isLeave == true && !exceptClear) {
await removeProfileInOrganize(_profile.id, "OFFICER");
} else if (item.isLeave == true && exceptClear && _profile.keycloak != null) {
const enableActive = await enableStatus(_profile.keycloak, false);
if (!enableActive) throw new Error("Failed. Cannot change enable status.");
_profile.isActive = false;
}
//คำสั่งพักราชการ หรือ ให้ออกจากราชการไว้ก่อน solutionเดิม ให้ disable user ไว้แต่ยังไม่ลบ เปลี่ยนเป็นลบ user ออกเลย
else if (item.isLeave == true && exceptClear && _profile.keycloak != null) {
// const enableActive = await enableStatus(_profile.keycloak, false);
// if (!enableActive) throw new Error("Failed. Cannot change enable status.");
const delUserKeycloak = await deleteUser(_profile.keycloak);
if (delUserKeycloak) {
_profile.keycloak = _null;
_profile.roleKeycloaks = [];
_profile.isActive = false;
}
}
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.roleKeycloaks = [];
if (delUserKeycloak) {
_profile.keycloak = _null;
_profile.roleKeycloaks = [];
_profile.isActive = false;
}
}
_profile.position = _null;
_profile.posTypeId = _null;
@ -2903,8 +2922,11 @@ export class CommandController extends Controller {
if (clearProfile) {
if (_profile.keycloak != null) {
const delUserKeycloak = await deleteUser(_profile.keycloak);
if (delUserKeycloak) _profile.keycloak = _null;
_profile.roleKeycloaks = [];
if (delUserKeycloak) {
_profile.keycloak = _null;
_profile.roleKeycloaks = [];
_profile.isActive = false;
}
}
_profile.position = _null;
_profile.posTypeId = _null;
@ -2989,9 +3011,9 @@ export class CommandController extends Controller {
firstName: profile.firstName,
lastName: profile.lastName,
});
if (typeof userKeycloakId !== "string") {
throw new Error(userKeycloakId.errorMessage);
}
// 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.");
@ -3004,9 +3026,9 @@ export class CommandController extends Controller {
name: x.name,
})),
);
if (!result) throw new Error("Failed. Cannot set user's role.");
profile.keycloak = userKeycloakId;
profile.roleKeycloaks = roleKeycloak ? [roleKeycloak] : [];
// if (!result) throw new Error("Failed. Cannot set user's role.");
profile.keycloak = typeof userKeycloakId === "string" ? userKeycloakId : "";
profile.roleKeycloaks = result && roleKeycloak ? [roleKeycloak] : [];
profile.email = item.bodyProfile.email;
profile.dateStart = item.bodyProfile.dateStart;
await this.profileRepository.save(profile);
@ -3322,9 +3344,9 @@ export class CommandController extends Controller {
lastName: profile.lastName,
// email: profile.email,
});
if (typeof userKeycloakId !== "string") {
throw new Error(userKeycloakId.errorMessage);
}
// 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.");
@ -3337,9 +3359,9 @@ export class CommandController extends Controller {
name: x.name,
})),
);
if (!result) throw new Error("Failed. Cannot set user's role.");
profile.keycloak = userKeycloakId;
profile.roleKeycloaks = roleKeycloak ? [roleKeycloak] : [];
// 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
positionNew.positionIsSelected = true;