This commit is contained in:
2026-03-10 11:16:16 +03:30
parent 4104bab0c6
commit cb4c8e1901
7 changed files with 47 additions and 39 deletions
+6 -6
View File
@@ -2,12 +2,12 @@ import { Module, forwardRef } from '@nestjs/common';
import { AuthService } from './services/auth.service';
import { AuthController } from './controllers/auth.controller';
import { UtilsModule } from '../utils/utils.module';
import { JwtModule } from '@nestjs/jwt';
import { JwtModule } from '@nestjs/jwt';
import { ConfigService } from '@nestjs/config';
import { AdminModule } from '../admin/admin.module';
import { TokensService } from './services/tokens.service';
import { MikroOrmModule } from '@mikro-orm/nestjs';
import { AdminAuthGuard } from './guards/adminAuth.guard';
import { MikroOrmModule } from '@mikro-orm/nestjs';
import { AdminAuthGuard } from './guards/adminAuth.guard';
import { RefreshToken } from './entities/refresh-token.entity';
import { NotificationsModule } from '../notifications/notifications.module';
import { BusinessModule } from '../business/business.module';
@@ -15,7 +15,7 @@ import { BusinessModule } from '../business/business.module';
@Module({
imports: [
UtilsModule,
JwtModule.registerAsync({
JwtModule.registerAsync({
useFactory: (configService: ConfigService) => {
const expiresIn = configService.getOrThrow<number>('JWT_EXPIRATION_TIME');
return {
@@ -33,10 +33,10 @@ import { BusinessModule } from '../business/business.module';
forwardRef(() => BusinessModule),
MikroOrmModule.forFeature([RefreshToken]),
forwardRef(() => NotificationsModule),
BusinessModule
],
controllers: [AuthController],
providers: [AuthService, TokensService, AdminAuthGuard],
exports: [AdminAuthGuard],
})
export class AuthModule {}
export class AuthModule { }
@@ -22,7 +22,7 @@ export class AuthController {
// return this.authService.requestOtp(dto, false);
// }
// @RefreshTokenRateLimit()
// @ApiOperation({ summary: 'refresh the user access token / refresh token' })
// @Post('public/auth/refresh')
@@ -54,12 +54,12 @@ export class AuthController {
// //super admin routes
// @UseGuards(SuperAdminAuthGuard)
// @Post('super-admin/auth/direct-login')
// @ApiOperation({ summary: 'Direct login for DSC - returns login credentials' })
// @ApiBody({ type: DirectLoginDto, description: 'Phone number and restaurant slug for direct login' })
// directloginForDsc(@Body() dto: DirectLoginDto) {
// return this.authService.loginAdminForDsc(dto.phone, dto.slug);
// }
@UseGuards(SuperAdminAuthGuard)
@Post('super-admin/auth/direct-login')
@ApiOperation({ summary: 'Direct login for DSC - returns login credentials' })
@ApiBody({ type: DirectLoginDto, description: 'Danak Subscriptionid' })
directloginForDsc(@Body() dto: DirectLoginDto) {
return this.authService.directLogin(dto.subscriptionId);
}
}
+2 -8
View File
@@ -4,12 +4,6 @@ import { ApiProperty } from '@nestjs/swagger';
export class DirectLoginDto {
@IsNotEmpty()
@IsString()
@ApiProperty({ example: '09362532122', description: 'Mobile number' })
@IsMobilePhone('fa-IR')
phone: string;
@IsNotEmpty()
@IsString()
@ApiProperty({ example: 'zhivan', description: 'Restaurant slug' })
slug: string;
@ApiProperty()
subscriptionId: string;
}
+13 -7
View File
@@ -4,10 +4,11 @@ import { CacheService } from '../../utils/cache.service';
import { SmsService } from '../../notifications/services/sms.service';
import { randomInt } from 'crypto';
import { TokensService } from './tokens.service';
import { AuthMessage, RestMessage } from 'src/common/enums/message.enum';
import { AuthMessage, RestMessage } from 'src/common/enums/message.enum';
import { AdminRepository } from 'src/modules/admin/repositories/admin.repository';
import { ConfigService } from '@nestjs/config';
import { normalizePhone } from '../../utils/phone.util';
import { BusinessService } from 'src/modules/business/business.service';
@Injectable()
export class AuthService {
@@ -17,18 +18,23 @@ export class AuthService {
private readonly cacheService: CacheService,
private readonly smsService: SmsService,
private readonly tokensService: TokensService,
private readonly adminRepository: AdminRepository,
private readonly adminRepository: AdminRepository,
private readonly configService: ConfigService,
private readonly businessService: BusinessService,
) {
this.OTP_EXPIRATION_TIME = this.configService.get<number>('OTP_EXPIRATION_TIME') ?? 240;
}
private userOtpKey(restaurantSlug: string, phone: string) {
return `otp:${restaurantSlug}:${phone}`;
}
// private userOtpKey(restaurantSlug: string, phone: string) {
// return `otp:${restaurantSlug}:${phone}`;
// }
private adminOtpKey(restaurantSlug: string, phone: string) {
return `otp-admin:${restaurantSlug}:${phone}`;
// private adminOtpKey(restaurantSlug: string, phone: string) {
// return `otp-admin:${restaurantSlug}:${phone}`;
// }
directLogin(danakSubId: string) {
const business=this.businessService.findBySubIdOrFail(danakSubId)
}
// async requestOtp(dto: RequestOtpDto, isAdmin: boolean) {
+4 -4
View File
@@ -17,10 +17,10 @@ export class BusinessController {
return this.businessService.findAll();
}
@Get(':id')
findOne(@Param('id') id: string) {
return this.businessService.findOne(+id);
}
// @Get(':id')
// findOne(@Param('id') id: string) {
// return this.businessService.fin(id);
// }
@Patch(':id')
update(@Param('id') id: string, @Body() updateBusinessDto: UpdateBusinessDto) {
+4 -2
View File
@@ -3,10 +3,12 @@ import { BusinessService } from './business.service';
import { BusinessController } from './business.controller';
import { MikroOrmModule } from '@mikro-orm/nestjs';
import { Business } from './entities/business.entity';
import { JwtModule } from '@nestjs/jwt';
@Module({
imports:[MikroOrmModule.forFeature([Business])],
imports: [MikroOrmModule.forFeature([Business]), JwtModule.register({})],
controllers: [BusinessController],
providers: [BusinessService],
exports: [BusinessService],
})
export class BusinessModule {}
export class BusinessModule { }
+10 -4
View File
@@ -1,19 +1,21 @@
import { BadRequestException, Injectable } from '@nestjs/common';
import { BadRequestException, Injectable, NotFoundException } from '@nestjs/common';
import { CreateBusinessDto } from './dto/create-business.dto';
import { UpdateBusinessDto } from './dto/update-business.dto';
import { EntityManager } from '@mikro-orm/postgresql';
import { Business } from './entities/business.entity';
import { Admin } from '../admin/entities/admin.entity';
import { BusinessRepository } from './repositories/business.repository';
@Injectable()
export class BusinessService {
constructor(
private readonly em: EntityManager,
private readonly businessRepository: BusinessRepository
) { }
async create(dto: CreateBusinessDto) {
const admin = await this.em.findOne(Admin, {})
if(!admin){
if (!admin) {
throw new BadRequestException()
}
const business = this.em.create(Business, { ...dto, admin })
@@ -24,8 +26,12 @@ export class BusinessService {
return `This action returns all business`;
}
findOne(id: number) {
return `This action returns a #${id} business`;
async findBySubIdOrFail(danakSubscriptionId: string) {
const business = await this.businessRepository.findOne({ danakSubscriptionId })
if (!business) {
throw new NotFoundException("Business not found")
}
return business
}
update(id: number, updateBusinessDto: UpdateBusinessDto) {