Merge branch 'develop' into adiDev

This commit is contained in:
AdisakKanthawilang 2024-12-06 09:19:45 +07:00
commit 4ed6a08557
6 changed files with 92 additions and 4 deletions

View file

@ -128,4 +128,56 @@ export class ApiKeyController extends Controller {
}); });
return new HttpSuccess(apiName); 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);
}
} }

View file

@ -2240,8 +2240,8 @@ export class CommandController extends Controller {
firstName: profile.firstName, firstName: profile.firstName,
lastName: profile.lastName, lastName: profile.lastName,
}); });
// กรณี Keycloak ไม่ถูกลบ ให้ลบซ้ำอีกรอบแล้วสร้างใหม่ // กรณี Keycloak ไม่ถูกลบ ให้ลบซ้ำอีกรอบแล้วสร้างใหม่ และหากยังไม่สามารถลบได้ให้แสดง Error
if (profile.keycloak != null && userKeycloakId && userKeycloakId.error === "User exists with same username") { if (profile.keycloak != null && userKeycloakId && userKeycloakId.errorMessage === "User exists with same username") {
const delUserKeycloak = await deleteUser(profile.keycloak); const delUserKeycloak = await deleteUser(profile.keycloak);
if(delUserKeycloak) { if(delUserKeycloak) {
userKeycloakId = await createUser(profile.citizenId, profile.citizenId, { userKeycloakId = await createUser(profile.citizenId, profile.citizenId, {
@ -2249,6 +2249,9 @@ export class CommandController extends Controller {
lastName: profile.lastName, lastName: profile.lastName,
}); });
} }
else {
throw new HttpError(HttpStatus.BAD_REQUEST, "พบข้อผิดพลาด ไม่สามารถจัดการผู้ใช้งานได้");
}
} }
const list = await getRoles(); const list = await getRoles();
let result = false; let result = false;

View file

@ -41,7 +41,7 @@ export const AppDataSource = new DataSource({
password: process.env.DB_PASSWORD, password: process.env.DB_PASSWORD,
connectorPackage: "mysql2", connectorPackage: "mysql2",
synchronize: false, synchronize: false,
logging: ["query", "error"], logging: true,
entities: entities:
process.env.NODE_ENV !== "production" process.env.NODE_ENV !== "production"
? ["src/entities/**/*.ts"] ? ["src/entities/**/*.ts"]

View file

@ -1,6 +1,7 @@
import { Entity, Column, ManyToOne, JoinColumn } from "typeorm"; import { Entity, Column, ManyToOne, JoinColumn } from "typeorm";
import { EntityBase } from "./base/Base"; import { EntityBase } from "./base/Base";
import { ApiKey } from "./ApiKey"; import { ApiKey } from "./ApiKey";
import { ApiName } from "./ApiName";
@Entity("apiHistory") @Entity("apiHistory")
export class ApiHistory extends EntityBase { export class ApiHistory extends EntityBase {
@ -63,4 +64,16 @@ export class ApiHistory extends EntityBase {
@ManyToOne(() => ApiKey, (apiKey) => apiKey.apiHistorys) @ManyToOne(() => ApiKey, (apiKey) => apiKey.apiHistorys)
@JoinColumn({ name: "apiKeyId" }) @JoinColumn({ name: "apiKeyId" })
apiKey: ApiKey; apiKey: ApiKey;
@Column({
nullable: true,
length: 40,
comment: "คีย์นอก(FK)ของตาราง ApiName",
default: null,
})
apiNameId: string;
@ManyToOne(() => ApiName, (apiName) => apiName.apiHistorys)
@JoinColumn({ name: "apiNameId" })
apiName: ApiName;
} }

View file

@ -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 { EntityBase } from "./base/Base";
import { ApiKey } from "./ApiKey"; import { ApiKey } from "./ApiKey";
import { ApiHistory } from "./ApiHistory";
@Entity("apiName") @Entity("apiName")
export class ApiName extends EntityBase { export class ApiName extends EntityBase {
@ -31,4 +32,7 @@ export class ApiName extends EntityBase {
@ManyToMany(() => ApiKey, (apiKey) => apiKey.apiNames) @ManyToMany(() => ApiKey, (apiKey) => apiKey.apiNames)
@JoinTable() @JoinTable()
apiKeys: ApiKey[]; apiKeys: ApiKey[];
@OneToMany(() => ApiHistory, (v) => v.apiName)
apiHistorys: ApiHistory[];
} }

View 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\``);
}
}