154 lines
5.1 KiB
TypeScript
154 lines
5.1 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 { KpiPeriod, createKpi, updateKpi } from "../entities/kpi";
|
||
|
|
|
||
|
|
@Route("api/v1/kpi/period")
|
||
|
|
@Tags("kpi")
|
||
|
|
@Security("bearerAuth")
|
||
|
|
@Response(
|
||
|
|
HttpStatusCode.INTERNAL_SERVER_ERROR,
|
||
|
|
"เกิดข้อผิดพลาด ไม่สามารถแสดงรายการได้ กรุณาลองใหม่ในภายหลัง",
|
||
|
|
)
|
||
|
|
@SuccessResponse(HttpStatusCode.OK, "สำเร็จ")
|
||
|
|
export class kpiController extends Controller {
|
||
|
|
private kpiRepository = AppDataSource.getRepository(KpiPeriod);
|
||
|
|
/**
|
||
|
|
* สร้างรอบการประเมินผลการปฏิบัติหน้าที่ราชการ
|
||
|
|
* @param requestBody
|
||
|
|
* @param request
|
||
|
|
*/
|
||
|
|
@Post()
|
||
|
|
@Example({
|
||
|
|
durationKPI: "string", //รอบเดือนที่สร้าง
|
||
|
|
startDate: "datetime", //วันเริ่มต้น
|
||
|
|
endDate: "datetime", //วันสิ้นสุด
|
||
|
|
})
|
||
|
|
async createKpi(
|
||
|
|
@Body() requestBody: createKpi,
|
||
|
|
@Request() request: { user: Record<string, any> },
|
||
|
|
) {
|
||
|
|
const kpi = Object.assign(new KpiPeriod(), requestBody);
|
||
|
|
kpi.durationKPI = requestBody.durationKPI.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
|
||
|
|
* @param requestBody
|
||
|
|
* @param request
|
||
|
|
*/
|
||
|
|
@Put("{id}")
|
||
|
|
async update_kpi(
|
||
|
|
@Path() id: string,
|
||
|
|
@Body() requestBody: updateKpi,
|
||
|
|
@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.durationKPI = requestBody.durationKPI.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 Guid, *Id รอบการประเมินผลการปฏิบัติหน้าที่ราชการ
|
||
|
|
*/
|
||
|
|
@Get("{id}")
|
||
|
|
@Example({
|
||
|
|
durationKPI: "string", //รอบเดือนที่สร้าง
|
||
|
|
startDate: "datetime", //วันเริ่มต้น
|
||
|
|
endDate: "datetime", //วันสิ้นสุด
|
||
|
|
})
|
||
|
|
async GetSalaryById(@Path() id: string) {
|
||
|
|
const kpi = await this.kpiRepository.findOne({
|
||
|
|
where: { id: id },
|
||
|
|
select: ["durationKPI", "startDate", "endDate"],
|
||
|
|
});
|
||
|
|
if (!kpi) {
|
||
|
|
throw new HttpError(
|
||
|
|
HttpStatusCode.NOT_FOUND,
|
||
|
|
"ไม่พบข้อมูลรอบการประเมินผลการปฏิบัติหน้าที่ราชการนี้",
|
||
|
|
);
|
||
|
|
}
|
||
|
|
return new HttpSuccess(kpi);
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* API list รอบการประเมินผลการปฏิบัติหน้าที่ราชการ
|
||
|
|
* @param page
|
||
|
|
* @param pageSize
|
||
|
|
* @param keyword
|
||
|
|
*/
|
||
|
|
@Get()
|
||
|
|
async listKpiPeriod(
|
||
|
|
@Query("page") page: number = 1,
|
||
|
|
@Query("pageSize") pageSize: number = 10,
|
||
|
|
@Query("keyword") keyword?: string,
|
||
|
|
) {
|
||
|
|
const [kpiPeriod, total] = await this.kpiRepository.findAndCount({
|
||
|
|
...(keyword ? {} : { skip: (page - 1) * pageSize, take: pageSize }),
|
||
|
|
});
|
||
|
|
|
||
|
|
return new HttpSuccess({ data: kpiPeriod, total });
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* API ลบรอบการประเมินผลการปฏิบัติหน้าที่ราชการ
|
||
|
|
* @param id
|
||
|
|
*/
|
||
|
|
@Delete("{id}")
|
||
|
|
async delete_salary(@Path() id: string) {
|
||
|
|
const chk_KpiPeriod = await this.kpiRepository.findOne({
|
||
|
|
where: { id: id },
|
||
|
|
});
|
||
|
|
if (!chk_KpiPeriod) {
|
||
|
|
throw new HttpError(
|
||
|
|
HttpStatusCode.NOT_FOUND,
|
||
|
|
"ไม่พบข้อมูลรอบการประเมินผลการปฏิบัติหน้าที่ราชการนี้",
|
||
|
|
);
|
||
|
|
}
|
||
|
|
|
||
|
|
await this.kpiRepository.remove(chk_KpiPeriod);
|
||
|
|
return new HttpSuccess();
|
||
|
|
}
|
||
|
|
}
|