fix: bug in thre login process

This commit is contained in:
mahyargdz
2025-03-02 16:28:07 +03:30
parent 4d0344c376
commit 28486d64ee
21 changed files with 606 additions and 588 deletions
+126 -142
View File
@@ -6,15 +6,7 @@ import { FastifyReply } from "fastify";
import slugify from "slugify";
import { In, Not, QueryRunner } from "typeorm";
import {
AdminMessage,
AdsMessage,
AuthMessage,
CommonMessage,
LegalUserMessage,
RealUserMessage,
UserMessage,
} from "../../../common/enums/message.enum";
import { AdminMessage, AdsMessage, AuthMessage, CommonMessage, UserMessage } from "../../../common/enums/message.enum";
import { AddressService } from "../../address/providers/address.service";
import { CompleteRegistrationDto } from "../../auth/DTO/complete-register.dto";
import { RequestOtpDto } from "../../auth/DTO/request-otp.dto";
@@ -30,12 +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 { CreateCustomerDto } from "../DTO/create-user.dto";
import { SearchAdminQueryDto } from "../DTO/search-admins-query.dto";
import { SearchCustomersDto } from "../DTO/search-customers.dto";
import { SearchRolesQueryDto } from "../DTO/search-roles.dto";
import { UpdateProfileDto } from "../DTO/update-profile.dto";
import { CreateUserGroupDto } from "../DTO/user-group.dto";
@@ -44,9 +32,7 @@ import { Role } from "../entities/role.entity";
import { User } from "../entities/user.entity";
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";
@@ -62,8 +48,6 @@ export class UsersService {
private readonly rolesRepository: RoleRepository,
private readonly permissionsRepository: PermissionsRepository,
private readonly passwordService: PasswordService,
private readonly realUserRepository: RealUserRepository,
private readonly legalUserRepository: LegalUserRepository,
private readonly addressService: AddressService,
private readonly emailService: EmailService,
private readonly configService: ConfigService,
@@ -90,7 +74,7 @@ export class UsersService {
/************************************************************ */
async getMe(userId: string) {
const user = await this.userRepository.findOne({ where: { id: userId }, relations: { roles: true, city: { province: true } } });
const user = await this.userRepository.findOne({ where: { id: userId }, relations: { roles: true } });
if (!user) throw new BadRequestException(UserMessage.USER_NOT_FOUND);
return { user };
}
@@ -405,93 +389,93 @@ export class UsersService {
/************************************************************ */
async findAllCustomers(queryDto: SearchCustomersDto) {
const { limit, skip } = PaginationUtils(queryDto);
// async findAllCustomers(queryDto: SearchCustomersDto) {
// const { limit, skip } = PaginationUtils(queryDto);
const queryBuilder = this.userRepository
.createQueryBuilder("user")
.leftJoin("user.roles", "role")
.where("role.name = :roleName", { roleName: RoleEnum.USER })
.leftJoin("user.groups", "groups")
.addSelect(["groups.id", "groups.name"])
.loadRelationCountAndMap("user.invoicesCount", "user.invoices")
.loadRelationCountAndMap("user.subscriptionsCount", "user.subscriptions")
.loadRelationCountAndMap("user.ticketsCount", "user.tickets");
// const queryBuilder = this.userRepository
// .createQueryBuilder("user")
// .leftJoin("user.roles", "role")
// .where("role.name = :roleName", { roleName: RoleEnum.USER })
// .leftJoin("user.groups", "groups")
// .addSelect(["groups.id", "groups.name"])
// .loadRelationCountAndMap("user.invoicesCount", "user.invoices")
// .loadRelationCountAndMap("user.subscriptionsCount", "user.subscriptions")
// .loadRelationCountAndMap("user.ticketsCount", "user.tickets");
if (queryDto.q) {
queryBuilder
.orWhere("user.firstName ILIKE :search", { search: `%${queryDto.q}%` })
.orWhere("user.lastName ILIKE :search", { search: `%${queryDto.q}%` })
.orWhere("user.userName ILIKE :search", { search: `%${queryDto.q}%` });
}
// if (queryDto.q) {
// queryBuilder
// .orWhere("user.firstName ILIKE :search", { search: `%${queryDto.q}%` })
// .orWhere("user.lastName ILIKE :search", { search: `%${queryDto.q}%` })
// .orWhere("user.userName ILIKE :search", { search: `%${queryDto.q}%` });
// }
const [customers, count] = await queryBuilder.skip(skip).take(limit).getManyAndCount();
// const [customers, count] = await queryBuilder.skip(skip).take(limit).getManyAndCount();
return { customers, count };
}
// return { customers, count };
// }
/************************************************************ */
async createCustomer(createDto: CreateCustomerDto) {
const { cityId, address, postalCode, economicCode, companyRegisteredName, registrationId, nationalId, number, ...userData } = createDto;
// async createCustomer(createDto: CreateCustomerDto) {
// const { cityId, address, postalCode, economicCode, companyRegisteredName, registrationId, nationalId, number, ...userData } = createDto;
const queryRunner = this.userRepository.manager.connection.createQueryRunner();
await queryRunner.connect();
await queryRunner.startTransaction();
// const queryRunner = this.userRepository.manager.connection.createQueryRunner();
// await queryRunner.connect();
// await queryRunner.startTransaction();
try {
const existUser = await queryRunner.manager.findOneBy(User, { email: createDto.email });
if (existUser) throw new BadRequestException(UserMessage.EMAIL_EXIST);
// try {
// const existUser = await queryRunner.manager.findOneBy(User, { email: createDto.email });
// if (existUser) throw new BadRequestException(UserMessage.EMAIL_EXIST);
const role = await queryRunner.manager.findOneBy(Role, { name: RoleEnum.USER });
if (!role) throw new BadRequestException(UserMessage.ROLE_NOT_FOUND);
// const role = await queryRunner.manager.findOneBy(Role, { name: RoleEnum.USER });
// if (!role) throw new BadRequestException(UserMessage.ROLE_NOT_FOUND);
const hashedPassword = await this.passwordService.hashPassword(createDto.password);
// const hashedPassword = await this.passwordService.hashPassword(createDto.password);
const legalUserData = economicCode
? {
economicCode,
companyRegisteredName,
registrationId,
nationalId,
number,
address: {
address,
city: cityId
? {
id: +cityId,
}
: undefined,
postalCode,
},
}
: undefined;
// const legalUserData = economicCode
// ? {
// economicCode,
// companyRegisteredName,
// registrationId,
// nationalId,
// number,
// address: {
// address,
// city: cityId
// ? {
// id: +cityId,
// }
// : undefined,
// postalCode,
// },
// }
// : undefined;
const user = queryRunner.manager.create(User, {
...userData,
password: hashedPassword,
roles: [role],
legalUser: legalUserData,
});
await queryRunner.manager.save(user);
// const user = queryRunner.manager.create(User, {
// ...userData,
// password: hashedPassword,
// roles: [role],
// legalUser: legalUserData,
// });
// await queryRunner.manager.save(user);
await this.userSettingsService.createUserSettings(user.id, queryRunner);
// await this.userSettingsService.createUserSettings(user.id, queryRunner);
await this.walletsService.createUserWallet(user.id, queryRunner);
// await this.walletsService.createUserWallet(user.id, queryRunner);
await queryRunner.commitTransaction();
// await queryRunner.commitTransaction();
return {
message: CommonMessage.CREATED,
user,
};
} catch (error) {
await queryRunner.rollbackTransaction();
throw error;
} finally {
await queryRunner.release();
}
}
// return {
// message: CommonMessage.CREATED,
// user,
// };
// } catch (error) {
// await queryRunner.rollbackTransaction();
// throw error;
// } finally {
// await queryRunner.release();
// }
// }
/************************************************************ */
@@ -581,74 +565,74 @@ export class UsersService {
}
/************************************************************ */
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);
// 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 (existingRealUser) throw new BadRequestException(RealUserMessage.REAL_DATA_EXIST);
// if (existingRealUser) throw new BadRequestException(RealUserMessage.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 realUser = this.realUserRepository.create({
// ...createDto,
// user: userExist,
// address: {
// address: createDto.address,
// city,
// postalCode: createDto.postalCode,
// user: userExist,
// },
// });
await this.realUserRepository.save(realUser);
// await this.realUserRepository.save(realUser);
return {
message: CommonMessage.CREATED,
realUser,
};
}
// 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);
// async createLegalUserData(createDto: CreateLegalUserDto, userId: string) {
// const userExist = await this.userRepository.findOneBy({
// id: userId,
// });
// if (!userExist) throw new BadRequestException(UserMessage.USER_NOT_FOUND);
const existingLegalUser = await this.legalUserRepository.findOne({
where: { user: { id: userExist.id } },
});
// const existingLegalUser = await this.legalUserRepository.findOne({
// where: { user: { id: userExist.id } },
// });
if (existingLegalUser) throw new BadRequestException(LegalUserMessage.LEGAL_DATA_EXIST);
// if (existingLegalUser) throw new BadRequestException(LegalUserMessage.LEGAL_DATA_EXIST);
const { city } = await this.addressService.getCity(+createDto.cityId);
// const { city } = await this.addressService.getCity(+createDto.cityId);
const legalUser = this.legalUserRepository.create({
...createDto,
user: userExist,
address: {
address: createDto.address,
city,
postalCode: createDto.postalCode,
user: userExist,
},
});
// const legalUser = this.legalUserRepository.create({
// ...createDto,
// user: userExist,
// address: {
// address: createDto.address,
// city,
// postalCode: createDto.postalCode,
// user: userExist,
// },
// });
await this.legalUserRepository.save(legalUser);
// await this.legalUserRepository.save(legalUser);
return {
message: CommonMessage.CREATED,
legalUser,
};
}
// return {
// message: CommonMessage.CREATED,
// legalUser,
// };
// }
/************************************************************ */