api web service remove pk before response

This commit is contained in:
Warunee Tamkoo 2025-08-13 16:47:01 +07:00
parent 66b2f10fee
commit 6e0a61bafa

View file

@ -1,22 +1,9 @@
import { import { Controller, Route, Security, Tags, Path, Request, Response, Get, Query } from "tsoa";
Controller,
Post,
Route,
Security,
Tags,
Body,
Path,
Request,
Response,
Get,
Query,
} from "tsoa";
import { AppDataSource } from "../database/data-source"; import { AppDataSource } from "../database/data-source";
import HttpSuccess from "../interfaces/http-success"; import HttpSuccess from "../interfaces/http-success";
import HttpStatusCode from "../interfaces/http-status"; import HttpStatusCode from "../interfaces/http-status";
import HttpError from "../interfaces/http-error"; import HttpError from "../interfaces/http-error";
import { ApiName } from "../entities/ApiName"; import { ApiName } from "../entities/ApiName";
import { Profile } from "../entities/Profile";
import { isPermissionRequest } from "../middlewares/authWebService"; import { isPermissionRequest } from "../middlewares/authWebService";
import { RequestWithUserWebService } from "../middlewares/user"; import { RequestWithUserWebService } from "../middlewares/user";
import { OrgRevision } from "../entities/OrgRevision"; import { OrgRevision } from "../entities/OrgRevision";
@ -45,7 +32,6 @@ export class ApiWebServiceController extends Controller {
@Query("page") page: number = 1, @Query("page") page: number = 1,
@Query("pageSize") pageSize: number = 100, @Query("pageSize") pageSize: number = 100,
): Promise<HttpSuccess | HttpError> { ): Promise<HttpSuccess | HttpError> {
const apiName = await this.apiNameRepository.findOne({ const apiName = await this.apiNameRepository.findOne({
where: { code }, where: { code },
select: ["id", "code", "methodApi", "system", "isActive"], select: ["id", "code", "methodApi", "system", "isActive"],
@ -64,26 +50,23 @@ export class ApiWebServiceController extends Controller {
const offset = (page - 1) * pageSize; const offset = (page - 1) * pageSize;
const propertyKey = apiName.apiAttributes.map((attr) => `${attr.tbName}.${attr.propertyKey}`); const propertyKey = apiName.apiAttributes.map((attr) => `${attr.tbName}.${attr.propertyKey}`);
let Main:string = "" let Main: string = "";
let condition: string = "1=1" let condition: string = "1=1";
if (system == "registry") { if (system == "registry") {
Main = "Profile" Main = "Profile";
} } else if (system == "registry_emp") {
else if (system == "registry_emp") { Main = "ProfileEmployee";
Main = "ProfileEmployee" condition = `ProfileEmployee.employeeClass = "PERM"`;
condition = `ProfileEmployee.employeeClass = "PERM"` } else if (system == "registry_temp") {
} Main = "ProfileEmployee";
else if (system == "registry_temp") { condition = `ProfileEmployee.employeeClass = "TEMP"`;
Main = "ProfileEmployee" } else {
condition = `ProfileEmployee.employeeClass = "TEMP"` Main = "OrgRoot";
}
else {
Main = "OrgRoot"
const revision = await this.orgRevisionRepository.findOne({ const revision = await this.orgRevisionRepository.findOne({
select: ["id"], select: ["id"],
where: { orgRevisionIsCurrent: true, orgRevisionIsDraft: false } where: { orgRevisionIsCurrent: true, orgRevisionIsDraft: false },
}); });
condition = `OrgRoot.orgRevisionId = "${revision?.id}"` condition = `OrgRoot.orgRevisionId = "${revision?.id}"`;
} }
const repo = AppDataSource.getRepository(Main); const repo = AppDataSource.getRepository(Main);
@ -97,14 +80,10 @@ export class ApiWebServiceController extends Controller {
// ดึงเฉพาะตารางรอง (ถ้าเลือกไว้) // ดึงเฉพาะตารางรอง (ถ้าเลือกไว้)
let propertyOtherKey: any[] = []; let propertyOtherKey: any[] = [];
propertyOtherKey = [ propertyOtherKey = [
...new Set( ...new Set(propertyKey.map((x) => x.split(".")[0]).filter((tb) => tb !== Main)),
propertyKey ];
.map((x) => x.split(".")[0])
.filter((tb) => tb !== Main)
)
]
const queryBuilder = repo.createQueryBuilder(Main) const queryBuilder = repo.createQueryBuilder(Main);
// join กับตารารอง // join กับตารารอง
if (propertyOtherKey.length > 0) { if (propertyOtherKey.length > 0) {
@ -122,9 +101,9 @@ export class ApiWebServiceController extends Controller {
// } // }
// add FK // add FK
let pk:string = "" let pk: string = "";
const primaryColumns = metadata.primaryColumns; const primaryColumns = metadata.primaryColumns;
primaryColumns.forEach(col => { primaryColumns.forEach((col) => {
pk = col.propertyName; pk = col.propertyName;
if (!propertyKey.includes(`${Main}.${pk}`)) { if (!propertyKey.includes(`${Main}.${pk}`)) {
propertyKey.push(`${Main}.${pk}`); propertyKey.push(`${Main}.${pk}`);
@ -148,7 +127,15 @@ export class ApiWebServiceController extends Controller {
// return item; // 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 = { const history = {
headerApi: JSON.stringify({ headerApi: JSON.stringify({
host: request.headers.host, host: request.headers.host,
@ -169,6 +156,6 @@ export class ApiWebServiceController extends Controller {
lastUpdateFullName: request.user.name, lastUpdateFullName: request.user.name,
}; };
await this.apiHistoryRepository.save(history); await this.apiHistoryRepository.save(history);
return new HttpSuccess({ items: items, total }); return new HttpSuccess({ data, total });
} }
} }