hrms-api-kpi/src/controllers/KpiUserPlannedController.ts

282 lines
11 KiB
TypeScript
Raw Normal View History

2024-04-22 18:13:30 +07:00
import {
Controller,
Get,
Post,
Put,
Delete,
Patch,
Route,
Security,
Tags,
Body,
Path,
Request,
Example,
SuccessResponse,
Response,
Query,
} from "tsoa";
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";
2024-04-22 18:13:30 +07:00
import HttpError from "../interfaces/http-error";
import { Not } from "typeorm";
import { KpiUserEvaluation } from "../entities/kpiUserEvaluation";
import { KpiPlan } from "../entities/kpiPlan";
2024-04-22 17:10:37 +07:00
2024-04-22 18:13:30 +07:00
@Route("api/v1/kpi/user/achievement/planned")
@Tags("KpiUserPlanned")
@Security("bearerAuth")
@Response(
HttpStatusCode.INTERNAL_SERVER_ERROR,
"เกิดข้อผิดพลาด ไม่สามารถแสดงรายการได้ กรุณาลองใหม่ในภายหลัง",
)
@SuccessResponse(HttpStatusCode.OK, "สำเร็จ")
export class KpiUserPlannedController extends Controller {
private kpiUserPlannedRepository = AppDataSource.getRepository(KpiUserPlanned);
private kpiUserEvaluationRepository = AppDataSource.getRepository(KpiUserEvaluation);
private kpiPlanRepository = AppDataSource.getRepository(KpiPlan);
2024-04-22 17:10:37 +07:00
2024-04-22 18:13:30 +07:00
/**
* API
*
* @summary - #48
*
*/
@Post()
async createKpiUserPlanned(
@Body()
requestBody: CreateKpiUserPlanned,
@Request() request: { user: Record<string, any> },
) {
const chkUserEvaluation = await this.kpiUserEvaluationRepository.findOne({
where: { id: requestBody.kpiUserEvaluationId },
});
2024-04-22 18:13:30 +07:00
if (!chkUserEvaluation) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลแบบประเมินผู้ใช้งาน");
}
2024-04-22 17:10:37 +07:00
2024-04-22 18:13:30 +07:00
const chkKpiPlan = await this.kpiPlanRepository.findOne({
where: { id: requestBody.kpiPlanId },
});
2024-04-22 18:13:30 +07:00
if (!chkKpiPlan) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลแบบประเมินตามแผน");
}
2024-04-22 17:10:37 +07:00
2024-04-25 11:21:59 +07:00
const chk_indicator = await this.kpiUserPlannedRepository.findOne({
where: {
kpiUserEvaluationId: requestBody.kpiUserEvaluationId,
kpiPlanId: requestBody.kpiPlanId,
},
});
2024-04-25 11:21:59 +07:00
if (chk_indicator) {
throw new HttpError(
HttpStatusCode.CONFLICT,
"ไม่สามารถเพิ่มข้อมูลได้เนื่องจากข้อมูลตัวชี้วัดซ้ำ",
);
2024-04-25 11:21:59 +07:00
}
2024-04-22 18:13:30 +07:00
const kpiUserPlanned = Object.assign(new KpiUserPlanned(), requestBody);
if (!kpiUserPlanned) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล");
}
2024-04-22 17:10:37 +07:00
2024-04-22 18:13:30 +07:00
kpiUserPlanned.createdUserId = request.user.sub;
kpiUserPlanned.createdFullName = request.user.name;
kpiUserPlanned.lastUpdateUserId = request.user.sub;
kpiUserPlanned.lastUpdateFullName = request.user.name;
kpiUserPlanned.documentInfoEvidence = request.user.documentInfoEvidence;
kpiUserPlanned.startDate = request.user.startDate;
kpiUserPlanned.endDate = request.user.endDate;
kpiUserPlanned.achievement1 = request.user.achievement1;
kpiUserPlanned.achievement2 = request.user.achievement2;
kpiUserPlanned.achievement3 = request.user.achievement3;
kpiUserPlanned.achievement4 = request.user.achievement4;
kpiUserPlanned.achievement5 = request.user.achievement5;
2024-04-22 18:13:30 +07:00
await this.kpiUserPlannedRepository.save(kpiUserPlanned);
return new HttpSuccess(kpiUserPlanned.id);
}
2024-04-22 17:10:37 +07:00
2024-04-22 18:13:30 +07:00
/**
* API
*
* @summary - #49
*
* @param {string} id Id
*/
@Put("{id}")
async editKpiUserPlanned(
@Path() id: string,
@Body() requestBody: UpdateKpiUserPlanned,
@Request() request: { user: Record<string, any> },
) {
const kpiUserPlanned = await this.kpiUserPlannedRepository.findOne({ where: { id } });
if (!kpiUserPlanned) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลงานตามแผนปฏิบัติราชการประจำปีนี้");
}
2024-04-25 11:21:59 +07:00
const chk_indicator = await this.kpiUserPlannedRepository.findOne({
where: {
id: Not(id),
kpiUserEvaluationId: requestBody.kpiUserEvaluationId,
kpiPlanId: requestBody.kpiPlanId,
},
});
2024-04-25 11:21:59 +07:00
if (chk_indicator) {
throw new HttpError(
HttpStatusCode.CONFLICT,
"ไม่สามารถเพิ่มข้อมูลได้เนื่องจากข้อมูลตัวชี้วัดซ้ำ",
);
2024-04-25 11:21:59 +07:00
}
2024-04-22 18:13:30 +07:00
kpiUserPlanned.lastUpdateUserId = request.user.sub;
kpiUserPlanned.lastUpdateFullName = request.user.name;
kpiUserPlanned.documentInfoEvidence = request.user.documentInfoEvidence;
kpiUserPlanned.startDate = request.user.startDate;
kpiUserPlanned.endDate = request.user.endDate;
kpiUserPlanned.achievement1 = request.user.achievement1;
kpiUserPlanned.achievement2 = request.user.achievement2;
kpiUserPlanned.achievement3 = request.user.achievement3;
kpiUserPlanned.achievement4 = request.user.achievement4;
kpiUserPlanned.achievement5 = request.user.achievement5;
2024-04-22 18:13:30 +07:00
this.kpiUserPlannedRepository.merge(kpiUserPlanned, requestBody);
await this.kpiUserPlannedRepository.save(kpiUserPlanned);
return new HttpSuccess(kpiUserPlanned.id);
}
2024-04-22 17:10:37 +07:00
2024-04-22 18:13:30 +07:00
/**
* API
*
* @summary - #50
*
* @param {string} id Id
*/
@Delete("{id}")
async deleteKpiUserPlanned(@Path() id: string) {
const delKpiUserPlanned = await this.kpiUserPlannedRepository.findOne({ where: { id } });
if (!delKpiUserPlanned) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลงานตามแผนปฏิบัติราชการประจำปีนี้");
}
await this.kpiUserPlannedRepository.remove(delKpiUserPlanned);
return new HttpSuccess();
}
2024-04-22 17:10:37 +07:00
2024-04-23 12:29:26 +07:00
/**
* API
*
* @summary - #51
*
* @param {string} id Id
*/
@Get("{id}")
async GetKpiUserPlannedDetail(@Path() id: string) {
const getKpiUserPlanned = await this.kpiUserPlannedRepository.findOne({
relations: ["kpiPlan", "kpiUserEvaluation"],
2024-04-23 12:29:26 +07:00
where: { id: id },
});
if (!getKpiUserPlanned) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลงานตามแผนปฏิบัติราชการประจำปีนี้");
}
2024-04-22 17:10:37 +07:00
const mapGetKpiUserPlanned = {
2024-04-23 12:29:26 +07:00
id: getKpiUserPlanned.id,
evaluationId: getKpiUserPlanned.kpiUserEvaluation.id,
2024-04-23 14:48:25 +07:00
kpiPlanId: getKpiUserPlanned.kpiPlan.id,
2024-04-23 12:29:26 +07:00
including: getKpiUserPlanned.kpiPlan.including, //รหัสตัวชี้วัด
includingName: getKpiUserPlanned.kpiPlan.includingName, //ชื่อตัวชี้วัด
target: getKpiUserPlanned.target,
weight: getKpiUserPlanned.weight,
unit: getKpiUserPlanned.unit,
meaning: getKpiUserPlanned.meaning,
formula: getKpiUserPlanned.formula,
};
2024-04-22 18:13:30 +07:00
2024-04-23 12:29:26 +07:00
return new HttpSuccess(mapGetKpiUserPlanned);
}
2024-04-22 18:13:30 +07:00
/**
* API
*
* @summary - #29
*
*/
@Get()
async GetKpiUserPlanned(@Query("id") id: string) {
2024-04-22 18:13:30 +07:00
const kpiUserPlanned = await this.kpiUserPlannedRepository.find({
where: {
kpiUserEvaluationId: id,
2024-04-23 12:29:26 +07:00
},
relations: ["kpiPlan", "kpiUserEvaluation"],
2024-04-22 18:13:30 +07:00
order: { createdAt: "ASC" },
});
2024-04-22 18:13:30 +07:00
const mapKpiUserPlanned = kpiUserPlanned.map((item) => ({
id: item.id,
evaluationId: item.kpiUserEvaluation.id,
2024-04-22 18:13:30 +07:00
indicatorId: item.kpiPlan.id,
includingName: item.kpiPlan.includingName,
2024-04-22 18:13:30 +07:00
target: item.target,
weight: item.weight,
2024-04-23 11:08:15 +07:00
unit: item.unit,
meaning: item.meaning,
formula: item.formula,
point: item.point,
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,
achievement4: item.kpiPlan.achievement4,
achievement5: item.kpiPlan.achievement5,
2024-04-22 18:13:30 +07:00
}));
return new HttpSuccess(mapKpiUserPlanned);
}
2024-04-23 17:06:16 +07:00
/**
* API
*
2024-04-23 17:06:16 +07:00
* @summary
*
*
2024-04-23 17:06:16 +07:00
*/
@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}`,
);
2024-04-23 17:06:16 +07:00
}
this.kpiUserPlannedRepository.merge(kpiUserPlanned, item);
kpiUserPlanned.lastUpdateUserId = request.user.sub;
kpiUserPlanned.lastUpdateFullName = request.user.name;
await this.kpiUserPlannedRepository.save(kpiUserPlanned);
2024-04-23 17:06:16 +07:00
}
return new HttpSuccess();
}
2024-04-22 18:13:30 +07:00
}