Skip to content

Commit

Permalink
fix duplicate private name tag in an account
Browse files Browse the repository at this point in the history
  • Loading branch information
duonghb53 committed Sep 18, 2023
1 parent 79b39aa commit 5de3acd
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 },
Expand Down Expand Up @@ -161,17 +165,28 @@ 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,
};
}
}

// 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;
}

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 5de3acd

Please sign in to comment.