parent
79b8e9f1ba
commit
40f1a08b90
1 changed files with 149 additions and 53 deletions
|
|
@ -908,21 +908,33 @@ export class KeycloakController extends Controller {
|
||||||
) {
|
) {
|
||||||
const profiles = await this.profileEmpRepo.find({
|
const profiles = await this.profileEmpRepo.find({
|
||||||
where: {
|
where: {
|
||||||
keycloak: IsNull(),
|
// keycloak: IsNull(),
|
||||||
|
isLeave: false,
|
||||||
},
|
},
|
||||||
relations: ["roleKeycloaks"],
|
relations: ["roleKeycloaks"],
|
||||||
});
|
});
|
||||||
|
|
||||||
for await (const _item of profiles) {
|
// ดึงข้อมูลที่ใช้บ่อยก่อน (cache)
|
||||||
|
const rolesList = await getRoles();
|
||||||
|
if (!Array.isArray(rolesList))
|
||||||
|
throw new Error("Failed. Cannot get role(s) data from the server.");
|
||||||
|
|
||||||
|
const roleKeycloak = await this.roleKeycloakRepo.find({
|
||||||
|
where: { id: "8a1a0dc9-304c-4e5b-a90a-65f841048212" },
|
||||||
|
});
|
||||||
|
|
||||||
|
// Process แบบ batch เพื่อลดการเรียก API ทีละตัว
|
||||||
|
const batchSize = 10;
|
||||||
|
const batches = [];
|
||||||
|
for (let i = 0; i < profiles.length; i += batchSize) {
|
||||||
|
batches.push(profiles.slice(i, i + batchSize));
|
||||||
|
}
|
||||||
|
|
||||||
|
for (const batch of batches) {
|
||||||
|
await Promise.all(
|
||||||
|
batch.map(async (_item) => {
|
||||||
let password = _item.citizenId;
|
let password = _item.citizenId;
|
||||||
if (_item.birthDate != null) {
|
if (_item.birthDate != null) {
|
||||||
// const gregorianYear = _item.birthDate.getFullYear() + 543;
|
|
||||||
|
|
||||||
// const formattedDate =
|
|
||||||
// _item.birthDate.toISOString().slice(8, 10) +
|
|
||||||
// _item.birthDate.toISOString().slice(5, 7) +
|
|
||||||
// gregorianYear;
|
|
||||||
// password = formattedDate;
|
|
||||||
const _date = new Date(_item.birthDate.toDateString())
|
const _date = new Date(_item.birthDate.toDateString())
|
||||||
.getDate()
|
.getDate()
|
||||||
.toString()
|
.toString()
|
||||||
|
|
@ -933,41 +945,45 @@ export class KeycloakController extends Controller {
|
||||||
const _year = new Date(_item.birthDate.toDateString()).getFullYear() + 543;
|
const _year = new Date(_item.birthDate.toDateString()).getFullYear() + 543;
|
||||||
password = `${_date}${_month}${_year}`;
|
password = `${_date}${_month}${_year}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
const checkUser = await getUserByUsername(_item.citizenId);
|
const checkUser = await getUserByUsername(_item.citizenId);
|
||||||
let userId: any = "";
|
let userId: any = "";
|
||||||
if (checkUser.length == 0) {
|
if (checkUser.length == 0) {
|
||||||
userId = await createUser(_item.citizenId, password, {
|
userId = await createUser(_item.citizenId, password, {
|
||||||
firstName: _item.firstName,
|
firstName: _item.firstName,
|
||||||
lastName: _item.lastName,
|
lastName: _item.lastName,
|
||||||
// email: _item.email,
|
|
||||||
});
|
});
|
||||||
if (typeof userId !== "string") {
|
if (typeof userId !== "string") {
|
||||||
throw new Error(userId.errorMessage);
|
console.error(`Failed to create user for ${_item.citizenId}:`, userId.errorMessage);
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
userId = checkUser[0].id;
|
userId = checkUser[0].id;
|
||||||
}
|
}
|
||||||
|
|
||||||
const list = await getRoles();
|
|
||||||
if (!Array.isArray(list)) throw new Error("Failed. Cannot get role(s) data from the server.");
|
|
||||||
const result = await addUserRoles(
|
const result = await addUserRoles(
|
||||||
userId,
|
userId,
|
||||||
list.filter((v) => v.id == "8a1a0dc9-304c-4e5b-a90a-65f841048212"),
|
rolesList.filter((v) => v.id == "8a1a0dc9-304c-4e5b-a90a-65f841048212"),
|
||||||
);
|
);
|
||||||
|
|
||||||
if (!result) {
|
if (!result) {
|
||||||
throw new Error("Failed. Cannot set user's role.");
|
console.error(`Failed to set role for user ${_item.citizenId}`);
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (typeof userId === "string") {
|
if (typeof userId === "string") {
|
||||||
_item.keycloak = userId;
|
_item.keycloak = userId;
|
||||||
}
|
}
|
||||||
const roleKeycloak = await this.roleKeycloakRepo.find({
|
|
||||||
where: { id: "8a1a0dc9-304c-4e5b-a90a-65f841048212" },
|
|
||||||
});
|
|
||||||
if (_item) {
|
if (_item) {
|
||||||
_item.roleKeycloaks = Array.from(new Set([..._item.roleKeycloaks, ...roleKeycloak]));
|
_item.roleKeycloaks = Array.from(new Set([..._item.roleKeycloaks, ...roleKeycloak]));
|
||||||
this.profileEmpRepo.save(_item);
|
await this.profileEmpRepo.save(_item);
|
||||||
}
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.error(`Error processing ${_item.citizenId}:`, error);
|
||||||
|
}
|
||||||
|
}),
|
||||||
|
);
|
||||||
}
|
}
|
||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
|
|
@ -1079,4 +1095,84 @@ export class KeycloakController extends Controller {
|
||||||
}
|
}
|
||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Post("add-role-staff/user-emp/{child1Id}")
|
||||||
|
@Security("bearerAuth", ["system", "admin"])
|
||||||
|
async addroleStaffToUserEmp(
|
||||||
|
@Path() child1Id: string,
|
||||||
|
@Request() request: { user: { sub: string; preferred_username: string } },
|
||||||
|
) {
|
||||||
|
const profiles = await this.profileEmpRepo.find({
|
||||||
|
where: {
|
||||||
|
keycloak: Not(IsNull()),
|
||||||
|
isLeave: false,
|
||||||
|
current_holders: {
|
||||||
|
orgChild1Id: child1Id,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
relations: ["roleKeycloaks"],
|
||||||
|
});
|
||||||
|
// return profiles.length;
|
||||||
|
|
||||||
|
for await (const _item of profiles) {
|
||||||
|
let password = _item.citizenId;
|
||||||
|
if (_item.birthDate != null) {
|
||||||
|
const _date = new Date(_item.birthDate.toDateString())
|
||||||
|
.getDate()
|
||||||
|
.toString()
|
||||||
|
.padStart(2, "0");
|
||||||
|
const _month = (new Date(_item.birthDate.toDateString()).getMonth() + 1)
|
||||||
|
.toString()
|
||||||
|
.padStart(2, "0");
|
||||||
|
const _year = new Date(_item.birthDate.toDateString()).getFullYear() + 543;
|
||||||
|
password = `${_date}${_month}${_year}`;
|
||||||
|
}
|
||||||
|
const checkUser = await getUserByUsername(_item.citizenId);
|
||||||
|
let userId: any = "";
|
||||||
|
if (checkUser.length == 0) {
|
||||||
|
userId = await createUser(_item.citizenId, password, {
|
||||||
|
firstName: _item.firstName,
|
||||||
|
lastName: _item.lastName,
|
||||||
|
});
|
||||||
|
if (typeof userId !== "string") {
|
||||||
|
throw new Error(userId.errorMessage);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
userId = checkUser[0].id;
|
||||||
|
}
|
||||||
|
|
||||||
|
const list = await getRoles();
|
||||||
|
if (!Array.isArray(list)) throw new Error("Failed. Cannot get role(s) data from the server.");
|
||||||
|
const resultUser = await addUserRoles(
|
||||||
|
userId,
|
||||||
|
list.filter((v) => v.id == "8a1a0dc9-304c-4e5b-a90a-65f841048212"),
|
||||||
|
);
|
||||||
|
const resultStaff = await addUserRoles(
|
||||||
|
userId,
|
||||||
|
list.filter((v) => v.id == "f1fff8db-0795-47c1-9952-f3c18d5b6172"),
|
||||||
|
);
|
||||||
|
|
||||||
|
// if (!resultUser) {
|
||||||
|
// throw new Error("Failed. Cannot set user's role.");
|
||||||
|
// }
|
||||||
|
|
||||||
|
// if (!resultStaff) {
|
||||||
|
// throw new Error("Failed. Cannot set staff's role.");
|
||||||
|
// }
|
||||||
|
if (typeof userId === "string") {
|
||||||
|
_item.keycloak = userId;
|
||||||
|
}
|
||||||
|
const roleKeycloakUser = await this.roleKeycloakRepo.find({
|
||||||
|
where: { id: "8a1a0dc9-304c-4e5b-a90a-65f841048212" },
|
||||||
|
});
|
||||||
|
const roleKeycloakStaff = await this.roleKeycloakRepo.find({
|
||||||
|
where: { id: "f1fff8db-0795-47c1-9952-f3c18d5b6172" },
|
||||||
|
});
|
||||||
|
if (_item) {
|
||||||
|
_item.roleKeycloaks = Array.from(new Set([...roleKeycloakUser, ...roleKeycloakStaff]));
|
||||||
|
this.profileEmpRepo.save(_item);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return "";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue