Refactor admin module structure: moved services and controllers to providers and controllers directories, added PermissionService and PermissionController, updated Admin entity relationships, and created AdminRole entity.
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { AdminService } from './admin.service';
|
||||
import { AdminService } from './providers/admin.service';
|
||||
import { MikroOrmModule } from '@mikro-orm/nestjs';
|
||||
import { Admin } from './entities/admin.entity';
|
||||
import { JwtModule } from '@nestjs/jwt';
|
||||
@@ -7,14 +7,16 @@ import { AdminRepository } from './repositories/rest.repository';
|
||||
import { Role } from './entities/role.entity';
|
||||
import { Permission } from './entities/permission.entity';
|
||||
import { RolePermission } from './entities/rolePermission.entity';
|
||||
import { AdminController } from './admin.controller';
|
||||
import { AdminController } from './controllers/admin.controller';
|
||||
import { RoleService } from './providers/role.service';
|
||||
import { RoleController } from './controllers/role.controller';
|
||||
import { PermissionService } from './providers/permission.service';
|
||||
import { PermissionController } from './controllers/permission.controller';
|
||||
|
||||
@Module({
|
||||
providers: [AdminService, AdminRepository, RoleService],
|
||||
controllers: [AdminController, RoleController],
|
||||
providers: [AdminService, AdminRepository, RoleService, PermissionService],
|
||||
controllers: [AdminController, RoleController, PermissionController],
|
||||
imports: [MikroOrmModule.forFeature([Admin, Role, Permission, RolePermission]), JwtModule],
|
||||
exports: [AdminService, AdminRepository, RoleService],
|
||||
exports: [AdminService, AdminRepository, RoleService, PermissionService],
|
||||
})
|
||||
export class AdminModule {}
|
||||
|
||||
+2
-2
@@ -1,6 +1,6 @@
|
||||
import { Controller, Post, Body, HttpCode, HttpStatus, Get } from '@nestjs/common';
|
||||
import { AdminService } from './admin.service';
|
||||
import { CreateAdminDto } from './dto/create-admin.dto';
|
||||
import { AdminService } from '../providers/admin.service';
|
||||
import { CreateAdminDto } from '../dto/create-admin.dto';
|
||||
import { ApiTags, ApiOperation, ApiBody, ApiResponse } from '@nestjs/swagger';
|
||||
|
||||
@ApiTags('admin')
|
||||
@@ -0,0 +1,17 @@
|
||||
import { Controller, Get } from '@nestjs/common';
|
||||
import { ApiTags, ApiOperation, ApiResponse } from '@nestjs/swagger';
|
||||
import { PermissionService } from '../providers/permission.service';
|
||||
|
||||
@ApiTags('admin/permissions')
|
||||
@Controller('admin/permissions')
|
||||
export class PermissionController {
|
||||
constructor(private readonly permissionService: PermissionService) {}
|
||||
|
||||
@Get()
|
||||
@ApiOperation({ summary: 'Get all permissions' })
|
||||
@ApiResponse({ status: 200, description: 'Permissions retrieved successfully' })
|
||||
async findAll() {
|
||||
return this.permissionService.findAll();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { Entity, ManyToOne, OneToOne, Property } from '@mikro-orm/core';
|
||||
import { Entity, ManyToOne, Property } from '@mikro-orm/core';
|
||||
import { BaseEntity } from '../../../common/entities/base.entity';
|
||||
import { Restaurant } from '../../../modules/restaurants/entities/restaurant.entity';
|
||||
import { Role } from './role.entity';
|
||||
import { Restaurant } from '../../restaurants/entities/restaurant.entity';
|
||||
|
||||
@Entity({ tableName: 'admins' })
|
||||
// Add the Unique constraint for the combination of 'phone' and 'restaurant'
|
||||
@@ -13,13 +13,13 @@ export class Admin extends BaseEntity {
|
||||
@Property({ nullable: true })
|
||||
lastName?: string;
|
||||
|
||||
@Property()
|
||||
@Property({ unique: true })
|
||||
phone!: string;
|
||||
|
||||
// Add the new role property
|
||||
@ManyToOne(() => Role)
|
||||
role!: Role;
|
||||
|
||||
@OneToOne(() => Restaurant, { owner: true })
|
||||
restaurant!: Restaurant;
|
||||
@ManyToOne(() => Restaurant, { nullable: true })
|
||||
restaurant?: Restaurant;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
import { Entity, ManyToOne } from '@mikro-orm/core';
|
||||
import { BaseEntity } from '../../../common/entities/base.entity';
|
||||
import { Role } from './role.entity';
|
||||
import { Restaurant } from 'src/modules/restaurants/entities/restaurant.entity';
|
||||
import { Admin } from './admin.entity';
|
||||
|
||||
@Entity({ tableName: 'admin_roles' })
|
||||
// Add the Unique constraint for the combination of 'phone' and 'restaurant'
|
||||
// @Unique({ properties: ['phone', 'restaurant'] })
|
||||
export class AdminRole extends BaseEntity {
|
||||
@ManyToOne(() => Admin)
|
||||
admin!: Admin;
|
||||
|
||||
@ManyToOne(() => Role)
|
||||
role!: Role;
|
||||
|
||||
@ManyToOne(() => Restaurant, { nullable: true })
|
||||
restaurant?: Restaurant;
|
||||
}
|
||||
@@ -1,12 +1,13 @@
|
||||
// role.entity.ts
|
||||
import { Collection, Entity, ManyToMany, OneToMany, OneToOne, Property } from '@mikro-orm/core';
|
||||
import { Collection, Entity, ManyToMany, OneToMany, ManyToOne, Property } from '@mikro-orm/core';
|
||||
import { BaseEntity } from '../../../common/entities/base.entity';
|
||||
import { Admin } from './admin.entity';
|
||||
import { Permission } from './permission.entity';
|
||||
import { Restaurant } from '../../../modules/restaurants/entities/restaurant.entity';
|
||||
import { RolePermission } from './rolePermission.entity';
|
||||
import { AdminRole } from './adminRole.entity';
|
||||
|
||||
@Entity()
|
||||
@Entity({ tableName: 'roles' })
|
||||
export class Role extends BaseEntity {
|
||||
@Property()
|
||||
name!: string;
|
||||
@@ -17,6 +18,9 @@ export class Role extends BaseEntity {
|
||||
@OneToMany(() => Admin, admin => admin.role)
|
||||
admins = new Collection<Admin>(this);
|
||||
|
||||
@OneToOne(() => Restaurant, { owner: true, nullable: true })
|
||||
@ManyToOne(() => Restaurant, { nullable: true })
|
||||
restaurant?: Restaurant;
|
||||
|
||||
@OneToMany(() => AdminRole, adminRoleRestaurant => adminRoleRestaurant.role)
|
||||
adminRoles = new Collection<AdminRole>(this);
|
||||
}
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { InjectRepository } from '@mikro-orm/nestjs';
|
||||
import { EntityRepository } from '@mikro-orm/core';
|
||||
import { Admin } from './entities/admin.entity';
|
||||
import { Role } from './entities/role.entity';
|
||||
import { Restaurant } from '../restaurants/entities/restaurant.entity';
|
||||
import { Admin } from '../entities/admin.entity';
|
||||
import { Role } from '../entities/role.entity';
|
||||
import { Restaurant } from '../../restaurants/entities/restaurant.entity';
|
||||
import { EntityManager } from '@mikro-orm/postgresql';
|
||||
|
||||
@Injectable()
|
||||
@@ -0,0 +1,18 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { InjectRepository } from '@mikro-orm/nestjs';
|
||||
import { EntityRepository } from '@mikro-orm/core';
|
||||
import { Permission } from '../entities/permission.entity';
|
||||
|
||||
@Injectable()
|
||||
export class PermissionService {
|
||||
constructor(
|
||||
@InjectRepository(Permission)
|
||||
private readonly permissionRepository: EntityRepository<Permission>,
|
||||
) {}
|
||||
|
||||
async findAll() {
|
||||
const permissions = await this.permissionRepository.findAll();
|
||||
return permissions;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user