From 4c0b729b817ca4f063d8c64f61543cdcfea9e180 Mon Sep 17 00:00:00 2001 From: waruneeauy Date: Fri, 8 Aug 2025 10:52:24 +0700 Subject: [PATCH] add manual swagger json & fix check use before delete api --- src/controllers/ApiManageController.ts | 92 ++++++++++++++++++++++++++ 1 file changed, 92 insertions(+) diff --git a/src/controllers/ApiManageController.ts b/src/controllers/ApiManageController.ts index e1a95ea9..fe7504c2 100644 --- a/src/controllers/ApiManageController.ts +++ b/src/controllers/ApiManageController.ts @@ -1,3 +1,4 @@ +import { ApiKey } from "./../entities/ApiKey"; import { ApiAttribute } from "./../entities/ApiAttribute"; import { CreateApi } from "./../entities/ApiName"; import { PosMaster } from "../entities/PosMaster"; @@ -596,6 +597,19 @@ export class ApiManageController extends Controller { throw new HttpError(HttpStatusCode.FORBIDDEN, "คุณไม่มีสิทธิ์ในการเข้าถึงข้อมูลนี้"); } + const checkUsed = await AppDataSource.getRepository(ApiKey) + .createQueryBuilder("apiKey") + .innerJoin("apiKey.apiNames", "apiName") + .where("apiName.id = :id", { id }) + .getCount(); + + if (checkUsed > 0) { + throw new HttpError( + HttpStatusCode.BAD_REQUEST, + "ไม่สามารถลบ API นี้ได้ เนื่องจากมีการใช้งานอยู่", + ); + } + await queryRunner.manager.getRepository(ApiAttribute).delete({ apiNameId: id }); await queryRunner.manager.getRepository(ApiName).delete({ id }); @@ -615,4 +629,82 @@ export class ApiManageController extends Controller { ); } } + + /** + * list systems + * @summary รายการ systems + */ + @Get("/manual/swagger") + async getManualRequestWebService() { + const json: any = {}; + // create swagger documentation for manual request only ApiWebServiceController + json["openapi"] = "3.0.0"; + json["info"] = { + title: "Request Web Service", + version: "1.0.0", + description: "This is a manual request web service.", + }; + json["servers"] = [ + { + url: process.env.API_URL?.replace("/api/v1", "") || "http://localhost:13009", + }, + { + url: "http://localhost:13009", + description: "Local server", + }, + ]; + json["paths"] = { + "/api/v2/org/api-service/{system}/{code}": { + get: { + summary: "Get Registry Data", + parameters: [ + { + name: "system", + in: "path", + required: true, + schema: { + type: "string", + enum: ["registry", "registry_emp", "registry_temp", "organization"], + }, + }, + { + name: "code", + in: "path", + required: true, + schema: { + type: "string", + }, + }, + ], + responses: { + 200: { + description: "Successful response", + }, + 400: { + description: "Bad request", + }, + 500: { + description: "Internal server error", + }, + }, + }, + }, + }; + + json["components"] = { + securitySchemes: { + ApiKeyAuth: { + type: "apiKey", + in: "header", + name: "X-API-Key", + }, + }, + }; + json["security"] = [ + { + ApiKeyAuth: [], + }, + ]; + return new HttpSuccess(json); + } }