From 856315b7bc5deddb1dff49d8f6dd0db9458ec930 Mon Sep 17 00:00:00 2001 From: kittapath Date: Fri, 14 Mar 2025 13:26:37 +0700 Subject: [PATCH] org root add responsibility --- src/app.ts | 2 +- src/controllers/OrgChild1Controller.ts | 15 +++++----- src/controllers/OrgChild2Controller.ts | 11 ++++---- src/controllers/OrgChild3Controller.ts | 11 ++++---- src/controllers/OrgChild4Controller.ts | 11 ++++---- src/controllers/OrgRootController.ts | 1 + src/controllers/UserController.ts | 29 +++++++++++++++---- src/keycloak/index.ts | 39 +++++++++++++++----------- 8 files changed, 74 insertions(+), 45 deletions(-) diff --git a/src/app.ts b/src/app.ts index ce725fc9..b0652ce4 100644 --- a/src/app.ts +++ b/src/app.ts @@ -30,7 +30,7 @@ async function main() { ); app.use(express.json()); app.use(express.urlencoded({ extended: true })); - // app.use(logMiddleware); + app.use(logMiddleware); app.use("/", express.static("static")); app.use("/api-docs", swaggerUi.serve, swaggerUi.setup(swaggerDocument)); diff --git a/src/controllers/OrgChild1Controller.ts b/src/controllers/OrgChild1Controller.ts index 77e1dfa4..7f3dde58 100644 --- a/src/controllers/OrgChild1Controller.ts +++ b/src/controllers/OrgChild1Controller.ts @@ -54,6 +54,7 @@ export class OrgChild1Controller { } const getOrgChild1 = { orgChild1Id: orgChild1.id, + responsibility: orgChild1.responsibility, orgRootName: orgChild1.orgRoot.orgRootName, orgName: `${orgChild1.orgChild1Name}/${orgChild1.orgRoot.orgRootName}`, orgChild1Name: orgChild1.orgChild1Name, @@ -323,23 +324,23 @@ export class OrgChild1Controller { // if(requestBody.orgChild1ShortName == rootIdExits.orgRootShortName){ // throw new HttpError(HttpStatusCode.NOT_FOUND, "อักษรย่อนี้ซ้ำกับอักษรย่อหน่วยงาน"); // } - const _null:any = null; + const _null: any = null; const before = structuredClone(child1); - + child1.lastUpdateUserId = request.user.sub; child1.lastUpdateFullName = request.user.name; child1.lastUpdatedAt = new Date(); child1.orgRevisionId = String(rootIdExits?.orgRevisionId); child1.orgRootId = String(rootIdExits?.id); child1.responsibility = child1.responsibility || ""; - this.child1Repository.merge(child1,{ + this.child1Repository.merge(child1, { orgChild1Name: requestBody.orgChild1Name, orgChild1ShortName: requestBody.orgChild1ShortName, orgChild1Code: requestBody.orgChild1Code, - DEPARTMENT_CODE: requestBody.DEPARTMENT_CODE != null ? requestBody.DEPARTMENT_CODE : _null, - DIVISION_CODE: requestBody.DIVISION_CODE != null ? requestBody.DIVISION_CODE : _null, - SECTION_CODE: requestBody.SECTION_CODE != null ? requestBody.SECTION_CODE : _null, - JOB_CODE: requestBody.JOB_CODE != null ? requestBody.JOB_CODE : _null, + DEPARTMENT_CODE: requestBody.DEPARTMENT_CODE != null ? requestBody.DEPARTMENT_CODE : _null, + DIVISION_CODE: requestBody.DIVISION_CODE != null ? requestBody.DIVISION_CODE : _null, + SECTION_CODE: requestBody.SECTION_CODE != null ? requestBody.SECTION_CODE : _null, + JOB_CODE: requestBody.JOB_CODE != null ? requestBody.JOB_CODE : _null, isOfficer: requestBody.isOfficer, isInformation: requestBody.isInformation, orgChild1PhoneEx: requestBody.orgChild1PhoneEx, diff --git a/src/controllers/OrgChild2Controller.ts b/src/controllers/OrgChild2Controller.ts index 1246438f..b0ff4b88 100644 --- a/src/controllers/OrgChild2Controller.ts +++ b/src/controllers/OrgChild2Controller.ts @@ -67,6 +67,7 @@ export class OrgChild2Controller extends Controller { } const getOrgChild2 = { orgChild2Id: orgChild2.id, + responsibility: orgChild2.responsibility, orgRootName: orgChild2.orgRoot.orgRootName, orgName: `${orgChild2.orgChild2Name}/${orgChild2.orgChild1.orgChild1Name}/${orgChild2.orgRoot.orgRootName}`, orgChild2Name: orgChild2.orgChild2Name, @@ -230,7 +231,7 @@ export class OrgChild2Controller extends Controller { if (!child2) { throw new HttpError(HttpStatusCode.NOT_FOUND, "not found."); } - const _null:any = null; + const _null: any = null; const before = structuredClone(child2); child2.lastUpdateUserId = request.user.sub; child2.lastUpdateFullName = request.user.name; @@ -244,10 +245,10 @@ export class OrgChild2Controller extends Controller { orgChild2Code: requestBody.orgChild2Code, orgChild2Rank: requestBody.orgChild2Rank != null ? requestBody.orgChild2Rank : _null, orgChild2RankSub: requestBody.orgChild2RankSub, - DEPARTMENT_CODE: requestBody.DEPARTMENT_CODE != null ? requestBody.DEPARTMENT_CODE : _null, - DIVISION_CODE: requestBody.DIVISION_CODE != null ? requestBody.DIVISION_CODE : _null, - SECTION_CODE: requestBody.SECTION_CODE != null ? requestBody.SECTION_CODE : _null, - JOB_CODE: requestBody.JOB_CODE != null ? requestBody.JOB_CODE : _null, + DEPARTMENT_CODE: requestBody.DEPARTMENT_CODE != null ? requestBody.DEPARTMENT_CODE : _null, + DIVISION_CODE: requestBody.DIVISION_CODE != null ? requestBody.DIVISION_CODE : _null, + SECTION_CODE: requestBody.SECTION_CODE != null ? requestBody.SECTION_CODE : _null, + JOB_CODE: requestBody.JOB_CODE != null ? requestBody.JOB_CODE : _null, orgChild2PhoneEx: requestBody.orgChild2PhoneEx, orgChild2PhoneIn: requestBody.orgChild2PhoneIn, orgChild2Fax: requestBody.orgChild2Fax, diff --git a/src/controllers/OrgChild3Controller.ts b/src/controllers/OrgChild3Controller.ts index 0dbf6071..3ba365ae 100644 --- a/src/controllers/OrgChild3Controller.ts +++ b/src/controllers/OrgChild3Controller.ts @@ -51,6 +51,7 @@ export class OrgChild3Controller { } const getOrgChild3 = { orgChild3Id: orgChild3.id, + responsibility: orgChild3.responsibility, orgRootName: orgChild3.orgRoot.orgRootName, orgName: `${orgChild3.orgChild3Name}/${orgChild3.orgChild2.orgChild2Name}/${orgChild3.orgChild1.orgChild1Name}/${orgChild3.orgRoot.orgRootName}`, orgChild3Name: orgChild3.orgChild3Name, @@ -184,7 +185,7 @@ export class OrgChild3Controller { if (!child3) { throw new HttpError(HttpStatusCode.NOT_FOUND, "not found."); } - const _null:any = null; + const _null: any = null; const before = structuredClone(child3); child3.lastUpdateUserId = request.user.sub; child3.lastUpdateFullName = request.user.name; @@ -199,10 +200,10 @@ export class OrgChild3Controller { orgChild3Code: requestBody.orgChild3Code, orgChild3Rank: requestBody.orgChild3Rank != null ? requestBody.orgChild3Rank : _null, orgChild3RankSub: requestBody.orgChild3RankSub, - DEPARTMENT_CODE: requestBody.DEPARTMENT_CODE != null ? requestBody.DEPARTMENT_CODE : _null, - DIVISION_CODE: requestBody.DIVISION_CODE != null ? requestBody.DIVISION_CODE : _null, - SECTION_CODE: requestBody.SECTION_CODE != null ? requestBody.SECTION_CODE : _null, - JOB_CODE: requestBody.JOB_CODE != null ? requestBody.JOB_CODE : _null, + DEPARTMENT_CODE: requestBody.DEPARTMENT_CODE != null ? requestBody.DEPARTMENT_CODE : _null, + DIVISION_CODE: requestBody.DIVISION_CODE != null ? requestBody.DIVISION_CODE : _null, + SECTION_CODE: requestBody.SECTION_CODE != null ? requestBody.SECTION_CODE : _null, + JOB_CODE: requestBody.JOB_CODE != null ? requestBody.JOB_CODE : _null, orgChild3PhoneEx: requestBody.orgChild3PhoneEx, orgChild3PhoneIn: requestBody.orgChild3PhoneIn, orgChild3Fax: requestBody.orgChild3Fax, diff --git a/src/controllers/OrgChild4Controller.ts b/src/controllers/OrgChild4Controller.ts index f183415e..a43b7234 100644 --- a/src/controllers/OrgChild4Controller.ts +++ b/src/controllers/OrgChild4Controller.ts @@ -64,6 +64,7 @@ export class OrgChild4Controller extends Controller { } const getOrgChild4 = { orgChild4Id: orgChild4.id, + responsibility: orgChild4.responsibility, orgRootName: orgChild4.orgRoot.orgRootName, orgName: `${orgChild4.orgChild4Name}/${orgChild4.orgChild3.orgChild3Name}/${orgChild4.orgChild2.orgChild2Name}/${orgChild4.orgChild1.orgChild1Name}/${orgChild4.orgRoot.orgRootName}`, orgChild4Name: orgChild4.orgChild4Name, @@ -230,7 +231,7 @@ export class OrgChild4Controller extends Controller { if (!child4) { throw new HttpError(HttpStatusCode.NOT_FOUND, "not found."); } - const _null:any = null; + const _null: any = null; const before = structuredClone(child4); child4.lastUpdateUserId = request.user.sub; child4.lastUpdateFullName = request.user.name; @@ -246,10 +247,10 @@ export class OrgChild4Controller extends Controller { orgChild4Code: requestBody.orgChild4Code, orgChild4Rank: requestBody.orgChild4Rank != null ? requestBody.orgChild4Rank : _null, orgChild4RankSub: requestBody.orgChild4RankSub, - DEPARTMENT_CODE: requestBody.DEPARTMENT_CODE != null ? requestBody.DEPARTMENT_CODE : _null, - DIVISION_CODE: requestBody.DIVISION_CODE != null ? requestBody.DIVISION_CODE : _null, - SECTION_CODE: requestBody.SECTION_CODE != null ? requestBody.SECTION_CODE : _null, - JOB_CODE: requestBody.JOB_CODE != null ? requestBody.JOB_CODE : _null, + DEPARTMENT_CODE: requestBody.DEPARTMENT_CODE != null ? requestBody.DEPARTMENT_CODE : _null, + DIVISION_CODE: requestBody.DIVISION_CODE != null ? requestBody.DIVISION_CODE : _null, + SECTION_CODE: requestBody.SECTION_CODE != null ? requestBody.SECTION_CODE : _null, + JOB_CODE: requestBody.JOB_CODE != null ? requestBody.JOB_CODE : _null, orgChild4PhoneEx: requestBody.orgChild4PhoneEx, orgChild4PhoneIn: requestBody.orgChild4PhoneIn, orgChild4Fax: requestBody.orgChild4Fax, diff --git a/src/controllers/OrgRootController.ts b/src/controllers/OrgRootController.ts index ae27a5ed..9af60c20 100644 --- a/src/controllers/OrgRootController.ts +++ b/src/controllers/OrgRootController.ts @@ -64,6 +64,7 @@ export class OrgRootController extends Controller { } const getOrgRoot = { orgRootId: orgRoot.id, + responsibility: orgRoot.responsibility, orgRootName: orgRoot.orgRootName, orgName: "-", orgRootShortName: orgRoot.orgRootShortName, diff --git a/src/controllers/UserController.ts b/src/controllers/UserController.ts index dc8d601d..91834c26 100644 --- a/src/controllers/UserController.ts +++ b/src/controllers/UserController.ts @@ -39,7 +39,7 @@ import { Profile } from "../entities/Profile"; import { ProfileEmployee } from "../entities/ProfileEmployee"; import { RequestWithUser } from "../middlewares/user"; import HttpSuccess from "../interfaces/http-success"; -import { Brackets, In, IsNull } from "typeorm"; +import { Brackets, In, IsNull, Not } from "typeorm"; import HttpError from "../interfaces/http-error"; import HttpStatus from "../interfaces/http-status"; import { RoleKeycloak } from "../entities/RoleKeycloak"; @@ -830,7 +830,7 @@ export class KeycloakController extends Controller { return result; } - @Post("user/asdasdasd") + @Post("user/create") @Security("bearerAuth", ["system", "admin"]) async createUserImport( @Request() request: { user: { sub: string; preferred_username: string } }, @@ -872,7 +872,7 @@ export class KeycloakController extends Controller { 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"), + list.filter((v) => v.id == "f8619dc2-dc0d-4aab-957f-66bdf905e9d0"), ); if (!result) { @@ -882,13 +882,32 @@ export class KeycloakController extends Controller { _item.keycloak = userId; } const roleKeycloak = await this.roleKeycloakRepo.find({ - where: { id: "8a1a0dc9-304c-4e5b-a90a-65f841048212" }, + where: { id: "f8619dc2-dc0d-4aab-957f-66bdf905e9d0" }, }); if (_item) { _item.roleKeycloaks = Array.from(new Set([..._item.roleKeycloaks, ...roleKeycloak])); this.profileRepo.save(_item); } } - return "555+"; + return ""; + } + + @Post("user/change-password-all") + async changeUserPasswordAll( + @Request() request: { user: { sub: string; preferred_username: string } }, + ) { + const profiles = await this.profileRepo.find({ + where: { + keycloak: Not(IsNull()), + }, + }); + + for await (const _item of profiles) { + const result = await changeUserPassword(_item.keycloak, "P@ssw0rd"); + if (!result) { + continue; + } + } + return; } } diff --git a/src/keycloak/index.ts b/src/keycloak/index.ts index 1297b70f..fc971898 100644 --- a/src/keycloak/index.ts +++ b/src/keycloak/index.ts @@ -4,8 +4,8 @@ const KC_URL = process.env.KC_URL; const KC_REALMS = process.env.KC_REALMS; const KC_CLIENT_ID = process.env.KC_SERVICE_ACCOUNT_CLIENT_ID; const KC_SECRET = process.env.KC_SERVICE_ACCOUNT_SECRET; -const AUTH_ACCOUNT_SECRET = process.env.AUTH_ACCOUNT_SECRET -const API_KEY = process.env.API_KEY +const AUTH_ACCOUNT_SECRET = process.env.AUTH_ACCOUNT_SECRET; +const API_KEY = process.env.API_KEY; let token: string | null = null; let decoded: DecodedJwt | null = null; @@ -742,7 +742,6 @@ export async function removeUserGroup(userId: string, groupId: string) { // Function to change user password export async function changeUserPassword(userId: string, newPassword: string) { try { - console.log(await getToken()); const res = await fetch(`${KC_URL}/admin/realms/${KC_REALMS}/users/${userId}/reset-password`, { // prettier-ignore headers: { @@ -788,27 +787,33 @@ export async function resetPassword(username: string) { // const tokenData = await tokenResponse.json(); // const adminToken = tokenData.access_token; - const users = await fetch(`${KC_URL}/admin/realms/${KC_REALMS}/users?email=${encodeURIComponent(username)}`, { - headers: { - "authorization": `Bearer ${await getToken()}`, + const users = await fetch( + `${KC_URL}/admin/realms/${KC_REALMS}/users?email=${encodeURIComponent(username)}`, + { + headers: { + authorization: `Bearer ${await getToken()}`, // "authorization": `Bearer ${adminToken}`, "content-type": `application/json`, }, - }); - if(!users.ok) { + }, + ); + if (!users.ok) { return false; } const usersData = await users.json(); const userId = usersData[0].id; - const resetResponse = await fetch(`${KC_URL}/admin/realms/${KC_REALMS}/users/${userId}/execute-actions-email`, { - method: "PUT", - headers: { - "Authorization": `Bearer ${await getToken()}`, - // "Authorization": `Bearer ${adminToken}`, - "Content-Type": "application/json" + const resetResponse = await fetch( + `${KC_URL}/admin/realms/${KC_REALMS}/users/${userId}/execute-actions-email`, + { + method: "PUT", + headers: { + Authorization: `Bearer ${await getToken()}`, + // "Authorization": `Bearer ${adminToken}`, + "Content-Type": "application/json", + }, + body: JSON.stringify(["UPDATE_PASSWORD"]), }, - body: JSON.stringify(["UPDATE_PASSWORD"]) - }); + ); if (!resetResponse.ok) { return false; } @@ -817,4 +822,4 @@ export async function resetPassword(username: string) { console.error("Error triggering password reset:", error); return false; } -} \ No newline at end of file +}