-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1064 from aura-nw/Baseline/main_20231225
[IBC][MAIN] Baseline/main 20231225
- Loading branch information
Showing
71 changed files
with
1,060 additions
and
1,939 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
import { | ||
Controller, | ||
Get, | ||
Post, | ||
Body, | ||
Param, | ||
Delete, | ||
Put, | ||
UseGuards, | ||
HttpCode, | ||
HttpStatus, | ||
} from '@nestjs/common'; | ||
import { ChainInfoService } from './chain-info.service'; | ||
import { CreateChainInfoDto } from './dto/create-chain-info.dto'; | ||
import { UpdateChainInfoDto } from './dto/update-chain-info.dto'; | ||
import { | ||
ApiBadRequestResponse, | ||
ApiBearerAuth, | ||
ApiCreatedResponse, | ||
ApiForbiddenResponse, | ||
ApiNoContentResponse, | ||
ApiOkResponse, | ||
ApiTags, | ||
ApiUnauthorizedResponse, | ||
} from '@nestjs/swagger'; | ||
import { Roles } from '../../auth/role/roles.decorator'; | ||
import { RoleGuard } from '../../auth/role/roles.guard'; | ||
import { MESSAGES, SwaggerBaseApiResponse, USER_ROLE } from '../../shared'; | ||
import { JwtAuthGuard } from '../../auth/jwt/jwt-auth.guard'; | ||
import { ChainInfoResponseDto } from './dto/chain-info-response.dto'; | ||
import { ChainInfo } from '../../shared/entities/chain-info.entity'; | ||
|
||
@ApiUnauthorizedResponse({ | ||
description: MESSAGES.ERROR.NOT_PERMISSION, | ||
}) | ||
@ApiForbiddenResponse({ | ||
description: MESSAGES.ERROR.NOT_PERMISSION, | ||
}) | ||
@ApiBadRequestResponse({ | ||
description: MESSAGES.ERROR.BAD_REQUEST, | ||
}) | ||
@ApiTags('chain-info') | ||
@Controller() | ||
export class ChainInfoController { | ||
constructor(private readonly chainInfoService: ChainInfoService) {} | ||
|
||
@Post('admin/chain-info') | ||
@UseGuards(JwtAuthGuard, RoleGuard) | ||
@Roles(USER_ROLE.ADMIN) | ||
@ApiBearerAuth() | ||
@ApiCreatedResponse({ | ||
type: ChainInfoResponseDto, | ||
}) | ||
async create(@Body() createChainInfoDto: CreateChainInfoDto) { | ||
return this.chainInfoService.create(createChainInfoDto); | ||
} | ||
|
||
@Get('chain-info') | ||
@ApiOkResponse({ | ||
type: SwaggerBaseApiResponse(ChainInfoResponseDto), | ||
}) | ||
@HttpCode(HttpStatus.OK) | ||
async findAll(): Promise<{ data: ChainInfo[]; meta: { count: number } }> { | ||
return await this.chainInfoService.findAll(); | ||
} | ||
|
||
@Get('chain-info/:id') | ||
@ApiOkResponse({ | ||
type: ChainInfoResponseDto, | ||
}) | ||
async findOne(@Param('id') id: string): Promise<ChainInfo> { | ||
return this.chainInfoService.findOne(+id); | ||
} | ||
|
||
@Put('admin/chain-info/:id') | ||
@UseGuards(JwtAuthGuard, RoleGuard) | ||
@Roles(USER_ROLE.ADMIN) | ||
@ApiBearerAuth() | ||
@ApiOkResponse({ | ||
type: ChainInfoResponseDto, | ||
}) | ||
async update( | ||
@Param('id') id: string, | ||
@Body() updateChainInfoDto: UpdateChainInfoDto, | ||
): Promise<ChainInfo> { | ||
return this.chainInfoService.update(+id, updateChainInfoDto); | ||
} | ||
|
||
@Delete('admin/chain-info/:id') | ||
@UseGuards(JwtAuthGuard, RoleGuard) | ||
@Roles(USER_ROLE.ADMIN) | ||
@ApiBearerAuth() | ||
@ApiNoContentResponse({ | ||
description: 'Delete successfully.', | ||
}) | ||
@HttpCode(HttpStatus.NO_CONTENT) | ||
async remove(@Param('id') id: string): Promise<void> { | ||
return this.chainInfoService.remove(+id); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { ChainInfoService } from './chain-info.service'; | ||
import { ChainInfoController } from './chain-info.controller'; | ||
import { ChainInfo } from '../../shared/entities/chain-info.entity'; | ||
import { TypeOrmModule } from '@nestjs/typeorm'; | ||
import { IsUniqueConstraint } from './validator/is-unique.validator'; | ||
import { UserModule } from '../user/user.module'; | ||
|
||
@Module({ | ||
imports: [TypeOrmModule.forFeature([ChainInfo]), UserModule], | ||
controllers: [ChainInfoController], | ||
providers: [ChainInfoService, IsUniqueConstraint], | ||
}) | ||
export class ChainInfoModule {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import { Injectable, NotFoundException } from '@nestjs/common'; | ||
import { CreateChainInfoDto } from './dto/create-chain-info.dto'; | ||
import { UpdateChainInfoDto } from './dto/update-chain-info.dto'; | ||
import { InjectRepository } from '@nestjs/typeorm'; | ||
import { ChainInfo } from '../../shared/entities/chain-info.entity'; | ||
import { Repository } from 'typeorm'; | ||
|
||
@Injectable() | ||
export class ChainInfoService { | ||
constructor( | ||
@InjectRepository(ChainInfo) | ||
private readonly chainInfoRepository: Repository<ChainInfo>, | ||
) {} | ||
|
||
async create(createChainInfoDto: CreateChainInfoDto): Promise<ChainInfo> { | ||
return this.chainInfoRepository.save(createChainInfoDto); | ||
} | ||
|
||
async findAll(): Promise<{ data: ChainInfo[]; meta: { count: number } }> { | ||
const [data, count] = await this.chainInfoRepository.findAndCount({ | ||
order: { updated_at: 'DESC', created_at: 'DESC' }, | ||
}); | ||
return { data: data || [], meta: { count: count || 0 } }; | ||
} | ||
|
||
async findOne(id: number): Promise<ChainInfo> { | ||
const chainInfo = await this.chainInfoRepository.findOne(id); | ||
|
||
if (!chainInfo) { | ||
throw new NotFoundException('Chain not found.'); | ||
} | ||
|
||
return chainInfo; | ||
} | ||
|
||
async update( | ||
id: number, | ||
updateChainInfoDto: UpdateChainInfoDto, | ||
): Promise<ChainInfo> { | ||
const currentChainInfo = await this.chainInfoRepository.findOne(id); | ||
|
||
if (!currentChainInfo) { | ||
throw new NotFoundException('Chain not found.'); | ||
} | ||
|
||
updateChainInfoDto.id = id; | ||
this.chainInfoRepository.merge(currentChainInfo, updateChainInfoDto); | ||
|
||
return this.chainInfoRepository.save(currentChainInfo); | ||
} | ||
|
||
async remove(id: number): Promise<void> { | ||
const chainInfo = await this.chainInfoRepository.findOne(id); | ||
|
||
if (!chainInfo) { | ||
throw new NotFoundException('Chain not found.'); | ||
} | ||
|
||
this.chainInfoRepository.delete(id); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { ApiProperty, PartialType } from '@nestjs/swagger'; | ||
import { UpdateChainInfoDto } from './update-chain-info.dto'; | ||
|
||
export class ChainInfoResponseDto extends PartialType(UpdateChainInfoDto) { | ||
@ApiProperty() | ||
created_at: Date; | ||
|
||
@ApiProperty() | ||
updated_at: Date; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { ApiProperty } from '@nestjs/swagger'; | ||
import { IsString } from 'class-validator'; | ||
import { IsUnique } from '../validator/is-unique.validator'; | ||
|
||
export class CreateChainInfoDto { | ||
@ApiProperty() | ||
@IsString() | ||
@IsUnique(['chainId'], { message: 'Chain id must be unique.' }) | ||
chainId: string; | ||
|
||
@ApiProperty() | ||
@IsString() | ||
chainName: string; | ||
|
||
@ApiProperty() | ||
@IsString() | ||
chainImage: string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { ApiProperty, PartialType } from '@nestjs/swagger'; | ||
import { CreateChainInfoDto } from './create-chain-info.dto'; | ||
|
||
export class UpdateChainInfoDto extends PartialType(CreateChainInfoDto) { | ||
@ApiProperty() | ||
id: number; | ||
} |
54 changes: 54 additions & 0 deletions
54
src/components/chain-info/validator/is-unique.validator.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import { | ||
registerDecorator, | ||
ValidationOptions, | ||
ValidatorConstraint, | ||
ValidatorConstraintInterface, | ||
ValidationArguments, | ||
} from 'class-validator'; | ||
import { Injectable } from '@nestjs/common'; | ||
import { InjectRepository } from '@nestjs/typeorm'; | ||
import { Not, Repository } from 'typeorm'; | ||
import { ChainInfo } from '../../../shared/entities/chain-info.entity'; | ||
|
||
@Injectable() | ||
@ValidatorConstraint({ name: 'isUnique', async: true }) | ||
export class IsUniqueConstraint implements ValidatorConstraintInterface { | ||
constructor( | ||
@InjectRepository(ChainInfo) | ||
private chainInfoRepository: Repository<ChainInfo>, | ||
) {} | ||
|
||
async validate(value: any, args: ValidationArguments) { | ||
const entity = args.object as any; | ||
const propertiesName = args.constraints[0]; | ||
|
||
const whereCondition = propertiesName.reduce( | ||
(where, propertyName) => { | ||
where[propertyName] = args.object[propertyName]; | ||
return where; | ||
}, | ||
{ id: Not(Number(entity?.id) || 0) }, | ||
); | ||
const existingChainInfo = await this.chainInfoRepository.findOne({ | ||
where: whereCondition, | ||
}); | ||
|
||
return !existingChainInfo; | ||
} | ||
} | ||
|
||
export function IsUnique( | ||
properties: string[], | ||
validationOptions?: ValidationOptions, | ||
) { | ||
return function (object: any, propertyName: string) { | ||
registerDecorator({ | ||
name: 'isUnique', | ||
target: object.constructor, | ||
propertyName: propertyName, | ||
constraints: [properties], | ||
options: validationOptions, | ||
validator: IsUniqueConstraint, | ||
}); | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,13 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { ScheduleModule } from '@nestjs/schedule'; | ||
import { SharedModule } from '../shared/shared.module'; | ||
import { MetricModule } from './metric/metric.module'; | ||
import { ChainInfoModule } from './chain-info/chain-info.module'; | ||
|
||
@Module({ | ||
imports: [SharedModule, MetricModule, ScheduleModule.forRoot()], | ||
imports: [ | ||
SharedModule, | ||
ScheduleModule.forRoot(), | ||
ChainInfoModule, | ||
], | ||
}) | ||
export class ComponentsModule {} |
Oops, something went wrong.