rabbitMq draft test

This commit is contained in:
AdisakKanthawilang 2025-03-17 14:08:31 +07:00
parent a03ce9d55a
commit 283deb49f1
2 changed files with 682 additions and 630 deletions

View file

@ -29,7 +29,7 @@ import { RequestWithUser } from "../middlewares/user";
import permission from "../interfaces/permission";
import { PermissionOrg } from "../entities/PermissionOrg";
import { setLogDataDiff } from "../interfaces/utils";
import { sendToQueueOrg } from "../services/rabbitmq";
import { sendToQueueOrg, sendToQueueOrgDraft } from "../services/rabbitmq";
import { PosMasterAssign } from "../entities/PosMasterAssign";
import { PosMasterAct } from "../entities/PosMasterAct";
import { EmployeePosition } from "../entities/EmployeePosition";
@ -147,631 +147,19 @@ export class OrganizationController extends Controller {
revision.lastUpdatedAt = new Date();
await this.orgRevisionRepository.save(revision, { data: request });
setLogDataDiff(request, { before, after: revision });
//cone tree
if (
requestBody.typeDraft.toUpperCase() == "ORG" ||
requestBody.typeDraft.toUpperCase() == "ORG_POSITION" ||
requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON" ||
requestBody.typeDraft.toUpperCase() == "ORG_POSITION_ROLE" ||
requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON_ROLE"
) {
//cone by revisionId
if (requestBody.orgRevisionId == null)
throw new HttpError(HttpStatusCode.NOT_FOUND, "not found.");
const _revision = await this.orgRevisionRepository.findOne({
where: { id: requestBody.orgRevisionId },
});
if (!_revision) throw new HttpError(HttpStatusCode.NOT_FOUND, "not found.");
//หา dna tree ถ้าไม่มีให้เอาตัวเองเป็น dna
const orgRoot = await this.orgRootRepository.find({
where: { orgRevisionId: requestBody.orgRevisionId },
order:{orgRootOrder:"ASC"}
});
let _orgRoot: any = orgRoot.map((x) => ({
...x,
ancestorDNA:
x.ancestorDNA == null || x.ancestorDNA == "00000000-0000-0000-0000-000000000000"
? x.id
: x.ancestorDNA,
}));
await this.orgRootRepository.save(_orgRoot);
const orgChild1 = await this.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 this.child1Repository.save(_orgChild1);
const orgChild2 = await this.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 this.child2Repository.save(_orgChild2);
const orgChild3 = await this.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 this.child3Repository.save(_orgChild3);
const orgChild4 = await this.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 this.child4Repository.save(_orgChild4);
//หา dna posmaster ถ้าไม่มีให้เอาตัวเองเป็น dna
const orgPosMaster = await this.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,
ancestorDNA:
x.ancestorDNA == null || x.ancestorDNA == "00000000-0000-0000-0000-000000000000"
? x.id
: x.ancestorDNA,
}));
await this.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.user.sub;
data.createdFullName = request.user.name;
data.createdAt = new Date();
data.lastUpdateUserId = request.user.sub;
data.lastUpdateFullName = request.user.name;
data.lastUpdatedAt = new Date();
await this.orgRootRepository.save(data);
const orgTypes = new Set([
"ORG_POSITION",
"ORG_POSITION_PERSON",
"ORG_POSITION_ROLE",
"ORG_POSITION_PERSON_ROLE",
]);
if (orgTypes.has(requestBody.typeDraft.toUpperCase())) {
// Create posMaster
for await (const item of orgPosMaster.filter(
(x: PosMaster) => x.orgRootId == dataId && x.orgChild1Id == null,
) as any) {
delete item.id;
const posMaster: any = 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.createdUserId = request.user.sub;
posMaster.createdFullName = request.user.name;
posMaster.createdAt = new Date();
posMaster.lastUpdateUserId = request.user.sub;
posMaster.lastUpdateFullName = request.user.name;
posMaster.lastUpdatedAt = new Date();
await this.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.user.sub;
position.createdFullName = request.user.name;
position.createdAt = new Date();
position.lastUpdateUserId = request.user.sub;
position.lastUpdateFullName = request.user.name;
position.lastUpdatedAt = new Date();
await this.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.user.sub;
data1.createdFullName = request.user.name;
data1.createdAt = new Date();
data1.lastUpdateUserId = request.user.sub;
data1.lastUpdateFullName = request.user.name;
data1.lastUpdatedAt = new Date();
await this.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.user.sub;
posMaster.createdFullName = request.user.name;
posMaster.createdAt = new Date();
posMaster.lastUpdateUserId = request.user.sub;
posMaster.lastUpdateFullName = request.user.name;
posMaster.lastUpdatedAt = new Date();
await this.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.user.sub;
position.createdFullName = request.user.name;
position.createdAt = new Date();
position.lastUpdateUserId = request.user.sub;
position.lastUpdateFullName = request.user.name;
position.lastUpdatedAt = new Date();
await this.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.user.sub;
data2.createdFullName = request.user.name;
data2.createdAt = new Date();
data2.lastUpdateUserId = request.user.sub;
data2.lastUpdateFullName = request.user.name;
data2.lastUpdatedAt = new Date();
await this.child2Repository.save(data2);
if (orgTypes.has(requestBody.typeDraft.toUpperCase())) {
// Create posMaster
for await (const item of orgPosMaster.filter(
(x: PosMaster) => x.orgChild2Id == data2Id && x.orgChild3Id == 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.createdUserId = request.user.sub;
posMaster.createdFullName = request.user.name;
posMaster.createdAt = new Date();
posMaster.lastUpdateUserId = request.user.sub;
posMaster.lastUpdateFullName = request.user.name;
posMaster.lastUpdatedAt = new Date();
await this.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.user.sub;
position.createdFullName = request.user.name;
position.createdAt = new Date();
position.lastUpdateUserId = request.user.sub;
position.lastUpdateFullName = request.user.name;
position.lastUpdatedAt = new Date();
await this.positionRepository.save(position);
}
}
}
// 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.user.sub;
data3.createdFullName = request.user.name;
data3.createdAt = new Date();
data3.lastUpdateUserId = request.user.sub;
data3.lastUpdateFullName = request.user.name;
data3.lastUpdatedAt = new Date();
await this.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.user.sub;
posMaster.createdFullName = request.user.name;
posMaster.createdAt = new Date();
posMaster.lastUpdateUserId = request.user.sub;
posMaster.lastUpdateFullName = request.user.name;
posMaster.lastUpdatedAt = new Date();
await this.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.user.sub;
position.createdFullName = request.user.name;
position.createdAt = new Date();
position.lastUpdateUserId = request.user.sub;
position.lastUpdateFullName = request.user.name;
position.lastUpdatedAt = new Date();
await this.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.user.sub;
data4.createdFullName = request.user.name;
data4.createdAt = new Date();
data4.lastUpdateUserId = request.user.sub;
data4.lastUpdateFullName = request.user.name;
data4.lastUpdatedAt = new Date();
await this.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.user.sub;
posMaster.createdFullName = request.user.name;
posMaster.createdAt = new Date();
posMaster.lastUpdateUserId = request.user.sub;
posMaster.lastUpdateFullName = request.user.name;
posMaster.lastUpdatedAt = new Date();
await this.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.user.sub;
position.createdFullName = request.user.name;
position.createdAt = new Date();
position.lastUpdateUserId = request.user.sub;
position.lastUpdateFullName = request.user.name;
position.lastUpdatedAt = new Date();
await this.positionRepository.save(position);
}
}
}
}
}
}
}
}
const msg = {
data: {
requestBody: requestBody,
request: request.user,
revision: revision
},
}
try {
await sendToQueueOrgDraft(msg);
return { message: 'Draft is being created... Processing in the background.' };
} catch (error:any) {
return { message: 'Failed to process the draft. Please try again later.', error: error.message };
}
const _orgRevisions = await this.orgRevisionRepository.find({
where: [{ orgRevisionIsDraft: true, id: Not(revision.id) }],
});
const _roots = await this.orgRootRepository.find({
where: [{ orgRevisionId: In(_orgRevisions.map((x) => x.id)) }],
});
const _posMasters = await this.posMasterRepository.find({
where: [{ orgRevisionId: In(_orgRevisions.map((x) => x.id)) }],
});
const _employeePosMasters = await this.employeePosMasterRepository.find({
where: [{ orgRevisionId: In(_orgRevisions.map((x) => x.id)) }],
});
const _employeeTempPosMasters = await this.employeeTempPosMasterRepository.find({
where: [{ orgRevisionId: In(_orgRevisions.map((x) => x.id)) }],
});
await this.positionRepository.delete({ posMasterId: In(_posMasters.map((x) => x.id)) });
await this.employeePositionRepository.delete({
posMasterId: In(_employeePosMasters.map((x) => x.id)),
});
await this.employeePositionRepository.delete({
posMasterTempId: In(_employeeTempPosMasters.map((x) => x.id)),
});
await this.posMasterAssignRepository.delete({ posMasterId: In(_posMasters.map((x) => x.id)) });
await this.posMasterActRepository.delete({ posMasterId: In(_posMasters.map((x) => x.id)) });
await this.posMasterActRepository.delete({
posMasterChildId: In(_posMasters.map((x) => x.id)),
});
await this.posMasterRepository.remove(_posMasters);
await this.employeePosMasterRepository.remove(_employeePosMasters);
await this.employeeTempPosMasterRepository.remove(_employeeTempPosMasters);
await this.child4Repository.delete({ orgRevisionId: In(_orgRevisions.map((x) => x.id)) });
await this.child3Repository.delete({ orgRevisionId: In(_orgRevisions.map((x) => x.id)) });
await this.child2Repository.delete({ orgRevisionId: In(_orgRevisions.map((x) => x.id)) });
await this.child1Repository.delete({ orgRevisionId: In(_orgRevisions.map((x) => x.id)) });
await this.permissionOrgRepository.delete({
orgRootId: In(_roots.map((x) => x.id)),
});
await this.orgRootRepository.delete({ orgRevisionId: In(_orgRevisions.map((x) => x.id)) });
await this.orgRevisionRepository.remove(_orgRevisions);
return new HttpSuccess(revision);
}
/**