chore: complete utils modules

This commit is contained in:
mahyargdz
2025-01-19 18:40:51 +03:30
parent a934e5b55e
commit 7347a696d0
9 changed files with 118 additions and 7990 deletions
+3
View File
@@ -21,6 +21,8 @@
},
"dependencies": {
"@fastify/static": "^7.0.4",
"@keyv/redis": "^4.2.0",
"@nestjs/cache-manager": "^3.0.0",
"@nestjs/common": "^10.4.15",
"@nestjs/config": "^4.0.0",
"@nestjs/core": "^10.4.15",
@@ -28,6 +30,7 @@
"@nestjs/swagger": "^8.1.1",
"@nestjs/typeorm": "^10.0.2",
"axios": "^1.7.9",
"cache-manager": "^6.3.2",
"pg": "^8.13.1",
"reflect-metadata": "^0.2.2",
"rxjs": "^7.8.1",
-7987
View File
File diff suppressed because it is too large Load Diff
+3
View File
@@ -1,7 +1,9 @@
import { CacheModule } from "@nestjs/cache-manager";
import { Module } from "@nestjs/common";
import { ConfigModule } from "@nestjs/config";
import { TypeOrmModule } from "@nestjs/typeorm";
import { cacheConfig } from "./configs/cache.config";
import { DatabaseConfigs } from "./configs/typeorm.config";
import { TicketsModule } from "./modules/tickets/tickets.module";
import { UsersModule } from "./modules/users/users.module";
@@ -12,6 +14,7 @@ import { UsersModule } from "./modules/users/users.module";
cache: true,
isGlobal: true,
}),
CacheModule.registerAsync(cacheConfig()),
TypeOrmModule.forRootAsync(DatabaseConfigs()),
UsersModule,
TicketsModule,
+28
View File
@@ -0,0 +1,28 @@
export const enum AuthMessage {
PHONE_REGISTERED = "شماره ثبت شد",
INVALID_NEZAM_PHONE = "شماره موبایل وارد نامعتبر می باشد",
OTP_SENT = "کد یکبار مصرف به شماره شما ارسال شد.",
INVALID_PHONE = "شماره موبایل اشتباه می باشد",
INVALID_CREDENTIAL = "شماره موبایل یا رمز عبور اشتباه می باشد",
OTP_FAILED = "کد یا شماره موبایل اشتباه است",
LOGIN_SUCCESS = "ورود با موفقیت انجام شد",
PASSWORD_LOGIN_SUCCESS = "با موفقیت وارد شدید",
FORGOT_PASSWORD_SUCCESS = "درخواست فراموشی رمز عبور با موفقیت ثبت شد",
PASSWORD_SET_SUCCESS = "پسورد با موفقیت تنظیم شد",
PASSWORD_UPDATE_SUCCESS = "رمز عبور شما تغییر کرد",
INVALID_OTP = "کد صحیح نمی‌باشد",
PASSWORD_MISMATCH = "رمز عبور یکسان نیست",
INVALID_PASSWORD = "رمز عبور اشتباه است",
USER_NOT_FOUND = "کاربری با این شماره وجود ندارد",
USER_EXISTS = "با این شماره قبلا ثبت نام شده است",
USER_REGISTER_SUCCESS = "ثبت نام موفقیت آمیز بود",
INVALID_USER = "کاربری با این مشخصات یافت نشد",
PHONE_NOT_FOUND = "کاربری با این آیدی یافت نشد",
TOKEN_EXPIRED = "توکن منقضی شده است",
TOKEN_INVALID = "توکن نامعتبر است",
Banned = "در حال حاضر شما امکان دسترسی به این سرویس را ندارید",
PHONE_EXISTS = "شماره تلفن قبلا ثبت شده است",
ADMIN_CREATED = "ادمین با موفقیت ایجاد شد",
Permission_NOT_FOUND = "دسترسی یافت نشد",
ADMIN_NOT_FOUND = "ادمین یافت نشد",
}
+16
View File
@@ -0,0 +1,16 @@
import KeyvRedis from "@keyv/redis";
import { CacheModuleAsyncOptions } from "@nestjs/cache-manager";
import { ConfigService } from "@nestjs/config";
export function cacheConfig(): CacheModuleAsyncOptions {
return {
inject: [ConfigService],
isGlobal: true,
useFactory: async (configService: ConfigService) => {
return {
ttl: configService.getOrThrow<string>("CACHE_TTL"),
stores: [new KeyvRedis(configService.getOrThrow<string>("REDIS_URI"))],
};
},
};
}
@@ -0,0 +1,19 @@
import { CACHE_MANAGER, Cache } from "@nestjs/cache-manager";
import { Inject, Injectable } from "@nestjs/common";
@Injectable()
export class CacheService {
constructor(@Inject(CACHE_MANAGER) private cacheManager: Cache) {}
async get<T>(key: string) {
return await this.cacheManager.get<T>(key);
}
async set(key: string, value: string, ttl: number = 120 * 1000) {
await this.cacheManager.set(key, value, ttl);
}
async del(key: string) {
await this.cacheManager.del(key);
}
}
@@ -0,0 +1,35 @@
import { randomInt } from "node:crypto";
import { BadRequestException, Injectable } from "@nestjs/common";
import { CacheService } from "./cache.service";
import { AuthMessage } from "../../../common/enums/message.enum";
@Injectable()
export class OTPService {
constructor(private cacheService: CacheService) {}
//
private generateOTP(): string {
return randomInt(10000, 99999).toString();
}
async generateAndStore(identifier: string, cacheKey: string = "Login") {
const otp = this.generateOTP();
const key = `${identifier}:${cacheKey}:OTP`;
await this.cacheService.set(key, otp);
}
async verifyOtp(identifier: string, otpCode: string, cacheKey: string = "Login") {
const key = `${identifier}:${cacheKey}:Otp`;
const codeInCache = await this.cacheService.get(key);
if (!codeInCache || otpCode !== codeInCache) throw new BadRequestException(AuthMessage.INVALID_OTP);
await this.cacheService.del(key);
}
// async verify(identifier: string, otp: string): Promise<boolean> {
// // Here you would typically verify the OTP against stored value
// // and check if it's not expired
// return true; // Placeholder implementation
// }
}
@@ -0,0 +1,6 @@
import { Injectable } from "@nestjs/common";
@Injectable()
export class PasswordService {
async hashPassword(rawPassword: string) {}
}
+8 -3
View File
@@ -1,15 +1,20 @@
import { Module } from "@nestjs/common";
import { SMS_CONFIG } from "./constants";
import { CacheService } from "./providers/cache.service";
import { OTPService } from "./providers/otp.service";
import { SmsConfigs } from "../../configs/sms.config";
@Module({
providers: [
OTPService,
CacheService,
{
provide: SMS_CONFIG,
useFactory: SmsConfigs().useFactory,
inject: SmsConfigs().inject
inject: SmsConfigs().inject,
},
],
exports: [SMS_CONFIG]
exports: [SMS_CONFIG],
})
export class UtilsModule { }
export class UtilsModule {}