387 lines
15 KiB
TypeScript
387 lines
15 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";
|
|
import { RequestWithUser } from "../middlewares/user";
|
|
import permission from "../interfaces/permission";
|
|
import { setLogDataDiff } from "../interfaces/utils";
|
|
|
|
@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: RequestWithUser) {
|
|
await new permission().PermissionCreate(request, "SYS_KPI_ROUND");
|
|
const chkkpiPeriod = await this.kpiPeriodRepository.findOne({
|
|
where: {
|
|
durationKPI: requestBody.durationKPI,
|
|
year: requestBody.year,
|
|
},
|
|
});
|
|
if (chkkpiPeriod) {
|
|
throw new HttpError(HttpStatusCode.NOT_FOUND, "รอบการประเมินผลนี้มีอยู่ในระบบแล้ว");
|
|
}
|
|
const before = null;
|
|
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;
|
|
kpiPeriod.createdAt = new Date();
|
|
kpiPeriod.lastUpdatedAt = new Date();
|
|
await this.kpiPeriodRepository.save(kpiPeriod, { data: request });
|
|
setLogDataDiff(request, { before, after: 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: RequestWithUser,
|
|
) {
|
|
await new permission().PermissionUpdate(request, "SYS_KPI_ROUND");
|
|
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, "รอบการประเมินผลนี้มีอยู่ในระบบแล้ว");
|
|
}
|
|
const before = structuredClone(kpiPeriod);
|
|
|
|
requestBody.durationKPI = requestBody.durationKPI.trim().toUpperCase();
|
|
this.kpiPeriodRepository.merge(kpiPeriod, requestBody);
|
|
kpiPeriod.lastUpdateUserId = request.user.sub;
|
|
kpiPeriod.lastUpdateFullName = request.user.name;
|
|
kpiPeriod.lastUpdatedAt = new Date();
|
|
await this.kpiPeriodRepository.save(kpiPeriod, { data: request });
|
|
setLogDataDiff(request, { before, after: kpiPeriod });
|
|
|
|
return new HttpSuccess(id);
|
|
}
|
|
|
|
/**
|
|
* API ปิดรอบการประเมินผลการปฏิบัติหน้าที่ราชการ (ADMIN)
|
|
* @param id Guid, *Id ปิดรอบการประเมินผลการปฏิบัติหน้าที่ราชการ
|
|
*/
|
|
@Get("close/{id}")
|
|
@Example({
|
|
durationKPI: "string", //รอบเดือนที่สร้าง
|
|
startDate: "datetime", //วันเริ่มต้น
|
|
endDate: "datetime", //วันสิ้นสุด
|
|
})
|
|
async CloseKpiPeriodById(@Path() id: string, @Request() request: RequestWithUser) {
|
|
await new permission().PermissionUpdate(request, "SYS_KPI_ROUND");
|
|
const kpiPeriod = await this.kpiPeriodRepository.findOne({
|
|
where: { id: id },
|
|
});
|
|
if (!kpiPeriod) {
|
|
throw new HttpError(
|
|
HttpStatusCode.NOT_FOUND,
|
|
"ไม่พบข้อมูลรอบการประเมินผลการปฏิบัติหน้าที่ราชการนี้",
|
|
);
|
|
}
|
|
const before = structuredClone(kpiPeriod);
|
|
|
|
kpiPeriod.isActive = false;
|
|
kpiPeriod.lastUpdateUserId = request.user.sub;
|
|
kpiPeriod.lastUpdateFullName = request.user.name;
|
|
kpiPeriod.lastUpdatedAt = new Date();
|
|
await this.kpiPeriodRepository.save(kpiPeriod, { data: request });
|
|
setLogDataDiff(request, { before, after: kpiPeriod });
|
|
|
|
return new HttpSuccess(kpiPeriod);
|
|
}
|
|
|
|
/**
|
|
* API เปิดรอบการประเมินผลการปฏิบัติหน้าที่ราชการ (ADMIN)
|
|
* @param id Guid, *Id เปิดรอบการประเมินผลการปฏิบัติหน้าที่ราชการ
|
|
*/
|
|
@Get("open/{id}")
|
|
@Example({
|
|
durationKPI: "string", //รอบเดือนที่สร้าง
|
|
startDate: "datetime", //วันเริ่มต้น
|
|
endDate: "datetime", //วันสิ้นสุด
|
|
})
|
|
async OpenKpiPeriodById(@Path() id: string, @Request() request: RequestWithUser) {
|
|
let _workflow = await new permission().Workflow(request, id, "SYS_KPI_ROUND");
|
|
if (_workflow == false) await new permission().PermissionGet(request, "SYS_KPI_ROUND");
|
|
const kpiPeriod = await this.kpiPeriodRepository.findOne({
|
|
where: { id: id },
|
|
});
|
|
if (!kpiPeriod) {
|
|
throw new HttpError(
|
|
HttpStatusCode.NOT_FOUND,
|
|
"ไม่พบข้อมูลรอบการประเมินผลการปฏิบัติหน้าที่ราชการนี้",
|
|
);
|
|
}
|
|
const before = structuredClone(kpiPeriod);
|
|
|
|
kpiPeriod.isActive = true;
|
|
kpiPeriod.lastUpdateUserId = request.user.sub;
|
|
kpiPeriod.lastUpdateFullName = request.user.name;
|
|
kpiPeriod.lastUpdatedAt = new Date();
|
|
await this.kpiPeriodRepository.save(kpiPeriod, { data: request });
|
|
setLogDataDiff(request, { before, after: kpiPeriod });
|
|
|
|
return new HttpSuccess(kpiPeriod);
|
|
}
|
|
|
|
/**
|
|
* API list รอบการประเมินผลการปฏิบัติหน้าที่ราชการ (USER)
|
|
* @param page
|
|
* @param pageSize
|
|
* @param keyword
|
|
*/
|
|
@Get("user")
|
|
async listKpiPeriodUser(
|
|
@Request() request: RequestWithUser,
|
|
@Query("page") page: number = 1,
|
|
@Query("pageSize") pageSize: number = 10,
|
|
@Query("year") year?: number,
|
|
@Query("keyword") keyword?: string,
|
|
) {
|
|
// await new permission().PermissionDelete(request, "SYS_KPI_ROUND");
|
|
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.startDate", "ASC")
|
|
.addOrderBy("kpiPeriod.year", "ASC")
|
|
.skip((page - 1) * pageSize)
|
|
.take(pageSize)
|
|
.getManyAndCount();
|
|
|
|
return new HttpSuccess({ data: kpiPeriod, total });
|
|
}
|
|
|
|
@Get("admin")
|
|
async listKpiPeriodAdmin(
|
|
@Request() request: RequestWithUser,
|
|
@Query("page") page: number = 1,
|
|
@Query("pageSize") pageSize: number = 10,
|
|
@Query("year") year?: number,
|
|
@Query("keyword") keyword?: string,
|
|
@Query("sortBy") sortBy?: string,
|
|
@Query("descending") descending?: boolean,
|
|
) {
|
|
await new permission().PermissionList(request, "SYS_KPI_ROUND");
|
|
let query = await AppDataSource.getRepository(KpiPeriod)
|
|
.createQueryBuilder("kpiPeriod")
|
|
.andWhere(
|
|
year !== 0 && year != null && year != undefined ? "kpiPeriod.year = :year" : "1=1",
|
|
{ year: year },
|
|
)
|
|
|
|
if (sortBy) {
|
|
query = query.orderBy(
|
|
`kpiPeriod.${sortBy}`,
|
|
descending ? "DESC" : "ASC"
|
|
);
|
|
}else{
|
|
query = query.orderBy("kpiPeriod.year", "DESC")
|
|
.addOrderBy("kpiPeriod.durationKPI", "DESC")
|
|
.addOrderBy("kpiPeriod.startDate", "DESC")
|
|
}
|
|
|
|
const [kpiPeriod, total] = await query
|
|
.skip((page - 1) * pageSize)
|
|
.take(pageSize)
|
|
.getManyAndCount();
|
|
|
|
return new HttpSuccess({ data: kpiPeriod, total });
|
|
}
|
|
|
|
/**
|
|
* 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 รอบการประเมินผลการปฏิบัติหน้าที่ราชการ (USER)
|
|
* @param page
|
|
* @param pageSize
|
|
* @param keyword
|
|
*/
|
|
@Get()
|
|
async listKpiPeriod(
|
|
@Request() request: RequestWithUser,
|
|
@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.year", "DESC")
|
|
.addOrderBy("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, @Request() request: RequestWithUser) {
|
|
await new permission().PermissionDelete(request, "SYS_KPI_ROUND");
|
|
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 },
|
|
});
|
|
if (kpiUserEvaluation && kpiUserEvaluation.length > 0) {
|
|
throw new HttpError(
|
|
HttpStatusCode.NOT_FOUND,
|
|
"ไม่สามารถลบรอบการประเมินนี้ได้ เนื่องจากพบรายการผู้รับการประเมินในรอบนี้",
|
|
);
|
|
}
|
|
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, { data: request });
|
|
await this.kpiUserPlannedRepository.remove(kpiUserPlanned, { data: request });
|
|
|
|
await this.kpiUserRoleRepository.remove(_kpiUserRole, { data: request });
|
|
await this.kpiUserPlannedRepository.remove(_kpiUserPlanned, { data: request });
|
|
await this.kpiUserCapacityRepository.remove(_kpiUserCapacity, { data: request });
|
|
await this.kpiUserSpecialRepository.remove(_kpiUserSpecial, { data: request });
|
|
|
|
await this.kpiRoleRepository.remove(kpiRole, { data: request });
|
|
await this.kpiPlanRepository.remove(kpiPlan, { data: request });
|
|
await this.kpiUserEvaluationRepository.remove(kpiUserEvaluation, { data: request });
|
|
await this.kpiPeriodRepository.remove(kpiPeriod, { data: request });
|
|
return new HttpSuccess();
|
|
}
|
|
}
|