diff --git a/src/controllers/EmployeePosTypeController.ts b/src/controllers/EmployeePosTypeController.ts new file mode 100644 index 00000000..1f70e347 --- /dev/null +++ b/src/controllers/EmployeePosTypeController.ts @@ -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 }, + ) { + 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 }, + ) { + 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); + } +} \ No newline at end of file diff --git a/src/controllers/EmployeePositionController.ts b/src/controllers/EmployeePositionController.ts new file mode 100644 index 00000000..4d14762c --- /dev/null +++ b/src/controllers/EmployeePositionController.ts @@ -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 }, + ) { + + 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 }, + ) { + 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); + } +} diff --git a/tsoa.json b/tsoa.json index 87fa35f9..25ecac0c 100644 --- a/tsoa.json +++ b/tsoa.json @@ -79,6 +79,15 @@ }, { "name": "OrganizationUnauthorize", "description": "โครงสร้างส่วนอื่น ๆ (Unauthorize)" + }, + { + "name": "Employee", "description": "ตำแหน่งลูกจ้างประจำ" + }, + { + "name": "EmployeePosType", "description": "ประเภทกลุ่มงานลูกจ้างประจำ" + }, + { + "name": "EmployeePosLevel", "description": "ระดับชั้นงานลูกจ้างประจำ" } ] },