api ข้อมูลตำแหน่งลูกจ้างประจำ

This commit is contained in:
Bright 2024-03-12 17:53:54 +07:00
parent 0c8abe053f
commit 5ddb1b769e
3 changed files with 468 additions and 0 deletions

View file

@ -0,0 +1,193 @@
import {
Controller,
Get,
Post,
Put,
Delete,
Route,
Security,
Tags,
Body,
Path,
Request,
SuccessResponse,
Response,
} 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 { Not } from "typeorm";
import { EmployeePosType, CreateEmployeePosType, UpdateEmployeePosType } from "../entities/EmployeePosType";
import { EmployeePosLevel } from "../entities/EmployeePosLevel";
@Route("api/v1/org/employee/pos/type")
@Tags("EmployeePosType")
@Security("bearerAuth")
@Response(
HttpStatusCode.INTERNAL_SERVER_ERROR,
"เกิดข้อผิดพลาด ไม่สามารถแสดงรายการได้ กรุณาลองใหม่ในภายหลัง",
)
@SuccessResponse(HttpStatusCode.OK, "สำเร็จ")
export class EmployeePosTypeController extends Controller {
private employeePosTypeRepository = AppDataSource.getRepository(EmployeePosType);
private employeePosLevelRepository = AppDataSource.getRepository(EmployeePosLevel);
/**
* API
*
* @summary ORG_ - (ADMIN) #
*
*/
@Post()
async CreateEmpType(
@Body()
requestBody: CreateEmployeePosType,
@Request() request: { user: Record<string, any> },
) {
const EmpPosType = Object.assign(new EmployeePosType(), requestBody);
if (!EmpPosType) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล");
}
const chkEmpPosTypeName = await this.employeePosTypeRepository.findOne({
where: {
posTypeName: requestBody.posTypeName,
},
});
if (chkEmpPosTypeName) {
throw new HttpError(
HttpStatusCode.NOT_FOUND,
"ชื่อประเภทกลุ่มงานลูกจ้างประจำนี้มีอยู่ในระบบแล้ว",
);
}
EmpPosType.createdUserId = request.user.sub;
EmpPosType.createdFullName = request.user.name;
EmpPosType.lastUpdateUserId = request.user.sub;
EmpPosType.lastUpdateFullName = request.user.name;
await this.employeePosTypeRepository.save(EmpPosType);
return new HttpSuccess(EmpPosType.id);
}
/**
* API
*
* @summary ORG_ - (ADMIN) #
*
* @param {string} id Id
*/
@Put("{id}")
async EditEmpType(
@Path() id: string,
@Body() requestBody: UpdateEmployeePosType,
@Request() request: { user: Record<string, any> },
) {
const EmpPosType = await this.employeePosTypeRepository.findOne({ where: { id } });
if (!EmpPosType) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลประเภทกลุ่มงานลูกจ้างประจำนี้");
}
const chkEmpPosType = await this.employeePosTypeRepository.findOne({
where: {
id: Not(id),
posTypeName: requestBody.posTypeName,
},
});
if (chkEmpPosType) {
throw new HttpError(
HttpStatusCode.NOT_FOUND,
"ชื่อประเภทกลุ่มงานลูกจ้างประจำนี้มีอยู่ในระบบแล้ว",
);
}
EmpPosType.lastUpdateUserId = request.user.sub;
EmpPosType.lastUpdateFullName = request.user.name;
this.employeePosTypeRepository.merge(EmpPosType, requestBody);
await this.employeePosTypeRepository.save(EmpPosType);
return new HttpSuccess(EmpPosType.id);
}
/**
* API
*
* @summary ORG_ - (ADMIN) #
*
* @param {string} id Id
*/
@Delete("{id}")
async deleteType(@Path() id: string) {
const delEmpPosType = await this.employeePosTypeRepository.findOne({ where: { id } });
if (!delEmpPosType) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลประเภทกลุ่มงานลูกจ้างประจำนี้");
}
const EmpPosLevel = await this.employeePosLevelRepository.find({
where: { employeePosTypeId: id },
});
if (EmpPosLevel.length > 0) {
throw new HttpError(
HttpStatusCode.NOT_FOUND,
"ไม่สามารถลบได้เนื่องจากพบข้อมูลที่ตารางระดับชั้นงาน",
);
}
await this.employeePosTypeRepository.remove(delEmpPosType);
return new HttpSuccess();
}
/**
* API
*
* @summary ORG_ - (ADMIN) #
*
* @param {string} id Id
*/
@Get("{id}")
async GetEmpTypeById(@Path() id: string) {
const getEmpPosType = await this.employeePosTypeRepository.findOne({
select: ["id", "posTypeName", "posTypeRank"],
relations: ["employeePosLevels"],
where: { id: id },
});
if (!getEmpPosType) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลประเภทกลุ่มงานลูกจ้างประจำนี้");
}
const mapGetEmpPosType = {
id: getEmpPosType.id,
posTypeName: getEmpPosType.posTypeName,
posTypeRank: getEmpPosType.posTypeRank,
posLevels: getEmpPosType.employeePosLevels.map((empPosLevel) => ({
id: empPosLevel.id,
posLevelName: empPosLevel.posLevelName,
posLevelRank: empPosLevel.posLevelRank,
})),
};
return new HttpSuccess(mapGetEmpPosType);
}
/**
* API
*
* @summary ORG_ - (ADMIN) #
*
*/
@Get()
async GetEmpPosType() {
const empPosType = await this.employeePosTypeRepository.find({
select: ["id", "posTypeName", "posTypeRank"],
relations: ["employeePosLevels"],
});
const mapEmpPosType = empPosType.map((item) => ({
id: item.id,
posTypeName: item.posTypeName,
posTypeRank: item.posTypeRank,
posLevels: item.employeePosLevels.map((empPosLevel) => ({
id: empPosLevel.id,
posLevelName: empPosLevel.posLevelName,
posLevelRank: empPosLevel.posLevelRank,
})),
}));
return new HttpSuccess(mapEmpPosType);
}
}

