api apikeyhistory
This commit is contained in:
parent
f7653aa4cf
commit
62f803f3e3
5 changed files with 87 additions and 2 deletions
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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"]
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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[];
|
||||
}
|
||||
|
|
|
|||
16
src/migration/1733318856709-update_keyapi_add_KeynameId.ts
Normal file
16
src/migration/1733318856709-update_keyapi_add_KeynameId.ts
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
import { MigrationInterface, QueryRunner } from "typeorm";
|
||||
|
||||
export class UpdateKeyapiAddKeynameId1733318856709 implements MigrationInterface {
|
||||
name = 'UpdateKeyapiAddKeynameId1733318856709'
|
||||
|
||||
public async up(queryRunner: QueryRunner): Promise<void> {
|
||||
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<void> {
|
||||
await queryRunner.query(`ALTER TABLE \`apiHistory\` DROP FOREIGN KEY \`FK_0b7ae98d4f342bf920339ada78b\``);
|
||||
await queryRunner.query(`ALTER TABLE \`apiHistory\` DROP COLUMN \`apiNameId\``);
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue