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