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 { KpiUserRole, CreateKpiUserRole, UpdateKpiUserRole, KpiUserRoleDataPoint } from "../entities/kpiUserRole";
import {
KpiUserRole,
CreateKpiUserRole,
UpdateKpiUserRole,
KpiUserRoleDataPoint,
} from "../entities/kpiUserRole";
import HttpError from "../interfaces/http-error";
import { Not } from "typeorm";
import { KpiUserEvaluation } from "../entities/kpiUserEvaluation";
@ -51,17 +56,20 @@ export class KpiUserRoleController 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 chkKpiRole = await this.kpiRoleRepository.findOne({
where:{id:requestBody.kpiRoleId},
})
where: { id: requestBody.kpiRoleId },
});
if (!chkKpiRole) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลแบบประเมินตามหน้าที่ความรับผิดชอบหลัก");
throw new HttpError(
HttpStatusCode.NOT_FOUND,
"ไม่พบข้อมูลแบบประเมินตามหน้าที่ความรับผิดชอบหลัก",
);
}
const kpiUserRole = Object.assign(new KpiUserRole(), requestBody);
@ -95,19 +103,22 @@ export class KpiUserRoleController extends Controller {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลงานตามหน้าที่ความรับผิดชอบหลักนี้");
}
const chkUserEvaluation = await this.kpiUserEvaluationRepository.findOne({
where:{id:requestBody.kpiUserEvaluationId},
})
where: { id: requestBody.kpiUserEvaluationId },
});
if (!chkUserEvaluation) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลแบบประเมินผู้ใช้งาน");
}
const chkKpiRole = await this.kpiRoleRepository.findOne({
where:{id:requestBody.kpiRoleId},
})
where: { id: requestBody.kpiRoleId },
});
if (!chkKpiRole) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลแบบประเมินตามหน้าที่ความรับผิดชอบหลัก");
throw new HttpError(
HttpStatusCode.NOT_FOUND,
"ไม่พบข้อมูลแบบประเมินตามหน้าที่ความรับผิดชอบหลัก",
);
}
kpiUserRole.lastUpdateUserId = request.user.sub;
kpiUserRole.lastUpdateFullName = request.user.name;
this.kpiUserRoleRepository.merge(kpiUserRole, requestBody);
@ -142,14 +153,14 @@ export class KpiUserRoleController extends Controller {
@Get("{id}")
async GetKpiUserRoleDetail(@Path() id: string) {
const getKpiUserRole = await this.kpiUserRoleRepository.findOne({
relations: ["kpiRole","kpiUserEvaluation"],
relations: ["kpiRole", "kpiUserEvaluation"],
where: { id: id },
});
if (!getKpiUserRole) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลงานตามหน้าที่ความรับผิดชอบหลักนี้");
}
const mapKpiUserRole = ({
const mapKpiUserRole = {
id: getKpiUserRole.id,
evaluationId: getKpiUserRole.kpiUserEvaluation.id,
kpiRoleId: getKpiUserRole.kpiRole.id,
@ -160,7 +171,7 @@ export class KpiUserRoleController extends Controller {
unit: getKpiUserRole.unit,
meaning: getKpiUserRole.meaning,
formula: getKpiUserRole.formula,
});
};
return new HttpSuccess(mapKpiUserRole);
}
@ -172,17 +183,15 @@ export class KpiUserRoleController extends Controller {
*
*/
@Get()
async GetKpiUserRole(
@Query("id") id: string ,
) {
async GetKpiUserRole(@Query("id") id: string) {
const kpiUserRole = await this.kpiUserRoleRepository.find({
where:{
kpiUserEvaluationId:id
where: {
kpiUserEvaluationId: id,
},
relations: ["kpiRole","kpiUserEvaluation"],
relations: ["kpiRole", "kpiUserEvaluation"],
order: { createdAt: "ASC" },
});
const mapKpiUserRole = kpiUserRole.map((item) => ({
id: item.id,
evaluationId: item.kpiUserEvaluation.id,
@ -195,11 +204,18 @@ export class KpiUserRoleController 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.kpiRole.achievement1
: item.point === 2
? item.kpiRole.achievement2
: item.point === 3
? item.kpiRole.achievement3
: item.point === 4
? item.kpiRole.achievement4
: item.point === 5
? item.kpiRole.achievement5
: null,
achievement1: item.kpiRole.achievement1,
achievement2: item.kpiRole.achievement2,
achievement3: item.kpiRole.achievement3,
@ -209,31 +225,33 @@ export class KpiUserRoleController extends Controller {
return new HttpSuccess(mapKpiUserRole);
}
/**
/**
* API
*
*
* @summary
*
*
*
*
*/
@Post("point")
async CreateKpiUserRolePoint(
@Body() requestBody: KpiUserRoleDataPoint[],
@Request() request: { user: Record<string, any> },
){
for (const item of requestBody) {
const kpiUserRole = await this.kpiUserRoleRepository.findOne({
where: { id: item.id },
});
if (!kpiUserRole) {
throw new HttpError(HttpStatusCode.NOT_FOUND, `ไม่พบข้อมูลงานตามหน้าที่ความรับผิดชอบหลักนี้: ${item.id}`);
}
this.kpiUserRoleRepository.merge(kpiUserRole, item);
kpiUserRole.lastUpdateUserId = request.user.sub;
kpiUserRole.lastUpdateFullName = request.user.name;
await this.kpiUserRoleRepository.save(kpiUserRole);
}
return new HttpSuccess();
}
@Post("point")
async CreateKpiUserRolePoint(
@Body() requestBody: KpiUserRoleDataPoint[],
@Request() request: { user: Record<string, any> },
) {
for (const item of requestBody) {
const kpiUserRole = await this.kpiUserRoleRepository.findOne({
where: { id: item.id },
});
if (!kpiUserRole) {
throw new HttpError(
HttpStatusCode.NOT_FOUND,
`ไม่พบข้อมูลงานตามหน้าที่ความรับผิดชอบหลักนี้: ${item.id}`,
);
}
this.kpiUserRoleRepository.merge(kpiUserRole, item);
kpiUserRole.lastUpdateUserId = request.user.sub;
kpiUserRole.lastUpdateFullName = request.user.name;
await this.kpiUserRoleRepository.save(kpiUserRole);
}
return new HttpSuccess();
}
}