Skip to content

Commit

Permalink
Baseline/serenity 20230926 (#937)
Browse files Browse the repository at this point in the history
[Public-name-tag][CW20] Enhance admin nametag
  • Loading branch information
nthduc95 authored Sep 26, 2023
1 parent f27e99c commit 23e5f22
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 30 deletions.
2 changes: 1 addition & 1 deletion src/auth/google/google-oauth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export class GoogleOAuthService {
}

if (request.site !== SITE.MAIN) {
this.userService.checkRole(user);
this.userService.checkRole(user, SITE.ADMIN);
}
return { user, picture };
} catch (error) {
Expand Down
22 changes: 17 additions & 5 deletions src/components/cw20-token/services/cw20-token.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -235,11 +235,11 @@ export class Cw20TokenService {
.data[this.chainDB]['cw20_contract'];

let cw20Price = 0;

let ibcPrice = 0;
const listTokenMarketsInfo = await this.tokenMarketsRepository.find({
where: { coin_id: Not(''), verify_status: 'VERIFIED' },
});
if (response?.length > 0) {
const listTokenMarketsInfo = await this.tokenMarketsRepository.find({
where: { coin_id: Not(''), verify_status: 'VERIFIED' },
});
response.forEach((item) => {
const tokenMarketsInfo = listTokenMarketsInfo?.find(
(f) => f.contract_address === item.smart_contract.address,
Expand All @@ -257,7 +257,19 @@ export class Cw20TokenService {
});
}

return auraPrice + cw20Price;
//Get IBC tokens
const ibcTokens = await this.getIBCTokens(ctx, accountAddress);
ibcTokens?.forEach((item) => {
const tokenMarketsInfo = listTokenMarketsInfo?.find(
(f) => f.denom === item.denom,
);
const price = tokenMarketsInfo?.current_price
? Number(tokenMarketsInfo?.current_price)
: 0;
ibcPrice += price * item.balance;
});

return auraPrice + cw20Price + ibcPrice;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
Post,
Put,
Query,
Req,
UseGuards,
UseInterceptors,
} from '@nestjs/common';
Expand Down Expand Up @@ -41,6 +42,7 @@ import { JwtAuthGuard } from '../../../auth/jwt/jwt-auth.guard';
import { PublicNameTag } from '../../../shared/entities/public-name-tag.entity';
import { GetPublicNameTagResult } from '../dtos/get-public-name-tag-result.dto';
import { GetPublicNameTagAdminResult } from '../dtos/get-public-name-tag-admin.dto';
import { UpdatePublicNameTagParamsDto } from '../dtos/update-public-name-tag-params.dto';

@Controller()
@ApiTags('public-name-tag')
Expand Down Expand Up @@ -104,10 +106,15 @@ export class PublicNameTagController {
@ApiResponse({ status: HttpStatus.CREATED })
async createPublicNameTag(
@ReqContext() ctx: RequestContext,
@Req() req,
@Body() request: StorePublicNameTagParamsDto,
): Promise<any> {
this.logger.log(ctx, `${this.createPublicNameTag.name} was called!`);
return await this.nameTagService.createPublicNameTag(ctx, request);
return await this.nameTagService.createPublicNameTag(
ctx,
req.user.id,
request,
);
}

@Put('admin/public-name-tag')
Expand All @@ -118,10 +125,15 @@ export class PublicNameTagController {
@ApiResponse({ status: HttpStatus.OK })
async updateNameTag(
@ReqContext() ctx: RequestContext,
@Body() request: StorePublicNameTagParamsDto,
@Req() req,
@Body() request: UpdatePublicNameTagParamsDto,
): Promise<any> {
this.logger.log(ctx, `${this.updateNameTag.name} was called!`);
return await this.nameTagService.updatePublicNameTag(ctx, request);
return await this.nameTagService.updatePublicNameTag(
ctx,
req.user.id,
request,
);
}

@Delete('admin/public-name-tag/:id')
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger';
import { NAME_TAG_TYPE } from '../../../shared';
import { IsOptional } from 'class-validator';
import { IsOptional, MaxLength } from 'class-validator';

export class StorePublicNameTagParamsDto {
@ApiProperty({ default: '' })
Expand All @@ -13,11 +13,9 @@ export class StorePublicNameTagParamsDto {
address: string;

@ApiProperty({ default: '' })
@MaxLength(35)
nameTag: string;

@ApiProperty({ default: '' })
userId: number;

@IsOptional()
@ApiPropertyOptional({
default: null,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger';
import { NAME_TAG_TYPE } from '../../../shared';
import { IsOptional, MaxLength } from 'class-validator';

export class UpdatePublicNameTagParamsDto {
@ApiProperty({ default: '' })
id: number;

@ApiProperty({ default: '' })
@MaxLength(35)
nameTag: string;

@IsOptional()
@ApiPropertyOptional({
default: null,
})
enterpriseUrl: string;
}
33 changes: 17 additions & 16 deletions src/components/public-name-tag/services/public-name-tag.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { PublicNameTag } from '../../../shared/entities/public-name-tag.entity';
import { GetPublicNameTagResult } from '../dtos/get-public-name-tag-result.dto';
import { Not } from 'typeorm';
import { ServiceUtil } from '../../../shared/utils/service.util';
import { UpdatePublicNameTagParamsDto } from '../dtos/update-public-name-tag-params.dto';

@Injectable()
export class PublicNameTagService {
Expand Down Expand Up @@ -39,6 +40,7 @@ export class PublicNameTagService {

async createPublicNameTag(
ctx: RequestContext,
userId: number,
req: StorePublicNameTagParamsDto,
) {
this.logger.log(ctx, `${this.createPublicNameTag.name} was called!`);
Expand All @@ -50,7 +52,7 @@ export class PublicNameTagService {
entity.address = req.address;
entity.type = req.type;
entity.name_tag = req.nameTag;
entity.updated_by = req.userId;
entity.updated_by = userId;
entity.enterpriseUrl = req.enterpriseUrl;
try {
const result = await this.nameTagRepository.save(entity);
Expand All @@ -65,18 +67,17 @@ export class PublicNameTagService {

async updatePublicNameTag(
ctx: RequestContext,
req: StorePublicNameTagParamsDto,
userId: number,
req: UpdatePublicNameTagParamsDto,
) {
this.logger.log(ctx, `${this.updatePublicNameTag.name} was called!`);
const errorMsg = await this.validate(req, false);
if (errorMsg) {
return errorMsg;
}
const entity = new PublicNameTag();
entity.address = req.address;
entity.type = req.type;
entity.name_tag = req.nameTag;
entity.updated_by = req.userId;
entity.updated_by = userId;
entity.enterpriseUrl = req.enterpriseUrl;
try {
const result = await this.nameTagRepository.update(req.id, entity);
Expand All @@ -101,17 +102,7 @@ export class PublicNameTagService {
}
}

private async validate(req: StorePublicNameTagParamsDto, isCreate = true) {
const validFormat = await this.serviceUtil.isValidBech32Address(
req.address,
);

if (!validFormat) {
return {
code: ADMIN_ERROR_MAP.INVALID_FORMAT.Code,
message: ADMIN_ERROR_MAP.INVALID_FORMAT.Message,
};
}
private async validate(req: any, isCreate = true) {
if (!req.nameTag.match(REGEX_PARTERN.NAME_TAG)) {
return {
code: ADMIN_ERROR_MAP.INVALID_NAME_TAG.Code,
Expand All @@ -127,6 +118,16 @@ export class PublicNameTagService {
}

if (isCreate) {
const validFormat = await this.serviceUtil.isValidBech32Address(
req.address,
);

if (!validFormat) {
return {
code: ADMIN_ERROR_MAP.INVALID_FORMAT.Code,
message: ADMIN_ERROR_MAP.INVALID_FORMAT.Message,
};
}
// check duplicate address
const address = await this.nameTagRepository.findOne({
where: { address: req.address },
Expand Down
7 changes: 6 additions & 1 deletion src/components/user/user.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
MSGS_USER,
PROVIDER,
QUEUES,
SITE,
SUPPORT_EMAIL,
USER_ACTIVITIES,
USER_ROLE,
Expand Down Expand Up @@ -100,14 +101,18 @@ export class UserService {
await this.usersRepository.save(user);
}

checkRole(user: DeepPartial<User>): void {
checkRole(user: DeepPartial<User>, site = SITE.MAIN): void {
if (!user) {
throw new UnauthorizedException(MESSAGES.ERROR.NOT_PERMISSION);
}

if (user.role === USER_ROLE.BANNED) {
throw new UnauthorizedException(MESSAGES.ERROR.BANNED);
}

if (site === SITE.ADMIN && user.role === USER_ROLE.USER) {
throw new UnauthorizedException(MESSAGES.ERROR.NOT_PERMISSION);
}
}

async createUserWithPassword(
Expand Down

0 comments on commit 23e5f22

Please sign in to comment.