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

256 lines
8.3 KiB
TypeScript
Raw Normal View History

import {
Controller,
Get,
Post,
Put,
Delete,
Patch,
Route,
Security,
Tags,
Body,
Path,
Request,
Example,
SuccessResponse,
Response,
Query,
} from "tsoa";
import { AppDataSource } from "../database/data-source";
import HttpSuccess from "../interfaces/http-success";
import HttpStatusCode from "../interfaces/http-status";
import { PosType } from "../entities/PosType";
import { PosLevel, CreatePosLevel, UpdatePosLevel } from "../entities/PosLevel";
import HttpError from "../interfaces/http-error";
import { Not } from "typeorm";
@Route("api/v1/org/pos/level")
@Tags("PosLevel")
@Security("bearerAuth")
@Response(
HttpStatusCode.INTERNAL_SERVER_ERROR,
"เกิดข้อผิดพลาด ไม่สามารถแสดงรายการได้ กรุณาลองใหม่ในภายหลัง",
)
@SuccessResponse(HttpStatusCode.OK, "สำเร็จ")
export class PosLevelController extends Controller {
private posTypeRepository = AppDataSource.getRepository(PosType);
private posLevelRepository = AppDataSource.getRepository(PosLevel);
/**
* API
*
* @summary ORG_049 - (ADMIN) #52
*
*/
@Post()
2024-02-28 11:31:01 +07:00
@Example({
posLevelName: "นักบริหาร",
posLevelRank: 1,
posLevelAuthority: "HEAD",
posTypeId: "08db9e81-fc46-4e95-8b33-be4ca0016abf",
})
async createLevel(
@Body()
requestBody: CreatePosLevel,
@Request() request: { user: Record<string, any> },
) {
const posLevel = Object.assign(new PosLevel(), requestBody);
if (!posLevel) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล");
}
2024-02-28 11:31:01 +07:00
const chkPosTypeId = await this.posTypeRepository.findOne({
where: {
id: requestBody.posTypeId,
},
});
if (!chkPosTypeId) {
2024-03-18 11:35:36 +07:00
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลประเภทตำแหน่งนี้");
}
2024-02-28 11:31:01 +07:00
const chkPosLevelName = await this.posLevelRepository.findOne({
where: {
posTypeId: requestBody.posTypeId, //ระดับประเภทเดียวกัน ชื่อระดับตำแหน่ง ห้ามซ้ำ
posLevelName: requestBody.posLevelName,
},
});
if (chkPosLevelName) {
2024-03-18 11:35:36 +07:00
throw new HttpError(HttpStatusCode.NOT_FOUND, "ชื่อระดับตำแหน่งนี้มีอยู่ในระบบแล้ว");
}
const validPosLevelAuthority = ["HEAD", "DEPUTY", "GOVERNOR"];
if (
requestBody.posLevelAuthority == null ||
!validPosLevelAuthority.includes(requestBody.posLevelAuthority.toUpperCase())
) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. posLevelAuthority");
}
2024-02-28 11:31:01 +07:00
posLevel.createdUserId = request.user.sub;
posLevel.createdFullName = request.user.name;
posLevel.lastUpdateUserId = request.user.sub;
posLevel.lastUpdateFullName = request.user.name;
await this.posLevelRepository.save(posLevel);
return new HttpSuccess(posLevel);
}
/**
* API
*
* @summary ORG_050 - (ADMIN) #53
*
* @param {string} id Id
*/
@Put("{id}")
2024-02-28 11:31:01 +07:00
@Example({
positionName: "นักบริหาร",
posTypeRank: 1,
})
async editLevel(
@Path() id: string,
@Body() requestBody: UpdatePosLevel,
@Request() request: { user: Record<string, any> },
) {
const posLevel = await this.posLevelRepository.findOne({ where: { id } });
if (!posLevel) {
2024-02-28 14:00:38 +07:00
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลระดับตำแหน่ง");
}
2024-02-28 11:31:01 +07:00
const chkPosTypeId = await this.posTypeRepository.findOne({
where: {
id: requestBody.posTypeId,
},
});
if (!chkPosTypeId) {
2024-03-18 11:35:36 +07:00
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลประเภทตำแหน่งนี้");
}
2024-02-28 11:31:01 +07:00
const chkPosLevelName = await this.posLevelRepository.findOne({
where: {
id: Not(id),
posTypeId: requestBody.posTypeId,
posLevelName: requestBody.posLevelName,
},
});
if (chkPosLevelName) {
2024-03-18 11:35:36 +07:00
throw new HttpError(HttpStatusCode.NOT_FOUND, "ชื่อระดับตำแหน่งนี้มีอยู่ในระบบแล้ว");
}
const validPosLevelAuthority = ["HEAD", "DEPUTY", "GOVERNOR"];
if (
requestBody.posLevelAuthority == null ||
!validPosLevelAuthority.includes(requestBody.posLevelAuthority.toUpperCase())
) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. posLevelAuthority");
}
2024-02-28 11:31:01 +07:00
posLevel.lastUpdateUserId = request.user.sub;
posLevel.lastUpdateFullName = request.user.name;
this.posLevelRepository.merge(posLevel, requestBody);
await this.posLevelRepository.save(posLevel);
return new HttpSuccess(posLevel.id);
}
/**
* API
*
* @summary ORG_051 - (ADMIN) #54
*
* @param {string} id Id
*/
@Delete("{id}")
async deleteLevel(@Path() id: string) {
const delPosLevel = await this.posLevelRepository.findOne({ where: { id } });
if (!delPosLevel) {
2024-02-28 14:00:38 +07:00
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลระดับตำแหน่งนี้");
}
2024-02-28 11:31:01 +07:00
await this.posLevelRepository.remove(delPosLevel);
return new HttpSuccess();
}
/**
* API
*
* @summary ORG_052 - (ADMIN) #55
*
* @param {string} id Id
*/
@Get("{id}")
2024-02-28 11:31:01 +07:00
@Example({
id: "00000000-0000-0000-0000-000000000000",
posLevelName: "นักบริหาร",
posLevelRank: 1,
posLevelAuthority: "HEAD",
posTypes: {
id: "00000000-0000-0000-0000-000000000000",
2024-02-28 11:31:01 +07:00
posTypeName: "นักบริหาร",
posTypeRank: 1,
},
2024-02-28 11:31:01 +07:00
})
async GetLevelDetail(@Path() id: string) {
2024-02-28 11:31:01 +07:00
const getPosType = await this.posLevelRepository.findOne({
select: ["id", "posLevelName", "posLevelRank", "posLevelAuthority"],
relations: ["posType"],
where: { id: id },
});
if (!getPosType) {
2024-02-28 14:00:38 +07:00
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลประเภทตำแหน่งนี้");
2024-02-28 11:31:01 +07:00
}
2024-02-28 11:31:01 +07:00
const mapPosLevel = {
id: getPosType.id,
posLevelName: getPosType.posLevelName,
posLevelRank: getPosType.posLevelRank,
posLevelAuthority: getPosType.posLevelAuthority,
posTypes: {
id: getPosType.posType.id,
posTypeName: getPosType.posType.posTypeName,
posTypeRank: getPosType.posType.posTypeRank,
},
};
2024-02-28 11:31:01 +07:00
return new HttpSuccess(mapPosLevel);
}
/**
* API
*
* @summary ORG_028 - (ADMIN) #30
*
*/
@Get()
@Example([
{
id: "00000000-0000-0000-0000-000000000000",
posLevelName: "นักบริหาร",
posLevelRank: 1,
posLevelAuthority: "HEAD",
posTypes: {
id: "00000000-0000-0000-0000-000000000000",
posTypeName: "นักบริหาร",
posTypeRank: 1,
},
},
])
async GetPosLevel() {
2024-02-28 11:31:01 +07:00
const posLevel = await this.posLevelRepository.find({
select: ["id", "posLevelName", "posLevelRank", "posLevelAuthority", "posTypeId"],
relations: ["posType"],
2024-03-18 11:35:36 +07:00
order: { posLevelRank: "ASC" },
2024-02-28 11:31:01 +07:00
});
2024-02-28 15:07:35 +07:00
// if (!posLevel) {
// return new HttpSuccess([]);
// }
2024-02-28 11:31:01 +07:00
const mapPosLevel = posLevel.map((item) => ({
id: item.id,
posLevelName: item.posLevelName,
posLevelRank: item.posLevelRank,
posLevelAuthority: item.posLevelAuthority,
posTypes: {
id: item.posType.id,
posTypeName: item.posType.posTypeName,
posTypeRank: item.posType.posTypeRank,
},
}));
return new HttpSuccess(mapPosLevel);
}
}