Integrate RestaurantsModule into AdminModule; enhance AdminRepository to find admins by phone and restaurant slug using AdminRole; update AuthService to throw specific exception message when admin not found; correct last name in DatabaseSeeder for consistency.

This commit is contained in:
2025-11-19 00:00:48 +03:30
parent fdaa8b3dfd
commit e620dd6e04
4 changed files with 27 additions and 8 deletions
+2
View File
@@ -9,6 +9,7 @@ import { Permission } from '../roles/entities/permission.entity';
import { RolePermission } from '../roles/entities/rolePermission.entity'; import { RolePermission } from '../roles/entities/rolePermission.entity';
import { AdminController } from './controllers/admin.controller'; import { AdminController } from './controllers/admin.controller';
import { UtilsModule } from '../utils/utils.module'; import { UtilsModule } from '../utils/utils.module';
import { RestaurantsModule } from '../restaurants/restaurants.module';
@Module({ @Module({
providers: [AdminService, AdminRepository], providers: [AdminService, AdminRepository],
@@ -17,6 +18,7 @@ import { UtilsModule } from '../utils/utils.module';
MikroOrmModule.forFeature([Admin, Role, Permission, RolePermission]), MikroOrmModule.forFeature([Admin, Role, Permission, RolePermission]),
JwtModule, JwtModule,
UtilsModule, UtilsModule,
RestaurantsModule,
], ],
exports: [AdminService, AdminRepository], exports: [AdminService, AdminRepository],
}) })
@@ -1,18 +1,35 @@
import { EntityManager, EntityRepository } from '@mikro-orm/postgresql'; import { EntityManager, EntityRepository } from '@mikro-orm/postgresql';
import { Admin } from '../entities/admin.entity'; import { Admin } from '../entities/admin.entity';
import { AdminRole } from '../entities/adminRole.entity';
import { RestRepository } from '../../restaurants/repositories/rest.repository';
import { Injectable } from '@nestjs/common'; import { Injectable } from '@nestjs/common';
@Injectable() @Injectable()
export class AdminRepository extends EntityRepository<Admin> { export class AdminRepository extends EntityRepository<Admin> {
constructor(readonly em: EntityManager) { constructor(
readonly em: EntityManager,
private readonly restRepository: RestRepository,
) {
super(em, Admin); super(em, Admin);
} }
async findByPhoneAndRestaurantSlug(phone: string, slug: string): Promise<Admin | null> { async findByPhoneAndRestaurantSlug(phone: string, slug: string): Promise<Admin | null> {
return this.em.findOne( // First, find the restaurant by slug using the same repository as auth service
Admin, const restaurant = await this.restRepository.findOne({ slug });
{ phone, roles: { role: { restaurant: { slug } } } }, if (!restaurant) {
{ populate: ['roles', 'roles.role'] }, 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;
} }
} }
+1 -1
View File
@@ -40,7 +40,7 @@ export class AuthService {
const admin = await this.adminRepository.findByPhoneAndRestaurantSlug(phone, slug); const admin = await this.adminRepository.findByPhoneAndRestaurantSlug(phone, slug);
if (!admin) { if (!admin) {
throw new NotFoundException(); throw new NotFoundException(AuthMessage.ADMIN_NOT_FOUND);
} }
const code = this.generateOtpCode(); const code = this.generateOtpCode();
+1 -1
View File
@@ -440,7 +440,7 @@ export class DatabaseSeeder extends Seeder {
const admin = em.create(Admin, { const admin = em.create(Admin, {
phone: '09185290775', phone: '09185290775',
firstName: 'حمید', firstName: 'حمید',
lastName: 'زرگامی', lastName: 'ضرقامی',
}); });
em.persist(admin); em.persist(admin);