Skip to content

Commit

Permalink
Merge pull request #1064 from aura-nw/Baseline/main_20231225
Browse files Browse the repository at this point in the history
[IBC][MAIN] Baseline/main 20231225
  • Loading branch information
nhphuc2411 authored Dec 26, 2023
2 parents b6f2146 + 2db2441 commit bf15cf9
Show file tree
Hide file tree
Showing 71 changed files with 1,060 additions and 1,939 deletions.
16 changes: 0 additions & 16 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,26 +39,10 @@ services:
- "6379:6379"
networks:
- dev_network
influxdb:
image: influxdb:2.1.1
environment:
- DOCKER_INFLUXDB_INIT_MODE=setup
- DOCKER_INFLUXDB_INIT_USERNAME=admin
- DOCKER_INFLUXDB_INIT_PASSWORD=adminAurascan
- DOCKER_INFLUXDB_INIT_ORG=aura_dev
- DOCKER_INFLUXDB_INIT_BUCKET=aurascan
- DOCKER_INFLUXDB_INIT_ADMIN_TOKEN=aurascan-example-token
volumes:
- influxdb_data:/var/lib/influxdb
ports:
- '8086:8086'
networks:
- dev_network

volumes:
db_data:
redis_data:
influxdb_data:

networks:
dev_network:
Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
"@cosmjs/cosmwasm-stargate": "^0.29.4",
"@cosmjs/crypto": "^0.29.4",
"@cosmjs/encoding": "^0.29.4",
"@influxdata/influxdb-client": "^1.31.0",
"@json2csv/plainjs": "^6.1.3",
"@nestjs-modules/mailer": "^1.8.1",
"@nestjs/axios": "0.0.3",
Expand Down
3 changes: 1 addition & 2 deletions src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import { AccountModule } from './components/account/account.module';
import { ServiceUtil } from './shared/utils/service.util';
import { ContractModule } from './components/contract/contract.module';
import { Cw20TokenModule } from './components/cw20-token/cw20-token.module';
import { MetricService } from './components/metric/services/metric.service';
import { SoulboundTokenModule } from './components/soulbound-token/soulbound-token.module';
import { AuthModule } from './auth/auth.module';
import { MailModule } from './components/mail/mail.module';
Expand Down Expand Up @@ -50,6 +49,6 @@ import { WatchListModule } from './components/watch-list/watch-list.module';
inject: [ConfigService],
}),
],
providers: [ServiceUtil, MetricService],
providers: [ServiceUtil],
})
export class AppModule {}
100 changes: 100 additions & 0 deletions src/components/chain-info/chain-info.controller.ts
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);
}
}
14 changes: 14 additions & 0 deletions src/components/chain-info/chain-info.module.ts
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 {}
61 changes: 61 additions & 0 deletions src/components/chain-info/chain-info.service.ts
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);
}
}
10 changes: 10 additions & 0 deletions src/components/chain-info/dto/chain-info-response.dto.ts
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;
}
18 changes: 18 additions & 0 deletions src/components/chain-info/dto/create-chain-info.dto.ts
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;
}
7 changes: 7 additions & 0 deletions src/components/chain-info/dto/update-chain-info.dto.ts
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 src/components/chain-info/validator/is-unique.validator.ts
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,
});
};
}
8 changes: 6 additions & 2 deletions src/components/components.module.ts
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 {}
Loading

0 comments on commit bf15cf9

Please sign in to comment.