diff --git a/src/modules/admin/admin.module.ts b/src/modules/admin/admin.module.ts index ce0b205..74f3c00 100644 --- a/src/modules/admin/admin.module.ts +++ b/src/modules/admin/admin.module.ts @@ -9,6 +9,7 @@ import { Permission } from '../roles/entities/permission.entity'; import { RolePermission } from '../roles/entities/rolePermission.entity'; import { AdminController } from './controllers/admin.controller'; import { UtilsModule } from '../utils/utils.module'; +import { RestaurantsModule } from '../restaurants/restaurants.module'; @Module({ providers: [AdminService, AdminRepository], @@ -17,6 +18,7 @@ import { UtilsModule } from '../utils/utils.module'; MikroOrmModule.forFeature([Admin, Role, Permission, RolePermission]), JwtModule, UtilsModule, + RestaurantsModule, ], exports: [AdminService, AdminRepository], }) diff --git a/src/modules/admin/repositories/admin.repository.ts b/src/modules/admin/repositories/admin.repository.ts index bc7b282..5c4d18d 100644 --- a/src/modules/admin/repositories/admin.repository.ts +++ b/src/modules/admin/repositories/admin.repository.ts @@ -1,18 +1,35 @@ import { EntityManager, EntityRepository } from '@mikro-orm/postgresql'; import { Admin } from '../entities/admin.entity'; +import { AdminRole } from '../entities/adminRole.entity'; +import { RestRepository } from '../../restaurants/repositories/rest.repository'; import { Injectable } from '@nestjs/common'; @Injectable() export class AdminRepository extends EntityRepository { - constructor(readonly em: EntityManager) { + constructor( + readonly em: EntityManager, + private readonly restRepository: RestRepository, + ) { super(em, Admin); } async findByPhoneAndRestaurantSlug(phone: string, slug: string): Promise { - return this.em.findOne( - Admin, - { phone, roles: { role: { restaurant: { slug } } } }, - { populate: ['roles', 'roles.role'] }, - ); + // First, find the restaurant by slug using the same repository as auth service + const restaurant = await this.restRepository.findOne({ slug }); + if (!restaurant) { + return null; + } + + // Find AdminRole that matches the admin phone and restaurant + const adminRole = await this.em.findOne(AdminRole, { + admin: { phone }, + restaurant: { id: restaurant.id }, + }); + + if (!adminRole || !adminRole.admin) { + return null; + } + + return adminRole.admin; } } diff --git a/src/modules/auth/services/auth.service.ts b/src/modules/auth/services/auth.service.ts index 31b5704..10d7f3e 100644 --- a/src/modules/auth/services/auth.service.ts +++ b/src/modules/auth/services/auth.service.ts @@ -40,7 +40,7 @@ export class AuthService { const admin = await this.adminRepository.findByPhoneAndRestaurantSlug(phone, slug); if (!admin) { - throw new NotFoundException(); + throw new NotFoundException(AuthMessage.ADMIN_NOT_FOUND); } const code = this.generateOtpCode(); diff --git a/src/seeders/DatabaseSeeder.ts b/src/seeders/DatabaseSeeder.ts index c589a57..5878f92 100644 --- a/src/seeders/DatabaseSeeder.ts +++ b/src/seeders/DatabaseSeeder.ts @@ -440,7 +440,7 @@ export class DatabaseSeeder extends Seeder { const admin = em.create(Admin, { phone: '09185290775', firstName: 'حمید', - lastName: 'زرگامی', + lastName: 'ضرقامی', }); em.persist(admin);