สร้างโครงสร้าง+ตำแหน่ง

This commit is contained in:
Kittapath 2024-02-01 09:27:38 +07:00
parent cd21c42710
commit 9ac7517518

View file

@ -27,6 +27,9 @@ import { OrgRoot } from "../entities/OrgRoot";
import { OrgChild2 } from "../entities/OrgChild2";
import { OrgChild3 } from "../entities/OrgChild3";
import { OrgChild4 } from "../entities/OrgChild4";
import { PosMaster } from "../entities/PosMaster";
import { Position } from "../entities/Position";
import { log } from "console";
@Route("api/v1/org")
@Tags("Organization")
@ -43,6 +46,8 @@ export class OrganizationController extends Controller {
private child2Repository = AppDataSource.getRepository(OrgChild2);
private child3Repository = AppDataSource.getRepository(OrgChild3);
private child4Repository = AppDataSource.getRepository(OrgChild4);
private posMasterRepository = AppDataSource.getRepository(PosMaster);
private positionRepository = AppDataSource.getRepository(Position);
/**
* API
@ -128,7 +133,11 @@ export class OrganizationController extends Controller {
revision.lastUpdateFullName = request.user.name;
await this.orgRevisionRepository.save(revision);
if (requestBody.typeDraft.toUpperCase() == "ORG") {
if (
requestBody.typeDraft.toUpperCase() == "ORG" ||
requestBody.typeDraft.toUpperCase() == "ORG_POSITION" ||
requestBody.typeDraft.toUpperCase() == "ORG"
) {
if (requestBody.orgRevisionId == null)
throw new HttpError(HttpStatusCode.NOT_FOUND, "not found.");
const _revision = await this.orgRevisionRepository.findOne({
@ -139,6 +148,7 @@ export class OrganizationController extends Controller {
//clone data
const orgRoot = await this.orgRootRepository.find({
where: { orgRevisionId: requestBody.orgRevisionId },
relations: ["posMasters"],
});
const _orgRoot = orgRoot.map((x) => ({
...x,
@ -154,7 +164,9 @@ export class OrganizationController extends Controller {
//clone data
const orgChild1 = await this.child1Repository.find({
where: { orgRootId: dataId, orgRevisionId: requestBody.orgRevisionId },
relations: ["posMasters"],
});
const _orgChild1 = orgChild1.map((x) => ({
...x,
isAncestorDNA:
@ -174,6 +186,51 @@ export class OrganizationController extends Controller {
data.lastUpdateFullName = request.user.name;
data.lastUpdatedAt = new Date();
await this.orgRootRepository.save(data);
if (requestBody.typeDraft.toUpperCase() == "ORG_POSITION") {
const _posMaster = x.posMasters.map((item: any) => ({
...item,
isAncestorDNA:
item.isAncestorDNA == null ||
item.isAncestorDNA == "00000000-0000-0000-0000-000000000000"
? item.id
: item.isAncestorDNA,
}));
await this.posMasterRepository.save(_posMaster);
await Promise.all(
x.posMasters.map(async (item: any) => {
var masterId = x.id;
delete item.id;
const posMaster = Object.assign(new PosMaster(), item);
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);
const positions = await this.positionRepository.find({
where: { posMasterId: masterId },
});
await Promise.all(
positions.map(async (pos: any) => {
delete pos.id;
const position = Object.assign(new Position(), pos);
position.posMasterId = posMaster.id;
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);
}),
);
}),
);
}
_orgChild1.forEach(async (x: any) => {
var data1Id = x.id;
@ -184,6 +241,7 @@ export class OrganizationController extends Controller {
orgChild1Id: data1Id,
orgRevisionId: requestBody.orgRevisionId,
},
relations: ["posMasters"],
});
const _orgChild2 = orgChild2.map((x) => ({
...x,
@ -205,6 +263,53 @@ export class OrganizationController extends Controller {
data1.lastUpdateFullName = request.user.name;
data1.lastUpdatedAt = new Date();
await this.child1Repository.save(data1);
if (requestBody.typeDraft.toUpperCase() == "ORG_POSITION") {
const _posMaster = x.posMasters.map((item: any) => ({
...item,
isAncestorDNA:
item.isAncestorDNA == null ||
item.isAncestorDNA == "00000000-0000-0000-0000-000000000000"
? item.id
: item.isAncestorDNA,
}));
await this.posMasterRepository.save(_posMaster);
await Promise.all(
x.posMasters.map(async (item: any) => {
var masterId = x.id;
delete item.id;
const posMaster = Object.assign(new PosMaster(), item);
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);
const positions = await this.positionRepository.find({
where: { posMasterId: masterId },
});
await Promise.all(
positions.map(async (pos: any) => {
delete pos.id;
const position = Object.assign(new Position(), pos);
position.posMasterId = posMaster.id;
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);
}),
);
}),
);
}
_orgChild2.forEach(async (x: any) => {
var data2Id = x.id;
@ -216,6 +321,7 @@ export class OrganizationController extends Controller {
orgChild2Id: data2Id,
orgRevisionId: requestBody.orgRevisionId,
},
relations: ["posMasters"],
});
const _orgChild3 = orgChild3.map((x) => ({
...x,
@ -239,6 +345,54 @@ export class OrganizationController extends Controller {
data2.lastUpdateFullName = request.user.name;
data2.lastUpdatedAt = new Date();
await this.child2Repository.save(data2);
if (requestBody.typeDraft.toUpperCase() == "ORG_POSITION") {
const _posMaster = x.posMasters.map((item: any) => ({
...item,
isAncestorDNA:
item.isAncestorDNA == null ||
item.isAncestorDNA == "00000000-0000-0000-0000-000000000000"
? item.id
: item.isAncestorDNA,
}));
await this.posMasterRepository.save(_posMaster);
await Promise.all(
x.posMasters.map(async (item: any) => {
var masterId = x.id;
delete item.id;
const posMaster = Object.assign(new PosMaster(), item);
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);
const positions = await this.positionRepository.find({
where: { posMasterId: masterId },
});
await Promise.all(
positions.map(async (pos: any) => {
delete pos.id;
const position = Object.assign(new Position(), pos);
position.posMasterId = posMaster.id;
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);
}),
);
}),
);
}
_orgChild3.forEach(async (x: any) => {
var data3Id = x.id;
@ -251,6 +405,7 @@ export class OrganizationController extends Controller {
orgChild3Id: data3Id,
orgRevisionId: requestBody.orgRevisionId,
},
relations: ["posMasters"],
});
const _orgChild4 = orgChild4.map((x) => ({
...x,
@ -275,6 +430,55 @@ export class OrganizationController extends Controller {
data3.lastUpdateFullName = request.user.name;
data3.lastUpdatedAt = new Date();
await this.child3Repository.save(data3);
if (requestBody.typeDraft.toUpperCase() == "ORG_POSITION") {
const _posMaster = x.posMasters.map((item: any) => ({
...item,
isAncestorDNA:
item.isAncestorDNA == null ||
item.isAncestorDNA == "00000000-0000-0000-0000-000000000000"
? item.id
: item.isAncestorDNA,
}));
await this.posMasterRepository.save(_posMaster);
await Promise.all(
x.posMasters.map(async (item: any) => {
var masterId = x.id;
delete item.id;
const posMaster = Object.assign(new PosMaster(), item);
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);
const positions = await this.positionRepository.find({
where: { posMasterId: masterId },
});
await Promise.all(
positions.map(async (pos: any) => {
delete pos.id;
const position = Object.assign(new Position(), pos);
position.posMasterId = posMaster.id;
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);
}),
);
}),
);
}
_orgChild4.forEach(async (x: any) => {
delete x.id;
@ -291,118 +495,77 @@ export class OrganizationController extends Controller {
data4.lastUpdateFullName = request.user.name;
data4.lastUpdatedAt = new Date();
await this.child4Repository.save(data4);
if (requestBody.typeDraft.toUpperCase() == "ORG_POSITION") {
const _posMaster = x.posMasters.map((item: any) => ({
...item,
isAncestorDNA:
item.isAncestorDNA == null ||
item.isAncestorDNA == "00000000-0000-0000-0000-000000000000"
? item.id
: item.isAncestorDNA,
}));
await this.posMasterRepository.save(_posMaster);
await Promise.all(
x.posMasters.map(async (item: any) => {
var masterId = x.id;
delete item.id;
const posMaster = Object.assign(new PosMaster(), item);
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);
const positions = await this.positionRepository.find({
where: { posMasterId: masterId },
});
await Promise.all(
positions.map(async (pos: any) => {
delete pos.id;
const position = Object.assign(new Position(), pos);
position.posMasterId = posMaster.id;
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);
}),
);
}),
);
}
});
});
});
});
});
// //clone data
// const orgChild1 = await this.child1Repository.find({
// where: { orgRevisionId: requestBody.orgRevisionId },
// });
// const _orgChild1 = orgChild1.map((x) => ({
// ...x,
// isAncestorDNA:
// x.isAncestorDNA == null || x.isAncestorDNA == "00000000-0000-0000-0000-000000000000"
// ? x.id
// : x.isAncestorDNA,
// }));
// await this.child1Repository.save(_orgChild1);
// _orgChild1.forEach(async (x: any) => {
// delete x.id;
// const data = Object.assign(new OrgChild1(), x);
// 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.child1Repository.save(data);
// });
// //clone data
// const orgChild2 = await this.child2Repository.find({
// where: { orgRevisionId: requestBody.orgRevisionId },
// });
// const _orgChild2 = orgChild2.map((x) => ({
// ...x,
// isAncestorDNA:
// x.isAncestorDNA == null || x.isAncestorDNA == "00000000-0000-0000-0000-000000000000"
// ? x.id
// : x.isAncestorDNA,
// }));
// await this.child2Repository.save(_orgChild2);
// _orgChild2.forEach(async (x: any) => {
// delete x.id;
// const data = Object.assign(new OrgChild2(), x);
// 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.child2Repository.save(data);
// });
// //clone data
// const orgChild3 = await this.child3Repository.find({
// where: { orgRevisionId: requestBody.orgRevisionId },
// });
// const _orgChild3 = orgChild3.map((x) => ({
// ...x,
// isAncestorDNA:
// x.isAncestorDNA == null || x.isAncestorDNA == "00000000-0000-0000-0000-000000000000"
// ? x.id
// : x.isAncestorDNA,
// }));
// await this.child3Repository.save(_orgChild3);
// _orgChild3.forEach(async (x: any) => {
// delete x.id;
// const data = Object.assign(new OrgChild3(), x);
// 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.child3Repository.save(data);
// // });
// //clone data
// const orgChild4 = await this.child4Repository.find({
// where: { orgRevisionId: requestBody.orgRevisionId },
// });
// const _orgChild4 = orgChild4.map((x) => ({
// ...x,
// isAncestorDNA:
// x.isAncestorDNA == null || x.isAncestorDNA == "00000000-0000-0000-0000-000000000000"
// ? x.id
// : x.isAncestorDNA,
// }));
// await this.child4Repository.save(_orgChild4);
// _orgChild4.forEach(async (x: any) => {
// delete x.id;
// const data = Object.assign(new OrgChild4(), x);
// 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.child4Repository.save(data);
// });
} else if (requestBody.typeDraft.toUpperCase() == "ORG_POSITION") {
} else if (requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON") {
}
// else if (requestBody.typeDraft.toUpperCase() == "ORG_POSITION") {
// } else if (requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON") {
// }
const orgRevisions = await this.orgRevisionRepository.find({
where: [{ orgRevisionIsDraft: true, id: Not(revision.id) }],
});
const posMasters = await this.posMasterRepository.find({
where: [{ orgRevisionId: In(orgRevisions.map((x) => x.id)) }],
});
const positions = await this.positionRepository.find({
where: [{ posMasterId: In(posMasters.map((x) => x.id)) }],
});
await this.positionRepository.remove(positions);
await this.posMasterRepository.remove(posMasters);
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)) });