get admins, get system roles,

This commit is contained in:
2025-12-28 10:26:24 +03:30
parent 4da1680b29
commit 9b409f3e07
5 changed files with 139 additions and 37 deletions
@@ -4,21 +4,23 @@ import { CreateAdminDto } from '../dto/create-admin.dto';
import { UpdateAdminDto } from '../dto/update-admin.dto';
import { ApiTags, ApiOperation, ApiBody, ApiBearerAuth, ApiParam, ApiHeader } from '@nestjs/swagger';
import { AdminAuthGuard } from 'src/modules/auth/guards/adminAuth.guard';
import { SuperAdminAuthGuard } from 'src/modules/auth/guards/superAdminAuth.guard';
import { RestId } from 'src/common/decorators';
import { AdminId } from 'src/common/decorators/admin-id.decorator';
import { Admin } from '../entities/admin.entity';
import { Permission } from 'src/common/enums/permission.enum';
import { Permissions } from 'src/common/decorators/permissions.decorator';
import { CreateMyRestaurantAdminDto } from '../dto/create-my-restaurant-admin.dto';
@ApiBearerAuth()
@ApiTags('admin')
@UseGuards(AdminAuthGuard)
@Permissions(Permission.MANAGE_ADMINS)
@Controller('admin/admins')
export class AdminController {
constructor(private readonly adminService: AdminService) {}
constructor(private readonly adminService: AdminService) { }
@Get()
@UseGuards(AdminAuthGuard)
@Permissions(Permission.MANAGE_ADMINS)
@ApiOperation({ summary: 'admin' })
async getAll(@RestId() restId: string) {
const admin = await this.adminService.findAllByRestaurantId(restId);
@@ -26,6 +28,8 @@ export class AdminController {
}
@Get('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);
@@ -33,6 +37,8 @@ export class AdminController {
}
@Post()
@UseGuards(AdminAuthGuard)
@Permissions(Permission.MANAGE_ADMINS)
@HttpCode(HttpStatus.CREATED)
@ApiOperation({ summary: 'Create a new admin' })
@ApiBody({ type: CreateAdminDto })
@@ -42,22 +48,55 @@ export class AdminController {
}
@Patch(':adminId')
@UseGuards(AdminAuthGuard)
@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> {
return this.adminService.update(adminId, restId, dto);
}
@Get(':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);
}
@Delete(':adminId')
@UseGuards(AdminAuthGuard)
@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> {
return this.adminService.remove(adminId, restId);
}
/** Super Admin Endpoints */
@UseGuards(SuperAdminAuthGuard)
@Post('super-admin/restaurants/:restaurantId/admins')
@ApiOperation({ summary: 'Create admin for a specific restaurant (Super Admin only)' })
@ApiParam({ name: 'restaurantId', description: 'Restaurant ID' })
@ApiBody({ type: CreateMyRestaurantAdminDto })
createAdminForRestaurant(
@Param('restaurantId') restaurantId: string,
@Body() dto: CreateMyRestaurantAdminDto,
): Promise<Admin> {
return this.adminService.createAdminForRestaurantBySuperAdmin(restaurantId, dto);
}
@UseGuards(SuperAdminAuthGuard)
@Delete('super-admin/restaurants/:restaurantId/admins/:adminId')
@ApiOperation({ summary: 'Revoke admin role from a restaurant (Super Admin only)' })
@ApiParam({ name: 'restaurantId', description: 'Restaurant ID' })
@ApiParam({ name: 'adminId', description: 'Admin ID' })
revokeAdminFromRestaurant(
@Param('restaurantId') restaurantId: string,
@Param('adminId') adminId: string,
): Promise<void> {
return this.adminService.remove(adminId, restaurantId);
}
}
+36 -23
View File
@@ -20,7 +20,7 @@ export class AdminService {
private readonly adminRoleRepository: EntityRepository<AdminRole>,
private readonly em: EntityManager,
private readonly cacheService: CacheService,
) {}
) { }
async findByPhone(phone: string): Promise<Admin | null> {
const normalizedPhone = normalizePhone(phone);
@@ -79,32 +79,45 @@ export class AdminService {
return admin;
}
// async createRestaurantBySuperAdmin(data: {
// phone: string;
// firstName?: string;
// lastName?: string;
// roleId: string;
// restId: string;
// }) {
// const { phone, firstName, lastName, roleId, restId } = data;
async createAdminForRestaurantBySuperAdmin(restId: string, dto: CreateMyRestaurantAdminDto) {
const { phone, firstName, lastName, roleId } = dto;
const normalizedPhone = normalizePhone(phone);
let admin: Admin | null = null;
admin = await this.adminRepository.findOne({
phone: normalizedPhone,
});
if (!admin) {
admin = this.adminRepository.create({
phone: normalizedPhone,
firstName,
lastName,
});
await this.em.persistAndFlush(admin);
}
const role = await this.em.findOne(Role, { id: roleId, isSystem: false });
if (!role) throw new NotFoundException('Role not found');
// // check existing
// const existing = await this.adminRepository.findOne({ phone, roles: { restaurant: { id: restId } } });
// if (existing) {
// return existing;
// }
const restaurant = await this.em.findOne(Restaurant, { id: restId });
if (!restaurant) throw new NotFoundException('Restaurant not found');
// // load Role and Restaurant using the EntityManager
// const role = await this.em.findOne(Role, { id: roleId });
// if (!role) throw new Error('Role not found');
let adminRole = await this.adminRoleRepository.findOne({
admin: admin,
restaurant: restaurant,
});
// const restaurant = await this.em.findOne(Restaurant, { id: restId });
// if (!restaurant) throw new Error('Restaurant not found');
if (!adminRole) {
adminRole = this.adminRoleRepository.create({
admin: admin,
role,
restaurant,
});
} else {
adminRole.role = role;
}
// const admin = this.adminRepository.create({ phone, firstName, lastName, roles: [{ role, restaurant }] });
// await this.em.persistAndFlush(admin);
// return admin;
// }
await this.em.persistAndFlush(adminRole);
return admin;
}
async update(adminId: string, restId: string, dto: UpdateAdminDto): Promise<Admin> {
const admin = await this.adminRepository.findOne(