This commit is contained in:
Bright 2025-08-08 09:26:48 +07:00
parent 803845d568
commit ca4bb683ea

View file

@ -17,9 +17,11 @@ 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 { Profile } from "../entities/Profile";
import { isPermissionRequest } from "../middlewares/authWebService";
import { RequestWithUserWebService } from "../middlewares/user";
@Route("api/v2/org/api-service") @Route("api/v2/org/api-service")
@Tags("ApiKey") @Tags("ApiKey")
// @Security("bearerAuth") @Security("webServiceAuth")
@Response( @Response(
HttpStatusCode.INTERNAL_SERVER_ERROR, HttpStatusCode.INTERNAL_SERVER_ERROR,
"เกิดข้อผิดพลาด ไม่สามารถแสดงรายการได้ กรุณาลองใหม่ในภายหลัง", "เกิดข้อผิดพลาด ไม่สามารถแสดงรายการได้ กรุณาลองใหม่ในภายหลัง",
@ -33,12 +35,13 @@ export class ApiWebServiceController extends Controller {
*/ */
@Get("/:system/:code") @Get("/:system/:code")
async listAttribute( async listAttribute(
@Request() request: RequestWithUserWebService,
@Path("system") system: "registry" | "registry_emp" | "registry_temp" | "organization", @Path("system") system: "registry" | "registry_emp" | "registry_temp" | "organization",
@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,
): Promise<HttpSuccess | HttpError> { ): Promise<HttpSuccess | HttpError> {
try { // try {
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"],
@ -53,7 +56,7 @@ export class ApiWebServiceController extends Controller {
if (!apiName || apiName.system != system || !apiName.isActive || apiName.methodApi != "GET") { if (!apiName || apiName.system != system || !apiName.isActive || apiName.methodApi != "GET") {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบ API ที่ระบุ"); throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบ API ที่ระบุ");
} }
await isPermissionRequest(request, apiName.id);
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}`);
@ -68,12 +71,12 @@ export class ApiWebServiceController extends Controller {
.getManyAndCount(); .getManyAndCount();
return new HttpSuccess({ items, total }); return new HttpSuccess({ items, total });
} catch (error) { // } catch (error) {
throw new HttpError( // throw new HttpError(
HttpStatusCode.INTERNAL_SERVER_ERROR, // HttpStatusCode.INTERNAL_SERVER_ERROR,
(error instanceof Error ? error.message : String(error)) || // (error instanceof Error ? error.message : String(error)) ||
"เกิดข้อผิดพลาด ไม่สามารถแสดงรายการได้ กรุณาลองใหม่ในภายหลัง", // "เกิดข้อผิดพลาด ไม่สามารถแสดงรายการได้ กรุณาลองใหม่ในภายหลัง",
); // );
} // }
} }
} }