diff --git a/src/controllers/ApiManageController.ts b/src/controllers/ApiManageController.ts index 1f89e98d..3effdbd5 100644 --- a/src/controllers/ApiManageController.ts +++ b/src/controllers/ApiManageController.ts @@ -106,10 +106,10 @@ export class ApiManageController extends Controller { code: "organization", name: "ข้อมูลโครงสร้าง", }, - { - code: "position", - name: "ข้อมูลอัตรากำลัง", - }, + // { + // code: "position", + // name: "ข้อมูลอัตรากำลัง", + // }, ]; // รายการเอนทิตีทั้งหมด @@ -273,49 +273,49 @@ export class ApiManageController extends Controller { description: "ข้อมูลส่วนราชการ ระดับที่ 4", system: ["organization"], }, - { - name: "PosMaster", - repository: this.posMasterRepository, - description: "ข้อมูลอัตรากำลัง", - isMain: true, - system: ["position"], - }, - { - name: "Position", - repository: this.positionRepository, - description: "ข้อมูลตำแหน่ง", - system: ["position"], - }, - { - name: "OrgRoot", - repository: this.orgRootRepository, - description: "ข้อมูลหน่วยงาน", - system: ["position"], - }, - { - name: "OrgChild1", - repository: this.orgChild1Repository, - description: "ข้อมูลส่วนราชการ ระดับที่ 1", - system: ["position"], - }, - { - name: "OrgChild2", - repository: this.orgChild2Repository, - description: "ข้อมูลส่วนราชการ ระดับที่ 2", - system: ["position"], - }, - { - name: "OrgChild3", - repository: this.orgChild3Repository, - description: "ข้อมูลส่วนราชการ ระดับที่ 3", - system: ["position"], - }, - { - name: "OrgChild4", - repository: this.orgChild4Repository, - description: "ข้อมูลส่วนราชการ ระดับที่ 4", - system: ["position"], - }, + // { + // name: "PosMaster", + // repository: this.posMasterRepository, + // description: "ข้อมูลอัตรากำลัง", + // isMain: true, + // system: ["position"], + // }, + // { + // name: "Position", + // repository: this.positionRepository, + // description: "ข้อมูลตำแหน่ง", + // system: ["position"], + // }, + // { + // name: "OrgRoot", + // repository: this.orgRootRepository, + // description: "ข้อมูลหน่วยงาน", + // system: ["position"], + // }, + // { + // name: "OrgChild1", + // repository: this.orgChild1Repository, + // description: "ข้อมูลส่วนราชการ ระดับที่ 1", + // system: ["position"], + // }, + // { + // name: "OrgChild2", + // repository: this.orgChild2Repository, + // description: "ข้อมูลส่วนราชการ ระดับที่ 2", + // system: ["position"], + // }, + // { + // name: "OrgChild3", + // repository: this.orgChild3Repository, + // description: "ข้อมูลส่วนราชการ ระดับที่ 3", + // system: ["position"], + // }, + // { + // name: "OrgChild4", + // repository: this.orgChild4Repository, + // description: "ข้อมูลส่วนราชการ ระดับที่ 4", + // system: ["position"], + // }, // { // name: "Profile", // repository: this.profileRepository, @@ -343,13 +343,13 @@ export class ApiManageController extends Controller { "orgChild2Id", "orgChild3Id", "orgChild4Id", - "ancestorDNA", "keycloak", "commandId", "prefixMain", "authRoleId", "next_holderId", "current_holderId", + "ancestorDNA", ]; // ฟิลด์ที่ไม่ต้องการแสดงในผลลัพธ์ // การแทนที่ฟิลด์ ID ด้วยฟิลด์ Name สำหรับ Profile entity diff --git a/src/controllers/ApiWebServiceController.ts b/src/controllers/ApiWebServiceController.ts index 8673e684..1b4ffb51 100644 --- a/src/controllers/ApiWebServiceController.ts +++ b/src/controllers/ApiWebServiceController.ts @@ -189,6 +189,22 @@ export class ApiWebServiceController extends Controller { return conditions.length > 0 ? `(${conditions.join(" OR ")})` : "1=1"; } + /** + * rename ancestorDNA to id + * @summary เปลี่ยนชื่อฟิลด์ ancestorDNA เป็น id + */ + private renameAncestorDnaToId(obj: any): any { + if (!obj || typeof obj !== "object") { + return obj; + } + const result = { ...obj }; + if (result.ancestorDNA !== undefined) { + result.id = result.ancestorDNA; + delete result.ancestorDNA; + } + return result; + } + /** * list fields by systems * @summary รายการ fields ตาม systems @@ -227,6 +243,28 @@ export class ApiWebServiceController extends Controller { selectedFieldsByTable[attr.tbName].add(attr.propertyKey); }); + // สำหรับ Organization: ให้รวม ancestorDNA เสมอ เพื่อแสดงเป็น id + if (system === "organization") { + // สำหรับ OrgRoot + const ancestorDnaField = "OrgRoot.ancestorDNA"; + if (!propertyKey.includes(ancestorDnaField)) { + propertyKey.push(ancestorDnaField); + } + if (!selectedFieldsByTable["OrgRoot"]) { + selectedFieldsByTable["OrgRoot"] = new Set(); + } + selectedFieldsByTable["OrgRoot"].add("ancestorDNA"); + + // สำหรับ OrgChild1, OrgChild2, OrgChild3, OrgChild4 + const childTables = ["OrgChild1", "OrgChild2", "OrgChild3", "OrgChild4"]; + childTables.forEach((table) => { + if (!selectedFieldsByTable[table]) { + selectedFieldsByTable[table] = new Set(); + } + selectedFieldsByTable[table].add("ancestorDNA"); + }); + } + let tbMain: string = ""; let condition: string = "1=1"; if (system == "registry") { @@ -594,6 +632,14 @@ export class ApiWebServiceController extends Controller { flattened[nameField] = rest[alias][config.joinField]; delete flattened[alias]; } + // Also handle nested relation objects (e.g., "posLevel": { "posLevelName": "..." }) + if ( + rest[config.joinRelation] && + rest[config.joinRelation][config.joinField] !== undefined + ) { + flattened[nameField] = rest[config.joinRelation][config.joinField]; + delete flattened[config.joinRelation]; + } }); return flattened; } @@ -632,6 +678,14 @@ export class ApiWebServiceController extends Controller { } // Remove the joined table object delete flattened[alias]; + // Also handle nested relation objects (e.g., "posType": { "posTypeName": "..." }) + if ( + rest[config.joinRelation] && + rest[config.joinRelation][config.joinField] !== undefined + ) { + flattened[nameField] = rest[config.joinRelation][config.joinField]; + delete flattened[config.joinRelation]; + } }); return flattened; } @@ -679,7 +733,12 @@ export class ApiWebServiceController extends Controller { const out: any = {}; fields.forEach((field) => { if (obj[field] !== undefined) { - out[field] = obj[field]; + // ถ้าเป็น ancestorDNA ให้เปลี่ยนชื่อเป็น id + if (field === "ancestorDNA") { + out.id = obj[field]; + } else { + out[field] = obj[field]; + } } }); return out; @@ -707,14 +766,14 @@ export class ApiWebServiceController extends Controller { const [child1Rows, child2Rows, child3Rows, child4Rows] = await Promise.all([ AppDataSource.getRepository(OrgChild1) .createQueryBuilder("OrgChild1") - .select(buildSelect("OrgChild1", ["id", "orgRootId"], child1VisibleFields)) + .select(buildSelect("OrgChild1", ["id", "orgRootId", "ancestorDNA"], child1VisibleFields)) .where("OrgChild1.orgRootId IN (:...rootIds)", { rootIds }) .orderBy("OrgChild1.id", "ASC") .getMany(), AppDataSource.getRepository(OrgChild2) .createQueryBuilder("OrgChild2") .select( - buildSelect("OrgChild2", ["id", "orgRootId", "orgChild1Id"], child2VisibleFields), + buildSelect("OrgChild2", ["id", "orgRootId", "orgChild1Id", "ancestorDNA"], child2VisibleFields), ) .where("OrgChild2.orgRootId IN (:...rootIds)", { rootIds }) .orderBy("OrgChild2.id", "ASC") @@ -724,7 +783,7 @@ export class ApiWebServiceController extends Controller { .select( buildSelect( "OrgChild3", - ["id", "orgRootId", "orgChild1Id", "orgChild2Id"], + ["id", "orgRootId", "orgChild1Id", "orgChild2Id", "ancestorDNA"], child3VisibleFields, ), ) @@ -736,7 +795,7 @@ export class ApiWebServiceController extends Controller { .select( buildSelect( "OrgChild4", - ["id", "orgRootId", "orgChild1Id", "orgChild2Id", "orgChild3Id"], + ["id", "orgRootId", "orgChild1Id", "orgChild2Id", "orgChild3Id", "ancestorDNA"], child4VisibleFields, ), ) @@ -804,6 +863,36 @@ export class ApiWebServiceController extends Controller { } responseData = Array.from(rootMap.values()); + + // สำหรับ Organization: เปลี่ยนชื่อ ancestorDNA เป็น id + responseData = responseData.map((root: any) => { + const renamed = this.renameAncestorDnaToId(root); + if (Array.isArray(renamed.children)) { + renamed.children = renamed.children.map((child1: any) => { + const renamedChild1 = this.renameAncestorDnaToId(child1); + if (Array.isArray(renamedChild1.children)) { + renamedChild1.children = renamedChild1.children.map((child2: any) => { + const renamedChild2 = this.renameAncestorDnaToId(child2); + if (Array.isArray(renamedChild2.children)) { + renamedChild2.children = renamedChild2.children.map((child3: any) => { + const renamedChild3 = this.renameAncestorDnaToId(child3); + if (Array.isArray(renamedChild3.children)) { + renamedChild3.children = renamedChild3.children.map((child4: any) => + this.renameAncestorDnaToId(child4), + ); + } + return renamedChild3; + }); + } + return renamedChild2; + }); + } + return renamedChild1; + }); + } + return renamed; + }); + responseTotal = responseData.length; }