121 lines
3.5 KiB
TypeScript
121 lines
3.5 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 HttpStatusCode from "../interfaces/http-status";
|
||
|
|
import { KpiGroup, creatGroupKpi, updateGroupKpi } from "../entities/kpiGrop";
|
||
|
|
@Route("api/v1/kpi/group")
|
||
|
|
@Tags("kpi")
|
||
|
|
@Security("bearerAuth")
|
||
|
|
@Response(
|
||
|
|
HttpStatusCode.INTERNAL_SERVER_ERROR,
|
||
|
|
"เกิดข้อผิดพลาด ไม่สามารถแสดงรายการได้ กรุณาลองใหม่ในภายหลัง",
|
||
|
|
)
|
||
|
|
@SuccessResponse(HttpStatusCode.OK, "สำเร็จ")
|
||
|
|
export class kpiGroupController extends Controller {
|
||
|
|
private kpiRepository = AppDataSource.getRepository(KpiGroup);
|
||
|
|
|
||
|
|
/**
|
||
|
|
* API สร้างกลุ่มงาน
|
||
|
|
* @param requestBody
|
||
|
|
* @param request
|
||
|
|
* @returns
|
||
|
|
*/
|
||
|
|
@Post()
|
||
|
|
@Example({
|
||
|
|
nameGroupKPI: "string", //ชื่อกลุ่มงาน
|
||
|
|
})
|
||
|
|
async createKpi(
|
||
|
|
@Body() requestBody: creatGroupKpi,
|
||
|
|
@Request() request: { user: Record<string, any> },
|
||
|
|
) {
|
||
|
|
const kpi = Object.assign(new KpiGroup(), requestBody);
|
||
|
|
kpi.nameGroupKPI = requestBody.nameGroupKPI.trim().toUpperCase();
|
||
|
|
kpi.createdUserId = request.user.sub;
|
||
|
|
kpi.createdFullName = request.user.name;
|
||
|
|
kpi.lastUpdateUserId = request.user.sub;
|
||
|
|
kpi.lastUpdateFullName = request.user.name;
|
||
|
|
await this.kpiRepository.save(kpi);
|
||
|
|
return new HttpSuccess(kpi.id);
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* API แก้ไขชื่อกลุ่มงาน
|
||
|
|
* @param id ไอดีของกลุ่มงาน
|
||
|
|
*/
|
||
|
|
@Put("{id}")
|
||
|
|
async update_kpi(
|
||
|
|
@Path() id: string,
|
||
|
|
@Body() requestBody: updateGroupKpi,
|
||
|
|
@Request() request: { user: Record<string, any> },
|
||
|
|
) {
|
||
|
|
const kpi_update = await this.kpiRepository.findOne({
|
||
|
|
where: { id: id },
|
||
|
|
});
|
||
|
|
if (!kpi_update) {
|
||
|
|
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลกลุ่มงานนี้");
|
||
|
|
}
|
||
|
|
|
||
|
|
requestBody.nameGroupKPI = requestBody.nameGroupKPI.trim().toUpperCase();
|
||
|
|
this.kpiRepository.merge(kpi_update, requestBody);
|
||
|
|
kpi_update.createdUserId = request.user.sub;
|
||
|
|
kpi_update.createdFullName = request.user.name;
|
||
|
|
kpi_update.lastUpdateUserId = request.user.sub;
|
||
|
|
kpi_update.lastUpdateFullName = request.user.name;
|
||
|
|
await this.kpiRepository.save(kpi_update);
|
||
|
|
return new HttpSuccess(id);
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* API ชื่อกลุ่มงาน
|
||
|
|
* @param id
|
||
|
|
*/
|
||
|
|
@Get("{id}")
|
||
|
|
@Example({
|
||
|
|
nameGroupKPI: "string", //ชื่อกลุ่มงาน
|
||
|
|
})
|
||
|
|
async CloseSalaryById(@Path() id: string) {
|
||
|
|
const kpi = await this.kpiRepository.findOne({
|
||
|
|
where: { id: id },
|
||
|
|
select: ["nameGroupKPI"],
|
||
|
|
});
|
||
|
|
if (!kpi) {
|
||
|
|
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลกลุ่มงานนี้");
|
||
|
|
}
|
||
|
|
return new HttpSuccess(kpi);
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* API ลบกลุ่มงาน
|
||
|
|
* @param id
|
||
|
|
*/
|
||
|
|
@Delete("{id}")
|
||
|
|
async delete_salary(@Path() id: string) {
|
||
|
|
const chk_KpiGroup = await this.kpiRepository.findOne({
|
||
|
|
where: { id: id },
|
||
|
|
});
|
||
|
|
if (!chk_KpiGroup) {
|
||
|
|
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลกลุ่มงานนี้");
|
||
|
|
}
|
||
|
|
|
||
|
|
await this.kpiRepository.remove(chk_KpiGroup);
|
||
|
|
return new HttpSuccess();
|
||
|
|
}
|
||
|
|
}
|