mirror of
https://github.com/HamzaSha1/zod-backend.git
synced 2026-10-26 01:17:56 +00:00
refactor: enhance customer creation code
This commit is contained in:
@@ -1,9 +1,7 @@
|
||||
import { BadRequestException, forwardRef, Inject, Injectable, Logger } from '@nestjs/common';
|
||||
import { FindOptionsWhere } from 'typeorm';
|
||||
import { Transactional } from 'typeorm-transactional';
|
||||
import { CustomerNotificationSettings } from '~/customer/entities/customer-notification-settings.entity';
|
||||
import { CustomerService } from '~/customer/services';
|
||||
import { Guardian } from '~/guardian/entities/guradian.entity';
|
||||
import { CreateUnverifiedUserRequestDto } from '../../auth/dtos/request';
|
||||
import { Roles } from '../../auth/enums';
|
||||
import { User } from '../entities';
|
||||
@@ -43,7 +41,7 @@ export class UserService {
|
||||
this.logger.log(`User with phone number ${phoneNumber} not found, creating new user`);
|
||||
return this.userRepository.createUnverifiedUser({ phoneNumber, countryCode, roles: [Roles.GUARDIAN] });
|
||||
}
|
||||
if (user && user.roles.includes(Roles.GUARDIAN) && user.isPasswordSet) {
|
||||
if (user && user.roles.includes(Roles.GUARDIAN) && user.isProfileCompleted) {
|
||||
this.logger.error(`User with phone number ${phoneNumber} already exists`);
|
||||
throw new BadRequestException('USERS.PHONE_NUMBER_ALREADY_EXISTS');
|
||||
}
|
||||
@@ -91,30 +89,18 @@ export class UserService {
|
||||
this.logger.log(`Creating google user with googleId ${googleId} and email ${email}`);
|
||||
const user = await this.userRepository.createUser({ googleId, email, roles: [Roles.GUARDIAN] });
|
||||
|
||||
await this.customerService.createCustomer(
|
||||
{
|
||||
guardian: Guardian.create({ id: user.id }),
|
||||
notificationSettings: new CustomerNotificationSettings(),
|
||||
},
|
||||
user,
|
||||
);
|
||||
await this.customerService.createGuardianCustomer(user.id);
|
||||
|
||||
return this.findUserOrThrow({ id: user.id });
|
||||
}
|
||||
|
||||
@Transactional()
|
||||
async verifyUserAndCreateCustomer(user: User) {
|
||||
this.logger.log(`Verifying user ${user.id} and creating customer`);
|
||||
await this.userRepository.update(user.id, { isPhoneVerified: true });
|
||||
await this.customerService.createCustomer(
|
||||
{
|
||||
guardian: Guardian.create({ id: user.id }),
|
||||
notificationSettings: new CustomerNotificationSettings(),
|
||||
},
|
||||
user,
|
||||
);
|
||||
async verifyUserAndCreateCustomer(userId: string) {
|
||||
this.logger.log(`Verifying user ${userId} and creating customer`);
|
||||
await this.userRepository.update(userId, { isPhoneVerified: true });
|
||||
await this.customerService.createGuardianCustomer(userId);
|
||||
|
||||
this.logger.log(`User ${user.id} verified and customer created successfully`);
|
||||
return this.findUserOrThrow({ id: user.id });
|
||||
this.logger.log(`User ${userId} verified and customer created successfully`);
|
||||
return this.findUserOrThrow({ id: userId });
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user