Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix duplicate private name tag in an account #929

Merged
merged 2 commits into from
Sep 18, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -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(request, false);
if (errorMsg) {
return errorMsg;
}

const entity = await this.privateNameTagRepository.findOne(id, {
where: { createdBy: ctx.user.id },
Expand Down Expand Up @@ -161,17 +164,28 @@ export class PrivateNameTagService {
}

// check duplicate address
const address = await this.privateNameTagRepository.findOne({
const entity = await this.privateNameTagRepository.findOne({
duonghb53 marked this conversation as resolved.
Show resolved Hide resolved
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) },
duonghb53 marked this conversation as resolved.
Show resolved Hide resolved
});
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
Loading