From c667f7361f67232920e143d52da83ca79e5af0ad Mon Sep 17 00:00:00 2001 From: Bright Date: Mon, 10 Jun 2024 14:02:53 +0700 Subject: [PATCH] =?UTF-8?q?=E0=B9=81=E0=B8=81=E0=B9=89=E0=B8=A5=E0=B8=9A?= =?UTF-8?q?=E0=B8=82=E0=B9=89=E0=B8=AD=E0=B8=A1=E0=B8=B9=E0=B8=A5=E0=B8=95?= =?UTF-8?q?=E0=B8=B3=E0=B9=81=E0=B8=AB=E0=B8=99=E0=B9=88=E0=B8=87=E0=B8=82?= =?UTF-8?q?=E0=B8=A3=E0=B8=81./=E0=B8=A5=E0=B8=B9=E0=B8=81=E0=B8=88?= =?UTF-8?q?=E0=B9=89=E0=B8=B2=E0=B8=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/controllers/EmployeePosLevelController.ts | 37 ++++++++----- src/controllers/EmployeePosTypeController.ts | 55 +++++++++++-------- src/controllers/PosLevelController.ts | 17 ++++-- src/controllers/PosTypeController.ts | 33 +++++++---- 4 files changed, 89 insertions(+), 53 deletions(-) diff --git a/src/controllers/EmployeePosLevelController.ts b/src/controllers/EmployeePosLevelController.ts index 31e1271f..d3539d3b 100644 --- a/src/controllers/EmployeePosLevelController.ts +++ b/src/controllers/EmployeePosLevelController.ts @@ -139,23 +139,32 @@ export class EmployeePosLevelController extends Controller { */ @Delete("{id}") async deleteType(@Path() id: string) { - const delEmpPosLevel = await this.employeePosLevelRepository.findOne({ where: { id } }); - if (!delEmpPosLevel) { - throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลระดับชั้นงานลูกจ้างประจำนี้"); + let result :any + try { + result = await this.employeePosLevelRepository.delete({ id: id }); + } catch { + throw new HttpError( HttpStatusCode.NOT_FOUND, "ไม่สามารถลบได้เนื่องจากมีการใช้งานระดับชั้นงานนี้อยู่"); } - - //ตารางตำแหน่งลูกจ้างประจำ - const EmpPosition = await this.employeePosDictRepository.find({ - where: { posLevelId: id }, - }); - if (EmpPosition.length > 0) { - throw new HttpError( - HttpStatusCode.NOT_FOUND, - "ไม่สามารถลบได้เนื่องจากพบข้อมูลที่ตารางตำแหน่งลูกจ้างประจำ", - ); + if (result.affected == undefined || result.affected <= 0) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล"); } + // const delEmpPosLevel = await this.employeePosLevelRepository.findOne({ where: { id } }); + // if (!delEmpPosLevel) { + // throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลระดับชั้นงานลูกจ้างประจำนี้"); + // } - await this.employeePosLevelRepository.remove(delEmpPosLevel); + // //ตารางตำแหน่งลูกจ้างประจำ + // const EmpPosition = await this.employeePosDictRepository.find({ + // where: { posLevelId: id }, + // }); + // if (EmpPosition.length > 0) { + // throw new HttpError( + // HttpStatusCode.NOT_FOUND, + // "ไม่สามารถลบได้เนื่องจากพบข้อมูลที่ตารางตำแหน่งลูกจ้างประจำ", + // ); + // } + + // await this.employeePosLevelRepository.remove(delEmpPosLevel); return new HttpSuccess(); } diff --git a/src/controllers/EmployeePosTypeController.ts b/src/controllers/EmployeePosTypeController.ts index 03bd9dd6..aeefffac 100644 --- a/src/controllers/EmployeePosTypeController.ts +++ b/src/controllers/EmployeePosTypeController.ts @@ -134,32 +134,41 @@ export class EmployeePosTypeController extends Controller { */ @Delete("{id}") async deleteType(@Path() id: string) { - const delEmpPosType = await this.employeePosTypeRepository.findOne({ where: { id } }); - if (!delEmpPosType) { - throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลกลุ่มงานลูกจ้างประจำนี้"); + let result :any + try { + result = await this.employeePosTypeRepository.delete({ id: id }); + } catch { + throw new HttpError( HttpStatusCode.NOT_FOUND, "ไม่สามารถลบได้เนื่องจากมีการใช้งานระดับกลุ่มงานนี้อยู่"); } - //ตารางระดับชั้นงาน - const EmpPosLevel = await this.employeePosLevelRepository.find({ - where: { posTypeId: id }, - }); - if (EmpPosLevel.length > 0) { - throw new HttpError( - HttpStatusCode.NOT_FOUND, - "ไม่สามารถลบได้เนื่องจากพบข้อมูลที่ตารางระดับชั้นงาน", - ); - } - //ตารางตำแหน่งลูกจ้างประจำ - const EmpPosition = await this.employeePosDictRepository.find({ - where: { posTypeId: id }, - }); - if (EmpPosition.length > 0) { - throw new HttpError( - HttpStatusCode.NOT_FOUND, - "ไม่สามารถลบได้เนื่องจากพบข้อมูลที่ตารางตำแหน่งลูกจ้างประจำ", - ); + if (result.affected == undefined || result.affected <= 0) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล"); } + // const delEmpPosType = await this.employeePosTypeRepository.findOne({ where: { id } }); + // if (!delEmpPosType) { + // throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลกลุ่มงานลูกจ้างประจำนี้"); + // } + // // ตารางระดับชั้นงาน + // const EmpPosLevel = await this.employeePosLevelRepository.find({ + // where: { posTypeId: id }, + // }); + // if (EmpPosLevel.length > 0) { + // throw new HttpError( + // HttpStatusCode.NOT_FOUND, + // "ไม่สามารถลบข้อมูลได้เนื่องจากมีการใช้งานระดับชั้นงานนี้อยู่", + // ); + // } + // //ตารางตำแหน่งลูกจ้างประจำ + // const EmpPosition = await this.employeePosDictRepository.find({ + // where: { posTypeId: id }, + // }); + // if (EmpPosition.length > 0) { + // throw new HttpError( + // HttpStatusCode.NOT_FOUND, + // "ไม่สามารถลบได้เนื่องจากพบข้อมูลที่ตารางตำแหน่งลูกจ้างประจำ", + // ); + // } - await this.employeePosTypeRepository.remove(delEmpPosType); + // await this.employeePosTypeRepository.remove(delEmpPosType); return new HttpSuccess(); } diff --git a/src/controllers/PosLevelController.ts b/src/controllers/PosLevelController.ts index e612bc5c..eea43181 100644 --- a/src/controllers/PosLevelController.ts +++ b/src/controllers/PosLevelController.ts @@ -158,11 +158,20 @@ export class PosLevelController extends Controller { */ @Delete("{id}") async deleteLevel(@Path() id: string) { - const delPosLevel = await this.posLevelRepository.findOne({ where: { id } }); - if (!delPosLevel) { - throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลระดับตำแหน่งนี้"); + let result :any + try { + result = await this.posLevelRepository.delete({ id: id }); + } catch { + throw new HttpError( HttpStatusCode.NOT_FOUND, "ไม่สามารถลบได้เนื่องจากมีการใช้งานระดับตำแหน่งนี้อยู่"); } - await this.posLevelRepository.remove(delPosLevel); + 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(); } diff --git a/src/controllers/PosTypeController.ts b/src/controllers/PosTypeController.ts index 950d7645..8cd28488 100644 --- a/src/controllers/PosTypeController.ts +++ b/src/controllers/PosTypeController.ts @@ -118,21 +118,30 @@ export class PosTypeController extends Controller { */ @Delete("{id}") async deleteType(@Path() id: string) { - const delPosType = await this.posTypeRepository.findOne({ where: { id } }); - if (!delPosType) { - throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลประเภทตำแหน่งนี้"); + let result :any + try { + result = await this.posTypeRepository.delete({ id: id }); + } catch { + throw new HttpError( HttpStatusCode.NOT_FOUND, "ไม่สามารถลบได้เนื่องจากมีการใช้งานประเภทตำแหน่งนี้อยู่"); } - const IdExitsInLevel = await this.posLevelRepository.find({ - where: { posTypeId: id }, - }); - if (IdExitsInLevel.length > 0) { - throw new HttpError( - HttpStatusCode.NOT_FOUND, - "ไม่สามารถลบได้เนื่องจากพบข้อมูลที่ตารางระดับตำแหน่ง", - ); + if (result.affected == undefined || result.affected <= 0) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล"); } + // const delPosType = await this.posTypeRepository.findOne({ where: { id } }); + // if (!delPosType) { + // throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลประเภทตำแหน่งนี้"); + // } + // const IdExitsInLevel = await this.posLevelRepository.find({ + // where: { posTypeId: id }, + // }); + // if (IdExitsInLevel.length > 0) { + // throw new HttpError( + // HttpStatusCode.NOT_FOUND, + // "ไม่สามารถลบได้เนื่องจากพบข้อมูลที่ตารางระดับตำแหน่ง", + // ); + // } - await this.posTypeRepository.remove(delPosType); + // await this.posTypeRepository.remove(delPosType); return new HttpSuccess(); }