diff --git a/src/controllers/KpiSpecialController.ts b/src/controllers/KpiSpecialController.ts index fffd3b6..6b1ae48 100644 --- a/src/controllers/KpiSpecialController.ts +++ b/src/controllers/KpiSpecialController.ts @@ -45,12 +45,12 @@ export class kpiSpecialController extends Controller { @Request() request: { user: Record }, ) { const chk_kpiSpecial = await this.kpiSpecialRepository.findOne({ - where: { + where: { including: String(requestBody.including), includingName: String(requestBody.includingName), }, }); - if(chk_kpiSpecial){ + if (chk_kpiSpecial) { throw new HttpError( HttpStatusCode.CONFLICT, "ไม่สามารถเพิ่มข้อมูลได้เนื่องจากข้อมูลตัวชี้วัดซ้ำ", @@ -88,13 +88,13 @@ export class kpiSpecialController extends Controller { } const chk_kpiSpecial = await this.kpiSpecialRepository.findOne({ - where: { + where: { id: Not(id), including: String(requestBody.including), includingName: String(requestBody.includingName), }, }); - if(chk_kpiSpecial){ + if (chk_kpiSpecial) { throw new HttpError( HttpStatusCode.CONFLICT, "ไม่สามารถเพิ่มข้อมูลได้เนื่องจากข้อมูลตัวชี้วัดซ้ำ", @@ -114,7 +114,7 @@ export class kpiSpecialController extends Controller { @Get("{id}") async GetKpiSpecialById(@Path() id: string) { const KpiSpecial = await this.kpiSpecialRepository.findOne({ - where: { id: id } + where: { id: id }, }); if (!KpiSpecial) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลตัวชี้วัด Specialนี้"); @@ -151,50 +151,61 @@ export class kpiSpecialController extends Controller { * @param pageSize * @param keyword */ - @Get() + @Post("search") async listKpiSpecial( - @Query("page") page: number = 1, - @Query("pageSize") pageSize: number = 10, - @Query("keyword") keyword?: string, + @Body() + requestBody: { + page: number; + pageSize: number; + year?: string | null; + period?: string | null; + keyword?: string | null; + }, ) { - let whereClause: any = {}; - if (keyword !== undefined && keyword !== "") { - whereClause = { - where: [{ - including: Like(`%${keyword}%`), - includingName: Like(`%${keyword}%`), - }], + let condition: any = {}; + if (requestBody.keyword !== undefined && requestBody.keyword !== "") { + condition = { + where: [ + { + including: Like(`%${requestBody.keyword}%`), + includingName: Like(`%${requestBody.keyword}%`), + }, + ], }; } - const [kpiSpecial, total] = await this.kpiSpecialRepository.findAndCount({ - ...whereClause, - ...(keyword ? {} : { skip: (page - 1) * pageSize, take: pageSize }), - order: { createdAt: "ASC" }, - }); - const mapData = kpiSpecial.map((KpiSpecial) => ({ - id: KpiSpecial.id, - period: KpiSpecial.period, - year: KpiSpecial.year, - including: KpiSpecial.including, - includingName: KpiSpecial.includingName, - target: KpiSpecial.target, - unit: KpiSpecial.unit, - weight: KpiSpecial.weight, - point: KpiSpecial.point, - summary: KpiSpecial.summary, - documentInfoEvidence: KpiSpecial.documentInfoEvidence, - startDate: KpiSpecial.startDate, - endDate: KpiSpecial.endDate, - achievement1: KpiSpecial.achievement1, - achievement2: KpiSpecial.achievement2, - achievement3: KpiSpecial.achievement3, - achievement4: KpiSpecial.achievement4, - achievement5: KpiSpecial.achievement5, - meaning: KpiSpecial.meaning, - formula: KpiSpecial.formula, - })); - return new HttpSuccess({ data: mapData, total }); + const [kpiSpecial, total] = await AppDataSource.getRepository(KpiSpecial) + .createQueryBuilder("kpiSpecial") + .andWhere(condition) + .andWhere(requestBody.year ? "kpiSpecial.year LIKE :year" : "1=1", { + year: `%${requestBody.year}%`, + }) + .andWhere(requestBody.period ? "kpiSpecial.period LIKE :period" : "1=1", { + period: `%${requestBody.period}%`, + }) + .andWhere( + new Brackets((qb) => { + qb.orWhere("kpiSpecial.including LIKE :keyword", { + keyword: `%${requestBody.keyword}%`, + }).orWhere("kpiSpecial.includingName LIKE :keyword", { + keyword: `%${requestBody.keyword}%`, + }); + }), + ) + .select([ + "kpiSpecial.id", + "kpiSpecial.year", + "kpiSpecial.period", + "kpiSpecial.including", + "kpiSpecial.includingName", + "kpiSpecial.createdAt", + ]) + .orderBy("kpiSpecial.createdAt", "DESC") + .skip((requestBody.page - 1) * requestBody.pageSize) + .take(requestBody.pageSize) + .getManyAndCount(); + + return new HttpSuccess({ data: kpiSpecial, total }); } /**