diff --git a/src/controllers/OrganizationController.ts b/src/controllers/OrganizationController.ts index 8423bdd3..8799d8cd 100644 --- a/src/controllers/OrganizationController.ts +++ b/src/controllers/OrganizationController.ts @@ -5443,37 +5443,93 @@ export class OrganizationController extends Controller { */ @Get("act/{id}") async detailAct(@Path() id: string, @Request() request: RequestWithUser) { - // let _data = { - // root: null, - // child1: null, - // child2: null, - // child3: null, - // child4: null, - // }; - + let _data: any = { + root: null, + child1: null, + child2: null, + child3: null, + child4: null, + }; // if (!request.user.role.includes("SUPER_ADMIN")) { // _data = await new permission().PermissionOrgList(request, "SYS_ACTING"); // } - await new permission().PermissionOrgList(request, "SYS_ACTING"); + const _privilege = await new permission().PermissionOrgList(request, "SYS_ACTING"); const orgRevision = await this.orgRevisionRepository.findOne({ where: { id } }); if (!orgRevision) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล"); } + const attrOwnership = _privilege.root === null ? true : false; + + const profile = await this.profileRepo.findOne({ + where: { keycloak: request.user.sub }, + relations: ["permissionProfiles", "current_holders", "current_holders.posMasterAssigns"], + }); + if (!profile) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลผู้ใช้งานในทะเบียนประวัติ"); + } + let profileAssign = profile.current_holders + ?.find((x) => x.orgRevisionId === id) + ?.posMasterAssigns.find((x) => x.assignId === "SYS_ORG"); + + if (orgRevision.orgRevisionIsDraft && !orgRevision.orgRevisionIsCurrent && !attrOwnership) { + if (Array.isArray(profile.permissionProfiles) && profile.permissionProfiles.length > 0) { + _data.root = profile.permissionProfiles.map((x) => x.orgRootId); + } else { + return new HttpSuccess({ remark: "", data: [] }); + } + } + // กำหนดการเข้าถึงข้อมูลตามสถานะและสิทธิ์ + const isCurrentActive = !orgRevision.orgRevisionIsDraft && orgRevision.orgRevisionIsCurrent; + if (isCurrentActive) { + if (profileAssign && _privilege.privilege !== "OWNER") { + if (_privilege.privilege == "NORMAL") { + const holder = profile.current_holders.find((x) => x.orgRevisionId === id); + if (!holder) return; + _data.root = [holder.orgRootId]; + _data.child1 = [holder.orgChild1Id]; + _data.child2 = [holder.orgChild2Id]; + _data.child3 = [holder.orgChild3Id]; + _data.child4 = [holder.orgChild4Id]; + } else if (_privilege.privilege == "CHILD" || _privilege.privilege == "BROTHER") { + const holder = profile.current_holders.find((x) => x.orgRevisionId === id); + if (!holder) return; + _data.root = [holder.orgRootId]; + if (_privilege.root && _privilege.child1 === null) { + } else if (_privilege.child1 && _privilege.child2 === null) { + _data.child1 = [holder.orgChild1Id]; + } else if (_privilege.child2 && _privilege.child3 === null) { + _data.child1 = [holder.orgChild1Id]; + _data.child2 = [holder.orgChild2Id]; + } else if (_privilege.child3 && _privilege.child4 === null) { + _data.child1 = [holder.orgChild1Id]; + _data.child2 = [holder.orgChild2Id]; + _data.child3 = [holder.orgChild3Id]; + _data.child4 = [holder.orgChild4Id]; + } + } else { + _data.root = [profile.current_holders.find((x) => x.orgRevisionId === id)?.orgRootId]; + } + } else { + if (!attrOwnership) _data = _privilege; + } + } + + const orgRootData = await AppDataSource.getRepository(OrgRoot) .createQueryBuilder("orgRoot") .where("orgRoot.orgRevisionId = :id", { id }) - // .andWhere( - // _data.root != undefined && _data.root != null - // ? _data.root[0] != null - // ? `orgRoot.id IN (:...node)` - // : `orgRoot.id is null` - // : "1=1", - // { - // node: _data.root, - // }, - // ) + .andWhere( + _data.root != undefined && _data.root != null + ? _data.root[0] != null + ? `orgRoot.id IN (:...node)` + : `orgRoot.id is null` + : "1=1", + { + node: _data.root, + }, + ) .leftJoinAndSelect("orgRoot.posMasters", "posMasters") .leftJoinAndSelect("posMasters.current_holder", "current_holder") .orderBy("orgRoot.orgRootOrder", "ASC") @@ -5485,16 +5541,16 @@ export class OrganizationController extends Controller { ? await AppDataSource.getRepository(OrgChild1) .createQueryBuilder("orgChild1") .where("orgChild1.orgRootId IN (:...ids)", { ids: orgRootIds }) - // .andWhere( - // _data.child1 != undefined && _data.child1 != null - // ? _data.child1[0] != null - // ? `orgChild1.id IN (:...node)` - // : `orgChild1.id is null` - // : "1=1", - // { - // node: _data.child1, - // }, - // ) + .andWhere( + _data.child1 != undefined && _data.child1 != null + ? _data.child1[0] != null + ? `orgChild1.id IN (:...node)` + : `orgChild1.id is null` + : "1=1", + { + node: _data.child1, + }, + ) .leftJoinAndSelect("orgChild1.posMasters", "posMasters") .leftJoinAndSelect("posMasters.current_holder", "current_holder") .orderBy("orgChild1.orgChild1Order", "ASC") @@ -5507,16 +5563,16 @@ export class OrganizationController extends Controller { ? await AppDataSource.getRepository(OrgChild2) .createQueryBuilder("orgChild2") .where("orgChild2.orgChild1Id IN (:...ids)", { ids: orgChild1Ids }) - // .andWhere( - // _data.child2 != undefined && _data.child2 != null - // ? _data.child2[0] != null - // ? `orgChild2.id IN (:...node)` - // : `orgChild2.id is null` - // : "1=1", - // { - // node: _data.child2, - // }, - // ) + .andWhere( + _data.child2 != undefined && _data.child2 != null + ? _data.child2[0] != null + ? `orgChild2.id IN (:...node)` + : `orgChild2.id is null` + : "1=1", + { + node: _data.child2, + }, + ) .leftJoinAndSelect("orgChild2.posMasters", "posMasters") .leftJoinAndSelect("posMasters.current_holder", "current_holder") .orderBy("orgChild2.orgChild2Order", "ASC") @@ -5529,16 +5585,16 @@ export class OrganizationController extends Controller { ? await AppDataSource.getRepository(OrgChild3) .createQueryBuilder("orgChild3") .where("orgChild3.orgChild2Id IN (:...ids)", { ids: orgChild2Ids }) - // .andWhere( - // _data.child3 != undefined && _data.child3 != null - // ? _data.child3[0] != null - // ? `orgChild3.id IN (:...node)` - // : `orgChild3.id is null` - // : "1=1", - // { - // node: _data.child3, - // }, - // ) + .andWhere( + _data.child3 != undefined && _data.child3 != null + ? _data.child3[0] != null + ? `orgChild3.id IN (:...node)` + : `orgChild3.id is null` + : "1=1", + { + node: _data.child3, + }, + ) .leftJoinAndSelect("orgChild3.posMasters", "posMasters") .leftJoinAndSelect("posMasters.current_holder", "current_holder") .orderBy("orgChild3.orgChild3Order", "ASC") @@ -5551,16 +5607,16 @@ export class OrganizationController extends Controller { ? await AppDataSource.getRepository(OrgChild4) .createQueryBuilder("orgChild4") .where("orgChild4.orgChild3Id IN (:...ids)", { ids: orgChild3Ids }) - // .andWhere( - // _data.child4 != undefined && _data.child4 != null - // ? _data.child4[0] != null - // ? `orgChild4.id IN (:...node)` - // : `orgChild4.id is null` - // : "1=1", - // { - // node: _data.child4, - // }, - // ) + .andWhere( + _data.child4 != undefined && _data.child4 != null + ? _data.child4[0] != null + ? `orgChild4.id IN (:...node)` + : `orgChild4.id is null` + : "1=1", + { + node: _data.child4, + }, + ) .leftJoinAndSelect("orgChild4.posMasters", "posMasters") .leftJoinAndSelect("posMasters.current_holder", "current_holder") .orderBy("orgChild4.orgChild4Order", "ASC")