checkpoint crud Achievement
This commit is contained in:
parent
245e7f9df5
commit
f16fff075f
4 changed files with 425 additions and 225 deletions
183
src/controllers/KpiUserRoleController.ts
Normal file
183
src/controllers/KpiUserRoleController.ts
Normal file
|
|
@ -0,0 +1,183 @@
|
|||
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 } from "../entities/kpiUserRole";
|
||||
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},
|
||||
})
|
||||
if (!chkUserEvaluation) {
|
||||
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลแบบประเมินผู้ใช้งาน");
|
||||
}
|
||||
|
||||
const chkKpiRole = await this.kpiRoleRepository.findOne({
|
||||
where:{id:requestBody.kpiRoleId},
|
||||
})
|
||||
if (!chkKpiRole) {
|
||||
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลแบบประเมินตามหน้าที่ความรับผิดชอบหลัก");
|
||||
}
|
||||
|
||||
const kpiUserRole = Object.assign(new KpiUserRole(), requestBody);
|
||||
if (!kpiUserRole) {
|
||||
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล");
|
||||
}
|
||||
|
||||
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, "ไม่พบข้อมูลงานตามหน้าที่ความรับผิดชอบหลักนี้");
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
// /**
|
||||
// * API รายละเอียดงานตามหน้าที่ความรับผิดชอบหลัก
|
||||
// *
|
||||
// * @summary - รายละเอียดงานตามหน้าที่ความรับผิดชอบหลัก #51
|
||||
// *
|
||||
// * @param {string} id Id งานตามหน้าที่ความรับผิดชอบหลัก
|
||||
// */
|
||||
// @Get("{id}")
|
||||
// async GetKpiUserRoleDetail(@Path() id: string) {
|
||||
// const getKpiUserRole = await this.kpiUserRoleRepository.findOne({
|
||||
// select: ["id", "kpiUserRoleName", "kpiUserRoleRank"],
|
||||
// relations: ["posLevels"],
|
||||
// where: { id: id },
|
||||
// });
|
||||
// if (!getKpiUserRole) {
|
||||
// throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลงานตามหน้าที่ความรับผิดชอบหลักนี้");
|
||||
// }
|
||||
|
||||
// const mapGetKpiUserRole = {
|
||||
// id: getKpiUserRole.id,
|
||||
// kpiUserRoleName: getKpiUserRole.kpiUserRoleName,
|
||||
// kpiUserRoleRank: getKpiUserRole.kpiUserRoleRank,
|
||||
// posLevels: getKpiUserRole.posLevels
|
||||
// .sort((a, b) => a.posLevelRank - b.posLevelRank)
|
||||
// .map((posLevel) => ({
|
||||
// id: posLevel.id,
|
||||
// posLevelName: posLevel.posLevelName,
|
||||
// posLevelRank: posLevel.posLevelRank,
|
||||
// posLevelAuthority: posLevel.posLevelAuthority,
|
||||
// })),
|
||||
// };
|
||||
|
||||
// return new HttpSuccess(mapGetKpiUserRole);
|
||||
// }
|
||||
|
||||
/**
|
||||
* API รายการงานตามหน้าที่ความรับผิดชอบหลัก
|
||||
*
|
||||
* @summary - รายการงานตามหน้าที่ความรับผิดชอบหลัก #29
|
||||
*
|
||||
*/
|
||||
@Get()
|
||||
async GetKpiUserRole() {
|
||||
const kpiUserRole = await this.kpiUserRoleRepository.find({
|
||||
relations: ["kpiRole","kpiUserEvaluation"],
|
||||
order: { createdAt: "ASC" },
|
||||
});
|
||||
// if (!kpiUserRole) {
|
||||
// return new HttpSuccess([]);
|
||||
// }
|
||||
const mapKpiUserRole = kpiUserRole.map((item) => ({
|
||||
id: item.id,
|
||||
indicatorId: item.kpiRole.id,
|
||||
target: item.target,
|
||||
unit: item.unit,
|
||||
weight: item.weight,
|
||||
meaning: item.meaning,
|
||||
formula: item.formula,
|
||||
}));
|
||||
return new HttpSuccess(mapKpiUserRole);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue