api งานที่ได้รับมอบหมายพิเศษ
This commit is contained in:
parent
5db5f445c8
commit
55ceeac3d9
4 changed files with 415 additions and 93 deletions
229
src/controllers/KpiUserSpecialController.ts
Normal file
229
src/controllers/KpiUserSpecialController.ts
Normal file
|
|
@ -0,0 +1,229 @@
|
|||
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";
|
||||
|
||||
@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, "ไม่พบข้อมูล");
|
||||
}
|
||||
|
||||
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);
|
||||
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, "ไม่พบข้อมูลแบบประเมินผู้ใช้งาน");
|
||||
}
|
||||
|
||||
kpiUserSpecial.lastUpdateUserId = request.user.sub;
|
||||
kpiUserSpecial.lastUpdateFullName = request.user.name;
|
||||
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,
|
||||
formula: getKpiUserSpecial.formula,
|
||||
};
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue