update admin
This commit is contained in:
@@ -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]),
|
||||
|
||||
@@ -21,19 +21,15 @@ export class AdminController {
|
||||
@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<Admin> {
|
||||
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<Admin | null> {
|
||||
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<void> {
|
||||
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<Admin[]> {
|
||||
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<void> {
|
||||
) {
|
||||
return this.adminService.remove(adminId, restaurantId);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -28,6 +28,4 @@ export class Admin extends BaseEntity {
|
||||
})
|
||||
roles = new Collection<AdminRole>(this);
|
||||
|
||||
/** Restaurant-specific role (set when fetched in restaurant context, not persisted) */
|
||||
role?: AdminRole;
|
||||
}
|
||||
|
||||
@@ -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<Admin>,
|
||||
@InjectRepository(AdminRole)
|
||||
private readonly adminRoleRepository: EntityRepository<AdminRole>,
|
||||
private readonly adminRepository: AdminRepository,
|
||||
private readonly adminRoleRepository: AdminRoleRepository,
|
||||
private readonly em: EntityManager,
|
||||
private readonly cacheService: CacheService,
|
||||
) { }
|
||||
|
||||
async findByPhone(phone: string): Promise<Admin | null> {
|
||||
async findByPhone(phone: string) {
|
||||
const normalizedPhone = normalizePhone(phone);
|
||||
return this.adminRepository.findOne({ phone: normalizedPhone });
|
||||
}
|
||||
|
||||
async findById(adminId: string, restId: string): Promise<Admin | null> {
|
||||
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<Admin[]> {
|
||||
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<Admin[]> {
|
||||
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<Admin> {
|
||||
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'] },
|
||||
|
||||
@@ -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<AdminRole> {
|
||||
constructor(
|
||||
readonly em: EntityManager,
|
||||
) {
|
||||
super(em, AdminRole);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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
|
||||
Reference in New Issue
Block a user