diff --git a/src/controllers/OrgChild1Controller.ts b/src/controllers/OrgChild1Controller.ts index 3440fe3f..c318c635 100644 --- a/src/controllers/OrgChild1Controller.ts +++ b/src/controllers/OrgChild1Controller.ts @@ -47,17 +47,18 @@ export class OrgChild1Controller { */ @Get("{id}") async GetChild1(@Path() id: string) { - const orgChild1 = await this.child1Repository.findOne({ where: { id } }); + const orgChild1 = await this.child1Repository.findOne({ + where: { id }, + relations: ["orgRoot"], + }); if (!orgChild1) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล โครงสร้างระดับ 1"); } - const orgRoot = await this.orgRootRepository.findOne({ where: { id: orgChild1.orgRootId } }); - if (!orgRoot) { - throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล โครงสร้างระดับ Root"); - } try { const getOrgChild1 = { orgChild1Id: orgChild1.id, + orgRootName: orgChild1.orgRoot.orgRootName, + orgName: `${orgChild1.orgChild1Name}/${orgChild1.orgRoot.orgRootName}`, orgChild1Name: orgChild1.orgChild1Name, orgChild1ShortName: orgChild1.orgChild1ShortName, orgChild1Code: orgChild1.orgChild1Code, @@ -67,7 +68,7 @@ export class OrgChild1Controller { orgChild1PhoneIn: orgChild1.orgChild1PhoneIn, orgChild1Fax: orgChild1.orgChild1Fax, orgRevisionId: orgChild1.orgRevisionId, - orgCode: orgRoot.orgRootCode + orgChild1.orgChild1Code, + orgCode: orgChild1.orgRoot.orgRootCode + orgChild1.orgChild1Code, }; return new HttpSuccess(getOrgChild1); } catch (error) { diff --git a/src/controllers/OrgChild2Controller.ts b/src/controllers/OrgChild2Controller.ts index 572d68e3..a2dc7d11 100644 --- a/src/controllers/OrgChild2Controller.ts +++ b/src/controllers/OrgChild2Controller.ts @@ -48,17 +48,18 @@ export class OrgChild2Controller extends Controller { */ @Get("{id}") async GetChild2(@Path() id: string) { - const orgChild2 = await this.child2Repository.findOne({ where: { id } }); + const orgChild2 = await this.child2Repository.findOne({ + where: { id }, + relations: ["orgRoot", "orgChild1"], + }); if (!orgChild2) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล โครงสร้างระดับ 2"); } - const orgRoot = await this.orgRootRepository.findOne({ where: { id: orgChild2.orgRootId } }); - if (!orgRoot) { - throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล โครงสร้างระดับ Root"); - } try { const getOrgChild2 = { orgChild2Id: orgChild2.id, + orgRootName: orgChild2.orgRoot.orgRootName, + orgName: `${orgChild2.orgChild2Name}/${orgChild2.orgChild1.orgChild1Name}/${orgChild2.orgRoot.orgRootName}`, orgChild2Name: orgChild2.orgChild2Name, orgChild2ShortName: orgChild2.orgChild2ShortName, orgChild2Code: orgChild2.orgChild2Code, @@ -68,7 +69,7 @@ export class OrgChild2Controller extends Controller { orgChild2PhoneIn: orgChild2.orgChild2PhoneIn, orgChild2Fax: orgChild2.orgChild2Fax, orgRevisionId: orgChild2.orgRevisionId, - orgCode: orgRoot.orgRootCode + orgChild2.orgChild2Code, + orgCode: orgChild2.orgRoot.orgRootCode + orgChild2.orgChild2Code, }; return new HttpSuccess(getOrgChild2); } catch (error) { diff --git a/src/controllers/OrgChild3Controller.ts b/src/controllers/OrgChild3Controller.ts index dcfed9e1..58dc225f 100644 --- a/src/controllers/OrgChild3Controller.ts +++ b/src/controllers/OrgChild3Controller.ts @@ -46,17 +46,18 @@ export class OrgChild3Controller { */ @Get("{id}") async GetChild3(@Path() id: string) { - const orgChild3 = await this.child3Repository.findOne({ where: { id } }); + const orgChild3 = await this.child3Repository.findOne({ + where: { id }, + relations: ["orgRoot", "orgChild1", "orgChild2"], + }); if (!orgChild3) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล โครงสร้างระดับ 3"); } - const orgRoot = await this.orgRootRepository.findOne({ where: { id: orgChild3.orgRootId } }); - if (!orgRoot) { - throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล โครงสร้างระดับ Root"); - } try { const getOrgChild3 = { orgChild3Id: orgChild3.id, + orgRootName: orgChild3.orgRoot.orgRootName, + orgName: `${orgChild3.orgChild3Name}/${orgChild3.orgChild2.orgChild2Name}/${orgChild3.orgChild1.orgChild1Name}/${orgChild3.orgRoot.orgRootName}`, orgChild3Name: orgChild3.orgChild3Name, orgChild3ShortName: orgChild3.orgChild3ShortName, orgChild3Code: orgChild3.orgChild3Code, @@ -66,7 +67,7 @@ export class OrgChild3Controller { orgChild3PhoneIn: orgChild3.orgChild3PhoneIn, orgChild3Fax: orgChild3.orgChild3Fax, orgRevisionId: orgChild3.orgRevisionId, - orgCode: orgRoot.orgRootCode + orgChild3.orgChild3Code, + orgCode: orgChild3.orgRoot.orgRootCode + orgChild3.orgChild3Code, }; return new HttpSuccess(getOrgChild3); } catch (error) { diff --git a/src/controllers/OrgChild4Controller.ts b/src/controllers/OrgChild4Controller.ts index efdbfd71..08d15b0c 100644 --- a/src/controllers/OrgChild4Controller.ts +++ b/src/controllers/OrgChild4Controller.ts @@ -48,17 +48,18 @@ export class OrgChild4Controller extends Controller { */ @Get("{id}") async GetChild4(@Path() id: string) { - const orgChild4 = await this.child4Repository.findOne({ where: { id } }); + const orgChild4 = await this.child4Repository.findOne({ + where: { id }, + relations: ["orgRoot", "orgChild1", "orgChild2", "orgChild3"], + }); if (!orgChild4) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล โครงสร้างระดับ 4"); } - const orgRoot = await this.orgRootRepository.findOne({ where: { id: orgChild4.orgRootId } }); - if (!orgRoot) { - throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล โครงสร้างระดับ Root"); - } try { const getOrgChild4 = { orgChild4Id: orgChild4.id, + orgRootName: orgChild4.orgRoot.orgRootName, + orgName: `${orgChild4.orgChild4Name}/${orgChild4.orgChild3.orgChild3Name}/${orgChild4.orgChild2.orgChild2Name}/${orgChild4.orgChild1.orgChild1Name}/${orgChild4.orgRoot.orgRootName}`, orgChild4Name: orgChild4.orgChild4Name, orgChild4ShortName: orgChild4.orgChild4ShortName, orgChild4Code: orgChild4.orgChild4Code, @@ -68,7 +69,7 @@ export class OrgChild4Controller extends Controller { orgChild4PhoneIn: orgChild4.orgChild4PhoneIn, orgChild4Fax: orgChild4.orgChild4Fax, orgRevisionId: orgChild4.orgRevisionId, - orgCode: orgRoot.orgRootCode + orgChild4.orgChild4Code, + orgCode: orgChild4.orgRoot.orgRootCode + orgChild4.orgChild4Code, }; return new HttpSuccess(getOrgChild4); } catch (error) { diff --git a/src/controllers/OrgRootController.ts b/src/controllers/OrgRootController.ts index a64b3edc..0b2c2f9d 100644 --- a/src/controllers/OrgRootController.ts +++ b/src/controllers/OrgRootController.ts @@ -46,6 +46,7 @@ export class OrgRootController extends Controller { const getOrgRoot = { orgRootId: orgRoot.id, orgRootName: orgRoot.orgRootName, + orgName: "-", orgRootShortName: orgRoot.orgRootShortName, orgRootCode: orgRoot.orgRootCode, orgRootRank: orgRoot.orgRootRank, diff --git a/src/controllers/OrganizationController.ts b/src/controllers/OrganizationController.ts index 03d8ca0c..781696e6 100644 --- a/src/controllers/OrganizationController.ts +++ b/src/controllers/OrganizationController.ts @@ -595,6 +595,7 @@ export class OrganizationController extends Controller { orgTreePhoneIn: orgChild2.orgChild2PhoneIn, orgTreeFax: orgChild2.orgChild2Fax, orgRevisionId: orgRoot.orgRevisionId, + orgRootName: orgRoot.orgRootName, children: orgChild3Data .filter((orgChild3) => orgChild3.orgChild2Id === orgChild2.id) .map((orgChild3) => ({ @@ -802,119 +803,119 @@ export class OrganizationController extends Controller { * */ @Post("sort") - async Sort( - @Body() requestBody: { - id: string, type: number, sortId: string[] - } - ){ + async Sort(@Body() requestBody: { id: string; type: number; sortId: string[] }) { try { - switch(requestBody.type){ - case 0 :{ - const revisionId = await this.orgRevisionRepository.findOne({ where: {id : requestBody.id} }) - if(!revisionId?.id){ - throw new HttpError(HttpStatusCode.NOT_FOUND, "not found revisionId: "+ requestBody.id); + switch (requestBody.type) { + case 0: { + const revisionId = await this.orgRevisionRepository.findOne({ + where: { id: requestBody.id }, + }); + if (!revisionId?.id) { + throw new HttpError( + HttpStatusCode.NOT_FOUND, + "not found revisionId: " + requestBody.id, + ); } - const listRootId = await this.orgRootRepository.find({ - where: { orgRevisionId: requestBody.id}, - select: ["id", "orgRootOrder"], - }) - if(!listRootId){ + const listRootId = await this.orgRootRepository.find({ + where: { orgRevisionId: requestBody.id }, + select: ["id", "orgRootOrder"], + }); + if (!listRootId) { throw new HttpError(HttpStatusCode.NOT_FOUND, "not found rootId."); } const sortData = listRootId.map((data) => ({ - id : data.id, - orgRootOrder: requestBody.sortId.indexOf(data.id) + 1 + id: data.id, + orgRootOrder: requestBody.sortId.indexOf(data.id) + 1, })); await this.orgRootRepository.save(sortData); break; } - case 1 :{ - const rootId = await this.orgRootRepository.findOne({ where: {id : requestBody.id} }) - if(!rootId?.id){ - throw new HttpError(HttpStatusCode.NOT_FOUND, "not found rootId: "+ requestBody.id); + case 1: { + const rootId = await this.orgRootRepository.findOne({ where: { id: requestBody.id } }); + if (!rootId?.id) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "not found rootId: " + requestBody.id); } - const listChild1Id = await this.child1Repository.find({ - where: { orgRootId: requestBody.id}, - select: ["id", "orgChild1Order"], - }) - if(!listChild1Id){ + const listChild1Id = await this.child1Repository.find({ + where: { orgRootId: requestBody.id }, + select: ["id", "orgChild1Order"], + }); + if (!listChild1Id) { throw new HttpError(HttpStatusCode.NOT_FOUND, "not found child1Id."); } const sortData = listChild1Id.map((data) => ({ - id : data.id, - orgChild1Order: requestBody.sortId.indexOf(data.id) + 1 + id: data.id, + orgChild1Order: requestBody.sortId.indexOf(data.id) + 1, })); await this.child1Repository.save(sortData); break; } - case 2 :{ - const child1Id = await this.child1Repository.findOne({ where: {id : requestBody.id} }) - if(!child1Id?.id){ - throw new HttpError(HttpStatusCode.NOT_FOUND, "not found child1Id: "+ requestBody.id); + case 2: { + const child1Id = await this.child1Repository.findOne({ where: { id: requestBody.id } }); + if (!child1Id?.id) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "not found child1Id: " + requestBody.id); } - const listChild2Id = await this.child2Repository.find({ - where: { orgChild1Id: requestBody.id}, - select: ["id", "orgChild2Order"], - }) - if(!listChild2Id){ + const listChild2Id = await this.child2Repository.find({ + where: { orgChild1Id: requestBody.id }, + select: ["id", "orgChild2Order"], + }); + if (!listChild2Id) { throw new HttpError(HttpStatusCode.NOT_FOUND, "not found child2Id."); } const sortData = listChild2Id.map((data) => ({ - id : data.id, - orgChild2Order: requestBody.sortId.indexOf(data.id) + 1 + id: data.id, + orgChild2Order: requestBody.sortId.indexOf(data.id) + 1, })); await this.child2Repository.save(sortData); break; } - case 3 :{ - const child2Id = await this.child2Repository.findOne({ where: {id : requestBody.id} }) - if(!child2Id?.id){ - throw new HttpError(HttpStatusCode.NOT_FOUND, "not found child2Id: "+ requestBody.id); + case 3: { + const child2Id = await this.child2Repository.findOne({ where: { id: requestBody.id } }); + if (!child2Id?.id) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "not found child2Id: " + requestBody.id); } - const listChild3Id = await this.child3Repository.find({ - where: { orgChild2Id: requestBody.id}, - select: ["id", "orgChild3Order"], - }) - if(!listChild3Id){ + const listChild3Id = await this.child3Repository.find({ + where: { orgChild2Id: requestBody.id }, + select: ["id", "orgChild3Order"], + }); + if (!listChild3Id) { throw new HttpError(HttpStatusCode.NOT_FOUND, "not found child3Id."); } const sortData = listChild3Id.map((data) => ({ - id : data.id, - orgChild3Order: requestBody.sortId.indexOf(data.id) + 1 + id: data.id, + orgChild3Order: requestBody.sortId.indexOf(data.id) + 1, })); await this.child3Repository.save(sortData); break; } - case 4 :{ - const child3Id = await this.child3Repository.findOne({ where: {id : requestBody.id} }) - if(!child3Id?.id){ - throw new HttpError(HttpStatusCode.NOT_FOUND, "not found child3Id: "+ requestBody.id); + case 4: { + const child3Id = await this.child3Repository.findOne({ where: { id: requestBody.id } }); + if (!child3Id?.id) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "not found child3Id: " + requestBody.id); } - const listChild4Id = await this.child4Repository.find({ - where: { orgChild3Id: requestBody.id}, - select: ["id", "orgChild4Order"], - }) - if(!listChild4Id){ + const listChild4Id = await this.child4Repository.find({ + where: { orgChild3Id: requestBody.id }, + select: ["id", "orgChild4Order"], + }); + if (!listChild4Id) { throw new HttpError(HttpStatusCode.NOT_FOUND, "not found child4Id."); } const sortData = listChild4Id.map((data) => ({ - id : data.id, - orgChild4Order: requestBody.sortId.indexOf(data.id) + 1 + id: data.id, + orgChild4Order: requestBody.sortId.indexOf(data.id) + 1, })); await this.child4Repository.save(sortData); break; } - + default: - throw new HttpError(HttpStatusCode.NOT_FOUND, "not found type: "+ requestBody.type); + throw new HttpError(HttpStatusCode.NOT_FOUND, "not found type: " + requestBody.type); } return new HttpSuccess(); - } - catch(error){ + } catch (error) { return error; } } diff --git a/src/controllers/PositionController.ts b/src/controllers/PositionController.ts index f86d46d9..420ffd3d 100644 --- a/src/controllers/PositionController.ts +++ b/src/controllers/PositionController.ts @@ -418,25 +418,35 @@ export class PositionController extends Controller { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล"); } - const orgRoot = await this.orgRootRepository.findOne({ - where: { id: requestBody.orgRootId }, - }); - if (!orgRoot) { - const orgChild1 = await this.child1Repository.findOne({ - where: { id: requestBody.orgChild1Id }, + let orgRoot: any = null; + if (requestBody.orgRootId != null) + orgRoot = await this.orgRootRepository.findOne({ + where: { id: requestBody.orgRootId }, }); - if (!orgChild1) { - const orgChild2 = await this.child2Repository.findOne({ - where: { id: requestBody.orgChild2Id }, + if (!orgRoot) { + let orgChild1: any = null; + if (requestBody.orgChild1Id != null) + orgChild1 = await this.child1Repository.findOne({ + where: { id: requestBody.orgChild1Id }, }); - if (!orgChild2) { - const orgChild3 = await this.child3Repository.findOne({ - where: { id: requestBody.orgChild3Id }, + if (!orgChild1) { + let orgChild2: any = null; + if (requestBody.orgChild2Id != null) + orgChild2 = await this.child2Repository.findOne({ + where: { id: requestBody.orgChild2Id }, }); - if (!orgChild3) { - const orgChild4 = await this.child4Repository.findOne({ - where: { id: requestBody.orgChild4Id }, + if (!orgChild2) { + let orgChild3: any = null; + if (requestBody.orgChild3Id != null) + orgChild3 = await this.child3Repository.findOne({ + where: { id: requestBody.orgChild3Id }, }); + if (!orgChild3) { + let orgChild4: any = null; + if (requestBody.orgChild4Id != null) + orgChild4 = await this.child4Repository.findOne({ + where: { id: requestBody.orgChild4Id }, + }); if (!orgChild4) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลโครงสร้าง"); } else { @@ -541,25 +551,35 @@ export class PositionController extends Controller { posMaster.orgChild3Id = ""; posMaster.orgChild4Id = ""; - const orgRoot = await this.orgRootRepository.findOne({ - where: { id: requestBody.orgRootId }, - }); - if (!orgRoot) { - const orgChild1 = await this.child1Repository.findOne({ - where: { id: requestBody.orgChild1Id }, + let orgRoot: any = null; + if (requestBody.orgRootId != null) + orgRoot = await this.orgRootRepository.findOne({ + where: { id: requestBody.orgRootId }, }); - if (!orgChild1) { - const orgChild2 = await this.child2Repository.findOne({ - where: { id: requestBody.orgChild2Id }, + if (!orgRoot) { + let orgChild1: any = null; + if (requestBody.orgChild1Id != null) + orgChild1 = await this.child1Repository.findOne({ + where: { id: requestBody.orgChild1Id }, }); - if (!orgChild2) { - const orgChild3 = await this.child3Repository.findOne({ - where: { id: requestBody.orgChild3Id }, + if (!orgChild1) { + let orgChild2: any = null; + if (requestBody.orgChild2Id != null) + orgChild2 = await this.child2Repository.findOne({ + where: { id: requestBody.orgChild2Id }, }); - if (!orgChild3) { - const orgChild4 = await this.child4Repository.findOne({ - where: { id: requestBody.orgChild4Id }, + if (!orgChild2) { + let orgChild3: any = null; + if (requestBody.orgChild3Id != null) + orgChild3 = await this.child3Repository.findOne({ + where: { id: requestBody.orgChild3Id }, }); + if (!orgChild3) { + let orgChild4: any = null; + if (requestBody.orgChild4Id != null) + orgChild4 = await this.child4Repository.findOne({ + where: { id: requestBody.orgChild4Id }, + }); if (!orgChild4) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลโครงสร้าง"); } else { @@ -737,36 +757,38 @@ export class PositionController extends Controller { const posMaster = await this.posMasterRepository.find({ where: typeCondition, }); - if (!posMaster || posMaster.length === 0 ) { + if (!posMaster || posMaster.length === 0) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล"); } - const formattedData = await Promise.all(posMaster.map(async (posMaster) => { - const positions = await this.positionRepository.find({ - where: { posMasterId: posMaster.id }, - }); - - return { - id: posMaster.id, - posMasterNoPrefix: posMaster.posMasterNoPrefix, - posMasterNo: posMaster.posMasterNo, - posMasterNoSuffix: posMaster.posMasterNoSuffix, - positions: positions.map((position) => ({ - id: position.id, - positionName: position.positionName, - positionField: position.positionField, - posTypeId: position.posTypeId, - posTypeName: position.posType == null ? null : position.posType.posTypeName, - posLevelId: position.posLevelId, - posLevelName: position.posLevel == null ? null : position.posLevel.posLevelName, - posExecutiveId: position.posExecutiveId, - posExecutiveName: - position.posExecutive == null ? null : position.posExecutive.posExecutiveName, - positionExecutiveField: position.positionExecutiveField, - positionArea: position.positionArea, - positionIsSelected: position.positionIsSelected, - })), - }; - })); + const formattedData = await Promise.all( + posMaster.map(async (posMaster) => { + const positions = await this.positionRepository.find({ + where: { posMasterId: posMaster.id }, + }); + + return { + id: posMaster.id, + posMasterNoPrefix: posMaster.posMasterNoPrefix, + posMasterNo: posMaster.posMasterNo, + posMasterNoSuffix: posMaster.posMasterNoSuffix, + positions: positions.map((position) => ({ + id: position.id, + positionName: position.positionName, + positionField: position.positionField, + posTypeId: position.posTypeId, + posTypeName: position.posType == null ? null : position.posType.posTypeName, + posLevelId: position.posLevelId, + posLevelName: position.posLevel == null ? null : position.posLevel.posLevelName, + posExecutiveId: position.posExecutiveId, + posExecutiveName: + position.posExecutive == null ? null : position.posExecutive.posExecutiveName, + positionExecutiveField: position.positionExecutiveField, + positionArea: position.positionArea, + positionIsSelected: position.positionIsSelected, + })), + }; + }), + ); return new HttpSuccess(formattedData); } catch (error) { return error;