diff --git a/src/controllers/ProfileController.ts b/src/controllers/ProfileController.ts index c8a0e3c3..33d33227 100644 --- a/src/controllers/ProfileController.ts +++ b/src/controllers/ProfileController.ts @@ -8567,6 +8567,8 @@ export class ProfileController extends Controller { keyword?: string; system?: string; }, + @Query("sortBy") sortBy?: string, + @Query("descending") descending?: boolean ) { // ค้นหารายชื่อถ้าไม่ส่ง system มาให้ default ตามทะเบียนประวัติ let _system: string = "SYS_REGISTRY_OFFICER"; @@ -8638,7 +8640,7 @@ export class ProfileController extends Controller { break; } - const [findProfile, total] = await this.profileRepo + let query = await this.profileRepo .createQueryBuilder("profile") .leftJoinAndSelect("profile.posType", "posType") .leftJoinAndSelect("profile.posLevel", "posLevel") @@ -8694,6 +8696,27 @@ export class ProfileController extends Controller { qb.orWhere(body.keyword ? queryLike : "1=1", { keyword: `%${body.keyword}%` }); }), ) + + if (sortBy) { + if(sortBy === "name"){ + query = query + .orderBy(`profile.prefix`,descending ? "DESC" : "ASC") + .addOrderBy(`profile.firstName`,descending ? "DESC" : "ASC") + .addOrderBy(`profile.lastName`,descending ? "DESC" : "ASC") + }else if(sortBy === "organization"){ + query = query.orderBy( + `orgRoot.orgRootName`, + descending ? "DESC" : "ASC" + ); + }else{ + query = query.orderBy( + `profile.${sortBy}`, + descending ? "DESC" : "ASC" + ); + } + } + + const [findProfile, total] = await query .skip((page - 1) * pageSize) .take(pageSize) .getManyAndCount();