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

161 lines
5.5 KiB
TypeScript
Raw Normal View History

2024-02-02 10:47:19 +07:00
import {
Controller,
Post,
Put,
Delete,
Route,
Security,
Tags,
Body,
Path,
Request,
SuccessResponse,
Response,
2024-02-02 13:16:40 +07:00
Get,
2024-02-28 11:31:01 +07:00
Example,
2024-02-02 10:47:19 +07:00
} 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";
2024-02-02 13:16:40 +07:00
import { CreatePosExecutive, PosExecutive } from "../entities/PosExecutive";
2024-02-02 10:47:19 +07:00
import { Position } from "../entities/Position";
2024-02-02 13:31:59 +07:00
@Route("api/v1/org/pos/executive")
2024-02-02 12:52:49 +07:00
@Tags("PosExecutive")
2024-02-02 10:47:19 +07:00
@Security("bearerAuth")
@Response(
HttpStatusCode.INTERNAL_SERVER_ERROR,
"เกิดข้อผิดพลาด ไม่สามารถแสดงรายการได้ กรุณาลองใหม่ในภายหลัง",
)
@SuccessResponse(HttpStatusCode.OK, "สำเร็จ")
export class PosExecutiveController extends Controller {
private posExecutiveRepository = AppDataSource.getRepository(PosExecutive);
private positionRepository = AppDataSource.getRepository(Position);
/**
* API
*
* @summary ORG_041 - (ADMIN) #44
*
*/
2024-02-02 13:31:59 +07:00
@Post()
2024-02-02 10:47:19 +07:00
async createPosExecutive(
@Body()
2024-02-02 13:16:40 +07:00
requestBody: CreatePosExecutive,
2024-02-02 10:47:19 +07:00
@Request() request: { user: Record<string, any> },
) {
const checkName = await this.posExecutiveRepository.findOne({
where: { posExecutiveName: requestBody.posExecutiveName },
});
if (checkName) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ชื่อนี้มีอยู่ในระบบแล้ว");
2024-02-02 10:47:19 +07:00
}
2024-02-28 11:31:01 +07:00
const posExecutive = Object.assign(new PosExecutive(), requestBody);
posExecutive.createdUserId = request.user.sub;
posExecutive.createdFullName = request.user.name;
posExecutive.lastUpdateUserId = request.user.sub;
posExecutive.lastUpdateFullName = request.user.name;
await this.posExecutiveRepository.save(posExecutive);
return new HttpSuccess();
2024-02-02 10:47:19 +07:00
}
/**
* API
*
* @summary ORG_042 - (ADMIN) #45
*
* @param {string} id Id
*/
2024-02-02 13:31:59 +07:00
@Put("{id}")
2024-02-02 10:47:19 +07:00
async updatePosExecutive(
@Path() id: string,
@Body()
2024-02-02 13:16:40 +07:00
requestBody: CreatePosExecutive,
2024-02-02 10:47:19 +07:00
@Request() request: { user: Record<string, any> },
) {
2024-02-28 11:31:01 +07:00
const posExecutive = await this.posExecutiveRepository.findOne({ where: { id: id } });
2024-02-02 10:47:19 +07:00
if (!posExecutive) {
2024-02-28 14:00:38 +07:00
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลตำแหน่งทางการบริหารนี้");
2024-02-02 10:47:19 +07:00
}
2024-02-28 11:31:01 +07:00
const checkName = await this.posExecutiveRepository.findOne({
where: { posExecutiveName: requestBody.posExecutiveName },
});
if (checkName) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ชื่อนี้มีอยู่ในระบบแล้ว");
}
2024-02-28 11:31:01 +07:00
posExecutive.lastUpdateUserId = request.user.sub;
posExecutive.lastUpdateFullName = request.user.name;
this.posExecutiveRepository.merge(posExecutive, requestBody);
await this.posExecutiveRepository.save(posExecutive);
return new HttpSuccess();
2024-02-02 10:47:19 +07:00
}
/**
* API
*
* @summary ORG_043 - (ADMIN) #46
*
2024-02-02 13:16:40 +07:00
* @param {string} id Id
2024-02-02 10:47:19 +07:00
*/
2024-02-02 13:31:59 +07:00
@Delete("{id}")
2024-02-02 10:47:19 +07:00
async deletePosExecutive(@Path() id: string) {
const delPosExecutive = await this.posExecutiveRepository.findOne({
where: { id },
});
if (!delPosExecutive) {
2024-02-28 14:00:38 +07:00
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลตำแหน่งทางการบริหารนี้");
2024-02-02 10:47:19 +07:00
}
2024-02-28 11:31:01 +07:00
await this.positionRepository.delete({ posExecutiveId: id });
await this.posExecutiveRepository.delete({ id });
return new HttpSuccess();
2024-02-02 10:47:19 +07:00
}
2024-02-02 13:16:40 +07:00
2024-02-28 11:31:01 +07:00
/**
2024-02-02 13:16:40 +07:00
* API
*
* @summary ORG_044 - (ADMIN) #47
2024-02-28 11:31:01 +07:00
*
2024-02-02 13:16:40 +07:00
* @param {string} id Id
*/
2024-02-28 11:31:01 +07:00
@Get("{id}")
async detailPosExecutive(@Path() id: string) {
2024-02-02 13:16:40 +07:00
const posExecutive = await this.posExecutiveRepository.findOne({
where: { id },
2024-02-28 11:31:01 +07:00
select: ["id", "posExecutiveName", "posExecutivePriority"],
});
if (!posExecutive) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล");
}
return new HttpSuccess(posExecutive);
}
2024-02-02 13:31:59 +07:00
2024-02-28 11:31:01 +07:00
/**
2024-02-02 13:31:59 +07:00
* API
*
* @summary ORG_026 - (ADMIN) #28
*
*/
@Get()
@Example([
{
id: "00000000-0000-0000-0000-000000000000",
posExecutiveName: "นักบริหาร",
posExecutivePriority: 1,
},
])
async GetPosExecutive() {
2024-02-28 11:31:01 +07:00
const posExecutive = await this.posExecutiveRepository.find({
select: ["id", "posExecutiveName", "posExecutivePriority"],
});
if (!posExecutive) {
return new HttpSuccess([]);
2024-02-02 13:31:59 +07:00
}
2024-02-28 11:31:01 +07:00
return new HttpSuccess(posExecutive);
2024-02-02 13:31:59 +07:00
}
2024-02-02 10:47:19 +07:00
}