mirror of
https://github.com/HamzaSha1/zod-backend.git
synced 2025-08-25 21:59:40 +00:00
refactor: enhance customer creation code
This commit is contained in:
@ -1,15 +1,10 @@
|
||||
import React, { useState } from 'react';
|
||||
import {
|
||||
Box,
|
||||
Button,
|
||||
CircularProgress,
|
||||
Alert
|
||||
} from '@mui/material';
|
||||
import { CloudUpload as CloudUploadIcon } from '@mui/icons-material';
|
||||
import { documentApi } from '../../api/client';
|
||||
import { DocumentType } from '../../types/document';
|
||||
import { ApiError } from '../../types/api';
|
||||
import { Alert, Box, Button, CircularProgress } from '@mui/material';
|
||||
import { AxiosError } from 'axios';
|
||||
import React, { useState } from 'react';
|
||||
import { documentApi } from '../../api/client';
|
||||
import { ApiError } from '../../types/api';
|
||||
import { DocumentType } from '../../types/document';
|
||||
|
||||
interface DocumentUploadProps {
|
||||
onUploadSuccess: (documentId: string) => void;
|
||||
@ -41,7 +36,7 @@ export const DocumentUpload = ({ onUploadSuccess, documentType, label }: Documen
|
||||
if (err instanceof AxiosError && err.response?.data) {
|
||||
const apiError = err.response.data as ApiError;
|
||||
const messages = Array.isArray(apiError.message)
|
||||
? apiError.message.map(m => `${m.field}: ${m.message}`).join('\n')
|
||||
? apiError.message.map((m) => `${m.field}: ${m.message}`).join('\n')
|
||||
: apiError.message;
|
||||
setError(messages);
|
||||
} else {
|
||||
@ -52,17 +47,18 @@ export const DocumentUpload = ({ onUploadSuccess, documentType, label }: Documen
|
||||
}
|
||||
};
|
||||
|
||||
const now = new Date();
|
||||
return (
|
||||
<Box>
|
||||
<input
|
||||
accept="image/*,.pdf"
|
||||
style={{ display: 'none' }}
|
||||
id={`upload-${documentType}`}
|
||||
id={`upload-${documentType}-${now.getTime()}`}
|
||||
type="file"
|
||||
onChange={handleFileChange}
|
||||
disabled={loading}
|
||||
/>
|
||||
<label htmlFor={`upload-${documentType}`}>
|
||||
<label htmlFor={`upload-${documentType}-${now.getTime()}`}>
|
||||
<Button
|
||||
variant="outlined"
|
||||
component="span"
|
||||
|
@ -61,7 +61,7 @@ export class AuthService {
|
||||
this.logger.log(`Verifying user with phone number ${verifyUserDto.countryCode + verifyUserDto.phoneNumber}`);
|
||||
const user = await this.userService.findUserOrThrow({ phoneNumber: verifyUserDto.phoneNumber });
|
||||
|
||||
if (user.isPasswordSet) {
|
||||
if (user.isProfileCompleted) {
|
||||
this.logger.error(
|
||||
`User with phone number ${verifyUserDto.countryCode + verifyUserDto.phoneNumber} already verified`,
|
||||
);
|
||||
@ -82,7 +82,18 @@ export class AuthService {
|
||||
throw new BadRequestException('USERS.INVALID_OTP');
|
||||
}
|
||||
|
||||
const updatedUser = await this.userService.verifyUserAndCreateCustomer(user);
|
||||
if (user.isPhoneVerified) {
|
||||
this.logger.log(
|
||||
`User with phone number ${
|
||||
verifyUserDto.countryCode + verifyUserDto.phoneNumber
|
||||
} already verified but did not complete registration process`,
|
||||
);
|
||||
|
||||
const tokens = await this.generateAuthToken(user);
|
||||
return [tokens, user];
|
||||
}
|
||||
|
||||
const updatedUser = await this.userService.verifyUserAndCreateCustomer(user.id);
|
||||
|
||||
const tokens = await this.generateAuthToken(updatedUser);
|
||||
this.logger.log(
|
||||
|
@ -1,8 +1,9 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { InjectRepository } from '@nestjs/typeorm';
|
||||
import { FindOptionsWhere, Repository } from 'typeorm';
|
||||
import { Roles } from '~/auth/enums';
|
||||
import { User } from '~/user/entities';
|
||||
import { Guardian } from '~/guardian/entities/guradian.entity';
|
||||
import { CreateJuniorRequestDto } from '~/junior/dtos/request';
|
||||
import { Junior } from '~/junior/entities';
|
||||
import { UpdateNotificationsSettingsRequestDto } from '../dtos/request';
|
||||
import { Customer } from '../entities';
|
||||
import { CustomerNotificationSettings } from '../entities/customer-notification-settings.entity';
|
||||
@ -19,14 +20,35 @@ export class CustomerRepository {
|
||||
return this.customerRepository.findOne({ where, relations: ['profilePicture'] });
|
||||
}
|
||||
|
||||
createCustomer(customerData: Partial<Customer>, user: User) {
|
||||
createJuniorCustomer(guardianId: string, userId: string, body: CreateJuniorRequestDto) {
|
||||
return this.customerRepository.save(
|
||||
this.customerRepository.create({
|
||||
...customerData,
|
||||
id: user.id,
|
||||
user,
|
||||
isGuardian: user.roles.includes(Roles.GUARDIAN),
|
||||
isJunior: user.roles.includes(Roles.JUNIOR),
|
||||
firstName: body.firstName,
|
||||
lastName: body.lastName,
|
||||
dateOfBirth: body.dateOfBirth,
|
||||
junior: Junior.create({
|
||||
id: userId,
|
||||
guardianId,
|
||||
relationship: body.relationship,
|
||||
civilIdFrontId: body.civilIdFrontId,
|
||||
civilIdBackId: body.civilIdBackId,
|
||||
}),
|
||||
id: userId,
|
||||
userId,
|
||||
isJunior: true,
|
||||
notificationSettings: new CustomerNotificationSettings(),
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
createGuardianCustomer(userId: string) {
|
||||
return this.customerRepository.save(
|
||||
this.customerRepository.create({
|
||||
notificationSettings: new CustomerNotificationSettings(),
|
||||
guardian: Guardian.create({ id: userId }),
|
||||
id: userId,
|
||||
userId,
|
||||
isGuardian: true,
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
@ -1,6 +1,6 @@
|
||||
import { BadRequestException, Injectable, Logger } from '@nestjs/common';
|
||||
import { DocumentService, OciService } from '~/document/services';
|
||||
import { User } from '~/user/entities';
|
||||
import { CreateJuniorRequestDto } from '~/junior/dtos/request';
|
||||
import { DeviceService } from '~/user/services';
|
||||
import { UpdateCustomerRequestDto, UpdateNotificationsSettingsRequestDto } from '../dtos/request';
|
||||
import { Customer } from '../entities';
|
||||
@ -42,9 +42,14 @@ export class CustomerService {
|
||||
return this.findCustomerById(userId);
|
||||
}
|
||||
|
||||
createCustomer(customerData: Partial<Customer>, user: User) {
|
||||
this.logger.log(`Creating customer for user ${user.id}`);
|
||||
return this.customerRepository.createCustomer(customerData, user);
|
||||
createJuniorCustomer(guardianId: string, juniorId: string, body: CreateJuniorRequestDto) {
|
||||
this.logger.log(`Creating customer for user ${juniorId}`);
|
||||
return this.customerRepository.createJuniorCustomer(guardianId, juniorId, body);
|
||||
}
|
||||
|
||||
createGuardianCustomer(userId: string) {
|
||||
this.logger.log(`Creating guardian customer for user ${userId}`);
|
||||
return this.customerRepository.createGuardianCustomer(userId);
|
||||
}
|
||||
|
||||
async findCustomerById(id: string) {
|
||||
|
@ -2,7 +2,6 @@ import { BadRequestException, Injectable, Logger } from '@nestjs/common';
|
||||
import { Transactional } from 'typeorm-transactional';
|
||||
import { Roles } from '~/auth/enums';
|
||||
import { PageOptionsRequestDto } from '~/core/dtos';
|
||||
import { CustomerNotificationSettings } from '~/customer/entities/customer-notification-settings.entity';
|
||||
import { CustomerService } from '~/customer/services';
|
||||
import { DocumentService, OciService } from '~/document/services';
|
||||
import { UserService } from '~/user/services';
|
||||
@ -42,22 +41,7 @@ export class JuniorService {
|
||||
roles: [Roles.JUNIOR],
|
||||
});
|
||||
|
||||
await this.customerService.createCustomer(
|
||||
{
|
||||
firstName: body.firstName,
|
||||
lastName: body.lastName,
|
||||
dateOfBirth: body.dateOfBirth,
|
||||
junior: Junior.create({
|
||||
id: user.id,
|
||||
guardianId,
|
||||
relationship: body.relationship,
|
||||
civilIdFrontId: body.civilIdFrontId,
|
||||
civilIdBackId: body.civilIdBackId,
|
||||
}),
|
||||
notificationSettings: new CustomerNotificationSettings(),
|
||||
},
|
||||
user,
|
||||
);
|
||||
await this.customerService.createJuniorCustomer(guardianId, user.id, body);
|
||||
|
||||
this.logger.log(`Junior ${user.id} created successfully`);
|
||||
|
||||
|
@ -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