chore: add company request

This commit is contained in:
Mahyargdz
2025-05-19 14:22:41 +03:30
parent 7d0abbeac0
commit f755a3343a
27 changed files with 480 additions and 105 deletions
+17 -17
View File
@@ -3,6 +3,7 @@ import { BadRequestException, Injectable } from "@nestjs/common";
import { TokensService } from "./tokens.service";
import { AuthMessage, UserMessage } from "../../../common/enums/message.enum";
import { Business } from "../../businesses/entities/business.entity";
import { NotificationQueue } from "../../notifications/queue/notification.queue";
import { Role } from "../../users/entities/role.entity";
import { RoleEnum } from "../../users/enums/role.enum";
@@ -15,7 +16,6 @@ import { CompleteRegistrationDto } from "../DTO/complete-register.dto";
import { CheckUserExistDto, LoginPasswordDTO } from "../DTO/loginPassword.dto";
import { RequestOtpDto } from "../DTO/request-otp.dto";
import { VerifyOtpDto } from "../DTO/verify-otp.dto";
@Injectable()
export class AuthService {
constructor(
@@ -29,9 +29,9 @@ export class AuthService {
) {}
//****************** */
//****************** */
async initiateRegistration(requestOtpDto: RequestOtpDto) {
async initiateRegistration(requestOtpDto: RequestOtpDto, businessId: string) {
const { phone } = requestOtpDto;
const existUser = await this.usersService.findOneWithPhone(phone);
const existUser = await this.usersService.findOneWithPhone(phone, businessId);
if (existUser) throw new BadRequestException(AuthMessage.PHONE_EXISTS);
const existCode = await this.otpService.checkExistOtp(phone, "REGISTER");
@@ -52,7 +52,7 @@ export class AuthService {
}
//****************** */
//****************** */
async completeRegistration(completeRegistrationDto: CompleteRegistrationDto) {
async completeRegistration(completeRegistrationDto: CompleteRegistrationDto, business: Business) {
const { phone, code } = completeRegistrationDto;
const entityManager = this.em.fork();
@@ -67,7 +67,7 @@ export class AuthService {
await this.otpService.delOtpFormCache(phone, "REGISTER");
const hashedPassword = await this.passwordService.hashPassword(completeRegistrationDto.password);
const user = await this.usersService.createUser(completeRegistrationDto, hashedPassword, entityManager);
const user = await this.usersService.createUser(completeRegistrationDto, hashedPassword, business, entityManager);
const tokens = await this.tokensService.generateTokens(user, entityManager);
@@ -84,10 +84,10 @@ export class AuthService {
}
//****************** */
//****************** */
async loginWithPassword(loginDto: LoginPasswordDTO) {
async loginWithPassword(loginDto: LoginPasswordDTO, businessId: string) {
const { email, password } = loginDto;
const user = await this.checkUserLoginCredentialWithEmail(email, password);
const user = await this.checkUserLoginCredentialWithEmail(email, password, businessId);
if (this.checkUserIsAdmin(user.role)) throw new BadRequestException(AuthMessage.ADMIN_CAN_NOT_LOGIN);
@@ -104,8 +104,8 @@ export class AuthService {
//****************** */
//****************** */
async checkUserExist(checkUserExistDto: CheckUserExistDto) {
const user = await this.usersService.findOneWithEmail(checkUserExistDto.email);
async checkUserExist(checkUserExistDto: CheckUserExistDto, businessId: string) {
const user = await this.usersService.findOneWithEmail(checkUserExistDto.email, businessId);
if (!user) throw new BadRequestException(UserMessage.USER_NOT_FOUND);
return { message: UserMessage.USER_EXISTS };
}
@@ -113,9 +113,9 @@ export class AuthService {
//****************** */
//****************** */
async requestLoginOtp(requestOtpDto: RequestOtpDto, isAdmin: boolean = false) {
async requestLoginOtp(requestOtpDto: RequestOtpDto, businessId: string, isAdmin: boolean = false) {
const { phone } = requestOtpDto;
const user = await this.usersService.findOneWithPhone(phone);
const user = await this.usersService.findOneWithPhone(phone, businessId);
if (!user) throw new BadRequestException(AuthMessage.PHONE_NOT_FOUND);
//check the if the method call is from admin or not
@@ -144,10 +144,10 @@ export class AuthService {
//****************** */
//****************** */
async verifyLoginOtp(verifyOtpDto: VerifyOtpDto) {
async verifyLoginOtp(verifyOtpDto: VerifyOtpDto, businessId: string) {
const { code, phone } = verifyOtpDto;
const user = await this.checkUserLoginCredentialWithPhone(phone, code);
const user = await this.checkUserLoginCredentialWithPhone(phone, code, businessId);
if (this.checkUserIsAdmin(user.role)) throw new BadRequestException(AuthMessage.ADMIN_CAN_NOT_LOGIN);
@@ -193,8 +193,8 @@ export class AuthService {
//****************** */
private async checkUserLoginCredentialWithEmail(email: string, password: string) {
const user = await this.usersService.findOneWithEmail(email);
private async checkUserLoginCredentialWithEmail(email: string, password: string, businessId: string) {
const user = await this.usersService.findOneWithEmail(email, businessId);
if (!user) throw new BadRequestException(AuthMessage.INVALID_PASSWORD);
const passCompare = await this.passwordService.comparePassword(password, user.password);
@@ -205,13 +205,13 @@ export class AuthService {
//****************** */
//****************** */
private async checkUserLoginCredentialWithPhone(phone: string, otpCode: string) {
private async checkUserLoginCredentialWithPhone(phone: string, otpCode: string, businessId: string) {
const isValid = await this.otpService.verifyOtp(phone, otpCode, "LOGIN");
if (!isValid) throw new BadRequestException(AuthMessage.INVALID_OTP);
await this.otpService.delOtpFormCache(phone, "LOGIN");
const user = await this.usersService.findOneWithPhone(phone);
const user = await this.usersService.findOneWithPhone(phone, businessId);
if (!user) throw new BadRequestException(UserMessage.USER_NOT_FOUND);
return user;