@@ -7,19 +7,26 @@ import {
7
7
NotFoundException ,
8
8
} from '@nestjs/common' ;
9
9
import { InjectRepository } from '@nestjs/typeorm' ;
10
+ import { InjectQueue } from '@nestjs/bull' ;
10
11
11
12
import { Cache } from 'cache-manager' ;
12
13
import { Repository } from 'typeorm' ;
14
+ import { Queue } from 'bull' ;
15
+
16
+ import { PhoneVerificationService } from '@laze/nestjs-phone-verification' ;
17
+
18
+ import { environment } from '../../environments/environment' ;
13
19
14
20
import { User } from '../users/entities/user.entity' ;
15
21
import { UserVerification } from './entities/user-verification.entity' ;
16
22
23
+ import { OtpType } from './enums/otp-type.enum' ;
24
+
25
+ import { SendOtpInput } from './dto/send-otp.input' ;
17
26
import { VerifyEmailViaOtpInput } from './dto/verify-email-via-otp.input' ;
18
27
import { VerifyPhoneViaOtpInput } from './dto/verify-phone-via-otp.input' ;
19
- import { PhoneVerificationService } from '@laze/nestjs-phone-verification' ;
20
- import { SendOtpInput } from './dto/send-otp.input' ;
21
- import { OtpType } from './enums/otp-type.enum' ;
22
- import { environment } from '../../environments/environment' ;
28
+ import { Email } from '../@common/interfaces/jobs/email.interface' ;
29
+ import { TokenGenerationService } from '../@common/services/token-generation.service' ;
23
30
24
31
@Injectable ( )
25
32
export class VerificationService {
@@ -28,10 +35,12 @@ export class VerificationService {
28
35
constructor (
29
36
@Inject ( CACHE_MANAGER ) private readonly cache : Cache ,
30
37
private readonly phoneVerificationService : PhoneVerificationService ,
38
+ private readonly tokenGenerationService : TokenGenerationService ,
31
39
@InjectRepository ( UserVerification )
32
40
private readonly userVerificationRepository : Repository < UserVerification > ,
33
41
@InjectRepository ( User )
34
- private readonly userRepository : Repository < User >
42
+ private readonly userRepository : Repository < User > ,
43
+ @InjectQueue ( 'email' ) private readonly emailQueue : Queue
35
44
) {
36
45
this . logger = new Logger ( VerificationService . name ) ;
37
46
}
@@ -137,6 +146,43 @@ export class VerificationService {
137
146
} ) ;
138
147
}
139
148
149
+ async sendOtpForEmailVerification ( sendOtpInput : SendOtpInput , user : User ) {
150
+ try {
151
+ const { type } = sendOtpInput ;
152
+
153
+ const otp = '' + this . tokenGenerationService . generateOtp ( 6 ) ;
154
+ await this . cache . set ( `users.${ user . id } .otp.${ type } ` , otp , {
155
+ ttl : 600 ,
156
+ } ) ;
157
+
158
+ const fullUser = await this . userRepository . findOneOrFail ( {
159
+ where : {
160
+ id : user . id ,
161
+ } ,
162
+ relations : [ 'profile' , 'verification' ] ,
163
+ } ) ;
164
+
165
+ const job : Email = {
166
+ receiver : user . email ,
167
+ subject : `${ environment . app . name } Email Verification` ,
168
+ template : './auth/email-otp' ,
169
+ data : {
170
+ name : `${ fullUser . profile . name . first } ${ fullUser . profile . name . last } ` ,
171
+ otp,
172
+ } ,
173
+ } ;
174
+
175
+ await this . emailQueue . add ( job ) ;
176
+
177
+ return fullUser ;
178
+ } catch ( e ) {
179
+ this . logger . error ( e ) ;
180
+ throw new BadRequestException (
181
+ 'Failed to send OTP code. Please try again later!'
182
+ ) ;
183
+ }
184
+ }
185
+
140
186
async findUserVerificationById ( id : number ) {
141
187
try {
142
188
return await this . userVerificationRepository . findOneOrFail ( {
0 commit comments