From 5de3acd138c760c45762b1721c9327012397536b Mon Sep 17 00:00:00 2001 From: duonghb53 Date: Mon, 18 Sep 2023 14:24:18 +0700 Subject: [PATCH] fix duplicate private name tag in an account --- .../services/private-name-tag.service.ts | 23 +++++++++++++++---- src/shared/constants/common.ts | 4 ++++ 2 files changed, 23 insertions(+), 4 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 16664e3b6..e63d697ec 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,11 @@ 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'; +import { assert } from 'joi'; @Injectable() export class PrivateNameTagService { @@ -94,6 +93,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(request, false); + if (errorMsg) { + return errorMsg; + } const entity = await this.privateNameTagRepository.findOne(id, { where: { createdBy: ctx.user.id }, @@ -161,10 +165,10 @@ export class PrivateNameTagService { } // check duplicate address - const address = await this.privateNameTagRepository.findOne({ + const entity = await this.privateNameTagRepository.findOne({ where: { address: req.address }, }); - if (address) { + if (entity) { return { code: ADMIN_ERROR_MAP.DUPLICATE_ADDRESS.Code, message: ADMIN_ERROR_MAP.DUPLICATE_ADDRESS.Message, @@ -172,6 +176,17 @@ export class PrivateNameTagService { } } + // check duplicate private name tag + const entity = await this.privateNameTagRepository.findOne({ + where: { 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 fbc4e7b55..70f32e63f 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',