From 72ac021b1632bc14ec6f948d40f48dbe96cacf33 Mon Sep 17 00:00:00 2001 From: AdisakKanthawilang Date: Thu, 25 Apr 2024 18:45:22 +0700 Subject: [PATCH] fix --- src/controllers/PositionController.ts | 164 +++++++++++++------------- 1 file changed, 83 insertions(+), 81 deletions(-) diff --git a/src/controllers/PositionController.ts b/src/controllers/PositionController.ts index fb16e0c8..f52c6f1c 100644 --- a/src/controllers/PositionController.ts +++ b/src/controllers/PositionController.ts @@ -621,7 +621,7 @@ export class PositionController extends Controller { posMaster.orgRevisionId = orgRoot.orgRevisionId; SName = orgRoot.orgRootShortName; } - + const chk_SName0 = await this.posMasterRepository.findOne({ where: { orgRevisionId: posMaster.orgRevisionId, @@ -700,7 +700,7 @@ export class PositionController extends Controller { "ไม่สามารถใส่เลขที่ตำแหน่งซ้ำกันได้", ); } - + posMaster.createdUserId = request.user.sub; posMaster.createdFullName = request.user.name; posMaster.lastUpdateUserId = request.user.sub; @@ -773,7 +773,7 @@ export class PositionController extends Controller { posMaster.posMasterNoPrefix = requestBody.posMasterNoPrefix; posMaster.posMasterNoSuffix = requestBody.posMasterNoSuffix; posMaster.reason = requestBody.reason == null ? "" : requestBody.reason; - + let orgRoot: any = null; let SName: any = null; if (requestBody.orgRootId != null) @@ -821,84 +821,86 @@ export class PositionController extends Controller { } else { SName = orgRoot.orgRootShortName; } - - const chk_SName0 = await this.posMasterRepository.findOne({ - where: { - orgRevisionId: posMaster.orgRevisionId, - orgRoot: { orgRootShortName: SName }, - orgChild1Id: IsNull(), - posMasterNo: requestBody.posMasterNo, - }, - relations: ["orgRoot"], - }); - if (chk_SName0 != null) { - throw new HttpError( - HttpStatusCode.INTERNAL_SERVER_ERROR, - "ไม่สามารถใส่เลขที่ตำแหน่งซ้ำกันได้", - ); - } - const chk_SName1 = await this.posMasterRepository.findOne({ - where: { - orgRevisionId: posMaster.orgRevisionId, - orgChild1: { orgChild1ShortName: SName }, - orgChild2Id: IsNull(), - posMasterNo: requestBody.posMasterNo, - }, - relations: ["orgChild1"], - }); - if (chk_SName1 != null) { - throw new HttpError( - HttpStatusCode.INTERNAL_SERVER_ERROR, - "ไม่สามารถใส่เลขที่ตำแหน่งซ้ำกันได้", - ); - } - - const chk_SName2 = await this.posMasterRepository.findOne({ - where: { - orgRevisionId: posMaster.orgRevisionId, - orgChild2: { orgChild2ShortName: SName }, - orgChild3Id: IsNull(), - posMasterNo: requestBody.posMasterNo, - }, - relations: ["orgChild2"], - }); - if (chk_SName2 != null) { - throw new HttpError( - HttpStatusCode.INTERNAL_SERVER_ERROR, - "ไม่สามารถใส่เลขที่ตำแหน่งซ้ำกันได้", - ); - } - - const chk_SName3 = await this.posMasterRepository.findOne({ - where: { - orgRevisionId: posMaster.orgRevisionId, - orgChild3: { orgChild3ShortName: SName }, - orgChild4Id: IsNull(), - posMasterNo: requestBody.posMasterNo, - }, - relations: ["orgChild3"], - }); - if (chk_SName3 != null) { - throw new HttpError( - HttpStatusCode.INTERNAL_SERVER_ERROR, - "ไม่สามารถใส่เลขที่ตำแหน่งซ้ำกันได้", - ); - } - - const chk_SName4 = await this.posMasterRepository.findOne({ - where: { - orgRevisionId: posMaster.orgRevisionId, - orgChild4: { orgChild4ShortName: SName }, - posMasterNo: requestBody.posMasterNo, - }, - relations: ["orgChild4"], - }); - if (chk_SName4 != null) { - throw new HttpError( - HttpStatusCode.INTERNAL_SERVER_ERROR, - "ไม่สามารถใส่เลขที่ตำแหน่งซ้ำกันได้", - ); + if (posMaster.orgRootId != null) { + const chk_SName0 = await this.posMasterRepository.findOne({ + where: { + orgRevisionId: posMaster.orgRevisionId, + orgRoot: { orgRootShortName: SName }, + orgChild1Id: IsNull(), + posMasterNo: requestBody.posMasterNo, + }, + relations: ["orgRoot"], + }); + if (chk_SName0 != null) { + throw new HttpError( + HttpStatusCode.INTERNAL_SERVER_ERROR, + "ไม่สามารถใส่เลขที่ตำแหน่งซ้ำกันได้", + ); + } + } else if (posMaster.orgChild1Id != null) { + const chk_SName1 = await this.posMasterRepository.findOne({ + where: { + orgRevisionId: posMaster.orgRevisionId, + orgChild1: { orgChild1ShortName: SName }, + orgChild2Id: IsNull(), + posMasterNo: requestBody.posMasterNo, + }, + relations: ["orgChild1"], + }); + if (chk_SName1 != null) { + throw new HttpError( + HttpStatusCode.INTERNAL_SERVER_ERROR, + "ไม่สามารถใส่เลขที่ตำแหน่งซ้ำกันได้", + ); + } + } else if (posMaster.orgChild2Id != null) { + const chk_SName2 = await this.posMasterRepository.findOne({ + where: { + orgRevisionId: posMaster.orgRevisionId, + orgChild2: { orgChild2ShortName: SName }, + orgChild3Id: IsNull(), + posMasterNo: requestBody.posMasterNo, + }, + relations: ["orgChild2"], + }); + if (chk_SName2 != null) { + throw new HttpError( + HttpStatusCode.INTERNAL_SERVER_ERROR, + "ไม่สามารถใส่เลขที่ตำแหน่งซ้ำกันได้", + ); + } + } else if (posMaster.orgChild3Id != null) { + const chk_SName3 = await this.posMasterRepository.findOne({ + where: { + orgRevisionId: posMaster.orgRevisionId, + orgChild3: { orgChild3ShortName: SName }, + orgChild4Id: IsNull(), + posMasterNo: requestBody.posMasterNo, + }, + relations: ["orgChild3"], + }); + if (chk_SName3 != null) { + throw new HttpError( + HttpStatusCode.INTERNAL_SERVER_ERROR, + "ไม่สามารถใส่เลขที่ตำแหน่งซ้ำกันได้", + ); + } + } else if (posMaster.orgChild4Id != null) { + const chk_SName4 = await this.posMasterRepository.findOne({ + where: { + orgRevisionId: posMaster.orgRevisionId, + orgChild4: { orgChild4ShortName: SName }, + posMasterNo: requestBody.posMasterNo, + }, + relations: ["orgChild4"], + }); + if (chk_SName4 != null) { + throw new HttpError( + HttpStatusCode.INTERNAL_SERVER_ERROR, + "ไม่สามารถใส่เลขที่ตำแหน่งซ้ำกันได้", + ); + } } posMaster.createdUserId = request.user.sub; @@ -1065,7 +1067,7 @@ export class PositionController extends Controller { orgChild3Id: body.id, }; if (!body.isAll) { - checkChildConditions = { + checkChildConditions = { orgChild4Id: IsNull(), }; searchShortName = `CONCAT(orgChild3.orgChild3ShortName,posMaster.posMasterNoPrefix,posMaster.posMasterNo,posMaster.posMasterNoSuffix) like '%${body.keyword}%'`;