Skip to content

Commit

Permalink
fix duplicate private name tag in an account (#929)
Browse files Browse the repository at this point in the history
* fix duplicate private name tag in an account

* fix comment
  • Loading branch information
duonghb53 authored Sep 18, 2023
1 parent 79b39aa commit 3b87f65
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,10 @@ import { PrivateNameTagParamsDto } from '../dtos/private-name-tag-params.dto';
import { PrivateNameTagRepository } from '../repositories/private-name-tag.repository';
import { CreatePrivateNameTagParamsDto } from '../dtos/create-private-name-tag-params.dto';
import { GetPrivateNameTagResult } from '../dtos/get-private-name-tag-result.dto';
import { Not } from 'typeorm';
import { PrivateNameTag } from '../../../shared/entities/private-name-tag.entity';
import { UpdatePrivateNameTagParamsDto } from '../dtos/update-private-name-tag-params.dto';
import { EncryptionService } from '../../encryption/encryption.service';
import { ServiceUtil } from '../../../shared/utils/service.util';
import { validate } from 'class-validator';

@Injectable()
export class PrivateNameTagService {
Expand Down Expand Up @@ -64,7 +62,7 @@ export class PrivateNameTagService {

async createNameTag(ctx: RequestContext, req: CreatePrivateNameTagParamsDto) {
this.logger.log(ctx, `${this.createNameTag.name} was called!`);
const errorMsg = await this.validate(req);
const errorMsg = await this.validate(ctx.user.id, req);
if (errorMsg) {
return errorMsg;
}
Expand Down Expand Up @@ -94,6 +92,11 @@ export class PrivateNameTagService {
req: UpdatePrivateNameTagParamsDto,
) {
this.logger.log(ctx, `${this.updateNameTag.name} was called!`);
const request: CreatePrivateNameTagParamsDto = { ...req, address: '' };
const errorMsg = await this.validate(ctx.user.id, request, false);
if (errorMsg) {
return errorMsg;
}

const entity = await this.privateNameTagRepository.findOne(id, {
where: { createdBy: ctx.user.id },
Expand Down Expand Up @@ -141,7 +144,11 @@ export class PrivateNameTagService {
}
}

private async validate(req: CreatePrivateNameTagParamsDto, isCreate = true) {
private async validate(
user_id: number,
req: CreatePrivateNameTagParamsDto,
isCreate = true,
) {
if (isCreate) {
const validFormat = await this.serviceUtil.isValidBech32Address(
req.address,
Expand All @@ -161,17 +168,31 @@ export class PrivateNameTagService {
}

// check duplicate address
const address = await this.privateNameTagRepository.findOne({
where: { address: req.address },
const entity = await this.privateNameTagRepository.findOne({
where: { createdBy: user_id, address: req.address },
});
if (address) {
if (entity) {
return {
code: ADMIN_ERROR_MAP.DUPLICATE_ADDRESS.Code,
message: ADMIN_ERROR_MAP.DUPLICATE_ADDRESS.Message,
};
}
}

// check duplicate private name tag
const entity = await this.privateNameTagRepository.findOne({
where: {
createdBy: user_id,
nameTag: await this.encryptionService.encrypt(req.nameTag),
},
});
if (entity) {
return {
code: ADMIN_ERROR_MAP.DUPLICATE_PRIVATE_TAG.Code,
message: ADMIN_ERROR_MAP.DUPLICATE_PRIVATE_TAG.Message,
};
}

return false;
}

Expand Down
4 changes: 4 additions & 0 deletions src/shared/constants/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,10 @@ export const ADMIN_ERROR_MAP = {
Code: 'E002',
Message: 'Duplicate name tag',
},
DUPLICATE_PRIVATE_TAG: {
Code: 'E002',
Message: 'Duplicate private name tag',
},
INVALID_FORMAT: {
Code: 'E003',
Message: 'Invalid aura address format',
Expand Down

0 comments on commit 3b87f65

Please sign in to comment.