diff --git a/src/controllers/ApiWebServiceController.ts b/src/controllers/ApiWebServiceController.ts index 7c664da4..124f0914 100644 --- a/src/controllers/ApiWebServiceController.ts +++ b/src/controllers/ApiWebServiceController.ts @@ -1,22 +1,9 @@ -import { - Controller, - Post, - Route, - Security, - Tags, - Body, - Path, - Request, - Response, - Get, - Query, -} from "tsoa"; +import { Controller, Route, Security, Tags, Path, Request, Response, Get, Query } from "tsoa"; import { AppDataSource } from "../database/data-source"; import HttpSuccess from "../interfaces/http-success"; import HttpStatusCode from "../interfaces/http-status"; import HttpError from "../interfaces/http-error"; import { ApiName } from "../entities/ApiName"; -import { Profile } from "../entities/Profile"; import { isPermissionRequest } from "../middlewares/authWebService"; import { RequestWithUserWebService } from "../middlewares/user"; import { OrgRevision } from "../entities/OrgRevision"; @@ -45,7 +32,6 @@ export class ApiWebServiceController extends Controller { @Query("page") page: number = 1, @Query("pageSize") pageSize: number = 100, ): Promise { - const apiName = await this.apiNameRepository.findOne({ where: { code }, select: ["id", "code", "methodApi", "system", "isActive"], @@ -64,67 +50,60 @@ export class ApiWebServiceController extends Controller { const offset = (page - 1) * pageSize; const propertyKey = apiName.apiAttributes.map((attr) => `${attr.tbName}.${attr.propertyKey}`); - let Main:string = "" - let condition: string = "1=1" + let Main: string = ""; + let condition: string = "1=1"; if (system == "registry") { - Main = "Profile" - } - else if (system == "registry_emp") { - Main = "ProfileEmployee" - condition = `ProfileEmployee.employeeClass = "PERM"` - } - else if (system == "registry_temp") { - Main = "ProfileEmployee" - condition = `ProfileEmployee.employeeClass = "TEMP"` - } - else { - Main = "OrgRoot" + Main = "Profile"; + } else if (system == "registry_emp") { + Main = "ProfileEmployee"; + condition = `ProfileEmployee.employeeClass = "PERM"`; + } else if (system == "registry_temp") { + Main = "ProfileEmployee"; + condition = `ProfileEmployee.employeeClass = "TEMP"`; + } else { + Main = "OrgRoot"; const revision = await this.orgRevisionRepository.findOne({ - select: ["id"], - where: { orgRevisionIsCurrent: true, orgRevisionIsDraft: false } - }); - condition = `OrgRoot.orgRevisionId = "${revision?.id}"` + select: ["id"], + where: { orgRevisionIsCurrent: true, orgRevisionIsDraft: false }, + }); + condition = `OrgRoot.orgRevisionId = "${revision?.id}"`; } const repo = AppDataSource.getRepository(Main); const metadata = repo.metadata; - + const relationMap: Record = {}; metadata.relations.forEach((rel) => { relationMap[rel.inverseEntityMetadata.name] = rel.propertyName; }); - + // ดึงเฉพาะตารางรอง (ถ้าเลือกไว้) - let propertyOtherKey:any[] = []; + let propertyOtherKey: any[] = []; propertyOtherKey = [ - ...new Set( - propertyKey - .map((x) => x.split(".")[0]) - .filter((tb) => tb !== Main) - ) - ] - - const queryBuilder = repo.createQueryBuilder(Main) + ...new Set(propertyKey.map((x) => x.split(".")[0]).filter((tb) => tb !== Main)), + ]; + + const queryBuilder = repo.createQueryBuilder(Main); // join กับตารารอง if (propertyOtherKey.length > 0) { propertyOtherKey.forEach((tb) => { const relationName = relationMap[tb]; if (relationName) { - queryBuilder.leftJoin(`${Main}.${relationName}`, tb); + queryBuilder.leftJoin(`${Main}.${relationName}`, tb); } }); } - + // // เพิ่ม Main.id เพราะจะใช้ pk ในการแมบและนับจำนวน // if (!propertyKey.includes(`${Main}.id`)) { // propertyKey.push(`${Main}.id`); // } // add FK - let pk:string = "" + let pk: string = ""; const primaryColumns = metadata.primaryColumns; - primaryColumns.forEach(col => { + primaryColumns.forEach((col) => { pk = col.propertyName; if (!propertyKey.includes(`${Main}.${pk}`)) { propertyKey.push(`${Main}.${pk}`); @@ -148,7 +127,15 @@ export class ApiWebServiceController extends Controller { // return item; // }); - // save api history + // split object id ออกก่อน return + const data = items.map((item) => { + const { [pk]: removedPk, ...x } = item; + return x; + }); + + // console.log("queryBuilder ===> ", queryBuilder.getQuery()); + + // save api history after query success const history = { headerApi: JSON.stringify({ host: request.headers.host, @@ -169,6 +156,6 @@ export class ApiWebServiceController extends Controller { lastUpdateFullName: request.user.name, }; await this.apiHistoryRepository.save(history); - return new HttpSuccess({ items: items, total }); + return new HttpSuccess({ data, total }); } }