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

237 lines
7.6 KiB
TypeScript
Raw Normal View History

2024-04-17 16:53:20 +07:00
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";
2024-12-13 23:30:21 +07:00
import { In, Like, Not } from "typeorm";
2024-04-17 16:53:20 +07:00
import HttpStatusCode from "../interfaces/http-status";
2024-04-19 09:44:46 +07:00
import { KpiGroup, createKpiGroup, updateKpiGroup } from "../entities/kpiGroup";
import permission from "../interfaces/permission";
import { RequestWithUser } from "../middlewares/user";
import { setLogDataDiff } from "../interfaces/utils";
2024-12-13 23:30:21 +07:00
import { KpiLink } from "../entities/kpiLink";
import { Position } from "../entities/position";
2024-08-22 14:23:50 +07:00
2024-04-17 16:53:20 +07:00
@Route("api/v1/kpi/group")
2024-04-18 10:24:18 +07:00
@Tags("kpiGroup")
2024-04-17 16:53:20 +07:00
@Security("bearerAuth")
@Response(
HttpStatusCode.INTERNAL_SERVER_ERROR,
"เกิดข้อผิดพลาด ไม่สามารถแสดงรายการได้ กรุณาลองใหม่ในภายหลัง",
)
@SuccessResponse(HttpStatusCode.OK, "สำเร็จ")
export class kpiGroupController extends Controller {
private kpiGroupRepository = AppDataSource.getRepository(KpiGroup);
2024-12-13 23:30:21 +07:00
private kpiLinkRepository = AppDataSource.getRepository(KpiLink);
private positionRepository = AppDataSource.getRepository(Position);
2024-04-17 16:53:20 +07:00
/**
* API
* @param requestBody
* @returns
*/
@Post()
@Example({
nameGroupKPI: "string", //ชื่อกลุ่มงาน
})
async createKpiGroup(@Body() requestBody: createKpiGroup, @Request() request: RequestWithUser) {
await new permission().PermissionCreate(request, "SYS_EVA_COMPETENCY");
2024-04-18 10:24:18 +07:00
const kpiGroup = Object.assign(new KpiGroup(), requestBody);
const chkkpinameGroup = await this.kpiGroupRepository.findOne({
2024-04-18 10:24:18 +07:00
where: {
nameGroupKPI: requestBody.nameGroupKPI,
},
});
if (chkkpinameGroup) {
2024-04-18 10:24:18 +07:00
throw new HttpError(HttpStatusCode.NOT_FOUND, "ชื่อกลุ่มงานนี้มีอยู่ในระบบแล้ว");
}
2024-08-22 14:23:50 +07:00
const before = null;
2024-04-18 10:24:18 +07:00
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();
2024-08-22 14:23:50 +07:00
await this.kpiGroupRepository.save(kpiGroup, { data: request });
setLogDataDiff(request, { before, after: kpiGroup });
2024-04-18 10:24:18 +07:00
return new HttpSuccess(kpiGroup.id);
2024-04-17 16:53:20 +07:00
}
/**
* API
* @param id
*/
@Put("{id}")
async updateKpiGroup(
2024-04-17 16:53:20 +07:00
@Path() id: string,
@Body() requestBody: updateKpiGroup,
@Request() request: RequestWithUser,
2024-04-17 16:53:20 +07:00
) {
await new permission().PermissionUpdate(request, "SYS_EVA_COMPETENCY");
2024-04-18 10:24:18 +07:00
const kpiGroup = await this.kpiGroupRepository.findOne({
2024-04-17 16:53:20 +07:00
where: { id: id },
});
2024-04-18 10:24:18 +07:00
if (!kpiGroup) {
2024-04-17 16:53:20 +07:00
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลกลุ่มงานนี้");
}
const chkkpinameGroup = await this.kpiGroupRepository.findOne({
2024-04-18 10:24:18 +07:00
where: {
nameGroupKPI: requestBody.nameGroupKPI,
id: Not(id),
2024-04-18 10:24:18 +07:00
},
});
if (chkkpinameGroup) {
2024-04-18 10:24:18 +07:00
throw new HttpError(HttpStatusCode.NOT_FOUND, "ชื่อกลุ่มงานนี้มีอยู่ในระบบแล้ว");
}
2024-08-22 14:23:50 +07:00
const before = structuredClone(kpiGroup);
2024-04-18 10:24:18 +07:00
this.kpiGroupRepository.merge(kpiGroup, requestBody);
kpiGroup.lastUpdateUserId = request.user.sub;
kpiGroup.lastUpdateFullName = request.user.name;
kpiGroup.lastUpdatedAt = new Date();
2024-08-22 14:23:50 +07:00
await this.kpiGroupRepository.save(kpiGroup, { data: request });
setLogDataDiff(request, { before, after: kpiGroup });
2024-04-17 16:53:20 +07:00
return new HttpSuccess(id);
}
2024-08-22 17:21:00 +07:00
/**
* API
* @param id
*/
@Get("edit/{id}")
@Example({
nameGroupKPI: "string", //ชื่อกลุ่มงาน
})
async KpiGroupByIdEdit(@Request() request: RequestWithUser, @Path() id: string) {
2024-10-22 08:21:16 +07:00
let _workflow = await new permission().Workflow(request, id, "SYS_EVA_COMPETENCY");
if (_workflow == false) await new permission().PermissionGet(request, "SYS_EVA_COMPETENCY");
2024-08-22 17:21:00 +07:00
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 });
}
2024-04-17 16:53:20 +07:00
/**
* API
* @param id
*/
@Get("{id}")
@Example({
nameGroupKPI: "string", //ชื่อกลุ่มงาน
})
async KpiGroupById(@Request() request: RequestWithUser, @Path() id: string) {
2024-04-18 10:24:18 +07:00
const kpiGroup = await this.kpiGroupRepository.findOne({
2024-04-17 16:53:20 +07:00
where: { id: id },
select: ["nameGroupKPI"],
});
2024-04-18 10:24:18 +07:00
if (!kpiGroup) {
2024-04-17 16:53:20 +07:00
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลกลุ่มงานนี้");
}
2024-04-18 10:24:18 +07:00
return new HttpSuccess(kpiGroup);
2024-04-17 16:53:20 +07:00
}
/**
* API
* @param id
*/
@Delete("{id}")
async deleteKpiGroup(@Path() id: string, @Request() request: RequestWithUser) {
await new permission().PermissionDelete(request, "SYS_EVA_COMPETENCY");
2024-04-18 10:24:18 +07:00
const kpiGroup = await this.kpiGroupRepository.findOne({
2024-04-17 16:53:20 +07:00
where: { id: id },
});
2024-04-18 10:24:18 +07:00
if (!kpiGroup) {
2024-04-17 16:53:20 +07:00
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลกลุ่มงานนี้");
}
2024-12-13 23:30:21 +07:00
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 });
2024-08-22 14:23:50 +07:00
await this.kpiGroupRepository.remove(kpiGroup, { data: request });
2024-04-17 16:53:20 +07:00
return new HttpSuccess();
}
2024-04-18 10:24:18 +07:00
/**
* API list
* @param page
* @param pageSize
*/
@Get()
async listKpiGroup(
@Request() request: RequestWithUser,
2024-04-18 10:24:18 +07:00
@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}%`) }],
};
}
2024-04-18 10:24:18 +07:00
const [kpiGroup, total] = await this.kpiGroupRepository.findAndCount({
...whereClause,
2024-04-18 10:24:18 +07:00
...(keyword ? {} : { skip: (page - 1) * pageSize, take: pageSize }),
2024-04-23 18:15:27 +07:00
order: { createdAt: "ASC" },
2024-04-18 10:24:18 +07:00
});
return new HttpSuccess({ data: kpiGroup, total });
}
2024-04-17 16:53:20 +07:00
}