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

159 lines
4.5 KiB
TypeScript
Raw Normal View History

2024-02-02 18:01:26 +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 { CreateRank, Rank } from "../entities/Rank";
import { Not } from "typeorm";
import { setLogDataDiff } from "../interfaces/utils";
import { RequestWithUser } from "../middlewares/user";
@Route("api/v1/org/metadata/rank")
@Tags("Rank")
@Security("bearerAuth")
@Response(
HttpStatusCode.INTERNAL_SERVER_ERROR,
"เกิดข้อผิดพลาด ไม่สามารถแสดงรายการได้ กรุณาลองใหม่ในภายหลัง",
)
export class RankController extends Controller {
private rankRepository = AppDataSource.getRepository(Rank);
/**
* API
*
* @summary ORG_059 - (ADMIN) #65
*
*/
@Post()
async createRank(
@Body()
requestBody: CreateRank,
@Request() request: RequestWithUser,
) {
const checkName = await this.rankRepository.findOne({
where: { name: requestBody.name },
});
if (checkName) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ชื่อนี้มีอยู่ในระบบแล้ว");
2024-02-02 18:01:26 +07:00
}
const before = null;
2024-02-28 14:47:28 +07:00
const rank = Object.assign(new Rank(), requestBody);
rank.createdUserId = request.user.sub;
rank.createdFullName = request.user.name;
rank.lastUpdateUserId = request.user.sub;
rank.lastUpdateFullName = request.user.name;
rank.createdAt = new Date();
rank.lastUpdatedAt = new Date();
await this.rankRepository.save(rank, { data: request });
2024-10-07 14:53:27 +07:00
setLogDataDiff(request, { before, after: rank });
2024-02-28 14:47:28 +07:00
return new HttpSuccess();
}
/**
* API
*
* @summary ORG_059 - (ADMIN) #65
*
* @param {string} id Id
*/
@Put("{id}")
async updateRank(
@Path() id: string,
@Body()
requestBody: CreateRank,
@Request() request: RequestWithUser,
) {
const rank = await this.rankRepository.findOne({ where: { id: id } });
if (!rank) {
2024-03-07 09:33:15 +07:00
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลตำแหน่งนี้");
}
const checkName = await this.rankRepository.findOne({
where: { id: Not(id), name: requestBody.name },
});
if (checkName) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ชื่อนี้มีอยู่ในระบบแล้ว");
}
const before = structuredClone(rank);
2024-02-28 14:47:28 +07:00
rank.lastUpdateUserId = request.user.sub;
rank.lastUpdateFullName = request.user.name;
rank.lastUpdatedAt = new Date();
2024-02-28 14:47:28 +07:00
this.rankRepository.merge(rank, requestBody);
await this.rankRepository.save(rank, { data: request });
setLogDataDiff(request, { before, after: rank });
2024-02-28 14:47:28 +07:00
return new HttpSuccess();
}
/**
* API
*
* @summary ORG_059 - (ADMIN) #65
*
* @param {string} id Id
*/
@Delete("{id}")
async deleteRank(@Path() id: string, @Request() req: RequestWithUser) {
const delRank = await this.rankRepository.findOne({
where: { id },
});
if (!delRank) {
2024-03-07 09:33:15 +07:00
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลตำแหน่งนี้");
2024-02-02 18:01:26 +07:00
}
await this.rankRepository.remove(delRank, { data: req });
2024-02-28 14:47:28 +07:00
return new HttpSuccess();
}
/**
* API
*
* @summary ORG_059 - (ADMIN) #65
*
* @param {string} id Id
*/
@Get("{id}")
async detailRank(@Path() id: string) {
const rank = await this.rankRepository.findOne({
where: { id },
select: ["id", "name"],
});
if (!rank) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล");
}
2024-02-28 14:47:28 +07:00
return new HttpSuccess(rank);
}
/**
* API
*
* @summary ORG_059 - (ADMIN) #65
*
*/
@Get()
async listRank() {
const rank = await this.rankRepository.find({
select: ["id", "name", "createdAt", "lastUpdatedAt", "createdFullName", "lastUpdateFullName"],
2024-05-15 15:29:12 +07:00
order: { name: "ASC" },
});
2024-02-28 15:07:35 +07:00
// if (!rank) {
// return new HttpSuccess([]);
// }
2024-02-28 14:47:28 +07:00
return new HttpSuccess(rank);
2024-02-02 18:01:26 +07:00
}
}