272 lines
8.9 KiB
TypeScript
272 lines
8.9 KiB
TypeScript
|
|
import { AppDataSource } from "../database/data-source";
|
||
|
|
import { CreateDirector, Director } from "../entities/Director";
|
||
|
|
import {
|
||
|
|
Body,
|
||
|
|
Delete,
|
||
|
|
Get,
|
||
|
|
Path,
|
||
|
|
Post,
|
||
|
|
Put,
|
||
|
|
Response,
|
||
|
|
Route,
|
||
|
|
SuccessResponse,
|
||
|
|
Tags,
|
||
|
|
Query,
|
||
|
|
Request,
|
||
|
|
Security,
|
||
|
|
} from "tsoa";
|
||
|
|
import HttpStatusCode from "../interfaces/http-status";
|
||
|
|
import HttpSuccess from "../interfaces/http-success";
|
||
|
|
import HttpError from "../interfaces/http-error";
|
||
|
|
import { Not, Brackets } from "typeorm";
|
||
|
|
import permission from "../interfaces/permission";
|
||
|
|
import { RequestWithUser } from "../middlewares/user";
|
||
|
|
import { setLogDataDiff } from "../interfaces/utils";
|
||
|
|
@Route("api/v1/evaluation/director")
|
||
|
|
@Tags("director")
|
||
|
|
@Security("bearerAuth")
|
||
|
|
@Response(
|
||
|
|
HttpStatusCode.INTERNAL_SERVER_ERROR,
|
||
|
|
"เกิดข้อผิดพลาด ไม่สามารถแสดงรายการได้ กรุณาลองใหม่ในภายหลัง",
|
||
|
|
)
|
||
|
|
@SuccessResponse(HttpStatusCode.OK, "สำเร็จ")
|
||
|
|
export class DirectorController {
|
||
|
|
private directorRepository = AppDataSource.getRepository(Director);
|
||
|
|
|
||
|
|
/**
|
||
|
|
* API สำหรับแสดงรายการกรรมการ
|
||
|
|
*
|
||
|
|
* @summary EV4_001 - รายการกรรมการ (ADMIN)
|
||
|
|
*
|
||
|
|
*/
|
||
|
|
@Get()
|
||
|
|
async all(
|
||
|
|
@Request() request: RequestWithUser,
|
||
|
|
@Query("page") page: number = 1,
|
||
|
|
@Query("pageSize") pageSize: number = 10,
|
||
|
|
@Query("keyword") keyword?: string,
|
||
|
|
) {
|
||
|
|
await new permission().PermissionList(request, "SYS_EVA_INFO");
|
||
|
|
// const directors = await this.directorRepository.find({
|
||
|
|
// skip: (page - 1) * pageSize,
|
||
|
|
// take: pageSize,
|
||
|
|
// });
|
||
|
|
// if (keyword != undefined && keyword !== "") {
|
||
|
|
// return directors.filter(
|
||
|
|
// (x) =>
|
||
|
|
// x.prefix?.includes(keyword) ||
|
||
|
|
// x.firstName?.includes(keyword) ||
|
||
|
|
// x.lastName?.includes(keyword) ||
|
||
|
|
// x.position?.includes(keyword) ||
|
||
|
|
// x.email?.includes(keyword) ||
|
||
|
|
// x.phone?.includes(keyword),
|
||
|
|
// );
|
||
|
|
// }
|
||
|
|
const directors = await AppDataSource.getRepository(Director)
|
||
|
|
.createQueryBuilder("director")
|
||
|
|
.andWhere(
|
||
|
|
new Brackets((qb) => {
|
||
|
|
qb.where(
|
||
|
|
keyword != null && keyword != ""
|
||
|
|
? "CONCAT(director.prefix, director.firstName, ' ', director.lastName) LIKE :keyword"
|
||
|
|
: "1=1",
|
||
|
|
{
|
||
|
|
keyword: `%${keyword}%`,
|
||
|
|
},
|
||
|
|
)
|
||
|
|
.orWhere(
|
||
|
|
keyword != null && keyword != ""
|
||
|
|
? "director.position LIKE :keyword"
|
||
|
|
: "1=1",
|
||
|
|
{
|
||
|
|
keyword: `%${keyword}%`,
|
||
|
|
},
|
||
|
|
)
|
||
|
|
.orWhere(
|
||
|
|
keyword != null && keyword != ""
|
||
|
|
? "director.email LIKE :keyword"
|
||
|
|
: "1=1",
|
||
|
|
{
|
||
|
|
keyword: `%${keyword}%`,
|
||
|
|
},
|
||
|
|
)
|
||
|
|
.orWhere(
|
||
|
|
keyword != null && keyword != ""
|
||
|
|
? "director.phone LIKE :keyword"
|
||
|
|
: "1=1",
|
||
|
|
{
|
||
|
|
keyword: `%${keyword}%`,
|
||
|
|
},
|
||
|
|
);
|
||
|
|
}),
|
||
|
|
)
|
||
|
|
.orderBy("director.createdAt", "DESC")
|
||
|
|
.skip((page - 1) * pageSize)
|
||
|
|
.take(pageSize)
|
||
|
|
.getMany();
|
||
|
|
|
||
|
|
return new HttpSuccess(directors);
|
||
|
|
}
|
||
|
|
|
||
|
|
@Get("admin")
|
||
|
|
async allAdmin(
|
||
|
|
@Request() request: RequestWithUser,
|
||
|
|
@Query("page") page: number = 1,
|
||
|
|
@Query("pageSize") pageSize: number = 10,
|
||
|
|
@Query("keyword") keyword?: string,
|
||
|
|
) {
|
||
|
|
// const directors = await this.directorRepository.find({
|
||
|
|
// skip: (page - 1) * pageSize,
|
||
|
|
// take: pageSize,
|
||
|
|
// });
|
||
|
|
// if (keyword != undefined && keyword !== "") {
|
||
|
|
// return directors.filter(
|
||
|
|
// (x) =>
|
||
|
|
// x.prefix?.includes(keyword) ||
|
||
|
|
// x.firstName?.includes(keyword) ||
|
||
|
|
// x.lastName?.includes(keyword) ||
|
||
|
|
// x.position?.includes(keyword) ||
|
||
|
|
// x.email?.includes(keyword) ||
|
||
|
|
// x.phone?.includes(keyword),
|
||
|
|
// );
|
||
|
|
// }
|
||
|
|
const directors = await AppDataSource.getRepository(Director)
|
||
|
|
.createQueryBuilder("director")
|
||
|
|
.andWhere(
|
||
|
|
new Brackets((qb) => {
|
||
|
|
qb.where(
|
||
|
|
keyword != null && keyword != ""
|
||
|
|
? "CONCAT(director.prefix, director.firstName, ' ', director.lastName) LIKE :keyword"
|
||
|
|
: "1=1",
|
||
|
|
{
|
||
|
|
keyword: `%${keyword}%`,
|
||
|
|
},
|
||
|
|
)
|
||
|
|
.orWhere(
|
||
|
|
keyword != null && keyword != ""
|
||
|
|
? "director.position LIKE :keyword"
|
||
|
|
: "1=1",
|
||
|
|
{
|
||
|
|
keyword: `%${keyword}%`,
|
||
|
|
},
|
||
|
|
)
|
||
|
|
.orWhere(
|
||
|
|
keyword != null && keyword != ""
|
||
|
|
? "director.email LIKE :keyword"
|
||
|
|
: "1=1",
|
||
|
|
{
|
||
|
|
keyword: `%${keyword}%`,
|
||
|
|
},
|
||
|
|
)
|
||
|
|
.orWhere(
|
||
|
|
keyword != null && keyword != ""
|
||
|
|
? "director.phone LIKE :keyword"
|
||
|
|
: "1=1",
|
||
|
|
{
|
||
|
|
keyword: `%${keyword}%`,
|
||
|
|
},
|
||
|
|
);
|
||
|
|
}),
|
||
|
|
)
|
||
|
|
.orderBy("director.createdAt", "DESC")
|
||
|
|
.skip((page - 1) * pageSize)
|
||
|
|
.take(pageSize)
|
||
|
|
.getMany();
|
||
|
|
return new HttpSuccess(directors);
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* API สำหรับแสดงรายละเอียดกรรมการ
|
||
|
|
*
|
||
|
|
* @summary EV4_002 - รายละเอียดกรรมการ (ADMIN)
|
||
|
|
*
|
||
|
|
*/
|
||
|
|
@Get("{id}")
|
||
|
|
async one(@Path() id: string, @Request() request: RequestWithUser) {
|
||
|
|
let _workflow = await new permission().Workflow(request, id, "SYS_EVA_INFO");
|
||
|
|
if (_workflow == false) await new permission().PermissionGet(request, "SYS_EVA_INFO");
|
||
|
|
const director = await this.directorRepository.findOne({ where: { id } });
|
||
|
|
if (!director) {
|
||
|
|
throw new HttpError(HttpStatusCode.NOT_FOUND, "not found.");
|
||
|
|
}
|
||
|
|
return new HttpSuccess(director);
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* API สำหรับเพิ่มรายละเอียดกรรมการ
|
||
|
|
*
|
||
|
|
* @summary EV4_003 - เพิ่มรายละเอียดกรรมการ (ADMIN)
|
||
|
|
*
|
||
|
|
*/
|
||
|
|
@Post()
|
||
|
|
async save(@Body() requestBody: CreateDirector, @Request() request: RequestWithUser) {
|
||
|
|
await new permission().PermissionCreate(request, "SYS_EVA_INFO");
|
||
|
|
let directorDup = await this.directorRepository.findOne({
|
||
|
|
where: { firstName: requestBody.firstName, lastName: requestBody.lastName },
|
||
|
|
});
|
||
|
|
if (directorDup != null) {
|
||
|
|
throw new HttpError(HttpStatusCode.NOT_FOUND, "ชื่อกรรมการนี้มีอยู่ในระบบแล้ว");
|
||
|
|
}
|
||
|
|
const director = Object.assign(new Director(), requestBody);
|
||
|
|
director.createdUserId = request.user.sub;
|
||
|
|
director.createdFullName = request.user.name;
|
||
|
|
director.createdAt = new Date();
|
||
|
|
director.lastUpdateUserId = request.user.sub;
|
||
|
|
director.lastUpdateFullName = request.user.name;
|
||
|
|
director.lastUpdatedAt = new Date();
|
||
|
|
|
||
|
|
const before = null;
|
||
|
|
|
||
|
|
await this.directorRepository.save(director, { data: request });
|
||
|
|
setLogDataDiff(request, { before, after: director });
|
||
|
|
|
||
|
|
return new HttpSuccess();
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* API สำหรับแก้ไขรายละเอียดกรรมการ
|
||
|
|
*
|
||
|
|
* @summary EV4_004 - แก้ไขรายละเอียดกรรมการ (ADMIN)
|
||
|
|
*
|
||
|
|
*/
|
||
|
|
@Put("{id}")
|
||
|
|
async update(@Path() id: string, @Body() u: CreateDirector, @Request() request: RequestWithUser) {
|
||
|
|
await new permission().PermissionUpdate(request, "SYS_EVA_INFO");
|
||
|
|
let directorDup = await this.directorRepository.findOne({
|
||
|
|
where: { firstName: u.firstName, lastName: u.lastName, id: Not(id) },
|
||
|
|
});
|
||
|
|
if (directorDup != null) {
|
||
|
|
throw new HttpError(HttpStatusCode.NOT_FOUND, "ชื่อกรรมการนี้มีอยู่ในระบบแล้ว");
|
||
|
|
}
|
||
|
|
let director = await this.directorRepository.findOneBy({ id });
|
||
|
|
if (!director) {
|
||
|
|
throw new HttpError(HttpStatusCode.NOT_FOUND, "not found.");
|
||
|
|
}
|
||
|
|
const before = structuredClone(directorDup);
|
||
|
|
director.lastUpdateUserId = request.user.sub;
|
||
|
|
director.lastUpdateFullName = request.user.name;
|
||
|
|
director.lastUpdatedAt = new Date();
|
||
|
|
this.directorRepository.merge(director, u);
|
||
|
|
await this.directorRepository.save(director, { data: request });
|
||
|
|
setLogDataDiff(request, { before, after: director });
|
||
|
|
return new HttpSuccess();
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* API สำหรับลบรายละเอียดกรรมการ
|
||
|
|
*
|
||
|
|
* @summary EV4_005 - ลบรายละเอียดกรรมการ (ADMIN)
|
||
|
|
*
|
||
|
|
*/
|
||
|
|
@Delete("{id}")
|
||
|
|
async remove(id: string, @Request() request: RequestWithUser) {
|
||
|
|
await new permission().PermissionDelete(request, "SYS_EVA_INFO");
|
||
|
|
let director = await this.directorRepository.findOneBy({ id });
|
||
|
|
if (!director) {
|
||
|
|
throw new HttpError(HttpStatusCode.NOT_FOUND, "not found.");
|
||
|
|
}
|
||
|
|
await this.directorRepository.remove(director, { data: request });
|
||
|
|
return new HttpSuccess();
|
||
|
|
}
|
||
|
|
}
|