From 748c89294b59b460fefaac68883102be198342cb Mon Sep 17 00:00:00 2001 From: AdisakKanthawilang Date: Thu, 25 Apr 2024 14:21:00 +0700 Subject: [PATCH] fix test created --- src/controllers/PositionController.ts | 171 ++++++++++++-------------- 1 file changed, 78 insertions(+), 93 deletions(-) diff --git a/src/controllers/PositionController.ts b/src/controllers/PositionController.ts index 6f1f3e35..e8835b1b 100644 --- a/src/controllers/PositionController.ts +++ b/src/controllers/PositionController.ts @@ -701,7 +701,84 @@ export class PositionController extends Controller { "ไม่สามารถใส่เลขที่ตำแหน่งซ้ำกันได้", ); } - + + let fullPosName = String((requestBody.posMasterNoPrefix).trim() + (requestBody.posMasterNo) + (requestBody.posMasterNoSuffix).trim()); + let filterName = `CONCAT(posMaster.posMasterNoPrefix,posMaster.posMasterNo,posMaster.posMasterNoSuffix)`; + + const chk_FullName0 = await AppDataSource.getRepository(PosMaster) + .createQueryBuilder("posMaster") + .from("posMaster", "posMaster") + .where("posMaster.orgRevisionId = :orgRevisionId", { orgRevisionId: posMaster.orgRevisionId }) + .andWhere("posMaster.orgRootShortName = :orgRootShortName", { orgRootShortName: SName }) + .andWhere("posMaster.orgChild1Id IS NULL") + .andWhere(`${filterName} = :fullPosName`, { fullPosName }); + + if (chk_FullName0 != null) { + throw new HttpError( + HttpStatusCode.INTERNAL_SERVER_ERROR, + "ไม่สามารถเพิ่มชื่อตำแหน่งซ้ำกันได้", + ); + } + + const chk_FullName1 = await AppDataSource.getRepository(PosMaster) + .createQueryBuilder("posMaster") + .from("posMaster", "posMaster") + .where("posMaster.orgRevisionId = :orgRevisionId", { orgRevisionId: posMaster.orgRevisionId }) + .andWhere("posMaster.orgChild1ShortName = :orgChild1ShortName", { orgChild1ShortName: SName }) + .andWhere("posMaster.orgChild2Id IS NULL") + .andWhere(`${filterName} = :fullPosName`, { fullPosName }); + + if (chk_FullName1 != null) { + throw new HttpError( + HttpStatusCode.INTERNAL_SERVER_ERROR, + "ไม่สามารถเพิ่มชื่อตำแหน่งซ้ำกันได้", + ); + } + + const chk_FullName2 = await AppDataSource.getRepository(PosMaster) + .createQueryBuilder("posMaster") + .from("posMaster", "posMaster") + .where("posMaster.orgRevisionId = :orgRevisionId", { orgRevisionId: posMaster.orgRevisionId }) + .andWhere("posMaster.orgChild2ShortName = :orgChild2ShortName", { orgChild2ShortName: SName }) + .andWhere("posMaster.orgChild3Id IS NULL") + .andWhere(`${filterName} = :fullPosName`, { fullPosName }); + + if (chk_FullName2 != null) { + throw new HttpError( + HttpStatusCode.INTERNAL_SERVER_ERROR, + "ไม่สามารถเพิ่มชื่อตำแหน่งซ้ำกันได้", + ); + } + + const chk_FullName3 = await AppDataSource.getRepository(PosMaster) + .createQueryBuilder("posMaster") + .from("posMaster", "posMaster") + .where("posMaster.orgRevisionId = :orgRevisionId", { orgRevisionId: posMaster.orgRevisionId }) + .andWhere("posMaster.orgChild3ShortName = :orgChild3ShortName", { orgChild3ShortName: SName }) + .andWhere("posMaster.orgChild4Id IS NULL") + .andWhere(`${filterName} = :fullPosName`, { fullPosName }); + + if (chk_FullName3 != null) { + throw new HttpError( + HttpStatusCode.INTERNAL_SERVER_ERROR, + "ไม่สามารถเพิ่มชื่อตำแหน่งซ้ำกันได้", + ); + } + + const chk_FullName4 = await AppDataSource.getRepository(PosMaster) + .createQueryBuilder("posMaster") + .from("posMaster", "posMaster") + .where("posMaster.orgRevisionId = :orgRevisionId", { orgRevisionId: posMaster.orgRevisionId }) + .andWhere("posMaster.orgChild4ShortName = :orgChild4ShortName", { orgChild4ShortName: SName }) + .andWhere(`${filterName} = :fullPosName`, { fullPosName }); + + if (chk_FullName4 != null) { + throw new HttpError( + HttpStatusCode.INTERNAL_SERVER_ERROR, + "ไม่สามารถเพิ่มชื่อตำแหน่งซ้ำกันได้", + ); + } + posMaster.createdUserId = request.user.sub; posMaster.createdFullName = request.user.name; @@ -921,98 +998,6 @@ export class PositionController extends Controller { ); } } - - // let fullPosMasterNo = String((requestBody.posMasterNoPrefix).trim() + (requestBody.posMasterNo) + (requestBody.posMasterNoSuffix).trim()); - // if (fullPosMasterNo === String(requestBody.posMasterNo)) { - // throw new HttpError( - // HttpStatusCode.INTERNAL_SERVER_ERROR, - // "ไม่สามารถเพิ่มข้อมูลชื่อตำแหน่งซ้ำกันได้", - // ); - // } - - // const chk_SName0 = await this.posMasterRepository.findOne({ - // where: { - // orgRevisionId: posMaster.orgRevisionId, - // orgRoot: { orgRootShortName: SName }, - // orgChild1Id: IsNull(), - // posMasterNo: requestBody.posMasterNo, - // id: Not(posMaster.id), - // }, - // 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, - // id: Not(posMaster.id), - // }, - // 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, - // id: Not(posMaster.id), - // }, - // 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, - // id: Not(posMaster.id), - // }, - // 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, - // id: Not(posMaster.id), - // }, - // relations: ["orgChild4"], - // }); - // if (chk_SName4 != null) { - // throw new HttpError( - // HttpStatusCode.INTERNAL_SERVER_ERROR, - // "ไม่สามารถใส่เลขที่ตำแหน่งซ้ำกันได้", - // ); - // } posMaster.createdUserId = request.user.sub; posMaster.createdFullName = request.user.name;