diff --git a/src/controllers/OrganizationController.ts b/src/controllers/OrganizationController.ts index 25a5163d..eed30251 100644 --- a/src/controllers/OrganizationController.ts +++ b/src/controllers/OrganizationController.ts @@ -588,15 +588,22 @@ export class OrganizationController extends Controller { * */ @Get("{id}") - async detail(@Path() id: string) { + async detail(@Path() id: string, @Request() request: RequestWithUser) { const orgRevision = await this.orgRevisionRepository.findOne({ where: { id } }); if (!orgRevision) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล"); } + let _data = await new permission().PermissionOrgList(request, "SYS_REGISTRY_OFFICER"); const orgRootData = await AppDataSource.getRepository(OrgRoot) .createQueryBuilder("orgRoot") .where("orgRoot.orgRevisionId = :id", { id }) + .andWhere( + _data.root != undefined && _data.root != null ? `orgRoot.id IN (:...node)` : "1=1", + { + node: _data.root, + }, + ) .select([ "orgRoot.id", "orgRoot.orgRootName", @@ -613,13 +620,20 @@ export class OrganizationController extends Controller { ]) .orderBy("orgRoot.orgRootOrder", "ASC") .getMany(); - const orgRootIds = orgRootData.map((orgRoot) => orgRoot.id) || null; const orgChild1Data = orgRootIds && orgRootIds.length > 0 ? await AppDataSource.getRepository(OrgChild1) .createQueryBuilder("orgChild1") .where("orgChild1.orgRootId IN (:...ids)", { ids: orgRootIds }) + .andWhere( + _data.child1 != undefined && _data.child1 != null + ? `orgChild1.id IN (:...node)` + : "1=1", + { + node: _data.child1, + }, + ) .select([ "orgChild1.id", "orgChild1.orgChild1Name", @@ -644,6 +658,14 @@ 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 + ? `orgChild2.id IN (:...node)` + : "1=1", + { + node: _data.child2, + }, + ) .select([ "orgChild2.id", "orgChild2.orgChild2Name", @@ -669,6 +691,14 @@ 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 + ? `orgChild3.id IN (:...node)` + : "1=1", + { + node: _data.child3, + }, + ) .select([ "orgChild3.id", "orgChild3.orgChild3Name", @@ -694,6 +724,14 @@ 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 + ? `orgChild4.id IN (:...node)` + : "1=1", + { + node: _data.child4, + }, + ) .select([ "orgChild4.id", "orgChild4.orgChild4Name", @@ -1486,7 +1524,10 @@ export class OrganizationController extends Controller { * */ @Post("sort") - async Sort(@Body() requestBody: { id: string; type: number; sortId: string[] }, @Request() request: RequestWithUser) { + async Sort( + @Body() requestBody: { id: string; type: number; sortId: string[] }, + @Request() request: RequestWithUser, + ) { await new permission().PermissionUpdate(request, "SYS_ORG"); switch (requestBody.type) { case 0: { @@ -3728,4 +3769,774 @@ export class OrganizationController extends Controller { return new HttpSuccess(maps); } + + /** + * API รายละเอียดโครงสร้าง + * + * @summary ORG_023 - รายละเอียดโครงสร้าง (ADMIN) #25 + * + */ + @Get("{id}/{system}") + async detailBySystem( + @Path() id: string, + @Path() system: string, + @Request() request: RequestWithUser, + ) { + const orgRevision = await this.orgRevisionRepository.findOne({ where: { id } }); + if (!orgRevision) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล"); + } + + let _data = await new permission().PermissionOrgList(request, system.trim().toUpperCase()); + const orgRootData = await AppDataSource.getRepository(OrgRoot) + .createQueryBuilder("orgRoot") + .where("orgRoot.orgRevisionId = :id", { id }) + .andWhere( + _data.root != undefined && _data.root != null ? `orgRoot.id IN (:...node)` : "1=1", + { + node: _data.root, + }, + ) + .select([ + "orgRoot.id", + "orgRoot.orgRootName", + "orgRoot.orgRootShortName", + "orgRoot.orgRootCode", + "orgRoot.orgRootOrder", + "orgRoot.orgRootPhoneEx", + "orgRoot.orgRootPhoneIn", + "orgRoot.orgRootFax", + "orgRoot.orgRevisionId", + "orgRoot.orgRootRank", + "orgRoot.orgRootRankSub", + "orgRoot.responsibility", + ]) + .orderBy("orgRoot.orgRootOrder", "ASC") + .getMany(); + const orgRootIds = orgRootData.map((orgRoot) => orgRoot.id) || null; + const orgChild1Data = + orgRootIds && orgRootIds.length > 0 + ? await AppDataSource.getRepository(OrgChild1) + .createQueryBuilder("orgChild1") + .where("orgChild1.orgRootId IN (:...ids)", { ids: orgRootIds }) + .andWhere( + _data.child1 != undefined && _data.child1 != null + ? `orgChild1.id IN (:...node)` + : "1=1", + { + node: _data.child1, + }, + ) + .select([ + "orgChild1.id", + "orgChild1.orgChild1Name", + "orgChild1.orgChild1ShortName", + "orgChild1.orgChild1Code", + "orgChild1.orgChild1Order", + "orgChild1.orgChild1PhoneEx", + "orgChild1.orgChild1PhoneIn", + "orgChild1.orgChild1Fax", + "orgChild1.orgRootId", + "orgChild1.orgChild1Rank", + "orgChild1.orgChild1RankSub", + "orgChild1.responsibility", + ]) + .orderBy("orgChild1.orgChild1Order", "ASC") + .getMany() + : []; + + const orgChild1Ids = orgChild1Data.map((orgChild1) => orgChild1.id) || null; + const orgChild2Data = + orgChild1Ids && orgChild1Ids.length > 0 + ? await AppDataSource.getRepository(OrgChild2) + .createQueryBuilder("orgChild2") + .where("orgChild2.orgChild1Id IN (:...ids)", { ids: orgChild1Ids }) + .andWhere( + _data.child2 != undefined && _data.child2 != null + ? `orgChild2.id IN (:...node)` + : "1=1", + { + node: _data.child2, + }, + ) + .select([ + "orgChild2.id", + "orgChild2.orgChild2Name", + "orgChild2.orgChild2ShortName", + "orgChild2.orgChild2Code", + "orgChild2.orgChild2Order", + "orgChild2.orgChild2PhoneEx", + "orgChild2.orgChild2PhoneIn", + "orgChild2.orgChild2Fax", + "orgChild2.orgRootId", + "orgChild2.orgChild2Rank", + "orgChild2.orgChild2RankSub", + "orgChild2.orgChild1Id", + "orgChild2.responsibility", + ]) + .orderBy("orgChild2.orgChild2Order", "ASC") + .getMany() + : []; + + const orgChild2Ids = orgChild2Data.map((orgChild2) => orgChild2.id) || null; + const orgChild3Data = + orgChild2Ids && orgChild2Ids.length > 0 + ? await AppDataSource.getRepository(OrgChild3) + .createQueryBuilder("orgChild3") + .where("orgChild3.orgChild2Id IN (:...ids)", { ids: orgChild2Ids }) + .andWhere( + _data.child3 != undefined && _data.child3 != null + ? `orgChild3.id IN (:...node)` + : "1=1", + { + node: _data.child3, + }, + ) + .select([ + "orgChild3.id", + "orgChild3.orgChild3Name", + "orgChild3.orgChild3ShortName", + "orgChild3.orgChild3Code", + "orgChild3.orgChild3Order", + "orgChild3.orgChild3PhoneEx", + "orgChild3.orgChild3PhoneIn", + "orgChild3.orgChild3Fax", + "orgChild3.orgRootId", + "orgChild3.orgChild3Rank", + "orgChild3.orgChild3RankSub", + "orgChild3.orgChild2Id", + "orgChild3.responsibility", + ]) + .orderBy("orgChild3.orgChild3Order", "ASC") + .getMany() + : []; + + const orgChild3Ids = orgChild3Data.map((orgChild3) => orgChild3.id) || null; + const orgChild4Data = + orgChild3Ids && orgChild3Ids.length > 0 + ? await AppDataSource.getRepository(OrgChild4) + .createQueryBuilder("orgChild4") + .where("orgChild4.orgChild3Id IN (:...ids)", { ids: orgChild3Ids }) + .andWhere( + _data.child4 != undefined && _data.child4 != null + ? `orgChild4.id IN (:...node)` + : "1=1", + { + node: _data.child4, + }, + ) + .select([ + "orgChild4.id", + "orgChild4.orgChild4Name", + "orgChild4.orgChild4ShortName", + "orgChild4.orgChild4Code", + "orgChild4.orgChild4Order", + "orgChild4.orgChild4PhoneEx", + "orgChild4.orgChild4PhoneIn", + "orgChild4.orgChild4Fax", + "orgChild4.orgRootId", + "orgChild4.orgChild4Rank", + "orgChild4.orgChild4RankSub", + "orgChild4.orgChild3Id", + "orgChild4.responsibility", + ]) + .orderBy("orgChild4.orgChild4Order", "ASC") + .getMany() + : []; + + // const formattedData = orgRootData.map((orgRoot) => { + const formattedData = await Promise.all( + orgRootData.map(async (orgRoot) => { + return { + orgTreeId: orgRoot.id, + orgLevel: 0, + orgName: orgRoot.orgRootName, + orgTreeName: orgRoot.orgRootName, + orgTreeShortName: orgRoot.orgRootShortName, + orgTreeCode: orgRoot.orgRootCode, + orgCode: orgRoot.orgRootCode + "00", + orgTreeRank: orgRoot.orgRootRank, + orgTreeRankSub: orgRoot.orgRootRankSub, + orgTreeOrder: orgRoot.orgRootOrder, + orgTreePhoneEx: orgRoot.orgRootPhoneEx, + orgTreePhoneIn: orgRoot.orgRootPhoneIn, + orgTreeFax: orgRoot.orgRootFax, + orgRevisionId: orgRoot.orgRevisionId, + orgRootName: orgRoot.orgRootName, + responsibility: orgRoot.responsibility, + labelName: + orgRoot.orgRootName + " " + orgRoot.orgRootCode + "00" + " " + orgRoot.orgRootShortName, + totalPosition: await this.posMasterRepository.count({ + where: { orgRevisionId: orgRoot.orgRevisionId, orgRootId: orgRoot.id }, + }), + totalPositionCurrentUse: await this.posMasterRepository.count({ + where: { + orgRevisionId: orgRoot.orgRevisionId, + orgRootId: orgRoot.id, + current_holderId: Not(IsNull()) || Not(""), + }, + }), + totalPositionCurrentVacant: await this.posMasterRepository.count({ + where: { + orgRevisionId: orgRoot.orgRevisionId, + orgRootId: orgRoot.id, + current_holderId: IsNull() || "", + }, + }), + totalPositionNextUse: await this.posMasterRepository.count({ + where: { + orgRevisionId: orgRoot.orgRevisionId, + orgRootId: orgRoot.id, + next_holderId: Not(IsNull()) || Not(""), + }, + }), + totalPositionNextVacant: await this.posMasterRepository.count({ + where: { + orgRevisionId: orgRoot.orgRevisionId, + orgRootId: orgRoot.id, + next_holderId: IsNull() || "", + }, + }), + totalRootPosition: await this.posMasterRepository.count({ + where: { + orgRevisionId: orgRoot.orgRevisionId, + orgRootId: orgRoot.id, + orgChild1Id: IsNull() || "", + orgChild2Id: IsNull() || "", + orgChild3Id: IsNull() || "", + orgChild4Id: IsNull() || "", + }, + }), + totalRootPositionCurrentUse: await this.posMasterRepository.count({ + where: { + orgRevisionId: orgRoot.orgRevisionId, + orgRootId: orgRoot.id, + orgChild1Id: IsNull() || "", + orgChild2Id: IsNull() || "", + orgChild3Id: IsNull() || "", + orgChild4Id: IsNull() || "", + current_holderId: Not(IsNull()) || Not(""), + }, + }), + totalRootPositionCurrentVacant: await this.posMasterRepository.count({ + where: { + orgRevisionId: orgRoot.orgRevisionId, + orgRootId: orgRoot.id, + orgChild1Id: IsNull() || "", + orgChild2Id: IsNull() || "", + orgChild3Id: IsNull() || "", + orgChild4Id: IsNull() || "", + current_holderId: IsNull() || "", + }, + }), + totalRootPositionNextUse: await this.posMasterRepository.count({ + where: { + orgRevisionId: orgRoot.orgRevisionId, + orgRootId: orgRoot.id, + orgChild1Id: IsNull() || "", + orgChild2Id: IsNull() || "", + orgChild3Id: IsNull() || "", + orgChild4Id: IsNull() || "", + next_holderId: Not(IsNull()) || Not(""), + }, + }), + totalRootPositionNextVacant: await this.posMasterRepository.count({ + where: { + orgRevisionId: orgRoot.orgRevisionId, + orgRootId: orgRoot.id, + orgChild1Id: IsNull() || "", + orgChild2Id: IsNull() || "", + orgChild3Id: IsNull() || "", + orgChild4Id: IsNull() || "", + next_holderId: IsNull() || "", + }, + }), + + children: await Promise.all( + orgChild1Data + .filter((orgChild1) => orgChild1.orgRootId === orgRoot.id) + .map(async (orgChild1) => ({ + orgTreeId: orgChild1.id, + orgRootId: orgRoot.id, + orgLevel: 1, + orgName: `${orgChild1.orgChild1Name}/${orgRoot.orgRootName}`, + orgTreeName: orgChild1.orgChild1Name, + orgTreeShortName: orgChild1.orgChild1ShortName, + orgTreeCode: orgChild1.orgChild1Code, + orgCode: orgRoot.orgRootCode + orgChild1.orgChild1Code, + orgTreeRank: orgChild1.orgChild1Rank, + orgTreeRankSub: orgChild1.orgChild1RankSub, + orgTreeOrder: orgChild1.orgChild1Order, + orgRootCode: orgRoot.orgRootCode, + orgTreePhoneEx: orgChild1.orgChild1PhoneEx, + orgTreePhoneIn: orgChild1.orgChild1PhoneIn, + orgTreeFax: orgChild1.orgChild1Fax, + orgRevisionId: orgRoot.orgRevisionId, + orgRootName: orgRoot.orgRootName, + responsibility: orgChild1.responsibility, + labelName: + orgChild1.orgChild1Name + + " " + + orgRoot.orgRootCode + + orgChild1.orgChild1Code + + " " + + orgChild1.orgChild1ShortName, + totalPosition: await this.posMasterRepository.count({ + where: { orgRevisionId: orgRoot.orgRevisionId, orgChild1Id: orgChild1.id }, + }), + totalPositionCurrentUse: await this.posMasterRepository.count({ + where: { + orgRevisionId: orgRoot.orgRevisionId, + orgChild1Id: orgChild1.id, + current_holderId: Not(IsNull()) || Not(""), + }, + }), + totalPositionCurrentVacant: await this.posMasterRepository.count({ + where: { + orgRevisionId: orgRoot.orgRevisionId, + orgChild1Id: orgChild1.id, + current_holderId: IsNull() || "", + }, + }), + totalPositionNextUse: await this.posMasterRepository.count({ + where: { + orgRevisionId: orgRoot.orgRevisionId, + orgChild1Id: orgChild1.id, + next_holderId: Not(IsNull()) || Not(""), + }, + }), + totalPositionNextVacant: await this.posMasterRepository.count({ + where: { + orgRevisionId: orgRoot.orgRevisionId, + orgChild1Id: orgChild1.id, + next_holderId: IsNull() || "", + }, + }), + totalRootPosition: await this.posMasterRepository.count({ + where: { + orgRevisionId: orgRoot.orgRevisionId, + orgRootId: orgRoot.id, + orgChild1Id: orgChild1.id, + orgChild2Id: IsNull() || "", + orgChild3Id: IsNull() || "", + orgChild4Id: IsNull() || "", + }, + }), + totalRootPositionCurrentUse: await this.posMasterRepository.count({ + where: { + orgRevisionId: orgRoot.orgRevisionId, + orgRootId: orgRoot.id, + orgChild1Id: orgChild1.id, + orgChild2Id: IsNull() || "", + orgChild3Id: IsNull() || "", + orgChild4Id: IsNull() || "", + current_holderId: Not(IsNull()) || Not(""), + }, + }), + totalRootPositionCurrentVacant: await this.posMasterRepository.count({ + where: { + orgRevisionId: orgRoot.orgRevisionId, + orgRootId: orgRoot.id, + orgChild1Id: orgChild1.id, + orgChild2Id: IsNull() || "", + orgChild3Id: IsNull() || "", + orgChild4Id: IsNull() || "", + current_holderId: IsNull() || "", + }, + }), + totalRootPositionNextUse: await this.posMasterRepository.count({ + where: { + orgRevisionId: orgRoot.orgRevisionId, + orgRootId: orgRoot.id, + orgChild1Id: orgChild1.id, + orgChild2Id: IsNull() || "", + orgChild3Id: IsNull() || "", + orgChild4Id: IsNull() || "", + next_holderId: Not(IsNull()) || Not(""), + }, + }), + totalRootPositionNextVacant: await this.posMasterRepository.count({ + where: { + orgRevisionId: orgRoot.orgRevisionId, + orgRootId: orgRoot.id, + orgChild1Id: orgChild1.id, + orgChild2Id: IsNull() || "", + orgChild3Id: IsNull() || "", + orgChild4Id: IsNull() || "", + next_holderId: IsNull() || "", + }, + }), + + children: await Promise.all( + orgChild2Data + .filter((orgChild2) => orgChild2.orgChild1Id === orgChild1.id) + .map(async (orgChild2) => ({ + orgTreeId: orgChild2.id, + orgRootId: orgChild1.id, + orgLevel: 2, + orgName: `${orgChild2.orgChild2Name}/${orgChild1.orgChild1Name}/${orgRoot.orgRootName}`, + orgTreeName: orgChild2.orgChild2Name, + orgTreeShortName: orgChild2.orgChild2ShortName, + orgTreeCode: orgChild2.orgChild2Code, + orgCode: orgRoot.orgRootCode + orgChild2.orgChild2Code, + orgTreeRank: orgChild2.orgChild2Rank, + orgTreeRankSub: orgChild2.orgChild2RankSub, + orgTreeOrder: orgChild2.orgChild2Order, + orgRootCode: orgRoot.orgRootCode, + orgTreePhoneEx: orgChild2.orgChild2PhoneEx, + orgTreePhoneIn: orgChild2.orgChild2PhoneIn, + orgTreeFax: orgChild2.orgChild2Fax, + orgRevisionId: orgRoot.orgRevisionId, + orgRootName: orgRoot.orgRootName, + responsibility: orgChild2.responsibility, + labelName: + orgChild2.orgChild2Name + + " " + + orgRoot.orgRootCode + + orgChild2.orgChild2Code + + " " + + orgChild2.orgChild2ShortName, + totalPosition: await this.posMasterRepository.count({ + where: { + orgRevisionId: orgRoot.orgRevisionId, + orgChild2Id: orgChild2.id, + }, + }), + totalPositionCurrentUse: await this.posMasterRepository.count({ + where: { + orgRevisionId: orgRoot.orgRevisionId, + orgChild2Id: orgChild2.id, + current_holderId: Not(IsNull()) || Not(""), + }, + }), + totalPositionCurrentVacant: await this.posMasterRepository.count({ + where: { + orgRevisionId: orgRoot.orgRevisionId, + orgChild2Id: orgChild2.id, + current_holderId: IsNull() || "", + }, + }), + totalPositionNextUse: await this.posMasterRepository.count({ + where: { + orgRevisionId: orgRoot.orgRevisionId, + orgChild2Id: orgChild2.id, + next_holderId: Not(IsNull()) || Not(""), + }, + }), + totalPositionNextVacant: await this.posMasterRepository.count({ + where: { + orgRevisionId: orgRoot.orgRevisionId, + orgChild2Id: orgChild2.id, + next_holderId: IsNull() || "", + }, + }), + totalRootPosition: await this.posMasterRepository.count({ + where: { + orgRevisionId: orgRoot.orgRevisionId, + orgRootId: orgRoot.id, + orgChild1Id: orgChild1.id, + orgChild2Id: orgChild2.id, + orgChild3Id: IsNull() || "", + orgChild4Id: IsNull() || "", + }, + }), + totalRootPositionCurrentUse: await this.posMasterRepository.count({ + where: { + orgRevisionId: orgRoot.orgRevisionId, + orgRootId: orgRoot.id, + orgChild1Id: orgChild1.id, + orgChild2Id: orgChild2.id, + orgChild3Id: IsNull() || "", + orgChild4Id: IsNull() || "", + current_holderId: Not(IsNull()) || Not(""), + }, + }), + totalRootPositionCurrentVacant: await this.posMasterRepository.count({ + where: { + orgRevisionId: orgRoot.orgRevisionId, + orgRootId: orgRoot.id, + orgChild1Id: orgChild1.id, + orgChild2Id: orgChild2.id, + orgChild3Id: IsNull() || "", + orgChild4Id: IsNull() || "", + current_holderId: IsNull() || "", + }, + }), + totalRootPositionNextUse: await this.posMasterRepository.count({ + where: { + orgRevisionId: orgRoot.orgRevisionId, + orgRootId: orgRoot.id, + orgChild1Id: orgChild1.id, + orgChild2Id: orgChild2.id, + orgChild3Id: IsNull() || "", + orgChild4Id: IsNull() || "", + next_holderId: Not(IsNull()) || Not(""), + }, + }), + totalRootPositionNextVacant: await this.posMasterRepository.count({ + where: { + orgRevisionId: orgRoot.orgRevisionId, + orgRootId: orgRoot.id, + orgChild1Id: orgChild1.id, + orgChild2Id: orgChild2.id, + orgChild3Id: IsNull() || "", + orgChild4Id: IsNull() || "", + next_holderId: IsNull() || "", + }, + }), + + children: await Promise.all( + orgChild3Data + .filter((orgChild3) => orgChild3.orgChild2Id === orgChild2.id) + .map(async (orgChild3) => ({ + orgTreeId: orgChild3.id, + orgRootId: orgChild2.id, + orgLevel: 3, + orgName: `${orgChild3.orgChild3Name}/${orgChild2.orgChild2Name}/${orgChild1.orgChild1Name}/${orgRoot.orgRootName}`, + orgTreeName: orgChild3.orgChild3Name, + orgTreeShortName: orgChild3.orgChild3ShortName, + orgTreeCode: orgChild3.orgChild3Code, + orgCode: orgRoot.orgRootCode + orgChild3.orgChild3Code, + orgTreeRank: orgChild3.orgChild3Rank, + orgTreeRankSub: orgChild3.orgChild3RankSub, + orgTreeOrder: orgChild3.orgChild3Order, + orgRootCode: orgRoot.orgRootCode, + orgTreePhoneEx: orgChild3.orgChild3PhoneEx, + orgTreePhoneIn: orgChild3.orgChild3PhoneIn, + orgTreeFax: orgChild3.orgChild3Fax, + orgRevisionId: orgRoot.orgRevisionId, + orgRootName: orgRoot.orgRootName, + responsibility: orgChild3.responsibility, + labelName: + orgChild3.orgChild3Name + + " " + + orgRoot.orgRootCode + + orgChild3.orgChild3Code + + " " + + orgChild3.orgChild3ShortName, + totalPosition: await this.posMasterRepository.count({ + where: { + orgRevisionId: orgRoot.orgRevisionId, + orgChild3Id: orgChild3.id, + }, + }), + totalPositionCurrentUse: await this.posMasterRepository.count({ + where: { + orgRevisionId: orgRoot.orgRevisionId, + orgChild3Id: orgChild3.id, + current_holderId: Not(IsNull()) || Not(""), + }, + }), + totalPositionCurrentVacant: await this.posMasterRepository.count({ + where: { + orgRevisionId: orgRoot.orgRevisionId, + orgChild3Id: orgChild3.id, + current_holderId: IsNull() || "", + }, + }), + totalPositionNextUse: await this.posMasterRepository.count({ + where: { + orgRevisionId: orgRoot.orgRevisionId, + orgChild3Id: orgChild3.id, + next_holderId: Not(IsNull()) || Not(""), + }, + }), + totalPositionNextVacant: await this.posMasterRepository.count({ + where: { + orgRevisionId: orgRoot.orgRevisionId, + orgChild3Id: orgChild3.id, + next_holderId: IsNull() || "", + }, + }), + totalRootPosition: await this.posMasterRepository.count({ + where: { + orgRevisionId: orgRoot.orgRevisionId, + orgRootId: orgRoot.id, + orgChild1Id: orgChild1.id, + orgChild2Id: orgChild2.id, + orgChild3Id: orgChild3.id, + orgChild4Id: IsNull() || "", + }, + }), + totalRootPositionCurrentUse: await this.posMasterRepository.count({ + where: { + orgRevisionId: orgRoot.orgRevisionId, + orgRootId: orgRoot.id, + orgChild1Id: orgChild1.id, + orgChild2Id: orgChild2.id, + orgChild3Id: orgChild3.id, + orgChild4Id: IsNull() || "", + current_holderId: Not(IsNull()) || Not(""), + }, + }), + totalRootPositionCurrentVacant: await this.posMasterRepository.count({ + where: { + orgRevisionId: orgRoot.orgRevisionId, + orgRootId: orgRoot.id, + orgChild1Id: orgChild1.id, + orgChild2Id: orgChild2.id, + orgChild3Id: orgChild3.id, + orgChild4Id: IsNull() || "", + current_holderId: IsNull() || "", + }, + }), + totalRootPositionNextUse: await this.posMasterRepository.count({ + where: { + orgRevisionId: orgRoot.orgRevisionId, + orgRootId: orgRoot.id, + orgChild1Id: orgChild1.id, + orgChild2Id: orgChild2.id, + orgChild3Id: orgChild3.id, + orgChild4Id: IsNull() || "", + next_holderId: Not(IsNull()) || Not(""), + }, + }), + totalRootPositionNextVacant: await this.posMasterRepository.count({ + where: { + orgRevisionId: orgRoot.orgRevisionId, + orgRootId: orgRoot.id, + orgChild1Id: orgChild1.id, + orgChild2Id: orgChild2.id, + orgChild3Id: orgChild3.id, + orgChild4Id: IsNull() || "", + next_holderId: IsNull() || "", + }, + }), + + children: await Promise.all( + orgChild4Data + .filter((orgChild4) => orgChild4.orgChild3Id === orgChild3.id) + .map(async (orgChild4) => ({ + orgTreeId: orgChild4.id, + orgRootId: orgChild3.id, + orgLevel: 4, + orgName: `${orgChild4.orgChild4Name}/${orgChild3.orgChild3Name}/${orgChild2.orgChild2Name}/${orgChild1.orgChild1Name}/${orgRoot.orgRootName}`, + orgTreeName: orgChild4.orgChild4Name, + orgTreeShortName: orgChild4.orgChild4ShortName, + orgTreeCode: orgChild4.orgChild4Code, + orgCode: orgRoot.orgRootCode + orgChild4.orgChild4Code, + orgTreeRank: orgChild4.orgChild4Rank, + orgTreeRankSub: orgChild4.orgChild4RankSub, + orgTreeOrder: orgChild4.orgChild4Order, + orgRootCode: orgRoot.orgRootCode, + orgTreePhoneEx: orgChild4.orgChild4PhoneEx, + orgTreePhoneIn: orgChild4.orgChild4PhoneIn, + orgTreeFax: orgChild4.orgChild4Fax, + orgRevisionId: orgRoot.orgRevisionId, + orgRootName: orgRoot.orgRootName, + responsibility: orgChild4.responsibility, + labelName: + orgChild4.orgChild4Name + + " " + + orgRoot.orgRootCode + + orgChild4.orgChild4Code + + " " + + orgChild4.orgChild4ShortName, + totalPosition: await this.posMasterRepository.count({ + where: { + orgRevisionId: orgRoot.orgRevisionId, + orgChild4Id: orgChild4.id, + }, + }), + totalPositionCurrentUse: await this.posMasterRepository.count({ + where: { + orgRevisionId: orgRoot.orgRevisionId, + orgChild4Id: orgChild4.id, + current_holderId: Not(IsNull()) || Not(""), + }, + }), + totalPositionCurrentVacant: await this.posMasterRepository.count({ + where: { + orgRevisionId: orgRoot.orgRevisionId, + orgChild4Id: orgChild4.id, + current_holderId: IsNull() || "", + }, + }), + totalPositionNextUse: await this.posMasterRepository.count({ + where: { + orgRevisionId: orgRoot.orgRevisionId, + orgChild4Id: orgChild4.id, + next_holderId: Not(IsNull()) || Not(""), + }, + }), + totalPositionNextVacant: await this.posMasterRepository.count({ + where: { + orgRevisionId: orgRoot.orgRevisionId, + orgChild4Id: orgChild4.id, + next_holderId: IsNull() || "", + }, + }), + totalRootPosition: await this.posMasterRepository.count({ + where: { + orgRevisionId: orgRoot.orgRevisionId, + orgRootId: orgRoot.id, + orgChild1Id: orgChild1.id, + orgChild2Id: orgChild2.id, + orgChild3Id: orgChild3.id, + orgChild4Id: orgChild4.id, + }, + }), + totalRootPositionCurrentUse: await this.posMasterRepository.count( + { + where: { + orgRevisionId: orgRoot.orgRevisionId, + orgRootId: orgRoot.id, + orgChild1Id: orgChild1.id, + orgChild2Id: orgChild2.id, + orgChild3Id: orgChild3.id, + orgChild4Id: orgChild4.id, + current_holderId: Not(IsNull()) || Not(""), + }, + }, + ), + totalRootPositionCurrentVacant: + await this.posMasterRepository.count({ + where: { + orgRevisionId: orgRoot.orgRevisionId, + orgRootId: orgRoot.id, + orgChild1Id: orgChild1.id, + orgChild2Id: orgChild2.id, + orgChild3Id: orgChild3.id, + orgChild4Id: orgChild4.id, + current_holderId: IsNull() || "", + }, + }), + totalRootPositionNextUse: await this.posMasterRepository.count({ + where: { + orgRevisionId: orgRoot.orgRevisionId, + orgRootId: orgRoot.id, + orgChild1Id: orgChild1.id, + orgChild2Id: orgChild2.id, + orgChild3Id: orgChild3.id, + orgChild4Id: orgChild4.id, + next_holderId: Not(IsNull()) || Not(""), + }, + }), + totalRootPositionNextVacant: await this.posMasterRepository.count( + { + where: { + orgRevisionId: orgRoot.orgRevisionId, + orgRootId: orgRoot.id, + orgChild1Id: orgChild1.id, + orgChild2Id: orgChild2.id, + orgChild3Id: orgChild3.id, + orgChild4Id: orgChild4.id, + next_holderId: IsNull() || "", + }, + }, + ), + })), + ), + })), + ), + })), + ), + })), + ), + }; + }), + ); + + return new HttpSuccess(formattedData); + } } diff --git a/src/controllers/PermissionController.ts b/src/controllers/PermissionController.ts index 625520a0..04378e6a 100644 --- a/src/controllers/PermissionController.ts +++ b/src/controllers/PermissionController.ts @@ -4,7 +4,6 @@ import { RequestWithUser } from "../middlewares/user"; import HttpError from "../interfaces/http-error"; import HttpStatus from "../interfaces/http-status"; import HttpSuccess from "../interfaces/http-success"; -import HttpStatusCode from "../interfaces/http-status"; import { AuthRole } from "../entities/AuthRole"; import { AuthRoleAttr } from "../entities/AuthRoleAttr"; import { PosMaster } from "../entities/PosMaster"; @@ -197,16 +196,60 @@ export class PermissionController extends Controller { */ @Get("dotnet/{action}/{system}") public async dotnet( - @Request() req: RequestWithUser, + @Request() req: RequestWithUser, @Path() action: string, - @Path() system: string + @Path() system: string, ) { - - if(!["CREATE", "DELETE", "GET", "LIST", "UPDATE"].includes(action)) { - throw new HttpError(HttpStatus.NOT_FOUND, "Action ไม่ถูกต้อง"); + if (!["CREATE", "DELETE", "GET", "LIST", "UPDATE"].includes(action)) { + throw new HttpError(HttpStatus.NOT_FOUND, "Action ไม่ถูกต้อง"); } let res = await new permission().Permission(req, system.toLocaleUpperCase(), action); return new HttpSuccess(res); } + + @Get("org") + public async listAuthSysOrg(@Request() request: RequestWithUser) { + const redisClient = await this.redis.createClient({ + host: REDIS_HOST, + port: REDIS_PORT, + }); + const getAsync = promisify(redisClient.get).bind(redisClient); + + const profile = await this.profileRepo.findOne({ + // select: ["id"], + where: { keycloak: request.user.sub }, + }); + if (!profile) { + throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลบุคคลนี้ในระบบ"); + } + + let reply = await getAsync("posMaster_" + profile.id); + if (reply != null) { + reply = JSON.parse(reply); + } else { + const posMaster = await this.posMasterRepository.findOne({ + where: { + current_holderId: profile.id, + orgRevision: { + orgRevisionIsDraft: false, + orgRevisionIsCurrent: true, + }, + }, + }); + if (!posMaster) { + throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลตำแหน่งในโครงสร้าง"); + } + reply = { + orgRootId: posMaster.orgRootId, + orgChild1Id: posMaster.orgChild1Id, + orgChild2Id: posMaster.orgChild2Id, + orgChild3Id: posMaster.orgChild3Id, + orgChild4Id: posMaster.orgChild4Id, + }; + redisClient.setex("posMaster_" + profile.id, 86400, JSON.stringify(reply)); + } + + return new HttpSuccess(reply); + } } diff --git a/src/controllers/ProfileController.ts b/src/controllers/ProfileController.ts index 905f5d8b..9bfad0f8 100644 --- a/src/controllers/ProfileController.ts +++ b/src/controllers/ProfileController.ts @@ -2557,9 +2557,11 @@ export class ProfileController extends Controller { }, }) async listProfile( + @Request() request: RequestWithUser, @Query("page") page: number = 1, @Query("pageSize") pageSize: number = 10, - @Query() searchField?: "firstName" | "lastName" | "fullName" | "citizenId" | "position" | "posNo", + @Query() + searchField?: "firstName" | "lastName" | "fullName" | "citizenId" | "position" | "posNo", @Query() searchKeyword: string = "", @Query() posType?: string, @Query() posLevel?: string, @@ -2597,6 +2599,7 @@ export class ProfileController extends Controller { } else if (node === 4 && nodeId) { nodeCondition = "current_holders.orgChild4Id = :nodeId"; } + // let _data = await new permission().PermissionOrgList(request, "SYS_REGISTRY_OFFICER"); const [record, total] = await this.profileRepo .createQueryBuilder("profile") .leftJoinAndSelect("profile.posLevel", "posLevel") @@ -2609,6 +2612,46 @@ export class ProfileController extends Controller { .leftJoinAndSelect("current_holders.orgChild2", "orgChild2") .leftJoinAndSelect("current_holders.orgChild3", "orgChild3") .leftJoinAndSelect("current_holders.orgChild4", "orgChild4") + // .andWhere( + // _data.root != undefined && _data.root != null + // ? `current_holders.orgRootId IN (:...node)` + // : "1=1", + // { + // node: _data.root, + // }, + // ) + // .andWhere( + // _data.child1 != undefined && _data.child1 != null + // ? `current_holders.orgChild1Id IN (:...node)` + // : "1=1", + // { + // node: _data.child1, + // }, + // ) + // .andWhere( + // _data.child2 != undefined && _data.child2 != null + // ? `current_holders.orgChild2Id IN (:...node)` + // : "1=1", + // { + // node: _data.child2, + // }, + // ) + // .andWhere( + // _data.child3 != undefined && _data.child3 != null + // ? `current_holders.orgChild3Id IN (:...node)` + // : "1=1", + // { + // node: _data.child3, + // }, + // ) + // .andWhere( + // _data.child4 != undefined && _data.child4 != null + // ? `current_holders.orgChild4Id IN (:...node)` + // : "1=1", + // { + // node: _data.child4, + // }, + // ) .andWhere( posType != undefined && posType != null && posType != "" ? "posType.posTypeName LIKE :keyword1" @@ -2647,11 +2690,11 @@ export class ProfileController extends Controller { ) .andWhere(nodeCondition, { nodeId: nodeId, - }) + }) .skip((page - 1) * pageSize) .take(pageSize) .getManyAndCount(); - + const findRevision = await this.orgRevisionRepo.findOne({ where: { orgRevisionIsCurrent: true }, }); diff --git a/src/interfaces/permission.ts b/src/interfaces/permission.ts index 6bafa7b1..e385d41b 100644 --- a/src/interfaces/permission.ts +++ b/src/interfaces/permission.ts @@ -32,12 +32,12 @@ class CheckAuth { let permission = false; let role = x.roles.find((x: any) => x.authSysId == system); if (!role) throw "ไม่มีสิทธิ์เข้าระบบ"; + if (role.attrOwnership == "OWNER") return "OWNER"; if (action.trim().toLocaleUpperCase() == "CREATE") permission = role.attrIsCreate; if (action.trim().toLocaleUpperCase() == "DELETE") permission = role.attrIsDelete; if (action.trim().toLocaleUpperCase() == "GET") permission = role.attrIsGet; if (action.trim().toLocaleUpperCase() == "LIST") permission = role.attrIsList; if (action.trim().toLocaleUpperCase() == "UPDATE") permission = role.attrIsUpdate; - if (role.attrOwnership == "OWNER") permission = true; if (permission == false) throw "ไม่มีสิทธิ์ใช้งานระบบนี้"; return role.attrPrivilege; }) @@ -45,6 +45,87 @@ class CheckAuth { throw new HttpError(HttpStatus.FORBIDDEN, x); }); } + public async PermissionOrg(req: RequestWithUser, system: string, action: string) { + if ( + req.headers.hasOwnProperty("api_key") && + req.headers["api_key"] && + req.headers["api_key"] == process.env.API_KEY + ) { + return null; + } + return await new CallAPI() + .GetData(req, "/org/permission/org") + .then(async (x) => { + let privilege = null; + if (action.trim().toLocaleUpperCase() == "CREATE") + privilege = await this.PermissionCreate(req, system); + if (action.trim().toLocaleUpperCase() == "DELETE") + privilege = await this.PermissionDelete(req, system); + if (action.trim().toLocaleUpperCase() == "GET") + privilege = await this.PermissionGet(req, system); + if (action.trim().toLocaleUpperCase() == "LIST") + privilege = await this.PermissionList(req, system); + if (action.trim().toLocaleUpperCase() == "UPDATE") + privilege = await this.PermissionUpdate(req, system); + + let data: any = { + root: [null], + child1: [null], + child2: [null], + child3: [null], + child4: [null], + }; + let node = 4; + if (x.orgChild1Id == null) { + node = 0; + } else if (x.orgChild2Id == null) { + node = 1; + } else if (x.orgChild3Id == null) { + node = 2; + } else if (x.orgChild4Id == null) { + node = 3; + } + if (privilege == "ROOT") { + data = { + root: [x.orgRootId], + child1: null, + child2: null, + child3: null, + child4: null, + }; + } else if (privilege == "CHILD") { + data = { + root: node >= 0 ? [x.orgRootId] : null, + child1: node >= 1 ? [x.orgChild1Id] : null, + child2: node >= 2 ? [x.orgChild2Id] : null, + child3: node >= 3 ? [x.orgChild3Id] : null, + child4: node >= 4 ? [x.orgChild4Id] : null, + }; + } else if (privilege == "NORMAL") { + data = { + root: [x.orgRootId], + child1: [x.orgChild1Id], + child2: [x.orgChild2Id], + child3: [x.orgChild3Id], + child4: [x.orgChild4Id], + }; + } else if (privilege == "SPECIFIC") { + } else if (privilege == "OWNER") { + data = { + root: null, + child1: null, + child2: null, + child3: null, + child4: null, + }; + } + + return data; + }) + .catch((x) => { + throw new HttpError(HttpStatus.FORBIDDEN, x); + }); + } public async PermissionCreate(req: RequestWithUser, system: string) { return await this.Permission(req, system, "CREATE"); } @@ -60,6 +141,22 @@ class CheckAuth { public async PermissionUpdate(req: RequestWithUser, system: string) { return await this.Permission(req, system, "UPDATE"); } + + public async PermissionOrgCreate(req: RequestWithUser, system: string) { + return await this.PermissionOrg(req, system, "CREATE"); + } + public async PermissionOrgDelete(req: RequestWithUser, system: string) { + return await this.PermissionOrg(req, system, "DELETE"); + } + public async PermissionOrgGet(req: RequestWithUser, system: string) { + return await this.PermissionOrg(req, system, "GET"); + } + public async PermissionOrgList(req: RequestWithUser, system: string) { + return await this.PermissionOrg(req, system, "LIST"); + } + public async PermissionOrgUpdate(req: RequestWithUser, system: string) { + return await this.PermissionOrg(req, system, "UPDATE"); + } } export default CheckAuth;