diff --git a/src/controllers/PositionController.ts b/src/controllers/PositionController.ts index 80c61f57..9bd10fde 100644 --- a/src/controllers/PositionController.ts +++ b/src/controllers/PositionController.ts @@ -1453,18 +1453,80 @@ export class PositionController extends Controller { where: { id: In(requestBody.positionMaster) }, }); + const type0LastPosMasterNo = + requestBody.type == 0 + ? await this.posMasterRepository.find({ + where: { + orgRootId: requestBody.id, + orgChild1Id: IsNull(), + }, + }) + : []; + + const type1LastPosMasterNo = + requestBody.type == 1 + ? await this.posMasterRepository.find({ + where: { + orgChild1Id: requestBody.id, + orgChild2Id: IsNull(), + }, + }) + : []; + + const type2LastPosMasterNo = + requestBody.type == 2 + ? await this.posMasterRepository.find({ + where: { + orgChild2Id: requestBody.id, + orgChild3Id: IsNull(), + }, + }) + : []; + + const type3LastPosMasterNo = + requestBody.type == 3 + ? await this.posMasterRepository.find({ + where: { + orgChild3Id: requestBody.id, + orgChild4Id: IsNull(), + }, + }) + : []; + + const type4LastPosMasterNo = + requestBody.type == 4 + ? await this.posMasterRepository.find({ + where: { + orgChild4Id: requestBody.id, + }, + }) + : []; + + const allLastPosMasterNo = [ + ...type0LastPosMasterNo, + ...type1LastPosMasterNo, + ...type2LastPosMasterNo, + ...type3LastPosMasterNo, + ...type4LastPosMasterNo, + ]; + + 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) => { posMaster.orgRootId = null; posMaster.orgChild1Id = null; posMaster.orgChild2Id = null; posMaster.orgChild3Id = null; posMaster.orgChild4Id = null; + let change = true; if (requestBody.type == 0) { const org = await this.orgRootRepository.findOne({ where: { id: requestBody.id }, }); if (org != null) { + if (posMaster.orgRootId == org.id) change = false; posMaster.orgRootId = org.id; posMaster.orgRevisionId = org.orgRevisionId; } @@ -1474,6 +1536,7 @@ export class PositionController extends Controller { where: { id: requestBody.id }, }); if (org != null) { + if (posMaster.orgChild1Id == org.id) change = false; posMaster.orgRootId = org.orgRootId; posMaster.orgChild1Id = org.id; posMaster.orgRevisionId = org.orgRevisionId; @@ -1484,6 +1547,7 @@ export class PositionController extends Controller { where: { id: requestBody.id }, }); if (org != null) { + if (posMaster.orgChild2Id == org.id) change = false; posMaster.orgRootId = org.orgRootId; posMaster.orgChild1Id = org.orgChild1Id; posMaster.orgChild2Id = org.id; @@ -1495,6 +1559,7 @@ export class PositionController extends Controller { where: { id: requestBody.id }, }); if (org != null) { + if (posMaster.orgChild3Id == org.id) change = false; posMaster.orgRootId = org.orgRootId; posMaster.orgChild1Id = org.orgChild1Id; posMaster.orgChild2Id = org.orgChild2Id; @@ -1507,6 +1572,9 @@ export class PositionController extends Controller { where: { id: requestBody.id }, }); if (org != null) { + if (posMaster.orgChild4Id == org.id) { + change = false; + } posMaster.orgRootId = org.orgRootId; posMaster.orgChild1Id = org.orgChild1Id; posMaster.orgChild2Id = org.orgChild2Id; @@ -1515,74 +1583,15 @@ export class PositionController extends Controller { posMaster.orgRevisionId = org.orgRevisionId; } } - const type0LastPosMasterNo = - requestBody.type == 0 - ? await this.posMasterRepository.find({ - where: { - orgRootId: requestBody.id, - orgChild1Id: IsNull(), - }, - }) - : []; - - const type1LastPosMasterNo = - requestBody.type == 1 - ? await this.posMasterRepository.find({ - where: { - orgChild1Id: requestBody.id, - orgChild2Id: IsNull(), - }, - }) - : []; - - const type2LastPosMasterNo = - requestBody.type == 2 - ? await this.posMasterRepository.find({ - where: { - orgChild2Id: requestBody.id, - orgChild3Id: IsNull(), - }, - }) - : []; - - const type3LastPosMasterNo = - requestBody.type == 3 - ? await this.posMasterRepository.find({ - where: { - orgChild3Id: requestBody.id, - orgChild4Id: IsNull(), - }, - }) - : []; - - const type4LastPosMasterNo = - requestBody.type == 4 - ? await this.posMasterRepository.find({ - where: { - orgChild4Id: requestBody.id, - }, - }) - : []; - - const allLastPosMasterNo = [ - ...type0LastPosMasterNo, - ...type1LastPosMasterNo, - ...type2LastPosMasterNo, - ...type3LastPosMasterNo, - ...type4LastPosMasterNo, - ]; - - const maxPosMasterNo = Math.max(...allLastPosMasterNo.map((pos) => pos.posMasterNo), 0); - - const maxPosMasterOrder = Math.max(...allLastPosMasterNo.map((pos) => pos.posMasterOrder), 0); - - 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.posMasterRepository.save(posMaster); + 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.posMasterRepository.save(posMaster); + } }); return new HttpSuccess(); }