chore: change the real user abd legal user data

This commit is contained in:
mahyargdz
2025-03-02 17:59:02 +03:30
parent c559fdb9e8
commit 0da12b89eb
13 changed files with 203 additions and 187 deletions
+98 -67
View File
@@ -22,6 +22,8 @@ import { WalletsService } from "../../wallets/providers/wallets.service";
import { ChangeEmailDto } from "../DTO/change-email.dto";
import { CheckValidityDTO } from "../DTO/check-validity.dto";
import { CreateAdminDto } from "../DTO/create-admin.dto";
import { CreateLegalUserDto } from "../DTO/create-legal-user.dto";
import { CreateRealUserDto } from "../DTO/create-real-user.dto";
import { CreateRoleDto } from "../DTO/create-role.dto";
import { SearchAdminQueryDto } from "../DTO/search-admins-query.dto";
import { SearchRolesQueryDto } from "../DTO/search-roles.dto";
@@ -30,9 +32,12 @@ import { CreateUserGroupDto } from "../DTO/user-group.dto";
import { EmailVerifyQueryDto } from "../DTO/verify-email-query.dto";
import { Role } from "../entities/role.entity";
import { User } from "../entities/user.entity";
import { FinancialType } from "../enums/financial-type.enum";
import { RoleEnum } from "../enums/role.enum";
import { ValidityType } from "../enums/validity-type.enum";
import { LegalUserRepository } from "../repositories/legal-user.repository";
import { PermissionsRepository } from "../repositories/permissions.repository";
import { RealUserRepository } from "../repositories/real-user.repository";
import { RoleRepository } from "../repositories/roles.repository";
import { UserGroupRepository } from "../repositories/user-group.repository";
import { UserRepository } from "../repositories/users.repository";
@@ -49,6 +54,8 @@ export class UsersService {
private readonly permissionsRepository: PermissionsRepository,
private readonly passwordService: PasswordService,
private readonly addressService: AddressService,
private readonly realUserRepository: RealUserRepository,
private readonly legalUserRepository: LegalUserRepository,
private readonly emailService: EmailService,
private readonly configService: ConfigService,
private readonly otpService: OTPService,
@@ -81,20 +88,22 @@ export class UsersService {
/************************************************************ */
async getMyFinancialInfo(userId: string) {
const user = await this.userRepository
.createQueryBuilder("user")
.leftJoinAndSelect("user.address", "address")
.leftJoinAndSelect("address.city", "city")
.leftJoinAndSelect("city.province", "province")
.leftJoinAndSelect("user.realUser", "realUser")
.leftJoinAndSelect("user.legalUser", "legalUser")
.where("user.id = :id", { id: userId })
.getOne();
if (!user) throw new BadRequestException(UserMessage.USER_NOT_FOUND);
return { user };
async getRealUserData(userId: string) {
const realUser = await this.realUserRepository.findOne({
where: { user: { id: userId } },
relations: { address: { city: { province: true } } },
});
return { realUser };
}
/************************************************************ */
async getLegalUserData(userId: string) {
const legalUser = await this.legalUserRepository.findOne({
where: { user: { id: userId } },
relations: { address: { city: { province: true } } },
});
return { legalUser };
}
/************************************************************ */
async updateProfile(userId: string, updateProfileDto: UpdateProfileDto) {
@@ -487,7 +496,10 @@ export class UsersService {
name: RoleEnum.USER,
},
},
relations: ["legalUser", "realUser"],
relations: {
legalUser: true,
realUser: true,
},
});
return { customer };
@@ -564,75 +576,94 @@ export class UsersService {
};
}
/************************************************************ */
//TODO:query manager
async createRealUserData(createDto: CreateRealUserDto, userId: string) {
const user = await this.userRepository.findOneBy({ id: userId });
if (!user) throw new BadRequestException(UserMessage.USER_NOT_FOUND);
// async createRealUserData(createDto: CreateRealUserDto, userId: string) {
// const userExist = await this.userRepository.findOneBy({
// id: userId,
// });
// if (!userExist) throw new BadRequestException(UserMessage.USER_NOT_FOUND);
// console.log(userExist);
// const existingRealUser = await this.realUserRepository.findOne({
// where: { user: { id: userExist.id } },
// });
// console.log(existingRealUser);
if (user.financialType && user.financialType === FinancialType.LEGAL)
throw new BadRequestException(UserMessage.FINANCIAL_TYPE_IS_LEGAL);
//
const existRealUser = await this.realUserRepository.findOne({ where: { user: { id: user.id } } });
// if (existingRealUser) throw new BadRequestException(RealUserMessage.REAL_DATA_EXIST);
if (existRealUser) throw new BadRequestException(UserMessage.REAL_DATA_EXIST);
// const { city } = await this.addressService.getCity(+createDto.cityId);
const { city } = await this.addressService.getCity(+createDto.cityId);
// const realUser = this.realUserRepository.create({
// ...createDto,
// user: userExist,
// address: {
// address: createDto.address,
// city,
// postalCode: createDto.postalCode,
// user: userExist,
// },
// });
const existNationalCode = await this.realUserRepository.findOne({ where: { nationalCode: createDto.nationalCode } });
if (existNationalCode) throw new BadRequestException(UserMessage.NATIONAL_CODE_EXIST);
// await this.realUserRepository.save(realUser);
const existPhone = await this.realUserRepository.findOne({ where: { phone: createDto.phone } });
if (existPhone) throw new BadRequestException(UserMessage.PHONE_EXIST);
// return {
// message: CommonMessage.CREATED,
// realUser,
// };
// }
const realUser = this.realUserRepository.create({
...createDto,
user,
address: {
fullAddress: createDto.address,
city,
postalCode: createDto.postalCode,
},
});
await this.realUserRepository.save(realUser);
user.financialType = FinancialType.REAL;
await this.userRepository.save(user);
return {
message: CommonMessage.CREATED,
realUser,
};
}
// /************************************************************ */
// async createLegalUserData(createDto: CreateLegalUserDto, userId: string) {
// const userExist = await this.userRepository.findOneBy({
// id: userId,
// });
// if (!userExist) throw new BadRequestException(UserMessage.USER_NOT_FOUND);
//TODO:query manager
async createLegalUserData(createDto: CreateLegalUserDto, userId: string) {
const user = await this.userRepository.findOneBy({ id: userId });
if (!user) throw new BadRequestException(UserMessage.USER_NOT_FOUND);
// const existingLegalUser = await this.legalUserRepository.findOne({
// where: { user: { id: userExist.id } },
// });
if (user.financialType && user.financialType === FinancialType.REAL) throw new BadRequestException(UserMessage.FINANCIAL_TYPE_IS_REAL);
// if (existingLegalUser) throw new BadRequestException(LegalUserMessage.LEGAL_DATA_EXIST);
const existingLegalUser = await this.legalUserRepository.findOne({ where: { user: { id: user.id } } });
// const { city } = await this.addressService.getCity(+createDto.cityId);
if (existingLegalUser) throw new BadRequestException(UserMessage.LEGAL_DATA_EXIST);
// const legalUser = this.legalUserRepository.create({
// ...createDto,
// user: userExist,
// address: {
// address: createDto.address,
// city,
// postalCode: createDto.postalCode,
// user: userExist,
// },
// });
const existEconomicCode = await this.legalUserRepository.findOne({ where: { economicCode: createDto.economicCode } });
if (existEconomicCode) throw new BadRequestException(UserMessage.ECONOMIC_CODE_EXIST);
// await this.legalUserRepository.save(legalUser);
const existPhone = await this.legalUserRepository.findOne({ where: { phone: createDto.phone } });
if (existPhone) throw new BadRequestException(UserMessage.PHONE_EXIST);
// return {
// message: CommonMessage.CREATED,
// legalUser,
// };
// }
const existRegistrationCode = await this.legalUserRepository.findOne({ where: { registrationCode: createDto.registrationCode } });
if (existRegistrationCode) throw new BadRequestException(UserMessage.REGISTRATION_CODE_EXIST);
const existNationalIdentity = await this.legalUserRepository.findOne({ where: { nationalIdentity: createDto.nationalIdentity } });
if (existNationalIdentity) throw new BadRequestException(UserMessage.NATIONAL_IDENTITY_EXIST);
const { city } = await this.addressService.getCity(+createDto.cityId);
const legalUser = this.legalUserRepository.create({
...createDto,
user,
address: {
fullAddress: createDto.address,
city,
postalCode: createDto.postalCode,
},
});
await this.legalUserRepository.save(legalUser);
user.financialType = FinancialType.LEGAL;
await this.userRepository.save(user);
return {
message: CommonMessage.CREATED,
legalUser,
};
}
/************************************************************ */