update #141
This commit is contained in:
parent
929ab87b1c
commit
5865ada163
1 changed files with 107 additions and 33 deletions
|
|
@ -19,6 +19,7 @@ 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";
|
||||
@Route("api/v2/org/api-service")
|
||||
@Tags("ApiKey")
|
||||
@Security("webServiceAuth")
|
||||
|
|
@ -28,7 +29,7 @@ import { RequestWithUserWebService } from "../middlewares/user";
|
|||
)
|
||||
export class ApiWebServiceController extends Controller {
|
||||
private apiNameRepository = AppDataSource.getRepository(ApiName);
|
||||
|
||||
private orgRevisionRepository = AppDataSource.getRepository(OrgRevision);
|
||||
/**
|
||||
* list fields by systems
|
||||
* @summary รายการ fields ตาม systems
|
||||
|
|
@ -41,42 +42,115 @@ export class ApiWebServiceController extends Controller {
|
|||
@Query("page") page: number = 1,
|
||||
@Query("pageSize") pageSize: number = 100,
|
||||
): Promise<HttpSuccess | HttpError> {
|
||||
// try {
|
||||
const apiName = await this.apiNameRepository.findOne({
|
||||
where: { code },
|
||||
select: ["id", "code", "methodApi", "system", "isActive"],
|
||||
relations: ["apiAttributes"],
|
||||
order: {
|
||||
apiAttributes: {
|
||||
ordering: "ASC",
|
||||
},
|
||||
|
||||
const apiName = await this.apiNameRepository.findOne({
|
||||
where: { code },
|
||||
select: ["id", "code", "methodApi", "system", "isActive"],
|
||||
relations: ["apiAttributes"],
|
||||
order: {
|
||||
apiAttributes: {
|
||||
ordering: "ASC",
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
if (!apiName || apiName.system != system || !apiName.isActive || apiName.methodApi != "GET") {
|
||||
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบ API ที่ระบุ");
|
||||
}
|
||||
await isPermissionRequest(request, apiName.id);
|
||||
const offset = (page - 1) * pageSize;
|
||||
|
||||
const propertyKey = apiName.apiAttributes.map((attr) => `${attr.tbName}.${attr.propertyKey}`);
|
||||
|
||||
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"
|
||||
const revision = await this.orgRevisionRepository.findOne({
|
||||
select: ["id"],
|
||||
where: { orgRevisionIsCurrent: true, orgRevisionIsDraft: false }
|
||||
});
|
||||
condition = `OrgRoot.orgRevisionId = '${revision?.id}'`
|
||||
}
|
||||
|
||||
const repo = AppDataSource.getRepository(Main);
|
||||
const metadata = repo.metadata;
|
||||
|
||||
const relationMap: Record<string, string> = {};
|
||||
metadata.relations.forEach((rel) => {
|
||||
relationMap[rel.inverseEntityMetadata.name] = rel.propertyName;
|
||||
});
|
||||
|
||||
// ดึงเฉพาะตารางรอง (ถ้าเลือกไว้)
|
||||
let propertyOtherKey:any[] = [];
|
||||
propertyOtherKey = [
|
||||
...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.leftJoinAndSelect(`${Main}.${relationName}`, tb);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
const [items, total] = await queryBuilder
|
||||
.where(condition)
|
||||
.orderBy(`${Main}.createdAt`, "DESC")
|
||||
.skip(offset)
|
||||
.take(pageSize)
|
||||
.getManyAndCount();
|
||||
|
||||
if (!apiName || apiName.system != system || !apiName.isActive || apiName.methodApi != "GET") {
|
||||
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบ API ที่ระบุ");
|
||||
}
|
||||
await isPermissionRequest(request, apiName.id);
|
||||
const offset = (page - 1) * pageSize;
|
||||
const results = items.map(item => {
|
||||
const result: any = {};
|
||||
propertyKey.forEach(key => {
|
||||
const [table, field] = key.split(".");
|
||||
|
||||
const propertyKey = apiName.apiAttributes.map((attr) => `${attr.tbName}.${attr.propertyKey}`);
|
||||
const queryBuilder = AppDataSource.getRepository(Profile)
|
||||
.createQueryBuilder("Profile")
|
||||
.select(propertyKey);
|
||||
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;
|
||||
});
|
||||
|
||||
const [items, total] = await queryBuilder
|
||||
.skip(offset)
|
||||
.take(pageSize)
|
||||
.orderBy("Profile.createdAt", "DESC")
|
||||
.getManyAndCount();
|
||||
|
||||
return new HttpSuccess({ items, total });
|
||||
// } catch (error) {
|
||||
// throw new HttpError(
|
||||
// HttpStatusCode.INTERNAL_SERVER_ERROR,
|
||||
// (error instanceof Error ? error.message : String(error)) ||
|
||||
// "เกิดข้อผิดพลาด ไม่สามารถแสดงรายการได้ กรุณาลองใหม่ในภายหลัง",
|
||||
// );
|
||||
// }
|
||||
return new HttpSuccess({ items: results, total });
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue