From cbeac798ddeaf63bafe9e72804860536e78d3748 Mon Sep 17 00:00:00 2001 From: AdisakKanthawilang Date: Fri, 12 Sep 2025 13:32:08 +0700 Subject: [PATCH] sort api/v1/evaluation/user --- src/controllers/EvaluationController.ts | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/controllers/EvaluationController.ts b/src/controllers/EvaluationController.ts index d418233..1d77bf9 100644 --- a/src/controllers/EvaluationController.ts +++ b/src/controllers/EvaluationController.ts @@ -269,10 +269,12 @@ export class EvaluationController { pageSize: number; keyword?: string; status?: string[]; + sortBy?: string, + descending?: boolean, }, ) { try { - const [evaluation, total] = await AppDataSource.getRepository(Evaluation) + let query = await AppDataSource.getRepository(Evaluation) .createQueryBuilder("evaluation") .andWhere( new Brackets((qb) => { @@ -293,6 +295,15 @@ export class EvaluationController { }), ) .orderBy("evaluation.lastUpdatedAt", "DESC") + + if (body.sortBy) { + query = query.orderBy( + `evaluation.${body.sortBy}`, + body.descending ? "DESC" : "ASC" + ); + } + + const [evaluation, total] = await query .skip((body.page - 1) * body.pageSize) .take(body.pageSize) .getManyAndCount();