Skip to content

Commit 0afd640

Browse files
kastovmurzindima
andcommitted
feat: add SHORT_UUID_LENGTH configuration and update UsersService to utilize it
- Introduced SHORT_UUID_LENGTH in the config schema with validation for values between 16 and 64. - Updated UsersService to retrieve SHORT_UUID_LENGTH from the configuration and use it in the createNanoId method for generating unique IDs. Co-authored-by: Dima Murzin <18555945+murzindima@users.noreply.github.com>
1 parent c56a8ed commit 0afd640

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

src/common/config/app-config/config.schema.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,11 @@ export const configSchema = z
4646
.transform((db) => parseInt(db, 10))
4747
.refine((db) => db >= 0 && db <= 15, 'Redis DB index must be between 0 and 15')
4848
.default('1'),
49+
SHORT_UUID_LENGTH: z
50+
.string()
51+
.default('16')
52+
.transform((val) => parseInt(val, 10))
53+
.refine((val) => val >= 16 && val <= 64, 'SHORT_UUID_LENGTH must be between 16 and 64'),
4954
})
5055
.superRefine((data, ctx) => {
5156
if (data.WEBHOOK_ENABLED === 'true') {

src/modules/users/users.service.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { CommandBus, EventBus, QueryBus } from '@nestjs/cqrs';
88
import { Transactional } from '@nestjs-cls/transactional';
99
import { EventEmitter2 } from '@nestjs/event-emitter';
1010
import { Injectable, Logger } from '@nestjs/common';
11+
import { ConfigService } from '@nestjs/config';
1112

1213
import { ICommandResponse } from '@common/types/command-response.type';
1314
import { ERRORS, USERS_STATUS, EVENTS } from '@libs/contracts/constants';
@@ -52,14 +53,18 @@ dayjs.extend(utc);
5253
@Injectable()
5354
export class UsersService {
5455
private readonly logger = new Logger(UsersService.name);
56+
private readonly shortUuidLength: number;
5557

5658
constructor(
5759
private readonly userRepository: UsersRepository,
5860
private readonly commandBus: CommandBus,
5961
private readonly eventBus: EventBus,
6062
private readonly eventEmitter: EventEmitter2,
6163
private readonly queryBus: QueryBus,
62-
) {}
64+
private readonly configService: ConfigService,
65+
) {
66+
this.shortUuidLength = this.configService.getOrThrow<number>('SHORT_UUID_LENGTH');
67+
}
6368

6469
public async createUser(
6570
dto: CreateUserRequestDto,
@@ -737,7 +742,7 @@ export class UsersService {
737742

738743
private createNanoId(): string {
739744
const alphabet = '0123456789ABCDEFGHJKLMNPQRSTUVWXYZ_abcdefghjkmnopqrstuvwxyz-';
740-
const nanoid = customAlphabet(alphabet, 16);
745+
const nanoid = customAlphabet(alphabet, this.shortUuidLength);
741746

742747
return nanoid();
743748
}

0 commit comments

Comments
 (0)