roles
This commit is contained in:
@@ -1,13 +1,12 @@
|
||||
import { Injectable, NotFoundException, BadRequestException } from '@nestjs/common';
|
||||
import { InjectRepository } from '@mikro-orm/nestjs';
|
||||
import { EntityRepository } from '@mikro-orm/core';
|
||||
import { EntityRepository, FilterQuery } from '@mikro-orm/core';
|
||||
import { Role } from '../entities/role.entity';
|
||||
import { Permission } from '../entities/permission.entity';
|
||||
import { Restaurant } from '../../restaurants/entities/restaurant.entity';
|
||||
import { EntityManager } from '@mikro-orm/postgresql';
|
||||
import { CreateRoleDto } from '../dto/create-role.dto';
|
||||
import { UpdateRoleDto } from '../dto/update-role.dto';
|
||||
import { FindRolesDto } from '../dto/find-roles.dto';
|
||||
import { RolePermission } from '../entities/rolePermission.entity';
|
||||
|
||||
@Injectable()
|
||||
@@ -20,7 +19,7 @@ export class RolesService {
|
||||
private readonly em: EntityManager,
|
||||
) {}
|
||||
|
||||
async create(restId: string, dto: CreateRoleDto) {
|
||||
async createRestaurantRole(dto: CreateRoleDto, restId: string) {
|
||||
const { name, permissionIds } = dto;
|
||||
|
||||
// Check if role already exists
|
||||
@@ -51,7 +50,7 @@ export class RolesService {
|
||||
// Check if any permission is special
|
||||
const specialPermissions = permissions.filter(p => p.group === 'special');
|
||||
if (specialPermissions.length > 0) {
|
||||
throw new BadRequestException('Special permissions cannot be assigned to roles');
|
||||
throw new BadRequestException('Special permissions cannot be assigned');
|
||||
}
|
||||
permissions.forEach(p => role.permissions.add(p));
|
||||
}
|
||||
@@ -60,35 +59,15 @@ export class RolesService {
|
||||
return role;
|
||||
}
|
||||
|
||||
async findAll(restId: string, dto: FindRolesDto) {
|
||||
const { name, page = 1, limit = 20 } = dto;
|
||||
const offset = (page - 1) * limit;
|
||||
async findAllGeneralAndRestaurantRoles(restId: string) {
|
||||
const where: FilterQuery<Role> = { $or: [{ restaurant: restId }, { restaurant: null }] };
|
||||
|
||||
// Build query dynamically
|
||||
const query: Record<string, unknown> = restId ? { restaurant: { id: restId } } : {};
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
|
||||
const [roles, count] = await this.roleRepository.findAndCount(query as any, {
|
||||
offset,
|
||||
limit,
|
||||
const roles = await this.roleRepository.find(where, {
|
||||
orderBy: { createdAt: 'desc' },
|
||||
populate: ['permissions', 'restaurant'],
|
||||
});
|
||||
|
||||
// Filter by name in memory if provided
|
||||
let filtered = roles;
|
||||
if (name) {
|
||||
filtered = roles.filter(
|
||||
r => r.name.toLowerCase().includes(name.toLowerCase()),
|
||||
);
|
||||
}
|
||||
|
||||
return {
|
||||
data: filtered,
|
||||
total: count,
|
||||
page,
|
||||
limit,
|
||||
totalPages: Math.ceil(count / limit),
|
||||
};
|
||||
return roles;
|
||||
}
|
||||
|
||||
async findOne(restId: string, id: string) {
|
||||
|
||||
Reference in New Issue
Block a user