feat: handle notification async using event emitter

This commit is contained in:
Abdalhamid Alhamad
2024-12-29 11:44:51 +03:00
parent 5663a287f9
commit f383f6d14d
13 changed files with 57 additions and 15 deletions

View File

@ -1,5 +1,6 @@
import { Injectable } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { NotificationsService } from '../../notification/services/notifications.service';
import { DEFAULT_OTP_DIGIT, DEFAULT_OTP_LENGTH } from '../constants';
import { OtpType } from '../enums';
import { ISendOtp, IVerifyOtp } from '../interfaces';
@ -8,7 +9,11 @@ import { generateRandomOtp } from '../utils';
@Injectable()
export class OtpService {
constructor(private readonly configService: ConfigService, private readonly otpRepository: OtpRepository) {}
constructor(
private readonly configService: ConfigService,
private readonly otpRepository: OtpRepository,
private readonly notificationService: NotificationsService,
) {}
private useMock = this.configService.get<boolean>('USE_MOCK', false);
async generateAndSendOtp(sendotpRequest: ISendOtp): Promise<string> {
const otp = this.useMock ? DEFAULT_OTP_DIGIT.repeat(DEFAULT_OTP_LENGTH) : generateRandomOtp(DEFAULT_OTP_LENGTH);
@ -28,8 +33,7 @@ export class OtpService {
return !!otp;
}
private sendOtp(sendotpRequest: ISendOtp, otp: string) {
// TODO: send OTP to the user
return;
private sendOtp(sendOtpRequest: ISendOtp, otp: string) {
return this.notificationService.sendOtpNotification(sendOtpRequest, otp);
}
}