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 { UpdateAdminDto } from '../dto/update-admin.dto';
import { ApiTags, ApiOperation, ApiBody, ApiBearerAuth, ApiParam, ApiHeader } from '@nestjs/swagger'; import { ApiTags, ApiOperation, ApiBody, ApiBearerAuth, ApiParam, ApiHeader } from '@nestjs/swagger';
import { AdminAuthGuard } from 'src/modules/auth/guards/adminAuth.guard'; 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 { RestId } from 'src/common/decorators';
import { AdminId } from 'src/common/decorators/admin-id.decorator'; import { AdminId } from 'src/common/decorators/admin-id.decorator';
import { Admin } from '../entities/admin.entity'; import { Admin } from '../entities/admin.entity';
import { Permission } from 'src/common/enums/permission.enum'; import { Permission } from 'src/common/enums/permission.enum';
import { Permissions } from 'src/common/decorators/permissions.decorator'; import { Permissions } from 'src/common/decorators/permissions.decorator';
import { CreateMyRestaurantAdminDto } from '../dto/create-my-restaurant-admin.dto';
@ApiBearerAuth() @ApiBearerAuth()
@ApiTags('admin') @ApiTags('admin')
@UseGuards(AdminAuthGuard)
@Permissions(Permission.MANAGE_ADMINS)
@Controller('admin/admins') @Controller('admin/admins')
export class AdminController { export class AdminController {
constructor(private readonly adminService: AdminService) { } constructor(private readonly adminService: AdminService) { }
@Get() @Get()
@UseGuards(AdminAuthGuard)
@Permissions(Permission.MANAGE_ADMINS)
@ApiOperation({ summary: 'admin' }) @ApiOperation({ summary: 'admin' })
async getAll(@RestId() restId: string) { async getAll(@RestId() restId: string) {
const admin = await this.adminService.findAllByRestaurantId(restId); const admin = await this.adminService.findAllByRestaurantId(restId);
@@ -26,6 +28,8 @@ export class AdminController {
} }
@Get('profile') @Get('profile')
@UseGuards(AdminAuthGuard)
@Permissions(Permission.MANAGE_ADMINS)
@ApiOperation({ summary: 'admin' }) @ApiOperation({ summary: 'admin' })
async getMe(@AdminId() adminId: string, @RestId() restId: string) { async getMe(@AdminId() adminId: string, @RestId() restId: string) {
const admin = await this.adminService.findById(adminId, restId); const admin = await this.adminService.findById(adminId, restId);
@@ -33,6 +37,8 @@ export class AdminController {
} }
@Post() @Post()
@UseGuards(AdminAuthGuard)
@Permissions(Permission.MANAGE_ADMINS)
@HttpCode(HttpStatus.CREATED) @HttpCode(HttpStatus.CREATED)
@ApiOperation({ summary: 'Create a new admin' }) @ApiOperation({ summary: 'Create a new admin' })
@ApiBody({ type: CreateAdminDto }) @ApiBody({ type: CreateAdminDto })
@@ -42,22 +48,55 @@ export class AdminController {
} }
@Patch(':adminId') @Patch(':adminId')
@UseGuards(AdminAuthGuard)
@Permissions(Permission.MANAGE_ADMINS)
@ApiOperation({ summary: 'Update an admin' }) @ApiOperation({ summary: 'Update an admin' })
@ApiParam({ name: 'adminId', description: 'Admin ID' }) @ApiParam({ name: 'adminId', description: 'Admin ID' })
@ApiBody({ type: UpdateAdminDto }) @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): Promise<Admin> {
return this.adminService.update(adminId, restId, dto); return this.adminService.update(adminId, restId, dto);
} }
@Get(':adminId') @Get(':adminId')
@UseGuards(AdminAuthGuard)
@Permissions(Permission.MANAGE_ADMINS)
@ApiOperation({ summary: 'Get an admin by ID' }) @ApiOperation({ summary: 'Get an admin by ID' })
@ApiParam({ name: 'id', description: 'Admin ID' }) @ApiParam({ name: 'id', description: 'Admin ID' })
getById(@Param('adminId') adminId: string, @RestId() restId: string): Promise<Admin | null> { getById(@Param('adminId') adminId: string, @RestId() restId: string): Promise<Admin | null> {
return this.adminService.findById(adminId, restId); return this.adminService.findById(adminId, restId);
} }
@Delete(':adminId') @Delete(':adminId')
@UseGuards(AdminAuthGuard)
@Permissions(Permission.MANAGE_ADMINS)
@ApiOperation({ summary: 'Delete an admin by ID' }) @ApiOperation({ summary: 'Delete an admin by ID' })
@ApiParam({ name: 'id', description: 'Admin 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): Promise<void> {
return this.adminService.remove(adminId, restId); 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);
}
} }
+35 -22
View File
@@ -79,32 +79,45 @@ export class AdminService {
return admin; return admin;
} }
// async createRestaurantBySuperAdmin(data: { async createAdminForRestaurantBySuperAdmin(restId: string, dto: CreateMyRestaurantAdminDto) {
// phone: string; const { phone, firstName, lastName, roleId } = dto;
// firstName?: string; const normalizedPhone = normalizePhone(phone);
// lastName?: string; let admin: Admin | null = null;
// roleId: string; admin = await this.adminRepository.findOne({
// restId: string; phone: normalizedPhone,
// }) { });
// const { phone, firstName, lastName, roleId, restId } = data; 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 restaurant = await this.em.findOne(Restaurant, { id: restId });
// const existing = await this.adminRepository.findOne({ phone, roles: { restaurant: { id: restId } } }); if (!restaurant) throw new NotFoundException('Restaurant not found');
// if (existing) {
// return existing;
// }
// // load Role and Restaurant using the EntityManager let adminRole = await this.adminRoleRepository.findOne({
// const role = await this.em.findOne(Role, { id: roleId }); admin: admin,
// if (!role) throw new Error('Role not found'); restaurant: restaurant,
});
// const restaurant = await this.em.findOne(Restaurant, { id: restId }); if (!adminRole) {
// if (!restaurant) throw new Error('Restaurant not found'); 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(adminRole);
// await this.em.persistAndFlush(admin); return admin;
// return admin; }
// }
async update(adminId: string, restId: string, dto: UpdateAdminDto): Promise<Admin> { async update(adminId: string, restId: string, dto: UpdateAdminDto): Promise<Admin> {
const admin = await this.adminRepository.findOne( const admin = await this.adminRepository.findOne(
@@ -89,5 +89,14 @@ export class RestaurantsController {
return this.restaurantsService.findOneBySubscriptionId(subscriptionId); return this.restaurantsService.findOneBySubscriptionId(subscriptionId);
} }
@UseGuards(SuperAdminAuthGuard)
@ApiBearerAuth()
@ApiOperation({ summary: 'Update a restaurant by ID' })
@ApiParam({ name: 'id', required: true, description: 'Restaurant ID' })
@ApiBody({ type: UpdateRestaurantDto })
@Patch('super-admin/restaurants/:id')
updateRestaurant(@Param('id') id: string, @Body() updateRestaurantDto: UpdateRestaurantDto) {
return this.restaurantsService.update(id, updateRestaurantDto);
}
} }
@@ -5,30 +5,35 @@ import { PermissionsService } from '../providers/permissions.service';
import { CreateRoleDto } from '../dto/create-role.dto'; import { CreateRoleDto } from '../dto/create-role.dto';
import { UpdateRoleDto } from '../dto/update-role.dto'; import { UpdateRoleDto } from '../dto/update-role.dto';
import { AdminAuthGuard } from 'src/modules/auth/guards/adminAuth.guard'; import { AdminAuthGuard } from 'src/modules/auth/guards/adminAuth.guard';
import { SuperAdminAuthGuard } from 'src/modules/auth/guards/superAdminAuth.guard';
import { Permissions } from 'src/common/decorators/permissions.decorator'; import { Permissions } from 'src/common/decorators/permissions.decorator';
import { RestId } from 'src/common/decorators'; import { RestId } from 'src/common/decorators';
import { AdminId } from 'src/common/decorators/admin-id.decorator'; import { AdminId } from 'src/common/decorators/admin-id.decorator';
import { Permission } from 'src/common/enums/permission.enum'; import { Permission } from 'src/common/enums/permission.enum';
@UseGuards(AdminAuthGuard)
@Permissions(Permission.MANAGE_ROLES)
@ApiBearerAuth()
@ApiTags('roles') @ApiTags('roles')
@Controller('admin/roles') @Controller()
export class RolesController { export class RolesController {
constructor( constructor(
private readonly roleService: RolesService, private readonly roleService: RolesService,
private readonly permissionService: PermissionsService, private readonly permissionService: PermissionsService,
) { } ) { }
@Get() @Get('admin/roles')
@UseGuards(AdminAuthGuard)
@Permissions(Permission.MANAGE_ROLES)
@ApiBearerAuth()
@ApiOperation({ summary: 'Get all through restaurant roles with pagination and filters' }) @ApiOperation({ summary: 'Get all through restaurant roles with pagination and filters' })
findAll(@RestId() restId: string) { findAll(@RestId() restId: string) {
return this.roleService.findAllGeneralAndRestaurantRoles(restId); return this.roleService.findAllGeneralAndRestaurantRoles(restId);
} }
@Get('permissions') @Get('admin/roles/permissions')
@UseGuards(AdminAuthGuard)
@Permissions(Permission.MANAGE_ROLES)
@ApiBearerAuth()
@ApiOperation({ summary: 'Get all permissions that the admin has' }) @ApiOperation({ summary: 'Get all permissions that the admin has' })
async findAllPermissions(@AdminId() adminId: string, @RestId() restId: string) { async findAllPermissions(@AdminId() adminId: string, @RestId() restId: string) {
const adminPermissionNames = await this.permissionService.getAdminFullPermissions(adminId, restId); const adminPermissionNames = await this.permissionService.getAdminFullPermissions(adminId, restId);
@@ -36,29 +41,53 @@ export class RolesController {
return adminPermissionNames; return adminPermissionNames;
} }
@Post()
@Post('admin/roles')
@UseGuards(AdminAuthGuard)
@Permissions(Permission.MANAGE_ROLES)
@ApiBearerAuth()
@ApiOperation({ summary: 'Create a new role' }) @ApiOperation({ summary: 'Create a new role' })
@ApiBody({ type: CreateRoleDto }) @ApiBody({ type: CreateRoleDto })
create(@Body() dto: CreateRoleDto, @RestId() restId: string) { create(@Body() dto: CreateRoleDto, @RestId() restId: string) {
return this.roleService.createRestaurantRole(dto, restId); return this.roleService.createRestaurantRole(dto, restId);
} }
@Get(':id') @Get('admin/roles/:id')
@UseGuards(AdminAuthGuard)
@Permissions(Permission.MANAGE_ROLES)
@ApiBearerAuth()
@ApiOperation({ summary: 'Get a specific role by ID' }) @ApiOperation({ summary: 'Get a specific role by ID' })
findOne(@Param('id') id: string, @RestId() restId: string) { findOne(@Param('id') id: string, @RestId() restId: string) {
return this.roleService.findOne(restId, id); return this.roleService.findOne(restId, id);
} }
@Patch(':id') @Patch('admin/roles/:id')
@UseGuards(AdminAuthGuard)
@Permissions(Permission.MANAGE_ROLES)
@ApiBearerAuth()
@ApiOperation({ summary: 'Update a role' }) @ApiOperation({ summary: 'Update a role' })
@ApiBody({ type: UpdateRoleDto }) @ApiBody({ type: UpdateRoleDto })
update(@Param('id') id: string, @Body() dto: UpdateRoleDto, @RestId() restId: string) { update(@Param('id') id: string, @Body() dto: UpdateRoleDto, @RestId() restId: string) {
return this.roleService.update(restId, id, dto); return this.roleService.update(restId, id, dto);
} }
@Delete(':id') @Delete('admin/roles/:id')
@UseGuards(AdminAuthGuard)
@Permissions(Permission.MANAGE_ROLES)
@ApiBearerAuth()
@ApiOperation({ summary: 'Delete a role' }) @ApiOperation({ summary: 'Delete a role' })
remove(@Param('id') id: string, @RestId() restId: string) { remove(@Param('id') id: string, @RestId() restId: string) {
return this.roleService.remove(restId, id); return this.roleService.remove(restId, id);
} }
/** Super Admin Endpoints */
@UseGuards(SuperAdminAuthGuard)
@ApiBearerAuth()
@ApiOperation({ summary: 'Get all system roles for super-admin' })
@Get('super-admin/system-roles')
@ApiOperation({ summary: 'Get all system roles for super-admin' })
findAllSystemRoles() {
return this.roleService.findAllSystemRoles();
}
} }
@@ -104,6 +104,18 @@ export class RolesService {
return role; return role;
} }
async findAllSystemRoles() {
const roles = await this.roleRepository.find(
{ isSystem: true },
{
orderBy: { createdAt: 'desc' },
populate: ['permissions', 'restaurant'],
},
);
return roles;
}
async remove(restId: string, id: string) { async remove(restId: string, id: string) {
const role = await this.roleRepository.findOne( const role = await this.roleRepository.findOne(
{ id, restaurant: { id: restId } }, { id, restaurant: { id: restId } },