แก้ไขประเภท ระดับตำแหน่ง และจังหวัด อำเภอ ตำบลให้แสดงฟิลด์ที่เป็นชื่อให้เลือก
This commit is contained in:
parent
9fe91ce49c
commit
b2d59ef698
2 changed files with 182 additions and 10 deletions
|
|
@ -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<string, string> = {}; // 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());
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue