feat: onboarding signup journey

This commit is contained in:
Abdalhamid Alhamad
2025-07-27 13:15:54 +03:00
parent bf43e62b17
commit c493bd57e1
12 changed files with 234 additions and 10 deletions
+1 -2
View File
@@ -11,8 +11,7 @@ export class UserRepository {
createUnverifiedUser(data: Partial<User>) {
return this.userRepository.save(
this.userRepository.create({
email: data.email,
roles: data.roles,
...data,
}),
);
}
+61 -4
View File
@@ -7,7 +7,12 @@ import { Transactional } from 'typeorm-transactional';
import { CountryIso } from '~/common/enums';
import { NotificationsService } from '~/common/modules/notification/services';
import { CustomerService } from '~/customer/services';
import { CreateUnverifiedUserRequestDto, VerifyUserRequestDto } from '../../auth/dtos/request';
import {
CreateUnverifiedUserRequestDto,
CreateUnverifiedUserV2RequestDto,
VerifyUserRequestDto,
VerifyUserV2RequestDto,
} from '../../auth/dtos/request';
import { Roles } from '../../auth/enums';
import {
CreateCheckerRequestDto,
@@ -55,8 +60,8 @@ export class UserService {
}
@Transactional()
async verifyUser(userId: string, body: VerifyUserRequestDto) {
this.logger.log(`Verifying user email with id ${userId}`);
async verifyUser(userId: string, body: VerifyUserRequestDto | VerifyUserV2RequestDto) {
this.logger.log(`Verifying user with id ${userId}`);
await Promise.all([
this.customerService.createGuardianCustomer(userId, {
firstName: body.firstName,
@@ -64,7 +69,27 @@ export class UserService {
dateOfBirth: body.dateOfBirth,
countryOfResidence: body.countryOfResidence,
}),
this.userRepository.update(userId, { isEmailVerified: true }),
this.userRepository.update(userId, {
isEmailVerified: true,
}),
]);
}
@Transactional()
async verifyUserV2(userId: string, body: VerifyUserV2RequestDto) {
this.logger.log(`Verifying user 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, {
isPhoneVerified: true,
...(body.email && { email: body.email }),
}),
]);
}
@@ -110,6 +135,38 @@ export class UserService {
return user;
}
async findOrCreateByPhoneNumber(body: CreateUnverifiedUserV2RequestDto) {
this.logger.log(`Finding or creating user with phone number ${body.countryCode + body.phoneNumber}`);
const user = await this.userRepository.findOne({
phoneNumber: body.phoneNumber,
countryCode: body.countryCode,
});
if (!user) {
this.logger.log(`User with phone number ${body.phoneNumber} not found, creating new user`);
return this.userRepository.createUnverifiedUser({
phoneNumber: body.phoneNumber,
countryCode: body.countryCode,
email: body.email,
roles: [Roles.GUARDIAN],
});
}
if (user && user.roles.includes(Roles.GUARDIAN) && user.isPhoneVerified) {
this.logger.error(`User with phone number ${body.phoneNumber} already exists`);
throw new BadRequestException('USER.PHONE_NUMBER_ALREADY_TAKEN');
}
if (user && user.roles.includes(Roles.JUNIOR)) {
this.logger.error(`User with phone number ${body.phoneNumber} 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 ${body.phoneNumber} found successfully`);
return user;
}
async findOrCreateByEmail(email: string) {
this.logger.log(`Finding or creating user with email ${email} `);
const user = await this.userRepository.findOne({ email });