diff --git a/src/modules/admin/admin.module.ts b/src/modules/admin/admin.module.ts index c474d15..eced9fc 100644 --- a/src/modules/admin/admin.module.ts +++ b/src/modules/admin/admin.module.ts @@ -11,9 +11,10 @@ import { AdminController } from './controllers/admin.controller'; import { UtilsModule } from '../utils/utils.module'; import { RestaurantsModule } from '../restaurants/restaurants.module'; import { AdminRole } from './entities/adminRole.entity'; +import { AdminRoleRepository } from './repositories/admin-role.repository'; @Module({ - providers: [AdminService, AdminRepository], + providers: [AdminService, AdminRepository, AdminRoleRepository], controllers: [AdminController], imports: [ MikroOrmModule.forFeature([Admin, Role, Permission, RolePermission, AdminRole]), @@ -23,4 +24,4 @@ import { AdminRole } from './entities/adminRole.entity'; ], exports: [AdminService, AdminRepository], }) -export class AdminModule {} +export class AdminModule { } diff --git a/src/modules/admin/controllers/admin.controller.ts b/src/modules/admin/controllers/admin.controller.ts index 3bea57b..f621b7d 100644 --- a/src/modules/admin/controllers/admin.controller.ts +++ b/src/modules/admin/controllers/admin.controller.ts @@ -16,24 +16,20 @@ import { CreateMyRestaurantAdminDto } from '../dto/create-my-restaurant-admin.dt @ApiTags('admin') @Controller() export class AdminController { - constructor(private readonly adminService: AdminService) {} + constructor(private readonly adminService: AdminService) { } @Get('admin/admins') @UseGuards(AdminAuthGuard) @Permissions(Permission.MANAGE_ADMINS) - @ApiOperation({ summary: 'admin' }) - async getAll(@RestId() restId: string) { - const admin = await this.adminService.findAllByRestaurantId(restId); - return admin; + getAll(@RestId() restId: string) { + return this.adminService.findAllByRestaurantId(restId); } @Get('admin/admins/profile') @UseGuards(AdminAuthGuard) @Permissions(Permission.MANAGE_ADMINS) - @ApiOperation({ summary: 'admin' }) - async getMe(@AdminId() adminId: string, @RestId() restId: string) { - const admin = await this.adminService.findById(adminId, restId); - return admin; + getMe(@AdminId() adminId: string, @RestId() restId: string) { + return this.adminService.finAdminById(adminId, restId); } @Post('admin/admins') @@ -52,18 +48,16 @@ export class AdminController { @Permissions(Permission.MANAGE_ADMINS) @ApiOperation({ summary: 'Update an admin' }) @ApiParam({ name: 'adminId', description: 'Admin ID' }) - @ApiBody({ type: UpdateAdminDto }) - update(@Param('adminId') adminId: string, @Body() dto: UpdateAdminDto, @RestId() restId: string): Promise { + update(@Param('adminId') adminId: string, @Body() dto: UpdateAdminDto, + @RestId() restId: string) { return this.adminService.update(adminId, restId, dto); } @Get('admin/admins/:adminId') @UseGuards(AdminAuthGuard) @Permissions(Permission.MANAGE_ADMINS) - @ApiOperation({ summary: 'Get an admin by ID' }) - @ApiParam({ name: 'id', description: 'Admin ID' }) - getById(@Param('adminId') adminId: string, @RestId() restId: string): Promise { - return this.adminService.findById(adminId, restId); + getById(@Param('adminId') adminId: string, @RestId() restId: string) { + return this.adminService.finAdminById(adminId, restId); } @Delete('admin/admins/:adminId') @@ -71,7 +65,7 @@ export class AdminController { @Permissions(Permission.MANAGE_ADMINS) @ApiOperation({ summary: 'Delete an admin by ID' }) @ApiParam({ name: 'id', description: 'Admin ID' }) - deleteById(@Param('adminId') adminId: string, @RestId() restId: string): Promise { + deleteById(@Param('adminId') adminId: string, @RestId() restId: string) { return this.adminService.remove(adminId, restId); } @@ -80,7 +74,7 @@ export class AdminController { @Get('super-admin/restaurants/:restaurantId/admins') @ApiOperation({ summary: 'Get admins for a specific restaurant (Super Admin only)' }) @ApiParam({ name: 'restaurantId', description: 'Restaurant ID' }) - getAdminForRestaurant(@Param('restaurantId') restaurantId: string): Promise { + getAdminForRestaurant(@Param('restaurantId') restaurantId: string) { return this.adminService.getAdminsForRestaurantBySuperAdmin(restaurantId); } @@ -104,7 +98,7 @@ export class AdminController { revokeAdminFromRestaurant( @Param('restaurantId') restaurantId: string, @Param('adminId') adminId: string, - ): Promise { + ) { return this.adminService.remove(adminId, restaurantId); } } diff --git a/src/modules/admin/entities/admin.entity.ts b/src/modules/admin/entities/admin.entity.ts index e0e4a8d..9ce3a28 100644 --- a/src/modules/admin/entities/admin.entity.ts +++ b/src/modules/admin/entities/admin.entity.ts @@ -28,6 +28,4 @@ export class Admin extends BaseEntity { }) roles = new Collection(this); - /** Restaurant-specific role (set when fetched in restaurant context, not persisted) */ - role?: AdminRole; } diff --git a/src/modules/admin/providers/admin.service.ts b/src/modules/admin/providers/admin.service.ts index 74e23d8..9c5f803 100644 --- a/src/modules/admin/providers/admin.service.ts +++ b/src/modules/admin/providers/admin.service.ts @@ -1,44 +1,47 @@ import { ConflictException, Injectable, NotFoundException } from '@nestjs/common'; -import { InjectRepository } from '@mikro-orm/nestjs'; -import { EntityRepository } from '@mikro-orm/core'; import { Admin } from '../entities/admin.entity'; import { Role } from '../../roles/entities/role.entity'; import { Restaurant } from '../../restaurants/entities/restaurant.entity'; +import { wrap } from '@mikro-orm/core'; import { EntityManager } from '@mikro-orm/postgresql'; -import { CacheService } from '../../utils/cache.service'; import { CreateMyRestaurantAdminDto } from '../dto/create-my-restaurant-admin.dto'; import { UpdateAdminDto } from '../dto/update-admin.dto'; import { AdminRole } from '../entities/adminRole.entity'; import { normalizePhone } from '../../utils/phone.util'; +import { AdminRepository } from '../repositories/admin.repository'; +import { AdminRoleRepository } from '../repositories/admin-role.repository'; @Injectable() export class AdminService { constructor( - @InjectRepository(Admin) - private readonly adminRepository: EntityRepository, - @InjectRepository(AdminRole) - private readonly adminRoleRepository: EntityRepository, + private readonly adminRepository: AdminRepository, + private readonly adminRoleRepository: AdminRoleRepository, private readonly em: EntityManager, - private readonly cacheService: CacheService, ) { } - async findByPhone(phone: string): Promise { + async findByPhone(phone: string) { const normalizedPhone = normalizePhone(phone); return this.adminRepository.findOne({ phone: normalizedPhone }); } - async findById(adminId: string, restId: string): Promise { + async finAdminById( + adminId: string, + restId: string, + ) { const admin = await this.adminRepository.findOne( { id: adminId, roles: { restaurant: { id: restId } } }, - { populate: ['roles', 'roles.role', 'roles.restaurant'] }, + { populate: ['roles'], exclude: ['roles'] }, ); - if (!admin) return null; - const adminRole = admin.roles.getItems().find(r => r.restaurant?.id === restId); - admin.role = adminRole; - return admin; + if (!admin) { + throw new NotFoundException('Admin not found'); + } + const adminPlain = wrap(admin).toObject(); + const adminRole = await this.adminRoleRepository.findOne({ admin: admin, restaurant: { id: restId } }); + + return { ...adminPlain, role: adminRole?.role }; } - async findAllByRestaurantId(restId: string): Promise { + async findAllByRestaurantId(restId: string) { return this.adminRepository.find({ roles: { restaurant: { id: restId } } }, { populate: ['roles', 'roles.role'] }); } @@ -122,14 +125,14 @@ export class AdminService { return admin; } - async getAdminsForRestaurantBySuperAdmin(restId: string): Promise { + async getAdminsForRestaurantBySuperAdmin(restId: string) { const restaurant = await this.em.findOne(Restaurant, { id: restId }); if (!restaurant) throw new NotFoundException('Restaurant not found'); return this.adminRepository.find({ roles: { restaurant: restaurant } }, { populate: ['roles', 'roles.role'] }); } - async update(adminId: string, restId: string, dto: UpdateAdminDto): Promise { + async update(adminId: string, restId: string, dto: UpdateAdminDto) { const admin = await this.adminRepository.findOne( { id: adminId, roles: { restaurant: { id: restId } } }, { populate: ['roles', 'roles.role', 'roles.restaurant'] }, diff --git a/src/modules/admin/repositories/admin-role.repository.ts b/src/modules/admin/repositories/admin-role.repository.ts new file mode 100644 index 0000000..8137d0c --- /dev/null +++ b/src/modules/admin/repositories/admin-role.repository.ts @@ -0,0 +1,13 @@ +import { EntityManager, EntityRepository } from '@mikro-orm/postgresql'; +import { AdminRole } from '../entities/adminRole.entity'; +import { Injectable } from '@nestjs/common'; + +@Injectable() +export class AdminRoleRepository extends EntityRepository { + constructor( + readonly em: EntityManager, + ) { + super(em, AdminRole); + } + +} diff --git a/src/modules/admin/transformers/admin-detail.transformer.ts b/src/modules/admin/transformers/admin-detail.transformer.ts deleted file mode 100644 index 6590afc..0000000 --- a/src/modules/admin/transformers/admin-detail.transformer.ts +++ /dev/null @@ -1,51 +0,0 @@ -import type { Admin } from '../entities/admin.entity'; - -export interface AdminDetailResponse { - id: string; - firstName?: string; - lastName?: string; - phone: string; - role: { - id: string; - name: string; - }; - permissions: string[]; - restaurant?: { - id: string; - name: string; - slug: string; - }; -} - -export class AdminDetailTransformer { - static transform(admin: Admin): AdminDetailResponse | null { - if (!admin) { - return null; - } - - // Extract role information (prefer restaurant-specific role when set) - const adminRole = admin.role ?? admin.roles.getItems().find(r => r.role); - const role = adminRole?.role; - if (!role) { - return null; - } - return { - id: admin.id, - firstName: admin.firstName, - lastName: admin.lastName, - phone: admin.phone, - role: { - id: role.id, - name: role.name, - }, - permissions: role.permissions.getItems().map(p => p.name) || [], - restaurant: { - id: adminRole?.restaurant?.id || role.restaurant?.id || '', - name: adminRole?.restaurant?.name || role.restaurant?.name || '', - slug: adminRole?.restaurant?.slug || role.restaurant?.slug || '', - }, - }; - } -} - -// Extract permissions - ensure collection is loaded if needed