feat: handle new registration flow

This commit is contained in:
Abdalhamid Alhamad
2025-05-19 17:00:32 +03:00
parent 35ab3df7c1
commit 49326e983f
13 changed files with 140 additions and 132 deletions
+26 -14
View File
@@ -5,7 +5,8 @@ import moment from 'moment';
import { FindOptionsWhere } from 'typeorm';
import { Transactional } from 'typeorm-transactional';
import { NotificationsService } from '~/common/modules/notification/services';
import { CreateUnverifiedUserRequestDto } from '../../auth/dtos/request';
import { CustomerService } from '~/customer/services';
import { CreateUnverifiedUserRequestDto, VerifyUserRequestDto } from '../../auth/dtos/request';
import { Roles } from '../../auth/enums';
import {
CreateCheckerRequestDto,
@@ -29,6 +30,7 @@ export class UserService {
private readonly deviceService: DeviceService,
private readonly userTokenService: UserTokenService,
private readonly configService: ConfigService,
private customerService: CustomerService,
) {}
findUser(where: FindOptionsWhere<User> | FindOptionsWhere<User>[]) {
@@ -51,9 +53,18 @@ export class UserService {
return this.userRepository.update(userId, { phoneNumber, countryCode });
}
verifyPhoneNumber(userId: string) {
this.logger.log(`Verifying phone number for user ${userId}`);
return this.userRepository.update(userId, { isPhoneVerified: true });
@Transactional()
async verifyUser(userId: string, body: VerifyUserRequestDto) {
this.logger.log(`Verifying user email with id ${userId}`);
await Promise.all([
this.customerService.createGuardianCustomer(userId, {
firstName: body.firstName,
lastName: body.lastName,
dateOfBirth: body.dateOfBirth,
countryOfResidence: body.countryOfResidence,
}),
this.userRepository.update(userId, { isEmailVerified: true }),
]);
}
findUsers(filters: UserFiltersRequestDto) {
@@ -74,26 +85,27 @@ export class UserService {
return user;
}
async findOrCreateUser({ phoneNumber, countryCode }: CreateUnverifiedUserRequestDto) {
this.logger.log(`Finding or creating user with phone number ${phoneNumber} and country code ${countryCode}`);
const user = await this.userRepository.findOne({ phoneNumber });
@Transactional()
async findOrCreateUser(body: CreateUnverifiedUserRequestDto) {
this.logger.log(`Finding or creating user with email ${body.email}`);
const user = await this.userRepository.findOne({ email: body.email });
if (!user) {
this.logger.log(`User with phone number ${phoneNumber} not found, creating new user`);
return this.userRepository.createUnverifiedUser({ phoneNumber, countryCode, roles: [Roles.GUARDIAN] });
this.logger.log(`User with email ${body.email} not found, creating new user`);
return this.userRepository.createUnverifiedUser({ email: body.email, roles: [Roles.GUARDIAN] });
}
if (user && user.roles.includes(Roles.GUARDIAN) && user.isProfileCompleted) {
this.logger.error(`User with phone number ${phoneNumber} already exists`);
throw new BadRequestException('USER.PHONE_NUMBER_ALREADY_EXISTS');
if (user && user.roles.includes(Roles.GUARDIAN) && user.isEmailVerified) {
this.logger.error(`User with email ${body.email} already exists`);
throw new BadRequestException('USER.EMAIL_ALREADY_TAKEN');
}
if (user && user.roles.includes(Roles.JUNIOR)) {
this.logger.error(`User with phone number ${phoneNumber} is an already registered junior`);
this.logger.error(`User with email ${body.email} is an already registered junior`);
throw new BadRequestException('USER.JUNIOR_UPGRADE_NOT_SUPPORTED_YET');
//TODO add role Guardian to the existing user and send OTP
}
this.logger.log(`User with phone number ${phoneNumber} and country code ${countryCode} found successfully`);
this.logger.log(`User with email ${body.email}`);
return user;
}