Skip to content

Commit 6eca0e3

Browse files
committed
Add sending otp to validate user email
1 parent 3acce30 commit 6eca0e3

File tree

2 files changed

+63
-5
lines changed

2 files changed

+63
-5
lines changed

apps/main-service/src/app/verification/verification.resolver.ts

+12
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,16 @@ export class VerificationResolver {
5353
user
5454
);
5555
}
56+
57+
@Mutation(() => User, { name: 'sendOtpForEmailVerification' })
58+
sendOtpForEmailVerification(
59+
@CurrentUser() user: User,
60+
@Args('sendOtpInput')
61+
sendOtpInput: SendOtpInput
62+
) {
63+
return this.verificationService.sendOtpForEmailVerification(
64+
sendOtpInput,
65+
user
66+
);
67+
}
5668
}

apps/main-service/src/app/verification/verification.service.ts

+51-5
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,26 @@ import {
77
NotFoundException,
88
} from '@nestjs/common';
99
import { InjectRepository } from '@nestjs/typeorm';
10+
import { InjectQueue } from '@nestjs/bull';
1011

1112
import { Cache } from 'cache-manager';
1213
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';
1319

1420
import { User } from '../users/entities/user.entity';
1521
import { UserVerification } from './entities/user-verification.entity';
1622

23+
import { OtpType } from './enums/otp-type.enum';
24+
25+
import { SendOtpInput } from './dto/send-otp.input';
1726
import { VerifyEmailViaOtpInput } from './dto/verify-email-via-otp.input';
1827
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';
2330

2431
@Injectable()
2532
export class VerificationService {
@@ -28,10 +35,12 @@ export class VerificationService {
2835
constructor(
2936
@Inject(CACHE_MANAGER) private readonly cache: Cache,
3037
private readonly phoneVerificationService: PhoneVerificationService,
38+
private readonly tokenGenerationService: TokenGenerationService,
3139
@InjectRepository(UserVerification)
3240
private readonly userVerificationRepository: Repository<UserVerification>,
3341
@InjectRepository(User)
34-
private readonly userRepository: Repository<User>
42+
private readonly userRepository: Repository<User>,
43+
@InjectQueue('email') private readonly emailQueue: Queue
3544
) {
3645
this.logger = new Logger(VerificationService.name);
3746
}
@@ -137,6 +146,43 @@ export class VerificationService {
137146
});
138147
}
139148

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+
140186
async findUserVerificationById(id: number) {
141187
try {
142188
return await this.userVerificationRepository.findOneOrFail({

0 commit comments

Comments
 (0)