diff --git a/src/controllers/PositionController.ts b/src/controllers/PositionController.ts index f85d4706..aaf0953b 100644 --- a/src/controllers/PositionController.ts +++ b/src/controllers/PositionController.ts @@ -1582,7 +1582,7 @@ export class PositionController extends Controller { ]; // let maxPosMasterNo = Math.max(...allLastPosMasterNo.map((pos) => pos.posMasterNo), 0); - + let _shortName : string let maxPosMasterOrder = Math.max(...allLastPosMasterNo.map((pos) => pos.posMasterOrder), 0); await Promise.all( posMasters.map(async (posMaster: any) => { @@ -1593,6 +1593,7 @@ export class PositionController extends Controller { where: { id: requestBody.id }, }); if (org != null) { + _shortName = org.orgRootShortName const _posMaster = await this.posMasterRepository.findOne({ where: { orgRootId: org.id, @@ -1626,6 +1627,7 @@ export class PositionController extends Controller { where: { id: requestBody.id }, }); if (org != null) { + _shortName = org.orgChild1ShortName const _posMaster = await this.posMasterRepository.findOne({ where: { orgChild1Id: org.id, @@ -1658,6 +1660,7 @@ export class PositionController extends Controller { where: { id: requestBody.id }, }); if (org != null) { + _shortName = org.orgChild2ShortName const _posMaster = await this.posMasterRepository.findOne({ where: { orgChild2Id: org.id, @@ -1689,6 +1692,7 @@ export class PositionController extends Controller { where: { id: requestBody.id }, }); if (org != null) { + _shortName = org.orgChild3ShortName const _posMaster = await this.posMasterRepository.findOne({ where: { orgChild3Id: org.id, @@ -1715,6 +1719,7 @@ export class PositionController extends Controller { where: { id: requestBody.id }, }); if (org != null) { + _shortName = org.orgChild4ShortName const _posMaster = await this.posMasterRepository.findOne({ where: { orgChild4Id: org.id, @@ -1736,6 +1741,143 @@ export class PositionController extends Controller { posMaster.orgRevisionId = org.orgRevisionId; } } + + //CheckAll + const orgRevision = await this.orgRevisionRepository.findOne({ + where: { + orgRevisionIsDraft: true, + orgRevisionIsCurrent: false, + } + }) + const _orgRoot = await this.orgRootRepository.find({ + where: { + orgRevisionId: orgRevision?.id, + orgRootShortName: _shortName, + id: Not(requestBody.id), + } + }) + if(_orgRoot.length > 0){ + for(const r of _orgRoot){ + const _posMasterRoot = await this.posMasterRepository.findOne({ + where: { + orgRevisionId: orgRevision?.id, + orgRootId: r.id, + orgChild1Id: IsNull() || "", + posMasterNo: posMaster.posMasterNo + } + }) + if(_posMasterRoot != null){ + throw new HttpError( + HttpStatusCode.NOT_FOUND, + `เลขที่ตำแหน่ง ${r.orgRootShortName}${posMaster.posMasterNo} มีอยู่ในระบบอยู่แล้ว`, + ); + } + } + } + //child1 + const _orgChild1 = await this.child1Repository.find({ + where: { + orgRevisionId: orgRevision?.id, + orgChild1ShortName: _shortName, + id: Not(requestBody.id), + } + }) + if(_orgChild1.length > 0){ + for(const c1 of _orgChild1){ + const _posMasterChild1 = await this.posMasterRepository.findOne({ + where: { + orgRevisionId: orgRevision?.id, + orgChild1Id: c1.id, + orgChild2Id: IsNull() || "", + posMasterNo: posMaster.posMasterNo + } + }) + if(_posMasterChild1 != null){ + throw new HttpError( + HttpStatusCode.NOT_FOUND, + `เลขที่ตำแหน่ง ${c1.orgChild1ShortName}${posMaster.posMasterNo} มีอยู่ในระบบอยู่แล้ว`, + ); + } + } + } + //child2 + const _orgChild2 = await this.child2Repository.find({ + where: { + orgRevisionId: orgRevision?.id, + orgChild2ShortName: _shortName, + id: Not(requestBody.id), + } + }) + if(_orgChild2.length > 0){ + for(const c2 of _orgChild2){ + const _posMasterChild2 = await this.posMasterRepository.findOne({ + where: { + orgRevisionId: orgRevision?.id, + orgChild2Id: c2.id, + orgChild3Id: IsNull() || "", + posMasterNo: posMaster.posMasterNo + } + }) + if(_posMasterChild2 != null){ + throw new HttpError( + HttpStatusCode.NOT_FOUND, + `เลขที่ตำแหน่ง ${c2.orgChild2ShortName}${posMaster.posMasterNo} มีอยู่ในระบบอยู่แล้ว`, + ); + } + } + } + //child3 + const _orgChild3 = await this.child3Repository.find({ + where: { + orgRevisionId: orgRevision?.id, + orgChild3ShortName: _shortName, + id: Not(requestBody.id), + } + }) + if(_orgChild3.length > 0){ + for(const c3 of _orgChild3){ + const _posMasterChild3 = await this.posMasterRepository.findOne({ + where: { + orgRevisionId: orgRevision?.id, + orgChild3Id: c3.id, + orgChild4Id: IsNull() || "", + posMasterNo: posMaster.posMasterNo + } + }) + if(_posMasterChild3 != null){ + throw new HttpError( + HttpStatusCode.NOT_FOUND, + `เลขที่ตำแหน่ง ${c3.orgChild3ShortName}${posMaster.posMasterNo} มีอยู่ในระบบอยู่แล้ว`, + ); + } + } + } + //child4 + const _orgChild4 = await this.child4Repository.find({ + where: { + orgRevisionId: orgRevision?.id, + orgChild4ShortName: _shortName, + id: Not(requestBody.id), + } + }) + if(_orgChild4.length > 0){ + for(const c4 of _orgChild4){ + const _posMasterChild4 = await this.posMasterRepository.findOne({ + where: { + orgRevisionId: orgRevision?.id, + orgChild4Id: c4.id, + posMasterNo: posMaster.posMasterNo + } + }) + if(_posMasterChild4 != null){ + throw new HttpError( + HttpStatusCode.NOT_FOUND, + `เลขที่ตำแหน่ง ${c4.orgChild4ShortName}${posMaster.posMasterNo} มีอยู่ในระบบอยู่แล้ว`, + ); + } + } + } + if (change == true) { posMaster.posMasterOrder = maxPosMasterOrder += 1; posMaster.createdUserId = request.user.sub;