diff --git a/src/controllers/ApiWebServiceController.ts b/src/controllers/ApiWebServiceController.ts index 5ac66ed4..2a958387 100644 --- a/src/controllers/ApiWebServiceController.ts +++ b/src/controllers/ApiWebServiceController.ts @@ -59,7 +59,6 @@ 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 Main:string = "" @@ -81,9 +80,9 @@ export class ApiWebServiceController extends Controller { select: ["id"], where: { orgRevisionIsCurrent: true, orgRevisionIsDraft: false } }); - condition = `OrgRoot.orgRevisionId = '${revision?.id}'` + condition = `OrgRoot.orgRevisionId = "${revision?.id}"` } - + const repo = AppDataSource.getRepository(Main); const metadata = repo.metadata; @@ -101,7 +100,7 @@ export class ApiWebServiceController extends Controller { .filter((tb) => tb !== Main) ) ] - + const queryBuilder = repo.createQueryBuilder(Main) // join กับตารารอง @@ -109,48 +108,26 @@ export class ApiWebServiceController extends Controller { propertyOtherKey.forEach((tb) => { const relationName = relationMap[tb]; if (relationName) { - queryBuilder.leftJoinAndSelect(`${Main}.${relationName}`, tb); + queryBuilder.leftJoin(`${Main}.${relationName}`, tb); } }); } + // เพิ่ม Main.id เพราะจะใช้ pk ในการแมบและนับจำนวน + if (!propertyKey.includes(`${Main}.id`)) { + propertyKey.push(`${Main}.id`); + } + const [items, total] = await queryBuilder + .select(propertyKey) .where(condition) - .orderBy(`${Main}.createdAt`, "DESC") + // .orderBy(`${Main}.createdAt`, "DESC") .skip(offset) .take(pageSize) .getManyAndCount(); - const results = items.map(item => { - const result: any = {}; - propertyKey.forEach(key => { - const [table, field] = key.split("."); - - if (table === Main) { - result[field] = item[field]; - } - else { - const relationName = relationMap[table]; - if (!relationName) return; - if (!Array.isArray(result[relationName])) { - result[relationName] = item[relationName] - ? item[relationName].map((relItem: any) => { - const filteredRel: any = {}; - propertyKey - .filter(k => k.startsWith(`${table}.`)) - .forEach(k => { - const [, relField] = k.split("."); - filteredRel[relField] = relItem[relField]; - }); - return filteredRel; - }) - : []; - } - } - }); - return result; - }); - + // ลบ Main.id + const results = items.map(({ id, ...x }) => x); return new HttpSuccess({ items: results, total }); } } diff --git a/src/controllers/OrganizationDotnetController.ts b/src/controllers/OrganizationDotnetController.ts index 5abd6a13..16fcd163 100644 --- a/src/controllers/OrganizationDotnetController.ts +++ b/src/controllers/OrganizationDotnetController.ts @@ -693,7 +693,11 @@ export class OrganizationDotnetController extends Controller { } } } - let positionLeaveName = profile.posLevel?.posLevelName ?? null; + let positionLeaveName = + profile.posType == null && profile.posLevel == null + ? "" + : `${profile.posType?.posTypeShortName ?? ""} ${profile.posLevel?.posLevelName ?? ""}` + const _profileCurrent = profile?.current_holders?.find( (x) => x.orgRevision?.orgRevisionIsDraft === false &&