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

159 lines
5 KiB
TypeScript
Raw Normal View History

2024-02-02 17:59:33 +07:00
import {
Controller,
Post,
Put,
Delete,
Route,
Security,
Tags,
Body,
Path,
Request,
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 { Prefixe, CreatePrefixe, UpdatePrefixe } from "../entities/Prefixe";
import { Not } from "typeorm";
import { setLogDataDiff } from "../interfaces/utils";
import { RequestWithUser } from "../middlewares/user";
2024-02-02 17:59:33 +07:00
@Route("api/v1/org/metadata/prefix")
2024-02-02 17:59:33 +07:00
@Tags("Prefix")
@Security("bearerAuth")
@Response(
HttpStatusCode.INTERNAL_SERVER_ERROR,
"เกิดข้อผิดพลาด ไม่สามารถแสดงรายการได้ กรุณาลองใหม่ในภายหลัง",
)
export class PrefixController extends Controller {
private prefixRepository = AppDataSource.getRepository(Prefixe);
/**
* API list
*
* @summary ORG_056 - CRUD (ADMIN) #61
*
*/
@Get()
async Get() {
const _prefix = await this.prefixRepository.find({
select: ["id", "name", "createdAt", "lastUpdatedAt", "createdFullName", "lastUpdateFullName"],
2024-11-27 10:20:40 +07:00
order: { createdAt: "DESC" },
2024-02-02 17:59:33 +07:00
});
2024-02-28 14:47:28 +07:00
return new HttpSuccess(_prefix);
2024-02-02 17:59:33 +07:00
}
/**
* API
*
* @summary ORG_056 - CRUD (ADMIN) #61
*
* @param {string} id Id
*/
@Get("{id}")
async GetById(@Path() id: string) {
const _prefix = await this.prefixRepository.findOne({
where: { id },
select: ["id", "name"],
});
if (!_prefix) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล");
}
2024-02-28 14:47:28 +07:00
return new HttpSuccess(_prefix);
2024-02-02 17:59:33 +07:00
}
/**
* API body
*
* @summary ORG_056 - CRUD (ADMIN) #61
*
*/
@Post()
async Post(
@Body()
requestBody: CreatePrefixe,
@Request() request: RequestWithUser,
2024-02-02 17:59:33 +07:00
) {
const _prefix = Object.assign(new Prefixe(), requestBody);
if (!_prefix) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล");
}
const checkName = await this.prefixRepository.findOne({
where: { name: requestBody.name },
});
if (checkName) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ชื่อนี้มีอยู่ในระบบแล้ว");
}
2024-10-07 14:53:27 +07:00
const before = null;
2024-02-28 14:47:28 +07:00
_prefix.createdUserId = request.user.sub;
_prefix.createdFullName = request.user.name;
_prefix.lastUpdateUserId = request.user.sub;
_prefix.lastUpdateFullName = request.user.name;
_prefix.createdAt = new Date();
_prefix.lastUpdatedAt = new Date();
await this.prefixRepository.save(_prefix, { data: request });
2024-10-07 14:53:27 +07:00
setLogDataDiff(request, { before, after: _prefix });
2024-02-28 14:47:28 +07:00
return new HttpSuccess();
2024-02-02 17:59:33 +07:00
}
/**
* API body
*
* @summary ORG_056 - CRUD (ADMIN) #61
*
* @param {string} id Id
*/
@Put("{id}")
async Put(
@Path() id: string,
@Body()
requestBody: UpdatePrefixe,
@Request() request: RequestWithUser,
2024-02-02 17:59:33 +07:00
) {
const _prefix = await this.prefixRepository.findOne({ where: { id: id } });
if (!_prefix) {
2024-02-28 14:00:38 +07:00
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลคำนำหน้าชื่อนี้");
2024-02-02 17:59:33 +07:00
}
const checkName = await this.prefixRepository.findOne({
where: { id: Not(id), name: requestBody.name },
});
if (checkName) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ชื่อนี้มีอยู่ในระบบแล้ว");
}
const before = structuredClone(_prefix);
2024-02-28 14:47:28 +07:00
_prefix.lastUpdateUserId = request.user.sub;
_prefix.lastUpdateFullName = request.user.name;
_prefix.lastUpdatedAt = new Date();
2024-02-28 14:47:28 +07:00
this.prefixRepository.merge(_prefix, requestBody);
await this.prefixRepository.save(_prefix, { data: request });
setLogDataDiff(request, { before, after: _prefix });
2024-02-28 14:47:28 +07:00
return new HttpSuccess();
2024-02-02 17:59:33 +07:00
}
/**
* API
2024-02-02 17:59:33 +07:00
*
* @summary ORG_056 - CRUD (ADMIN) #61
*
* @param {string} id Id
2024-02-02 17:59:33 +07:00
*/
@Delete("{id}")
async Delete(@Path() id: string, @Request() request: RequestWithUser) {
2024-02-02 17:59:33 +07:00
const _delPrefix = await this.prefixRepository.findOne({
where: { id: id },
});
if (!_delPrefix) {
2024-02-28 14:00:38 +07:00
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลคำนำหน้าชื่อนี้");
2024-02-02 17:59:33 +07:00
}
2024-10-07 14:53:27 +07:00
await this.prefixRepository.remove(_delPrefix, { data: request });
2024-02-28 14:47:28 +07:00
return new HttpSuccess();
2024-02-02 17:59:33 +07:00
}
}