From f0606f81e7075c2c395aeea6d0aad6f200411c5b Mon Sep 17 00:00:00 2001 From: faris Aljohari <83524184+farisaljohari@users.noreply.github.com> Date: Sat, 1 Jun 2024 20:25:05 +0300 Subject: [PATCH] Add generateRandomString helper function and remove nanoid dependency --- libs/common/src/helper/randomString.ts | 10 ++++++++++ package-lock.json | 15 --------------- package.json | 1 - src/unit/services/unit.service.ts | 7 ++++--- 4 files changed, 14 insertions(+), 19 deletions(-) create mode 100644 libs/common/src/helper/randomString.ts diff --git a/libs/common/src/helper/randomString.ts b/libs/common/src/helper/randomString.ts new file mode 100644 index 0000000..5a664e3 --- /dev/null +++ b/libs/common/src/helper/randomString.ts @@ -0,0 +1,10 @@ +export function generateRandomString(length: number): string { + const characters = + 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; + let randomString = ''; + for (let i = 0; i < length; i++) { + const randomIndex = Math.floor(Math.random() * characters.length); + randomString += characters.charAt(randomIndex); + } + return randomString; +} diff --git a/package-lock.json b/package-lock.json index a629215..47268ca 100644 --- a/package-lock.json +++ b/package-lock.json @@ -28,7 +28,6 @@ "helmet": "^7.1.0", "ioredis": "^5.3.2", "morgan": "^1.10.0", - "nanoid": "^5.0.7", "nodemailer": "^6.9.10", "passport-jwt": "^4.0.1", "pg": "^8.11.3", @@ -5255,20 +5254,6 @@ "integrity": "sha512-OO0pH2lK6a0hZnAdau5ItzHPI6pUlvI7jMVnxUQRtw4owF2wk8lOSabtGDCTP4Ggrg2MbGnWO9X8K1t4+fGMDw==", "dev": true }, - "node_modules/fsevents": { - "version": "2.3.3", - "resolved": "https://registry.npmjs.org/fsevents/-/fsevents-2.3.3.tgz", - "integrity": "sha512-5xoDfX+fL7faATnagmWPpbFtwh/R77WmMMqqHGS65C3vvB0YHrgF+B1YmZ3441tMj5n63k0212XNoJwzlhffQw==", - "dev": true, - "hasInstallScript": true, - "optional": true, - "os": [ - "darwin" - ], - "engines": { - "node": "^8.16.0 || ^10.6.0 || >=11.0.0" - } - }, "node_modules/function-bind": { "version": "1.1.2", "resolved": "https://registry.npmjs.org/function-bind/-/function-bind-1.1.2.tgz", diff --git a/package.json b/package.json index a5d2431..a8502be 100644 --- a/package.json +++ b/package.json @@ -39,7 +39,6 @@ "helmet": "^7.1.0", "ioredis": "^5.3.2", "morgan": "^1.10.0", - "nanoid": "^5.0.7", "nodemailer": "^6.9.10", "passport-jwt": "^4.0.1", "pg": "^8.11.3", diff --git a/src/unit/services/unit.service.ts b/src/unit/services/unit.service.ts index 8051469..449aaa1 100644 --- a/src/unit/services/unit.service.ts +++ b/src/unit/services/unit.service.ts @@ -18,6 +18,7 @@ import { import { SpaceEntity } from '@app/common/modules/space/entities'; import { UpdateUnitNameDto } from '../dtos/update.unit.dto'; import { UserSpaceRepository } from '@app/common/modules/user-space/repositories'; +import { generateRandomString } from '@app/common/helper/randomString'; @Injectable() export class UnitService { @@ -284,10 +285,8 @@ export class UnitService { } async getUnitInvitationCode(unitUuid: string): Promise { try { - const { nanoid } = await import('nanoid'); - // Generate a 6-character random invitation code - const invitationCode = nanoid(6); + const invitationCode = generateRandomString(6); // Update the unit with the new invitation code await this.spaceRepository.update({ uuid: unitUuid }, { invitationCode }); @@ -304,6 +303,8 @@ export class UnitService { type: updatedUnit.spaceType.type, }; } catch (err) { + console.log('err', err); + if (err instanceof BadRequestException) { throw err; } else {