From 830a6ab307510e68a1563892c2d3bc3008d44828 Mon Sep 17 00:00:00 2001 From: AnandaTon Date: Fri, 14 Jun 2024 11:01:34 +0700 Subject: [PATCH] Update ReportController.ts --- src/controllers/ReportController.ts | 45 ++++++++++++++++++++++++----- 1 file changed, 37 insertions(+), 8 deletions(-) diff --git a/src/controllers/ReportController.ts b/src/controllers/ReportController.ts index d8f8c02..36412e3 100644 --- a/src/controllers/ReportController.ts +++ b/src/controllers/ReportController.ts @@ -17,18 +17,47 @@ import { Brackets, Not } from "typeorm"; import HttpSuccess from "../interfaces/http-success"; import HttpError from "../interfaces/http-error"; import HttpStatusCode from "../interfaces/http-status"; +import { KpiPeriod } from "../entities/kpiPeriod"; +import { KpiUserEvaluation } from "../entities/kpiUserEvaluation"; @Route("api/v1/kpi/report") @Tags("Report") @Security("bearerAuth") export class ReportController extends Controller { - // private developmentScholarshipRepository = AppDataSource.getRepository(DevelopmentScholarship); - - @Get("announcement/{id}") - async GetReportAnnouncement(@Path() id: string) { - const formattedData = { - periodId: "", - root: "", - }; + private kpiPeriodRepository = AppDataSource.getRepository(KpiPeriod); + private kpiUserEvaluationRepository = AppDataSource.getRepository(KpiUserEvaluation); + @Post("announcement/{id}") + async GetReportAnnouncement( + @Path() id: string, + @Body() + requestBody: { root: string; periodId: string }, + ) { + const getEvaluations = await this.kpiUserEvaluationRepository.find({ + where: { kpiPeriodId: id }, + }); + const getPeriod = await this.kpiPeriodRepository.findOne({ + where: { id: id }, + }); + if (!getEvaluations || getEvaluations.length === 0) { + console.log("No evaluations found for the given kpiPeriodId: ", id); + return; + } + if (!getPeriod) { + console.log("No period data found for the given id: ", id); + return; + } + const formattedData = getEvaluations.map((evaluation) => ({ + periodId: requestBody.periodId, + root: requestBody.root, + fullName: `${evaluation.prefix} ${evaluation.firstName} ${evaluation.lastName}`, + position: evaluation.position, + posLevel: evaluation.posLevelName, + announceYear: getPeriod.year, + result: "ดีเด่น", + oc: "test", + organizationName: "test", + announceDate: "testDATE", + roundNo: "1", + })); return new HttpSuccess({ template: "announce",