From 62f803f3e30b4cddb882c697dd705fd4c3ae4b03 Mon Sep 17 00:00:00 2001 From: kittapath Date: Wed, 4 Dec 2024 21:18:10 +0700 Subject: [PATCH] api apikeyhistory --- src/controllers/ApiKeyController.ts | 52 +++++++++++++++++++ src/database/data-source.ts | 2 +- src/entities/ApiHistory.ts | 13 +++++ src/entities/ApiName.ts | 6 ++- ...33318856709-update_keyapi_add_KeynameId.ts | 16 ++++++ 5 files changed, 87 insertions(+), 2 deletions(-) create mode 100644 src/migration/1733318856709-update_keyapi_add_KeynameId.ts diff --git a/src/controllers/ApiKeyController.ts b/src/controllers/ApiKeyController.ts index 5dd2dbd1..8fa2ae4f 100644 --- a/src/controllers/ApiKeyController.ts +++ b/src/controllers/ApiKeyController.ts @@ -128,4 +128,56 @@ export class ApiKeyController extends Controller { }); return new HttpSuccess(apiName); } + + /** + * API สร้าง Api Key + * + * @summary สร้าง Api Key (ADMIN) + * + */ + @Post("history") + async getHistory( + @Body() + requestBody: { + startDate: Date; + endDate: Date; + apiNameId: string | null; + }, + @Request() request: RequestWithUser, + ) { + if (requestBody.apiNameId) { + const apiName = await this.apiNameRepository.findOne({ + where: { id: requestBody.apiNameId }, + }); + if (apiName) + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไท้พบรายการ Web Service นี้ในระบบ"); + } + const apiHistory = await AppDataSource.getRepository(ApiHistory) + .createQueryBuilder("apiHistory") + .leftJoinAndSelect("apiHistory.apiKey", "apiKey") + .leftJoinAndSelect("apiHistory.apiName", "apiName") + .andWhere(requestBody.apiNameId ? `apiHistory.apiNameId = :apiNameId` : "1=1", { + apiNameId: requestBody.apiNameId, + }) + .andWhere( + `apiHistory.createdAt >= DATE(:startDate) AND apiHistory.createdAt <= DATE(:endDate)`, + { + startDate: new Date(requestBody.startDate), + endDate: new Date( + requestBody.endDate.setDate(new Date(requestBody.endDate).getDate() + 1), + ), + }, + ) + .orderBy("apiHistory.createdAt", "DESC") + .getMany(); + const result = apiHistory.map((x) => ({ + id: x.id, + apiName: x.apiName.name, + apiKey: x.apiKey.name, + createdAt: x.createdAt, + ipApi: x.ipApi, + })); + + return new HttpSuccess(result); + } } diff --git a/src/database/data-source.ts b/src/database/data-source.ts index 67ff5b79..5bd34db2 100644 --- a/src/database/data-source.ts +++ b/src/database/data-source.ts @@ -41,7 +41,7 @@ export const AppDataSource = new DataSource({ password: process.env.DB_PASSWORD, connectorPackage: "mysql2", synchronize: false, - logging: ["query", "error"], + logging: true, entities: process.env.NODE_ENV !== "production" ? ["src/entities/**/*.ts"] diff --git a/src/entities/ApiHistory.ts b/src/entities/ApiHistory.ts index 94cffcaf..c5f640d0 100644 --- a/src/entities/ApiHistory.ts +++ b/src/entities/ApiHistory.ts @@ -1,6 +1,7 @@ import { Entity, Column, ManyToOne, JoinColumn } from "typeorm"; import { EntityBase } from "./base/Base"; import { ApiKey } from "./ApiKey"; +import { ApiName } from "./ApiName"; @Entity("apiHistory") export class ApiHistory extends EntityBase { @@ -63,4 +64,16 @@ export class ApiHistory extends EntityBase { @ManyToOne(() => ApiKey, (apiKey) => apiKey.apiHistorys) @JoinColumn({ name: "apiKeyId" }) apiKey: ApiKey; + + @Column({ + nullable: true, + length: 40, + comment: "คีย์นอก(FK)ของตาราง ApiName", + default: null, + }) + apiNameId: string; + + @ManyToOne(() => ApiName, (apiName) => apiName.apiHistorys) + @JoinColumn({ name: "apiNameId" }) + apiName: ApiName; } diff --git a/src/entities/ApiName.ts b/src/entities/ApiName.ts index 635e2114..510e51df 100644 --- a/src/entities/ApiName.ts +++ b/src/entities/ApiName.ts @@ -1,6 +1,7 @@ -import { Entity, Column, ManyToMany, JoinTable } from "typeorm"; +import { Entity, Column, ManyToMany, JoinTable, OneToMany } from "typeorm"; import { EntityBase } from "./base/Base"; import { ApiKey } from "./ApiKey"; +import { ApiHistory } from "./ApiHistory"; @Entity("apiName") export class ApiName extends EntityBase { @@ -31,4 +32,7 @@ export class ApiName extends EntityBase { @ManyToMany(() => ApiKey, (apiKey) => apiKey.apiNames) @JoinTable() apiKeys: ApiKey[]; + + @OneToMany(() => ApiHistory, (v) => v.apiName) + apiHistorys: ApiHistory[]; } diff --git a/src/migration/1733318856709-update_keyapi_add_KeynameId.ts b/src/migration/1733318856709-update_keyapi_add_KeynameId.ts new file mode 100644 index 00000000..712e7182 --- /dev/null +++ b/src/migration/1733318856709-update_keyapi_add_KeynameId.ts @@ -0,0 +1,16 @@ +import { MigrationInterface, QueryRunner } from "typeorm"; + +export class UpdateKeyapiAddKeynameId1733318856709 implements MigrationInterface { + name = 'UpdateKeyapiAddKeynameId1733318856709' + + public async up(queryRunner: QueryRunner): Promise { + await queryRunner.query(`ALTER TABLE \`apiHistory\` ADD \`apiNameId\` varchar(40) NULL COMMENT 'คีย์นอก(FK)ของตาราง ApiName'`); + await queryRunner.query(`ALTER TABLE \`apiHistory\` ADD CONSTRAINT \`FK_0b7ae98d4f342bf920339ada78b\` FOREIGN KEY (\`apiNameId\`) REFERENCES \`apiName\`(\`id\`) ON DELETE NO ACTION ON UPDATE NO ACTION`); + } + + public async down(queryRunner: QueryRunner): Promise { + await queryRunner.query(`ALTER TABLE \`apiHistory\` DROP FOREIGN KEY \`FK_0b7ae98d4f342bf920339ada78b\``); + await queryRunner.query(`ALTER TABLE \`apiHistory\` DROP COLUMN \`apiNameId\``); + } + +}