hrms-api-org/src/controllers/EducationLevelController.ts
Suphonchai Phoonsawat 4852131651 add dpis controller
2024-10-07 14:53:27 +07:00

175 lines
5.8 KiB
TypeScript

import {
Controller,
Post,
Put,
Delete,
Route,
Security,
Tags,
Body,
Path,
Request,
SuccessResponse,
Response,
Get,
} from "tsoa";
import { AppDataSource } from "../database/data-source";
import HttpSuccess from "../interfaces/http-success";
import HttpStatusCode from "../interfaces/http-status";
import HttpError from "../interfaces/http-error";
import { CreateEducationLevel, EducationLevel } from "../entities/EducationLevel";
import { Not } from "typeorm";
import { RequestWithUser } from "../middlewares/user";
import { setLogDataDiff } from "../interfaces/utils";
@Route("api/v1/org/metadata/educationLevel")
@Tags("EducationLevel")
@Security("bearerAuth")
@Response(
HttpStatusCode.INTERNAL_SERVER_ERROR,
"เกิดข้อผิดพลาด ไม่สามารถแสดงรายการได้ กรุณาลองใหม่ในภายหลัง",
)
@SuccessResponse(HttpStatusCode.OK, "สำเร็จ")
export class EducationLevelController extends Controller {
private educationLevelRepository = AppDataSource.getRepository(EducationLevel);
/**
* API สร้างระดับการศึกษา
*
* @summary ORG_061 - สร้างระดับการศึกษา (ADMIN) #65
*
*/
@Post()
async createEducationLevel(
@Body()
requestBody: CreateEducationLevel,
@Request() request: RequestWithUser,
) {
const checkName = await this.educationLevelRepository.findOne({
where: { name: requestBody.name },
});
if (checkName) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ชื่อนี้มีอยู่ในระบบแล้ว");
}
const before = null;
const educationLevel = Object.assign(new EducationLevel(), requestBody);
educationLevel.createdUserId = request.user.sub;
educationLevel.createdFullName = request.user.name;
educationLevel.createdAt = new Date();
educationLevel.lastUpdateUserId = request.user.sub;
educationLevel.lastUpdateFullName = request.user.name;
educationLevel.lastUpdatedAt = new Date();
await this.educationLevelRepository.save(educationLevel, { data: request });
setLogDataDiff(request, { before, after: educationLevel });
return new HttpSuccess();
}
/**
* API แก้ไขระดับการศึกษา
*
* @summary ORG_061 - แก้ไขระดับการศึกษา (ADMIN) #65
*
* @param {string} id Id ระดับการศึกษา
*/
@Put("{id}")
async updateEducationLevel(
@Path() id: string,
@Body()
requestBody: CreateEducationLevel,
@Request() request: RequestWithUser,
) {
const educationLevel = await this.educationLevelRepository.findOne({ where: { id: id } });
if (!educationLevel) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลระดับการศึกษานี้");
}
const checkName = await this.educationLevelRepository.findOne({
where: { id: Not(id), name: requestBody.name },
});
if (checkName) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ชื่อนี้มีอยู่ในระบบแล้ว");
}
const before = structuredClone(educationLevel);
educationLevel.lastUpdateUserId = request.user.sub;
educationLevel.lastUpdateFullName = request.user.name;
educationLevel.lastUpdatedAt = new Date();
this.educationLevelRepository.merge(educationLevel, requestBody);
await this.educationLevelRepository.save(educationLevel, { data: request });
setLogDataDiff(request, { before, after: educationLevel });
return new HttpSuccess();
}
/**
* API ลบระดับการศึกษา
*
* @summary ORG_061 - ลบระดับการศึกษา (ADMIN) #65
*
* @param {string} id Id ระดับการศึกษา
*/
@Delete("{id}")
async deleteEducationLevel(@Path() id: string, @Request() request: RequestWithUser) {
const delEducationLevel = await this.educationLevelRepository.findOne({
where: { id },
});
if (!delEducationLevel) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลระดับการศึกษานี้");
}
await this.educationLevelRepository.remove(delEducationLevel, { data: request });
return new HttpSuccess();
}
/**
* API รายละเอียดรายการระดับการศึกษา
*
* @summary ORG_061 - รายละเอียดรายการระดับการศึกษา (ADMIN) #65
*
* @param {string} id Id ระดับการศึกษา
*/
@Get("{id}")
async detailEducationLevel(@Path() id: string) {
const educationLevel = await this.educationLevelRepository.findOne({
where: { id },
select: [
"id",
"name",
"rank",
"createdAt",
"lastUpdatedAt",
"createdFullName",
"lastUpdateFullName",
],
});
if (!educationLevel) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลระดับการศึกษานี้");
}
return new HttpSuccess(educationLevel);
}
/**
* API รายการระดับการศึกษา
*
* @summary ORG_061 - รายการระดับการศึกษา (ADMIN) #65
*
*/
@Get()
async listEducationLevel() {
const educationLevel = await this.educationLevelRepository.find({
select: [
"id",
"name",
"rank",
"createdAt",
"lastUpdatedAt",
"createdFullName",
"lastUpdateFullName",
],
order: { rank: "ASC" },
});
// if (!educationLevel) {
// return new HttpSuccess([]);
// }
return new HttpSuccess(educationLevel);
}
}