Merge branch 'develop' of github.com:Frappet/bma-ehr-organization into develop

This commit is contained in:
Kittapath 2024-06-26 15:14:31 +07:00
commit 9bbbc4b14c

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 = {
posLevelId: profile?.posLevelId,
posTypeId: profile?.posTypeId,
keycloak: Not(IsNull()),
};
const caregiverQueryBuilder = 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) { const findChild4 = await this.profileRepo
caregiverQueryBuilder.andWhere("current_holders.orgRootId = :orgRootId", { .createQueryBuilder("profile")
orgRootId: orgRoot, .leftJoinAndSelect("profile.current_holders", "current_holders")
}); .leftJoinAndSelect("profile.posLevel", "posLevel")
} .leftJoinAndSelect("profile.posType", "posType")
if (orgChild1) { .andWhere({ keycloak: Not(IsNull()) })
caregiverQueryBuilder.andWhere("current_holders.orgRootId = :orgRootId", { .where("current_holders.current_holderId IS NOT NULL")
orgRootId: orgRoot, .andWhere("current_holders.orgRootId = :orgRootId", { orgRootId: orgRoot })
}); .andWhere("current_holders.current_holderId <> :current_holderId", { current_holderId: posMaster.current_holderId })
caregiverQueryBuilder.andWhere("current_holders.orgChild2Id = :orgChild1Id", { .getMany();
orgChild1Id: orgChild1, const findChild3 = await this.profileRepo
}); .createQueryBuilder("profile")
} .leftJoinAndSelect("profile.current_holders", "current_holders")
if (orgChild2) { .leftJoinAndSelect("profile.posLevel", "posLevel")
caregiverQueryBuilder.andWhere("current_holders.orgRootId = :orgRootId", { .leftJoinAndSelect("profile.posType", "posType")
orgRootId: orgRoot, .andWhere({ keycloak: Not(IsNull()) })
}); .where("current_holders.current_holderId IS NOT NULL")
caregiverQueryBuilder.andWhere("current_holders.orgChild2Id = :orgChild1Id", { .andWhere("current_holders.orgRootId = :orgRootId", { orgRootId: orgRoot })
orgChild1Id: orgChild1, .andWhere("current_holders.current_holderId <> :current_holderId", { current_holderId: posMaster.current_holderId })
}); .andWhere("current_holders.orgChild3Id = :orgChild3Id", { orgChild3Id: orgChild3 })
caregiverQueryBuilder.andWhere("current_holders.orgChild3Id = :orgChild2Id", { .andWhere("current_holders.orgChild4Id IS NULL")
orgChild2Id: orgChild2, .getMany();
}); const findChild2 = await this.profileRepo
} .createQueryBuilder("profile")
if (orgChild3) { .leftJoinAndSelect("profile.current_holders", "current_holders")
caregiverQueryBuilder.andWhere("current_holders.orgRootId = :orgRootId", { .leftJoinAndSelect("profile.posLevel", "posLevel")
orgRootId: orgRoot, .leftJoinAndSelect("profile.posType", "posType")
}); .andWhere({ keycloak: Not(IsNull()) })
caregiverQueryBuilder.andWhere("current_holders.orgChild1Id = :orgChild1Id", { .where("current_holders.current_holderId IS NOT NULL")
orgChild1Id: orgChild1, .andWhere("current_holders.orgRootId = :orgRootId", { orgRootId: orgRoot })
}); .andWhere("current_holders.current_holderId <> :current_holderId", { current_holderId: posMaster.current_holderId })
caregiverQueryBuilder.andWhere("current_holders.orgChild3Id = :orgChild2Id", { .andWhere("current_holders.orgChild2Id = :orgChild2Id", { orgChild2Id: orgChild2 })
orgChild2Id: orgChild2, .andWhere("current_holders.orgChild3Id IS NULL")
}); .getMany();
caregiverQueryBuilder.andWhere("current_holders.orgChild3Id = :orgChild3Id", { const findChild1 = await this.profileRepo
orgChild3Id: orgChild3, .createQueryBuilder("profile")
}); .leftJoinAndSelect("profile.current_holders", "current_holders")
} .leftJoinAndSelect("profile.posLevel", "posLevel")
if (orgChild4) { .leftJoinAndSelect("profile.posType", "posType")
caregiverQueryBuilder.andWhere("current_holders.orgRootId = :orgRootId", { .andWhere({ keycloak: Not(IsNull()) })
orgRootId: orgRoot, .where("current_holders.current_holderId IS NOT NULL")
}); .andWhere("current_holders.orgRootId = :orgRootId", { orgRootId: orgRoot })
caregiverQueryBuilder.andWhere("current_holders.orgChild1Id = :orgChild1Id", { .andWhere("current_holders.current_holderId <> :current_holderId", { current_holderId: posMaster.current_holderId })
orgChild1Id: orgChild1, .andWhere("current_holders.orgChild1Id = :orgChild1Id", { orgChild1Id: orgChild1 })
}); .andWhere("current_holders.orgChild2Id IS NULL")
caregiverQueryBuilder.andWhere("current_holders.orgChild2Id = :orgChild2Id", { .getMany();
orgChild2Id: orgChild2, const findRoot = await this.profileRepo
}); .createQueryBuilder("profile")
caregiverQueryBuilder.andWhere("current_holders.orgChild3Id = :orgChild3Id", { .leftJoinAndSelect("profile.current_holders", "current_holders")
orgChild3Id: orgChild3, .leftJoinAndSelect("profile.posLevel", "posLevel")
}); .leftJoinAndSelect("profile.posType", "posType")
caregiverQueryBuilder.andWhere("current_holders.orgChild4Id = :orgChild4Id", { .andWhere({ keycloak: Not(IsNull()) })
orgChild4Id: orgChild4, .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 _caregiver = await caregiverQueryBuilder.getMany(); let _caregiver: any;
let caregiver: any;
const commanderQueryBuilder = this.profileRepo let _commander: any;
.createQueryBuilder("profile") let commander: any;
.leftJoinAndSelect("profile.current_holders", "current_holders") let _chairman: any;
.leftJoinAndSelect("profile.posLevel", "posLevel") let chairman: any;
.leftJoinAndSelect("profile.posType", "posType") if (posMaster) {
.where("current_holders.current_holderId IS NOT NULL") if (orgChild4) {
.andWhere(profileConditions); if (findChild4) {
_caregiver = findChild4;
if (orgRoot) { _commander = findChild3;
commanderQueryBuilder.andWhere("current_holders.orgRootId = :orgRootId", { _chairman = findChild2;
orgRootId: orgRoot, } 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;
}
} else if (orgChild3) {
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;
}
} else if (orgChild2) {
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;
}
} else if (orgChild1) {
if (findChild1) {
_caregiver = findChild1;
_commander = findRoot;
_chairman = findRoot;
} else if (findRoot) {
_caregiver = findRoot;
_commander = findRoot;
_chairman = findRoot;
}
} else if (orgRoot) {
if (findRoot) {
_caregiver = findRoot;
_commander = findRoot;
_chairman = findRoot;
}
}
caregiver = _caregiver.map((_data:any) => ({
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,
}));
commander = _commander.map((_data:any) => ({
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,
}));
chairman = _chairman.map((_data:any) => ({
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,
}));
} }
if (orgChild1) {
commanderQueryBuilder.andWhere("current_holders.orgRootId = :orgRootId", {
orgRootId: orgRoot,
});
}
if (orgChild2) {
commanderQueryBuilder.andWhere("current_holders.orgRootId = :orgRootId", {
orgRootId: orgRoot,
});
commanderQueryBuilder.andWhere("current_holders.orgChild2Id = :orgChild1Id", {
orgChild1Id: orgChild1,
});
}
if (orgChild3) {
commanderQueryBuilder.andWhere("current_holders.orgRootId = :orgRootId", {
orgRootId: orgRoot,
});
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 });
} }