Add AdminId decorator for extracting adminId from requests; refactor AdminController to utilize new decorator for fetching admin details and restaurant-specific admins; update AdminService to handle admin creation and retrieval by restaurant; remove unused role and permission entities for cleaner architecture.
This commit is contained in:
@@ -4,6 +4,7 @@ import { BaseEntity } from '../../../common/entities/base.entity';
|
||||
import { Permission } from './permission.entity';
|
||||
import { Restaurant } from '../../restaurants/entities/restaurant.entity';
|
||||
import { RolePermission } from './rolePermission.entity';
|
||||
import { AdminRole } from 'src/modules/admin/entities/adminRole.entity';
|
||||
|
||||
@Entity({ tableName: 'roles' })
|
||||
export class Role extends BaseEntity {
|
||||
@@ -18,4 +19,7 @@ export class Role extends BaseEntity {
|
||||
|
||||
@ManyToOne(() => Restaurant, { nullable: true })
|
||||
restaurant?: Restaurant;
|
||||
|
||||
@OneToMany(() => AdminRole, adminRole => adminRole.role)
|
||||
admins = new Collection<AdminRole>(this);
|
||||
}
|
||||
|
||||
@@ -2,16 +2,71 @@ import { Injectable } from '@nestjs/common';
|
||||
import { InjectRepository } from '@mikro-orm/nestjs';
|
||||
import { EntityRepository } from '@mikro-orm/core';
|
||||
import { Permission } from '../entities/permission.entity';
|
||||
import { CacheService } from 'src/modules/utils/cache.service';
|
||||
import { AdminRepository } from 'src/modules/admin/repositories/admin.repository';
|
||||
|
||||
@Injectable()
|
||||
export class PermissionsService {
|
||||
private readonly ADMIN_PERMISSIONS_KEY = 'admin-perms';
|
||||
|
||||
constructor(
|
||||
@InjectRepository(Permission)
|
||||
private readonly permissionRepository: EntityRepository<Permission>,
|
||||
private readonly cacheService: CacheService,
|
||||
private readonly adminRepository: AdminRepository,
|
||||
) {}
|
||||
|
||||
async findAll() {
|
||||
const permissions = await this.permissionRepository.findAll();
|
||||
return permissions;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get admin permissions from cache or database
|
||||
* @param adminId - The admin ID
|
||||
* @param restId - 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}`;
|
||||
|
||||
// Try to get from cache first
|
||||
const cachedPermissions = await this.cacheService.get<string>(cacheKey);
|
||||
if (cachedPermissions) {
|
||||
try {
|
||||
const parsed: unknown = JSON.parse(cachedPermissions);
|
||||
// Ensure it's an array of strings
|
||||
if (Array.isArray(parsed) && parsed.every((p): p is string => typeof p === 'string')) {
|
||||
return parsed;
|
||||
}
|
||||
// If invalid format, continue to fetch from DB
|
||||
} catch {
|
||||
// If parsing fails, continue to fetch from DB
|
||||
}
|
||||
}
|
||||
|
||||
// If not in cache, fetch from database
|
||||
const admin = await this.adminRepository.findOne(
|
||||
{ id: adminId, roles: { restaurant: { id: restId } } },
|
||||
{ populate: ['roles', 'roles.role'] },
|
||||
);
|
||||
|
||||
if (!admin || !admin.roles) {
|
||||
return [];
|
||||
}
|
||||
|
||||
// Extract permission names as array of strings
|
||||
const permissions = admin.roles.map(r => r.role.permissions.getItems());
|
||||
return permissions.flat().map(p => p.name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Invalidate admin permissions cache
|
||||
* @param adminId - The admin ID
|
||||
* @param restId - The restaurant ID
|
||||
*/
|
||||
async invalidateAdminPermissionsCache(adminId: string, restId: string): Promise<void> {
|
||||
const cacheKey = `${this.ADMIN_PERMISSIONS_KEY}:${adminId}:${restId}`;
|
||||
await this.cacheService.del(cacheKey);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,12 +7,11 @@ import { Role } from './entities/role.entity';
|
||||
import { Permission } from './entities/permission.entity';
|
||||
import { RolePermission } from './entities/rolePermission.entity';
|
||||
import { JwtModule } from '@nestjs/jwt';
|
||||
import { UtilsModule } from '../utils/utils.module';
|
||||
import { AdminModule } from '../admin/admin.module';
|
||||
|
||||
@Module({
|
||||
imports: [
|
||||
MikroOrmModule.forFeature([Role, Permission, RolePermission]),
|
||||
JwtModule,
|
||||
],
|
||||
imports: [MikroOrmModule.forFeature([Role, Permission, RolePermission]), JwtModule, UtilsModule, AdminModule],
|
||||
controllers: [RolesController],
|
||||
providers: [RolesService, PermissionsService],
|
||||
exports: [RolesService, PermissionsService],
|
||||
|
||||
Reference in New Issue
Block a user