View file

@ -0,0 +1,266 @@
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 HttpError from "../interfaces/http-error";
import { Equal, ILike, In, IsNull, Like, Not, Brackets, Between } from "typeorm";
import { EmployeePosDict, CreateEmployeePosDict, UpdateEmployeePosDict } from "../entities/EmployeePosDict";
import { EmployeePosType, CreateEmployeePosType, UpdateEmployeePosType } from "../entities/EmployeePosType";
import { EmployeePosLevel, CreateEmployeePosLevel, UpdateEmployeePosLevel } from "../entities/EmployeePosLevel";
import { KeyObject } from "crypto";
@Route("api/v1/org/employee/pos")
@Tags("Employee")
@Security("bearerAuth")
@Response(
HttpStatusCode.INTERNAL_SERVER_ERROR,
"เกิดข้อผิดพลาด ไม่สามารถแสดงรายการได้ กรุณาลองใหม่ในภายหลัง",
)
@SuccessResponse(HttpStatusCode.OK, "สำเร็จ")
export class EmployeePositionController extends Controller {
private employeePosDictRepository = AppDataSource.getRepository(EmployeePosDict);
private employeePosTypeRepository = AppDataSource.getRepository(EmployeePosType);
private employeePosLevelRepository = AppDataSource.getRepository(EmployeePosLevel);
/**
* API
*
* @summary ORG_ - (ADMIN) #
*
*/
@Post("position")
async CreateEmployeePosition(
@Body()
requestBody: CreateEmployeePosDict,
@Request() request: { user: Record<string, any> },
) {
const empPosDict = Object.assign(new EmployeePosDict(), requestBody);
if (!empPosDict) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล");
}
const EmpPosType = await this.employeePosTypeRepository.findOne({
where: { id: String(requestBody.employeePosTypeId) },
});
if (!EmpPosType) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลกลุ่มงานนี้");
}
const EmpPosLevel = await this.employeePosLevelRepository.findOne({
where: { id: String(requestBody.employeePosLevelId) },
});
if (!EmpPosLevel) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลระดับชั้นงานนี้");
}
const rowRepeated = await this.employeePosDictRepository.findOne({
where: {
posDictName: String(requestBody.posDictName),
employeePosTypeId: String(requestBody.employeePosTypeId),
employeePosLevelId: String(requestBody.employeePosLevelId),
},
});
if (rowRepeated) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ข้อมูล Row นี้มีอยู่ในระบบแล้ว");
}
empPosDict.createdUserId = request.user.sub;
empPosDict.createdFullName = request.user.name;
empPosDict.lastUpdateUserId = request.user.sub;
empPosDict.lastUpdateFullName = request.user.name;
await this.employeePosDictRepository.save(empPosDict);
return new HttpSuccess(empPosDict.id);
}
/**
* API
*
* @summary (ADMIN)
*
*/
@Put("position/{id}")
async updatePosition(
@Path() id: string,
@Body()
requestBody: UpdateEmployeePosDict,
@Request() request: { user: Record<string, any> },
) {
const empPosDict = await this.employeePosDictRepository.findOne({
where: { id: id },
});
if (!empPosDict) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลตำแหน่งลูกจ้างประจำนี้");
}
const checkEmpPosTypeId = await this.employeePosTypeRepository.findOne({
where: { id: requestBody.employeePosTypeId },
});
if (!checkEmpPosTypeId) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลกลุ่มงานนี้");
}
const checkEmpPosLevelId = await this.employeePosLevelRepository.findOne({
where: { id: requestBody.employeePosLevelId },
});
if (!checkEmpPosLevelId) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลระดับกลุ่มงานนี้");
}
const rowRepeated = await this.employeePosDictRepository.findOne({
where: {
id: Not(id),
posDictName: requestBody.posDictName,
employeePosTypeId: requestBody.employeePosTypeId,
employeePosLevelId: requestBody.employeePosLevelId,
},
});
if (rowRepeated) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ข้อมูล Row นี้มีอยู่ในระบบแล้ว");
}
empPosDict.lastUpdateUserId = request.user.sub;
empPosDict.lastUpdateFullName = request.user.name;
this.employeePosDictRepository.merge(empPosDict, requestBody);
await this.employeePosDictRepository.save(empPosDict);
return new HttpSuccess(empPosDict.id);
}
/**
* API
*
* @summary ORG_ - (ADMIN) #
*
* @param {string} id Id
*/
@Delete("position/{id}")
async delete(@Path() id: string) {
const delEmpPosDict = await this.employeePosDictRepository.findOne({ where: { id } });
if (!delEmpPosDict) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลตำแหน่งลูกจ้างประจำนี้");
}
await this.employeePosDictRepository.remove(delEmpPosDict);
return new HttpSuccess();
}
/**
* API
*
* @summary ORG_037 - (ADMIN) #
*
*/
@Get("position/{id}")
async GetEmpPositionById(@Path() id: string) {
const empPosDict = await this.employeePosDictRepository.findOne({
select: ["id", "posDictName", "employeePosTypeId", "employeePosLevelId"],
where: { id: id },
});
if (!empPosDict) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลกลุ่มงานนี้");
}
const empPosType = await this.employeePosTypeRepository.findOne({
select: ["id", "posTypeName", "posTypeRank", "posTypeShortName"],
where: { id: empPosDict.employeePosTypeId },
});
if (!empPosType) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลระดับชั้นงานนี้");
}
const empPosLevel = await this.employeePosLevelRepository.findOne({
select: ["id", "posLevelName", "posLevelRank"],
where: { id: empPosDict.employeePosLevelId },
});
if (!empPosLevel) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลตำแหน่งลูกจ้างประจำนี้");
}
const mapData = {
id: empPosDict.id,
posDictName: empPosDict.posDictName, //ชื่อตำแหน่ง
posTypeName: empPosType.posTypeName, //กลุ่มงาน
posLevelName: empPosLevel.posLevelName, //ระดับขั้นงาน
}
return new HttpSuccess(mapData);
}
/**
* API
*
* @summary ORG_ - (ADMIN) #
*
*/
@Get("position")
async GetEmpPosition(@Query("keyword") keyword?: string, @Query("type") type?: string) {
let findData: any;
switch (type) {
case "posDictName":
findData = await this.employeePosDictRepository.find({
where: { posDictName: Like(`%${keyword}%`) },
relations: ["employeePosType", "employeePosLevel"],
});
break;
case "posTypeName":
const findEmpTypes: EmployeePosType[] = await this.employeePosTypeRepository.find({
where: { posTypeName: Like(`%${keyword}%`) },
select: ["id"],
});
findData = await this.employeePosDictRepository.find({
where: { employeePosTypeId: In(findEmpTypes.map((x) => x.id)) },
relations: ["employeePosType", "employeePosLevel"],
});
break;
case "posLevelName":
if(!isNaN(Number(keyword))) {
const findEmpLevels: EmployeePosLevel[] = await this.employeePosLevelRepository.find({
where: { posLevelName: Number(keyword) },
});
findData = await this.employeePosDictRepository.find({
where: { employeePosLevelId: In(findEmpLevels.map((x) => x.id)) },
relations: ["employeePosType", "employeePosLevel"],
});
}
else { //กรณีเลือกค้นหาจาก"ระดับชั้นงาน" แต่กรอกไม่ใช่ number ให้ปล่อยมาหมดเลย
findData = await this.employeePosDictRepository.find({
relations: ["employeePosType", "employeePosLevel"],
});
}
break;
default:
findData = await this.employeePosDictRepository.find({
relations: ["employeePosType", "employeePosLevel"],
});
break;
}
const mapDataEmpPosDict = await Promise.all(
findData.map(async (item: any) => {
return {
id: item.id,
posDictName: item.posDictName,
posTypeName: item.posType == null ? null : item.posType.posTypeName,
posLevelName: item.posLevel == null ? null : item.posLevel.posLevelName,
};
}),
);
return new HttpSuccess(mapDataEmpPosDict);
}
}

View file

@ -79,6 +79,15 @@
},
{
"name": "OrganizationUnauthorize", "description": "โครงสร้างส่วนอื่น ๆ (Unauthorize)"
},
{
"name": "Employee", "description": "ตำแหน่งลูกจ้างประจำ"
},
{
"name": "EmployeePosType", "description": "ประเภทกลุ่มงานลูกจ้างประจำ"
},
{
"name": "EmployeePosLevel", "description": "ระดับชั้นงานลูกจ้างประจำ"
}
]
},