From 6813ab3b8f23ef98da9a8e6002595760c7d63fa0 Mon Sep 17 00:00:00 2001 From: CllDMR Date: Sat, 18 Feb 2023 18:03:39 +0300 Subject: [PATCH] feat: :recycle: rewritten config calls with onModuleInit --- src/dispatch/optiyol.service-client.ts | 21 ++++--- src/notification/services/aws-sns.service.ts | 31 ++++++--- src/user/user.service.ts | 66 +++++++++++--------- 3 files changed, 70 insertions(+), 48 deletions(-) diff --git a/src/dispatch/optiyol.service-client.ts b/src/dispatch/optiyol.service-client.ts index 2371597..c8bb11a 100644 --- a/src/dispatch/optiyol.service-client.ts +++ b/src/dispatch/optiyol.service-client.ts @@ -1,5 +1,6 @@ -import { Injectable } from '@nestjs/common'; import { HttpService } from '@nestjs/axios'; +import { Injectable, OnModuleInit } from '@nestjs/common'; +import { ConfigService } from '@nestjs/config'; import { RawAxiosRequestHeaders } from 'axios'; import { PinoLogger } from 'nestjs-pino'; import { LogMe } from 'src/common/decorators/log.decorator'; @@ -11,21 +12,27 @@ import { OptiyolDispatchOrderResult, OptiyolDispatchVehicleResult, } from './types/optiyol.types'; -import { ConfigService } from '@nestjs/config'; @Injectable() -export class OptiyolServiceClient { - private readonly authHeaders: Partial; +export class OptiyolServiceClient implements OnModuleInit { + private authHeaders: Partial; + constructor( protected readonly logger: PinoLogger, private readonly config: ConfigService, private readonly httpService: HttpService ) { this.logger.setContext(OptiyolServiceClient.name); + } + + onModuleInit() { + const optiyolToken = this.config.get('optiyol.token'); + const optiyolCompany = this.config.get('optiyol.company'); + this.authHeaders = { - Authorization: `token ${this.config.get('optiyol.token')}`, - 'optiyol-company': this.config.get('optiyol.company'), - } as Partial; + Authorization: `token ${optiyolToken}`, + 'optiyol-company': optiyolCompany, + }; } @LogMe() diff --git a/src/notification/services/aws-sns.service.ts b/src/notification/services/aws-sns.service.ts index a7f1e0b..b4cb842 100644 --- a/src/notification/services/aws-sns.service.ts +++ b/src/notification/services/aws-sns.service.ts @@ -1,28 +1,39 @@ -import { Injectable } from '@nestjs/common'; +import { Injectable, OnModuleInit } from '@nestjs/common'; import { ConfigService } from '@nestjs/config'; -import { SNS, Credentials, config } from 'aws-sdk'; +import { config, Credentials, SNS } from 'aws-sdk'; import { PinoLogger } from 'nestjs-pino'; @Injectable() -export class AWSSNSService { +export class AWSSNSService implements OnModuleInit { + private awsRegion: string; + private awsAccessKey: string; + private awsSecretKey: string; + private debugBypassSms: boolean; + constructor( private readonly logger: PinoLogger, private configService: ConfigService - ) { + ) {} + + onModuleInit() { + this.awsRegion = this.configService.get('aws.region'); + this.awsAccessKey = this.configService.get('aws.accessKey'); + this.awsSecretKey = this.configService.get('aws.secretAccessKey'); + this.debugBypassSms = this.configService.get('debug.bypassSms'); + const credentials = new Credentials({ - accessKeyId: process.env.AWS_ACCESS_KEY, - secretAccessKey: process.env.AWS_SECRET_KEY, + accessKeyId: this.awsAccessKey, + secretAccessKey: this.awsSecretKey, }); config.credentials = credentials; // Set the region - config.update({ region: this.configService.get('aws.region') }); + config.update({ region: this.awsRegion }); } async sendSMS(phone: string, body: string): Promise { - if (this.configService.get('debug.bypassSms')) { - return true; - } + if (this.debugBypassSms) return true; + // Create publish parameters const params = { Message: JSON.stringify(body) /* required */, diff --git a/src/user/user.service.ts b/src/user/user.service.ts index 7cda1e5..acce1f3 100644 --- a/src/user/user.service.ts +++ b/src/user/user.service.ts @@ -1,35 +1,38 @@ -import { - LoginResponse, - UserStatuses, - ValidateVerificationCodeResponse, -} from './types'; -import { Injectable } from '@nestjs/common'; -import { LoginUserDto } from './dto/login-user.dto'; -import { PinoLogger } from 'nestjs-pino'; -import { Model, Types } from 'mongoose'; -import { InjectModel } from '@nestjs/mongoose'; -import { JwtService } from '@nestjs/jwt'; +import { Injectable, OnModuleInit } from '@nestjs/common'; import { ConfigService } from '@nestjs/config'; -import { User, UserDocument } from './schemas/user.schema'; -import UserNotFoundException from './exceptions/user-not-found.exception'; -import { LogMe } from '../common/decorators/log.decorator'; +import { JwtService } from '@nestjs/jwt'; +import { InjectModel } from '@nestjs/mongoose'; +import { compare, hash } from 'bcrypt'; +import { Model, Types } from 'mongoose'; +import { PinoLogger } from 'nestjs-pino'; import { AWSSNSService } from 'src/notification/services/aws-sns.service'; -import { AuthSMS } from './schemas/auth.sms.schema'; +import { Organization } from 'src/organization/schemas/organization.schema'; +import { LogMe } from '../common/decorators/log.decorator'; +import { FilterResult } from '../common/types'; +import { CreateUserDto } from './dto/create-user.dto'; +import { FilterUserDto } from './dto/filter-user.dto'; +import { LoginUserDto } from './dto/login-user.dto'; import { ResendVerificationCodeDto } from './dto/resend-verification-code.dto'; -import { VerifyOtpDto } from './dto/verify-otp.dto'; import { UpdateUserDto } from './dto/update-user.dto'; -import { CreateUserDto } from './dto/create-user.dto'; -import { Organization } from 'src/organization/schemas/organization.schema'; -import UserCanNotBeActivatedException from './exceptions/user-can-not-be-activated.exception'; -import PhoneNumberAlreadyExistsException from './exceptions/phone-number-already-exists.exception'; +import { VerifyOtpDto } from './dto/verify-otp.dto'; import InvalidVerificationCodeException from './exceptions/invalid-verification-code.exception'; -import { hash, compare } from 'bcrypt'; -import { FilterUserDto } from './dto/filter-user.dto'; -import { FilterResult } from '../common/types'; +import PhoneNumberAlreadyExistsException from './exceptions/phone-number-already-exists.exception'; +import UserCanNotBeActivatedException from './exceptions/user-can-not-be-activated.exception'; +import UserNotFoundException from './exceptions/user-not-found.exception'; +import { AuthSMS } from './schemas/auth.sms.schema'; +import { User, UserDocument } from './schemas/user.schema'; +import { + LoginResponse, + UserStatuses, + ValidateVerificationCodeResponse, +} from './types'; @Injectable() -export class UserService { +export class UserService implements OnModuleInit { + private bcryptSecret: string; + private bypassCode: string; saltRounds = 10; + constructor( private readonly logger: PinoLogger, private readonly snsService: AWSSNSService, @@ -43,6 +46,11 @@ export class UserService { private readonly organizationModel: Model ) {} + onModuleInit() { + this.bcryptSecret = this.configService.get('bcrypt.secret'); + this.bypassCode = this.configService.get('debug.bypassCode'); + } + @LogMe() async login(loginUserDto: LoginUserDto): Promise { const { phone } = loginUserDto; @@ -82,9 +90,8 @@ export class UserService { message: string; }> { const verificationCode = Math.floor(100000 + Math.random() * 900000); - const bcryptSecret = this.configService.get('bcrypt.secret'); const hashedVerificationCode = await hash( - verificationCode.toString() + bcryptSecret, + verificationCode.toString() + this.bcryptSecret, this.saltRounds ); const messageBody = `Doğrulama kodunuz: ${verificationCode}`; @@ -116,15 +123,12 @@ export class UserService { if (!authSMSDocument) throw new InvalidVerificationCodeException(); const verificationCode = authSMSDocument.verificationCode; - const bypassCode = this.configService.get('debug.bypassCode'); - - const bcryptSecret = this.configService.get('bcrypt.secret'); const isCodeValid = await compare( - enteredCode + bcryptSecret, + enteredCode + this.bcryptSecret, verificationCode ); - if (!isCodeValid && enteredCode !== bypassCode) + if (!isCodeValid && enteredCode !== this.bypassCode) throw new InvalidVerificationCodeException(); let user = await this.userModel.findOne({ phone });