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

158 lines
4.8 KiB
TypeScript
Raw Normal View History

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