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

279 lines
10 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 {
KpiUserRole,
CreateKpiUserRole,
UpdateKpiUserRole,
KpiUserRoleDataPoint,
} from "../entities/kpiUserRole";
2024-04-22 18:13:30 +07:00
import HttpError from "../interfaces/http-error";
import { Not } from "typeorm";
import { KpiUserEvaluation } from "../entities/kpiUserEvaluation";
import { KpiRole } from "../entities/kpiRole";
@Route("api/v1/kpi/user/achievement/role")
@Tags("KpiUserRole")
@Security("bearerAuth")
@Response(
HttpStatusCode.INTERNAL_SERVER_ERROR,
"เกิดข้อผิดพลาด ไม่สามารถแสดงรายการได้ กรุณาลองใหม่ในภายหลัง",
)
@SuccessResponse(HttpStatusCode.OK, "สำเร็จ")
export class KpiUserRoleController extends Controller {
private kpiUserRoleRepository = AppDataSource.getRepository(KpiUserRole);
private kpiUserEvaluationRepository = AppDataSource.getRepository(KpiUserEvaluation);
private kpiRoleRepository = AppDataSource.getRepository(KpiRole);
/**
* API
*
* @summary - #48
*
*/
@Post()
async createKpiUserRole(
@Body()
requestBody: CreateKpiUserRole,
@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, "ไม่พบข้อมูลแบบประเมินผู้ใช้งาน");
}
const chkKpiRole = await this.kpiRoleRepository.findOne({
where: { id: requestBody.kpiRoleId },
});
2024-04-22 18:13:30 +07:00
if (!chkKpiRole) {
throw new HttpError(
HttpStatusCode.NOT_FOUND,
"ไม่พบข้อมูลแบบประเมินตามหน้าที่ความรับผิดชอบหลัก",
);
2024-04-22 18:13:30 +07:00
}
const kpiUserRole = Object.assign(new KpiUserRole(), requestBody);
if (!kpiUserRole) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล");
}
2024-04-25 11:21:59 +07:00
const chk_indicator = await this.kpiUserRoleRepository.findOne({
where: {
kpiUserEvaluationId: requestBody.kpiUserEvaluationId,
kpiRoleId: requestBody.kpiRoleId,
},
})
if (chk_indicator) {
throw new HttpError(HttpStatusCode.CONFLICT, "ไม่สามารถเพิ่มข้อมูลได้เนื่องจากข้อมูลตัวชี้วัดซ้ำ");
}
2024-04-22 18:13:30 +07:00
kpiUserRole.createdUserId = request.user.sub;
kpiUserRole.createdFullName = request.user.name;
kpiUserRole.lastUpdateUserId = request.user.sub;
kpiUserRole.lastUpdateFullName = request.user.name;
await this.kpiUserRoleRepository.save(kpiUserRole);
return new HttpSuccess(kpiUserRole.id);
}
/**
* API
*
* @summary - #49
*
* @param {string} id Id
*/
@Put("{id}")
async editKpiUserRole(
@Path() id: string,
@Body() requestBody: UpdateKpiUserRole,
@Request() request: { user: Record<string, any> },
) {
const kpiUserRole = await this.kpiUserRoleRepository.findOne({ where: { id } });
if (!kpiUserRole) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลงานตามหน้าที่ความรับผิดชอบหลักนี้");
}
2024-04-23 11:08:15 +07:00
const chkUserEvaluation = await this.kpiUserEvaluationRepository.findOne({
where: { id: requestBody.kpiUserEvaluationId },
});
2024-04-23 11:08:15 +07:00
if (!chkUserEvaluation) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลแบบประเมินผู้ใช้งาน");
}
const chkKpiRole = await this.kpiRoleRepository.findOne({
where: { id: requestBody.kpiRoleId },
});
2024-04-23 11:08:15 +07:00
if (!chkKpiRole) {
throw new HttpError(
HttpStatusCode.NOT_FOUND,
"ไม่พบข้อมูลแบบประเมินตามหน้าที่ความรับผิดชอบหลัก",
);
2024-04-23 11:08:15 +07:00
}
2024-04-25 11:21:59 +07:00
const chk_indicator = await this.kpiUserRoleRepository.findOne({
where: {
id: Not(id),
kpiUserEvaluationId: requestBody.kpiUserEvaluationId,
kpiRoleId: requestBody.kpiRoleId,
},
})
if (chk_indicator) {
throw new HttpError(HttpStatusCode.CONFLICT, "ไม่สามารถเพิ่มข้อมูลได้เนื่องจากข้อมูลตัวชี้วัดซ้ำ");
}
2024-04-22 18:13:30 +07:00
kpiUserRole.lastUpdateUserId = request.user.sub;
kpiUserRole.lastUpdateFullName = request.user.name;
this.kpiUserRoleRepository.merge(kpiUserRole, requestBody);
await this.kpiUserRoleRepository.save(kpiUserRole);
return new HttpSuccess(kpiUserRole.id);
}
/**
* API
*
* @summary - #50
*
* @param {string} id Id
*/
@Delete("{id}")
async deleteKpiUserRole(@Path() id: string) {
const delKpiUserRole = await this.kpiUserRoleRepository.findOne({ where: { id } });
if (!delKpiUserRole) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลงานตามหน้าที่ความรับผิดชอบหลักนี้");
}
await this.kpiUserRoleRepository.remove(delKpiUserRole);
return new HttpSuccess();
}
2024-04-23 12:29:26 +07:00
/**
* API
*
* @summary - #51
*
* @param {string} id Id
*/
@Get("{id}")
async GetKpiUserRoleDetail(@Path() id: string) {
const getKpiUserRole = await this.kpiUserRoleRepository.findOne({
relations: ["kpiRole", "kpiUserEvaluation"],
2024-04-23 12:29:26 +07:00
where: { id: id },
});
if (!getKpiUserRole) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลงานตามหน้าที่ความรับผิดชอบหลักนี้");
}
2024-04-22 18:13:30 +07:00
const mapKpiUserRole = {
2024-04-23 12:29:26 +07:00
id: getKpiUserRole.id,
evaluationId: getKpiUserRole.kpiUserEvaluation.id,
kpiRoleId: getKpiUserRole.kpiRole.id,
including: getKpiUserRole.kpiRole.including,
includingName: getKpiUserRole.kpiRole.includingName,
target: getKpiUserRole.target,
weight: getKpiUserRole.weight,
unit: getKpiUserRole.unit,
meaning: getKpiUserRole.meaning,
formula: getKpiUserRole.formula,
};
2024-04-22 18:13:30 +07:00
2024-04-23 12:29:26 +07:00
return new HttpSuccess(mapKpiUserRole);
}
2024-04-22 18:13:30 +07:00
/**
* API
*
* @summary - #29
*
*/
@Get()
async GetKpiUserRole(@Query("id") id: string) {
2024-04-22 18:13:30 +07:00
const kpiUserRole = await this.kpiUserRoleRepository.find({
where: {
kpiUserEvaluationId: id,
2024-04-23 11:08:15 +07:00
},
relations: ["kpiRole", "kpiUserEvaluation"],
2024-04-22 18:13:30 +07:00
order: { createdAt: "ASC" },
});
2024-04-22 18:13:30 +07:00
const mapKpiUserRole = kpiUserRole.map((item) => ({
id: item.id,
evaluationId: item.kpiUserEvaluation.id,
2024-04-23 11:08:15 +07:00
kpiRoleId: item.kpiRole.id,
including: item.kpiRole.including,
includingName: item.kpiRole.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.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,
achievement4: item.kpiRole.achievement4,
achievement5: item.kpiRole.achievement5,
2024-04-22 18:13:30 +07:00
}));
return new HttpSuccess(mapKpiUserRole);
}
2024-04-23 17:06:16 +07:00
/**
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 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();
}
2024-04-22 18:13:30 +07:00
}