แก้แบบร่าง

This commit is contained in:
mamoss 2025-03-18 13:46:11 +07:00
parent 9b4db9a66f
commit ed1cbea7c6

View file

@ -30,9 +30,20 @@ export let sendToQueueOrg: (payload: any) => void;
export let sendToQueueOrgDraft: (payload: any) => void; export let sendToQueueOrgDraft: (payload: any) => void;
export async function init() { export async function init() {
//----> (1) Producer //----> (1) Producer
if (!process.env.AMQ_URL || !process.env.AMQ_QUEUE || !process.env.AMQ_QUEUE_ORG || !process.env.AMQ_QUEUE_ORG_DRAFT) return; if (
!process.env.AMQ_URL ||
!process.env.AMQ_QUEUE ||
!process.env.AMQ_QUEUE_ORG ||
!process.env.AMQ_QUEUE_ORG_DRAFT
)
return;
const { AMQ_URL: url, AMQ_QUEUE: queue, AMQ_QUEUE_ORG: queue_org, AMQ_QUEUE_ORG_DRAFT: queue_org_draft } = process.env; //----> (1.2) get url and queue from .env const {
AMQ_URL: url,
AMQ_QUEUE: queue,
AMQ_QUEUE_ORG: queue_org,
AMQ_QUEUE_ORG_DRAFT: queue_org_draft,
} = process.env; //----> (1.2) get url and queue from .env
const connection = await amqp.connect(url); //----> (1.3) set up url with amqp protocol const connection = await amqp.connect(url); //----> (1.3) set up url with amqp protocol
@ -43,9 +54,9 @@ export async function init() {
console.log(channel ? "[AMQ] Create channel success" : "[AMQ] Create channel failed"); console.log(channel ? "[AMQ] Create channel success" : "[AMQ] Create channel failed");
channel.assertQueue(queue, { durable: true }), //----> (1.5) assert queue and set durable (if "true" save to disk on RabbitMQ) channel.assertQueue(queue, { durable: true }), //----> (1.5) assert queue and set durable (if "true" save to disk on RabbitMQ)
channel.assertQueue(queue_org, { durable: true }), channel.assertQueue(queue_org, { durable: true }),
channel.assertQueue(queue_org_draft, { durable: true }), channel.assertQueue(queue_org_draft, { durable: true }),
channel.prefetch(1); channel.prefetch(1);
sendToQueue = (payload: any, persistent = true) => { sendToQueue = (payload: any, persistent = true) => {
//----> (2) sendQueue To RabbitMQ and set persistent (if "true" redo the failed queue when server run again) //----> (2) sendQueue To RabbitMQ and set persistent (if "true" redo the failed queue when server run again)
@ -64,7 +75,7 @@ export async function init() {
console.log("[AMQ] Listening for message..."); console.log("[AMQ] Listening for message...");
createConsumer(queue, channel, handler), //----> (3) Process Consumer createConsumer(queue, channel, handler), //----> (3) Process Consumer
createConsumer(queue_org, channel, handler_org); createConsumer(queue_org, channel, handler_org);
createConsumer(queue_org_draft, channel, handler_org_draft); createConsumer(queue_org_draft, channel, handler_org_draft);
// createConsumer(queue2, channel, handler2); // createConsumer(queue2, channel, handler2);
} }
@ -1090,144 +1101,326 @@ async function handler_org_draft(msg: amqp.ConsumeMessage): Promise<boolean> {
const child4Repository = AppDataSource.getRepository(OrgChild4); const child4Repository = AppDataSource.getRepository(OrgChild4);
try { try {
//cone tree //cone tree
if ( if (
requestBody.typeDraft.toUpperCase() == "ORG" || requestBody.typeDraft.toUpperCase() == "ORG" ||
requestBody.typeDraft.toUpperCase() == "ORG_POSITION" || requestBody.typeDraft.toUpperCase() == "ORG_POSITION" ||
requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON" || requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON" ||
requestBody.typeDraft.toUpperCase() == "ORG_POSITION_ROLE" || requestBody.typeDraft.toUpperCase() == "ORG_POSITION_ROLE" ||
requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON_ROLE" requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON_ROLE"
) { ) {
//cone by revisionId //cone by revisionId
if (requestBody.orgRevisionId == null) if (requestBody.orgRevisionId == null)
throw new HttpError(HttpStatusCode.NOT_FOUND, "not found."); throw new HttpError(HttpStatusCode.NOT_FOUND, "not found.");
const _revision = await orgRevisionRepository.findOne({ const _revision = await orgRevisionRepository.findOne({
where: { id: requestBody.orgRevisionId }, where: { id: requestBody.orgRevisionId },
}); });
if (!_revision) throw new HttpError(HttpStatusCode.NOT_FOUND, "not found."); if (!_revision) throw new HttpError(HttpStatusCode.NOT_FOUND, "not found.");
//หา dna tree ถ้าไม่มีให้เอาตัวเองเป็น dna //หา dna tree ถ้าไม่มีให้เอาตัวเองเป็น dna
const orgRoot = await orgRootRepository.find({ const orgRoot = await orgRootRepository.find({
where: { orgRevisionId: requestBody.orgRevisionId }, where: { orgRevisionId: requestBody.orgRevisionId },
order:{orgRootOrder:"ASC"} order: { orgRootOrder: "ASC" },
}); });
let _orgRoot: any = orgRoot.map((x) => ({ let _orgRoot: any = orgRoot.map((x) => ({
...x,
ancestorDNA:
x.ancestorDNA == null || x.ancestorDNA == "00000000-0000-0000-0000-000000000000"
? x.id
: x.ancestorDNA,
}));
await orgRootRepository.save(_orgRoot);
const orgChild1 = await child1Repository.find({
where: { orgRevisionId: requestBody.orgRevisionId },
order: { orgChild1Order: "ASC" },
});
let _orgChild1: any = orgChild1.map((x) => ({
...x,
ancestorDNA:
x.ancestorDNA == null || x.ancestorDNA == "00000000-0000-0000-0000-000000000000"
? x.id
: x.ancestorDNA,
}));
await child1Repository.save(_orgChild1);
const orgChild2 = await child2Repository.find({
where: { orgRevisionId: requestBody.orgRevisionId },
order: { orgChild2Order: "ASC" },
});
let _orgChild2: any = orgChild2.map((x) => ({
...x,
ancestorDNA:
x.ancestorDNA == null || x.ancestorDNA == "00000000-0000-0000-0000-000000000000"
? x.id
: x.ancestorDNA,
}));
await child2Repository.save(_orgChild2);
const orgChild3 = await child3Repository.find({
where: { orgRevisionId: requestBody.orgRevisionId },
order: { orgChild3Order: "ASC" },
});
let _orgChild3: any = orgChild3.map((x) => ({
...x,
ancestorDNA:
x.ancestorDNA == null || x.ancestorDNA == "00000000-0000-0000-0000-000000000000"
? x.id
: x.ancestorDNA,
}));
await child3Repository.save(_orgChild3);
const orgChild4 = await child4Repository.find({
where: { orgRevisionId: requestBody.orgRevisionId },
order: { orgChild4Order: "ASC" },
});
let _orgChild4: any = orgChild4.map((x) => ({
...x,
ancestorDNA:
x.ancestorDNA == null || x.ancestorDNA == "00000000-0000-0000-0000-000000000000"
? x.id
: x.ancestorDNA,
}));
await child4Repository.save(_orgChild4);
//หา dna posmaster ถ้าไม่มีให้เอาตัวเองเป็น dna
const orgPosMaster = await posMasterRepository.find({
where: { orgRevisionId: requestBody.orgRevisionId },
relations: ["positions", "posMasterAssigns"],
});
let _orgPosMaster: PosMaster[] = [];
if (
requestBody.typeDraft.toUpperCase() == "ORG_POSITION" ||
requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON" ||
requestBody.typeDraft.toUpperCase() == "ORG_POSITION_ROLE" ||
requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON_ROLE"
) {
_orgPosMaster = orgPosMaster
.filter(
(x) => x.ancestorDNA == null || x.ancestorDNA == "00000000-0000-0000-0000-000000000000",
)
.map((x) => ({
...x, ...x,
ancestorDNA: ancestorDNA:
x.ancestorDNA == null || x.ancestorDNA == "00000000-0000-0000-0000-000000000000" x.ancestorDNA == null || x.ancestorDNA == "00000000-0000-0000-0000-000000000000"
? x.id ? x.id
: x.ancestorDNA, : x.ancestorDNA,
})); }));
await orgRootRepository.save(_orgRoot); await posMasterRepository.save(_orgPosMaster);
}
// Create org
for await (const x0 of _orgRoot) {
var dataId = x0.id;
console.log(`ch0 ${x0.orgRootOrder}`);
console.log(dataId);
delete x0.id;
const data = Object.assign(new OrgRoot(), x0);
data.orgRevisionId = revision.id;
data.createdUserId = request.sub;
data.createdFullName = request.name;
data.createdAt = new Date();
data.lastUpdateUserId = request.sub;
data.lastUpdateFullName = request.name;
data.lastUpdatedAt = new Date();
await orgRootRepository.save(data);
const orgChild1 = await child1Repository.find({ const orgTypes = new Set([
where: { orgRevisionId: requestBody.orgRevisionId }, "ORG_POSITION",
order:{orgChild1Order:"ASC"} "ORG_POSITION_PERSON",
}); "ORG_POSITION_ROLE",
let _orgChild1: any = orgChild1.map((x) => ({ "ORG_POSITION_PERSON_ROLE",
...x, ]);
ancestorDNA:
x.ancestorDNA == null || x.ancestorDNA == "00000000-0000-0000-0000-000000000000"
? x.id
: x.ancestorDNA,
}));
await child1Repository.save(_orgChild1);
const orgChild2 = await child2Repository.find({ if (orgTypes.has(requestBody.typeDraft.toUpperCase())) {
where: { orgRevisionId: requestBody.orgRevisionId }, // Create posMaster
order:{orgChild2Order:"ASC"} for await (const item of orgPosMaster.filter(
}); (x: PosMaster) => x.orgRootId == dataId && x.orgChild1Id == null,
let _orgChild2: any = orgChild2.map((x) => ({ ) as any) {
...x, delete item.id;
ancestorDNA: const posMaster: any = Object.assign(new PosMaster(), item);
x.ancestorDNA == null || x.ancestorDNA == "00000000-0000-0000-0000-000000000000" posMaster.positions = [];
? x.id
: x.ancestorDNA,
}));
await child2Repository.save(_orgChild2);
const orgChild3 = await child3Repository.find({ if (
where: { orgRevisionId: requestBody.orgRevisionId }, ["ORG_POSITION_PERSON", "ORG_POSITION_PERSON_ROLE"].includes(
order:{orgChild3Order:"ASC"} requestBody.typeDraft.toUpperCase(),
});
let _orgChild3: any = orgChild3.map((x) => ({
...x,
ancestorDNA:
x.ancestorDNA == null || x.ancestorDNA == "00000000-0000-0000-0000-000000000000"
? x.id
: x.ancestorDNA,
}));
await child3Repository.save(_orgChild3);
const orgChild4 = await child4Repository.find({
where: { orgRevisionId: requestBody.orgRevisionId },
order:{orgChild4Order:"ASC"}
});
let _orgChild4: any = orgChild4.map((x) => ({
...x,
ancestorDNA:
x.ancestorDNA == null || x.ancestorDNA == "00000000-0000-0000-0000-000000000000"
? x.id
: x.ancestorDNA,
}));
await child4Repository.save(_orgChild4);
//หา dna posmaster ถ้าไม่มีให้เอาตัวเองเป็น dna
const orgPosMaster = await posMasterRepository.find({
where: { orgRevisionId: requestBody.orgRevisionId },
relations: ["positions", "posMasterAssigns"],
});
let _orgPosMaster: PosMaster[] = [];
if (
requestBody.typeDraft.toUpperCase() == "ORG_POSITION" ||
requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON" ||
requestBody.typeDraft.toUpperCase() == "ORG_POSITION_ROLE" ||
requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON_ROLE"
) {
_orgPosMaster = orgPosMaster
.filter(
(x) => x.ancestorDNA == null || x.ancestorDNA == "00000000-0000-0000-0000-000000000000",
) )
.map((x) => ({ ) {
...x, posMaster.next_holderId = item.current_holderId;
ancestorDNA: } else {
x.ancestorDNA == null || x.ancestorDNA == "00000000-0000-0000-0000-000000000000" posMaster.next_holderId = null;
? x.id posMaster.isSit = false;
: x.ancestorDNA, }
}));
await posMasterRepository.save(_orgPosMaster);
}
// Create org
for await (const x0 of _orgRoot) {
var dataId = x0.id;
console.log(`ch0 ${x0.orgRootOrder}`)
console.log(dataId)
delete x0.id;
const data = Object.assign(new OrgRoot(), x0);
data.orgRevisionId = revision.id;
data.createdUserId = request.sub;
data.createdFullName = request.name;
data.createdAt = new Date();
data.lastUpdateUserId = request.sub;
data.lastUpdateFullName = request.name;
data.lastUpdatedAt = new Date();
await orgRootRepository.save(data);
const orgTypes = new Set([ if (
"ORG_POSITION", ["ORG_POSITION_ROLE", "ORG_POSITION_PERSON_ROLE"].includes(
"ORG_POSITION_PERSON", requestBody.typeDraft.toUpperCase(),
"ORG_POSITION_ROLE", )
"ORG_POSITION_PERSON_ROLE", ) {
]); posMaster.authRoleId = item.authRoleId;
} else {
posMaster.authRoleId = null;
}
posMaster.current_holderId = null;
posMaster.orgRevisionId = revision.id;
posMaster.orgRootId = data.id;
posMaster.createdUserId = request.sub;
posMaster.createdFullName = request.name;
posMaster.createdAt = new Date();
posMaster.lastUpdateUserId = request.sub;
posMaster.lastUpdateFullName = request.name;
posMaster.lastUpdatedAt = new Date();
await posMasterRepository.save(posMaster);
// Copy assignments
item.posMasterAssigns = item.posMasterAssigns.map(
({ id, ...rest }: PosMasterAssign) => ({
...rest,
posMasterId: posMaster.id,
}),
);
posMaster.posMasterAssigns = item.posMasterAssigns;
// Create positions
for await (const pos of item.positions) {
delete pos.id;
const position = Object.assign(new Position(), pos);
position.posMasterId = posMaster.id;
if (
["ORG_POSITION", "ORG_POSITION_ROLE"].includes(requestBody.typeDraft.toUpperCase())
) {
position.positionIsSelected = false;
}
position.createdUserId = request.sub;
position.createdFullName = request.name;
position.createdAt = new Date();
position.lastUpdateUserId = request.sub;
position.lastUpdateFullName = request.name;
position.lastUpdatedAt = new Date();
await positionRepository.save(position);
}
}
}
// Create orgChild1
for await (const x1 of _orgChild1.filter((x: OrgChild1) => x.orgRootId == dataId)) {
var data1Id = x1.id;
console.log(`ch1 ${x1.orgChild1Order}`);
console.log(data1Id);
delete x1.id;
const data1 = Object.assign(new OrgChild1(), x1);
data1.orgRootId = data.id;
data1.orgRevisionId = revision.id;
data1.createdUserId = request.sub;
data1.createdFullName = request.name;
data1.createdAt = new Date();
data1.lastUpdateUserId = request.sub;
data1.lastUpdateFullName = request.name;
data1.lastUpdatedAt = new Date();
await child1Repository.save(data1);
if (orgTypes.has(requestBody.typeDraft.toUpperCase())) {
// Create posMaster
for await (const item of orgPosMaster.filter(
(x: PosMaster) => x.orgChild1Id == data1Id && x.orgChild2Id == null,
) as any) {
delete item.id;
const posMaster = Object.assign(new PosMaster(), item);
posMaster.positions = [];
if (
["ORG_POSITION_PERSON", "ORG_POSITION_PERSON_ROLE"].includes(
requestBody.typeDraft.toUpperCase(),
)
) {
posMaster.next_holderId = item.current_holderId;
} else {
posMaster.next_holderId = null;
posMaster.isSit = false;
}
if (
["ORG_POSITION_ROLE", "ORG_POSITION_PERSON_ROLE"].includes(
requestBody.typeDraft.toUpperCase(),
)
) {
posMaster.authRoleId = item.authRoleId;
} else {
posMaster.authRoleId = null;
}
posMaster.current_holderId = null;
posMaster.orgRevisionId = revision.id;
posMaster.orgRootId = data.id;
posMaster.orgChild1Id = data1.id;
posMaster.createdUserId = request.sub;
posMaster.createdFullName = request.name;
posMaster.createdAt = new Date();
posMaster.lastUpdateUserId = request.sub;
posMaster.lastUpdateFullName = request.name;
posMaster.lastUpdatedAt = new Date();
await posMasterRepository.save(posMaster);
// Copy assignments
item.posMasterAssigns = item.posMasterAssigns.map(
({ id, ...rest }: PosMasterAssign) => ({
...rest,
posMasterId: posMaster.id,
}),
);
posMaster.posMasterAssigns = item.posMasterAssigns;
// Create positions
for await (const pos of item.positions) {
delete pos.id;
const position = Object.assign(new Position(), pos);
position.posMasterId = posMaster.id;
if (
["ORG_POSITION", "ORG_POSITION_ROLE"].includes(
requestBody.typeDraft.toUpperCase(),
)
) {
position.positionIsSelected = false;
}
position.createdUserId = request.sub;
position.createdFullName = request.name;
position.createdAt = new Date();
position.lastUpdateUserId = request.sub;
position.lastUpdateFullName = request.name;
position.lastUpdatedAt = new Date();
await positionRepository.save(position);
}
}
}
for await (const x2 of _orgChild2.filter((x: OrgChild2) => x.orgChild1Id == data1Id)) {
var data2Id = x2.id;
console.log(`ch2 ${x2.orgChild2Order}`);
console.log(data2Id);
delete x2.id;
const data2 = Object.assign(new OrgChild2(), x2);
data2.orgChild1Id = data1.id;
data2.orgRootId = data.id;
data2.orgRevisionId = revision.id;
data2.createdUserId = request.sub;
data2.createdFullName = request.name;
data2.createdAt = new Date();
data2.lastUpdateUserId = request.sub;
data2.lastUpdateFullName = request.name;
data2.lastUpdatedAt = new Date();
await child2Repository.save(data2);
if (orgTypes.has(requestBody.typeDraft.toUpperCase())) { if (orgTypes.has(requestBody.typeDraft.toUpperCase())) {
// Create posMaster // Create posMaster
for await (const item of orgPosMaster.filter( for await (const item of orgPosMaster.filter(
(x: PosMaster) => x.orgRootId == dataId && x.orgChild1Id == null, (x: PosMaster) => x.orgChild2Id == data2Id && x.orgChild3Id == null,
) as any) { ) as any) {
delete item.id; delete item.id;
const posMaster: any = Object.assign(new PosMaster(), item); const posMaster = Object.assign(new PosMaster(), item);
posMaster.positions = []; posMaster.positions = [];
if ( if (
@ -1254,6 +1447,8 @@ async function handler_org_draft(msg: amqp.ConsumeMessage): Promise<boolean> {
posMaster.current_holderId = null; posMaster.current_holderId = null;
posMaster.orgRevisionId = revision.id; posMaster.orgRevisionId = revision.id;
posMaster.orgRootId = data.id; posMaster.orgRootId = data.id;
posMaster.orgChild1Id = data1.id;
posMaster.orgChild2Id = data2.id;
posMaster.createdUserId = request.sub; posMaster.createdUserId = request.sub;
posMaster.createdFullName = request.name; posMaster.createdFullName = request.name;
posMaster.createdAt = new Date(); posMaster.createdAt = new Date();
@ -1263,26 +1458,25 @@ async function handler_org_draft(msg: amqp.ConsumeMessage): Promise<boolean> {
await posMasterRepository.save(posMaster); await posMasterRepository.save(posMaster);
// Copy assignments // Copy assignments
item.posMasterAssigns = item.posMasterAssigns.map( posMaster.posMasterAssigns = item.posMasterAssigns.map(
({ id, ...rest }: PosMasterAssign) => ({ ({ id, ...rest }: PosMasterAssign) => ({
...rest, ...rest,
posMasterId: posMaster.id, posMasterId: posMaster.id,
}), }),
); );
posMaster.posMasterAssigns = item.posMasterAssigns;
// Create positions // Create positions
for await (const pos of item.positions) { for await (const pos of item.positions) {
delete pos.id; delete pos.id;
const position = Object.assign(new Position(), pos); const position = Object.assign(new Position(), pos);
position.posMasterId = posMaster.id; position.posMasterId = posMaster.id;
if ( if (
["ORG_POSITION", "ORG_POSITION_ROLE"].includes(requestBody.typeDraft.toUpperCase()) ["ORG_POSITION", "ORG_POSITION_ROLE"].includes(
requestBody.typeDraft.toUpperCase(),
)
) { ) {
position.positionIsSelected = false; position.positionIsSelected = false;
} }
position.createdUserId = request.sub; position.createdUserId = request.sub;
position.createdFullName = request.name; position.createdFullName = request.name;
position.createdAt = new Date(); position.createdAt = new Date();
@ -1294,27 +1488,29 @@ async function handler_org_draft(msg: amqp.ConsumeMessage): Promise<boolean> {
} }
} }
// Create orgChild1 // Create org
for await (const x1 of _orgChild1.filter((x: OrgChild1) => x.orgRootId == dataId)) { for await (const x3 of _orgChild3.filter((x: OrgChild3) => x.orgChild2Id == data2Id)) {
var data1Id = x1.id; var data3Id = x3.id;
console.log(`ch1 ${x1.orgChild1Order}`) console.log(`ch3 ${x3.orgChild3Order}`);
console.log(data1Id) console.log(data3Id);
delete x1.id; delete x3.id;
const data1 = Object.assign(new OrgChild1(), x1); const data3 = Object.assign(new OrgChild3(), x3);
data1.orgRootId = data.id; data3.orgChild2Id = data2.id;
data1.orgRevisionId = revision.id; data3.orgChild1Id = data1.id;
data1.createdUserId = request.sub; data3.orgRootId = data.id;
data1.createdFullName = request.name; data3.orgRevisionId = revision.id;
data1.createdAt = new Date(); data3.createdUserId = request.sub;
data1.lastUpdateUserId = request.sub; data3.createdFullName = request.name;
data1.lastUpdateFullName = request.name; data3.createdAt = new Date();
data1.lastUpdatedAt = new Date(); data3.lastUpdateUserId = request.sub;
await child1Repository.save(data1); data3.lastUpdateFullName = request.name;
data3.lastUpdatedAt = new Date();
await child3Repository.save(data3);
if (orgTypes.has(requestBody.typeDraft.toUpperCase())) { if (orgTypes.has(requestBody.typeDraft.toUpperCase())) {
// Create posMaster // Create posMaster
for await (const item of orgPosMaster.filter( for await (const item of orgPosMaster.filter(
(x: PosMaster) => x.orgChild1Id == data1Id && x.orgChild2Id == null, (x: PosMaster) => x.orgChild3Id == data3Id && x.orgChild4Id == null,
) as any) { ) as any) {
delete item.id; delete item.id;
const posMaster = Object.assign(new PosMaster(), item); const posMaster = Object.assign(new PosMaster(), item);
@ -1345,6 +1541,8 @@ async function handler_org_draft(msg: amqp.ConsumeMessage): Promise<boolean> {
posMaster.orgRevisionId = revision.id; posMaster.orgRevisionId = revision.id;
posMaster.orgRootId = data.id; posMaster.orgRootId = data.id;
posMaster.orgChild1Id = data1.id; posMaster.orgChild1Id = data1.id;
posMaster.orgChild2Id = data2.id;
posMaster.orgChild3Id = data3.id;
posMaster.createdUserId = request.sub; posMaster.createdUserId = request.sub;
posMaster.createdFullName = request.name; posMaster.createdFullName = request.name;
posMaster.createdAt = new Date(); posMaster.createdAt = new Date();
@ -1354,20 +1552,18 @@ async function handler_org_draft(msg: amqp.ConsumeMessage): Promise<boolean> {
await posMasterRepository.save(posMaster); await posMasterRepository.save(posMaster);
// Copy assignments // Copy assignments
item.posMasterAssigns = item.posMasterAssigns.map( posMaster.posMasterAssigns = item.posMasterAssigns.map(
({ id, ...rest }: PosMasterAssign) => ({ ({ id, ...rest }: PosMasterAssign) => ({
...rest, ...rest,
posMasterId: posMaster.id, posMasterId: posMaster.id,
}), }),
); );
posMaster.posMasterAssigns = item.posMasterAssigns;
// Create positions // Create positions
for await (const pos of item.positions) { for await (const pos of item.positions) {
delete pos.id; delete pos.id;
const position = Object.assign(new Position(), pos); const position = Object.assign(new Position(), pos);
position.posMasterId = posMaster.id; position.posMasterId = posMaster.id;
if ( if (
["ORG_POSITION", "ORG_POSITION_ROLE"].includes( ["ORG_POSITION", "ORG_POSITION_ROLE"].includes(
requestBody.typeDraft.toUpperCase(), requestBody.typeDraft.toUpperCase(),
@ -1375,7 +1571,6 @@ async function handler_org_draft(msg: amqp.ConsumeMessage): Promise<boolean> {
) { ) {
position.positionIsSelected = false; position.positionIsSelected = false;
} }
position.createdUserId = request.sub; position.createdUserId = request.sub;
position.createdFullName = request.name; position.createdFullName = request.name;
position.createdAt = new Date(); position.createdAt = new Date();
@ -1386,27 +1581,32 @@ async function handler_org_draft(msg: amqp.ConsumeMessage): Promise<boolean> {
} }
} }
} }
for await (const x2 of _orgChild2.filter((x: OrgChild2) => x.orgChild1Id == data1Id)) { // Create org
var data2Id = x2.id; for await (const x4 of _orgChild4.filter(
console.log(`ch2 ${x2.orgChild2Order}`) (x: OrgChild4) => x.orgChild3Id == data3Id,
console.log(data2Id) )) {
delete x2.id; var data4Id = x4.id;
const data2 = Object.assign(new OrgChild2(), x2); console.log(`ch4 ${x4.orgChild4Order}`);
data2.orgChild1Id = data1.id; console.log(data4Id);
data2.orgRootId = data.id; delete x4.id;
data2.orgRevisionId = revision.id; const data4 = Object.assign(new OrgChild4(), x4);
data2.createdUserId = request.sub; data4.orgChild3Id = data3.id;
data2.createdFullName = request.name; data4.orgChild2Id = data2.id;
data2.createdAt = new Date(); data4.orgChild1Id = data1.id;
data2.lastUpdateUserId = request.sub; data4.orgRootId = data.id;
data2.lastUpdateFullName = request.name; data4.orgRevisionId = revision.id;
data2.lastUpdatedAt = new Date(); data4.createdUserId = request.sub;
await child2Repository.save(data2); data4.createdFullName = request.name;
data4.createdAt = new Date();
data4.lastUpdateUserId = request.sub;
data4.lastUpdateFullName = request.name;
data4.lastUpdatedAt = new Date();
await child4Repository.save(data4);
if (orgTypes.has(requestBody.typeDraft.toUpperCase())) { if (orgTypes.has(requestBody.typeDraft.toUpperCase())) {
// Create posMaster // Create posMaster
for await (const item of orgPosMaster.filter( for await (const item of orgPosMaster.filter(
(x: PosMaster) => x.orgChild2Id == data2Id && x.orgChild3Id == null, (x: PosMaster) => x.orgChild4Id == data4Id,
) as any) { ) as any) {
delete item.id; delete item.id;
const posMaster = Object.assign(new PosMaster(), item); const posMaster = Object.assign(new PosMaster(), item);
@ -1438,6 +1638,8 @@ async function handler_org_draft(msg: amqp.ConsumeMessage): Promise<boolean> {
posMaster.orgRootId = data.id; posMaster.orgRootId = data.id;
posMaster.orgChild1Id = data1.id; posMaster.orgChild1Id = data1.id;
posMaster.orgChild2Id = data2.id; posMaster.orgChild2Id = data2.id;
posMaster.orgChild3Id = data3.id;
posMaster.orgChild4Id = data4.id;
posMaster.createdUserId = request.sub; posMaster.createdUserId = request.sub;
posMaster.createdFullName = request.name; posMaster.createdFullName = request.name;
posMaster.createdAt = new Date(); posMaster.createdAt = new Date();
@ -1476,243 +1678,52 @@ async function handler_org_draft(msg: amqp.ConsumeMessage): Promise<boolean> {
} }
} }
} }
// Create org
for await (const x3 of _orgChild3.filter((x: OrgChild3) => x.orgChild2Id == data2Id)) {
var data3Id = x3.id;
console.log(`ch3 ${x3.orgChild3Order}`)
console.log(data3Id)
delete x3.id;
const data3 = Object.assign(new OrgChild3(), x3);
data3.orgChild2Id = data2.id;
data3.orgChild1Id = data1.id;
data3.orgRootId = data.id;
data3.orgRevisionId = revision.id;
data3.createdUserId = request.sub;
data3.createdFullName = request.name;
data3.createdAt = new Date();
data3.lastUpdateUserId = request.sub;
data3.lastUpdateFullName = request.name;
data3.lastUpdatedAt = new Date();
await child3Repository.save(data3);
if (orgTypes.has(requestBody.typeDraft.toUpperCase())) {
// Create posMaster
for await (const item of orgPosMaster.filter(
(x: PosMaster) => x.orgChild3Id == data3Id && x.orgChild4Id == null,
) as any) {
delete item.id;
const posMaster = Object.assign(new PosMaster(), item);
posMaster.positions = [];
if (
["ORG_POSITION_PERSON", "ORG_POSITION_PERSON_ROLE"].includes(
requestBody.typeDraft.toUpperCase(),
)
) {
posMaster.next_holderId = item.current_holderId;
} else {
posMaster.next_holderId = null;
posMaster.isSit = false;
}
if (
["ORG_POSITION_ROLE", "ORG_POSITION_PERSON_ROLE"].includes(
requestBody.typeDraft.toUpperCase(),
)
) {
posMaster.authRoleId = item.authRoleId;
} else {
posMaster.authRoleId = null;
}
posMaster.current_holderId = null;
posMaster.orgRevisionId = revision.id;
posMaster.orgRootId = data.id;
posMaster.orgChild1Id = data1.id;
posMaster.orgChild2Id = data2.id;
posMaster.orgChild3Id = data3.id;
posMaster.createdUserId = request.sub;
posMaster.createdFullName = request.name;
posMaster.createdAt = new Date();
posMaster.lastUpdateUserId = request.sub;
posMaster.lastUpdateFullName = request.name;
posMaster.lastUpdatedAt = new Date();
await posMasterRepository.save(posMaster);
// Copy assignments
posMaster.posMasterAssigns = item.posMasterAssigns.map(
({ id, ...rest }: PosMasterAssign) => ({
...rest,
posMasterId: posMaster.id,
}),
);
// Create positions
for await (const pos of item.positions) {
delete pos.id;
const position = Object.assign(new Position(), pos);
position.posMasterId = posMaster.id;
if (
["ORG_POSITION", "ORG_POSITION_ROLE"].includes(
requestBody.typeDraft.toUpperCase(),
)
) {
position.positionIsSelected = false;
}
position.createdUserId = request.sub;
position.createdFullName = request.name;
position.createdAt = new Date();
position.lastUpdateUserId = request.sub;
position.lastUpdateFullName = request.name;
position.lastUpdatedAt = new Date();
await positionRepository.save(position);
}
}
}
// Create org
for await (const x4 of _orgChild4.filter(
(x: OrgChild4) => x.orgChild3Id == data3Id,
)) {
var data4Id = x4.id;
console.log(`ch4 ${x4.orgChild4Order}`)
console.log(data4Id)
delete x4.id;
const data4 = Object.assign(new OrgChild4(), x4);
data4.orgChild3Id = data3.id;
data4.orgChild2Id = data2.id;
data4.orgChild1Id = data1.id;
data4.orgRootId = data.id;
data4.orgRevisionId = revision.id;
data4.createdUserId = request.sub;
data4.createdFullName = request.name;
data4.createdAt = new Date();
data4.lastUpdateUserId = request.sub;
data4.lastUpdateFullName = request.name;
data4.lastUpdatedAt = new Date();
await child4Repository.save(data4);
if (orgTypes.has(requestBody.typeDraft.toUpperCase())) {
// Create posMaster
for await (const item of orgPosMaster.filter(
(x: PosMaster) => x.orgChild4Id == data4Id,
) as any) {
delete item.id;
const posMaster = Object.assign(new PosMaster(), item);
posMaster.positions = [];
if (
["ORG_POSITION_PERSON", "ORG_POSITION_PERSON_ROLE"].includes(
requestBody.typeDraft.toUpperCase(),
)
) {
posMaster.next_holderId = item.current_holderId;
} else {
posMaster.next_holderId = null;
posMaster.isSit = false;
}
if (
["ORG_POSITION_ROLE", "ORG_POSITION_PERSON_ROLE"].includes(
requestBody.typeDraft.toUpperCase(),
)
) {
posMaster.authRoleId = item.authRoleId;
} else {
posMaster.authRoleId = null;
}
posMaster.current_holderId = null;
posMaster.orgRevisionId = revision.id;
posMaster.orgRootId = data.id;
posMaster.orgChild1Id = data1.id;
posMaster.orgChild2Id = data2.id;
posMaster.orgChild3Id = data3.id;
posMaster.orgChild4Id = data4.id;
posMaster.createdUserId = request.sub;
posMaster.createdFullName = request.name;
posMaster.createdAt = new Date();
posMaster.lastUpdateUserId = request.sub;
posMaster.lastUpdateFullName = request.name;
posMaster.lastUpdatedAt = new Date();
await posMasterRepository.save(posMaster);
// Copy assignments
posMaster.posMasterAssigns = item.posMasterAssigns.map(
({ id, ...rest }: PosMasterAssign) => ({
...rest,
posMasterId: posMaster.id,
}),
);
// Create positions
for await (const pos of item.positions) {
delete pos.id;
const position = Object.assign(new Position(), pos);
position.posMasterId = posMaster.id;
if (
["ORG_POSITION", "ORG_POSITION_ROLE"].includes(
requestBody.typeDraft.toUpperCase(),
)
) {
position.positionIsSelected = false;
}
position.createdUserId = request.sub;
position.createdFullName = request.name;
position.createdAt = new Date();
position.lastUpdateUserId = request.sub;
position.lastUpdateFullName = request.name;
position.lastUpdatedAt = new Date();
await positionRepository.save(position);
}
}
}
}
}
} }
} }
} }
} }
}
const _orgRevisions = await orgRevisionRepository.find({ }
where: [{ orgRevisionIsDraft: true, id: Not(revision.id) }], console.log("ssssssssss");
}); const _orgRevisions = await orgRevisionRepository.find({
const _roots = await orgRootRepository.find({ where: [{ orgRevisionIsDraft: true, id: Not(revision.id) }],
where: [{ orgRevisionId: In(_orgRevisions.map((x) => x.id)) }], });
}); const _roots = await orgRootRepository.find({
const _posMasters = await posMasterRepository.find({ where: [{ orgRevisionId: In(_orgRevisions.map((x) => x.id)) }],
where: [{ orgRevisionId: In(_orgRevisions.map((x) => x.id)) }], });
}); const _posMasters = await posMasterRepository.find({
const _employeePosMasters = await employeePosMasterRepository.find({ where: [{ orgRevisionId: In(_orgRevisions.map((x) => x.id)) }],
where: [{ orgRevisionId: In(_orgRevisions.map((x) => x.id)) }], });
}); const _employeePosMasters = await employeePosMasterRepository.find({
const _employeeTempPosMasters = await employeeTempPosMasterRepository.find({ where: [{ orgRevisionId: In(_orgRevisions.map((x) => x.id)) }],
where: [{ orgRevisionId: In(_orgRevisions.map((x) => x.id)) }], });
}); const _employeeTempPosMasters = await employeeTempPosMasterRepository.find({
await positionRepository.delete({ posMasterId: In(_posMasters.map((x) => x.id)) }); where: [{ orgRevisionId: In(_orgRevisions.map((x) => x.id)) }],
await employeePositionRepository.delete({ });
posMasterId: In(_employeePosMasters.map((x) => x.id)), await positionRepository.delete({ posMasterId: In(_posMasters.map((x) => x.id)) });
}); await employeePositionRepository.delete({
await employeePositionRepository.delete({ posMasterId: In(_employeePosMasters.map((x) => x.id)),
posMasterTempId: In(_employeeTempPosMasters.map((x) => x.id)), });
}); await employeePositionRepository.delete({
await posMasterAssignRepository.delete({ posMasterId: In(_posMasters.map((x) => x.id)) }); posMasterTempId: In(_employeeTempPosMasters.map((x) => x.id)),
await posMasterActRepository.delete({ posMasterId: In(_posMasters.map((x) => x.id)) }); });
await posMasterActRepository.delete({ await posMasterAssignRepository.delete({ posMasterId: In(_posMasters.map((x) => x.id)) });
posMasterChildId: In(_posMasters.map((x) => x.id)), await posMasterActRepository.delete({ posMasterId: In(_posMasters.map((x) => x.id)) });
}); await posMasterActRepository.delete({
await posMasterRepository.remove(_posMasters); posMasterChildId: In(_posMasters.map((x) => x.id)),
await employeePosMasterRepository.remove(_employeePosMasters); });
await employeeTempPosMasterRepository.remove(_employeeTempPosMasters); await posMasterRepository.remove(_posMasters);
await child4Repository.delete({ orgRevisionId: In(_orgRevisions.map((x) => x.id)) }); await employeePosMasterRepository.remove(_employeePosMasters);
await child3Repository.delete({ orgRevisionId: In(_orgRevisions.map((x) => x.id)) }); await employeeTempPosMasterRepository.remove(_employeeTempPosMasters);
await child2Repository.delete({ orgRevisionId: In(_orgRevisions.map((x) => x.id)) }); await child4Repository.delete({ orgRevisionId: In(_orgRevisions.map((x) => x.id)) });
await child1Repository.delete({ orgRevisionId: In(_orgRevisions.map((x) => x.id)) }); await child3Repository.delete({ orgRevisionId: In(_orgRevisions.map((x) => x.id)) });
await permissionOrgRepository.delete({ await child2Repository.delete({ orgRevisionId: In(_orgRevisions.map((x) => x.id)) });
orgRootId: In(_roots.map((x) => x.id)), await child1Repository.delete({ orgRevisionId: In(_orgRevisions.map((x) => x.id)) });
}); await permissionOrgRepository.delete({
await orgRootRepository.delete({ orgRevisionId: In(_orgRevisions.map((x) => x.id)) }); orgRootId: In(_roots.map((x) => x.id)),
await orgRevisionRepository.remove(_orgRevisions); });
await orgRootRepository.delete({ orgRevisionId: In(_orgRevisions.map((x) => x.id)) });
await orgRevisionRepository.remove(_orgRevisions);
console.log("[AMQ] Create Draft Success"); console.log("[AMQ] Create Draft Success");
return true; return true;
@ -1721,4 +1732,3 @@ async function handler_org_draft(msg: amqp.ConsumeMessage): Promise<boolean> {
return false; return false;
} }
} }