This commit is contained in:
2025-11-23 14:48:58 +03:30
parent 7dd1b61cac
commit a3f0593443
4 changed files with 48 additions and 13 deletions
+2 -1
View File
@@ -10,12 +10,13 @@ import { RolePermission } from '../roles/entities/rolePermission.entity';
import { AdminController } from './controllers/admin.controller'; import { AdminController } from './controllers/admin.controller';
import { UtilsModule } from '../utils/utils.module'; import { UtilsModule } from '../utils/utils.module';
import { RestaurantsModule } from '../restaurants/restaurants.module'; import { RestaurantsModule } from '../restaurants/restaurants.module';
import { AdminRole } from './entities/adminRole.entity';
@Module({ @Module({
providers: [AdminService, AdminRepository], providers: [AdminService, AdminRepository],
controllers: [AdminController], controllers: [AdminController],
imports: [ imports: [
MikroOrmModule.forFeature([Admin, Role, Permission, RolePermission]), MikroOrmModule.forFeature([Admin, Role, Permission, RolePermission, AdminRole]),
JwtModule, JwtModule,
UtilsModule, UtilsModule,
RestaurantsModule, RestaurantsModule,
@@ -1,4 +1,4 @@
import { Controller, Post, Body, HttpCode, HttpStatus, Get, UseGuards, Patch, Param } from '@nestjs/common'; import { Controller, Post, Body, HttpCode, HttpStatus, Get, UseGuards, Patch, Param, Delete } from '@nestjs/common';
import { AdminService } from '../providers/admin.service'; import { AdminService } from '../providers/admin.service';
import { CreateAdminDto } from '../dto/create-admin.dto'; import { CreateAdminDto } from '../dto/create-admin.dto';
import { UpdateAdminDto } from '../dto/update-admin.dto'; import { UpdateAdminDto } from '../dto/update-admin.dto';
@@ -26,8 +26,8 @@ export class AdminController {
@Get('profile') @Get('profile')
@ApiOperation({ summary: 'admin' }) @ApiOperation({ summary: 'admin' })
@ApiResponse({ status: 201, description: 'Admins' }) @ApiResponse({ status: 201, description: 'Admins' })
async getMe(@AdminId() adminId: string) { async getMe(@AdminId() adminId: string, @RestId() restId: string) {
const admin = await this.adminService.findById(adminId); const admin = await this.adminService.findById(adminId, restId);
return admin; return admin;
} }
@@ -56,4 +56,20 @@ export class AdminController {
update(@Param('id') id: string, @Body() dto: UpdateAdminDto, @RestId() restId: string): Promise<Admin> { update(@Param('id') id: string, @Body() dto: UpdateAdminDto, @RestId() restId: string): Promise<Admin> {
return this.adminService.update(id, restId, dto); return this.adminService.update(id, restId, dto);
} }
@Get(':adminId')
@ApiOperation({ summary: 'Get an admin by ID' })
@ApiParam({ name: 'id', description: 'Admin ID' })
@ApiResponse({ status: 200, description: 'Admin found successfully' })
@ApiResponse({ status: 404, description: 'Admin not found' })
getById(@Param('adminId') adminId: string, @RestId() restId: string): Promise<Admin | null> {
return this.adminService.findById(adminId, restId);
}
@Delete(':adminId')
@ApiOperation({ summary: 'Delete an admin by ID' })
@ApiParam({ name: 'id', description: 'Admin ID' })
@ApiResponse({ status: 200, description: 'Admin deleted successfully' })
@ApiResponse({ status: 404, description: 'Admin not found' })
deleteById(@Param('adminId') adminId: string, @RestId() restId: string): Promise<void> {
return this.adminService.remove(adminId, restId);
}
} }
@@ -5,7 +5,7 @@ import { Restaurant } from 'src/modules/restaurants/entities/restaurant.entity';
import { Admin } from './admin.entity'; import { Admin } from './admin.entity';
@Entity({ tableName: 'admin_roles' }) @Entity({ tableName: 'admin_roles' })
@Unique({ properties: ['admin', 'role', 'restaurant'] }) @Unique({ properties: ['admin', 'restaurant'] })
export class AdminRole extends BaseEntity { export class AdminRole extends BaseEntity {
@ManyToOne(() => Admin) @ManyToOne(() => Admin)
admin!: Admin; admin!: Admin;
+26 -8
View File
@@ -1,4 +1,4 @@
import { Injectable, NotFoundException } from '@nestjs/common'; import { ConflictException, Injectable, NotFoundException } from '@nestjs/common';
import { InjectRepository } from '@mikro-orm/nestjs'; import { InjectRepository } from '@mikro-orm/nestjs';
import { EntityRepository } from '@mikro-orm/core'; import { EntityRepository } from '@mikro-orm/core';
import { Admin } from '../entities/admin.entity'; import { Admin } from '../entities/admin.entity';
@@ -15,6 +15,8 @@ export class AdminService {
constructor( constructor(
@InjectRepository(Admin) @InjectRepository(Admin)
private readonly adminRepository: EntityRepository<Admin>, private readonly adminRepository: EntityRepository<Admin>,
@InjectRepository(AdminRole)
private readonly adminRoleRepository: EntityRepository<AdminRole>,
private readonly em: EntityManager, private readonly em: EntityManager,
private readonly cacheService: CacheService, private readonly cacheService: CacheService,
) {} ) {}
@@ -23,8 +25,11 @@ export class AdminService {
return this.adminRepository.findOne({ phone }); return this.adminRepository.findOne({ phone });
} }
async findById(id: string): Promise<Admin | null> { async findById(adminId: string, restId: string): Promise<Admin | null> {
const admin = await this.adminRepository.findOne({ id }, { populate: ['roles', 'roles.role'] }); const admin = await this.adminRepository.findOne(
{ id: adminId, roles: { restaurant: { id: restId } } },
{ populate: ['roles', 'roles.role'] },
);
return admin; return admin;
} }
@@ -36,10 +41,10 @@ export class AdminService {
const { phone, firstName, lastName, roleId } = dto; const { phone, firstName, lastName, roleId } = dto;
const currentAdmin = await this.adminRepository.findOne({ const currentAdmin = await this.adminRepository.findOne({
phone, phone,
roles: { role: { id: roleId }, restaurant: { id: restId } }, roles: { restaurant: { id: restId } },
}); });
if (currentAdmin) { if (currentAdmin) {
return currentAdmin; throw new ConflictException('This Phone Number is already Admin for this restaurant');
} }
const role = await this.em.findOne(Role, { id: roleId }); const role = await this.em.findOne(Role, { id: roleId });
if (!role) throw new Error('Role not found'); if (!role) throw new Error('Role not found');
@@ -77,8 +82,11 @@ export class AdminService {
return admin; return admin;
} }
async update(id: string, restId: string, dto: UpdateAdminDto): Promise<Admin> { async update(adminId: string, restId: string, dto: UpdateAdminDto): Promise<Admin> {
const admin = await this.adminRepository.findOne({ id }, { populate: ['roles', 'roles.role', 'roles.restaurant'] }); const admin = await this.adminRepository.findOne(
{ id: adminId, roles: { restaurant: { id: restId } } },
{ populate: ['roles', 'roles.role', 'roles.restaurant'] },
);
if (!admin) { if (!admin) {
throw new NotFoundException('Admin not found'); throw new NotFoundException('Admin not found');
@@ -111,7 +119,7 @@ export class AdminService {
// Find existing AdminRole for this admin and restaurant // Find existing AdminRole for this admin and restaurant
const existingAdminRole = await this.em.findOne(AdminRole, { const existingAdminRole = await this.em.findOne(AdminRole, {
admin: { id }, admin: { id: adminId },
restaurant: { id: restId }, restaurant: { id: restId },
}); });
@@ -134,4 +142,14 @@ export class AdminService {
await this.em.persistAndFlush(admin); await this.em.persistAndFlush(admin);
return admin; return admin;
} }
async remove(adminId: string, restId: string): Promise<void> {
const adminRole = await this.adminRoleRepository.findOne({ admin: { id: adminId }, restaurant: { id: restId } });
if (!adminRole) {
throw new NotFoundException('Admin role not found');
}
await this.em.nativeDelete(AdminRole, { id: adminRole.id });
await this.em.persistAndFlush(adminRole);
}
} }