From aef313bac7e93574ddea681998304c62328446b8 Mon Sep 17 00:00:00 2001 From: Bright Date: Thu, 30 May 2024 14:16:00 +0700 Subject: [PATCH 1/2] errorMessage --- src/controllers/UserController.ts | 11 +++++++++-- src/keycloak/index.ts | 6 ++++-- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/src/controllers/UserController.ts b/src/controllers/UserController.ts index ff666b5f..39c7a0ea 100644 --- a/src/controllers/UserController.ts +++ b/src/controllers/UserController.ts @@ -88,7 +88,8 @@ export class KeycloakController extends Controller { }); if (typeof userId !== "string") { - throw new Error("ไม่สามารถติดต่อกับระบบจัดการผู้ใช้งานได้"); + // throw new Error("ไม่สามารถติดต่อกับระบบจัดการผู้ใช้งานได้"); + throw new Error(userId.errorMessage); } const now = new Date().toISOString(); @@ -140,9 +141,15 @@ export class KeycloakController extends Controller { firstName?: string; lastName?: string; email?: string; + attributes?: object }, ) { - return await editUser(userId, body); + // return await editUser(userId, body); + const chkUpdate = await editUser(userId, body); + if (typeof chkUpdate !== "boolean") { + throw new Error(chkUpdate.errorMessage); + } + return chkUpdate } @Delete("user/{userId}") diff --git a/src/keycloak/index.ts b/src/keycloak/index.ts index 0ff5edb9..f7825281 100644 --- a/src/keycloak/index.ts +++ b/src/keycloak/index.ts @@ -85,7 +85,8 @@ export async function createUser(username: string, password: string, opts?: Reco if (!res) return false; if (!res.ok) { - return Boolean(console.error("Keycloak Error Response: ", await res.json())); + // return Boolean(console.error("Keycloak Error Response: ", await res.json())); + return await res.json(); } const path = res.headers.get("Location"); @@ -174,7 +175,8 @@ export async function editUser(userId: string, opts: Record) { if (!res) return false; if (!res.ok) { - return Boolean(console.error("Keycloak Error Response: ", await res.json())); + // return Boolean(console.error("Keycloak Error Response: ", await res.json())); + return await res.json(); } const path = res.headers.get("Location"); From 7fdd950c854058ae9a6fb78c9b82791d2f6bd8c1 Mon Sep 17 00:00:00 2001 From: Bright Date: Thu, 30 May 2024 15:24:38 +0700 Subject: [PATCH 2/2] paging getList --- src/controllers/UserController.ts | 4 ++-- src/keycloak/index.ts | 7 ++++--- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/src/controllers/UserController.ts b/src/controllers/UserController.ts index 39c7a0ea..44629fac 100644 --- a/src/controllers/UserController.ts +++ b/src/controllers/UserController.ts @@ -198,8 +198,8 @@ export class KeycloakController extends Controller { } @Get("user") - async getUserList(@Query() search = "") { - const result = await getUserList(search); + async getUserList(@Query() first = "", @Query() max = "" ,@Query() search = "") { + const result = await getUserList(first, max, search); if (Array.isArray(result)) { return result; diff --git a/src/keycloak/index.ts b/src/keycloak/index.ts index f7825281..27317452 100644 --- a/src/keycloak/index.ts +++ b/src/keycloak/index.ts @@ -123,9 +123,10 @@ export async function getUser(userId: string) { * * @returns user list if success, false otherwise. */ -export async function getUserList(search = "") { +export async function getUserList(first = "", max = "", search = "") { const res = await fetch( - `${KC_URL}/admin/realms/${KC_REALM}/users`.concat(!!search ? `?search=${search}` : ""), + // `${KC_URL}/admin/realms/${KC_REALM}/users`.concat(!!search ? `?search=${search}` : ""), + `${KC_URL}/admin/realms/${KC_REALM}/users?first=${first || "0"}&max=${max || "-1"}${search ? `&search=${search}` : ""}`, { // prettier-ignore headers: { @@ -140,7 +141,7 @@ export async function getUserList(search = "") { return Boolean(console.error("Keycloak Error Response: ", await res.json())); } - return ((await res.json()) as any[]).map((v: Record) => ({ + return ((await res.json()) as any[]).slice(0, 5000).map((v: Record) => ({ id: v.id, username: v.username, firstName: v.firstName,