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

121 lines
3.5 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";
import HttpStatusCode from "../interfaces/http-status";
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 {
private kpiGroupRepository = AppDataSource.getRepository(KpiGroup);
2024-04-17 16:53:20 +07:00
/**
* API
* @param requestBody
* @param request
* @returns
*/
@Post()
@Example({
nameGroupKPI: "string", //ชื่อกลุ่มงาน
})
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);
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;
await this.kpiGroupRepository.save(kpi);
2024-04-17 16:53:20 +07:00
return new HttpSuccess(kpi.id);
}
/**
* API
* @param id
*/
@Put("{id}")
async updateKpiGroup(
2024-04-17 16:53:20 +07:00
@Path() id: string,
@Body() requestBody: updateKpiGroup,
2024-04-17 16:53:20 +07:00
@Request() request: { user: Record<string, any> },
) {
const kpiUpdate = await this.kpiGroupRepository.findOne({
2024-04-17 16:53:20 +07:00
where: { id: id },
});
if (!kpiUpdate) {
2024-04-17 16:53:20 +07:00
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลกลุ่มงานนี้");
}
requestBody.nameGroupKPI = requestBody.nameGroupKPI.trim().toUpperCase();
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", //ชื่อกลุ่มงาน
})
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}")
async deleteKpiGroup(@Path() id: string) {
const chkKpiGroup = await this.kpiGroupRepository.findOne({
2024-04-17 16:53:20 +07:00
where: { id: id },
});
if (!chkKpiGroup) {
2024-04-17 16:53:20 +07:00
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลกลุ่มงานนี้");
}
await this.kpiGroupRepository.remove(chkKpiGroup);
2024-04-17 16:53:20 +07:00
return new HttpSuccess();
}
}