สร้าง user ประจำของ keycloak #759

This commit is contained in:
AdisakKanthawilang 2024-11-05 17:26:42 +07:00
parent 458fb90187
commit 37fc689ecc

View file

@ -1604,24 +1604,26 @@ export class CommandController extends Controller {
commandRecive = Object.assign(new CommandRecive(), item);
commandRecive.order = order;
let salaryData = null_;
if(item.profileId){
if (item.profileId) {
salaryData = await this.profileRepository.findOne({
where: {
id: item.profileId
}
id: item.profileId,
},
});
let null_: any = 0;
if (!salaryData) {
salaryData = await this.profileEmployeeRepository.findOne({
salaryData = await this.profileEmployeeRepository.findOne({
where: {
id: item.profileId
}
});
id: item.profileId,
},
});
}
commandRecive.amount = salaryData ? salaryData.amount : null_;
commandRecive.positionSalaryAmount = salaryData ? salaryData.positionSalaryAmount : null_;
commandRecive.mouthSalaryAmount = salaryData ? salaryData.mouthSalaryAmount : null_;
}else{
commandRecive.positionSalaryAmount = salaryData
? salaryData.positionSalaryAmount
: null_;
commandRecive.mouthSalaryAmount = salaryData ? salaryData.mouthSalaryAmount : null_;
} else {
commandRecive.amount = null_;
commandRecive.positionSalaryAmount = null_;
commandRecive.mouthSalaryAmount = null_;
@ -2984,7 +2986,34 @@ export class CommandController extends Controller {
posMasterId: profile.posmasterIdTemp,
},
});
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 = userKeycloakId;
// End Create Keycloak
positionNew.positionIsSelected = true;
profile.posLevelId = positionNew.posLevelId;
profile.posTypeId = positionNew.posTypeId;