hrms-api-org/src/controllers/PosLevelController.ts
2024-10-18 11:33:04 +07:00

266 lines
8.8 KiB
TypeScript

import {
Controller,
Get,
Post,
Put,
Delete,
Route,
Security,
Tags,
Body,
Path,
Request,
Example,
Response,
} 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,
"เกิดข้อผิดพลาด ไม่สามารถแสดงรายการได้ กรุณาลองใหม่ในภายหลัง",
)
export class PosLevelController extends Controller {
private posTypeRepository = AppDataSource.getRepository(PosType);
private posLevelRepository = AppDataSource.getRepository(PosLevel);
/**
* API เพิ่มระดับตำแหน่ง
*
* @summary ORG_049 - เพิ่มระดับตำแหน่ง (ADMIN) #52
*
*/
@Post()
@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, "ไม่พบข้อมูล");
}
const chkPosTypeId = await this.posTypeRepository.findOne({
where: {
id: requestBody.posTypeId,
},
});
if (!chkPosTypeId) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลประเภทตำแหน่งนี้");
}
const chkPosLevelName = await this.posLevelRepository.findOne({
where: {
posTypeId: requestBody.posTypeId, //ระดับประเภทเดียวกัน ชื่อระดับตำแหน่ง ห้ามซ้ำ
posLevelName: requestBody.posLevelName,
},
});
if (chkPosLevelName) {
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");
}
posLevel.createdUserId = request.user.sub;
posLevel.createdFullName = request.user.name;
posLevel.lastUpdateUserId = request.user.sub;
posLevel.lastUpdateFullName = request.user.name;
posLevel.createdAt = new Date();
posLevel.lastUpdatedAt = new Date();
await this.posLevelRepository.save(posLevel);
return new HttpSuccess(posLevel);
}
/**
* API แก้ไขระดับตำแหน่ง
*
* @summary ORG_050 - แก้ไขระดับตำแหน่ง (ADMIN) #53
*
* @param {string} id Id ระดับตำแหน่ง
*/
@Put("{id}")
@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) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลระดับตำแหน่ง");
}
const chkPosTypeId = await this.posTypeRepository.findOne({
where: {
id: requestBody.posTypeId,
},
});
if (!chkPosTypeId) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลประเภทตำแหน่งนี้");
}
const chkPosLevelName = await this.posLevelRepository.findOne({
where: {
id: Not(id),
posTypeId: requestBody.posTypeId,
posLevelName: requestBody.posLevelName,
},
});
if (chkPosLevelName) {
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");
}
posLevel.lastUpdateUserId = request.user.sub;
posLevel.lastUpdateFullName = request.user.name;
posLevel.lastUpdatedAt = new Date();
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) {
let result: any;
try {
result = await this.posLevelRepository.delete({ id: id });
} catch {
throw new HttpError(
HttpStatusCode.NOT_FOUND,
"ไม่สามารถลบได้เนื่องจากมีการใช้งานระดับตำแหน่งนี้อยู่",
);
}
if (result.affected == undefined || result.affected <= 0) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล");
}
// const delPosLevel = await this.posLevelRepository.findOne({ where: { id } });
// if (!delPosLevel) {
// throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลระดับตำแหน่งนี้");
// }
// await this.posLevelRepository.remove(delPosLevel);
return new HttpSuccess();
}
/**
* API รายละเอียดระดับตำแหน่ง
*
* @summary ORG_052 - รายละเอียดระดับตำแหน่ง (ADMIN) #55
*
* @param {string} id Id ระดับตำแหน่ง
*/
@Get("{id}")
@Example({
id: "00000000-0000-0000-0000-000000000000",
posLevelName: "นักบริหาร",
posLevelRank: 1,
posLevelAuthority: "HEAD",
posTypes: {
id: "00000000-0000-0000-0000-000000000000",
posTypeName: "นักบริหาร",
posTypeRank: 1,
},
})
async GetLevelDetail(@Path() id: string) {
const getPosType = await this.posLevelRepository.findOne({
select: ["id", "posLevelName", "posLevelRank", "posLevelAuthority"],
relations: ["posType"],
where: { id: id },
});
if (!getPosType) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลประเภทตำแหน่งนี้");
}
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,
},
};
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() {
const posLevel = await this.posLevelRepository.find({
select: ["id", "posLevelName", "posLevelRank", "posLevelAuthority", "posTypeId"],
relations: ["posType"],
order: { posLevelRank: "ASC" },
});
// if (!posLevel) {
// return new HttpSuccess([]);
// }
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);
}
}