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";
|
|
|
|
|
import HttpStatusCode from "../interfaces/http-status";
|
2024-04-17 18:15:24 +07:00
|
|
|
import { KpiGroup, creatKpiGroup, updateKpiGroup } from "../entities/kpiGrop";
|
2024-04-17 16:53:20 +07:00
|
|
|
@Route("api/v1/kpi/group")
|
|
|
|
|
@Tags("kpi")
|
|
|
|
|
@Security("bearerAuth")
|
|
|
|
|
@Response(
|
|
|
|
|
HttpStatusCode.INTERNAL_SERVER_ERROR,
|
|
|
|
|
"เกิดข้อผิดพลาด ไม่สามารถแสดงรายการได้ กรุณาลองใหม่ในภายหลัง",
|
|
|
|
|
)
|
|
|
|
|
@SuccessResponse(HttpStatusCode.OK, "สำเร็จ")
|
|
|
|
|
export class kpiGroupController extends Controller {
|
2024-04-17 18:15:24 +07:00
|
|
|
private kpiGroupRepository = AppDataSource.getRepository(KpiGroup);
|
2024-04-17 16:53:20 +07:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* API สร้างกลุ่มงาน
|
|
|
|
|
* @param requestBody
|
|
|
|
|
* @param request
|
|
|
|
|
* @returns
|
|
|
|
|
*/
|
|
|
|
|
@Post()
|
|
|
|
|
@Example({
|
|
|
|
|
nameGroupKPI: "string", //ชื่อกลุ่มงาน
|
|
|
|
|
})
|
2024-04-17 18:15:24 +07:00
|
|
|
async createKpiGroup(
|
|
|
|
|
@Body() requestBody: creatKpiGroup,
|
2024-04-17 16:53:20 +07:00
|
|
|
@Request() request: { user: Record<string, any> },
|
|
|
|
|
) {
|
|
|
|
|
const kpi = Object.assign(new KpiGroup(), requestBody);
|
2024-04-17 18:15:24 +07:00
|
|
|
kpi.nameGroupKPI = requestBody.nameGroupKPI;
|
2024-04-17 16:53:20 +07:00
|
|
|
kpi.createdUserId = request.user.sub;
|
|
|
|
|
kpi.createdFullName = request.user.name;
|
|
|
|
|
kpi.lastUpdateUserId = request.user.sub;
|
|
|
|
|
kpi.lastUpdateFullName = request.user.name;
|
2024-04-17 18:15:24 +07:00
|
|
|
await this.kpiGroupRepository.save(kpi);
|
2024-04-17 16:53:20 +07:00
|
|
|
return new HttpSuccess(kpi.id);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* API แก้ไขชื่อกลุ่มงาน
|
|
|
|
|
* @param id ไอดีของกลุ่มงาน
|
|
|
|
|
*/
|
|
|
|
|
@Put("{id}")
|
2024-04-17 18:15:24 +07:00
|
|
|
async updateKpiGroup(
|
2024-04-17 16:53:20 +07:00
|
|
|
@Path() id: string,
|
2024-04-17 18:15:24 +07:00
|
|
|
@Body() requestBody: updateKpiGroup,
|
2024-04-17 16:53:20 +07:00
|
|
|
@Request() request: { user: Record<string, any> },
|
|
|
|
|
) {
|
2024-04-17 18:15:24 +07:00
|
|
|
const kpiUpdate = await this.kpiGroupRepository.findOne({
|
2024-04-17 16:53:20 +07:00
|
|
|
where: { id: id },
|
|
|
|
|
});
|
2024-04-17 18:15:24 +07:00
|
|
|
if (!kpiUpdate) {
|
2024-04-17 16:53:20 +07:00
|
|
|
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลกลุ่มงานนี้");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
requestBody.nameGroupKPI = requestBody.nameGroupKPI.trim().toUpperCase();
|
2024-04-17 18:15:24 +07:00
|
|
|
this.kpiGroupRepository.merge(kpiUpdate, requestBody);
|
|
|
|
|
kpiUpdate.createdUserId = request.user.sub;
|
|
|
|
|
kpiUpdate.createdFullName = request.user.name;
|
|
|
|
|
kpiUpdate.lastUpdateUserId = request.user.sub;
|
|
|
|
|
kpiUpdate.lastUpdateFullName = request.user.name;
|
|
|
|
|
await this.kpiGroupRepository.save(kpiUpdate);
|
2024-04-17 16:53:20 +07:00
|
|
|
return new HttpSuccess(id);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* API ชื่อกลุ่มงาน
|
|
|
|
|
* @param id
|
|
|
|
|
*/
|
|
|
|
|
@Get("{id}")
|
|
|
|
|
@Example({
|
|
|
|
|
nameGroupKPI: "string", //ชื่อกลุ่มงาน
|
|
|
|
|
})
|
2024-04-17 18:15:24 +07:00
|
|
|
async KpiGroupById(@Path() id: string) {
|
|
|
|
|
const kpi = await this.kpiGroupRepository.findOne({
|
2024-04-17 16:53:20 +07:00
|
|
|
where: { id: id },
|
|
|
|
|
select: ["nameGroupKPI"],
|
|
|
|
|
});
|
|
|
|
|
if (!kpi) {
|
|
|
|
|
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลกลุ่มงานนี้");
|
|
|
|
|
}
|
|
|
|
|
return new HttpSuccess(kpi);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* API ลบกลุ่มงาน
|
|
|
|
|
* @param id
|
|
|
|
|
*/
|
|
|
|
|
@Delete("{id}")
|
2024-04-17 18:15:24 +07:00
|
|
|
async deleteKpiGroup(@Path() id: string) {
|
|
|
|
|
const chkKpiGroup = await this.kpiGroupRepository.findOne({
|
2024-04-17 16:53:20 +07:00
|
|
|
where: { id: id },
|
|
|
|
|
});
|
2024-04-17 18:15:24 +07:00
|
|
|
if (!chkKpiGroup) {
|
2024-04-17 16:53:20 +07:00
|
|
|
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลกลุ่มงานนี้");
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-17 18:15:24 +07:00
|
|
|
await this.kpiGroupRepository.remove(chkKpiGroup);
|
2024-04-17 16:53:20 +07:00
|
|
|
return new HttpSuccess();
|
|
|
|
|
}
|
|
|
|
|
}
|