hrms-api-org/src/services/rabbitmq.ts

1061 lines
53 KiB
TypeScript
Raw Normal View History

2024-10-11 11:05:31 +07:00
import amqp from "amqplib";
import { AppDataSource } from "../database/data-source";
import { Command } from "../entities/Command";
import { commandTypePath } from "../interfaces/utils";
import CallAPI from "../interfaces/call-api";
import HttpError from "../interfaces/http-error";
import HttpStatusCode from "../interfaces/http-status";
import { RequestWithUser } from "../middlewares/user";
import { PosMaster } from "../entities/PosMaster";
import { Profile } from "../entities/Profile";
import { EmployeePosMaster } from "../entities/EmployeePosMaster";
import { EmployeeTempPosMaster } from "../entities/EmployeeTempPosMaster";
import { ProfileEmployee } from "../entities/ProfileEmployee";
import { OrgRevision } from "../entities/OrgRevision";
import { request } from "http";
import { EmployeePosition } from "../entities/EmployeePosition";
import { OrgChild1 } from "../entities/OrgChild1";
import { OrgChild2 } from "../entities/OrgChild2";
import { OrgChild3 } from "../entities/OrgChild3";
import { OrgChild4 } from "../entities/OrgChild4";
import { OrgRoot } from "../entities/OrgRoot";
2024-10-11 11:05:31 +07:00
export let sendToQueue: (payload: any) => void;
export let sendToQueueOrg: (payload: any) => void;
2024-10-18 16:11:22 +07:00
export async function init() {
//----> (1) Producer
2025-02-24 14:47:09 +07:00
if (!process.env.AMQ_URL || !process.env.AMQ_QUEUE || !process.env.AMQ_QUEUE_ORG) return;
2024-10-11 11:05:31 +07:00
2025-02-24 14:47:09 +07:00
const { AMQ_URL: url, AMQ_QUEUE: queue, AMQ_QUEUE_ORG: queue_org } = process.env; //----> (1.2) get url and queue from .env
2024-10-11 11:05:31 +07:00
const connection = await amqp.connect(url); //----> (1.3) set up url with amqp protocol
2024-11-18 12:02:45 +07:00
console.log(connection ? "[AMQ] Connection success" : "[AMQ] Connection failed");
2024-10-11 11:05:31 +07:00
const channel = await connection.createChannel(); //----> (1.4) create Channel
2024-11-18 12:02:45 +07:00
console.log(channel ? "[AMQ] Create channel success" : "[AMQ] Create channel failed");
2025-01-08 14:15:28 +07:00
channel.assertQueue(queue, { durable: true }), //----> (1.5) assert queue and set durable (if "true" save to disk on RabbitMQ)
2025-01-08 14:15:28 +07:00
channel.assertQueue(queue_org, { durable: true }),
channel.prefetch(1);
2024-10-11 11:05:31 +07:00
2024-10-18 16:11:22 +07:00
sendToQueue = (payload: any, persistent = true) => {
//----> (2) sendQueue To RabbitMQ and set persistent (if "true" redo the failed queue when server run again)
2024-10-11 11:05:31 +07:00
channel.sendToQueue(queue, Buffer.from(JSON.stringify(payload)), {
persistent,
});
};
2024-12-24 09:12:12 +07:00
sendToQueueOrg = (payload: any, persistent = true) => {
channel.sendToQueue(queue_org, Buffer.from(JSON.stringify(payload)), { persistent });
};
2024-10-11 11:05:31 +07:00
console.log("[AMQ] Listening for message...");
createConsumer(queue, channel, handler), //----> (3) Process Consumer
createConsumer(queue_org, channel, handler_org);
// createConsumer(queue2, channel, handler2);
2024-10-11 11:05:31 +07:00
}
2024-10-30 10:49:58 +07:00
let retries = 0;
function createConsumer( //----> consumer
2024-10-11 11:05:31 +07:00
queue: string,
channel: amqp.Channel,
handler: (msg: amqp.ConsumeMessage) => Promise<boolean> | boolean,
) {
channel.consume(
queue,
async (msg) => {
if (!msg) return;
2024-10-30 10:49:58 +07:00
if ((await handler(msg)) || retries++ >= 3) {
retries = 0;
console.log("[AMQ] Process Consumer success");
2024-10-30 10:49:58 +07:00
return channel.ack(msg);
}
2024-12-18 10:59:57 +07:00
console.log("[AMQ] Process Consumer failed");
2024-10-11 11:05:31 +07:00
return await new Promise((resolve) => setTimeout(() => resolve(channel.nack(msg)), 3000));
},
{ noAck: false },
);
}
2024-10-18 16:11:22 +07:00
async function handler(msg: amqp.ConsumeMessage): Promise<boolean> {
//----> condition before process consumer
2024-10-11 11:05:31 +07:00
const repo = AppDataSource.getRepository(Command);
const { data, token, user } = JSON.parse(msg.content.toString());
const { id, status, lastUpdateUserId, lastUpdateFullName, lastUpdatedAt } = data;
const command = await repo.findOne({
where: { id: id },
relations: ["commandType", "commandRecives"],
});
if (!command) return true;
const path = commandTypePath(command.commandType.code);
if (path == null) throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบประเภทคำสั่งนี้ในระบบ");
return await new CallAPI()
2025-01-08 14:15:28 +07:00
2024-10-11 11:05:31 +07:00
.PostData(
{
2024-12-18 10:59:57 +07:00
headers: { authorization: token },
2024-10-11 11:05:31 +07:00
},
path + "/excecute",
{
refIds: command.commandRecives
.filter((x) => x.refId != null)
.map((x) => ({
refId: x.refId,
commandNo: command.commandNo,
commandYear: command.commandYear,
2024-10-18 16:11:22 +07:00
commandId: command.id,
remark: command.positionDetail,
2024-10-11 11:05:31 +07:00
amount: x.amount,
2024-12-06 18:43:07 +07:00
amountSpecial: x.amountSpecial,
2024-10-11 11:05:31 +07:00
positionSalaryAmount: x.positionSalaryAmount,
mouthSalaryAmount: x.mouthSalaryAmount,
commandCode: command.commandType.commandCode,
commandName: command.commandType.name,
commandDateAffect: command.commandExcecuteDate,
commandDateSign: command.commandAffectDate,
2024-10-11 11:05:31 +07:00
})),
},
false,
)
.then(async (res) => {
console.log("[AMQ] Excecute Command Success");
Object.assign(command, { status, lastUpdateUserId, lastUpdateFullName, lastUpdatedAt });
const result = await repo.save(command).catch((e) => console.log(e));
if (result) return true;
return false;
})
.catch((e) => {
console.error(e);
return false;
});
}
async function handler_org(msg: amqp.ConsumeMessage): Promise<boolean> {
//----> condition before process consume
const repoPosmaster = AppDataSource.getRepository(PosMaster);
const repoEmployeePosmaster = AppDataSource.getRepository(EmployeePosMaster);
const repoEmployeeTempPosmaster = AppDataSource.getRepository(EmployeeTempPosMaster);
const repoProfile = AppDataSource.getRepository(Profile);
const repoProfileEmployee = AppDataSource.getRepository(ProfileEmployee);
const employeePositionRepository = AppDataSource.getRepository(EmployeePosition);
const repoOrgRevision = AppDataSource.getRepository(OrgRevision);
const orgRootRepository = AppDataSource.getRepository(OrgRoot);
const child1Repository = AppDataSource.getRepository(OrgChild1);
const child2Repository = AppDataSource.getRepository(OrgChild2);
const child3Repository = AppDataSource.getRepository(OrgChild3);
const child4Repository = AppDataSource.getRepository(OrgChild4);
const orgRevisionPublish = await repoOrgRevision
.createQueryBuilder("orgRevision")
.where("orgRevision.orgRevisionIsDraft = false")
.andWhere("orgRevision.orgRevisionIsCurrent = true")
.getOne();
const orgRevisionDraft = await repoOrgRevision
.createQueryBuilder("orgRevision")
.where("orgRevision.orgRevisionIsDraft = true")
.andWhere("orgRevision.orgRevisionIsCurrent = false")
.getOne();
if (orgRevisionPublish) {
orgRevisionPublish.orgRevisionIsDraft = false;
orgRevisionPublish.orgRevisionIsCurrent = false;
await repoOrgRevision.save(orgRevisionPublish);
}
if (orgRevisionDraft) {
orgRevisionDraft.orgRevisionIsCurrent = true;
orgRevisionDraft.orgRevisionIsDraft = false;
await repoOrgRevision.save(orgRevisionDraft);
}
const { data, token, user } = JSON.parse(msg.content.toString());
const { id, status, lastUpdateUserId, lastUpdateFullName, lastUpdatedAt } = data;
try {
const posMaster = await repoPosmaster.find({
where: { orgRevisionId: id },
relations: [
"orgRoot",
"orgChild4",
"orgChild3",
"orgChild2",
"orgChild1",
"positions",
"positions.posLevel",
"positions.posType",
"positions.posExecutive",
],
});
for (const item of posMaster) {
2025-03-12 12:15:13 +07:00
if (item.next_holderId != null) {
const profile = await repoProfile.findOne({
where: { id: item.next_holderId == null ? "" : item.next_holderId },
});
const position = await item.positions.find((x) => x.positionIsSelected == true);
const _null: any = null;
if (profile != null) {
profile.posLevelId = position?.posLevelId ?? _null;
profile.posTypeId = position?.posTypeId ?? _null;
profile.position = position?.positionName ?? _null;
await repoProfile.save(profile);
}
}
item.current_holderId = item.next_holderId;
item.next_holderId = null;
item.lastUpdateUserId = lastUpdateUserId;
item.lastUpdateFullName = lastUpdateFullName;
item.lastUpdatedAt = lastUpdatedAt;
await repoPosmaster.save(item).catch((e) => console.log(e));
}
2025-03-12 12:15:13 +07:00
if (orgRevisionPublish != null && orgRevisionDraft != null) {
//new main revision
const before = null;
//cone tree
// if (
// orgRevisionPublish.typeDraft.toUpperCase() == "ORG" ||
// orgRevisionPublish.typeDraft.toUpperCase() == "ORG_POSITION" ||
// orgRevisionPublish.typeDraft.toUpperCase() == "ORG_POSITION_PERSON" ||
// orgRevisionPublish.typeDraft.toUpperCase() == "ORG_POSITION_ROLE" ||
// orgRevisionPublish.typeDraft.toUpperCase() == "ORG_POSITION_PERSON_ROLE"
// ) {
//หา dna tree
const orgRoot = await orgRootRepository.find({
2025-03-13 17:46:17 +07:00
where: { orgRevisionId: orgRevisionPublish.id },
});
const orgChild1 = await child1Repository.find({
2025-03-13 17:46:17 +07:00
where: { orgRevisionId: orgRevisionPublish.id },
});
const orgChild2 = await child2Repository.find({
2025-03-13 17:46:17 +07:00
where: { orgRevisionId: orgRevisionPublish.id },
});
const orgChild3 = await child3Repository.find({
2025-03-13 17:46:17 +07:00
where: { orgRevisionId: orgRevisionPublish.id },
});
const orgChild4 = await child4Repository.find({
2025-03-13 17:46:17 +07:00
where: { orgRevisionId: orgRevisionPublish.id },
});
//หา dna posmaster ถ้าไม่มีให้เอาตัวเองเป็น dna
const orgemployeePosMaster = await repoEmployeePosmaster.find({
where: { orgRevisionId: orgRevisionPublish.id },
relations: ["positions"],
});
let _orgemployeePosMaster: EmployeePosMaster[];
// 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"
// ) {
_orgemployeePosMaster = orgemployeePosMaster.map((x) => ({
...x,
ancestorDNA:
x.ancestorDNA == null || x.ancestorDNA == "00000000-0000-0000-0000-000000000000"
? x.id
: x.ancestorDNA,
}));
await repoEmployeePosmaster.save(_orgemployeePosMaster);
// }
//หา dna posmaster ถ้าไม่มีให้เอาตัวเองเป็น dna
const orgemployeeTempPosMaster = await repoEmployeeTempPosmaster.find({
where: { orgRevisionId: orgRevisionPublish.id },
relations: ["positions"],
});
let _orgemployeeTempPosMaster: EmployeeTempPosMaster[];
// 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"
// ) {
_orgemployeeTempPosMaster = orgemployeeTempPosMaster.map((x) => ({
...x,
ancestorDNA:
x.ancestorDNA == null || x.ancestorDNA == "00000000-0000-0000-0000-000000000000"
? x.id
: x.ancestorDNA,
}));
await repoEmployeeTempPosmaster.save(_orgemployeeTempPosMaster);
// }
//create org
orgRoot.forEach(async (x: any) => {
var dataId = x.id;
// 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"
// ) {
//create employeePosmaster
await Promise.all(
_orgemployeePosMaster
.filter((x: EmployeePosMaster) => x.orgRootId == dataId && x.orgChild1Id == null)
.map(async (item: any) => {
delete item.id;
const employeePosMaster = Object.assign(new EmployeePosMaster(), item);
employeePosMaster.positions = [];
// if (
// requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON" ||
// requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON_ROLE"
// ) {
// employeePosMaster.current_holderId = item.current_holderId;
// } else {
// // employeePosMaster.next_holderId = null;
// employeePosMaster.isSit = false;
// }
// if (
// requestBody.typeDraft.toUpperCase() == "ORG_POSITION_ROLE" ||
// requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON_ROLE"
// ) {
// employeePosMaster.authRoleId = item.authRoleId;
// } else {
// employeePosMaster.authRoleId = null;
// }
// employeePosMaster.current_holderId = null;
2025-03-11 23:37:05 +07:00
employeePosMaster.orgRevisionId = orgRevisionDraft.id;
employeePosMaster.orgRootId = dataId;
employeePosMaster.createdUserId = "";
employeePosMaster.createdFullName = "System Administrator";
employeePosMaster.createdAt = new Date();
employeePosMaster.lastUpdateUserId = "";
employeePosMaster.lastUpdateFullName = "System Administrator";
employeePosMaster.lastUpdatedAt = new Date();
await repoEmployeePosmaster.save(employeePosMaster);
//create employeePosition
item.positions.map(async (pos: any) => {
delete pos.id;
const employeePosition: EmployeePosition = Object.assign(
new EmployeePosition(),
pos,
);
employeePosition.posMasterId = employeePosMaster.id;
// if (
// requestBody.typeDraft.toUpperCase() == "ORG_POSITION" ||
// requestBody.typeDraft.toUpperCase() == "ORG_POSITION_ROLE"
// ) {
// employeePosition.positionIsSelected = false;
// }
employeePosition.createdUserId = "";
employeePosition.createdFullName = "System Administrator";
employeePosition.createdAt = new Date();
employeePosition.lastUpdateUserId = "";
employeePosition.lastUpdateFullName = "System Administrator";
employeePosition.lastUpdatedAt = new Date();
await employeePositionRepository.save(employeePosition);
});
}),
);
//create employeeTempPosmaster
await Promise.all(
_orgemployeeTempPosMaster
.filter((x: EmployeeTempPosMaster) => x.orgRootId == dataId && x.orgChild1Id == null)
.map(async (item: any) => {
delete item.id;
const employeeTempPosMaster = Object.assign(new EmployeeTempPosMaster(), item);
employeeTempPosMaster.positions = [];
// if (
// requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON" ||
// requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON_ROLE"
// ) {
// employeeTempPosMaster.current_holderId = item.current_holderId;
// } else {
// // employeeTempPosMaster.next_holderId = null;
// employeeTempPosMaster.isSit = false;
// }
// if (
// requestBody.typeDraft.toUpperCase() == "ORG_POSITION_ROLE" ||
// requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON_ROLE"
// ) {
// employeeTempPosMaster.authRoleId = item.authRoleId;
// } else {
// employeeTempPosMaster.authRoleId = null;
// }
// employeeTempPosMaster.current_holderId = null;
2025-03-11 23:37:05 +07:00
employeeTempPosMaster.orgRevisionId = orgRevisionDraft.id;
employeeTempPosMaster.orgRootId = dataId;
employeeTempPosMaster.createdUserId = "";
employeeTempPosMaster.createdFullName = "System Administrator";
employeeTempPosMaster.createdAt = new Date();
employeeTempPosMaster.lastUpdateUserId = "";
employeeTempPosMaster.lastUpdateFullName = "System Administrator";
employeeTempPosMaster.lastUpdatedAt = new Date();
await repoEmployeeTempPosmaster.save(employeeTempPosMaster);
//create employeePosition
item.positions.map(async (pos: any) => {
delete pos.id;
const employeePosition: EmployeePosition = Object.assign(
new EmployeePosition(),
pos,
);
// employeePosition.posMasterTempId = employeeTempPosMaster.id;
// if (
// requestBody.typeDraft.toUpperCase() == "ORG_POSITION" ||
// requestBody.typeDraft.toUpperCase() == "ORG_POSITION_ROLE"
// ) {
// employeePosition.positionIsSelected = false;
// }
employeePosition.createdUserId = "";
employeePosition.createdFullName = "System Administrator";
employeePosition.createdAt = new Date();
employeePosition.lastUpdateUserId = "";
employeePosition.lastUpdateFullName = "System Administrator";
employeePosition.lastUpdatedAt = new Date();
await employeePositionRepository.save(employeePosition);
});
}),
);
// }
//create org
orgChild1
.filter((x: OrgChild1) => x.orgRootId == dataId)
.forEach(async (x: any) => {
var data1Id = x.id;
// 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"
// ) {
//create employeePosmaster
await Promise.all(
_orgemployeePosMaster
.filter((x: EmployeePosMaster) => x.orgChild1Id == data1Id && x.orgChild2Id == null)
.map(async (item: any) => {
delete item.id;
const employeePosMaster = Object.assign(new EmployeePosMaster(), item);
employeePosMaster.positions = [];
// if (
// requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON" ||
// requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON_ROLE"
// ) {
// employeePosMaster.current_holderId = item.current_holderId;
// } else {
// // employeePosMaster.next_holderId = null;
// employeePosMaster.isSit = false;
// }
// if (
// requestBody.typeDraft.toUpperCase() == "ORG_POSITION_ROLE" ||
// requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON_ROLE"
// ) {
// employeePosMaster.authRoleId = item.authRoleId;
// } else {
// employeePosMaster.authRoleId = null;
// }
// employeePosMaster.current_holderId = null;
2025-03-11 23:37:05 +07:00
employeePosMaster.orgRevisionId = orgRevisionDraft.id;
employeePosMaster.orgRootId = dataId;
employeePosMaster.orgChild1Id = data1Id;
employeePosMaster.createdUserId = "";
employeePosMaster.createdFullName = "System Administrator";
employeePosMaster.createdAt = new Date();
employeePosMaster.lastUpdateUserId = "";
employeePosMaster.lastUpdateFullName = "System Administrator";
employeePosMaster.lastUpdatedAt = new Date();
await repoEmployeePosmaster.save(employeePosMaster);
//create employeePosition
item.positions.map(async (pos: any) => {
delete pos.id;
const employeePosition: EmployeePosition = Object.assign(
new EmployeePosition(),
pos,
);
employeePosition.posMasterId = employeePosMaster.id;
// if (
// requestBody.typeDraft.toUpperCase() == "ORG_POSITION" ||
// requestBody.typeDraft.toUpperCase() == "ORG_POSITION_ROLE"
// ) {
// employeePosition.positionIsSelected = false;
// }
employeePosition.createdUserId = "";
employeePosition.createdFullName = "System Administrator";
employeePosition.createdAt = new Date();
employeePosition.lastUpdateUserId = "";
employeePosition.lastUpdateFullName = "System Administrator";
employeePosition.lastUpdatedAt = new Date();
await employeePositionRepository.save(employeePosition);
});
}),
);
// create employeeTempPosmaster
await Promise.all(
_orgemployeeTempPosMaster
.filter(
(x: EmployeeTempPosMaster) => x.orgChild1Id == data1Id && x.orgChild2Id == null,
)
.map(async (item: any) => {
delete item.id;
const employeeTempPosMaster = Object.assign(new EmployeeTempPosMaster(), item);
employeeTempPosMaster.positions = [];
// if (
// requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON" ||
// requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON_ROLE"
// ) {
// employeeTempPosMaster.current_holderId = item.current_holderId;
// } else {
// // employeeTempPosMaster.next_holderId = null;
// employeeTempPosMaster.isSit = false;
// }
// if (
// requestBody.typeDraft.toUpperCase() == "ORG_POSITION_ROLE" ||
// requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON_ROLE"
// ) {
// employeeTempPosMaster.authRoleId = item.authRoleId;
// } else {
// employeeTempPosMaster.authRoleId = null;
// }
// employeeTempPosMaster.current_holderId = null;
2025-03-11 23:37:05 +07:00
employeeTempPosMaster.orgRevisionId = orgRevisionDraft.id;
employeeTempPosMaster.orgRootId = dataId;
employeeTempPosMaster.orgChild1Id = data1Id;
employeeTempPosMaster.createdUserId = "";
employeeTempPosMaster.createdFullName = "System Administrator";
employeeTempPosMaster.createdAt = new Date();
employeeTempPosMaster.lastUpdateUserId = "";
employeeTempPosMaster.lastUpdateFullName = "System Administrator";
employeeTempPosMaster.lastUpdatedAt = new Date();
await repoEmployeeTempPosmaster.save(employeeTempPosMaster);
//create employeePosition
item.positions.map(async (pos: any) => {
delete pos.id;
const employeePosition: EmployeePosition = Object.assign(
new EmployeePosition(),
pos,
);
employeePosition.posMasterTempId = employeeTempPosMaster.id;
// if (
// requestBody.typeDraft.toUpperCase() == "ORG_POSITION" ||
// requestBody.typeDraft.toUpperCase() == "ORG_POSITION_ROLE"
// ) {
// employeePosition.positionIsSelected = false;
// }
employeePosition.createdUserId = "";
employeePosition.createdFullName = "System Administrator";
employeePosition.createdAt = new Date();
employeePosition.lastUpdateUserId = "";
employeePosition.lastUpdateFullName = "System Administrator";
employeePosition.lastUpdatedAt = new Date();
await employeePositionRepository.save(employeePosition);
});
}),
);
// }
//create org
orgChild2
.filter((x: OrgChild2) => x.orgChild1Id == data1Id)
.forEach(async (x: any) => {
var data2Id = x.id;
// 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"
// ) {
//create employeePosmaster
await Promise.all(
_orgemployeePosMaster
.filter(
(x: EmployeePosMaster) => x.orgChild2Id == data2Id && x.orgChild3Id == null,
)
.map(async (item: any) => {
delete item.id;
const employeePosMaster = Object.assign(new EmployeePosMaster(), item);
employeePosMaster.positions = [];
// if (
// requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON" ||
// requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON_ROLE"
// ) {
// employeePosMaster.current_holderId = item.current_holderId;
// } else {
// // employeePosMaster.next_holderId = null;
// employeePosMaster.isSit = false;
// }
// if (
// requestBody.typeDraft.toUpperCase() == "ORG_POSITION_ROLE" ||
// requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON_ROLE"
// ) {
// employeePosMaster.authRoleId = item.authRoleId;
// } else {
// employeePosMaster.authRoleId = null;
// }
// employeePosMaster.current_holderId = null;
2025-03-11 23:37:05 +07:00
employeePosMaster.orgRevisionId = orgRevisionDraft.id;
employeePosMaster.orgRootId = dataId;
employeePosMaster.orgChild1Id = data1Id;
employeePosMaster.orgChild2Id = data2Id;
employeePosMaster.createdUserId = "";
employeePosMaster.createdFullName = "System Administrator";
employeePosMaster.createdAt = new Date();
employeePosMaster.lastUpdateUserId = "";
employeePosMaster.lastUpdateFullName = "System Administrator";
employeePosMaster.lastUpdatedAt = new Date();
await repoEmployeePosmaster.save(employeePosMaster);
//create employeePosition
item.positions.map(async (pos: any) => {
delete pos.id;
const employeePosition: EmployeePosition = Object.assign(
new EmployeePosition(),
pos,
);
employeePosition.posMasterId = employeePosMaster.id;
// if (
// requestBody.typeDraft.toUpperCase() == "ORG_POSITION" ||
// requestBody.typeDraft.toUpperCase() == "ORG_POSITION_ROLE"
// ) {
// employeePosition.positionIsSelected = false;
// }
employeePosition.createdUserId = "";
employeePosition.createdFullName = "System Administrator";
employeePosition.createdAt = new Date();
employeePosition.lastUpdateUserId = "";
employeePosition.lastUpdateFullName = "System Administrator";
employeePosition.lastUpdatedAt = new Date();
await employeePositionRepository.save(employeePosition);
});
}),
);
// create employeeTempPosmaster
await Promise.all(
_orgemployeeTempPosMaster
.filter(
(x: EmployeeTempPosMaster) =>
x.orgChild2Id == data2Id && x.orgChild3Id == null,
)
.map(async (item: any) => {
delete item.id;
const employeeTempPosMaster = Object.assign(
new EmployeeTempPosMaster(),
item,
);
employeeTempPosMaster.positions = [];
// if (
// requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON" ||
// requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON_ROLE"
// ) {
// employeeTempPosMaster.current_holderId = item.current_holderId;
// } else {
// // employeeTempPosMaster.next_holderId = null;
// employeeTempPosMaster.isSit = false;
// }
// if (
// requestBody.typeDraft.toUpperCase() == "ORG_POSITION_ROLE" ||
// requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON_ROLE"
// ) {
// employeeTempPosMaster.authRoleId = item.authRoleId;
// } else {
// employeeTempPosMaster.authRoleId = null;
// }
// employeeTempPosMaster.current_holderId = null;
2025-03-11 23:37:05 +07:00
employeeTempPosMaster.orgRevisionId = orgRevisionDraft.id;
employeeTempPosMaster.orgRootId = dataId;
employeeTempPosMaster.orgChild1Id = data1Id;
employeeTempPosMaster.orgChild2Id = data2Id;
employeeTempPosMaster.createdUserId = "";
employeeTempPosMaster.createdFullName = "System Administrator";
employeeTempPosMaster.createdAt = new Date();
employeeTempPosMaster.lastUpdateUserId = "";
employeeTempPosMaster.lastUpdateFullName = "System Administrator";
employeeTempPosMaster.lastUpdatedAt = new Date();
await repoEmployeeTempPosmaster.save(employeeTempPosMaster);
//create employeePosition
item.positions.map(async (pos: any) => {
delete pos.id;
const employeePosition: EmployeePosition = Object.assign(
new EmployeePosition(),
pos,
);
employeePosition.posMasterTempId = employeeTempPosMaster.id;
// if (
// requestBody.typeDraft.toUpperCase() == "ORG_POSITION" ||
// requestBody.typeDraft.toUpperCase() == "ORG_POSITION_ROLE"
// ) {
// employeePosition.positionIsSelected = false;
// }
employeePosition.createdUserId = "";
employeePosition.createdFullName = "System Administrator";
employeePosition.createdAt = new Date();
employeePosition.lastUpdateUserId = "";
employeePosition.lastUpdateFullName = "System Administrator";
employeePosition.lastUpdatedAt = new Date();
await employeePositionRepository.save(employeePosition);
});
}),
);
// }
//create org
orgChild3
.filter((x: OrgChild3) => x.orgChild2Id == data2Id)
.forEach(async (x: any) => {
var data3Id = x.id;
// 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"
// ) {
//create employeePosmaster
await Promise.all(
_orgemployeePosMaster
.filter(
(x: EmployeePosMaster) =>
x.orgChild3Id == data3Id && x.orgChild4Id == null,
)
.map(async (item: any) => {
delete item.id;
const employeePosMaster = Object.assign(new EmployeePosMaster(), item);
employeePosMaster.positions = [];
// if (
// requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON" ||
// requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON_ROLE"
// ) {
// employeePosMaster.current_holderId = item.current_holderId;
// } else {
// // employeePosMaster.next_holderId = null;
// employeePosMaster.isSit = false;
// }
// if (
// requestBody.typeDraft.toUpperCase() == "ORG_POSITION_ROLE" ||
// requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON_ROLE"
// ) {
// employeePosMaster.authRoleId = item.authRoleId;
// } else {
// employeePosMaster.authRoleId = null;
// }
// employeePosMaster.current_holderId = null;
2025-03-11 23:37:05 +07:00
employeePosMaster.orgRevisionId = orgRevisionDraft.id;
employeePosMaster.orgRootId = dataId;
employeePosMaster.orgChild1Id = data1Id;
employeePosMaster.orgChild2Id = data2Id;
employeePosMaster.orgChild3Id = data3Id;
employeePosMaster.createdUserId = "";
employeePosMaster.createdFullName = "System Administrator";
employeePosMaster.createdAt = new Date();
employeePosMaster.lastUpdateUserId = "";
employeePosMaster.lastUpdateFullName = "System Administrator";
employeePosMaster.lastUpdatedAt = new Date();
await repoEmployeePosmaster.save(employeePosMaster);
//create employeePosition
item.positions.map(async (pos: any) => {
delete pos.id;
const employeePosition: EmployeePosition = Object.assign(
new EmployeePosition(),
pos,
);
employeePosition.posMasterId = employeePosMaster.id;
// if (
// requestBody.typeDraft.toUpperCase() == "ORG_POSITION" ||
// requestBody.typeDraft.toUpperCase() == "ORG_POSITION_ROLE"
// ) {
// employeePosition.positionIsSelected = false;
// }
employeePosition.createdUserId = "";
employeePosition.createdFullName = "System Administrator";
employeePosition.createdAt = new Date();
employeePosition.lastUpdateUserId = "";
employeePosition.lastUpdateFullName = "System Administrator";
employeePosition.lastUpdatedAt = new Date();
await employeePositionRepository.save(employeePosition);
});
}),
);
// create employeeTempPosmaster
await Promise.all(
_orgemployeeTempPosMaster
.filter(
(x: EmployeeTempPosMaster) =>
x.orgChild3Id == data3Id && x.orgChild4Id == null,
)
.map(async (item: any) => {
delete item.id;
const employeeTempPosMaster = Object.assign(
new EmployeeTempPosMaster(),
item,
);
employeeTempPosMaster.positions = [];
// if (
// requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON" ||
// requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON_ROLE"
// ) {
// employeeTempPosMaster.current_holderId = item.current_holderId;
// } else {
// // employeeTempPosMaster.next_holderId = null;
// employeeTempPosMaster.isSit = false;
// }
// if (
// requestBody.typeDraft.toUpperCase() == "ORG_POSITION_ROLE" ||
// requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON_ROLE"
// ) {
// employeeTempPosMaster.authRoleId = item.authRoleId;
// } else {
// employeeTempPosMaster.authRoleId = null;
// }
// employeeTempPosMaster.current_holderId = null;
2025-03-11 23:37:05 +07:00
employeeTempPosMaster.orgRevisionId = orgRevisionDraft.id;
employeeTempPosMaster.orgRootId = dataId;
employeeTempPosMaster.orgChild1Id = data1Id;
employeeTempPosMaster.orgChild2Id = data2Id;
employeeTempPosMaster.orgChild3Id = data3Id;
employeeTempPosMaster.createdUserId = "";
employeeTempPosMaster.createdFullName = "System Administrator";
employeeTempPosMaster.createdAt = new Date();
employeeTempPosMaster.lastUpdateUserId = "";
employeeTempPosMaster.lastUpdateFullName = "System Administrator";
employeeTempPosMaster.lastUpdatedAt = new Date();
await repoEmployeeTempPosmaster.save(employeeTempPosMaster);
//create employeePosition
item.positions.map(async (pos: any) => {
delete pos.id;
const employeePosition: EmployeePosition = Object.assign(
new EmployeePosition(),
pos,
);
employeePosition.posMasterTempId = employeeTempPosMaster.id;
// if (
// requestBody.typeDraft.toUpperCase() == "ORG_POSITION" ||
// requestBody.typeDraft.toUpperCase() == "ORG_POSITION_ROLE"
// ) {
// employeePosition.positionIsSelected = false;
// }
employeePosition.createdUserId = "";
employeePosition.createdFullName = "System Administrator";
employeePosition.createdAt = new Date();
employeePosition.lastUpdateUserId = "";
employeePosition.lastUpdateFullName = "System Administrator";
employeePosition.lastUpdatedAt = new Date();
await employeePositionRepository.save(employeePosition);
});
}),
);
// }
//create org
orgChild4
.filter((x: OrgChild4) => x.orgChild3Id == data3Id)
.forEach(async (x: any) => {
var data4Id = x.id;
// 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"
// ) {
//create employeePosmaster
await Promise.all(
_orgemployeePosMaster
.filter((x: EmployeePosMaster) => x.orgChild4Id == data4Id)
.map(async (item: any) => {
delete item.id;
const employeePosMaster = Object.assign(
new EmployeePosMaster(),
item,
);
employeePosMaster.positions = [];
// if (
// requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON" ||
// requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON_ROLE"
// ) {
// employeePosMaster.current_holderId = item.current_holderId;
// } else {
// // employeePosMaster.next_holderId = null;
// employeePosMaster.isSit = false;
// }
// if (
// requestBody.typeDraft.toUpperCase() == "ORG_POSITION_ROLE" ||
// requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON_ROLE"
// ) {
// employeePosMaster.authRoleId = item.authRoleId;
// } else {
// employeePosMaster.authRoleId = null;
// }
// employeePosMaster.current_holderId = null;
2025-03-11 23:37:05 +07:00
employeePosMaster.orgRevisionId = orgRevisionDraft.id;
employeePosMaster.orgRootId = dataId;
employeePosMaster.orgChild1Id = data1Id;
employeePosMaster.orgChild2Id = data2Id;
employeePosMaster.orgChild3Id = data3Id;
employeePosMaster.orgChild4Id = data4Id;
employeePosMaster.createdUserId = "";
employeePosMaster.createdFullName = "System Administrator";
employeePosMaster.createdAt = new Date();
employeePosMaster.lastUpdateUserId = "";
employeePosMaster.lastUpdateFullName = "System Administrator";
employeePosMaster.lastUpdatedAt = new Date();
await repoEmployeePosmaster.save(employeePosMaster);
//create employeePosition
item.positions.map(async (pos: any) => {
delete pos.id;
const employeePosition: EmployeePosition = Object.assign(
new EmployeePosition(),
pos,
);
employeePosition.posMasterId = employeePosMaster.id;
// if (
// requestBody.typeDraft.toUpperCase() == "ORG_POSITION" ||
// requestBody.typeDraft.toUpperCase() == "ORG_POSITION_ROLE"
// ) {
// employeePosition.positionIsSelected = false;
// }
employeePosition.createdUserId = "";
employeePosition.createdFullName = "System Administrator";
employeePosition.createdAt = new Date();
employeePosition.lastUpdateUserId = "";
employeePosition.lastUpdateFullName = "System Administrator";
employeePosition.lastUpdatedAt = new Date();
await employeePositionRepository.save(employeePosition);
});
}),
);
//create employeeTempPosmaster
await Promise.all(
_orgemployeeTempPosMaster
.filter((x: EmployeeTempPosMaster) => x.orgChild4Id == data4Id)
.map(async (item: any) => {
delete item.id;
const employeeTempPosMaster = Object.assign(
new EmployeeTempPosMaster(),
item,
);
employeeTempPosMaster.positions = [];
// if (
// requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON" ||
// requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON_ROLE"
// ) {
// employeeTempPosMaster.current_holderId = item.current_holderId;
// } else {
// // employeeTempPosMaster.next_holderId = null;
// employeeTempPosMaster.isSit = false;
// }
// if (
// requestBody.typeDraft.toUpperCase() == "ORG_POSITION_ROLE" ||
// requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON_ROLE"
// ) {
// employeeTempPosMaster.authRoleId = item.authRoleId;
// } else {
// employeeTempPosMaster.authRoleId = null;
// }
// employeeTempPosMaster.current_holderId = null;
2025-03-11 23:37:05 +07:00
employeeTempPosMaster.orgRevisionId = orgRevisionDraft.id;
employeeTempPosMaster.orgRootId = dataId;
employeeTempPosMaster.orgChild1Id = data1Id;
employeeTempPosMaster.orgChild2Id = data2Id;
employeeTempPosMaster.orgChild3Id = data3Id;
employeeTempPosMaster.orgChild4Id = data4Id;
employeeTempPosMaster.createdUserId = "";
employeeTempPosMaster.createdFullName = "System Administrator";
employeeTempPosMaster.createdAt = new Date();
employeeTempPosMaster.lastUpdateUserId = "";
employeeTempPosMaster.lastUpdateFullName = "System Administrator";
employeeTempPosMaster.lastUpdatedAt = new Date();
await repoEmployeeTempPosmaster.save(employeeTempPosMaster);
//create employeePosition
item.positions.map(async (pos: any) => {
delete pos.id;
const employeePosition: EmployeePosition = Object.assign(
new EmployeePosition(),
pos,
);
employeePosition.posMasterTempId = employeeTempPosMaster.id;
// if (
// requestBody.typeDraft.toUpperCase() == "ORG_POSITION" ||
// requestBody.typeDraft.toUpperCase() == "ORG_POSITION_ROLE"
// ) {
// employeePosition.positionIsSelected = false;
// }
employeePosition.createdUserId = "";
employeePosition.createdFullName = "System Administrator";
employeePosition.createdAt = new Date();
employeePosition.lastUpdateUserId = "";
employeePosition.lastUpdateFullName = "System Administrator";
employeePosition.lastUpdatedAt = new Date();
await employeePositionRepository.save(employeePosition);
});
}),
);
// }
});
});
});
});
});
// }
const employeePosMaster = await repoEmployeePosmaster.find({
2025-03-11 23:37:05 +07:00
where: { orgRevisionId: orgRevisionDraft.id },
relations: ["positions", "positions.posLevel", "positions.posType"],
});
for (const item of employeePosMaster) {
2025-03-12 12:15:13 +07:00
if (item.next_holderId != null) {
const profile = await repoProfileEmployee.findOne({
where: { id: item.next_holderId == null ? "" : item.next_holderId },
});
const position = await item.positions.find((x) => x.positionIsSelected == true);
const _null: any = null;
if (profile != null) {
profile.posLevelId = position?.posLevelId ?? _null;
profile.posTypeId = position?.posTypeId ?? _null;
profile.position = position?.positionName ?? _null;
await repoProfileEmployee.save(profile);
}
}
// item.current_holderId = item.next_holderId;
// item.next_holderId = null;
item.lastUpdateUserId = lastUpdateUserId;
item.lastUpdateFullName = lastUpdateFullName;
item.lastUpdatedAt = lastUpdatedAt;
await repoEmployeePosmaster.save(item).catch((e) => console.log(e));
}
const employeeTempPosMaster = await repoEmployeeTempPosmaster.find({
2025-03-11 23:37:05 +07:00
where: { orgRevisionId: orgRevisionDraft.id },
relations: ["positions", "positions.posLevel", "positions.posType"],
});
for (const item of employeeTempPosMaster) {
2025-03-12 12:15:13 +07:00
if (item.next_holderId != null) {
const profile = await repoProfileEmployee.findOne({
where: { id: item.next_holderId == null ? "" : item.next_holderId },
});
const position = await item.positions.find((x) => x.positionIsSelected == true);
const _null: any = null;
if (profile != null) {
profile.posLevelId = position?.posLevelId ?? _null;
profile.posTypeId = position?.posTypeId ?? _null;
profile.position = position?.positionName ?? _null;
await repoProfileEmployee.save(profile);
}
}
// item.current_holderId = item.next_holderId;
// item.next_holderId = null;
item.lastUpdateUserId = lastUpdateUserId;
item.lastUpdateFullName = lastUpdateFullName;
item.lastUpdatedAt = lastUpdatedAt;
await repoEmployeeTempPosmaster.save(item).catch((e) => console.log(e));
}
}
console.log("[AMQ] Excecute Organization Success");
return true;
} catch (error) {
console.error(error);
2024-10-30 10:49:58 +07:00
return false;
}
}