fix api service v2 to v1 & fix type & review code
This commit is contained in:
parent
6e0a61bafa
commit
4318a7b631
3 changed files with 485 additions and 431 deletions
File diff suppressed because it is too large
Load diff
|
|
@ -8,7 +8,8 @@ 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";
|
||||||
import { ApiHistory } from "../entities/ApiHistory";
|
import { ApiHistory } from "../entities/ApiHistory";
|
||||||
@Route("api/v2/org/api-service")
|
import { SystemCode } from "./../interfaces/api-type";
|
||||||
|
@Route("api/v1/org/api-service")
|
||||||
@Tags("ApiKey")
|
@Tags("ApiKey")
|
||||||
@Security("webServiceAuth")
|
@Security("webServiceAuth")
|
||||||
@Response(
|
@Response(
|
||||||
|
|
@ -27,7 +28,8 @@ export class ApiWebServiceController extends Controller {
|
||||||
@Get("/:system/:code")
|
@Get("/:system/:code")
|
||||||
async listAttribute(
|
async listAttribute(
|
||||||
@Request() request: RequestWithUserWebService,
|
@Request() request: RequestWithUserWebService,
|
||||||
@Path("system") system: "registry" | "registry_emp" | "registry_temp" | "organization",
|
@Path("system")
|
||||||
|
system: SystemCode,
|
||||||
@Path("code") code: string,
|
@Path("code") code: string,
|
||||||
@Query("page") page: number = 1,
|
@Query("page") page: number = 1,
|
||||||
@Query("pageSize") pageSize: number = 100,
|
@Query("pageSize") pageSize: number = 100,
|
||||||
|
|
@ -50,18 +52,18 @@ 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 tbMain: string = "";
|
||||||
let condition: string = "1=1";
|
let condition: string = "1=1";
|
||||||
if (system == "registry") {
|
if (system == "registry") {
|
||||||
Main = "Profile";
|
tbMain = "Profile";
|
||||||
} else if (system == "registry_emp") {
|
} else if (system == "registry_emp") {
|
||||||
Main = "ProfileEmployee";
|
tbMain = "ProfileEmployee";
|
||||||
condition = `ProfileEmployee.employeeClass = "PERM"`;
|
condition = `ProfileEmployee.employeeClass = "PERM"`;
|
||||||
} else if (system == "registry_temp") {
|
} else if (system == "registry_temp") {
|
||||||
Main = "ProfileEmployee";
|
tbMain = "ProfileEmployee";
|
||||||
condition = `ProfileEmployee.employeeClass = "TEMP"`;
|
condition = `ProfileEmployee.employeeClass = "TEMP"`;
|
||||||
} else {
|
} else {
|
||||||
Main = "OrgRoot";
|
tbMain = "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 },
|
||||||
|
|
@ -69,7 +71,7 @@ export class ApiWebServiceController extends Controller {
|
||||||
condition = `OrgRoot.orgRevisionId = "${revision?.id}"`;
|
condition = `OrgRoot.orgRevisionId = "${revision?.id}"`;
|
||||||
}
|
}
|
||||||
|
|
||||||
const repo = AppDataSource.getRepository(Main);
|
const repo = AppDataSource.getRepository(tbMain);
|
||||||
const metadata = repo.metadata;
|
const metadata = repo.metadata;
|
||||||
|
|
||||||
const relationMap: Record<string, string> = {};
|
const relationMap: Record<string, string> = {};
|
||||||
|
|
@ -80,17 +82,17 @@ export class ApiWebServiceController extends Controller {
|
||||||
// ดึงเฉพาะตารางรอง (ถ้าเลือกไว้)
|
// ดึงเฉพาะตารางรอง (ถ้าเลือกไว้)
|
||||||
let propertyOtherKey: any[] = [];
|
let propertyOtherKey: any[] = [];
|
||||||
propertyOtherKey = [
|
propertyOtherKey = [
|
||||||
...new Set(propertyKey.map((x) => x.split(".")[0]).filter((tb) => tb !== Main)),
|
...new Set(propertyKey.map((x) => x.split(".")[0]).filter((tb) => tb !== tbMain)),
|
||||||
];
|
];
|
||||||
|
|
||||||
const queryBuilder = repo.createQueryBuilder(Main);
|
const queryBuilder = repo.createQueryBuilder(tbMain);
|
||||||
|
|
||||||
// join กับตารารอง
|
// join กับตารารอง
|
||||||
if (propertyOtherKey.length > 0) {
|
if (propertyOtherKey.length > 0) {
|
||||||
propertyOtherKey.forEach((tb) => {
|
propertyOtherKey.forEach((tb) => {
|
||||||
const relationName = relationMap[tb];
|
const relationName = relationMap[tb];
|
||||||
if (relationName) {
|
if (relationName) {
|
||||||
queryBuilder.leftJoin(`${Main}.${relationName}`, tb);
|
queryBuilder.leftJoin(`${tbMain}.${relationName}`, tb);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
@ -105,8 +107,8 @@ export class ApiWebServiceController extends Controller {
|
||||||
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(`${tbMain}.${pk}`)) {
|
||||||
propertyKey.push(`${Main}.${pk}`);
|
propertyKey.push(`${tbMain}.${pk}`);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
@ -133,7 +135,7 @@ export class ApiWebServiceController extends Controller {
|
||||||
return x;
|
return x;
|
||||||
});
|
});
|
||||||
|
|
||||||
// console.log("queryBuilder ===> ", queryBuilder.getQuery());
|
console.log("queryBuilder ===> ", queryBuilder.getQuery());
|
||||||
|
|
||||||
// save api history after query success
|
// save api history after query success
|
||||||
const history = {
|
const history = {
|
||||||
|
|
|
||||||
16
src/interfaces/api-type.ts
Normal file
16
src/interfaces/api-type.ts
Normal file
|
|
@ -0,0 +1,16 @@
|
||||||
|
type SystemCode = "registry" | "registry_emp" | "registry_temp" | "organization" | "position";
|
||||||
|
|
||||||
|
interface SystemDefinition {
|
||||||
|
code: SystemCode;
|
||||||
|
name: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
interface EntityDefinition {
|
||||||
|
name: string;
|
||||||
|
repository: any;
|
||||||
|
description: string;
|
||||||
|
isMain?: boolean;
|
||||||
|
system: SystemCode[];
|
||||||
|
}
|
||||||
|
|
||||||
|
export { SystemCode, SystemDefinition, EntityDefinition };
|
||||||
Loading…
Add table
Add a link
Reference in a new issue