diff --git a/src/controllers/EmployeePosLevelController.ts b/src/controllers/EmployeePosLevelController.ts new file mode 100644 index 00000000..09a70f2d --- /dev/null +++ b/src/controllers/EmployeePosLevelController.ts @@ -0,0 +1,190 @@ +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,} from "../entities/EmployeePosType"; +import { EmployeePosLevel, CreateEmployeePosLevel, UpdateEmployeePosLevel } from "../entities/EmployeePosLevel"; +import { EmployeePosDict } from "../entities/EmployeePosDict"; + +@Route("api/v1/org/employee/pos/level") +@Tags("EmployeePosLevel") +@Security("bearerAuth") +@Response( + HttpStatusCode.INTERNAL_SERVER_ERROR, + "เกิดข้อผิดพลาด ไม่สามารถแสดงรายการได้ กรุณาลองใหม่ในภายหลัง", +) +@SuccessResponse(HttpStatusCode.OK, "สำเร็จ") +export class EmployeePosLevelController extends Controller { + + private employeePosDictRepository = AppDataSource.getRepository(EmployeePosDict); + private employeePosTypeRepository = AppDataSource.getRepository(EmployeePosType); + private employeePosLevelRepository = AppDataSource.getRepository(EmployeePosLevel); + + /** + * API เพิ่มระดับชั้นงานลูกจ้างประจำ + * + * @summary ORG_ - เพิ่มระดับชั้นงานลูกจ้างประจำ (ADMIN) # + * + */ + @Post() + async CreateEmpLevel( + @Body() + requestBody: CreateEmployeePosLevel, + @Request() request: { user: Record }, + ) { + const EmpPosLevel = Object.assign(new EmployeePosLevel(), requestBody); + if (!EmpPosLevel) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล"); + } + + const EmpPosType = await this.employeePosTypeRepository.findOne({ + where: { id: requestBody.employeePosTypeId } + }); + if (!EmpPosType) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลประเภทกลุ่มงานลูกจ้างประจำนี้"); + } + + const chkEmpPosLevelName = await this.employeePosLevelRepository.findOne({ + where: { + posLevelName: requestBody.posLevelName, + }, + }); + if (chkEmpPosLevelName) { + throw new HttpError( + HttpStatusCode.NOT_FOUND, + "ชื่อระดับชั้นงานลูกจ้างประจำนี้มีอยู่ในระบบแล้ว", + ); + } + + EmpPosLevel.createdUserId = request.user.sub; + EmpPosLevel.createdFullName = request.user.name; + EmpPosLevel.lastUpdateUserId = request.user.sub; + EmpPosLevel.lastUpdateFullName = request.user.name; + await this.employeePosLevelRepository.save(EmpPosLevel); + return new HttpSuccess(EmpPosLevel.id); + } + + /** + * API แก้ไขระดับชั้นงานลูกจ้างประจำ + * + * @summary ORG_ - แก้ไขระดับชั้นงานลูกจ้างประจำ (ADMIN) # + * + * @param {string} id Id ระดับชั้นงานลูกจ้างประจำ + */ + @Put("{id}") + async EditEmpLevel( + @Path() id: string, + @Body() requestBody: UpdateEmployeePosLevel, + @Request() request: { user: Record }, + ) { + const EmpPosLevel = await this.employeePosLevelRepository.findOne({ where: { id } }); + if (!EmpPosLevel) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลระดับชั้นงานลูกจ้างประจำนี้"); + } + + const EmpPosType = await this.employeePosTypeRepository.findOne({ + where: { id: requestBody.employeePosTypeId } + }); + if (!EmpPosType) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลประเภทกลุ่มงานลูกจ้างประจำนี้"); + } + + const chkEmpPosLevel = await this.employeePosLevelRepository.findOne({ + where: { + id: Not(id), + posLevelName: requestBody.posLevelName, + }, + }); + if (chkEmpPosLevel) { + throw new HttpError( + HttpStatusCode.NOT_FOUND, + "ชื่อระดับชั้นงานลูกจ้างประจำนี้มีอยู่ในระบบแล้ว", + ); + } + EmpPosLevel.lastUpdateUserId = request.user.sub; + EmpPosLevel.lastUpdateFullName = request.user.name; + this.employeePosLevelRepository.merge(EmpPosLevel, requestBody); + await this.employeePosLevelRepository.save(EmpPosLevel); + return new HttpSuccess(EmpPosLevel.id); + } + + /** + * API ลบระดับชั้นงานลูกจ้างประจำ + * + * @summary ORG_ - ลบระดับชั้นงานลูกจ้างประจำ (ADMIN) # + * + * @param {string} id Id ระดับชั้นงานลูกจ้างประจำ + */ + @Delete("{id}") + async deleteType(@Path() id: string) { + const delEmpPosLevel = await this.employeePosLevelRepository.findOne({ where: { id } }); + if (!delEmpPosLevel) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลระดับชั้นงานลูกจ้างประจำนี้"); + } + + //ตารางตำแหน่งลูกจ้างประจำ + const EmpPosition = await this.employeePosDictRepository.find({ + where: { employeePosLevelId: id }, + }); + if (EmpPosition.length > 0) { + throw new HttpError( + HttpStatusCode.NOT_FOUND, + "ไม่สามารถลบได้เนื่องจากพบข้อมูลที่ตารางตำแหน่งลูกจ้างประจำ", + ); + } + + await this.employeePosLevelRepository.remove(delEmpPosLevel); + return new HttpSuccess(); + } + + /** + * API รายละเอียดระดับชั้นงานลูกจ้างประจำ + * + * @summary ORG_ - รายละเอียดระดับชั้นงานลูกจ้างประจำ (ADMIN) # + * + * @param {string} id Id ระดับชั้นงานลูกจ้างประจำ + */ + @Get("{id}") + async GetEmpLevelById(@Path() id: string) { + const getEmpPosLevel = await this.employeePosLevelRepository.findOne({ + select: ["id", "posLevelName", "posLevelRank",], + where: { id: id }, + }); + if (!getEmpPosLevel) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลระดับชั้นงานลูกจ้างประจำนี้"); + } + + return new HttpSuccess(getEmpPosLevel); + } + + /** + * API รายการระดับชั้นงานลูกจ้างประจำ + * + * @summary ORG_ - รายการระดับชั้นงานลูกจ้างประจำ (ADMIN) # + * + */ + @Get() + async GetEmpPosLevel() { + const empPosLevel = await this.employeePosLevelRepository.find({ + select: ["id","posLevelName", "posLevelRank",], + }); + + return new HttpSuccess(empPosLevel); + } +} \ No newline at end of file diff --git a/src/controllers/EmployeePosTypeController.ts b/src/controllers/EmployeePosTypeController.ts index 1f70e347..24ef06ed 100644 --- a/src/controllers/EmployeePosTypeController.ts +++ b/src/controllers/EmployeePosTypeController.ts @@ -20,6 +20,7 @@ import HttpError from "../interfaces/http-error"; import { Not } from "typeorm"; import { EmployeePosType, CreateEmployeePosType, UpdateEmployeePosType } from "../entities/EmployeePosType"; import { EmployeePosLevel } from "../entities/EmployeePosLevel"; +import { EmployeePosDict } from "../entities/EmployeePosDict"; @Route("api/v1/org/employee/pos/type") @Tags("EmployeePosType") @@ -30,6 +31,8 @@ import { EmployeePosLevel } from "../entities/EmployeePosLevel"; ) @SuccessResponse(HttpStatusCode.OK, "สำเร็จ") export class EmployeePosTypeController extends Controller { + + private employeePosDictRepository = AppDataSource.getRepository(EmployeePosDict); private employeePosTypeRepository = AppDataSource.getRepository(EmployeePosType); private employeePosLevelRepository = AppDataSource.getRepository(EmployeePosLevel); @@ -118,7 +121,7 @@ export class EmployeePosTypeController extends Controller { if (!delEmpPosType) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลประเภทกลุ่มงานลูกจ้างประจำนี้"); } - + //ตารางระดับชั้นงาน const EmpPosLevel = await this.employeePosLevelRepository.find({ where: { employeePosTypeId: id }, }); @@ -128,6 +131,16 @@ export class EmployeePosTypeController extends Controller { "ไม่สามารถลบได้เนื่องจากพบข้อมูลที่ตารางระดับชั้นงาน", ); } + //ตารางตำแหน่งลูกจ้างประจำ + const EmpPosition = await this.employeePosDictRepository.find({ + where: { employeePosTypeId: id }, + }); + if (EmpPosition.length > 0) { + throw new HttpError( + HttpStatusCode.NOT_FOUND, + "ไม่สามารถลบได้เนื่องจากพบข้อมูลที่ตารางตำแหน่งลูกจ้างประจำ", + ); + } await this.employeePosTypeRepository.remove(delEmpPosType); return new HttpSuccess(); diff --git a/src/entities/EmployeePosLevel.ts b/src/entities/EmployeePosLevel.ts index c2843706..70f71fd8 100644 --- a/src/entities/EmployeePosLevel.ts +++ b/src/entities/EmployeePosLevel.ts @@ -39,6 +39,9 @@ export class CreateEmployeePosLevel { @Column() posLevelRank: number; + @Column("uuid") + employeePosTypeId: string; + } export type UpdateEmployeePosLevel= Partial; \ No newline at end of file