hrms-api-org/src/controllers/EducationLevelController.ts

169 lines
5.3 KiB
TypeScript
Raw Normal View History

2024-02-02 16:56:47 +07:00
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";
2024-02-02 18:01:26 +07:00
import { Not } from "typeorm";
@Route("api/v1/org/metadata/educationLevel")
2024-02-02 16:56:47 +07:00
@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: { user: Record<string, any> },
) {
const checkName = await this.educationLevelRepository.findOne({
where: { name: requestBody.name },
});
if (checkName) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ชื่อนี้มีอยู่ในระบบแล้ว");
}
2024-02-28 10:36:44 +07:00
const educationLevel = Object.assign(new EducationLevel(), requestBody);
educationLevel.createdUserId = request.user.sub;
educationLevel.createdFullName = request.user.name;
educationLevel.lastUpdateUserId = request.user.sub;
educationLevel.lastUpdateFullName = request.user.name;
await this.educationLevelRepository.save(educationLevel);
return new HttpSuccess();
2024-02-02 16:56:47 +07:00
}
/**
* API
*
* @summary ORG_061 - (ADMIN) #65
*
* @param {string} id Id
*/
@Put("{id}")
async updateEducationLevel(
@Path() id: string,
@Body()
requestBody: CreateEducationLevel,
@Request() request: { user: Record<string, any> },
) {
const educationLevel = await this.educationLevelRepository.findOne({ where: { id: id } });
if (!educationLevel) {
2024-02-28 14:00:38 +07:00
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลระดับการศึกษานี้");
2024-02-02 16:56:47 +07:00
}
const checkName = await this.educationLevelRepository.findOne({
where: { id: Not(id), name: requestBody.name },
2024-02-02 16:56:47 +07:00
});
if (checkName) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ชื่อนี้มีอยู่ในระบบแล้ว");
}
2024-02-28 10:36:44 +07:00
educationLevel.lastUpdateUserId = request.user.sub;
educationLevel.lastUpdateFullName = request.user.name;
this.educationLevelRepository.merge(educationLevel, requestBody);
await this.educationLevelRepository.save(educationLevel);
return new HttpSuccess();
2024-02-02 16:56:47 +07:00
}
/**
* API
*
* @summary ORG_061 - (ADMIN) #65
*
* @param {string} id Id
*/
@Delete("{id}")
async deleteEducationLevel(@Path() id: string) {
const delEducationLevel = await this.educationLevelRepository.findOne({
where: { id },
});
if (!delEducationLevel) {
2024-02-28 14:00:38 +07:00
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลระดับการศึกษานี้");
2024-02-02 16:56:47 +07:00
}
2024-02-28 10:36:44 +07:00
await this.educationLevelRepository.delete({ id: id });
return new HttpSuccess();
2024-02-02 16:56:47 +07:00
}
/**
* 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 },
2024-02-28 10:36:44 +07:00
select: [
"id",
"name",
"rank",
"createdAt",
"lastUpdatedAt",
"createdFullName",
"lastUpdateFullName",
],
2024-02-02 16:56:47 +07:00
});
if (!educationLevel) {
2024-03-04 14:54:15 +07:00
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลระดับการศึกษานี้");
2024-02-02 16:56:47 +07:00
}
2024-02-28 10:36:44 +07:00
return new HttpSuccess(educationLevel);
2024-02-02 16:56:47 +07:00
}
/**
* API
*
* @summary ORG_061 - (ADMIN) #65
*
*/
@Get()
async listEducationLevel() {
const educationLevel = await this.educationLevelRepository.find({
2024-02-28 10:36:44 +07:00
select: [
"id",
"name",
"rank",
"createdAt",
"lastUpdatedAt",
"createdFullName",
"lastUpdateFullName",
],
2024-05-14 11:56:40 +07:00
order: { rank: "ASC" },
2024-02-02 16:56:47 +07:00
});
2024-02-28 15:07:35 +07:00
// if (!educationLevel) {
// return new HttpSuccess([]);
// }
2024-02-28 10:36:44 +07:00
return new HttpSuccess(educationLevel);
2024-02-02 16:56:47 +07:00
}
}