342 lines
11 KiB
TypeScript
342 lines
11 KiB
TypeScript
import {
|
|
Controller,
|
|
Get,
|
|
Post,
|
|
Put,
|
|
Delete,
|
|
Route,
|
|
Security,
|
|
Tags,
|
|
Body,
|
|
Path,
|
|
Request,
|
|
SuccessResponse,
|
|
Response,
|
|
} 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 { KpiSpecial, CreateKpiSpecial, UpdateKpiSpecial } from "../entities/kpiSpecial";
|
|
import { Brackets, Not } from "typeorm";
|
|
import permission from "../interfaces/permission";
|
|
import { RequestWithUser } from "../middlewares/user";
|
|
import { addLogSequence, setLogDataDiff } from "../interfaces/utils";
|
|
|
|
@Route("api/v1/kpi/special")
|
|
@Tags("kpiSpecial")
|
|
@Security("bearerAuth")
|
|
@Response(
|
|
HttpStatusCode.INTERNAL_SERVER_ERROR,
|
|
"เกิดข้อผิดพลาด ไม่สามารถแสดงรายการได้ กรุณาลองใหม่ในภายหลัง",
|
|
)
|
|
@SuccessResponse(HttpStatusCode.OK, "สำเร็จ")
|
|
export class kpiSpecialController extends Controller {
|
|
private kpiSpecialRepository = AppDataSource.getRepository(KpiSpecial);
|
|
|
|
/**
|
|
* สร้างตัวชี้วัด Special
|
|
* @param requestBody
|
|
* @param request
|
|
*/
|
|
@Post()
|
|
async createKpiSpecial(
|
|
@Body() requestBody: CreateKpiSpecial,
|
|
@Request() request: RequestWithUser,
|
|
) {
|
|
await new permission().PermissionCreate(request, "SYS_EVA_INDICATOR");
|
|
const chk_kpiSpecial = await this.kpiSpecialRepository.findOne({
|
|
where: {
|
|
including: String(requestBody.including),
|
|
includingName: String(requestBody.includingName),
|
|
},
|
|
});
|
|
if (chk_kpiSpecial) {
|
|
throw new HttpError(
|
|
HttpStatusCode.CONFLICT,
|
|
"ไม่สามารถเพิ่มข้อมูลได้เนื่องจากข้อมูลตัวชี้วัดซ้ำ",
|
|
);
|
|
}
|
|
const kpiSpecial = Object.assign(new KpiSpecial(), requestBody);
|
|
if (!kpiSpecial) {
|
|
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล");
|
|
}
|
|
const before = null;
|
|
|
|
kpiSpecial.createdUserId = request.user.sub;
|
|
kpiSpecial.createdFullName = request.user.name;
|
|
kpiSpecial.lastUpdateUserId = request.user.sub;
|
|
kpiSpecial.lastUpdateFullName = request.user.name;
|
|
await this.kpiSpecialRepository.save(kpiSpecial, { data: request });
|
|
setLogDataDiff(request, { before, after: kpiSpecial });
|
|
|
|
return new HttpSuccess(kpiSpecial.id);
|
|
}
|
|
|
|
/**
|
|
* API แก้ไขตัวชี้วัด Special
|
|
* @param id
|
|
* @param requestBody
|
|
* @param request
|
|
*/
|
|
@Put("{id}")
|
|
async updateKpiSpecial(
|
|
@Path() id: string,
|
|
@Body() requestBody: UpdateKpiSpecial,
|
|
@Request() request: RequestWithUser,
|
|
) {
|
|
await new permission().PermissionUpdate(request, "SYS_EVA_INDICATOR");
|
|
const kpiSpecial = await this.kpiSpecialRepository.findOne({
|
|
where: { id: id },
|
|
});
|
|
if (!kpiSpecial) {
|
|
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลตัวชี้วัด Specialนี้");
|
|
}
|
|
|
|
const chk_kpiSpecial = await this.kpiSpecialRepository.findOne({
|
|
where: {
|
|
id: Not(id),
|
|
including: String(requestBody.including),
|
|
includingName: String(requestBody.includingName),
|
|
},
|
|
});
|
|
if (chk_kpiSpecial) {
|
|
throw new HttpError(
|
|
HttpStatusCode.CONFLICT,
|
|
"ไม่สามารถเพิ่มข้อมูลได้เนื่องจากข้อมูลตัวชี้วัดซ้ำ",
|
|
);
|
|
}
|
|
const before = structuredClone(kpiSpecial);
|
|
|
|
kpiSpecial.lastUpdateUserId = request.user.sub;
|
|
kpiSpecial.lastUpdateFullName = request.user.name;
|
|
Object.assign(kpiSpecial, requestBody);
|
|
await this.kpiSpecialRepository.save(kpiSpecial, { data: request });
|
|
setLogDataDiff(request, { before, after: kpiSpecial });
|
|
|
|
return new HttpSuccess(id);
|
|
}
|
|
|
|
/**
|
|
* API ตัวชี้วัด Special
|
|
* @param id Guid, *Id ตัวชี้วัด Special
|
|
*/
|
|
@Get("edit/{id}")
|
|
async GetKpiSpecialByIdEdit(@Request() request: RequestWithUser, @Path() id: string) {
|
|
let _data = await new permission().PermissionGet(request, "SYS_EVA_INDICATOR");
|
|
const KpiSpecial = await this.kpiSpecialRepository.findOne({
|
|
where: { id: id },
|
|
});
|
|
if (!KpiSpecial) {
|
|
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลตัวชี้วัด Specialนี้");
|
|
}
|
|
|
|
const mapData = {
|
|
id: KpiSpecial.id,
|
|
period: KpiSpecial.period,
|
|
year: KpiSpecial.year,
|
|
including: KpiSpecial.including,
|
|
includingName: KpiSpecial.includingName,
|
|
target: KpiSpecial.target,
|
|
unit: KpiSpecial.unit,
|
|
weight: KpiSpecial.weight,
|
|
point: KpiSpecial.point,
|
|
summary: KpiSpecial.summary,
|
|
documentInfoEvidence: KpiSpecial.documentInfoEvidence,
|
|
startDate: KpiSpecial.startDate,
|
|
endDate: KpiSpecial.endDate,
|
|
achievement1: KpiSpecial.achievement1,
|
|
achievement2: KpiSpecial.achievement2,
|
|
achievement3: KpiSpecial.achievement3,
|
|
achievement4: KpiSpecial.achievement4,
|
|
achievement5: KpiSpecial.achievement5,
|
|
meaning: KpiSpecial.meaning,
|
|
formula: KpiSpecial.formula,
|
|
};
|
|
return new HttpSuccess(mapData);
|
|
}
|
|
|
|
/**
|
|
* API ตัวชี้วัด Special
|
|
* @param id Guid, *Id ตัวชี้วัด Special
|
|
*/
|
|
@Get("{id}")
|
|
async GetKpiSpecialById(@Request() request: RequestWithUser, @Path() id: string) {
|
|
const KpiSpecial = await this.kpiSpecialRepository.findOne({
|
|
where: { id: id },
|
|
});
|
|
if (!KpiSpecial) {
|
|
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลตัวชี้วัด Specialนี้");
|
|
}
|
|
|
|
const mapData = {
|
|
id: KpiSpecial.id,
|
|
period: KpiSpecial.period,
|
|
year: KpiSpecial.year,
|
|
including: KpiSpecial.including,
|
|
includingName: KpiSpecial.includingName,
|
|
target: KpiSpecial.target,
|
|
unit: KpiSpecial.unit,
|
|
weight: KpiSpecial.weight,
|
|
point: KpiSpecial.point,
|
|
summary: KpiSpecial.summary,
|
|
documentInfoEvidence: KpiSpecial.documentInfoEvidence,
|
|
startDate: KpiSpecial.startDate,
|
|
endDate: KpiSpecial.endDate,
|
|
achievement1: KpiSpecial.achievement1,
|
|
achievement2: KpiSpecial.achievement2,
|
|
achievement3: KpiSpecial.achievement3,
|
|
achievement4: KpiSpecial.achievement4,
|
|
achievement5: KpiSpecial.achievement5,
|
|
meaning: KpiSpecial.meaning,
|
|
formula: KpiSpecial.formula,
|
|
};
|
|
return new HttpSuccess(mapData);
|
|
}
|
|
|
|
/**
|
|
* API list ตัวชี้วัด Special
|
|
* @param page
|
|
* @param pageSize
|
|
* @param keyword
|
|
*/
|
|
@Post("search")
|
|
async listKpiSpecial(
|
|
@Request() request: RequestWithUser,
|
|
@Body()
|
|
requestBody: {
|
|
page: number;
|
|
pageSize: number;
|
|
year?: string | null;
|
|
period?: string | null;
|
|
keyword?: string | null;
|
|
},
|
|
) {
|
|
// let condition: any = {};
|
|
// if (requestBody.keyword !== undefined && requestBody.keyword !== "") {
|
|
// condition = {
|
|
// where: [
|
|
// {
|
|
// including: Like(`%${requestBody.keyword}%`),
|
|
// includingName: Like(`%${requestBody.keyword}%`),
|
|
// },
|
|
// ],
|
|
// };
|
|
// }
|
|
|
|
const [kpiSpecial, total] = await AppDataSource.getRepository(KpiSpecial)
|
|
.createQueryBuilder("kpiSpecial")
|
|
// .andWhere(condition)
|
|
.andWhere(requestBody.year ? "kpiSpecial.year LIKE :year" : "1=1", {
|
|
year: `%${requestBody.year}%`,
|
|
})
|
|
.andWhere(requestBody.period ? "kpiSpecial.period LIKE :period" : "1=1", {
|
|
period: `%${requestBody.period}%`,
|
|
})
|
|
.andWhere(
|
|
new Brackets((qb) => {
|
|
qb.orWhere("kpiSpecial.including LIKE :keyword", {
|
|
keyword: `%${requestBody.keyword}%`,
|
|
}).orWhere("kpiSpecial.includingName LIKE :keyword", {
|
|
keyword: `%${requestBody.keyword}%`,
|
|
});
|
|
}),
|
|
)
|
|
.select([
|
|
"kpiSpecial.id",
|
|
"kpiSpecial.year",
|
|
"kpiSpecial.period",
|
|
"kpiSpecial.including",
|
|
"kpiSpecial.includingName",
|
|
"kpiSpecial.createdAt",
|
|
])
|
|
.orderBy("kpiSpecial.createdAt", "DESC")
|
|
.skip((requestBody.page - 1) * requestBody.pageSize)
|
|
.take(requestBody.pageSize)
|
|
.getManyAndCount();
|
|
|
|
return new HttpSuccess({ data: kpiSpecial, total });
|
|
}
|
|
|
|
/**
|
|
* API list ตัวชี้วัด Special
|
|
* @param page
|
|
* @param pageSize
|
|
* @param keyword
|
|
*/
|
|
@Post("search-edit")
|
|
async listKpiSpecialEdit(
|
|
@Request() request: RequestWithUser,
|
|
@Body()
|
|
requestBody: {
|
|
page: number;
|
|
pageSize: number;
|
|
year?: string | null;
|
|
period?: string | null;
|
|
keyword?: string | null;
|
|
},
|
|
) {
|
|
let _data = await new permission().PermissionList(request, "SYS_EVA_INDICATOR");
|
|
// let condition: any = {};
|
|
// if (requestBody.keyword !== undefined && requestBody.keyword !== "") {
|
|
// condition = {
|
|
// where: [
|
|
// {
|
|
// including: Like(`%${requestBody.keyword}%`),
|
|
// includingName: Like(`%${requestBody.keyword}%`),
|
|
// },
|
|
// ],
|
|
// };
|
|
// }
|
|
|
|
const [kpiSpecial, total] = await AppDataSource.getRepository(KpiSpecial)
|
|
.createQueryBuilder("kpiSpecial")
|
|
// .andWhere(condition)
|
|
.andWhere(requestBody.year ? "kpiSpecial.year LIKE :year" : "1=1", {
|
|
year: `%${requestBody.year}%`,
|
|
})
|
|
.andWhere(requestBody.period ? "kpiSpecial.period LIKE :period" : "1=1", {
|
|
period: `%${requestBody.period}%`,
|
|
})
|
|
.andWhere(
|
|
new Brackets((qb) => {
|
|
qb.orWhere("kpiSpecial.including LIKE :keyword", {
|
|
keyword: `%${requestBody.keyword}%`,
|
|
}).orWhere("kpiSpecial.includingName LIKE :keyword", {
|
|
keyword: `%${requestBody.keyword}%`,
|
|
});
|
|
}),
|
|
)
|
|
.select([
|
|
"kpiSpecial.id",
|
|
"kpiSpecial.year",
|
|
"kpiSpecial.period",
|
|
"kpiSpecial.including",
|
|
"kpiSpecial.includingName",
|
|
"kpiSpecial.createdAt",
|
|
])
|
|
.orderBy("kpiSpecial.createdAt", "DESC")
|
|
.skip((requestBody.page - 1) * requestBody.pageSize)
|
|
.take(requestBody.pageSize)
|
|
.getManyAndCount();
|
|
|
|
return new HttpSuccess({ data: kpiSpecial, total });
|
|
}
|
|
|
|
/**
|
|
* API ลบตัวชี้วัด Special
|
|
* @param id
|
|
*/
|
|
@Delete("{id}")
|
|
async deleteKpiSpecial(@Path() id: string, @Request() request: RequestWithUser) {
|
|
await new permission().PermissionDelete(request, "SYS_EVA_INDICATOR");
|
|
const kpiSpecial = await this.kpiSpecialRepository.findOne({
|
|
where: { id: id },
|
|
});
|
|
if (!kpiSpecial) {
|
|
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลตัวชี้วัด Specialนี้");
|
|
}
|
|
await this.kpiSpecialRepository.remove(kpiSpecial, { data: request });
|
|
return new HttpSuccess();
|
|
}
|
|
}
|