207 lines
7.4 KiB
TypeScript
207 lines
7.4 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, createKpiPeriod, updateKpiPeriod } 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 kpiPeriodRepository = AppDataSource.getRepository(KpiPeriod);
|
|
/**
|
|
* สร้างรอบการประเมินผลการปฏิบัติหน้าที่ราชการ
|
|
* @param requestBody
|
|
* @param request
|
|
*/
|
|
@Post()
|
|
@Example({
|
|
durationKPI: "string", //รอบเดือนที่สร้าง
|
|
startDate: "datetime", //วันเริ่มต้น
|
|
endDate: "datetime", //วันสิ้นสุด
|
|
})
|
|
async createKpi(
|
|
@Body() requestBody: createKpiPeriod,
|
|
@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.kpiPeriodRepository.save(kpi);
|
|
return new HttpSuccess(kpi.id);
|
|
}
|
|
|
|
/**
|
|
* API แก้ไขรอบการประเมินผลการปฏิบัติหน้าที่ราชการ
|
|
* @param id
|
|
* @param requestBody
|
|
* @param request
|
|
*/
|
|
@Put("{id}")
|
|
async updateKpiPeriod(
|
|
@Path() id: string,
|
|
@Body() requestBody: updateKpiPeriod,
|
|
@Request() request: { user: Record<string, any> },
|
|
) {
|
|
const kpiUpdate = await this.kpiPeriodRepository.findOne({
|
|
where: { id: id },
|
|
});
|
|
if (!kpiUpdate) {
|
|
throw new HttpError(
|
|
HttpStatusCode.NOT_FOUND,
|
|
"ไม่พบข้อมูลรอบการประเมินผลการปฏิบัติหน้าที่ราชการนี้",
|
|
);
|
|
}
|
|
|
|
requestBody.durationKPI = requestBody.durationKPI.trim().toUpperCase();
|
|
this.kpiPeriodRepository.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.kpiPeriodRepository.save(kpiUpdate);
|
|
return new HttpSuccess(id);
|
|
}
|
|
|
|
/**
|
|
* API ปิดรอบการประเมินผลการปฏิบัติหน้าที่ราชการ
|
|
* @param id Guid, *Id ปิดรอบการประเมินผลการปฏิบัติหน้าที่ราชการ
|
|
*/
|
|
@Get("close/{id}")
|
|
@Example({
|
|
durationKPI: "string", //รอบเดือนที่สร้าง
|
|
startDate: "datetime", //วันเริ่มต้น
|
|
endDate: "datetime", //วันสิ้นสุด
|
|
})
|
|
async CloseKpiPeriodById(@Path() id: string) {
|
|
const kpi = await this.kpiPeriodRepository.findOne({
|
|
where: { id: id },
|
|
});
|
|
if (!kpi) {
|
|
throw new HttpError(
|
|
HttpStatusCode.NOT_FOUND,
|
|
"ไม่พบข้อมูลรอบการประเมินผลการปฏิบัติหน้าที่ราชการนี้",
|
|
);
|
|
}
|
|
kpi.isActive = false;
|
|
await this.kpiPeriodRepository.save(kpi);
|
|
return new HttpSuccess(kpi);
|
|
}
|
|
|
|
/**
|
|
* API เปิดรอบการประเมินผลการปฏิบัติหน้าที่ราชการ
|
|
* @param id Guid, *Id เปิดรอบการประเมินผลการปฏิบัติหน้าที่ราชการ
|
|
*/
|
|
@Get("open/{id}")
|
|
@Example({
|
|
durationKPI: "string", //รอบเดือนที่สร้าง
|
|
startDate: "datetime", //วันเริ่มต้น
|
|
endDate: "datetime", //วันสิ้นสุด
|
|
})
|
|
async OpenKpiPeriodById(@Path() id: string) {
|
|
const kpi = await this.kpiPeriodRepository.findOne({
|
|
where: { id: id },
|
|
});
|
|
if (!kpi) {
|
|
throw new HttpError(
|
|
HttpStatusCode.NOT_FOUND,
|
|
"ไม่พบข้อมูลรอบการประเมินผลการปฏิบัติหน้าที่ราชการนี้",
|
|
);
|
|
}
|
|
kpi.isActive = true;
|
|
await this.kpiPeriodRepository.save(kpi);
|
|
return new HttpSuccess(kpi);
|
|
}
|
|
|
|
/**
|
|
* API รอบการประเมินผลการปฏิบัติหน้าที่ราชการ
|
|
* @param id Guid, *Id รอบการประเมินผลการปฏิบัติหน้าที่ราชการ
|
|
*/
|
|
@Get("{id}")
|
|
@Example({
|
|
durationKPI: "string", //รอบเดือนที่สร้าง
|
|
startDate: "datetime", //วันเริ่มต้น
|
|
endDate: "datetime", //วันสิ้นสุด
|
|
})
|
|
async GetKpiPeriodById(@Path() id: string) {
|
|
const kpi = await this.kpiPeriodRepository.findOne({
|
|
where: { id: id },
|
|
select: ["durationKPI", "startDate", "endDate", "isActive"],
|
|
});
|
|
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("year") year?: string,
|
|
@Query("keyword") keyword?: string,
|
|
) {
|
|
const [kpiPeriod, total] = await this.kpiPeriodRepository.findAndCount({
|
|
// where: {
|
|
// name: Like(`%${keyword}%`),
|
|
// },
|
|
...(keyword ? {} : { skip: (page - 1) * pageSize, take: pageSize }),
|
|
});
|
|
|
|
return new HttpSuccess({ data: kpiPeriod, total });
|
|
}
|
|
|
|
/**
|
|
* API ลบรอบการประเมินผลการปฏิบัติหน้าที่ราชการ
|
|
* @param id
|
|
*/
|
|
@Delete("{id}")
|
|
async deleteKpiPerriod(@Path() id: string) {
|
|
const chkKpiPeriod = await this.kpiPeriodRepository.findOne({
|
|
where: { id: id },
|
|
});
|
|
if (!chkKpiPeriod) {
|
|
throw new HttpError(
|
|
HttpStatusCode.NOT_FOUND,
|
|
"ไม่พบข้อมูลรอบการประเมินผลการปฏิบัติหน้าที่ราชการนี้",
|
|
);
|
|
}
|
|
|
|
await this.kpiPeriodRepository.remove(chkKpiPeriod);
|
|
return new HttpSuccess();
|
|
}
|
|
}
|