import { Injectable } from '@nestjs/common'; import { TokensService } from './tokens.service'; import { BusinessService } from 'src/modules/business/business.service'; @Injectable() export class AuthService { constructor( private readonly tokensService: TokensService, private readonly businessService: BusinessService, ) { } // private userOtpKey(restaurantSlug: string, phone: string) { // return `otp:${restaurantSlug}:${phone}`; // } // private adminOtpKey(restaurantSlug: string, phone: string) { // return `otp-admin:${restaurantSlug}:${phone}`; // } async directLogin(danakSubId: string) { const business = await this.businessService.findBySubIdOrFail(danakSubId) const admin = business.admin const tokens = await this.tokensService.generateAccessAndRefreshToken(admin.id, business.id); return tokens } // async requestOtp(dto: RequestOtpDto, isAdmin: boolean) { // const { phone, slug } = dto; // const normalizedPhone = normalizePhone(phone); // if (isAdmin) { // const admin = await this.adminRepository.findByPhoneAndRestaurantSlug(normalizedPhone, slug); // if (!admin) { // throw new BadRequestException(AuthMessage.ADMIN_NOT_FOUND); // } // } // const code = this.generateOtpCode(); // const key = isAdmin ? this.adminOtpKey(slug, normalizedPhone) : this.userOtpKey(slug, normalizedPhone); // await this.cacheService.set(key, code, this.OTP_EXPIRATION_TIME); // await this.smsService.sendotp(normalizedPhone, code); // return { code: null }; // } // async verifyOtpAdmin(phone: string, slug: string, code: string) { // const normalizedPhone = normalizePhone(phone); // const key = this.adminOtpKey(slug, normalizedPhone); // const cachedCode = await this.cacheService.get(key); // if (!cachedCode) throw new BadRequestException('OTP expired or not found'); // if (cachedCode !== code) throw new BadRequestException('Invalid OTP'); // await this.cacheService.del(key); // // const admin = await this.adminRepository.findByPhoneAndRestaurantSlug(normalizedPhone, slug); // // if (!admin) { // // throw new BadRequestException(AuthMessage.ADMIN_NOT_FOUND); // // } // const rest = await this.restRepository.findOne({ slug }); // if (!rest) { // throw new BadRequestException(RestMessage.NOT_FOUND); // } // const tokens = await this.tokensService.generateAccessAndRefreshToken(admin.id, rest.id, true, slug); // return { tokens, admin }; // } /** * to use for login directly from DSC */ // async loginAdminForDsc(phone: string, slug: string) { // const normalizedPhone = normalizePhone(phone); // const admin = await this.adminRepository.findByPhoneAndRestaurantSlug(normalizedPhone, slug); // if (!admin) { // throw new BadRequestException(AuthMessage.ADMIN_NOT_FOUND); // } // const rest = await this.restRepository.findOne({ slug }); // if (!rest) { // throw new BadRequestException(RestMessage.NOT_FOUND); // } // const tokens = await this.tokensService.generateAccessAndRefreshToken(admin.id, rest.id, true, slug); // return { tokens, admin }; // } // private generateOtpCode(): string { // const code = randomInt(10000, 100000); // return code.toString(); // } // refreshToken(oldRefreshToken: string) { // return this.tokensService.refreshToken(oldRefreshToken); // } }