diff --git a/src/controllers/PositionController.ts b/src/controllers/PositionController.ts index ad8603b6..3ab2ea8f 100644 --- a/src/controllers/PositionController.ts +++ b/src/controllers/PositionController.ts @@ -1236,11 +1236,10 @@ export class PositionController extends Controller { @Request() request: RequestWithUser, ) { await new permission().PermissionUpdate(request, "SYS_ORG"); - const posMaster = await this.posMasterRepository.findOne({ - relations: ["positions"], - where: { id: id } - } - ); + const posMaster = await this.posMasterRepository.findOne({ + relations: ["positions", "orgRevision"], + where: { id: id }, + }); if (!posMaster) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลอัตรากำลัง"); } @@ -1397,39 +1396,55 @@ export class PositionController extends Controller { setLogDataDiff(request, { before, after: posMaster }); const result = await Promise.all( - requestBody.positions.map(async (x:any) => { - const match = posMaster.positions.find((p:any) => p.id == x.id); - if(match){ - match.positionIsSelected = x.positionIsSelected??false; + requestBody.positions.map(async (x: any) => { + const match = posMaster.positions.find((p: any) => p.id == x.id); + if (match) { + match.positionIsSelected = x.positionIsSelected ?? false; match.orderNo = x.orderNo ?? null; - return match - }else{ + return match; + } else { x.id = x.id ?? undefined; x.posMasterId = posMaster.id; x.orderNo = x.orderNo ?? null; x.positionName = x.posDictName; x.positionField = x.posDictField; - x.posExecutiveId = x.posExecutiveId || null + x.posExecutiveId = x.posExecutiveId || null; x.createdUserId = request.user.sub; x.createdFullName = request.user.name; x.lastUpdateUserId = request.user.sub; x.lastUpdateFullName = request.user.name; x.createdAt = new Date(); - x.lastUpdatedAt = new Date(); - return x + x.lastUpdatedAt = new Date(); + return x; } }), ); await this.positionRepository.save(result, { data: request }); await Promise.all( - posMaster.positions.map(async (x:any) => { - const match = requestBody.positions.find((p:any) => p.id == x.id); - if(!match){ + posMaster.positions.map(async (x: any) => { + const match = requestBody.positions.find((p: any) => p.id == x.id); + if (!match) { //delete await this.positionRepository.remove(x); } }), ); + + if (posMaster.orgRevision?.orgRevisionIsDraft != false) { + const _position = posMaster.positions.find((p) => p.positionIsSelected == true); + if (_position) { + const current_holderId: any = posMaster.current_holderId; + const _profile = await this.profileRepository.findOne({ + where: { id: current_holderId }, + }); + if (_profile) { + _profile.position = _position.positionName; + _profile.posTypeId = _position.posTypeId; + _profile.posLevelId = _position.posLevelId; + await this.profileRepository.save(_profile); + } + } + } return new HttpSuccess(posMaster.id); } @@ -1524,7 +1539,7 @@ export class PositionController extends Controller { // posLevel: { // posLevelRank: "ASC", // }, - orderNo: "ASC" + orderNo: "ASC", }, }); const formattedData = { @@ -2974,7 +2989,7 @@ export class PositionController extends Controller { where: { // orgRevisionIsDraft: true, // orgRevisionIsCurrent: false, - id: posMaster.orgRevisionId + id: posMaster.orgRevisionId, }, }); const _orgRoot = await this.orgRootRepository.find({ @@ -3614,24 +3629,23 @@ export class PositionController extends Controller { }); if (chkRevision?.orgRevisionIsCurrent) { const _profile = await this.profileRepository.findOne({ - where: { id: requestBody.profileId } + where: { id: requestBody.profileId }, }); if (_profile) { let _position = await this.positionRepository.findOne({ - where: { id: requestBody.position, posMasterId: requestBody.posMaster } + where: { id: requestBody.position, posMasterId: requestBody.posMaster }, }); if (_position) { - _profile.position = _position.positionName - _profile.posTypeId = _position.posTypeId - _profile.posLevelId = _position.posLevelId + _profile.position = _position.positionName; + _profile.posTypeId = _position.posTypeId; + _profile.posLevelId = _position.posLevelId; await this.profileRepository.save(_profile); setLogDataDiff(request, { before, after: _profile }); } } dataMaster.current_holderId = requestBody.profileId; dataMaster.next_holderId = _null; - } - else { + } else { dataMaster.next_holderId = requestBody.profileId; dataMaster.current_holderId = _null; } @@ -3658,7 +3672,7 @@ export class PositionController extends Controller { if (!dataMaster) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลตำแหน่งนี้"); } - let _profileId: string = "" + let _profileId: string = ""; if (dataMaster?.current_holderId) { _profileId = dataMaster?.current_holderId; } @@ -3674,24 +3688,24 @@ export class PositionController extends Controller { positionIsSelected: false, }); }); - //เช็คถ้า revision ปัจจุบันให้ปั๊มที่ profile - const chkRevision = await this.orgRevisionRepository.findOne({ - where: { id: dataMaster.orgRevisionId }, - }); - if (chkRevision?.orgRevisionIsCurrent) { - const _profile = await this.profileRepository.findOne({ - where: { id: _profileId } - }); - if (_profile) { - const _null: any = null; - const before = null; - _profile.position = _null - _profile.posTypeId = _null - _profile.posLevelId = _null - await this.profileRepository.save(_profile); - setLogDataDiff(request, { before, after: _profile }); - } - } + // //เช็คถ้า revision ปัจจุบันให้ปั๊มที่ profile + // const chkRevision = await this.orgRevisionRepository.findOne({ + // where: { id: dataMaster.orgRevisionId }, + // }); + // if (chkRevision?.orgRevisionIsCurrent) { + // const _profile = await this.profileRepository.findOne({ + // where: { id: _profileId } + // }); + // if (_profile) { + // const _null: any = null; + // const before = null; + // _profile.position = _null + // _profile.posTypeId = _null + // _profile.posLevelId = _null + // await this.profileRepository.save(_profile); + // setLogDataDiff(request, { before, after: _profile }); + // } + // } return new HttpSuccess(); } @@ -3703,11 +3717,12 @@ export class PositionController extends Controller { */ @Post("dna") async dna( - @Body() requestBody: { - draftRevisionId?: string | null; //ใช้เฉพาะเคสสืบทอดจากโครงสร้างปัจจุบัน - draftPositionId: string; //ในเคสสืบทอดจากโครงสร้างปัจจุบันจะ "ตำแหน่งที่แบบร่าง" จะเป็น "ตำแหน่งปัจจุบัน" ใช้ draftRevisionId เพื่อเช็คว่าเป็นโครงสร้างปัจจุบันไหม + @Body() + requestBody: { + draftRevisionId?: string | null; //ใช้เฉพาะเคสสืบทอดจากโครงสร้างปัจจุบัน + draftPositionId: string; //ในเคสสืบทอดจากโครงสร้างปัจจุบันจะ "ตำแหน่งที่แบบร่าง" จะเป็น "ตำแหน่งปัจจุบัน" ใช้ draftRevisionId เพื่อเช็คว่าเป็นโครงสร้างปัจจุบันไหม publishRevisionId?: string | null; //ใช้เฉพาะเคสสืบทอดจากโครงสร้างปัจจุบัน - publishPositionId: string //ในเคสสืบทอดจากโครงสร้างปัจจุบันจะ "ตำแหน่งปัจจุบัน" จะเป็น "ตำแหน่งที่ต้องการจะสืบทอด" ตาม publishRevisionId ที่ส่งมา + publishPositionId: string; //ในเคสสืบทอดจากโครงสร้างปัจจุบันจะ "ตำแหน่งปัจจุบัน" จะเป็น "ตำแหน่งที่ต้องการจะสืบทอด" ตาม publishRevisionId ที่ส่งมา }, @Request() request: RequestWithUser, ) { @@ -3732,11 +3747,12 @@ export class PositionController extends Controller { // ถ้ามี publishPositionId ค่อยเพิ่มเข้าไป ***อันนี้อาจไม่ต้องทำเพราะใช้ publishPositionId where ได้โดยตรง (!!รอดูหน้าบ้าน!!)*** if (requestBody.publishRevisionId) { whereConditionPub.id = requestBody.publishPositionId; - whereConditionPub.orgRevisionId = requestBody.publishRevisionId + whereConditionPub.orgRevisionId = requestBody.publishRevisionId; } else { whereConditionPub.id = requestBody.publishPositionId; } - const dataPublish = await this.posMasterRepository.findOne({ //เคสโครงสร้างปัจจุบัน: ตัวที่ต้องการจะสืบทอด + const dataPublish = await this.posMasterRepository.findOne({ + //เคสโครงสร้างปัจจุบัน: ตัวที่ต้องการจะสืบทอด where: whereConditionPub, }); if (!dataPublish) { @@ -3747,11 +3763,12 @@ export class PositionController extends Controller { // ถ้ามี publishPositionId ค่อยเพิ่มเข้าไป ***อันนี้อาจไม่ต้องทำเพราะใช้ draftPositionId where ได้โดยตรง (!!รอดูหน้าบ้าน!!)*** if (requestBody.draftRevisionId) { whereConditionDraft.id = requestBody.draftPositionId; - whereConditionDraft.orgRevisionId = requestBody.draftRevisionId + whereConditionDraft.orgRevisionId = requestBody.draftRevisionId; } else { whereConditionDraft.id = requestBody.draftPositionId; } - const dataDraft = await this.posMasterRepository.findOne({ //เคสโครงสร้างปัจจุบัน: ตัวปัจจุบัน + const dataDraft = await this.posMasterRepository.findOne({ + //เคสโครงสร้างปัจจุบัน: ตัวปัจจุบัน where: whereConditionDraft, }); if (!dataDraft) {