create user emp
This commit is contained in:
parent
dde45bfcd7
commit
3dc653bfb0
3 changed files with 190 additions and 107 deletions
|
|
@ -892,6 +892,68 @@ export class KeycloakController extends Controller {
|
|||
return "";
|
||||
}
|
||||
|
||||
@Post("user-emp/create")
|
||||
@Security("bearerAuth", ["system", "admin"])
|
||||
async createUserImportEmp(
|
||||
@Request() request: { user: { sub: string; preferred_username: string } },
|
||||
) {
|
||||
const profiles = await this.profileEmpRepo.find({
|
||||
where: {
|
||||
keycloak: IsNull(),
|
||||
},
|
||||
relations: ["roleKeycloaks"],
|
||||
});
|
||||
|
||||
for await (const _item of profiles) {
|
||||
let password = _item.citizenId;
|
||||
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 checkUser = await getUserByUsername(_item.citizenId);
|
||||
let userId: any = "";
|
||||
if (checkUser.length == 0) {
|
||||
userId = await createUser(_item.citizenId, password, {
|
||||
firstName: _item.firstName,
|
||||
lastName: _item.lastName,
|
||||
// email: _item.email,
|
||||
});
|
||||
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 result = await addUserRoles(
|
||||
userId,
|
||||
list.filter((v) => v.id == "8a1a0dc9-304c-4e5b-a90a-65f841048212"),
|
||||
);
|
||||
|
||||
if (!result) {
|
||||
throw new Error("Failed. Cannot set user's role.");
|
||||
}
|
||||
if (typeof userId === "string") {
|
||||
_item.keycloak = userId;
|
||||
}
|
||||
const roleKeycloak = await this.roleKeycloakRepo.find({
|
||||
where: { id: "8a1a0dc9-304c-4e5b-a90a-65f841048212" },
|
||||
});
|
||||
if (_item) {
|
||||
_item.roleKeycloaks = Array.from(new Set([..._item.roleKeycloaks, ...roleKeycloak]));
|
||||
this.profileEmpRepo.save(_item);
|
||||
}
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
@Post("user/change-password-all")
|
||||
async changeUserPasswordAll(
|
||||
@Request() request: { user: { sub: string; preferred_username: string } },
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue