fix api commander

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

View file

@ -577,185 +577,149 @@ 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,
posTypeId: profile?.posTypeId,
keycloak: Not(IsNull()),
};
const caregiverQueryBuilder = this.profileRepo
.createQueryBuilder("profile") .createQueryBuilder("profile")
.leftJoinAndSelect("profile.current_holders", "current_holders") .leftJoinAndSelect("profile.current_holders", "current_holders")
.leftJoinAndSelect("profile.posLevel", "posLevel") .leftJoinAndSelect("profile.posLevel", "posLevel")
.leftJoinAndSelect("profile.posType", "posType") .leftJoinAndSelect("profile.posType", "posType")
.andWhere({ keycloak: Not(IsNull()) })
.where("current_holders.current_holderId IS NOT NULL") .where("current_holders.current_holderId IS NOT NULL")
.andWhere(profileConditions) .andWhere("current_holders.orgRootId = :orgRootId", { orgRootId: orgRoot })
.andWhere("current_holders.current_holderId <> :current_holderId", { current_holderId: posMaster.current_holderId })
if (orgRoot) { .getMany();
caregiverQueryBuilder.andWhere("current_holders.orgRootId = :orgRootId", { const findChild3 = await this.profileRepo
orgRootId: orgRoot,
});
}
if (orgChild1) {
caregiverQueryBuilder.andWhere("current_holders.orgRootId = :orgRootId", {
orgRootId: orgRoot,
});
caregiverQueryBuilder.andWhere("current_holders.orgChild2Id = :orgChild1Id", {
orgChild1Id: orgChild1,
});
}
if (orgChild2) {
caregiverQueryBuilder.andWhere("current_holders.orgRootId = :orgRootId", {
orgRootId: orgRoot,
});
caregiverQueryBuilder.andWhere("current_holders.orgChild2Id = :orgChild1Id", {
orgChild1Id: orgChild1,
});
caregiverQueryBuilder.andWhere("current_holders.orgChild3Id = :orgChild2Id", {
orgChild2Id: orgChild2,
});
}
if (orgChild3) {
caregiverQueryBuilder.andWhere("current_holders.orgRootId = :orgRootId", {
orgRootId: orgRoot,
});
caregiverQueryBuilder.andWhere("current_holders.orgChild1Id = :orgChild1Id", {
orgChild1Id: orgChild1,
});
caregiverQueryBuilder.andWhere("current_holders.orgChild3Id = :orgChild2Id", {
orgChild2Id: orgChild2,
});
caregiverQueryBuilder.andWhere("current_holders.orgChild3Id = :orgChild3Id", {
orgChild3Id: orgChild3,
});
}
if (orgChild4) {
caregiverQueryBuilder.andWhere("current_holders.orgRootId = :orgRootId", {
orgRootId: orgRoot,
});
caregiverQueryBuilder.andWhere("current_holders.orgChild1Id = :orgChild1Id", {
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();
const commanderQueryBuilder = this.profileRepo
.createQueryBuilder("profile") .createQueryBuilder("profile")
.leftJoinAndSelect("profile.current_holders", "current_holders") .leftJoinAndSelect("profile.current_holders", "current_holders")
.leftJoinAndSelect("profile.posLevel", "posLevel") .leftJoinAndSelect("profile.posLevel", "posLevel")
.leftJoinAndSelect("profile.posType", "posType") .leftJoinAndSelect("profile.posType", "posType")
.andWhere({ keycloak: Not(IsNull()) })
.where("current_holders.current_holderId IS NOT NULL") .where("current_holders.current_holderId IS NOT NULL")
.andWhere(profileConditions); .andWhere("current_holders.orgRootId = :orgRootId", { orgRootId: orgRoot })
.andWhere("current_holders.current_holderId <> :current_holderId", { current_holderId: posMaster.current_holderId })
if (orgRoot) { .andWhere("current_holders.orgChild3Id = :orgChild3Id", { orgChild3Id: orgChild3 })
commanderQueryBuilder.andWhere("current_holders.orgRootId = :orgRootId", { .andWhere("current_holders.orgChild4Id IS NULL")
orgRootId: orgRoot, .getMany();
}); const findChild2 = await this.profileRepo
}
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") .createQueryBuilder("profile")
.leftJoinAndSelect("profile.current_holders", "current_holders") .leftJoinAndSelect("profile.current_holders", "current_holders")
.leftJoinAndSelect("profile.posLevel", "posLevel") .leftJoinAndSelect("profile.posLevel", "posLevel")
.leftJoinAndSelect("profile.posType", "posType") .leftJoinAndSelect("profile.posType", "posType")
.andWhere({ keycloak: Not(IsNull()) })
.where("current_holders.current_holderId IS NOT NULL") .where("current_holders.current_holderId IS NOT NULL")
.andWhere(profileConditions); .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();
if (orgRoot) { let _caregiver: any;
chairManQueryBuilder.andWhere("current_holders.orgRootId = :orgRootId", { orgRootId: orgRoot }); let caregiver: any;
} let _commander: any;
if (orgChild1) { let commander: any;
chairManQueryBuilder.andWhere("current_holders.orgRootId = :orgRootId", { let _chairman: any;
orgRootId: orgRoot, let chairman: any;
}); if (posMaster) {
}
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) { if (orgChild4) {
chairManQueryBuilder.andWhere("current_holders.orgRootId = :orgRootId", { if (findChild4) {
orgRootId: orgRoot, _caregiver = findChild4;
}); _commander = findChild3;
chairManQueryBuilder.andWhere("current_holders.orgChild1Id = :orgChild1Id", { _chairman = findChild2;
orgChild1Id: orgChild1, } else if (findChild3) {
}); _caregiver = findChild3;
chairManQueryBuilder.andWhere("current_holders.orgChild2Id = :orgChild2Id", { _commander = findChild2;
orgChild2Id: orgChild2, _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) {
const _chairman = await chairManQueryBuilder.getMany(); if (findChild3) {
_caregiver = findChild3;
_commander = findChild2;
_chairman = findChild1;
const caregiver = _caregiver.map((_data) => ({ } 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, id: _data.id,
prefix: _data.prefix, prefix: _data.prefix,
firstName: _data.firstName, firstName: _data.firstName,
@ -766,7 +730,7 @@ export class ProfileController extends Controller {
posType: _data.posType == null ? null : _data.posType.posTypeName, posType: _data.posType == null ? null : _data.posType.posTypeName,
isDirector: true, isDirector: true,
})); }));
const commander = _commander.map((_data) => ({ commander = _commander.map((_data:any) => ({
id: _data.id, id: _data.id,
prefix: _data.prefix, prefix: _data.prefix,
firstName: _data.firstName, firstName: _data.firstName,
@ -777,7 +741,7 @@ export class ProfileController extends Controller {
posType: _data.posType == null ? null : _data.posType.posTypeName, posType: _data.posType == null ? null : _data.posType.posTypeName,
isDirector: true, isDirector: true,
})); }));
const chairman = _chairman.map((_data) => ({ chairman = _chairman.map((_data:any) => ({
id: _data.id, id: _data.id,
prefix: _data.prefix, prefix: _data.prefix,
firstName: _data.firstName, firstName: _data.firstName,
@ -788,6 +752,8 @@ export class ProfileController extends Controller {
posType: _data.posType == null ? null : _data.posType.posTypeName, posType: _data.posType == null ? null : _data.posType.posTypeName,
isDirector: true, isDirector: true,
})); }));
}
return new HttpSuccess({ caregiver, commander, chairman }); return new HttpSuccess({ caregiver, commander, chairman });
} }