From 3b87f659ee220920d2664f61acd5e07a2fc1d359 Mon Sep 17 00:00:00 2001 From: Maintain Date: Mon, 18 Sep 2023 15:59:36 +0700 Subject: [PATCH] fix duplicate private name tag in an account (#929) * fix duplicate private name tag in an account * fix comment --- .../services/private-name-tag.service.ts | 35 +++++++++++++++---- src/shared/constants/common.ts | 4 +++ 2 files changed, 32 insertions(+), 7 deletions(-) diff --git a/src/components/private-name-tag/services/private-name-tag.service.ts b/src/components/private-name-tag/services/private-name-tag.service.ts index 16664e3b..7f2d3515 100644 --- a/src/components/private-name-tag/services/private-name-tag.service.ts +++ b/src/components/private-name-tag/services/private-name-tag.service.ts @@ -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 { @@ -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; } @@ -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 }, @@ -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, @@ -161,10 +168,10 @@ 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, @@ -172,6 +179,20 @@ export class PrivateNameTagService { } } + // 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; } diff --git a/src/shared/constants/common.ts b/src/shared/constants/common.ts index fbc4e7b5..70f32e63 100644 --- a/src/shared/constants/common.ts +++ b/src/shared/constants/common.ts @@ -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',