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 { 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],
})
@@ -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<Admin> {
constructor(readonly em: EntityManager) {
constructor(
readonly em: EntityManager,
private readonly restRepository: RestRepository,
) {
super(em, Admin);
}
async findByPhoneAndRestaurantSlug(phone: string, slug: string): Promise<Admin | null> {
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;
}
}
+1 -1
View File
@@ -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();
+1 -1
View File
@@ -440,7 +440,7 @@ export class DatabaseSeeder extends Seeder {
const admin = em.create(Admin, {
phone: '09185290775',
firstName: 'حمید',
lastName: 'زرگامی',
lastName: 'ضرقامی',
});
em.persist(admin);