352 lines
14 KiB
TypeScript
352 lines
14 KiB
TypeScript
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";
|
|
import { KpiSpecial } from "../entities/kpiSpecial";
|
|
import { Not } from "typeorm";
|
|
import { RequestWithUser } from "../middlewares/user";
|
|
import permission from "../interfaces/permission";
|
|
import { addLogSequence, setLogDataDiff } from "../interfaces/utils";
|
|
|
|
@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);
|
|
private kpiSpecialRepository = AppDataSource.getRepository(KpiSpecial);
|
|
|
|
/**
|
|
* API เพิ่มงานที่ได้รับมอบหมายพิเศษ
|
|
*
|
|
* @summary - เพิ่มงานที่ได้รับมอบหมายพิเศษ #
|
|
*
|
|
*/
|
|
@Post()
|
|
async createKpiUserSpecial(
|
|
@Body()
|
|
requestBody: CreateKpiUserSpecial,
|
|
@Request() request: RequestWithUser,
|
|
) {
|
|
// await new permission().PermissionCreate(request, "SYS_KPI_LIST");
|
|
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, "ไม่พบข้อมูล");
|
|
}
|
|
|
|
const chk_indicator = await this.kpiUserSpecialRepository.findOne({
|
|
where: {
|
|
kpiUserEvaluationId: requestBody.kpiUserEvaluationId,
|
|
including: String(requestBody.including),
|
|
includingName: String(requestBody.includingName),
|
|
period: requestBody.period,
|
|
year: requestBody.year,
|
|
},
|
|
});
|
|
if (
|
|
// (chk_indicator && chk_indicator.including == requestBody.including) ||
|
|
// (chk_indicator && chk_indicator.includingName == requestBody.includingName)
|
|
chk_indicator
|
|
) {
|
|
throw new HttpError(
|
|
HttpStatusCode.CONFLICT,
|
|
"ไม่สามารถเพิ่มข้อมูลได้เนื่องจากข้อมูลตัวชี้วัดซ้ำ",
|
|
);
|
|
}
|
|
const chk_kpiSpecial = await this.kpiSpecialRepository.findOne({
|
|
where: {
|
|
including: String(requestBody.including),
|
|
includingName: String(requestBody.includingName),
|
|
},
|
|
});
|
|
let before = null;
|
|
if (!chk_kpiSpecial) {
|
|
const kpiSpecial = Object.assign(new KpiSpecial(), requestBody);
|
|
if (!kpiSpecial) {
|
|
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล");
|
|
}
|
|
kpiSpecial.createdUserId = request.user.sub;
|
|
kpiSpecial.createdFullName = request.user.name;
|
|
kpiSpecial.lastUpdateUserId = request.user.sub;
|
|
kpiSpecial.lastUpdateFullName = request.user.name;
|
|
await this.kpiSpecialRepository.save(kpiSpecial, { data: request });
|
|
setLogDataDiff(request, { before, after: kpiSpecial });
|
|
}
|
|
kpiUserSpecial.startDate = requestBody.startDate == undefined ? null : requestBody.startDate;
|
|
kpiUserSpecial.endDate = requestBody.endDate == undefined ? null : requestBody.endDate;
|
|
kpiUserSpecial.createdUserId = request.user.sub;
|
|
kpiUserSpecial.createdFullName = request.user.name;
|
|
kpiUserSpecial.lastUpdateUserId = request.user.sub;
|
|
kpiUserSpecial.lastUpdateFullName = request.user.name;
|
|
await this.kpiUserSpecialRepository.save(kpiUserSpecial, { data: request });
|
|
setLogDataDiff(request, { before, after: kpiUserSpecial });
|
|
|
|
return new HttpSuccess(kpiUserSpecial.id);
|
|
}
|
|
|
|
/**
|
|
* API แก้ไขงานที่ได้รับมอบหมายพิเศษ
|
|
*
|
|
* @summary - แก้ไขงานที่ได้รับมอบหมายพิเศษ #
|
|
*
|
|
* @param {string} id Id งานที่ได้รับมอบหมายพิเศษ
|
|
*/
|
|
@Put("{id}")
|
|
async editKpiUserSpecial(
|
|
@Path() id: string,
|
|
@Body() requestBody: UpdateKpiUserSpecial,
|
|
@Request() request: RequestWithUser,
|
|
) {
|
|
// await new permission().PermissionUpdate(request, "SYS_KPI_LIST");
|
|
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, "ไม่พบข้อมูลแบบประเมินผู้ใช้งาน");
|
|
}
|
|
|
|
const chk_indicator = await this.kpiUserSpecialRepository.findOne({
|
|
where: {
|
|
id: Not(id),
|
|
kpiUserEvaluationId: requestBody.kpiUserEvaluationId,
|
|
including: String(requestBody.including),
|
|
includingName: String(requestBody.includingName),
|
|
period: requestBody.period,
|
|
year: requestBody.year,
|
|
},
|
|
});
|
|
if (
|
|
// (chk_indicator && chk_indicator.including == requestBody.including) ||
|
|
// (chk_indicator && chk_indicator.includingName == requestBody.includingName)
|
|
chk_indicator
|
|
) {
|
|
throw new HttpError(
|
|
HttpStatusCode.CONFLICT,
|
|
"ไม่สามารถเพิ่มข้อมูลได้เนื่องจากข้อมูลตัวชี้วัดซ้ำ",
|
|
);
|
|
}
|
|
const chk_kpiSpecial = await this.kpiSpecialRepository.findOne({
|
|
where: {
|
|
including: String(requestBody.including),
|
|
includingName: String(requestBody.includingName),
|
|
},
|
|
});
|
|
let before = null;
|
|
if (!chk_kpiSpecial) {
|
|
const kpiSpecial = Object.assign(new KpiSpecial(), requestBody);
|
|
if (!kpiSpecial) {
|
|
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล");
|
|
}
|
|
before = structuredClone(kpiSpecial);
|
|
kpiSpecial.createdUserId = request.user.sub;
|
|
kpiSpecial.createdFullName = request.user.name;
|
|
kpiSpecial.lastUpdateUserId = request.user.sub;
|
|
kpiSpecial.lastUpdateFullName = request.user.name;
|
|
await this.kpiSpecialRepository.save(kpiSpecial, { data: request });
|
|
setLogDataDiff(request, { before, after: kpiSpecial });
|
|
}
|
|
before = structuredClone(kpiUserSpecial);
|
|
kpiUserSpecial.lastUpdateUserId = request.user.sub;
|
|
kpiUserSpecial.lastUpdateFullName = request.user.name;
|
|
Object.assign(kpiUserSpecial, requestBody);
|
|
kpiUserSpecial.startDate = requestBody.startDate == undefined ? null : requestBody.startDate;
|
|
kpiUserSpecial.endDate = requestBody.endDate == undefined ? null : requestBody.endDate;
|
|
await this.kpiUserSpecialRepository.save(kpiUserSpecial, { data: request });
|
|
setLogDataDiff(request, { before, after: kpiUserSpecial });
|
|
|
|
return new HttpSuccess(kpiUserSpecial.id);
|
|
}
|
|
|
|
/**
|
|
* API ลบงานที่ได้รับมอบหมายพิเศษ
|
|
*
|
|
* @summary - ลบงานที่ได้รับมอบหมายพิเศษ #
|
|
*
|
|
*/
|
|
@Delete("{id}")
|
|
async deleteKpiUserSpecial(@Path() id: string, @Request() request: RequestWithUser) {
|
|
// await new permission().PermissionDelete(request, "SYS_KPI_LIST");
|
|
const delKpiUserSpecial = await this.kpiUserSpecialRepository.findOne({ where: { id } });
|
|
if (!delKpiUserSpecial) {
|
|
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลงานที่ได้รับมอบหมายพิเศษนี้");
|
|
}
|
|
await this.kpiUserSpecialRepository.remove(delKpiUserSpecial, { data: request });
|
|
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, "ไม่พบข้อมูลงานที่ได้รับมอบหมายพิเศษนี้");
|
|
}
|
|
console.log("CODE28");
|
|
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,
|
|
formula: getKpiUserSpecial.formula,
|
|
summary: getKpiUserSpecial.summary,
|
|
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,
|
|
startDate: getKpiUserSpecial.startDate,
|
|
endDate: getKpiUserSpecial.endDate,
|
|
documentInfoEvidence: getKpiUserSpecial.documentInfoEvidence,
|
|
};
|
|
|
|
return new HttpSuccess(mapKpiUserSpecial);
|
|
}
|
|
|
|
/**
|
|
* API รายการงานที่ได้รับมอบหมายพิเศษ
|
|
*
|
|
* @summary - รายการงานที่ได้รับมอบหมายพิเศษ #
|
|
*
|
|
*/
|
|
@Get()
|
|
async GetKpiUserSpecial(@Request() request: RequestWithUser, @Query("id") id: string) {
|
|
await new permission().PermissionGet(request, "SYS_KPI_LIST");
|
|
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,
|
|
summary: item.summary,
|
|
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: RequestWithUser,
|
|
) {
|
|
// await new permission().PermissionCreate(request, "SYS_KPI_LIST");
|
|
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}`,
|
|
);
|
|
}
|
|
const before = null;
|
|
this.kpiUserSpecialRepository.merge(kpiUserSpecial, item);
|
|
kpiUserSpecial.lastUpdateUserId = request.user.sub;
|
|
kpiUserSpecial.lastUpdateFullName = request.user.name;
|
|
await this.kpiUserSpecialRepository.save(kpiUserSpecial, { data: request });
|
|
setLogDataDiff(request, { before, after: kpiUserSpecial });
|
|
}
|
|
return new HttpSuccess();
|
|
}
|
|
}
|