test เผยแพร่

This commit is contained in:
kittapath 2025-03-11 22:58:17 +07:00
parent f4c4804915
commit ce5a595bc6
4 changed files with 630 additions and 50 deletions

View file

@ -30,6 +30,9 @@ import { EmployeePosition } from "../entities/EmployeePosition";
import permission from "../interfaces/permission";
import { RequestWithUser } from "../middlewares/user";
import { setLogDataDiff } from "../interfaces/utils";
import { EmployeeTempPosMaster } from "../entities/EmployeeTempPosMaster";
import { Profile } from "../entities/Profile";
import { ProfileEmployee } from "../entities/ProfileEmployee";
@Route("api/v1/org/root")
@Tags("OrgRoot")
@ -332,17 +335,17 @@ export class OrgRootController extends Controller {
// if(chkShortChild1 != null){
// throw new HttpError(HttpStatusCode.NOT_FOUND, "อักษรย่อนี้ซ้ำกับอักษรย่อส่วนราชการ");
// }
const _null:any = null;
const _null: any = null;
const before = structuredClone(orgRoot);
orgRoot.lastUpdateUserId = request.user.sub;
orgRoot.lastUpdateFullName = request.user.name;
orgRoot.lastUpdatedAt = new Date();
this.orgRootRepository.merge(orgRoot, {
...requestBody,
DEPARTMENT_CODE: requestBody.DEPARTMENT_CODE != null ? requestBody.DEPARTMENT_CODE : _null,
DIVISION_CODE: requestBody.DIVISION_CODE != null ? requestBody.DIVISION_CODE : _null,
SECTION_CODE: requestBody.SECTION_CODE != null ? requestBody.SECTION_CODE : _null,
JOB_CODE: requestBody.JOB_CODE != null ? requestBody.JOB_CODE : _null,
DEPARTMENT_CODE: requestBody.DEPARTMENT_CODE != null ? requestBody.DEPARTMENT_CODE : _null,
DIVISION_CODE: requestBody.DIVISION_CODE != null ? requestBody.DIVISION_CODE : _null,
SECTION_CODE: requestBody.SECTION_CODE != null ? requestBody.SECTION_CODE : _null,
JOB_CODE: requestBody.JOB_CODE != null ? requestBody.JOB_CODE : _null,
});
await this.orgRootRepository.save(orgRoot, { data: request });
setLogDataDiff(request, { before, after: orgRoot });
@ -469,4 +472,570 @@ export class OrgRootController extends Controller {
return new HttpSuccess();
}
@Get("publish/employee")
async publishEmployee(@Request() request: RequestWithUser) {
const repoEmployeePosmaster = AppDataSource.getRepository(EmployeePosMaster);
const repoEmployeeTempPosmaster = AppDataSource.getRepository(EmployeeTempPosMaster);
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();
try {
if (orgRevisionPublish != null) {
//หา dna tree
const orgRoot = await orgRootRepository.find({
where: { orgRevisionId: orgRevisionPublish.id },
});
const orgChild1 = await child1Repository.find({
where: { orgRevisionId: orgRevisionPublish.id },
});
const orgChild2 = await child2Repository.find({
where: { orgRevisionId: orgRevisionPublish.id },
});
const orgChild3 = await child3Repository.find({
where: { orgRevisionId: orgRevisionPublish.id },
});
const orgChild4 = await child4Repository.find({
where: { orgRevisionId: orgRevisionPublish.id },
});
//หา dna posmaster ถ้าไม่มีให้เอาตัวเองเป็น dna
const orgemployeePosMaster = await repoEmployeePosmaster.find({
where: { orgRevisionId: orgRevisionPublish.id },
relations: ["positions"],
});
let _orgemployeePosMaster: EmployeePosMaster[];
_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[];
_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;
//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 = [];
employeePosMaster.orgRevisionId = orgRevisionPublish.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;
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 = [];
employeeTempPosMaster.orgRevisionId = orgRevisionPublish.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.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;
//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 = [];
employeePosMaster.orgRevisionId = orgRevisionPublish.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;
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 = [];
employeeTempPosMaster.orgRevisionId = orgRevisionPublish.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;
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;
//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 = [];
employeePosMaster.orgRevisionId = orgRevisionPublish.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;
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 = [];
employeeTempPosMaster.orgRevisionId = orgRevisionPublish.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;
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;
//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 = [];
employeePosMaster.orgRevisionId = orgRevisionPublish.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;
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 = [];
employeeTempPosMaster.orgRevisionId = orgRevisionPublish.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;
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;
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 = [];
employeePosMaster.orgRevisionId = orgRevisionPublish.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;
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 = [];
employeeTempPosMaster.orgRevisionId = orgRevisionPublish.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;
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({
where: { orgRevisionId: orgRevisionPublish.id },
relations: ["positions", "positions.posLevel", "positions.posType"],
});
for (const item of employeePosMaster) {
if (item.next_holderId != null && status == "NOW") {
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.lastUpdateUserId = request.user.sub;
item.lastUpdateFullName = request.user.name;
item.lastUpdatedAt = new Date();
}
const employeeTempPosMaster = await repoEmployeeTempPosmaster.find({
where: { orgRevisionId: orgRevisionPublish.id },
relations: ["positions", "positions.posLevel", "positions.posType"],
});
for (const item of employeeTempPosMaster) {
if (item.next_holderId != null && status == "NOW") {
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.lastUpdateUserId = request.user.sub;
item.lastUpdateFullName = request.user.name;
item.lastUpdatedAt = new Date();
await repoEmployeeTempPosmaster.save(item).catch((e) => console.log(e));
}
}
return new HttpSuccess();
} catch (error) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่สามารถสร้างโครงสร้างลูกจ้างได้");
}
}
}