fix api commander

This commit is contained in:
AdisakKanthawilang 2024-06-26 15:10:07 +07:00
parent 2128857d10
commit 56a257e82f

View file

@ -577,217 +577,183 @@ export class ProfileController extends Controller {
where: { current_holderId: profile?.id }, where: { current_holderId: profile?.id },
}); });
if (!posMaster) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลการครองตำแหน่ง"); if (!posMaster) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลการครองตำแหน่ง");
const orgRoot = posMaster.orgRootId; const orgRoot = posMaster.orgRootId;
const orgChild1 = posMaster.orgChild1Id; const orgChild1 = posMaster.orgChild1Id;
const orgChild2 = posMaster.orgChild2Id; const orgChild2 = posMaster.orgChild2Id;
const orgChild3 = posMaster.orgChild3Id; const orgChild3 = posMaster.orgChild3Id;
const orgChild4 = posMaster.orgChild4Id; const orgChild4 = posMaster.orgChild4Id;
const profileConditions = { const findChild4 = await this.profileRepo
posLevelId: profile?.posLevelId, .createQueryBuilder("profile")
posTypeId: profile?.posTypeId, .leftJoinAndSelect("profile.current_holders", "current_holders")
keycloak: Not(IsNull()), .leftJoinAndSelect("profile.posLevel", "posLevel")
}; .leftJoinAndSelect("profile.posType", "posType")
.andWhere({ keycloak: Not(IsNull()) })
.where("current_holders.current_holderId IS NOT NULL")
.andWhere("current_holders.orgRootId = :orgRootId", { orgRootId: orgRoot })
.andWhere("current_holders.current_holderId <> :current_holderId", { current_holderId: posMaster.current_holderId })
.getMany();
const findChild3 = await this.profileRepo
.createQueryBuilder("profile")
.leftJoinAndSelect("profile.current_holders", "current_holders")
.leftJoinAndSelect("profile.posLevel", "posLevel")
.leftJoinAndSelect("profile.posType", "posType")
.andWhere({ keycloak: Not(IsNull()) })
.where("current_holders.current_holderId IS NOT NULL")
.andWhere("current_holders.orgRootId = :orgRootId", { orgRootId: orgRoot })
.andWhere("current_holders.current_holderId <> :current_holderId", { current_holderId: posMaster.current_holderId })
.andWhere("current_holders.orgChild3Id = :orgChild3Id", { orgChild3Id: orgChild3 })
.andWhere("current_holders.orgChild4Id IS NULL")
.getMany();
const findChild2 = await this.profileRepo
.createQueryBuilder("profile")
.leftJoinAndSelect("profile.current_holders", "current_holders")
.leftJoinAndSelect("profile.posLevel", "posLevel")
.leftJoinAndSelect("profile.posType", "posType")
.andWhere({ keycloak: Not(IsNull()) })
.where("current_holders.current_holderId IS NOT NULL")
.andWhere("current_holders.orgRootId = :orgRootId", { orgRootId: orgRoot })
.andWhere("current_holders.current_holderId <> :current_holderId", { current_holderId: posMaster.current_holderId })
.andWhere("current_holders.orgChild2Id = :orgChild2Id", { orgChild2Id: orgChild2 })
.andWhere("current_holders.orgChild3Id IS NULL")
.getMany();
const findChild1 = await this.profileRepo
.createQueryBuilder("profile")
.leftJoinAndSelect("profile.current_holders", "current_holders")
.leftJoinAndSelect("profile.posLevel", "posLevel")
.leftJoinAndSelect("profile.posType", "posType")
.andWhere({ keycloak: Not(IsNull()) })
.where("current_holders.current_holderId IS NOT NULL")
.andWhere("current_holders.orgRootId = :orgRootId", { orgRootId: orgRoot })
.andWhere("current_holders.current_holderId <> :current_holderId", { current_holderId: posMaster.current_holderId })
.andWhere("current_holders.orgChild1Id = :orgChild1Id", { orgChild1Id: orgChild1 })
.andWhere("current_holders.orgChild2Id IS NULL")
.getMany();
const findRoot = await this.profileRepo
.createQueryBuilder("profile")
.leftJoinAndSelect("profile.current_holders", "current_holders")
.leftJoinAndSelect("profile.posLevel", "posLevel")
.leftJoinAndSelect("profile.posType", "posType")
.andWhere({ keycloak: Not(IsNull()) })
.where("current_holders.current_holderId IS NOT NULL")
.andWhere("current_holders.orgRootId = :orgRootId", { orgRootId: orgRoot })
.andWhere("current_holders.current_holderId <> :current_holderId", { current_holderId: posMaster.current_holderId })
.andWhere("current_holders.orgChild1Id IS NULL")
.getMany();
const caregiverQueryBuilder = this.profileRepo let _caregiver: any;
.createQueryBuilder("profile") let caregiver: any;
.leftJoinAndSelect("profile.current_holders", "current_holders") let _commander: any;
.leftJoinAndSelect("profile.posLevel", "posLevel") let commander: any;
.leftJoinAndSelect("profile.posType", "posType") let _chairman: any;
.where("current_holders.current_holderId IS NOT NULL") let chairman: any;
.andWhere(profileConditions) if (posMaster) {
if (orgChild4) {
if (orgRoot) { if (findChild4) {
caregiverQueryBuilder.andWhere("current_holders.orgRootId = :orgRootId", { _caregiver = findChild4;
orgRootId: orgRoot, _commander = findChild3;
}); _chairman = findChild2;
} else if (findChild3) {
_caregiver = findChild3;
_commander = findChild2;
_chairman = findChild1;
} else if (findChild2) {
_caregiver = findChild2;
_commander = findChild1;
_chairman = findRoot;
} else if (findChild1) {
_caregiver = findChild1;
_commander = findRoot;
_chairman = findRoot;
} else if (findRoot) {
_caregiver = findRoot;
_commander = findRoot;
_chairman = findRoot;
} }
if (orgChild1) { } else if (orgChild3) {
caregiverQueryBuilder.andWhere("current_holders.orgRootId = :orgRootId", { if (findChild3) {
orgRootId: orgRoot, _caregiver = findChild3;
}); _commander = findChild2;
caregiverQueryBuilder.andWhere("current_holders.orgChild2Id = :orgChild1Id", { _chairman = findChild1;
orgChild1Id: orgChild1, } else if (findChild2) {
}); _caregiver = findChild2;
_commander = findChild1;
_chairman = findRoot;
} else if (findChild1) {
_caregiver = findChild1;
_commander = findRoot;
_chairman = findRoot;
} else if (findRoot) {
_caregiver = findRoot;
_commander = findRoot;
_chairman = findRoot;
} }
if (orgChild2) { } else if (orgChild2) {
caregiverQueryBuilder.andWhere("current_holders.orgRootId = :orgRootId", { if (findChild2) {
orgRootId: orgRoot, _caregiver = findChild2;
}); _commander = findChild1;
caregiverQueryBuilder.andWhere("current_holders.orgChild2Id = :orgChild1Id", { _chairman = findRoot;
orgChild1Id: orgChild1, } else if (findChild1) {
}); _caregiver = findChild1;
caregiverQueryBuilder.andWhere("current_holders.orgChild3Id = :orgChild2Id", { _commander = findRoot;
orgChild2Id: orgChild2, _chairman = findRoot;
}); } else if (findRoot) {
_caregiver = findRoot;
_commander = findRoot;
_chairman = findRoot;
} }
if (orgChild3) { } else if (orgChild1) {
caregiverQueryBuilder.andWhere("current_holders.orgRootId = :orgRootId", { if (findChild1) {
orgRootId: orgRoot, _caregiver = findChild1;
}); _commander = findRoot;
caregiverQueryBuilder.andWhere("current_holders.orgChild1Id = :orgChild1Id", { _chairman = findRoot;
orgChild1Id: orgChild1, } else if (findRoot) {
}); _caregiver = findRoot;
caregiverQueryBuilder.andWhere("current_holders.orgChild3Id = :orgChild2Id", { _commander = findRoot;
orgChild2Id: orgChild2, _chairman = findRoot;
});
caregiverQueryBuilder.andWhere("current_holders.orgChild3Id = :orgChild3Id", {
orgChild3Id: orgChild3,
});
} }
if (orgChild4) { } else if (orgRoot) {
caregiverQueryBuilder.andWhere("current_holders.orgRootId = :orgRootId", { if (findRoot) {
orgRootId: orgRoot, _caregiver = findRoot;
}); _commander = findRoot;
caregiverQueryBuilder.andWhere("current_holders.orgChild1Id = :orgChild1Id", { _chairman = findRoot;
orgChild1Id: orgChild1,
});
caregiverQueryBuilder.andWhere("current_holders.orgChild2Id = :orgChild2Id", {
orgChild2Id: orgChild2,
});
caregiverQueryBuilder.andWhere("current_holders.orgChild3Id = :orgChild3Id", {
orgChild3Id: orgChild3,
});
caregiverQueryBuilder.andWhere("current_holders.orgChild4Id = :orgChild4Id", {
orgChild4Id: orgChild4,
});
} }
}
const _caregiver = await caregiverQueryBuilder.getMany(); caregiver = _caregiver.map((_data:any) => ({
id: _data.id,
const commanderQueryBuilder = this.profileRepo prefix: _data.prefix,
.createQueryBuilder("profile") firstName: _data.firstName,
.leftJoinAndSelect("profile.current_holders", "current_holders") lastName: _data.lastName,
.leftJoinAndSelect("profile.posLevel", "posLevel") citizenId: _data.citizenId,
.leftJoinAndSelect("profile.posType", "posType") position: _data.position,
.where("current_holders.current_holderId IS NOT NULL") posLevel: _data.posLevel == null ? null : _data.posLevel.posLevelName,
.andWhere(profileConditions); posType: _data.posType == null ? null : _data.posType.posTypeName,
isDirector: true,
if (orgRoot) { }));
commanderQueryBuilder.andWhere("current_holders.orgRootId = :orgRootId", { commander = _commander.map((_data:any) => ({
orgRootId: orgRoot, id: _data.id,
}); prefix: _data.prefix,
} firstName: _data.firstName,
if (orgChild1) { lastName: _data.lastName,
commanderQueryBuilder.andWhere("current_holders.orgRootId = :orgRootId", { citizenId: _data.citizenId,
orgRootId: orgRoot, position: _data.position,
}); posLevel: _data.posLevel == null ? null : _data.posLevel.posLevelName,
} posType: _data.posType == null ? null : _data.posType.posTypeName,
if (orgChild2) { isDirector: true,
commanderQueryBuilder.andWhere("current_holders.orgRootId = :orgRootId", { }));
orgRootId: orgRoot, chairman = _chairman.map((_data:any) => ({
}); id: _data.id,
commanderQueryBuilder.andWhere("current_holders.orgChild2Id = :orgChild1Id", { prefix: _data.prefix,
orgChild1Id: orgChild1, firstName: _data.firstName,
}); lastName: _data.lastName,
} citizenId: _data.citizenId,
if (orgChild3) { position: _data.position,
commanderQueryBuilder.andWhere("current_holders.orgRootId = :orgRootId", { posLevel: _data.posLevel == null ? null : _data.posLevel.posLevelName,
orgRootId: orgRoot, posType: _data.posType == null ? null : _data.posType.posTypeName,
}); isDirector: true,
commanderQueryBuilder.andWhere("current_holders.orgChild1Id = :orgChild1Id", { }));
orgChild1Id: orgChild1,
});
commanderQueryBuilder.andWhere("current_holders.orgChild3Id = :orgChild2Id", {
orgChild2Id: orgChild2,
});
}
if (orgChild4) {
commanderQueryBuilder.andWhere("current_holders.orgRootId = :orgRootId", {
orgRootId: orgRoot,
});
commanderQueryBuilder.andWhere("current_holders.orgChild1Id = :orgChild1Id", {
orgChild1Id: orgChild1,
});
commanderQueryBuilder.andWhere("current_holders.orgChild2Id = :orgChild2Id", {
orgChild2Id: orgChild2,
});
commanderQueryBuilder.andWhere("current_holders.orgChild3Id = :orgChild3Id", {
orgChild3Id: orgChild3,
});
} }
const _commander = await commanderQueryBuilder.getMany();
const chairManQueryBuilder = this.profileRepo
.createQueryBuilder("profile")
.leftJoinAndSelect("profile.current_holders", "current_holders")
.leftJoinAndSelect("profile.posLevel", "posLevel")
.leftJoinAndSelect("profile.posType", "posType")
.where("current_holders.current_holderId IS NOT NULL")
.andWhere(profileConditions);
if (orgRoot) {
chairManQueryBuilder.andWhere("current_holders.orgRootId = :orgRootId", { orgRootId: orgRoot });
}
if (orgChild1) {
chairManQueryBuilder.andWhere("current_holders.orgRootId = :orgRootId", {
orgRootId: orgRoot,
});
}
if (orgChild2) {
chairManQueryBuilder.andWhere("current_holders.orgRootId = :orgRootId", {
orgRootId: orgRoot,
});
}
if (orgChild3) {
chairManQueryBuilder.andWhere("current_holders.orgRootId = :orgRootId", {
orgRootId: orgRoot,
});
chairManQueryBuilder.andWhere("current_holders.orgChild1Id = :orgChild1Id", {
orgChild1Id: orgChild1,
});
}
if (orgChild4) {
chairManQueryBuilder.andWhere("current_holders.orgRootId = :orgRootId", {
orgRootId: orgRoot,
});
chairManQueryBuilder.andWhere("current_holders.orgChild1Id = :orgChild1Id", {
orgChild1Id: orgChild1,
});
chairManQueryBuilder.andWhere("current_holders.orgChild2Id = :orgChild2Id", {
orgChild2Id: orgChild2,
});
}
const _chairman = await chairManQueryBuilder.getMany();
const caregiver = _caregiver.map((_data) => ({
id: _data.id,
prefix: _data.prefix,
firstName: _data.firstName,
lastName: _data.lastName,
citizenId: _data.citizenId,
position: _data.position,
posLevel: _data.posLevel == null ? null : _data.posLevel.posLevelName,
posType: _data.posType == null ? null : _data.posType.posTypeName,
isDirector: true,
}));
const commander = _commander.map((_data) => ({
id: _data.id,
prefix: _data.prefix,
firstName: _data.firstName,
lastName: _data.lastName,
citizenId: _data.citizenId,
position: _data.position,
posLevel: _data.posLevel == null ? null : _data.posLevel.posLevelName,
posType: _data.posType == null ? null : _data.posType.posTypeName,
isDirector: true,
}));
const chairman = _chairman.map((_data) => ({
id: _data.id,
prefix: _data.prefix,
firstName: _data.firstName,
lastName: _data.lastName,
citizenId: _data.citizenId,
position: _data.position,
posLevel: _data.posLevel == null ? null : _data.posLevel.posLevelName,
posType: _data.posType == null ? null : _data.posType.posTypeName,
isDirector: true,
}));
return new HttpSuccess({ caregiver, commander, chairman }); return new HttpSuccess({ caregiver, commander, chairman });
} }