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 { OrgRevision } from "../entities/OrgRevision";
|
||||
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")
|
||||
@Security("webServiceAuth")
|
||||
@Response(
|
||||
|
|
@ -27,7 +28,8 @@ export class ApiWebServiceController extends Controller {
|
|||
@Get("/:system/:code")
|
||||
async listAttribute(
|
||||
@Request() request: RequestWithUserWebService,
|
||||
@Path("system") system: "registry" | "registry_emp" | "registry_temp" | "organization",
|
||||
@Path("system")
|
||||
system: SystemCode,
|
||||
@Path("code") code: string,
|
||||
@Query("page") page: number = 1,
|
||||
@Query("pageSize") pageSize: number = 100,
|
||||
|
|
@ -50,18 +52,18 @@ export class ApiWebServiceController extends Controller {
|
|||
const offset = (page - 1) * pageSize;
|
||||
const propertyKey = apiName.apiAttributes.map((attr) => `${attr.tbName}.${attr.propertyKey}`);
|
||||
|
||||
let Main: string = "";
|
||||
let tbMain: string = "";
|
||||
let condition: string = "1=1";
|
||||
if (system == "registry") {
|
||||
Main = "Profile";
|
||||
tbMain = "Profile";
|
||||
} else if (system == "registry_emp") {
|
||||
Main = "ProfileEmployee";
|
||||
tbMain = "ProfileEmployee";
|
||||
condition = `ProfileEmployee.employeeClass = "PERM"`;
|
||||
} else if (system == "registry_temp") {
|
||||
Main = "ProfileEmployee";
|
||||
tbMain = "ProfileEmployee";
|
||||
condition = `ProfileEmployee.employeeClass = "TEMP"`;
|
||||
} else {
|
||||
Main = "OrgRoot";
|
||||
tbMain = "OrgRoot";
|
||||
const revision = await this.orgRevisionRepository.findOne({
|
||||
select: ["id"],
|
||||
where: { orgRevisionIsCurrent: true, orgRevisionIsDraft: false },
|
||||
|
|
@ -69,7 +71,7 @@ export class ApiWebServiceController extends Controller {
|
|||
condition = `OrgRoot.orgRevisionId = "${revision?.id}"`;
|
||||
}
|
||||
|
||||
const repo = AppDataSource.getRepository(Main);
|
||||
const repo = AppDataSource.getRepository(tbMain);
|
||||
const metadata = repo.metadata;
|
||||
|
||||
const relationMap: Record<string, string> = {};
|
||||
|
|
@ -80,17 +82,17 @@ export class ApiWebServiceController extends Controller {
|
|||
// ดึงเฉพาะตารางรอง (ถ้าเลือกไว้)
|
||||
let propertyOtherKey: any[] = [];
|
||||
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 กับตารารอง
|
||||
if (propertyOtherKey.length > 0) {
|
||||
propertyOtherKey.forEach((tb) => {
|
||||
const relationName = relationMap[tb];
|
||||
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;
|
||||
primaryColumns.forEach((col) => {
|
||||
pk = col.propertyName;
|
||||
if (!propertyKey.includes(`${Main}.${pk}`)) {
|
||||
propertyKey.push(`${Main}.${pk}`);
|
||||
if (!propertyKey.includes(`${tbMain}.${pk}`)) {
|
||||
propertyKey.push(`${tbMain}.${pk}`);
|
||||
}
|
||||
});
|
||||
|
||||
|
|
@ -133,7 +135,7 @@ export class ApiWebServiceController extends Controller {
|
|||
return x;
|
||||
});
|
||||
|
||||
// console.log("queryBuilder ===> ", queryBuilder.getQuery());
|
||||
console.log("queryBuilder ===> ", queryBuilder.getQuery());
|
||||
|
||||
// save api history after query success
|
||||
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