diff --git a/src/controllers/PositionController.ts b/src/controllers/PositionController.ts index 359ee4c7..67c9c639 100644 --- a/src/controllers/PositionController.ts +++ b/src/controllers/PositionController.ts @@ -662,6 +662,8 @@ export class PositionController extends Controller { try { let typeCondition: any = {}; let checkChildConditions: any = {}; + let keywordAsInt: any; + if (body.type === 0) { typeCondition = { orgRootId: body.id, @@ -738,7 +740,6 @@ export class PositionController extends Controller { select: ["posMasterId"], }); masterId = masterId.concat(findPosition.map((position: any) => position.posMasterId)); - let keywordAsInt: any; keywordAsInt = body.keyword == null ? null : parseInt(body.keyword, 10); if (isNaN(keywordAsInt)) { keywordAsInt = "P@ssw0rd!z"; @@ -1795,18 +1796,16 @@ export class PositionController extends Controller { */ @Post("dna") async dna(@Body() requestBody: { draftPositionId: string; publishPositionId: string }) { - const dataDraft = await this.posMasterRepository.findOne({ - where: { id: requestBody.draftPositionId }, + const findDraft = await this.orgRevisionRepository.findOne({ + where: { + orgRevisionIsDraft: true, + }, }); - if (!dataDraft) { - throw new HttpError( - HttpStatusCode.NOT_FOUND, - "ไม่พบข้อมูลไอดีนี้(draftPositionId) : " + requestBody.draftPositionId, - ); - } const dataPublish = await this.posMasterRepository.findOne({ - where: { id: requestBody.publishPositionId }, + where: { + id: requestBody.publishPositionId, + }, }); if (!dataPublish) { throw new HttpError( @@ -1815,13 +1814,40 @@ export class PositionController extends Controller { ); } + const dataDraft = await this.posMasterRepository.findOne({ + where: { + id: requestBody.draftPositionId, + }, + }); + if (!dataDraft) { + throw new HttpError( + HttpStatusCode.NOT_FOUND, + "ไม่พบข้อมูลไอดีนี้(draftPositionId) : " + requestBody.draftPositionId, + ); + } + + const dataPosMaster = await this.posMasterRepository.find({ + where: { + orgRevisionId: findDraft?.id, + ancestorDNA: dataPublish.ancestorDNA, + }, + }); + if (!dataPosMaster) { + throw new HttpError( + HttpStatusCode.NOT_FOUND, + "ไม่พบข้อมูลไอดีนี้ใน posMaster (publishPositionId) : " + requestBody.publishPositionId, + ); + } + try { + await this.posMasterRepository.update( + { orgRevisionId: findDraft?.id, ancestorDNA: dataPublish.ancestorDNA }, + { ancestorDNA: "" }, + ); + dataDraft.ancestorDNA = dataPublish.ancestorDNA; await this.posMasterRepository.save(dataDraft); - dataPublish.ancestorDNA = ""; - await this.posMasterRepository.save(dataPublish); - return new HttpSuccess(); } catch (error) { return error;