user module

This commit is contained in:
2026-01-13 19:59:06 +03:30
parent 6522acff5f
commit d630cb844a
62 changed files with 1137 additions and 3040 deletions
@@ -7,7 +7,7 @@ import { UpdateRoleDto } from '../dto/update-role.dto';
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 { RestId } from 'src/common/decorators';
import { } from 'src/common/decorators';
import { AdminId } from 'src/common/decorators/admin-id.decorator';
import { Permission } from 'src/common/enums/permission.enum';
@@ -27,7 +27,7 @@ export class RolesController {
@ApiBearerAuth()
@ApiOperation({ summary: 'Get all through restaurant roles with pagination and filters' })
findAll() {
return this.roleService.findAllGeneralAndRestaurantRoles(restId);
return this.roleService.findAllGeneralAndRestaurantRoles();
}
@Get('admin/roles/permissions')
@@ -36,7 +36,7 @@ export class RolesController {
@ApiBearerAuth()
@ApiOperation({ summary: 'Get all permissions that the admin has' })
async findAllPermissions(@AdminId() adminId: string,) {
const adminPermissionNames = await this.permissionService.getAdminFullPermissions(adminId, restId);
const adminPermissionNames = await this.permissionService.getAdminFullPermissions(adminId,);
return adminPermissionNames;
}
@@ -50,7 +50,7 @@ export class RolesController {
@ApiOperation({ summary: 'Create a new role' })
@ApiBody({ type: CreateRoleDto })
create(@Body() dto: CreateRoleDto,) {
return this.roleService.createRestaurantRole(dto, restId);
return this.roleService.createRestaurantRole(dto,);
}
@Get('admin/roles/:id')
@@ -59,7 +59,7 @@ export class RolesController {
@ApiBearerAuth()
@ApiOperation({ summary: 'Get a specific role by ID' })
findOne(@Param('id') id: string,) {
return this.roleService.findOne(restId, id);
return this.roleService.findOne(, id);
}
@Patch('admin/roles/:id')
@@ -69,7 +69,7 @@ export class RolesController {
@ApiOperation({ summary: 'Update a role' })
@ApiBody({ type: UpdateRoleDto })
update(@Param('id') id: string, @Body() dto: UpdateRoleDto,) {
return this.roleService.update(restId, id, dto);
return this.roleService.update(, id, dto);
}
@Delete('admin/roles/:id')
@@ -78,7 +78,7 @@ export class RolesController {
@ApiBearerAuth()
@ApiOperation({ summary: 'Delete a role' })
remove(@Param('id') id: string,) {
return this.roleService.remove(restId, id);
return this.roleService.remove(, id);
}
/** Super Admin Endpoints */
@@ -26,11 +26,11 @@ export class PermissionsService {
/**
* Get admin permissions from cache or database
* @param adminId - The admin ID
* @param restId - The restaurant ID
* @param - The restaurant ID
* @returns Array of permission names (string[])
*/
async getAdminPermissions(adminId: string, restId: string): Promise<string[]> {
const cacheKey = `${this.ADMIN_PERMISSIONS_KEY}:${adminId}:${restId}`;
async getAdminPermissions(adminId: string, : string): Promise<string[]> {
const cacheKey = `${this.ADMIN_PERMISSIONS_KEY}:${adminId}:${}`;
// Try to get from cache first
const cachedPermissions = await this.cacheService.get<string>(cacheKey);
@@ -49,7 +49,7 @@ export class PermissionsService {
// If not in cache, fetch from database
const admin = await this.adminRepository.findOne(
{ id: adminId, roles: { restaurant: { id: restId } } },
{ id: adminId, roles: { restaurant: { id: } } },
{ populate: ['roles', 'roles.role', 'roles.role.permissions'] },
);
@@ -77,14 +77,14 @@ export class PermissionsService {
return permissions.flat().map(p => p.name);
}
async getAdminFullPermissions(adminId: string, restId: string): Promise<Permission[]> {
async getAdminFullPermissions(adminId: string, : string): Promise<Permission[]> {
const listOfPermissions = []
const adminRoles = await this.em.findOne(AdminRole, {
admin: {
id: adminId,
},
restaurant: {
id: restId,
id: ,
},
}, { populate: ['role', 'role.permissions'] });
if (adminRoles) {
@@ -96,10 +96,10 @@ export class PermissionsService {
/**
* Invalidate admin permissions cache
* @param adminId - The admin ID
* @param restId - The restaurant ID
* @param - The restaurant ID
*/
async invalidateAdminPermissionsCache(adminId: string, restId: string): Promise<void> {
const cacheKey = `${this.ADMIN_PERMISSIONS_KEY}:${adminId}:${restId}`;
async invalidateAdminPermissionsCache(adminId: string, : string): Promise<void> {
const cacheKey = `${this.ADMIN_PERMISSIONS_KEY}:${adminId}:${}`;
await this.cacheService.del(cacheKey);
}
}
+12 -12
View File
@@ -19,18 +19,18 @@ export class RolesService {
private readonly em: EntityManager,
) { }
async createRestaurantRole(dto: CreateRoleDto, restId: string) {
async createRestaurantRole(dto: CreateRoleDto, : string) {
const { name, permissionIds } = dto;
// Check if role already exists
const existing = await this.roleRepository.findOne({ name, restaurant: restId ? { id: restId } : null });
const existing = await this.roleRepository.findOne({ name, restaurant: ? { id: } : null });
if (existing) {
throw new BadRequestException('Role with this name already exists for the restaurant');
}
let restaurant: Restaurant | null = null;
if (restId) {
restaurant = await this.em.findOne(Restaurant, { id: restId });
if () {
restaurant = await this.em.findOne(Restaurant, { id: });
if (!restaurant) {
throw new NotFoundException('Restaurant not found');
}
@@ -55,8 +55,8 @@ export class RolesService {
return role;
}
async findAllGeneralAndRestaurantRoles(restId: string) {
const where: FilterQuery<Role> = { $or: [{ restaurant: restId }, { restaurant: null }], isSystem: false };
async findAllGeneralAndRestaurantRoles(: string) {
const where: FilterQuery<Role> = { $or: [{ restaurant: }, { restaurant: null }], isSystem: false };
const roles = await this.roleRepository.find(where, {
orderBy: { createdAt: 'desc' },
@@ -66,9 +66,9 @@ export class RolesService {
return roles;
}
async findOne(restId: string, id: string) {
async findOne(: string, id: string) {
const role = await this.roleRepository.findOne(
{ id, restaurant: { id: restId } },
{ id, restaurant: { id: } },
{ populate: ['permissions', 'restaurant'] },
);
if (!role) {
@@ -77,9 +77,9 @@ export class RolesService {
return role;
}
async update(restId: string, id: string, dto: UpdateRoleDto) {
async update(: string, id: string, dto: UpdateRoleDto) {
const role = await this.roleRepository.findOne(
{ id, restaurant: { id: restId } },
{ id, restaurant: { id: } },
{ populate: ['permissions', 'restaurant'] },
);
if (!role) {
@@ -116,9 +116,9 @@ export class RolesService {
return roles;
}
async remove(restId: string, id: string) {
async remove(: string, id: string) {
const role = await this.roleRepository.findOne(
{ id, restaurant: { id: restId } },
{ id, restaurant: { id: } },
{ populate: ['permissions', 'restaurant'] },
);
if (!role) {