From 6f57be2fe927638db68675378686b62b03ca82f3 Mon Sep 17 00:00:00 2001 From: AdisakKanthawilang Date: Thu, 21 Aug 2025 18:25:24 +0700 Subject: [PATCH] validate draft and publish --- src/controllers/OrganizationController.ts | 48 +++++++++++------------ src/controllers/PositionController.ts | 48 +++++++++++------------ src/entities/PosMaster.ts | 3 ++ src/interfaces/utils.ts | 26 ++++++------ src/services/rabbitmq.ts | 18 ++++++++- 5 files changed, 80 insertions(+), 63 deletions(-) diff --git a/src/controllers/OrganizationController.ts b/src/controllers/OrganizationController.ts index ec39bc53..c4c5740f 100644 --- a/src/controllers/OrganizationController.ts +++ b/src/controllers/OrganizationController.ts @@ -125,19 +125,19 @@ export class OrganizationController extends Controller { ) { try { // CheckQueueInProgress - // const [isBusyDraft, isBusyPublish] = await Promise.all([ - // checkQueueInProgress(`${process.env.AMQ_QUEUE_ORG_DRAFT}`), - // checkQueueInProgress(`${process.env.AMQ_QUEUE_ORG}`), - // ]); + const [isBusyDraft, isBusyPublish] = await Promise.all([ + checkQueueInProgress(`${process.env.AMQ_QUEUE_ORG_DRAFT}`), + checkQueueInProgress(`${process.env.AMQ_QUEUE_ORG}`), + ]); // console.log("✅ ตรวจสอบแล้ว Draft Busy:", isBusyDraft); // console.log("✅ ตรวจสอบแล้ว Publish Busy:", isBusyPublish); - // if (isBusyDraft || isBusyPublish) { - // console.log("🚫 พบว่ามีงานอยู่ในคิว — error") - // throw new HttpError( - // HttpStatusCode.CONFLICT, - // "ไม่สามารถดำเนินการได้ หากกำลังเผยแพร่หรือสร้างแบบร่างโครงสร้างหน่วยงาน", - // ); - // } + if (isBusyDraft || isBusyPublish) { + // console.log("🚫 พบว่ามีงานอยู่ในคิว — error") + throw new HttpError( + HttpStatusCode.CONFLICT, + "ไม่สามารถดำเนินการได้ หากกำลังเผยแพร่หรือสร้างแบบร่างโครงสร้างหน่วยงาน", + ); + } //new main revision const before = null; const revision = Object.assign(new OrgRevision(), requestBody) as OrgRevision; @@ -3209,19 +3209,19 @@ export class OrganizationController extends Controller { try { // CheckQueueInProgress // console.log("🚀 ตรวจสอบว่ามีงานอยู่ในคิว"); - // const [isBusyDraft, isBusyPublish] = await Promise.all([ - // checkQueueInProgress(`${process.env.AMQ_QUEUE_ORG_DRAFT}`), - // checkQueueInProgress(`${process.env.AMQ_QUEUE_ORG}`), - // ]); - // console.log("✅ ตรวจสอบแล้ว Draft Busy:", isBusyDraft); - // console.log("✅ ตรวจสอบแล้ว Publish Busy:", isBusyPublish); - // if (isBusyDraft || isBusyPublish) { - // console.log("🚫 พบว่ามีงานอยู่ในคิว — error") - // throw new HttpError( - // HttpStatusCode.CONFLICT, - // "ไม่สามารถดำเนินการได้ หากกำลังเผยแพร่หรือสร้างแบบร่างโครงสร้างหน่วยงาน", - // ); - // } + const [isBusyDraft, isBusyPublish] = await Promise.all([ + checkQueueInProgress(`${process.env.AMQ_QUEUE_ORG_DRAFT}`), + checkQueueInProgress(`${process.env.AMQ_QUEUE_ORG}`), + ]); + // console.log("✅ ตรวจสอบแล้ว Draft Busy:", isBusyDraft); + // console.log("✅ ตรวจสอบแล้ว Publish Busy:", isBusyPublish); + if (isBusyDraft || isBusyPublish) { + // console.log("🚫 พบว่ามีงานอยู่ในคิว — error") + throw new HttpError( + HttpStatusCode.CONFLICT, + "ไม่สามารถดำเนินการได้ หากกำลังเผยแพร่หรือสร้างแบบร่างโครงสร้างหน่วยงาน", + ); + } const today = new Date(); today.setHours(0, 0, 0, 0); // Set time to the beginning of the day diff --git a/src/controllers/PositionController.ts b/src/controllers/PositionController.ts index 4217f351..618ed125 100644 --- a/src/controllers/PositionController.ts +++ b/src/controllers/PositionController.ts @@ -1391,32 +1391,32 @@ export class PositionController extends Controller { posMaster.lastUpdatedAt = new Date(); await this.posMasterRepository.save(posMaster, { data: request }); setLogDataDiff(request, { before, after: posMaster }); - await this.positionRepository.delete({ posMasterId: posMaster.id }); + 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; + return match + }else{ + 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 + } + }), + ); + await this.positionRepository.save(result, { data: request }); await Promise.all( - requestBody.positions.map(async (x: any) => { - const position = Object.assign(new Position()); - position.positionName = x.posDictName; - position.positionField = x.posDictField; - position.posTypeId = x.posTypeId == "" ? null : x.posTypeId; - position.posLevelId = x.posLevelId == "" ? null : x.posLevelId; - position.posExecutiveId = x.posExecutiveId == "" ? null : x.posExecutiveId; - position.positionExecutiveField = x.posDictExecutiveField; - position.positionArea = x.posDictArea; - position.isSpecial = x.isSpecial; - position.isOfficer = x.isOfficer; - position.isStaff = x.isStaff; - position.isDirector = x.isDirector; - position.positionSign = x.positionSign; - position.positionIsSelected = x.positionIsSelected; - position.posMasterId = posMaster.id; - position.createdUserId = request.user.sub; - position.createdFullName = request.user.name; - position.lastUpdateUserId = request.user.sub; - position.lastUpdateFullName = request.user.name; - position.createdAt = new Date(); - position.lastUpdatedAt = new Date(); - await this.positionRepository.save(position, { data: request }); + 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); + } }), ); return new HttpSuccess(posMaster.id); diff --git a/src/entities/PosMaster.ts b/src/entities/PosMaster.ts index 59018baa..bff30aae 100644 --- a/src/entities/PosMaster.ts +++ b/src/entities/PosMaster.ts @@ -266,6 +266,9 @@ export class PosMaster extends EntityBase { } export class CreatePosMaster { + @Column() + id: string | null; + @Column() posMasterNoPrefix: string | null; diff --git a/src/interfaces/utils.ts b/src/interfaces/utils.ts index 244da06c..69b31e58 100644 --- a/src/interfaces/utils.ts +++ b/src/interfaces/utils.ts @@ -534,23 +534,23 @@ export function editLogSequence(req: RequestWithUser, index: number, data: LogSe } export async function checkQueueInProgress(queueName: string) { - // const axios = require('axios'); - // console.log("Checking queue in progress"); - // const res = await axios.get(`${process.env.RABBIT_API_URL}/api/queues/%2F/${queueName}`, { - // auth: { username: process.env.RABBIT_USER , password: process.env.RABBIT_PASS }, - // }); + const axios = require('axios'); + // console.log("Checking queue in progress"); + const res = await axios.get(`${process.env.RABBIT_API_URL}/api/queues/%2F/${queueName}`, { + auth: { username: process.env.RABBIT_USER , password: process.env.RABBIT_PASS }, + }); - // const q = res.data; + const q = res.data; - // console.log(`Queue "${queueName}" has:`); - // console.log(` - ${q.messages_ready} messages ready`); - // console.log(` - ${q.messages_unacknowledged} messages in progress (unacked)`); + // console.log(`Queue "${queueName}" has:`); + // console.log(` - ${q.messages_ready} messages ready`); + // console.log(` - ${q.messages_unacknowledged} messages in progress (unacked)`); - // if (q.messages_unacknowledged > 0) { - // return true; - // } + if (q.messages_unacknowledged > 0) { + return true; + } - // return false; + return false; } export function chunkArray(array: any, size: number) { diff --git a/src/services/rabbitmq.ts b/src/services/rabbitmq.ts index af8f4d2d..7001d710 100644 --- a/src/services/rabbitmq.ts +++ b/src/services/rabbitmq.ts @@ -622,14 +622,28 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise { // requestBody.typeDraft.toUpperCase() == "ORG_POSITION_ROLE" || // requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON_ROLE" // ) { + // _orgemployeePosMaster = orgemployeePosMaster.map((x) => ({ + // ...x, + // ancestorDNA: + // x.ancestorDNA == null || x.ancestorDNA == "00000000-0000-0000-0000-000000000000" + // ? x.id + // : x.ancestorDNA, + // })); + // await repoEmployeePosmaster.save(_orgemployeePosMaster); + const validProfileIds = new Set( + (await repoProfileEmployee.find({ select: ["id"] })).map(p => p.id) + ); + _orgemployeePosMaster = orgemployeePosMaster.map((x) => ({ ...x, + current_holderId: x.current_holderId && validProfileIds.has(x.current_holderId) + ? x.current_holderId + : null, ancestorDNA: - x.ancestorDNA == null || x.ancestorDNA == "00000000-0000-0000-0000-000000000000" + !x.ancestorDNA || x.ancestorDNA === "00000000-0000-0000-0000-000000000000" ? x.id : x.ancestorDNA, })); - // await repoEmployeePosmaster.save(_orgemployeePosMaster); await repoEmployeePosmaster .createQueryBuilder() .insert()