From 91281ba995b4719b2dda57225fa29a90e939da34 Mon Sep 17 00:00:00 2001 From: mamoss <> Date: Fri, 26 Sep 2025 23:42:26 +0700 Subject: [PATCH] add-role-staff/user --- src/controllers/UserController.ts | 105 +++++++++++++++++++++++++++--- 1 file changed, 97 insertions(+), 8 deletions(-) diff --git a/src/controllers/UserController.ts b/src/controllers/UserController.ts index e808a2be..8d77b1ec 100644 --- a/src/controllers/UserController.ts +++ b/src/controllers/UserController.ts @@ -852,10 +852,15 @@ export class KeycloakController extends Controller { // _item.birthDate.toISOString().slice(5, 7) + // gregorianYear; // password = formattedDate; - 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 _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 = ""; @@ -918,10 +923,15 @@ export class KeycloakController extends Controller { // _item.birthDate.toISOString().slice(5, 7) + // gregorianYear; // password = formattedDate; - 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 _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 = ""; @@ -990,4 +1000,83 @@ export class KeycloakController extends Controller { } return; } + + @Post("add-role-staff/user/{child1Id}") + @Security("bearerAuth", ["system", "admin"]) + async addroleStaffToUser( + @Path() child1Id: string, + @Request() request: { user: { sub: string; preferred_username: string } }, + ) { + const profiles = await this.profileRepo.find({ + where: { + keycloak: Not(IsNull()), + 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.profileRepo.save(_item); + } + } + return ""; + } }