diff --git a/src/controllers/ApiManageController.ts b/src/controllers/ApiManageController.ts index 8caad7fd..22db1c7d 100644 --- a/src/controllers/ApiManageController.ts +++ b/src/controllers/ApiManageController.ts @@ -348,6 +348,69 @@ export class ApiManageController extends Controller { "commandId", ]; // ฟิลด์ที่ไม่ต้องการแสดงในผลลัพธ์ + // การแทนที่ฟิลด์ ID ด้วยฟิลด์ Name สำหรับ Profile entity + private readonly PROFILE_FIELD_REPLACEMENTS: Record< + string, + { propertyName: string; type: string; comment: string; joinTable: string; joinField: string } + > = { + posLevelId: { + propertyName: "posLevelName", + type: "string", + comment: "ระดับตำแหน่ง", + joinTable: "PosLevel", + joinField: "posLevelName", + }, + posTypeId: { + propertyName: "posTypeName", + type: "string", + comment: "ประเภทตำแหน่ง", + joinTable: "PosType", + joinField: "posTypeName", + }, + registrationProvinceId: { + propertyName: "registrationProvinceName", + type: "string", + comment: "จังหวัดตามทะเบียนบ้าน", + joinTable: "Province", + joinField: "name", + }, + registrationDistrictId: { + propertyName: "registrationDistrictName", + type: "string", + comment: "เขตตามทะเบียนบ้าน", + joinTable: "District", + joinField: "name", + }, + registrationSubDistrictId: { + propertyName: "registrationSubDistrictName", + type: "string", + comment: "แขวงตามทะเบียนบ้าน", + joinTable: "SubDistrict", + joinField: "name", + }, + currentProvinceId: { + propertyName: "currentProvinceName", + type: "string", + comment: "จังหวัดตามปัจจุบัน", + joinTable: "Province", + joinField: "name", + }, + currentDistrictId: { + propertyName: "currentDistrictName", + type: "string", + comment: "เขตตามปัจจุบัน", + joinTable: "District", + joinField: "name", + }, + currentSubDistrictId: { + propertyName: "currentSubDistrictName", + type: "string", + comment: "แขวงตามปัจจุบัน", + joinTable: "SubDistrict", + joinField: "name", + }, + }; + private validateSuperAdminRole(user: any): void { if (!user.role.includes("SUPER_ADMIN")) { throw new HttpError(HttpStatusCode.FORBIDDEN, "คุณไม่มีสิทธิ์ในการเข้าถึงข้อมูลนี้"); @@ -385,11 +448,8 @@ export class ApiManageController extends Controller { const result = this.entities .filter((entity) => entity.system.includes(system)) - .map(({ name, repository, description, isMain }) => ({ - tb: name, - description, - isMain: isMain || false, - propertys: repository.metadata.columns + .map(({ name, repository, description, isMain }) => { + let columns = repository.metadata.columns .filter( (column: any) => !column.isPrimary && !this.EXCLUDED_COLUMNS.includes(column.propertyName), @@ -399,8 +459,35 @@ export class ApiManageController extends Controller { type: typeof column.type === "string" ? column.type : "string", comment: column.comment, key: column.propertyName, - })), - })); + })); + + // Special handling for Profile entity - replace ID fields with name fields + if (name === "Profile") { + const replacementKeys = Object.keys(this.PROFILE_FIELD_REPLACEMENTS); + + // Remove ID fields that should be replaced + columns = columns.filter((col: { propertyName: string }) => + !replacementKeys.includes(col.propertyName), + ); + + // Add the corresponding name fields + const nameFields = replacementKeys.map((key) => ({ + propertyName: this.PROFILE_FIELD_REPLACEMENTS[key].propertyName, + type: "string", + comment: this.PROFILE_FIELD_REPLACEMENTS[key].comment, + key: this.PROFILE_FIELD_REPLACEMENTS[key].propertyName, + })); + + columns = [...columns, ...nameFields]; + } + + return { + tb: name, + description, + isMain: isMain || false, + propertys: columns, + }; + }); return new HttpSuccess(result); } catch (error) { diff --git a/src/controllers/ApiWebServiceController.ts b/src/controllers/ApiWebServiceController.ts index 61f3d54a..8a1e87be 100644 --- a/src/controllers/ApiWebServiceController.ts +++ b/src/controllers/ApiWebServiceController.ts @@ -21,6 +21,53 @@ export class ApiWebServiceController extends Controller { private orgRevisionRepository = AppDataSource.getRepository(OrgRevision); private apiHistoryRepository = AppDataSource.getRepository(ApiHistory); + // การแทนที่ฟิลด์ ID ด้วยฟิลด์ Name สำหรับ Profile entity + private readonly PROFILE_FIELD_REPLACEMENTS: Record< + string, + { propertyName: string; joinRelation: string; joinField: string } + > = { + posLevelName: { + propertyName: "posLevelId", + joinRelation: "posLevel", + joinField: "posLevelName", + }, + posTypeName: { + propertyName: "posTypeId", + joinRelation: "posType", + joinField: "posTypeName", + }, + registrationProvinceName: { + propertyName: "registrationProvinceId", + joinRelation: "registrationProvince", + joinField: "name", + }, + registrationDistrictName: { + propertyName: "registrationDistrictId", + joinRelation: "registrationDistrict", + joinField: "name", + }, + registrationSubDistrictName: { + propertyName: "registrationSubDistrictId", + joinRelation: "registrationSubDistrict", + joinField: "name", + }, + currentProvinceName: { + propertyName: "currentProvinceId", + joinRelation: "currentProvince", + joinField: "name", + }, + currentDistrictName: { + propertyName: "currentDistrictId", + joinRelation: "currentDistrict", + joinField: "name", + }, + currentSubDistrictName: { + propertyName: "currentSubDistrictId", + joinRelation: "currentSubDistrict", + joinField: "name", + }, + }; + /** * list fields by systems * @summary รายการ fields ตาม systems @@ -50,7 +97,7 @@ export class ApiWebServiceController extends Controller { } await isPermissionRequest(request, apiName.id); const offset = (page - 1) * pageSize; - const propertyKey = apiName.apiAttributes.map((attr) => `${attr.tbName}.${attr.propertyKey}`); + let propertyKey = apiName.apiAttributes.map((attr) => `${attr.tbName}.${attr.propertyKey}`); let tbMain: string = ""; let condition: string = "1=1"; @@ -92,6 +139,23 @@ export class ApiWebServiceController extends Controller { ...new Set(propertyKey.map((x) => x.split(".")[0]).filter((tb) => tb !== tbMain)), ]; + // สำหรับ Profile: ตรวจสอบฟิลด์ที่ต้องการ join และแปลง propertyKey + const profileFieldJoins: Record = {}; // alias -> relationName + if (tbMain === "Profile") { + propertyKey = propertyKey.map((key) => { + const [table, field] = key.split("."); + if (table === "Profile") { + const replacement = this.PROFILE_FIELD_REPLACEMENTS[field]; + if (replacement) { + const alias = `${table}_${replacement.joinRelation}`; + profileFieldJoins[alias] = replacement.joinRelation; + return `${alias}.${replacement.joinField}`; + } + } + return key; + }); + } + const queryBuilder = repo.createQueryBuilder(tbMain); // join กับตารารอง @@ -107,6 +171,13 @@ export class ApiWebServiceController extends Controller { }); } + // join สำหรับฟิลด์ Profile ที่ต้องการดึงค่าจากตารางอื่น + if (tbMain === "Profile" && Object.keys(profileFieldJoins).length > 0) { + Object.entries(profileFieldJoins).forEach(([alias, relationName]) => { + queryBuilder.leftJoin(`${tbMain}.${relationName}`, alias); + }); + } + // // เพิ่ม Main.id เพราะจะใช้ pk ในการแมบและนับจำนวน // if (!propertyKey.includes(`${Main}.id`)) { // propertyKey.push(`${Main}.id`); @@ -141,8 +212,22 @@ export class ApiWebServiceController extends Controller { // split object id ออกก่อน return const data = items.map((item) => { - const { [pk]: removedPk, ...x } = item; - return x; + const { [pk]: removedPk, ...rest } = item; + + // สำหรับ Profile: แปลงฟิลด์ที่มาจาก join กลับเป็นชื่อเดิม + if (tbMain === "Profile") { + const flattened: any = { ...rest }; + Object.entries(this.PROFILE_FIELD_REPLACEMENTS).forEach(([nameField, config]) => { + const alias = `${tbMain}_${config.joinRelation}`; + if (rest[alias] && rest[alias][config.joinField] !== undefined) { + flattened[nameField] = rest[alias][config.joinField]; + delete flattened[alias]; + } + }); + return flattened; + } + + return rest; }); // console.log("queryBuilder ===> ", queryBuilder.getQuery());