diff --git a/src/controllers/KpiCapacityController.ts b/src/controllers/KpiCapacityController.ts index 0a8e391..7fc7146 100644 --- a/src/controllers/KpiCapacityController.ts +++ b/src/controllers/KpiCapacityController.ts @@ -348,18 +348,29 @@ export class kpiCapacityController extends Controller { @Query("pageSize") pageSize: number = 10, @Query("type") type?: string, @Query("keyword") keyword?: string, + @Query("sortBy") sortBy?: string, + @Query("descending") descending?: boolean, ) { let _data = await new permission().PermissionList(request, "SYS_EVA_COMPETENCY"); - const [kpiCapacity, total] = await AppDataSource.getRepository(KpiCapacity) + let query = await AppDataSource.getRepository(KpiCapacity) .createQueryBuilder("kpiCapacity") .leftJoinAndSelect("kpiCapacity.kpiCapacityDetails", "kpiCapacityDetail") .andWhere(keyword == undefined ? "1=1" : [{ name: Like(`%${keyword}%`) }]) .andWhere(type == undefined ? "1=1" : { type: type }) .orderBy("kpiCapacity.createdAt", "ASC") + + if (sortBy) { + query = query.orderBy( + `kpiCapacity.${sortBy}`, + descending ? "DESC" : "ASC" + ); + } + + const [kpiCapacity, total] = await query .skip((page - 1) * pageSize) .take(pageSize) .getManyAndCount(); - + const mapFormula = kpiCapacity.map((item) => ({ id: item.id, type: item.type, diff --git a/src/controllers/KpiGroupController.ts b/src/controllers/KpiGroupController.ts index a0b82e1..821be9f 100644 --- a/src/controllers/KpiGroupController.ts +++ b/src/controllers/KpiGroupController.ts @@ -145,9 +145,12 @@ export class kpiGroupController extends Controller { @Query("page") page: number = 1, @Query("pageSize") pageSize: number = 10, @Query("keyword") keyword?: string, + @Query("sortBy") sortBy?: string, + @Query("descending") descending?: boolean, ) { let _data = await new permission().PermissionList(request, "SYS_EVA_COMPETENCY"); let whereClause: any = {}; + let sort: any = {}; if (keyword !== undefined && keyword !== "") { whereClause = { @@ -155,12 +158,23 @@ export class kpiGroupController extends Controller { }; } + if(sortBy){ + sort = { + order: {[sortBy]: descending ? "DESC" : "ASC"} + } + }else{ + sort = { + order: { createdAt: "ASC" }, + } + } + const [kpiGroup, total] = await this.kpiGroupRepository.findAndCount({ ...whereClause, ...(keyword ? {} : { skip: (page - 1) * pageSize, take: pageSize }), - order: { createdAt: "ASC" }, + ...sort }); + return new HttpSuccess({ data: kpiGroup, total }); } diff --git a/src/controllers/KpiLinkController.ts b/src/controllers/KpiLinkController.ts index ffb71f9..785807f 100644 --- a/src/controllers/KpiLinkController.ts +++ b/src/controllers/KpiLinkController.ts @@ -230,6 +230,8 @@ export class kpiLinkController extends Controller { @Query("page") page: number = 1, @Query("pageSize") pageSize: number = 10, @Query("keyword") keyword?: string, + @Query("sortBy") sortBy?: string, + @Query("descending") descending?: boolean, ) { let _data = await new permission().PermissionList(request, "SYS_EVA_COMPETENCY"); const [_kpiLink, _total] = await AppDataSource.getRepository(KpiLink) @@ -253,7 +255,7 @@ export class kpiLinkController extends Controller { }); } - const [kpiLink, total] = await AppDataSource.getRepository(KpiLink) + let query = await AppDataSource.getRepository(KpiLink) .createQueryBuilder("kpiLink") .leftJoinAndSelect("kpiLink.kpiGroup", "kpiGroup") .leftJoinAndSelect("kpiLink.positions", "positions") @@ -261,9 +263,20 @@ export class kpiLinkController extends Controller { .andWhere("kpiLink.id In (:id)", { id: _kpiLink.map((x) => x.id), }) + .orderBy("kpiLink.createdAt", "ASC") + + if (sortBy) { + if(sortBy == 'groupName'){ + query = query.orderBy( + `kpiGroup.nameGroupKPI`, + descending ? "DESC" : "ASC" + ); + } + } + + const [kpiLink, total] = await query .skip((page - 1) * pageSize) .take(pageSize) - .orderBy("kpiLink.createdAt", "ASC") .getManyAndCount(); if (_total === 0) { diff --git a/src/controllers/KpiPlanController.ts b/src/controllers/KpiPlanController.ts index cfdcd88..12951e1 100644 --- a/src/controllers/KpiPlanController.ts +++ b/src/controllers/KpiPlanController.ts @@ -792,6 +792,8 @@ export class kpiPlanController extends Controller { keyword?: string | null; isAll?: boolean | false; // isNull?: boolean | false; + sortBy?: string, + descending?: boolean, }, ) { let _data = await new permission().PermissionList(request, "SYS_EVA_INDICATOR"); @@ -842,7 +844,7 @@ export class kpiPlanController extends Controller { } parameters.nodeId = `%${requestBody.nodeId}%`; } - const [kpiPlan, total] = await AppDataSource.getRepository(KpiPlan) + let query = await AppDataSource.getRepository(KpiPlan) .createQueryBuilder("kpiPlan") .leftJoinAndSelect("kpiPlan.kpiPeriod", "kpiPeriod") .andWhere(condition, parameters) @@ -877,6 +879,15 @@ export class kpiPlanController extends Controller { "kpiPlan.createdAt", ]) .orderBy("kpiPlan.createdAt", "DESC") + + if (requestBody.sortBy) { + query = query.orderBy( + `kpiPlan.${requestBody.sortBy}`, + requestBody.descending ? "DESC" : "ASC" + ); + } + + const [kpiPlan, total] = await query .skip((requestBody.page - 1) * requestBody.pageSize) .take(requestBody.pageSize) .getManyAndCount(); diff --git a/src/controllers/KpiRoleController.ts b/src/controllers/KpiRoleController.ts index 78a4aa2..4e49aec 100644 --- a/src/controllers/KpiRoleController.ts +++ b/src/controllers/KpiRoleController.ts @@ -728,6 +728,8 @@ export class kpiRoleController extends Controller { keyword?: string | null; isAll?: boolean | false; // isNull?: boolean | false; + sortBy?: string, + descending?: boolean, }, ) { let _data = await new permission().PermissionList(request, "SYS_EVA_INDICATOR"); @@ -783,7 +785,7 @@ export class kpiRoleController extends Controller { // parameters.year = `%${requestBody.year}%`; // parameters.period = `%${requestBody.period}%`; // } - const [kpiRole, total] = await AppDataSource.getRepository(KpiRole) + let query = await AppDataSource.getRepository(KpiRole) .createQueryBuilder("kpiRole") .leftJoinAndSelect("kpiRole.kpiPeriod", "kpiPeriod") .andWhere(condition, parameters) @@ -830,10 +832,20 @@ export class kpiRoleController extends Controller { "kpiRole.createdAt", ]) .orderBy("kpiRole.createdAt", "DESC") + + if (requestBody.sortBy) { + query = query.orderBy( + `kpiRole.${requestBody.sortBy}`, + requestBody.descending ? "DESC" : "ASC" + ); + } + + const [kpiRole, total] = await query .skip((requestBody.page - 1) * requestBody.pageSize) .take(requestBody.pageSize) .getManyAndCount(); + return new HttpSuccess({ data: kpiRole, total }); } diff --git a/src/controllers/KpiSpecialController.ts b/src/controllers/KpiSpecialController.ts index 279190e..04d63ca 100644 --- a/src/controllers/KpiSpecialController.ts +++ b/src/controllers/KpiSpecialController.ts @@ -330,6 +330,8 @@ export class kpiSpecialController extends Controller { year?: string | null; period?: string | null; keyword?: string | null; + sortBy?: string, + descending?: boolean, }, ) { let _data = await new permission().PermissionList(request, "SYS_EVA_INDICATOR"); @@ -345,7 +347,7 @@ export class kpiSpecialController extends Controller { // }; // } - const [kpiSpecial, total] = await AppDataSource.getRepository(KpiSpecial) + let query = await AppDataSource.getRepository(KpiSpecial) .createQueryBuilder("kpiSpecial") // .andWhere(condition) .andWhere(requestBody.year ? "kpiSpecial.year LIKE :year" : "1=1", { @@ -372,6 +374,15 @@ export class kpiSpecialController extends Controller { "kpiSpecial.createdAt", ]) .orderBy("kpiSpecial.createdAt", "DESC") + + if (requestBody.sortBy) { + query = query.orderBy( + `kpiSpecial.${requestBody.sortBy}`, + requestBody.descending ? "DESC" : "ASC" + ); + } + + const [kpiSpecial, total] = await query .skip((requestBody.page - 1) * requestBody.pageSize) .take(requestBody.pageSize) .getManyAndCount();