Merge branch 'develop' into adiDev

# Conflicts:
#	src/controllers/OrganizationController.ts
This commit is contained in:
AdisakKanthawilang 2024-09-06 12:01:25 +07:00
commit 59ef92c74d
16 changed files with 593 additions and 420 deletions

View file

@ -28,7 +28,6 @@ import { Position } from "../entities/Position";
import CallAPI from "../interfaces/call-api";
import { ProfileSalary } from "../entities/ProfileSalary";
import { Profile } from "../entities/Profile";
import { request } from "axios";
import { RequestWithUser } from "../middlewares/user";
import permission from "../interfaces/permission";
@ -121,6 +120,7 @@ export class OrganizationController extends Controller {
@Body() requestBody: CreateOrgRevision,
@Request() request: { user: Record<string, any> },
) {
//new main revision
const revision = Object.assign(new OrgRevision(), requestBody) as OrgRevision;
revision.orgRevisionIsDraft = true;
revision.orgRevisionIsCurrent = false;
@ -132,11 +132,13 @@ export class OrganizationController extends Controller {
revision.lastUpdatedAt = new Date();
await this.orgRevisionRepository.save(revision);
//cone tree
if (
requestBody.typeDraft.toUpperCase() == "ORG" ||
requestBody.typeDraft.toUpperCase() == "ORG_POSITION" ||
requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON"
) {
//cone by revisionId
if (requestBody.orgRevisionId == null)
throw new HttpError(HttpStatusCode.NOT_FOUND, "not found.");
const _revision = await this.orgRevisionRepository.findOne({
@ -144,8 +146,7 @@ export class OrganizationController extends Controller {
});
if (!_revision) throw new HttpError(HttpStatusCode.NOT_FOUND, "not found.");
let positions: Position[] = [];
//clone data
//หา dna tree ถ้าไม่มีให้เอาตัวเองเป็น dna
const orgRoot = await this.orgRootRepository.find({
where: { orgRevisionId: requestBody.orgRevisionId },
});
@ -206,6 +207,7 @@ export class OrganizationController extends Controller {
}));
await this.child4Repository.save(_orgChild4);
//หา dna posmaster ถ้าไม่มีให้เอาตัวเองเป็น dna
const orgPosMaster = await this.posMasterRepository.find({
where: { orgRevisionId: requestBody.orgRevisionId },
relations: ["positions"],
@ -224,7 +226,7 @@ export class OrganizationController extends Controller {
}));
await this.posMasterRepository.save(_orgPosMaster);
}
//create org
_orgRoot.forEach(async (x: any) => {
var dataId = x.id;
delete x.id;
@ -241,6 +243,7 @@ export class OrganizationController extends Controller {
requestBody.typeDraft.toUpperCase() == "ORG_POSITION" ||
requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON"
) {
//create posmaster
await Promise.all(
_orgPosMaster
.filter((x: PosMaster) => x.orgRootId == dataId && x.orgChild1Id == null)
@ -265,6 +268,7 @@ export class OrganizationController extends Controller {
posMaster.lastUpdatedAt = new Date();
await this.posMasterRepository.save(posMaster);
//create position
item.positions.map(async (pos: any) => {
delete pos.id;
const position = Object.assign(new Position(), pos);
@ -278,13 +282,13 @@ export class OrganizationController extends Controller {
position.lastUpdateUserId = request.user.sub;
position.lastUpdateFullName = request.user.name;
position.lastUpdatedAt = new Date();
// positions.push(position);
await this.positionRepository.save(position);
});
}),
);
}
//create org
_orgChild1
.filter((x: OrgChild1) => x.orgRootId == dataId)
.forEach(async (x: any) => {
@ -304,6 +308,7 @@ export class OrganizationController extends Controller {
requestBody.typeDraft.toUpperCase() == "ORG_POSITION" ||
requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON"
) {
//create posmaster
await Promise.all(
_orgPosMaster
.filter((x: PosMaster) => x.orgChild1Id == data1Id && x.orgChild2Id == null)
@ -329,6 +334,7 @@ export class OrganizationController extends Controller {
posMaster.lastUpdatedAt = new Date();
await this.posMasterRepository.save(posMaster);
//create position
item.positions.map(async (pos: any) => {
delete pos.id;
const position = Object.assign(new Position(), pos);
@ -342,13 +348,13 @@ export class OrganizationController extends Controller {
position.lastUpdateUserId = request.user.sub;
position.lastUpdateFullName = request.user.name;
position.lastUpdatedAt = new Date();
// positions.push(position);
await this.positionRepository.save(position);
});
}),
);
}
//create org
_orgChild2
.filter((x: OrgChild2) => x.orgChild1Id == data1Id)
.forEach(async (x: any) => {
@ -369,6 +375,7 @@ export class OrganizationController extends Controller {
requestBody.typeDraft.toUpperCase() == "ORG_POSITION" ||
requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON"
) {
//create posmaster
await Promise.all(
_orgPosMaster
.filter((x: PosMaster) => x.orgChild2Id == data2Id && x.orgChild3Id == null)
@ -395,6 +402,7 @@ export class OrganizationController extends Controller {
posMaster.lastUpdatedAt = new Date();
await this.posMasterRepository.save(posMaster);
//create position
item.positions.map(async (pos: any) => {
delete pos.id;
const position = Object.assign(new Position(), pos);
@ -408,13 +416,13 @@ export class OrganizationController extends Controller {
position.lastUpdateUserId = request.user.sub;
position.lastUpdateFullName = request.user.name;
position.lastUpdatedAt = new Date();
// positions.push(position);
await this.positionRepository.save(position);
});
}),
);
}
//create org
_orgChild3
.filter((x: OrgChild3) => x.orgChild2Id == data2Id)
.forEach(async (x: any) => {
@ -436,6 +444,7 @@ export class OrganizationController extends Controller {
requestBody.typeDraft.toUpperCase() == "ORG_POSITION" ||
requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON"
) {
//create posmaster
await Promise.all(
_orgPosMaster
.filter(
@ -465,6 +474,7 @@ export class OrganizationController extends Controller {
posMaster.lastUpdatedAt = new Date();
await this.posMasterRepository.save(posMaster);
//create position
item.positions.map(async (pos: any) => {
delete pos.id;
const position = Object.assign(new Position(), pos);
@ -478,13 +488,13 @@ export class OrganizationController extends Controller {
position.lastUpdateUserId = request.user.sub;
position.lastUpdateFullName = request.user.name;
position.lastUpdatedAt = new Date();
// positions.push(position);
await this.positionRepository.save(position);
});
}),
);
}
//create org
_orgChild4
.filter((x: OrgChild4) => x.orgChild3Id == data3Id)
.forEach(async (x: any) => {
@ -535,6 +545,7 @@ export class OrganizationController extends Controller {
posMaster.lastUpdatedAt = new Date();
await this.posMasterRepository.save(posMaster);
//create position
item.positions.map(async (pos: any) => {
delete pos.id;
const position = Object.assign(new Position(), pos);
@ -548,7 +559,6 @@ export class OrganizationController extends Controller {
position.lastUpdateUserId = request.user.sub;
position.lastUpdateFullName = request.user.name;
position.lastUpdatedAt = new Date();
// positions.push(position);
await this.positionRepository.save(position);
});
}),
@ -559,7 +569,6 @@ export class OrganizationController extends Controller {
});
});
});
// await this.positionRepository.save(positions);
}
const _orgRevisions = await this.orgRevisionRepository.find({
@ -606,7 +615,7 @@ export class OrganizationController extends Controller {
if (!orgRevision) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล");
}
const orgRootData = await AppDataSource.getRepository(OrgRoot)
.createQueryBuilder("orgRoot")
.where("orgRoot.orgRevisionId = :id", { id })
@ -3434,7 +3443,7 @@ export class OrganizationController extends Controller {
where: { orgRevisionIsCurrent: true, orgRevisionIsDraft: false },
});
if (!orgRevisionActive) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบโครงสร้างที่เผยแพรอยู่ตอนนี้");
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบโครงสร้างที่เผยแพรอยู่ตอนนี้");
}
const data = await this.orgRootRepository.find({
@ -3476,7 +3485,7 @@ export class OrganizationController extends Controller {
where: { orgRevisionIsCurrent: true, orgRevisionIsDraft: false },
});
if (!orgRevisionActive) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบโครงสร้างที่เผยแพรอยู่ตอนนี้");
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบโครงสร้างที่เผยแพรอยู่ตอนนี้");
}
const data = await this.orgRootRepository.find({
@ -3497,7 +3506,7 @@ export class OrganizationController extends Controller {
where: { orgRevisionIsCurrent: true, orgRevisionIsDraft: false },
});
if (!orgRevisionActive) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบโครงสร้างที่เผยแพรอยู่ตอนนี้");
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบโครงสร้างที่เผยแพรอยู่ตอนนี้");
}
return new HttpSuccess(orgRevisionActive.id);
}