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

286 lines
11 KiB
TypeScript
Raw Normal View History

import {
Controller,
Get,
Post,
Put,
Delete,
Route,
Security,
Tags,
Body,
Path,
Request,
SuccessResponse,
Response,
Query,
} from "tsoa";
import { AppDataSource } from "../database/data-source";
import HttpSuccess from "../interfaces/http-success";
import HttpStatusCode from "../interfaces/http-status";
import {
KpiUserSpecial,
CreateKpiUserSpecial,
UpdateKpiUserSpecial,
KpiUserSpecialDataPoint,
} from "../entities/kpiUserSpecial";
import HttpError from "../interfaces/http-error";
import { KpiUserEvaluation } from "../entities/kpiUserEvaluation";
2024-04-25 11:21:59 +07:00
import { Not } from "typeorm";
@Route("api/v1/kpi/user/achievement/special")
@Tags("KpiUserSpecial")
@Security("bearerAuth")
@Response(
HttpStatusCode.INTERNAL_SERVER_ERROR,
"เกิดข้อผิดพลาด ไม่สามารถแสดงรายการได้ กรุณาลองใหม่ในภายหลัง",
)
@SuccessResponse(HttpStatusCode.OK, "สำเร็จ")
export class KpiUserSpecialController extends Controller {
private kpiUserSpecialRepository = AppDataSource.getRepository(KpiUserSpecial);
private kpiUserEvaluationRepository = AppDataSource.getRepository(KpiUserEvaluation);
/**
* API
*
* @summary - #
*
*/
@Post()
async createKpiUserSpecial(
@Body()
requestBody: CreateKpiUserSpecial,
@Request() request: { user: Record<string, any> },
) {
const chkUserEvaluation = await this.kpiUserEvaluationRepository.findOne({
where: { id: requestBody.kpiUserEvaluationId },
});
if (!chkUserEvaluation) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลแบบประเมินผู้ใช้งาน");
}
const kpiUserSpecial = Object.assign(new KpiUserSpecial(), requestBody);
if (!kpiUserSpecial) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล");
}
2024-04-25 11:21:59 +07:00
const chk_indicator = await this.kpiUserSpecialRepository.findOne({
where: {
kpiUserEvaluationId: requestBody.kpiUserEvaluationId,
},
});
if (
(chk_indicator && chk_indicator.including == requestBody.including) ||
(chk_indicator && chk_indicator.includingName == requestBody.includingName)
) {
throw new HttpError(
HttpStatusCode.CONFLICT,
"ไม่สามารถเพิ่มข้อมูลได้เนื่องจากข้อมูลตัวชี้วัดซ้ำ",
);
2024-04-25 11:21:59 +07:00
}
kpiUserSpecial.createdUserId = request.user.sub;
kpiUserSpecial.createdFullName = request.user.name;
kpiUserSpecial.lastUpdateUserId = request.user.sub;
kpiUserSpecial.lastUpdateFullName = request.user.name;
kpiUserSpecial.documentInfoEvidence = request.user.documentInfoEvidence;
kpiUserSpecial.startDate = request.user.startDate;
kpiUserSpecial.endDate = request.user.endDate;
await this.kpiUserSpecialRepository.save(kpiUserSpecial);
return new HttpSuccess(kpiUserSpecial.id);
}
/**
* API
*
* @summary - #
*
* @param {string} id Id
*/
@Put("{id}")
async editKpiUserSpecial(
@Path() id: string,
@Body() requestBody: UpdateKpiUserSpecial,
@Request() request: { user: Record<string, any> },
) {
const kpiUserSpecial = await this.kpiUserSpecialRepository.findOne({ where: { id } });
if (!kpiUserSpecial) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลงานที่ได้รับมอบหมายพิเศษนี้");
}
const chkUserEvaluation = await this.kpiUserEvaluationRepository.findOne({
where: { id: requestBody.kpiUserEvaluationId },
});
if (!chkUserEvaluation) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลแบบประเมินผู้ใช้งาน");
}
2024-04-25 11:21:59 +07:00
const chk_indicator = await this.kpiUserSpecialRepository.findOne({
where: {
id: Not(id),
kpiUserEvaluationId: requestBody.kpiUserEvaluationId,
},
});
if (
(chk_indicator && chk_indicator.including == requestBody.including) ||
(chk_indicator && chk_indicator.includingName == requestBody.includingName)
) {
throw new HttpError(
HttpStatusCode.CONFLICT,
"ไม่สามารถเพิ่มข้อมูลได้เนื่องจากข้อมูลตัวชี้วัดซ้ำ",
);
2024-04-25 11:21:59 +07:00
}
kpiUserSpecial.lastUpdateUserId = request.user.sub;
kpiUserSpecial.lastUpdateFullName = request.user.name;
kpiUserSpecial.documentInfoEvidence = request.user.documentInfoEvidence;
kpiUserSpecial.startDate = request.user.startDate;
kpiUserSpecial.endDate = request.user.endDate;
Object.assign(kpiUserSpecial, requestBody);
await this.kpiUserSpecialRepository.save(kpiUserSpecial);
return new HttpSuccess(kpiUserSpecial.id);
}
/**
* API
*
* @summary - #
*
*/
@Delete("{id}")
async deleteKpiUserSpecial(@Path() id: string) {
const delKpiUserSpecial = await this.kpiUserSpecialRepository.findOne({ where: { id } });
if (!delKpiUserSpecial) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลงานที่ได้รับมอบหมายพิเศษนี้");
}
await this.kpiUserSpecialRepository.remove(delKpiUserSpecial);
return new HttpSuccess();
}
/**
* API
*
* @summary - #
*
* @param {string} id Id
*/
@Get("{id}")
async GetKpiUserSpecialDetail(@Path() id: string) {
const getKpiUserSpecial = await this.kpiUserSpecialRepository.findOne({
relations: ["kpiUserEvaluation"],
where: { id: id },
});
if (!getKpiUserSpecial) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลงานที่ได้รับมอบหมายพิเศษนี้");
}
const mapKpiUserSpecial = {
id: getKpiUserSpecial.id,
evaluationId: getKpiUserSpecial.kpiUserEvaluation.id,
including: getKpiUserSpecial.including,
includingName: getKpiUserSpecial.includingName,
target: getKpiUserSpecial.target,
weight: getKpiUserSpecial.weight,
unit: getKpiUserSpecial.unit,
meaning: getKpiUserSpecial.meaning,
2024-04-24 13:58:04 +07:00
formula: getKpiUserSpecial.formula,
point: getKpiUserSpecial.point,
achievement:
getKpiUserSpecial.point === 1
? getKpiUserSpecial.achievement1
: getKpiUserSpecial.point === 2
? getKpiUserSpecial.achievement2
: getKpiUserSpecial.point === 3
? getKpiUserSpecial.achievement3
: getKpiUserSpecial.point === 4
? getKpiUserSpecial.achievement4
: getKpiUserSpecial.point === 5
? getKpiUserSpecial.achievement5
: null,
achievement1: getKpiUserSpecial.achievement1,
achievement2: getKpiUserSpecial.achievement2,
achievement3: getKpiUserSpecial.achievement3,
achievement4: getKpiUserSpecial.achievement4,
achievement5: getKpiUserSpecial.achievement5,
};
return new HttpSuccess(mapKpiUserSpecial);
}
/**
* API
*
* @summary - #
*
*/
@Get()
async GetKpiUserSpecial(@Query("id") id: string) {
const kpiUserSpecial = await this.kpiUserSpecialRepository.find({
where: {
kpiUserEvaluationId: id,
},
relations: ["kpiUserEvaluation"],
order: { createdAt: "ASC" },
});
const mapKpiUserSpecial = kpiUserSpecial.map((item) => ({
id: item.id,
evaluationId: item.kpiUserEvaluation.id,
including: item.including,
includingName: item.includingName,
target: item.target,
weight: item.weight,
unit: item.unit,
meaning: item.meaning,
formula: item.formula,
point: item.point,
achievement:
item.point === 1
? item.achievement1
: item.point === 2
? item.achievement2
: item.point === 3
? item.achievement3
: item.point === 4
? item.achievement4
: item.point === 5
? item.achievement5
: null,
achievement1: item.achievement1,
achievement2: item.achievement2,
achievement3: item.achievement3,
achievement4: item.achievement4,
achievement5: item.achievement5,
}));
return new HttpSuccess(mapKpiUserSpecial);
}
/**
* API
*
* @summary
*
*
*/
@Post("point")
async CreateKpiUserSpecialPoint(
@Body() requestBody: KpiUserSpecialDataPoint[],
@Request() request: { user: Record<string, any> },
) {
for (const item of requestBody) {
const kpiUserSpecial = await this.kpiUserSpecialRepository.findOne({
where: { id: item.id },
});
if (!kpiUserSpecial) {
throw new HttpError(
HttpStatusCode.NOT_FOUND,
`ไม่พบข้อมูลงานที่ได้รับมอบหมายพิเศษนี้: ${item.id}`,
);
}
this.kpiUserSpecialRepository.merge(kpiUserSpecial, item);
kpiUserSpecial.lastUpdateUserId = request.user.sub;
kpiUserSpecial.lastUpdateFullName = request.user.name;
await this.kpiUserSpecialRepository.save(kpiUserSpecial);
}
return new HttpSuccess();
}
}