diff --git a/src/controllers/EmployeePositionController.ts b/src/controllers/EmployeePositionController.ts index 601842ea..7b6ccb33 100644 --- a/src/controllers/EmployeePositionController.ts +++ b/src/controllers/EmployeePositionController.ts @@ -1433,150 +1433,151 @@ export class EmployeePositionController extends Controller { ...type4LastPosMasterNo, ]; - let maxPosMasterNo = Math.max(...allLastPosMasterNo.map((pos) => pos.posMasterNo), 0); + // let maxPosMasterNo = Math.max(...allLastPosMasterNo.map((pos) => pos.posMasterNo), 0); let maxPosMasterOrder = Math.max(...allLastPosMasterNo.map((pos) => pos.posMasterOrder), 0); - posMasters.forEach(async (posMaster: any) => { - let change = true; + await Promise.all( + posMasters.map(async (posMaster: any) => { + let change = true; - if (requestBody.type == 0) { - const org = await this.orgRootRepository.findOne({ - where: { id: requestBody.id }, - }); - if (org != null) { - const _posMaster = await this.employeePosMasterRepository.findOne({ - where: { orgRootId: org.id, posMasterNo: posMaster.posMasterNo }, + if (requestBody.type == 0) { + const org = await this.orgRootRepository.findOne({ + where: { id: requestBody.id }, }); - if (_posMaster != null) - throw new HttpError( - HttpStatusCode.NOT_FOUND, - `เลขที่ตำแหน่ง ${org.orgRootShortName}${posMaster.posMasterNo} มีอยู่ในระบบอยู่แล้ว`, - ); - if ( - posMaster.orgRootId == org.id && - posMaster.orgChild1Id == null && - posMaster.orgChild2Id == null && - posMaster.orgChild3Id == null && - posMaster.orgChild4Id == null - ) - change = false; - posMaster.orgRootId = org.id; - posMaster.orgRevisionId = org.orgRevisionId; - posMaster.orgChild1Id = null; - posMaster.orgChild2Id = null; - posMaster.orgChild3Id = null; - posMaster.orgChild4Id = null; + if (org != null) { + const _posMaster = await this.employeePosMasterRepository.findOne({ + where: { orgRootId: org.id, posMasterNo: posMaster.posMasterNo }, + }); + if (_posMaster != null) + throw new HttpError( + HttpStatusCode.NOT_FOUND, + `เลขที่ตำแหน่ง ${org.orgRootShortName}${posMaster.posMasterNo} มีอยู่ในระบบอยู่แล้ว`, + ); + if ( + posMaster.orgRootId == org.id && + posMaster.orgChild1Id == null && + posMaster.orgChild2Id == null && + posMaster.orgChild3Id == null && + posMaster.orgChild4Id == null + ) + change = false; + posMaster.orgRootId = org.id; + posMaster.orgRevisionId = org.orgRevisionId; + posMaster.orgChild1Id = null; + posMaster.orgChild2Id = null; + posMaster.orgChild3Id = null; + posMaster.orgChild4Id = null; + } } - } - if (requestBody.type == 1) { - const org = await this.child1Repository.findOne({ - where: { id: requestBody.id }, - }); - if (org != null) { - const _posMaster = await this.employeePosMasterRepository.findOne({ - where: { orgChild1Id: org.id, posMasterNo: posMaster.posMasterNo }, + if (requestBody.type == 1) { + const org = await this.child1Repository.findOne({ + where: { id: requestBody.id }, }); - if (_posMaster != null) - throw new HttpError( - HttpStatusCode.NOT_FOUND, - `เลขที่ตำแหน่ง ${org.orgChild1ShortName}${posMaster.posMasterNo} มีอยู่ในระบบอยู่แล้ว`, - ); - if ( - posMaster.orgChild1Id == org.id && - posMaster.orgChild2Id == null && - posMaster.orgChild3Id == null && - posMaster.orgChild4Id == null - ) - change = false; - posMaster.orgRootId = org.orgRootId; - posMaster.orgChild1Id = org.id; - posMaster.orgRevisionId = org.orgRevisionId; - posMaster.orgChild2Id = null; - posMaster.orgChild3Id = null; - posMaster.orgChild4Id = null; + if (org != null) { + const _posMaster = await this.employeePosMasterRepository.findOne({ + where: { orgChild1Id: org.id, posMasterNo: posMaster.posMasterNo }, + }); + if (_posMaster != null) + throw new HttpError( + HttpStatusCode.NOT_FOUND, + `เลขที่ตำแหน่ง ${org.orgChild1ShortName}${posMaster.posMasterNo} มีอยู่ในระบบอยู่แล้ว`, + ); + if ( + posMaster.orgChild1Id == org.id && + posMaster.orgChild2Id == null && + posMaster.orgChild3Id == null && + posMaster.orgChild4Id == null + ) + change = false; + posMaster.orgRootId = org.orgRootId; + posMaster.orgChild1Id = org.id; + posMaster.orgRevisionId = org.orgRevisionId; + posMaster.orgChild2Id = null; + posMaster.orgChild3Id = null; + posMaster.orgChild4Id = null; + } } - } - if (requestBody.type == 2) { - const org = await this.child2Repository.findOne({ - where: { id: requestBody.id }, - }); - if (org != null) { - const _posMaster = await this.employeePosMasterRepository.findOne({ - where: { orgChild2Id: org.id, posMasterNo: posMaster.posMasterNo }, + if (requestBody.type == 2) { + const org = await this.child2Repository.findOne({ + where: { id: requestBody.id }, }); - if (_posMaster != null) - throw new HttpError( - HttpStatusCode.NOT_FOUND, - `เลขที่ตำแหน่ง ${org.orgChild2ShortName}${posMaster.posMasterNo} มีอยู่ในระบบอยู่แล้ว`, - ); - if ( - posMaster.orgChild2Id == org.id && - posMaster.orgChild3Id == null && - posMaster.orgChild4Id == null - ) - change = false; - posMaster.orgRootId = org.orgRootId; - posMaster.orgChild1Id = org.orgChild1Id; - posMaster.orgChild2Id = org.id; - posMaster.orgRevisionId = org.orgRevisionId; - posMaster.orgChild3Id = null; - posMaster.orgChild4Id = null; + if (org != null) { + const _posMaster = await this.employeePosMasterRepository.findOne({ + where: { orgChild2Id: org.id, posMasterNo: posMaster.posMasterNo }, + }); + if (_posMaster != null) + throw new HttpError( + HttpStatusCode.NOT_FOUND, + `เลขที่ตำแหน่ง ${org.orgChild2ShortName}${posMaster.posMasterNo} มีอยู่ในระบบอยู่แล้ว`, + ); + if ( + posMaster.orgChild2Id == org.id && + posMaster.orgChild3Id == null && + posMaster.orgChild4Id == null + ) + change = false; + posMaster.orgRootId = org.orgRootId; + posMaster.orgChild1Id = org.orgChild1Id; + posMaster.orgChild2Id = org.id; + posMaster.orgRevisionId = org.orgRevisionId; + posMaster.orgChild3Id = null; + posMaster.orgChild4Id = null; + } } - } - if (requestBody.type == 3) { - const org = await this.child3Repository.findOne({ - where: { id: requestBody.id }, - }); - if (org != null) { - const _posMaster = await this.employeePosMasterRepository.findOne({ - where: { orgChild3Id: org.id, posMasterNo: posMaster.posMasterNo }, + if (requestBody.type == 3) { + const org = await this.child3Repository.findOne({ + where: { id: requestBody.id }, }); - if (_posMaster != null) - throw new HttpError( - HttpStatusCode.NOT_FOUND, - `เลขที่ตำแหน่ง ${org.orgChild3ShortName}${posMaster.posMasterNo} มีอยู่ในระบบอยู่แล้ว`, - ); - if (posMaster.orgChild3Id == org.id && posMaster.orgChild4Id == null) change = false; - posMaster.orgRootId = org.orgRootId; - posMaster.orgChild1Id = org.orgChild1Id; - posMaster.orgChild2Id = org.orgChild2Id; - posMaster.orgChild3Id = org.id; - posMaster.orgRevisionId = org.orgRevisionId; - posMaster.orgChild4Id = null; + if (org != null) { + const _posMaster = await this.employeePosMasterRepository.findOne({ + where: { orgChild3Id: org.id, posMasterNo: posMaster.posMasterNo }, + }); + if (_posMaster != null) + throw new HttpError( + HttpStatusCode.NOT_FOUND, + `เลขที่ตำแหน่ง ${org.orgChild3ShortName}${posMaster.posMasterNo} มีอยู่ในระบบอยู่แล้ว`, + ); + if (posMaster.orgChild3Id == org.id && posMaster.orgChild4Id == null) change = false; + posMaster.orgRootId = org.orgRootId; + posMaster.orgChild1Id = org.orgChild1Id; + posMaster.orgChild2Id = org.orgChild2Id; + posMaster.orgChild3Id = org.id; + posMaster.orgRevisionId = org.orgRevisionId; + posMaster.orgChild4Id = null; + } } - } - if (requestBody.type == 4) { - const org = await this.child4Repository.findOne({ - where: { id: requestBody.id }, - }); - if (org != null) { - const _posMaster = await this.employeePosMasterRepository.findOne({ - where: { orgChild4Id: org.id, posMasterNo: posMaster.posMasterNo }, + if (requestBody.type == 4) { + const org = await this.child4Repository.findOne({ + where: { id: requestBody.id }, }); - if (_posMaster != null) - throw new HttpError( - HttpStatusCode.NOT_FOUND, - `เลขที่ตำแหน่ง ${org.orgChild4ShortName}${posMaster.posMasterNo} มีอยู่ในระบบอยู่แล้ว`, - ); - if (posMaster.orgChild4Id == org.id) change = false; - posMaster.orgRootId = org.orgRootId; - posMaster.orgChild1Id = org.orgChild1Id; - posMaster.orgChild2Id = org.orgChild2Id; - posMaster.orgChild3Id = org.orgChild3Id; - posMaster.orgChild4Id = org.id; - posMaster.orgRevisionId = org.orgRevisionId; + if (org != null) { + const _posMaster = await this.employeePosMasterRepository.findOne({ + where: { orgChild4Id: org.id, posMasterNo: posMaster.posMasterNo }, + }); + if (_posMaster != null) + throw new HttpError( + HttpStatusCode.NOT_FOUND, + `เลขที่ตำแหน่ง ${org.orgChild4ShortName}${posMaster.posMasterNo} มีอยู่ในระบบอยู่แล้ว`, + ); + if (posMaster.orgChild4Id == org.id) change = false; + posMaster.orgRootId = org.orgRootId; + posMaster.orgChild1Id = org.orgChild1Id; + posMaster.orgChild2Id = org.orgChild2Id; + posMaster.orgChild3Id = org.orgChild3Id; + posMaster.orgChild4Id = org.id; + posMaster.orgRevisionId = org.orgRevisionId; + } } - } - if (change == true) { - // posMaster.posMasterNo = maxPosMasterNo += 1; - posMaster.posMasterOrder = maxPosMasterOrder += 1; - posMaster.createdUserId = request.user.sub; - posMaster.createdFullName = request.user.name; - posMaster.lastUpdateUserId = request.user.sub; - posMaster.lastUpdateFullName = request.user.name; - await this.employeePosMasterRepository.save(posMaster); - } - }); + if (change == true) { + posMaster.posMasterOrder = maxPosMasterOrder += 1; + posMaster.createdUserId = request.user.sub; + posMaster.createdFullName = request.user.name; + posMaster.lastUpdateUserId = request.user.sub; + posMaster.lastUpdateFullName = request.user.name; + await this.employeePosMasterRepository.save(posMaster); + } + }), + ); return new HttpSuccess(); } diff --git a/src/controllers/PositionController.ts b/src/controllers/PositionController.ts index 108dec07..aa5abbde 100644 --- a/src/controllers/PositionController.ts +++ b/src/controllers/PositionController.ts @@ -1621,146 +1621,149 @@ export class PositionController extends Controller { // let maxPosMasterNo = Math.max(...allLastPosMasterNo.map((pos) => pos.posMasterNo), 0); let maxPosMasterOrder = Math.max(...allLastPosMasterNo.map((pos) => pos.posMasterOrder), 0); - posMasters.forEach(async (posMaster: any) => { - let change = true; + await Promise.all( + posMasters.map(async (posMaster: any) => { + let change = true; - if (requestBody.type == 0) { - const org = await this.orgRootRepository.findOne({ - where: { id: requestBody.id }, - }); - if (org != null) { - const _posMaster = await this.posMasterRepository.findOne({ - where: { orgRootId: org.id, posMasterNo: posMaster.posMasterNo }, + if (requestBody.type == 0) { + const org = await this.orgRootRepository.findOne({ + where: { id: requestBody.id }, }); - if (_posMaster != null) - throw new HttpError( - HttpStatusCode.NOT_FOUND, - `เลขที่ตำแหน่ง ${org.orgRootShortName}${posMaster.posMasterNo} มีอยู่ในระบบอยู่แล้ว`, - ); - if ( - posMaster.orgRootId == org.id && - posMaster.orgChild1Id == null && - posMaster.orgChild2Id == null && - posMaster.orgChild3Id == null && - posMaster.orgChild4Id == null - ) - change = false; - posMaster.orgRootId = org.id; - posMaster.orgRevisionId = org.orgRevisionId; - posMaster.orgChild1Id = null; - posMaster.orgChild2Id = null; - posMaster.orgChild3Id = null; - posMaster.orgChild4Id = null; + if (org != null) { + const _posMaster = await this.posMasterRepository.findOne({ + where: { orgRootId: org.id, posMasterNo: posMaster.posMasterNo }, + }); + if (_posMaster != null) + throw new HttpError( + HttpStatusCode.NOT_FOUND, + `เลขที่ตำแหน่ง ${org.orgRootShortName}${posMaster.posMasterNo} มีอยู่ในระบบอยู่แล้ว`, + ); + if ( + posMaster.orgRootId == org.id && + posMaster.orgChild1Id == null && + posMaster.orgChild2Id == null && + posMaster.orgChild3Id == null && + posMaster.orgChild4Id == null + ) + change = false; + posMaster.orgRootId = org.id; + posMaster.orgRevisionId = org.orgRevisionId; + posMaster.orgChild1Id = null; + posMaster.orgChild2Id = null; + posMaster.orgChild3Id = null; + posMaster.orgChild4Id = null; + } } - } - if (requestBody.type == 1) { - const org = await this.child1Repository.findOne({ - where: { id: requestBody.id }, - }); - if (org != null) { - const _posMaster = await this.posMasterRepository.findOne({ - where: { orgChild1Id: org.id, posMasterNo: posMaster.posMasterNo }, + if (requestBody.type == 1) { + const org = await this.child1Repository.findOne({ + where: { id: requestBody.id }, }); - if (_posMaster != null) - throw new HttpError( - HttpStatusCode.NOT_FOUND, - `เลขที่ตำแหน่ง ${org.orgChild1ShortName}${posMaster.posMasterNo} มีอยู่ในระบบอยู่แล้ว`, - ); - if ( - posMaster.orgChild1Id == org.id && - posMaster.orgChild2Id == null && - posMaster.orgChild3Id == null && - posMaster.orgChild4Id == null - ) - change = false; - posMaster.orgRootId = org.orgRootId; - posMaster.orgChild1Id = org.id; - posMaster.orgRevisionId = org.orgRevisionId; - posMaster.orgChild2Id = null; - posMaster.orgChild3Id = null; - posMaster.orgChild4Id = null; + if (org != null) { + const _posMaster = await this.posMasterRepository.findOne({ + where: { orgChild1Id: org.id, posMasterNo: posMaster.posMasterNo }, + }); + if (_posMaster != null) + throw new HttpError( + HttpStatusCode.NOT_FOUND, + `เลขที่ตำแหน่ง ${org.orgChild1ShortName}${posMaster.posMasterNo} มีอยู่ในระบบอยู่แล้ว`, + ); + if ( + posMaster.orgChild1Id == org.id && + posMaster.orgChild2Id == null && + posMaster.orgChild3Id == null && + posMaster.orgChild4Id == null + ) + change = false; + posMaster.orgRootId = org.orgRootId; + posMaster.orgChild1Id = org.id; + posMaster.orgRevisionId = org.orgRevisionId; + posMaster.orgChild2Id = null; + posMaster.orgChild3Id = null; + posMaster.orgChild4Id = null; + } } - } - if (requestBody.type == 2) { - const org = await this.child2Repository.findOne({ - where: { id: requestBody.id }, - }); - if (org != null) { - const _posMaster = await this.posMasterRepository.findOne({ - where: { orgChild2Id: org.id, posMasterNo: posMaster.posMasterNo }, + if (requestBody.type == 2) { + const org = await this.child2Repository.findOne({ + where: { id: requestBody.id }, }); - if (_posMaster != null) - throw new HttpError( - HttpStatusCode.NOT_FOUND, - `เลขที่ตำแหน่ง ${org.orgChild2ShortName}${posMaster.posMasterNo} มีอยู่ในระบบอยู่แล้ว`, - ); - if ( - posMaster.orgChild2Id == org.id && - posMaster.orgChild3Id == null && - posMaster.orgChild4Id == null - ) - change = false; - posMaster.orgRootId = org.orgRootId; - posMaster.orgChild1Id = org.orgChild1Id; - posMaster.orgChild2Id = org.id; - posMaster.orgRevisionId = org.orgRevisionId; - posMaster.orgChild3Id = null; - posMaster.orgChild4Id = null; + if (org != null) { + const _posMaster = await this.posMasterRepository.findOne({ + where: { orgChild2Id: org.id, posMasterNo: posMaster.posMasterNo }, + }); + if (_posMaster != null) + throw new HttpError( + HttpStatusCode.NOT_FOUND, + `เลขที่ตำแหน่ง ${org.orgChild2ShortName}${posMaster.posMasterNo} มีอยู่ในระบบอยู่แล้ว`, + ); + if ( + posMaster.orgChild2Id == org.id && + posMaster.orgChild3Id == null && + posMaster.orgChild4Id == null + ) + change = false; + posMaster.orgRootId = org.orgRootId; + posMaster.orgChild1Id = org.orgChild1Id; + posMaster.orgChild2Id = org.id; + posMaster.orgRevisionId = org.orgRevisionId; + posMaster.orgChild3Id = null; + posMaster.orgChild4Id = null; + } } - } - if (requestBody.type == 3) { - const org = await this.child3Repository.findOne({ - where: { id: requestBody.id }, - }); - if (org != null) { - const _posMaster = await this.posMasterRepository.findOne({ - where: { orgChild3Id: org.id, posMasterNo: posMaster.posMasterNo }, + if (requestBody.type == 3) { + const org = await this.child3Repository.findOne({ + where: { id: requestBody.id }, }); - if (_posMaster != null) - throw new HttpError( - HttpStatusCode.NOT_FOUND, - `เลขที่ตำแหน่ง ${org.orgChild3ShortName}${posMaster.posMasterNo} มีอยู่ในระบบอยู่แล้ว`, - ); - if (posMaster.orgChild3Id == org.id && posMaster.orgChild4Id == null) change = false; - posMaster.orgRootId = org.orgRootId; - posMaster.orgChild1Id = org.orgChild1Id; - posMaster.orgChild2Id = org.orgChild2Id; - posMaster.orgChild3Id = org.id; - posMaster.orgRevisionId = org.orgRevisionId; - posMaster.orgChild4Id = null; + if (org != null) { + const _posMaster = await this.posMasterRepository.findOne({ + where: { orgChild3Id: org.id, posMasterNo: posMaster.posMasterNo }, + }); + if (_posMaster != null) + throw new HttpError( + HttpStatusCode.NOT_FOUND, + `เลขที่ตำแหน่ง ${org.orgChild3ShortName}${posMaster.posMasterNo} มีอยู่ในระบบอยู่แล้ว`, + ); + if (posMaster.orgChild3Id == org.id && posMaster.orgChild4Id == null) change = false; + posMaster.orgRootId = org.orgRootId; + posMaster.orgChild1Id = org.orgChild1Id; + posMaster.orgChild2Id = org.orgChild2Id; + posMaster.orgChild3Id = org.id; + posMaster.orgRevisionId = org.orgRevisionId; + posMaster.orgChild4Id = null; + } } - } - if (requestBody.type == 4) { - const org = await this.child4Repository.findOne({ - where: { id: requestBody.id }, - }); - if (org != null) { - const _posMaster = await this.posMasterRepository.findOne({ - where: { orgChild4Id: org.id, posMasterNo: posMaster.posMasterNo }, + if (requestBody.type == 4) { + const org = await this.child4Repository.findOne({ + where: { id: requestBody.id }, }); - if (_posMaster != null) - throw new HttpError( - HttpStatusCode.NOT_FOUND, - `เลขที่ตำแหน่ง ${org.orgChild4ShortName}${posMaster.posMasterNo} มีอยู่ในระบบอยู่แล้ว`, - ); - if (posMaster.orgChild4Id == org.id) change = false; - posMaster.orgRootId = org.orgRootId; - posMaster.orgChild1Id = org.orgChild1Id; - posMaster.orgChild2Id = org.orgChild2Id; - posMaster.orgChild3Id = org.orgChild3Id; - posMaster.orgChild4Id = org.id; - posMaster.orgRevisionId = org.orgRevisionId; + if (org != null) { + const _posMaster = await this.posMasterRepository.findOne({ + where: { orgChild4Id: org.id, posMasterNo: posMaster.posMasterNo }, + }); + if (_posMaster != null) + throw new HttpError( + HttpStatusCode.NOT_FOUND, + `เลขที่ตำแหน่ง ${org.orgChild4ShortName}${posMaster.posMasterNo} มีอยู่ในระบบอยู่แล้ว`, + ); + + if (posMaster.orgChild4Id == org.id) change = false; + posMaster.orgRootId = org.orgRootId; + posMaster.orgChild1Id = org.orgChild1Id; + posMaster.orgChild2Id = org.orgChild2Id; + posMaster.orgChild3Id = org.orgChild3Id; + posMaster.orgChild4Id = org.id; + posMaster.orgRevisionId = org.orgRevisionId; + } } - } - if (change == true) { - posMaster.posMasterOrder = maxPosMasterOrder += 1; - posMaster.createdUserId = request.user.sub; - posMaster.createdFullName = request.user.name; - posMaster.lastUpdateUserId = request.user.sub; - posMaster.lastUpdateFullName = request.user.name; - await this.posMasterRepository.save(posMaster); - } - }); + if (change == true) { + posMaster.posMasterOrder = maxPosMasterOrder += 1; + posMaster.createdUserId = request.user.sub; + posMaster.createdFullName = request.user.name; + posMaster.lastUpdateUserId = request.user.sub; + posMaster.lastUpdateFullName = request.user.name; + await this.posMasterRepository.save(posMaster); + } + }), + ); return new HttpSuccess(); }