From 58e44de196abaed7b3a63e69870032c497805449 Mon Sep 17 00:00:00 2001 From: Adisak Date: Tue, 30 Sep 2025 13:52:48 +0700 Subject: [PATCH] sort org command --- src/controllers/ProfileController.ts | 32 +++++++++++++++++++++++----- 1 file changed, 27 insertions(+), 5 deletions(-) diff --git a/src/controllers/ProfileController.ts b/src/controllers/ProfileController.ts index e9ead8e5..c8a0e3c3 100644 --- a/src/controllers/ProfileController.ts +++ b/src/controllers/ProfileController.ts @@ -2585,6 +2585,8 @@ export class ProfileController extends Controller { keyword: string; page: number; pageSize: number; + sortBy?: string; + descending?: boolean; }, ) { let posMaster = await this.posMasterRepo.findOne({ @@ -2672,7 +2674,7 @@ export class ProfileController extends Controller { // condition.isDirector = true; // // conditionNow.isDirector = true; // } - const [lists, total] = await AppDataSource.getRepository(viewDirectorActing) + let query = await AppDataSource.getRepository(viewDirectorActing) .createQueryBuilder("viewDirectorActing") // .andWhere(condition) .andWhere( @@ -2724,9 +2726,19 @@ export class ProfileController extends Controller { ); }), ) - .skip((body.page - 1) * body.pageSize) - .take(body.pageSize) - .getManyAndCount(); + + if (body.sortBy) { + query = query.orderBy( + `viewDirectorActing.${body.sortBy}`, + body.descending ? "DESC" : "ASC" + ); + } + + const [lists, total] = await query + .skip((body.page - 1) * body.pageSize) + .take(body.pageSize) + .getManyAndCount(); + return new HttpSuccess({ data: lists, total }); } else { // const posMaster = await this.posMasterRepo.findOne({ @@ -2764,7 +2776,7 @@ export class ProfileController extends Controller { // condition.isDirector = true; // // conditionNow.isDirector = true; // } - const [lists, total] = await AppDataSource.getRepository(viewDirector) + let query = await AppDataSource.getRepository(viewDirector) .createQueryBuilder("viewDirector") // .andWhere(condition) .andWhere( @@ -2816,9 +2828,19 @@ export class ProfileController extends Controller { ); }), ) + + if (body.sortBy) { + query = query.orderBy( + `viewDirector.${body.sortBy}`, + body.descending ? "DESC" : "ASC" + ); + } + + const [lists, total] = await query .skip((body.page - 1) * body.pageSize) .take(body.pageSize) .getManyAndCount(); + return new HttpSuccess({ data: lists, total }); } }