api งานที่ได้รับมอบหมายพิเศษ

This commit is contained in:
Kittapath 2024-04-23 17:55:57 +07:00
parent 5db5f445c8
commit 55ceeac3d9
4 changed files with 415 additions and 93 deletions

View file

@ -19,7 +19,12 @@ import {
import { AppDataSource } from "../database/data-source";
import HttpSuccess from "../interfaces/http-success";
import HttpStatusCode from "../interfaces/http-status";
import { KpiUserPlanned, CreateKpiUserPlanned, UpdateKpiUserPlanned, KpiUserPlannedDataPoint } from "../entities/kpiUserPlanned";
import {
KpiUserPlanned,
CreateKpiUserPlanned,
UpdateKpiUserPlanned,
KpiUserPlannedDataPoint,
} from "../entities/kpiUserPlanned";
import HttpError from "../interfaces/http-error";
import { Not } from "typeorm";
import { KpiUserEvaluation } from "../entities/kpiUserEvaluation";
@ -51,15 +56,15 @@ export class KpiUserPlannedController extends Controller {
@Request() request: { user: Record<string, any> },
) {
const chkUserEvaluation = await this.kpiUserEvaluationRepository.findOne({
where:{id:requestBody.kpiUserEvaluationId},
})
where: { id: requestBody.kpiUserEvaluationId },
});
if (!chkUserEvaluation) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลแบบประเมินผู้ใช้งาน");
}
const chkKpiPlan = await this.kpiPlanRepository.findOne({
where:{id:requestBody.kpiPlanId},
})
where: { id: requestBody.kpiPlanId },
});
if (!chkKpiPlan) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลแบบประเมินตามแผน");
}
@ -94,7 +99,7 @@ export class KpiUserPlannedController extends Controller {
if (!kpiUserPlanned) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลงานตามแผนปฏิบัติราชการประจำปีนี้");
}
kpiUserPlanned.lastUpdateUserId = request.user.sub;
kpiUserPlanned.lastUpdateFullName = request.user.name;
this.kpiUserPlannedRepository.merge(kpiUserPlanned, requestBody);
@ -129,14 +134,14 @@ export class KpiUserPlannedController extends Controller {
@Get("{id}")
async GetKpiUserPlannedDetail(@Path() id: string) {
const getKpiUserPlanned = await this.kpiUserPlannedRepository.findOne({
relations: ["kpiPlan","kpiUserEvaluation"],
relations: ["kpiPlan", "kpiUserEvaluation"],
where: { id: id },
});
if (!getKpiUserPlanned) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลงานตามแผนปฏิบัติราชการประจำปีนี้");
}
const mapGetKpiUserPlanned = ({
const mapGetKpiUserPlanned = {
id: getKpiUserPlanned.id,
evaluationId: getKpiUserPlanned.kpiUserEvaluation.id,
kpiPlanId: getKpiUserPlanned.kpiPlan.id,
@ -147,7 +152,7 @@ export class KpiUserPlannedController extends Controller {
unit: getKpiUserPlanned.unit,
meaning: getKpiUserPlanned.meaning,
formula: getKpiUserPlanned.formula,
});
};
return new HttpSuccess(mapGetKpiUserPlanned);
}
@ -159,17 +164,15 @@ export class KpiUserPlannedController extends Controller {
*
*/
@Get()
async GetKpiUserPlanned(
@Query("id") id: string ,
) {
async GetKpiUserPlanned(@Query("id") id: string) {
const kpiUserPlanned = await this.kpiUserPlannedRepository.find({
where:{
kpiUserEvaluationId:id
where: {
kpiUserEvaluationId: id,
},
relations: ["kpiPlan","kpiUserEvaluation"],
relations: ["kpiPlan", "kpiUserEvaluation"],
order: { createdAt: "ASC" },
});
const mapKpiUserPlanned = kpiUserPlanned.map((item) => ({
id: item.id,
evaluationId: item.kpiUserEvaluation.id,
@ -181,11 +184,18 @@ export class KpiUserPlannedController extends Controller {
meaning: item.meaning,
formula: item.formula,
point: item.point,
achievement: item.point === 1 ? "ระดับ 1" :
item.point === 2 ? "ระดับ 2" :
item.point === 3 ? "ระดับ 3" :
item.point === 4 ? "ระดับ 4" :
item.point === 5 ? "ระดับ 5" : null,
achievement:
item.point === 1
? item.kpiPlan.achievement1
: item.point === 2
? item.kpiPlan.achievement2
: item.point === 3
? item.kpiPlan.achievement3
: item.point === 4
? item.kpiPlan.achievement4
: item.point === 5
? item.kpiPlan.achievement5
: null,
achievement1: item.kpiPlan.achievement1,
achievement2: item.kpiPlan.achievement2,
achievement3: item.kpiPlan.achievement3,
@ -197,29 +207,31 @@ export class KpiUserPlannedController extends Controller {
/**
* API
*
*
* @summary
*
*
*
*
*/
@Post("point")
async CreateKpiUserPlannedPoint(
@Body() requestBody: KpiUserPlannedDataPoint[],
@Request() request: { user: Record<string, any> },
){
for (const item of requestBody) {
const kpiUserPlanned = await this.kpiUserPlannedRepository.findOne({
where: { id: item.id },
});
if (!kpiUserPlanned) {
throw new HttpError(HttpStatusCode.NOT_FOUND, `ไม่พบข้อมูลงานตามแผนปฏิบัติราชการประจำปีนี้: ${item.id}`);
}
this.kpiUserPlannedRepository.merge(kpiUserPlanned, item);
kpiUserPlanned.lastUpdateUserId = request.user.sub;
kpiUserPlanned.lastUpdateFullName = request.user.name;
await this.kpiUserPlannedRepository.save(kpiUserPlanned);
@Post("point")
async CreateKpiUserPlannedPoint(
@Body() requestBody: KpiUserPlannedDataPoint[],
@Request() request: { user: Record<string, any> },
) {
for (const item of requestBody) {
const kpiUserPlanned = await this.kpiUserPlannedRepository.findOne({
where: { id: item.id },
});
if (!kpiUserPlanned) {
throw new HttpError(
HttpStatusCode.NOT_FOUND,
`ไม่พบข้อมูลงานตามแผนปฏิบัติราชการประจำปีนี้: ${item.id}`,
);
}
return new HttpSuccess();
this.kpiUserPlannedRepository.merge(kpiUserPlanned, item);
kpiUserPlanned.lastUpdateUserId = request.user.sub;
kpiUserPlanned.lastUpdateFullName = request.user.name;
await this.kpiUserPlannedRepository.save(kpiUserPlanned);
}
return new HttpSuccess();
}
}