286 lines
11 KiB
TypeScript
286 lines
11 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/kpiPeriod";
|
|
import { In, Not } from "typeorm";
|
|
import { KpiUserEvaluation } from "../entities/kpiUserEvaluation";
|
|
import { KpiPlan } from "../entities/kpiPlan";
|
|
import { KpiRole } from "../entities/kpiRole";
|
|
import { KpiUserRole } from "../entities/kpiUserRole";
|
|
import { KpiUserPlanned } from "../entities/kpiUserPlanned";
|
|
import { KpiUserCapacity } from "../entities/kpiUserCapacity";
|
|
import { KpiUserSpecial } from "../entities/kpiUserSpecial";
|
|
|
|
@Route("api/v1/kpi/period")
|
|
@Tags("kpiPeriod")
|
|
@Security("bearerAuth")
|
|
@Response(
|
|
HttpStatusCode.INTERNAL_SERVER_ERROR,
|
|
"เกิดข้อผิดพลาด ไม่สามารถแสดงรายการได้ กรุณาลองใหม่ในภายหลัง",
|
|
)
|
|
@SuccessResponse(HttpStatusCode.OK, "สำเร็จ")
|
|
export class kpiPeriodController extends Controller {
|
|
private kpiPeriodRepository = AppDataSource.getRepository(KpiPeriod);
|
|
private kpiUserEvaluationRepository = AppDataSource.getRepository(KpiUserEvaluation);
|
|
private kpiRoleRepository = AppDataSource.getRepository(KpiRole);
|
|
private kpiPlanRepository = AppDataSource.getRepository(KpiPlan);
|
|
private kpiUserRoleRepository = AppDataSource.getRepository(KpiUserRole);
|
|
private kpiUserPlannedRepository = AppDataSource.getRepository(KpiUserPlanned);
|
|
private kpiUserCapacityRepository = AppDataSource.getRepository(KpiUserCapacity);
|
|
private kpiUserSpecialRepository = AppDataSource.getRepository(KpiUserSpecial);
|
|
/**
|
|
* สร้างรอบการประเมินผลการปฏิบัติหน้าที่ราชการ
|
|
* @param requestBody
|
|
* @param request
|
|
*/
|
|
@Post()
|
|
@Example({
|
|
durationKPI: "string", //รอบเดือนที่สร้าง
|
|
startDate: "datetime", //วันเริ่มต้น
|
|
endDate: "datetime", //วันสิ้นสุด
|
|
})
|
|
async createKpi(
|
|
@Body() requestBody: createKpiPeriod,
|
|
@Request() request: { user: Record<string, any> },
|
|
) {
|
|
const chkkpiPeriod = await this.kpiPeriodRepository.findOne({
|
|
where: {
|
|
durationKPI: requestBody.durationKPI,
|
|
year: requestBody.year,
|
|
},
|
|
});
|
|
if (chkkpiPeriod) {
|
|
throw new HttpError(HttpStatusCode.NOT_FOUND, "รอบการประเมินผลนี้มีอยู่ในระบบแล้ว");
|
|
}
|
|
const kpiPeriod = Object.assign(new KpiPeriod(), requestBody);
|
|
kpiPeriod.durationKPI = requestBody.durationKPI.trim().toUpperCase();
|
|
kpiPeriod.createdUserId = request.user.sub;
|
|
kpiPeriod.createdFullName = request.user.name;
|
|
kpiPeriod.lastUpdateUserId = request.user.sub;
|
|
kpiPeriod.lastUpdateFullName = request.user.name;
|
|
await this.kpiPeriodRepository.save(kpiPeriod);
|
|
return new HttpSuccess(kpiPeriod.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 kpiPeriod = await this.kpiPeriodRepository.findOne({
|
|
where: { id: id },
|
|
});
|
|
if (!kpiPeriod) {
|
|
throw new HttpError(
|
|
HttpStatusCode.NOT_FOUND,
|
|
"ไม่พบข้อมูลรอบการประเมินผลการปฏิบัติหน้าที่ราชการนี้",
|
|
);
|
|
}
|
|
const chkkpiPeriod = await this.kpiPeriodRepository.findOne({
|
|
where: {
|
|
id: Not(id),
|
|
durationKPI: requestBody.durationKPI,
|
|
year: requestBody.year,
|
|
},
|
|
});
|
|
if (chkkpiPeriod) {
|
|
throw new HttpError(HttpStatusCode.NOT_FOUND, "รอบการประเมินผลนี้มีอยู่ในระบบแล้ว");
|
|
}
|
|
|
|
requestBody.durationKPI = requestBody.durationKPI.trim().toUpperCase();
|
|
this.kpiPeriodRepository.merge(kpiPeriod, requestBody);
|
|
kpiPeriod.createdUserId = request.user.sub;
|
|
kpiPeriod.createdFullName = request.user.name;
|
|
kpiPeriod.lastUpdateUserId = request.user.sub;
|
|
kpiPeriod.lastUpdateFullName = request.user.name;
|
|
await this.kpiPeriodRepository.save(kpiPeriod);
|
|
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 kpiPeriod = await this.kpiPeriodRepository.findOne({
|
|
where: { id: id },
|
|
});
|
|
if (!kpiPeriod) {
|
|
throw new HttpError(
|
|
HttpStatusCode.NOT_FOUND,
|
|
"ไม่พบข้อมูลรอบการประเมินผลการปฏิบัติหน้าที่ราชการนี้",
|
|
);
|
|
}
|
|
kpiPeriod.isActive = false;
|
|
await this.kpiPeriodRepository.save(kpiPeriod);
|
|
return new HttpSuccess(kpiPeriod);
|
|
}
|
|
|
|
/**
|
|
* API เปิดรอบการประเมินผลการปฏิบัติหน้าที่ราชการ
|
|
* @param id Guid, *Id เปิดรอบการประเมินผลการปฏิบัติหน้าที่ราชการ
|
|
*/
|
|
@Get("open/{id}")
|
|
@Example({
|
|
durationKPI: "string", //รอบเดือนที่สร้าง
|
|
startDate: "datetime", //วันเริ่มต้น
|
|
endDate: "datetime", //วันสิ้นสุด
|
|
})
|
|
async OpenKpiPeriodById(@Path() id: string) {
|
|
const kpiPeriod = await this.kpiPeriodRepository.findOne({
|
|
where: { id: id },
|
|
});
|
|
if (!kpiPeriod) {
|
|
throw new HttpError(
|
|
HttpStatusCode.NOT_FOUND,
|
|
"ไม่พบข้อมูลรอบการประเมินผลการปฏิบัติหน้าที่ราชการนี้",
|
|
);
|
|
}
|
|
kpiPeriod.isActive = true;
|
|
await this.kpiPeriodRepository.save(kpiPeriod);
|
|
return new HttpSuccess(kpiPeriod);
|
|
}
|
|
|
|
/**
|
|
* API รอบการประเมินผลการปฏิบัติหน้าที่ราชการ
|
|
* @param id Guid, *Id รอบการประเมินผลการปฏิบัติหน้าที่ราชการ
|
|
*/
|
|
@Get("{id}")
|
|
@Example({
|
|
durationKPI: "string", //รอบเดือนที่สร้าง
|
|
startDate: "datetime", //วันเริ่มต้น
|
|
endDate: "datetime", //วันสิ้นสุด
|
|
})
|
|
async GetKpiPeriodById(@Path() id: string) {
|
|
const kpiPeriod = await this.kpiPeriodRepository.findOne({
|
|
where: { id: id },
|
|
select: ["year", "durationKPI", "startDate", "endDate", "isActive"],
|
|
});
|
|
if (!kpiPeriod) {
|
|
throw new HttpError(
|
|
HttpStatusCode.NOT_FOUND,
|
|
"ไม่พบข้อมูลรอบการประเมินผลการปฏิบัติหน้าที่ราชการนี้",
|
|
);
|
|
}
|
|
return new HttpSuccess(kpiPeriod);
|
|
}
|
|
|
|
/**
|
|
* API list รอบการประเมินผลการปฏิบัติหน้าที่ราชการ
|
|
* @param page
|
|
* @param pageSize
|
|
* @param keyword
|
|
*/
|
|
@Get()
|
|
async listKpiPeriod(
|
|
@Query("page") page: number = 1,
|
|
@Query("pageSize") pageSize: number = 10,
|
|
@Query("year") year?: number,
|
|
@Query("keyword") keyword?: string,
|
|
) {
|
|
const [kpiPeriod, total] = await AppDataSource.getRepository(KpiPeriod)
|
|
.createQueryBuilder("kpiPeriod")
|
|
.andWhere(
|
|
year !== 0 && year != null && year != undefined ? "kpiPeriod.year = :year" : "1=1",
|
|
{ year: year },
|
|
)
|
|
.orderBy("kpiPeriod.createdAt", "DESC")
|
|
.skip((page - 1) * pageSize)
|
|
.take(pageSize)
|
|
.getManyAndCount();
|
|
|
|
return new HttpSuccess({ data: kpiPeriod, total });
|
|
}
|
|
|
|
/**
|
|
* API ลบรอบการประเมินผลการปฏิบัติหน้าที่ราชการ
|
|
* @param id
|
|
*/
|
|
@Delete("{id}")
|
|
async deleteKpiPeriod(@Path() id: string) {
|
|
const kpiPeriod = await this.kpiPeriodRepository.findOne({
|
|
where: { id: id },
|
|
});
|
|
if (!kpiPeriod) {
|
|
throw new HttpError(
|
|
HttpStatusCode.NOT_FOUND,
|
|
"ไม่พบข้อมูลรอบการประเมินผลการปฏิบัติหน้าที่ราชการนี้",
|
|
);
|
|
}
|
|
const kpiRole = await this.kpiRoleRepository.find({
|
|
where: { kpiPeriodId: id },
|
|
});
|
|
const kpiPlan = await this.kpiPlanRepository.find({
|
|
where: { kpiPeriodId: id },
|
|
});
|
|
const kpiUserEvaluation = await this.kpiUserEvaluationRepository.find({
|
|
where: { kpiPeriodId: id },
|
|
});
|
|
|
|
const kpiUserRole = await this.kpiUserRoleRepository.find({
|
|
where: { kpiRoleId: In(kpiRole.map((x) => x.id)) },
|
|
});
|
|
|
|
const kpiUserPlanned = await this.kpiUserPlannedRepository.find({
|
|
where: { kpiPlanId: In(kpiPlan.map((x) => x.id)) },
|
|
});
|
|
|
|
const _kpiUserRole = await this.kpiUserRoleRepository.find({
|
|
where: { kpiUserEvaluationId: In(kpiRole.map((x) => x.id)) },
|
|
});
|
|
const _kpiUserPlanned = await this.kpiUserPlannedRepository.find({
|
|
where: { kpiUserEvaluationId: In(kpiPlan.map((x) => x.id)) },
|
|
});
|
|
const _kpiUserCapacity = await this.kpiUserCapacityRepository.find({
|
|
where: { kpiUserEvaluationId: In(kpiPlan.map((x) => x.id)) },
|
|
});
|
|
const _kpiUserSpecial = await this.kpiUserSpecialRepository.find({
|
|
where: { kpiUserEvaluationId: In(kpiPlan.map((x) => x.id)) },
|
|
});
|
|
|
|
await this.kpiUserRoleRepository.remove(kpiUserRole);
|
|
await this.kpiUserPlannedRepository.remove(kpiUserPlanned);
|
|
|
|
await this.kpiUserRoleRepository.remove(_kpiUserRole);
|
|
await this.kpiUserPlannedRepository.remove(_kpiUserPlanned);
|
|
await this.kpiUserCapacityRepository.remove(_kpiUserCapacity);
|
|
await this.kpiUserSpecialRepository.remove(_kpiUserSpecial);
|
|
|
|
await this.kpiRoleRepository.remove(kpiRole);
|
|
await this.kpiPlanRepository.remove(kpiPlan);
|
|
await this.kpiUserEvaluationRepository.remove(kpiUserEvaluation);
|
|
await this.kpiPeriodRepository.remove(kpiPeriod);
|
|
return new HttpSuccess();
|
|
}
|
|
}
|