Skip to content

Commit

Permalink
[[Private Name Tag] Limited - Squash and merge to develop (#938) (#939)
Browse files Browse the repository at this point in the history
* add check limited private name tag

* fix litmit private name tag & remove api unused

* change message limit from env
  • Loading branch information
duonghb53 committed Oct 2, 2023
1 parent 657f35f commit bd995d1
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 174 deletions.
5 changes: 4 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -88,4 +88,7 @@ KMS_ACCESS_KEY_ID=
KMS_SECRET_ACCESS_KEY=
KMS_REGION=
KMS_API_VERSION=
KMS_ALIAS=
KMS_ALIAS=

# Private Name Tag
LIMITED_PRIVATE_NAME_TAG=10
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ import { Roles } from '../../../auth/role/roles.decorator';
import { JwtAuthGuard } from '../../../auth/jwt/jwt-auth.guard';
import { PrivateNameTag } from '../../../shared/entities/private-name-tag.entity';
import { GetPrivateNameTagAdminResult } from '../dtos/get-private-name-tag-admin.dto';
import { GetPrivateNameTagResult } from '../dtos/get-private-name-tag-result.dto';
import { UpdatePrivateNameTagParamsDto } from '../dtos/update-private-name-tag-params.dto';

@Controller()
Expand Down Expand Up @@ -138,47 +137,4 @@ export class PrivateNameTagController {
this.logger.log(ctx, `${this.deleteNameTag.name} was called!`);
return await this.nameTagService.deleteNameTag(ctx, id);
}

@Get('private-name-tag')
@UseGuards(JwtAuthGuard, RoleGuard)
@Roles(USER_ROLE.ADMIN, USER_ROLE.USER)
@ApiBearerAuth()
@ApiResponse({ status: HttpStatus.OK })
@HttpCode(HttpStatus.OK)
@ApiOperation({ summary: 'Get all private name tag.' })
@ApiOkResponse({
description: 'Get all private name tag.',
type: GetPrivateNameTagResult,
})
@ApiQuery({
name: 'limit',
type: Number,
required: false,
description: 'Number of private name tag per page. Max is 500.',
})
@ApiQuery({
name: 'nextKey',
type: Number,
required: false,
description: 'Key for next page.',
})
@ApiQuery({
name: 'keyword',
type: String,
required: false,
description: 'Key for search: Address/Name Tag.',
})
async getNameTag(
@ReqContext() ctx: RequestContext,
@Query('limit') limit?: number,
@Query('nextKey') nextKey?: number,
@Query('keyword') keyword?: string,
): Promise<GetPrivateNameTagResult> {
return await this.nameTagService.getNameTagMainSite({
user_id: ctx.user?.id || 0,
limit,
nextKey,
keyword,
});
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export class PrivateNameTagRepository extends Repository<PrivateNameTag> {
async getNameTags(
user_id: number,
keyword: string,
keywordEncrypt: string,
limit: number,
offset: number,
) {
Expand Down Expand Up @@ -53,61 +54,15 @@ export class PrivateNameTagRepository extends Repository<PrivateNameTag> {
if (keyword) {
builder.andWhere(
new Brackets((qb) => {
qb.where('LOWER(tag.address) LIKE LOWER(:keyword)', {
keyword: `%${keyword}%`,
}).orWhere('tag.name_tag LIKE :keyword', {
keyword: `%${keyword}%`,
qb.where('tag.address = :keyword', {
keyword: `${keyword}`,
}).orWhere('tag.name_tag = :keywordEncrypt', {
keywordEncrypt: `${keywordEncrypt}`,
});
}),
);
}

return await _finalizeResult();
}

async getNameTagMainSite(
user_id: number,
limit: number,
nextKey: number,
keyword: string,
keywordEncrypt: string,
): Promise<PrivateNameTag[]> {
limit = Number(limit) || PAGE_REQUEST.MAX_500;

if (limit > PAGE_REQUEST.MAX_500) {
limit = PAGE_REQUEST.MAX_500;
}

let qb = this.createQueryBuilder()
.select([
'id',
'is_favorite as isFavorite',
'address',
'name_tag as nameTag',
'note',
'created_at as createdAt',
'updated_at as updatedAt',
])
.where('created_by = :user_id', { user_id })
.orderBy('is_favorite', 'DESC')
.addOrderBy('updated_at', 'DESC')
.limit(Number(limit) || PAGE_REQUEST.MAX_500);

if (nextKey) {
qb = qb.andWhere('id > :nextKey', { nextKey });
}

if (keyword) {
qb.andWhere(
new Brackets((qb) => {
qb.where('address = :keyword', { keyword }).orWhere(
'name_tag = :keywordEncrypt',
{ keywordEncrypt: keywordEncrypt },
);
}),
);
}

return qb.getRawMany();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,27 +14,32 @@ import {
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 { 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 { Not } from 'typeorm';
import * as appConfig from '../../../shared/configs/configuration';

@Injectable()
export class PrivateNameTagService {
private config;

constructor(
private readonly logger: AkcLogger,
private encryptionService: EncryptionService,
private privateNameTagRepository: PrivateNameTagRepository,
private serviceUtil: ServiceUtil,
) {}
) {
this.config = appConfig.default();
}

async getNameTags(ctx: RequestContext, req: PrivateNameTagParamsDto) {
this.logger.log(ctx, `${this.getNameTags.name} was called!`);
const { result, count } = await this.privateNameTagRepository.getNameTags(
ctx.user.id,
req.keyword,
await this.encryptionService.encrypt(req.keyword ?? ''),
req.limit,
req.offset,
);
Expand Down Expand Up @@ -169,6 +174,18 @@ export class PrivateNameTagService {
};
}

// check limited private name tag
const count = await this.privateNameTagRepository.count({
where: { createdBy: user_id },
});

if (count >= this.config.limitedPrivateNameTag) {
return {
code: ADMIN_ERROR_MAP.LIMIT_PRIVATE_NAME_TAG.Code,
message: ADMIN_ERROR_MAP.LIMIT_PRIVATE_NAME_TAG.Message,
};
}

// check duplicate address
const entity = await this.privateNameTagRepository.findOne({
where: { createdBy: user_id, address: req.address },
Expand All @@ -186,7 +203,7 @@ export class PrivateNameTagService {
where: {
id: Not(id),
createdBy: user_id,
nameTag: await this.encryptionService.encrypt(req.nameTag ?? ""),
nameTag: await this.encryptionService.encrypt(req.nameTag ?? ''),
},
});
if (entity) {
Expand All @@ -198,38 +215,4 @@ export class PrivateNameTagService {

return false;
}

async getNameTagMainSite(req: {
user_id: number;
limit: number;
nextKey: number;
keyword: string;
}): Promise<GetPrivateNameTagResult> {
const nameTags = await this.privateNameTagRepository.getNameTagMainSite(
Number(req.user_id),
Number(req.limit),
Number(req.nextKey),
req.keyword,
await this.encryptionService.encrypt(req.keyword),
);

const nextKey = nameTags.slice(-1)[0]?.id;

const data = await Promise.all(
nameTags.map(async (item) => {
item.nameTag = await this.encryptionService.decrypt(item.nameTag);
return item;
}),
);

const result = {
data: {
nameTags: data,
count: Number(nameTags.length),
nextKey: nextKey || null,
},
};

return result;
}
}
1 change: 1 addition & 0 deletions src/shared/configs/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,5 @@ export default () => ({
alias: process.env.KMS_ALIAS,
},
addressPrefix: process.env.ADDRESS_PREFIX,
limitedPrivateNameTag: process.env.LIMITED_PRIVATE_NAME_TAG || 10,
});
6 changes: 6 additions & 0 deletions src/shared/constants/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,12 @@ export const ADMIN_ERROR_MAP = {
Code: 'E005',
Message: 'Invalid URL format',
},
LIMIT_PRIVATE_NAME_TAG: {
Code: 'E006',
Message: `You have reached out of ${
process.env.LIMITED_PRIVATE_NAME_TAG || 10
} max limitation of private name tag`,
},
};

export const PAGE_REQUEST = {
Expand Down

0 comments on commit bd995d1

Please sign in to comment.