From 3cad6dbf25743550e99aaadf2ec8c16ca729d038 Mon Sep 17 00:00:00 2001 From: AdisakKanthawilang Date: Fri, 12 Sep 2025 10:09:48 +0700 Subject: [PATCH] sort org/profile/edit/user --- src/controllers/ProfileEditController.ts | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/src/controllers/ProfileEditController.ts b/src/controllers/ProfileEditController.ts index 5d257956..9e90d294 100644 --- a/src/controllers/ProfileEditController.ts +++ b/src/controllers/ProfileEditController.ts @@ -39,13 +39,15 @@ export class ProfileEditController extends Controller { @Query("pageSize") pageSize: number = 10, @Query("keyword") keyword: string = "", @Query("status") status: string = "", + @Query("sortBy") sortBy?: string, + @Query("descending") descending?: boolean, ) { const profile = await this.profileRepo.findOneBy({ keycloak: request.user.sub }); if (!profile) { throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว"); } - let [getProfileEdit, total] = await AppDataSource.getRepository(ProfileEdit) + let query = await AppDataSource.getRepository(ProfileEdit) .createQueryBuilder("ProfileEdit") .leftJoinAndSelect("ProfileEdit.profile", "profile") .where((qb) => { @@ -74,9 +76,18 @@ export class ProfileEditController extends Controller { }), ) .orderBy("ProfileEdit.createdAt", "DESC") - .skip((page - 1) * pageSize) - .take(pageSize) - .getManyAndCount(); + + if (sortBy) { + query = query.orderBy( + `ProfileEdit.${sortBy}`, + descending ? "DESC" : "ASC" + ); + } + + const [getProfileEdit, total] = await query + .skip((page - 1) * pageSize) + .take(pageSize) + .getManyAndCount(); const _data = getProfileEdit.map((item) => ({ id: item.id,