hrms-api-org/src/controllers/ApiWebServiceController.ts

83 lines
2.9 KiB
TypeScript
Raw Normal View History

2025-08-07 13:05:58 +07:00
import {
Controller,
Post,
Route,
Security,
Tags,
Body,
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";
2025-08-08 09:26:48 +07:00
import { isPermissionRequest } from "../middlewares/authWebService";
import { RequestWithUserWebService } from "../middlewares/user";
2025-08-07 13:05:58 +07:00
@Route("api/v2/org/api-service")
@Tags("ApiKey")
2025-08-08 09:26:48 +07:00
@Security("webServiceAuth")
2025-08-07 13:05:58 +07:00
@Response(
HttpStatusCode.INTERNAL_SERVER_ERROR,
"เกิดข้อผิดพลาด ไม่สามารถแสดงรายการได้ กรุณาลองใหม่ในภายหลัง",
)
export class ApiWebServiceController extends Controller {
private apiNameRepository = AppDataSource.getRepository(ApiName);
/**
* list fields by systems
* @summary fields systems
*/
@Get("/:system/:code")
async listAttribute(
2025-08-08 09:26:48 +07:00
@Request() request: RequestWithUserWebService,
2025-08-07 13:05:58 +07:00
@Path("system") system: "registry" | "registry_emp" | "registry_temp" | "organization",
@Path("code") code: string,
@Query("page") page: number = 1,
@Query("pageSize") pageSize: number = 100,
): Promise<HttpSuccess | HttpError> {
2025-08-08 09:26:48 +07:00
// try {
2025-08-07 13:05:58 +07:00
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 ที่ระบุ");
}
2025-08-08 09:26:48 +07:00
await isPermissionRequest(request, apiName.id);
2025-08-07 13:05:58 +07:00
const offset = (page - 1) * pageSize;
const propertyKey = apiName.apiAttributes.map((attr) => `${attr.tbName}.${attr.propertyKey}`);
const queryBuilder = AppDataSource.getRepository(Profile)
.createQueryBuilder("Profile")
.select(propertyKey);
const [items, total] = await queryBuilder
.skip(offset)
.take(pageSize)
.orderBy("Profile.createdAt", "DESC")
.getManyAndCount();
return new HttpSuccess({ items, total });
2025-08-08 09:26:48 +07:00
// } catch (error) {
// throw new HttpError(
// HttpStatusCode.INTERNAL_SERVER_ERROR,
// (error instanceof Error ? error.message : String(error)) ||
// "เกิดข้อผิดพลาด ไม่สามารถแสดงรายการได้ กรุณาลองใหม่ในภายหลัง",
// );
// }
2025-08-07 13:05:58 +07:00
}
}