From a532fcf23d4dffc9e704dd06e716c4910f1603de Mon Sep 17 00:00:00 2001 From: "DESKTOP-1R2VSQH\\Lenovo ThinkPad E490" Date: Wed, 6 May 2026 16:38:56 +0700 Subject: [PATCH] refactor(ProfileController): add subquery to sortBy commandNo for Post probation API --- src/controllers/ProfileController.ts | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/controllers/ProfileController.ts b/src/controllers/ProfileController.ts index dbbecb80..c527f404 100644 --- a/src/controllers/ProfileController.ts +++ b/src/controllers/ProfileController.ts @@ -10049,7 +10049,19 @@ export class ProfileController extends Controller { } else if (body.sortBy === "posTypeName") { query = query.orderBy(`posType.posTypeName`, body.descending ? "DESC" : "ASC"); } else if (body.sortBy === "commandNo") { - query = query.orderBy(`profileSalary.commandNo`, body.descending ? "DESC" : "ASC"); + // Use subquery to get the latest commandNo for each profile + const subquery = AppDataSource.getRepository(ProfileSalary) + .createQueryBuilder("ps") + .select("ps.commandNo", "commandNo") + .where("ps.profileId = profile.id") + .orderBy("ps.order", "DESC") + .addOrderBy("ps.commandNo", "DESC") + .limit(1); + + query = query + .addSelect(`(${subquery.getSql()})`, "latestCommandNo") + .orderBy("latestCommandNo", body.descending ? "DESC" : "ASC") + .addOrderBy("profile.id", "ASC"); // Secondary sort for consistency } else if (body.sortBy === "orgRootName") { query = query.orderBy(`orgRoot.orgRootName`, body.descending ? "DESC" : "ASC"); } else {