236 lines
7.6 KiB
TypeScript
236 lines
7.6 KiB
TypeScript
import {
|
|
Controller,
|
|
Get,
|
|
Post,
|
|
Put,
|
|
Delete,
|
|
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 HttpError from "../interfaces/http-error";
|
|
import { In, Like, Not } from "typeorm";
|
|
import HttpStatusCode from "../interfaces/http-status";
|
|
import { KpiGroup, createKpiGroup, updateKpiGroup } from "../entities/kpiGroup";
|
|
import permission from "../interfaces/permission";
|
|
import { RequestWithUser } from "../middlewares/user";
|
|
import { setLogDataDiff } from "../interfaces/utils";
|
|
import { KpiLink } from "../entities/kpiLink";
|
|
import { Position } from "../entities/position";
|
|
|
|
@Route("api/v1/kpi/group")
|
|
@Tags("kpiGroup")
|
|
@Security("bearerAuth")
|
|
@Response(
|
|
HttpStatusCode.INTERNAL_SERVER_ERROR,
|
|
"เกิดข้อผิดพลาด ไม่สามารถแสดงรายการได้ กรุณาลองใหม่ในภายหลัง",
|
|
)
|
|
@SuccessResponse(HttpStatusCode.OK, "สำเร็จ")
|
|
export class kpiGroupController extends Controller {
|
|
private kpiGroupRepository = AppDataSource.getRepository(KpiGroup);
|
|
private kpiLinkRepository = AppDataSource.getRepository(KpiLink);
|
|
private positionRepository = AppDataSource.getRepository(Position);
|
|
|
|
/**
|
|
* API สร้างกลุ่มงาน
|
|
* @param requestBody
|
|
* @returns
|
|
*/
|
|
@Post()
|
|
@Example({
|
|
nameGroupKPI: "string", //ชื่อกลุ่มงาน
|
|
})
|
|
async createKpiGroup(@Body() requestBody: createKpiGroup, @Request() request: RequestWithUser) {
|
|
await new permission().PermissionCreate(request, "SYS_EVA_COMPETENCY");
|
|
const kpiGroup = Object.assign(new KpiGroup(), requestBody);
|
|
const chkkpinameGroup = await this.kpiGroupRepository.findOne({
|
|
where: {
|
|
nameGroupKPI: requestBody.nameGroupKPI,
|
|
},
|
|
});
|
|
if (chkkpinameGroup) {
|
|
throw new HttpError(HttpStatusCode.NOT_FOUND, "ชื่อกลุ่มงานนี้มีอยู่ในระบบแล้ว");
|
|
}
|
|
const before = null;
|
|
|
|
kpiGroup.createdUserId = request.user.sub;
|
|
kpiGroup.createdFullName = request.user.name;
|
|
kpiGroup.lastUpdateUserId = request.user.sub;
|
|
kpiGroup.lastUpdateFullName = request.user.name;
|
|
kpiGroup.createdAt = new Date();
|
|
kpiGroup.lastUpdatedAt = new Date();
|
|
await this.kpiGroupRepository.save(kpiGroup, { data: request });
|
|
setLogDataDiff(request, { before, after: kpiGroup });
|
|
|
|
return new HttpSuccess(kpiGroup.id);
|
|
}
|
|
|
|
/**
|
|
* API แก้ไขชื่อกลุ่มงาน
|
|
* @param id ไอดีของกลุ่มงาน
|
|
*/
|
|
@Put("{id}")
|
|
async updateKpiGroup(
|
|
@Path() id: string,
|
|
@Body() requestBody: updateKpiGroup,
|
|
@Request() request: RequestWithUser,
|
|
) {
|
|
await new permission().PermissionUpdate(request, "SYS_EVA_COMPETENCY");
|
|
const kpiGroup = await this.kpiGroupRepository.findOne({
|
|
where: { id: id },
|
|
});
|
|
if (!kpiGroup) {
|
|
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลกลุ่มงานนี้");
|
|
}
|
|
|
|
const chkkpinameGroup = await this.kpiGroupRepository.findOne({
|
|
where: {
|
|
nameGroupKPI: requestBody.nameGroupKPI,
|
|
id: Not(id),
|
|
},
|
|
});
|
|
if (chkkpinameGroup) {
|
|
throw new HttpError(HttpStatusCode.NOT_FOUND, "ชื่อกลุ่มงานนี้มีอยู่ในระบบแล้ว");
|
|
}
|
|
const before = structuredClone(kpiGroup);
|
|
|
|
this.kpiGroupRepository.merge(kpiGroup, requestBody);
|
|
kpiGroup.lastUpdateUserId = request.user.sub;
|
|
kpiGroup.lastUpdateFullName = request.user.name;
|
|
kpiGroup.lastUpdatedAt = new Date();
|
|
await this.kpiGroupRepository.save(kpiGroup, { data: request });
|
|
setLogDataDiff(request, { before, after: kpiGroup });
|
|
|
|
return new HttpSuccess(id);
|
|
}
|
|
|
|
/**
|
|
* API ชื่อกลุ่มงาน
|
|
* @param id
|
|
*/
|
|
@Get("edit/{id}")
|
|
@Example({
|
|
nameGroupKPI: "string", //ชื่อกลุ่มงาน
|
|
})
|
|
async KpiGroupByIdEdit(@Request() request: RequestWithUser, @Path() id: string) {
|
|
let _workflow = await new permission().Workflow(request, id, "SYS_EVA_COMPETENCY");
|
|
if (_workflow == false) await new permission().PermissionGet(request, "SYS_EVA_COMPETENCY");
|
|
const kpiGroup = await this.kpiGroupRepository.findOne({
|
|
where: { id: id },
|
|
select: ["nameGroupKPI"],
|
|
});
|
|
if (!kpiGroup) {
|
|
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลกลุ่มงานนี้");
|
|
}
|
|
return new HttpSuccess(kpiGroup);
|
|
}
|
|
|
|
/**
|
|
* API list กลุ่มงาน
|
|
* @param page
|
|
* @param pageSize
|
|
*/
|
|
@Get("edit")
|
|
async listKpiGroupEdit(
|
|
@Request() request: RequestWithUser,
|
|
@Query("page") page: number = 1,
|
|
@Query("pageSize") pageSize: number = 10,
|
|
@Query("keyword") keyword?: string,
|
|
) {
|
|
let _data = await new permission().PermissionList(request, "SYS_EVA_COMPETENCY");
|
|
let whereClause: any = {};
|
|
|
|
if (keyword !== undefined && keyword !== "") {
|
|
whereClause = {
|
|
where: [{ nameGroupKPI: Like(`%${keyword}%`) }],
|
|
};
|
|
}
|
|
|
|
const [kpiGroup, total] = await this.kpiGroupRepository.findAndCount({
|
|
...whereClause,
|
|
...(keyword ? {} : { skip: (page - 1) * pageSize, take: pageSize }),
|
|
order: { createdAt: "ASC" },
|
|
});
|
|
|
|
return new HttpSuccess({ data: kpiGroup, total });
|
|
}
|
|
|
|
/**
|
|
* API ชื่อกลุ่มงาน
|
|
* @param id
|
|
*/
|
|
@Get("{id}")
|
|
@Example({
|
|
nameGroupKPI: "string", //ชื่อกลุ่มงาน
|
|
})
|
|
async KpiGroupById(@Request() request: RequestWithUser, @Path() id: string) {
|
|
const kpiGroup = await this.kpiGroupRepository.findOne({
|
|
where: { id: id },
|
|
select: ["nameGroupKPI"],
|
|
});
|
|
if (!kpiGroup) {
|
|
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลกลุ่มงานนี้");
|
|
}
|
|
return new HttpSuccess(kpiGroup);
|
|
}
|
|
|
|
/**
|
|
* API ลบกลุ่มงาน
|
|
* @param id
|
|
*/
|
|
@Delete("{id}")
|
|
async deleteKpiGroup(@Path() id: string, @Request() request: RequestWithUser) {
|
|
await new permission().PermissionDelete(request, "SYS_EVA_COMPETENCY");
|
|
const kpiGroup = await this.kpiGroupRepository.findOne({
|
|
where: { id: id },
|
|
});
|
|
if (!kpiGroup) {
|
|
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลกลุ่มงานนี้");
|
|
}
|
|
const kpiLink = await this.kpiLinkRepository.find({
|
|
where: { kpiGroupId: id },
|
|
});
|
|
await this.positionRepository.delete({ kpiLinkId: In(kpiLink.map((x) => x.id)) });
|
|
await this.kpiLinkRepository.remove(kpiLink, { data: request });
|
|
await this.kpiGroupRepository.remove(kpiGroup, { data: request });
|
|
return new HttpSuccess();
|
|
}
|
|
|
|
/**
|
|
* API list กลุ่มงาน
|
|
* @param page
|
|
* @param pageSize
|
|
*/
|
|
@Get()
|
|
async listKpiGroup(
|
|
@Request() request: RequestWithUser,
|
|
@Query("page") page: number = 1,
|
|
@Query("pageSize") pageSize: number = 10,
|
|
@Query("keyword") keyword?: string,
|
|
) {
|
|
let whereClause: any = {};
|
|
|
|
if (keyword !== undefined && keyword !== "") {
|
|
whereClause = {
|
|
where: [{ nameGroupKPI: Like(`%${keyword}%`) }],
|
|
};
|
|
}
|
|
|
|
const [kpiGroup, total] = await this.kpiGroupRepository.findAndCount({
|
|
...whereClause,
|
|
...(keyword ? {} : { skip: (page - 1) * pageSize, take: pageSize }),
|
|
order: { createdAt: "ASC" },
|
|
});
|
|
|
|
return new HttpSuccess({ data: kpiGroup, total });
|
|
}
|
|
}
|