Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat/185-config #188

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 14 additions & 7 deletions src/dispatch/optiyol.service-client.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -11,21 +12,27 @@ import {
OptiyolDispatchOrderResult,
OptiyolDispatchVehicleResult,
} from './types/optiyol.types';
import { ConfigService } from '@nestjs/config';

@Injectable()
export class OptiyolServiceClient {
private readonly authHeaders: Partial<RawAxiosRequestHeaders>;
export class OptiyolServiceClient implements OnModuleInit {
private authHeaders: Partial<RawAxiosRequestHeaders>;

constructor(
protected readonly logger: PinoLogger,
private readonly config: ConfigService,
private readonly httpService: HttpService
) {
this.logger.setContext(OptiyolServiceClient.name);
}

onModuleInit() {
const optiyolToken = this.config.get<string>('optiyol.token');
const optiyolCompany = this.config.get<string>('optiyol.company');

this.authHeaders = {
Authorization: `token ${this.config.get<string>('optiyol.token')}`,
'optiyol-company': this.config.get<string>('optiyol.company'),
} as Partial<RawAxiosRequestHeaders>;
Authorization: `token ${optiyolToken}`,
'optiyol-company': optiyolCompany,
};
}

@LogMe()
Expand Down
31 changes: 21 additions & 10 deletions src/notification/services/aws-sns.service.ts
Original file line number Diff line number Diff line change
@@ -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<boolean> {
if (this.configService.get<boolean>('debug.bypassSms')) {
return true;
}
if (this.debugBypassSms) return true;

// Create publish parameters
const params = {
Message: JSON.stringify(body) /* required */,
Expand Down
66 changes: 35 additions & 31 deletions src/user/user.service.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -43,6 +46,11 @@ export class UserService {
private readonly organizationModel: Model<Organization>
) {}

onModuleInit() {
this.bcryptSecret = this.configService.get('bcrypt.secret');
this.bypassCode = this.configService.get('debug.bypassCode');
}

@LogMe()
async login(loginUserDto: LoginUserDto): Promise<LoginResponse> {
const { phone } = loginUserDto;
Expand Down Expand Up @@ -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}`;
Expand Down Expand Up @@ -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 });
Expand Down